Reputation: 1056
Say I have a string of characters like:
'TTATGACGTTATTCTACTTTGATTGTGCGAGACAATGCTACCTTACCGGTCGGAACTCGATCGGTTGAACTCTATCACGCCTGGTCTTCGAAGTTAGCA'
And I would like to use a regular expression like re.findall to locate any instances of 3 entries (e.g. TTA or GTT for instance).
How can I pass the sub-string to a user-defined function such that this is possible?
What I tried was something like this:
def finder(sequence, codons):
Y = re.findall(r'codons',sequence)
return Y
However, when I attempt to call this function on my string that I gave above using some input for codons like 'TTA', all I get as a return is an empty list...
I think the list is empty because within the regular expression in my function, "codons" is within the quotation marks next to the r. Is there a way to pass something to a regular expression in a manner that circumvents this?
Upvotes: 2
Views: 2913
Reputation: 21
A polished answer of your question (using python 3+):
# codon_search.py
import re
def finder(codon, fullseq):
matched = re.findall(codon, fullseq)
print (matched)
sequence = "TTATGACGTTATTCTACTTTGATTGTGCGAGACAATGCTACCTTACCGGTCGGAAC"
search_str = "TTA"
#call function
finder(search_str, sequence)
Output: 3 matches will be found in the above sequence
['TTA', 'TTA', 'TTA']
Upvotes: 2
Reputation: 71451
You are trying to match the string "codons"
, rather than the variable parameter. Try this:
def finder(sequence, codons):
return re.findall(codons, sequence)
Upvotes: 2