The goal with this function is to find one DNA sequence within another sequence, with a specified amount of mismatch tolerance. For example:
dnasearch('acc','ccc',0)
shouldn't find a match, whilednasearch('acc','ccc',1)
should find one.
EDIT: Function should also be able to find pat sequence in a sequence larger than the pat sequence; furthermore it should be able to find multiple matches within that sequence if they exist. For example, dnasearch('acc','acaaatc',1)
would find two matches: 'aca' and 'act'. Thanks to @weronika for pointing this out.
Here's my working draft:
def dnasearch(pat,seq,mismatch_allowed=0):
patset1=set([pat])
patset2=set([pat])
for number in range(mismatch_allowed):
for version in patset1:
for a in range(len(version)):
for letter in 'actg':
newpat=version[:a]+letter+version[a+1:]
patset2.add(newpat)
patset1|=patset2
for n in patset1:
if seq.find(n) != -1: print 'At position', seq.find(n),\
'found',n
I was wondering how to write this function:
- With fewer for-loops.
- Without the second
patset
. Originally I tried just adding new patterns topatset1
, but I got the error:RuntimeError: Set changed size during iteration.
- Simpler in any other way.