Reputation: 109
I'm trying to search a nucleotide sequence (composed of only A,C,G,T) for a user-defined pattern, using regex:
The relevant code is as follows:
match = re.match(r'{0}'.format(pattern), sequence)
match always returns None, where I need it to return the part of the sequence that matches the user query...
What am I doing wrong?
EDIT: This is how I constructed the search pattern:
askMotif = raw_input('Enter a motif to search for it in the sequence (The wildcard character ‘?’ represents any nucleotide in that position, and * represents none or many nucleotides in that position.): ')
listMotif= []
letterlist = ['A','C','G','T', 'a', 'c','g','t']
for letter in askMotif:
if letter in letterlist:
a = letter.capitalize()
listMotif.append(a)
if letter == '?':
listMotif.append('.')
if letter == '*':
listMotif.append('*?')
pattern = ''
for searcher in listMotif:
pattern+=searcher
Not very pythonic, I know...
Upvotes: 0
Views: 306
Reputation: 103754
That should work fine:
>>> tgt='AGAGAGAGACGTACACAC'
>>> re.match(r'{}'.format('ACGT'), tgt)
>>> re.search(r'{}'.format('ACGT'), tgt)
<_sre.SRE_Match object at 0x10a5d6920>
I think it may because you mean to use search vs match
Hint on your posted code:
prompt='''\
Enter a motif to search for it in the sequence
(The wildcard character '?' represents any nucleotide in that position,
and * represents none or many nucleotides in that position.)
'''
pattern=None
while pattern==None:
print prompt
user_input=raw_input('>>> ')
letterlist = ['A','C','G','T', '?', '*']
user_input=user_input.upper()
if len(user_input)>1 and all(c in letterlist for c in user_input):
pattern=user_input.replace('?', '.').replace('*', '.*?')
else:
print 'Bad pattern, please try again'
Upvotes: 2
Reputation: 286
re.match()
only matches at the beginning of the sequence. Perhaps you need re.search()
?
>>> re.match(r'{0}'.format('bar'), 'foobar').group(0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'group'
>>> re.search(r'{0}'.format('bar'), 'foobar').group(0)
'bar'
Upvotes: 1