0

I have the following code where frag is a list of strings which are cut up (in order) DNA sequence data:

for a in frag:
    length_fragment = len(a)
    if  (a[0:5] == 'CCAGC')       and (a[-1:] == 'C'):
        total_length.append(length_fragment) 

I however want to jump ahead to the next a in the for loop and see if the first letters of that next fragment are CCAGC... is this possible in python to do.

So I want to change the a[-1:] =='C' to be a statment which is the next a[0:5] =='ACGAG'. Key word there is the next a in the for loop. So I want to skip ahead briefly in the for loop.

1
  • I have no idea what you're trying to say. Could you show sample input, the corresponding output, and talk through the algorithm? Commented May 2, 2012 at 3:59

3 Answers 3

1
for a, next_a in zip(frag, frag[1:]):

If frag is large, it will be more efficient to use an itertools.islice instead of [1:]

Sign up to request clarification or add additional context in comments.

Comments

1

Use continue to skip the rest of the for loop and restart at the beginning with the next iteration.

Comments

0

(I'm not 100% clear on your intent, so I'll interpret: you want to find sequences that begin with CCAGC, but only if the following sequence begins with ACGAG. On that assumption...)

If it's convenient, store the data as a single string containing all the sequences, one per line, then use a regex:

ccagc_then_acgag = re.compile('(CCAGC.*)\n(?=ACGAG)')
sum( len(seq) for seq in ccagc_then_acgag.findall(sequences) )

I can't say whether this will be faster or slower than iterating over a list of strings (regex libraries have some nice optimisations and the entire loop runs in native code, but the list of strings has the advantage of not having to scan an entire line to find the ACGAG match), but it's worth testing.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.