您可以使用.find
字符串的方法找到不匹配的地方。不匹配用空格 (' ') 表示,因此我们在 blat 输出的中间行查找。我个人不知道 blat,所以我不确定输出是否总是以三联线形式出现,但假设确实如此,以下函数将返回一个位置不匹配的列表,每个位置表示为不匹配位置的元组顶部序列,底部序列相同。
blat_src = """00000001 taaaagatgaagtttctatcatccaaaaaatgggctacagaaacc 00000045
<<<<<<<< ||||||||||||||||||||||||||| |||||||||||||||| <<<<<<<<
41629392 taaaagatgaagtttctatcatccaaagtatgggctacagaaacc 41629348"""
def find_mismatch(blat):
#break the blat input into lines
lines = blat.split("\n")
#give some firendly names to the different lines
seq_a = lines[0]
seq_b = lines[2]
#We're not interested in the '<' and '>' so we strip them out with a slice
matchstr = lines[1][9:-9]
#Get the integer values of the starts of each sequence segment
pos_a = int(seq_a[:8])
pos_b = int(seq_b[:8])
results = []
#find the index of first space character, mmpos = mismatch position
mmpos = matchstr.find(" ")
#if a space exists (-1 if none found)
while mmpos != -1:
#the position of the mismatch is the start position of the
#sequence plus the index within the segment
results.append((posa+mmpos, posb+mmpos))
#search the rest of the string (from mmpos+1 onwards)
mmpos = matchstr.find(" ", mmpos+1)
return results
print find_mismatch(blat_src)
哪个生产
[(28, 41629419), (29, 41629420)]
告诉我们位置 28 和 29(根据顶部序列索引)或位置 41629419 和 41629420(根据底部序列索引)不匹配。