Reputation: 5
I am trying to compare the two sequences, a and b, and write code that prints out where these mismatches are. I know the code below is not correct, but it is the closest I can come for the sake of this. I feel I am misunderstanding something about the concept of flow. Any help would be greatly appreciated. Thanks.
a="CATCGTCCT"
b="CACCGACCG"
mismatch_seq=[]
for x in list(a):
mismatch=[]
if x!=x in list(b):
mismatch+=x in list(b)
mismatch_seq.append(mismatch)
else:
pass
print mismatch_seq
EDIT: In addition, how would you write code to tell you where, in terms of index, this mismatch occurred?
Upvotes: 0
Views: 175
Reputation: 5373
You can just use the following:
>>> filter(lambda m: m[1][0] != m[1][1] , enumerate(zip(a, b)))
[(2, ('T', 'C')), (5, ('T', 'A')), (8, ('T', 'G'))]
I would recommend using izip
. This way you can put in entire files which are arbitrarily long, and then can safely send the output to a file ...
Upvotes: 1
Reputation: 4887
Inspired by the preceding answers, this list comprehension looks at characters in b
where corresponding characters in a
exist, and returns their index i
if those positions are not equal.
a = "CATCGTCCT"
b = "CACCGACCG"
mismatch = [i for i in range(0, len(a)) if a[i] != b[i]]
for pos in mismatch:
print "At position", pos, "a had", a[pos], "while b had", b[pos], "."
Equivalent loop code for mismatch
would be, as in this answer:
for i in range(0, len(a)):
if a[i] != b[i]:
mismatch.append(i)
Upvotes: 1
Reputation: 224
Just do:
a="CATCGTCCT"
b="CACCGACCG"
mismatch_seq=[]
for x in range (0, len(a)):
if a[x] != b[x]:
mismatch_seq.append(x)
print mismatch_seq
Upvotes: 1
Reputation: 1
A simplistic code may use this
a = "CATCGTCCT" # SET a
b = "CACCGACCG" # SET b
aListOfMismatchPOSITIONs = [] # SET aList
for charPTR in range( len( a ) ): # LOOP over len( a )
if a[charPTR] != b[charPTR]: # .NE.
aListOfMismatchPOSITIONs.append( charPTR) # APPEND
else: # ELSE
continue # LOOP^
print "A sequence of non-matching positions = ", aListOfMismatchPOSITIONs
Upvotes: 0