I'm new to Python and I have a project that I just cannot understand. It requires using recursion to find the longest valid DNA sequence when given two strands. I am given a file called dna.txt. The first line of the file contains a number that is how many pairs of DNA strands there will be. Then the rest of the lines are strings of DNA. My job is to look at each pair of strands individually and find the longest valid DNA sequence and write it into a new file called dnaresults.txt. If this was to be done through iteration I'm fairly confident I could handle it. But the project requires that I use recursion to a)find valid DNA sequences and b)find the longest DNA pairs. I understand recursion on a very basic level (Fibonacci, rolling sum, etc.) but I just cannot wrap my head around how to apply it in this situation.
For example, the input file will look like follows:
3
GAAGCTCG
CCTCGGGA
AAATTT
GGGCCC
CTCTAGGAC
GAGTACCTG
and I am required to output this to a new file:
DNA sequence pair 0:
AGC
TCG
DNA sequence pair 1:
No matches found
DNA sequence pair 2:
GGAC
CCTG
This is what I have tried so far. I can use the number to determine how many times to execute my loop. I can get the pair of strands separated and in their own variable. But once the time has come to evaluate and compare them I'm stumped because I don't understand how to use recursion in this case.
def main():
dnaFile = open('dna.txt').readlines()
numOfPairs = int(dnaFile[0])
for i in range(0, numOfPairs*2, 2):
firstStrand = str(dnaFile[i+1])
secondStrand = str(dnaFile[i+2])
firstStrand.upper()
secondStrand.upper()
So that's where I'm at right now. If anyone could point me in the right direction with the recursion that would be amazing. I'm really just so clueless as to how I'm supposed to use recursion to compare the DNA strands while also storing and returning only the longest one. Thanks in advance!
Edit: My apologies. A DNA sequence is valid when an A on one strand is paired with a T on another (and vice versa) and a G on one strand is paired with a C on another (and vice versa).
ACTGTC
TGACAG
That is a valid sequence because every pair is A-T or G-C.
ACTGTC
GCACTA
That is not a completely valid sequence because not every pair is A-T or G-C. Only the 3rd and 4th pairs are valid (TG and AC).
ACTGC
andGACGT
, would the answer be no match orCTGC
/GACG
? – chepner Apr 22 '19 at 20:22