Reverse complement from a file

Question:

The task is: Write a script (call it what you want) that that can analyze a fastafile (MySequences.fasta) by finding the reverse complement of the sequences. Using python.

from itertools import repeat

#opening file

filename = "MySequences.fasta"
file = open(filename, 'r')

#reading the file

for line in file:
    line = line.strip()
    if ">" in line:
        header = line
    elif (len(line) == 0):
        continue
    else:
        seq = line

#reverse complement

def reverse_complement(seq):
    compline = ''
    for n in seq:
        if n == 'A':
            compline += 'T'
        elif n == 'T':
            compline += 'A'
        elif n == 'C':
            compline += 'G'
        elif n == 'G':
            compline += 'C'
    return((compline)[::-1])

#run each line

for line in file:
    rc = reverse_complement(seq)
    print(rc)    


Answers:

You run your function in the wrong place.
To run your function for each iterator, put the function there.

#reading the file

for line in file:
    line = line.strip()
    if ">" in line:
        header = line
    elif (len(line) == 0):
        continue
    else:
        seq = line
        #run function for each line, each time.
        rc = reverse_complement(seq)
        print(rc)

In your previous code, all iteration is successful. But you didn’t put the line to the function to run each time. In your previous code, after all, iterations, only the last line is assigned. Therefore you put the last line to the function at the end. This is why your code prints only one line.

The solution.

from itertools import repeat

#reverse complement

def reverse_complement(seq):
    compline = ''
    for n in seq:
        if n == 'A':
            compline += 'T'
        elif n == 'T':
            compline += 'A'
        elif n == 'C':
            compline += 'G'
        elif n == 'G':
            compline += 'C'
    return((compline)[::-1])


#opening file

filename = "MySequences.fasta"
file = open(filename, 'r')


#reading the file

for line in file:
    line = line.strip()
    if ">" in line:
        header = line
    elif (len(line) == 0):
        continue
    else:
        seq = line
        #run each line
        rc = reverse_complement(seq)
        print(rc)    

Also, this is your other mistake.
You put seq as input instead line.
But even if you fix this, this code won’t work for the same reason I told you before.

for line in file:
    rc = reverse_complement(seq)
    print(rc)   
Answered By: Constantin Hong
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.