简体   繁体   中英

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.

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.

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.

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

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(' ', '')
                ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM