user3490528
user3490528

Reputation: 1

Function that makes the reverse compliment in a dna sequence

I am trying to make a function to run in a program that I already have. I am making the reverse compliment in a dna sequence. Here is what I currently have.

for line in infile:
    line = line.strip()
    if line[0] == '>':
            outfile.write(line+'\n')
    else:
            line = line.upper().replace(' ','')
            if re.search('[^ACTG]', line) is None:
                    line = re.sub('A', 'F', line)
                    line = re.sub('T', 'A', line)
                    line = re.sub('F', 'T', line)
                    line = re.sub('G', 'Y', line)
                    line = re.sub('C', 'G', line)
                    line = re.sub('Y', 'C', line)
                    line = line[::-1]
                    outfile.write(line+'\n')
            else:
                    outfile.write('ERROR'+'\n')

How can I add a function into this program using a format like

def codon(infile):
      for line in infile:
return something

This is probably pretty simple I'm new to this. Thanks.

Upvotes: 0

Views: 275

Answers (4)

Sam
Sam

Reputation: 1206

To convert the current code you have into a function that returns the result (in addition to writing out to the file) you can use the following:

def codon(infile):
    all_lines = []
    for line in infile:
        line = line.strip()
        if line[0] == '>':
                pass
        else:
                line = line.upper().replace(' ','')
                if re.search('[^ACTG]', line) is None:
                        line = re.sub('A', 'F', line)
                        line = re.sub('T', 'A', line)
                        line = re.sub('F', 'T', line)
                        line = re.sub('G', 'Y', line)
                        line = re.sub('C', 'G', line)
                        line = re.sub('Y', 'C', line)
                        line = line[::-1]
                else:
                        line = 'ERROR'
         all_lines.append(line)
         outfile.write(line + '\n')
    return all_lines

which will give you a list with all your lines after processing as well as write out. I also restructured to only write out at the end of your logic. Since you probably don't want '\n' in your results, I only add it at the very end when it is output with outfile. When appending the line to your all_lines list however, you likely don't need the '\n'.

The pass line just tells python you don't want to do anything interesting in that case. You could just negate your test and only process what is in the else clause a la:

def codon(infile):
    for line in infile:
        line = line.strip()
        if line[0] != '>':
                line = line.upper().replace(' ', '')
                ...

Upvotes: 1

flakes
flakes

Reputation: 23624

instead of writing in to a file, save it in an array, then write it how you please

assuming the file isnt terribly large

def codon(infile):
    outlines = []
    ... # your stuff
                        outlines.append(line+'\n')
                else:
                        outlines.append('ERROR'+'\n')
    ... # more stuff
    return outlines

Upvotes: 1

thwllms
thwllms

Reputation: 155

Not sure what you're thinking of returning with this function. You could return a list representing the full reverse compliment of the sequence (in addition to writing "outfile"), but if you don't want to do that then perhaps you don't need to return anything.

Either way, it'll look like something this:

def codon(infile, outfile):
    for line in infile:
        # the rest of your code goes here.
    return something # Remember to indent your return statement.

Upvotes: 1

Ammar
Ammar

Reputation: 1324

the code doesn't seen to return anything, it only writes to an outfile, so you can do this:

def codon(infile):
    for line in infile:
        line = line.strip()
        if line[0] == '>':
                outfile.write(line+'\n')
        else:
                line = line.upper().replace(' ','')
                if re.search('[^ACTG]', line) is None:
                        line = re.sub('A', 'F', line)
                        line = re.sub('T', 'A', line)
                        line = re.sub('F', 'T', line)
                        line = re.sub('G', 'Y', line)
                        line = re.sub('C', 'G', line)
                        line = re.sub('Y', 'C', line)
                        line = line[::-1]
                        outfile.write(line+'\n')
                else:
                        outfile.write('ERROR'+'\n')
    return

or even remove the return statement.

hope this helps.

Upvotes: 1

Related Questions