iterating through list of tuples #python
1
0
Entering edit mode
7.0 years ago
KVC_bioinfo ▴ 600

Hello all,

pos = [66905851, 66905852]
cigar = [[(0, 117), (3, 29773), (0, 253), (3, 1325), (0, 145)], [(0, 116), (2, 1), (3, 3419), (0, 327), (3, 21529), (0, 286), (2, 1), (0, 1), (3, 4210), (0, 253), (3, 1325), (0, 145)]]

new = []    
for p in cigar:
    for k,v in enumerate(p):
        for i , j in enumerate(p[:]):
            for num in pos:
                if v == 0:
                    new.append(j+num)

I am working on above code. In which pos contains numbers and cigar contains two lists of tuples which are to be used for the two numbers in pos respectively.

I am trying to add the second number of each tuple (for eg 117 in (0,177)) in the first number of pos (for eg 66905851) if a first number of the tuple is zero.

Desired out put: [66905851, 66905851+117]

[66905851+117, 66905851+117+ 29773]

[66905851+117+ 29773, 66905851+117+ 29773+253]

and so on for the first number 66905851. and for 66905852 the code should use values from the second list of the tuple from the cigar list.

[(0, 116), (2, 1), (3, 3419), (0, 327), (3, 21529), (0, 286), (2, 1), (0, 1), (3, 4210), (0, 253), (3, 1325), (0, 145)]

My code is not giving me desired output. Could someone help me here? Thank you in advance.

python • 9.7k views
ADD COMMENT
1
Entering edit mode

Kindly add an example of desired output. I am looking for the format you need. I can quickly help

ADD REPLY
0
Entering edit mode

Hello Vijay, I have edited the original post with the desired output. I appreciate your help. Thank you.

ADD REPLY
1
Entering edit mode

Help me in understanding the issue. In OP, pos has two values and cigar is a tuple with two vales for each position. If first value of cigar is 0, then add 2nd value to pos (each pos). Is this correct? In that case, why 29773 is added to pos when first the value is not 0 and that too multiple times, in output expected? copy/pasted from OP:

I am trying to add the second number of each tuple (for eg 117 in (0,177)) in the first number of pos (for eg 66905851) if a first number of the tuple is zero.

Based on above logic, my output is as follows:

66905851+117
66905851+253
66905851+145
66905852+116
66905852+327
66905852+286
66905852+1
66905852+253
66905852+145
ADD REPLY
0
Entering edit mode

I have added 29773 because I am trying to get genomic coordinates from iterating through the cigar. The next match found is after 29773 so my next alignment coordinate will be after 29773.

ADD REPLY
3
Entering edit mode
7.0 years ago

Here is a solution, I hope this helps

pos = [66905851, 66905852]
cigar = [[(0, 117), (3, 29773), (0, 253), (3, 1325), (0, 145)], [(0, 116), (2, 1), (3, 3419), (0, 327), (3, 21529), (0, 286), (2, 1), (0, 1), (3, 4210), (0, 253), (3, 1325), (0, 145)]]
final1,final2,c,d=[],[],[],[]

for i in range(0,len(cigar)):
    for j in range(0,len(cigar[i])):
        for k in range(0,len(cigar[i][j])):
            if i==0 and k==1:
                # all second numbers of first list is stored in list named c
                c.append(cigar[i][j][k])
            if i==1 and k==1:
                # all second numbers of second list is stored in list named d
                d.append(cigar[i][j][k])

for p in pos:
    if p==66905851:
        final1.append(p)
        for num in c:
            p=p+num
            final1.append(p)
    if p==66905852:
       final2.append(p)
       for num in d:
           p=p+num
           final2.append(p)

print final1, final2

final result

[66905851, 66905968, 66935741, 66935994, 66937319, 66937464]

[66905852, 66905968, 66905969, 66909388, 66909715, 66931244, 66931530, 66931531, 66931532, 66935742, 66935995, 66937320, 66937465]
ADD COMMENT
0
Entering edit mode

Thank you very much, Vijay. This is pretty much what I wanted to do. The only issue is in the above example I have shown part of the lists. The original lists are huge. In that case making final1,final2,c,d=[],[],[],[] manually might be tedious. I will try to find a solution for that.

Thank you so much again. I really appreciate your help.

ADD REPLY

Login before adding your answer.

Traffic: 2799 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6