Question: Difficulty in python while loop
0
gravatar for KVC_bioinfo
2.8 years ago by
KVC_bioinfo510
Boston
KVC_bioinfo510 wrote:

Hello All,

I wrote following python code:

pos = 100
cigar = [(0, 150), (3, 50), (0, 130), (2, 270), (0, 750),(2,50),(3, 70), (0,500)]
result=list()
for i in cigar:
    while i[0] == 0:
        if i == 3:
            break
        end = pos + i[1]
        item = [i[0], pos, end]
        #print item
        result.append(item)
        pos=end

matches=list()
for i in result:
  if i[0]<1:
    matches.append([i[1], i[2]])
#print matches

Here I am trying to generate an output which will give me:

[100, 100+150]  i.e [100, 250] and so on

[100+150+50, 100+150+50+130+270+750+50]

[100+150+50+130+270+750+50+70, 100+150+50+130+270+750+50+70+500]

So basically starting from the pos which are equal to 100: it should keep adding the second value from each cigar tuple (0, 150) until a cigar tuple come where the first value is 3 (3, 50) and keep doing it until the end of cigar list.

I have used while loop for this. However, there is something wrong with the loop because it does not give the desired output and it never breaks. Could anyone figure out what am I doing wrong here?

Thank you very much in advance.

whileloop python • 1.0k views
ADD COMMENTlink modified 2.8 years ago • written 2.8 years ago by KVC_bioinfo510
1

Well, if you say "So basically starting from the pos which are equal to 100" that makes no sense to me, because I can not see 100 in your data at all. However, talking about your loop..... It is not surprising, that it never breaks: You check for i equals 3. However, i is the elements of "cigar" which are (0,50), (3, 50) .... etc, but NEVER 3 .... just add a print i after "for i in cigar:" and you will see the value of i. It can not be 3, ever.

ADD REPLYlink written 2.8 years ago by LLTommy1.2k

100 is the "pos" which are the first starting point

ADD REPLYlink written 2.8 years ago by KVC_bioinfo510

Ah sorry, I was too focused on the 'cigar'. Anyway, I hope you understood the point about not reaching the breaking point, as Devon described in more detail below.

ADD REPLYlink written 2.8 years ago by LLTommy1.2k

However, there is something wrong with the loop because it does not give the desired output and it never breaks

What do you get?

ADD REPLYlink written 2.8 years ago by st.ph.n2.5k
2
gravatar for Devon Ryan
2.8 years ago by
Devon Ryan96k
Freiburg, Germany
Devon Ryan96k wrote:

i == 3 can never be true, because i is a tuple. You'll want to test i[0] == 3 instead. Anyway, below is presumably what you were going for:

pos = 100
end = 0
cigar = [(0, 150), (3, 50), (0, 130), (2, 270), (0, 750),(2,50),(3, 70), (0,500)]
results = list()
for op, oplen in cigar:
    if op == 3:
        # Flush the memoized values
        results.append([pos, end])
        pos = end + oplen
        end = pos
    else:
        end += oplen

# It's not entirely clear what should happen if the last CIGAR op is a 3
results.append([pos, end])
for tup in results:
    print(tup)
ADD COMMENTlink written 2.8 years ago by Devon Ryan96k
cigar =  [(0, 150),(3, 50), (0, 130), (2, 270), (0, 750),(2,50),(3, 70), (0,500)]

Starting from 100 which is "pos" in the code the output should look like:

[100, 100+150]

[100+150+50, 100+150+50+130+270+750+50]

[100+150+50+130+270+750+50+70, 100+150+50+130+270+750+50+70+500]

This means it should stop once it hits 3 and add all values before that.

ADD REPLYlink modified 2.8 years ago by genomax89k • written 2.8 years ago by KVC_bioinfo510

Right, that's what it's currently doing.

ADD REPLYlink written 2.8 years ago by Devon Ryan96k

No, output from the code you suggested is

[100, 150]
[200, 1400]
[1470, 1970]

However it is not what I want.

ADD REPLYlink written 2.8 years ago by KVC_bioinfo510

I edited the post with my desired output.

ADD REPLYlink written 2.8 years ago by KVC_bioinfo510

So end = 100 rather than end = 0 on the second line.

ADD REPLYlink written 2.8 years ago by Devon Ryan96k

No that will mess with entire for loop below : it gives the following output when I have end = 100

[100, 250]
[300, 1500]
[1570, 2070]
ADD REPLYlink written 2.8 years ago by KVC_bioinfo510

Those are the exact values you specified earlier.

ADD REPLYlink written 2.8 years ago by Devon Ryan96k
[100, 100+150]

[100+150+50, 100+150+50+130+270+750+50]

[100+150+50+130+270+750+50+70, 100+150+50+130+270+750+50+70+500]

These are the values I am trying to get in the output.

ADD REPLYlink written 2.8 years ago by KVC_bioinfo510

Presumably meaning you don't want the values actually added, but just separated by a plus. It'd be useful if you very explicitly stated if that's the case, since normally people would read 100+150 as meaning, "I want those two values to be summed such that 250 is the second element of the list".

ADD REPLYlink written 2.8 years ago by Devon Ryan96k

Yes, I want those values to be summed. I had them like 100+150 so that people could understand what am I adding to get the final value.\

Sorry for not clarifying it earlier.

ADD REPLYlink written 2.8 years ago by KVC_bioinfo510
pos = [10, 100, 1000]

cigar = [[(0, 150), (3, 50), (0, 130), (3, 270), **(0, 750),(2,50),(1, 70), (0,500)**], [(0, 250), (3, 250), (0, 160), (3, 270), (0, 750)], [(0, 150), (3, 150), (0, 130), (3, 250), (0, 950)]] 

def find (cigar, pos):
    matches=list()
    result=list()
    end = 0
    for i in cigar:
        if i[0] == 1:
            continue    
        end = pos + i[1]
        item = [i[0], pos, end]
        result.append(item)
        pos=end

    #print result           
    for i in result:
        if i[0] == 3:

            matches.append([i[1], i[2]])

    return matches


for x, y in zip(cigar, pos):
    #print x, y
    res = find(x, y)
    print res

This gives me output however it stops the calculation for bold part (**). I want it to continue.

ADD REPLYlink modified 2.8 years ago • written 2.8 years ago by KVC_bioinfo510

Could anyone help me to figure this out?

ADD REPLYlink written 2.8 years ago by KVC_bioinfo510
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1461 users visited in the last hour