Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Reverse complement from a file

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)    


>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)   
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading