我在fastq文件中有36个这样的读数:atcttgttcaatggccgatcXXXXgtcgacaatcaa
XXXX是不同的条形码。我想在文件中的准确位置(21到24)搜索条形码,并按顺序打印最多3个不匹配的序列,而不是条形码。
例如:
我有条形码:aacg
在fastq文件中搜索位置21到24之间的条形码,允许序列中的3个不匹配,如:
atcttgttcaatggccgatcaacggtcgacaatcac # it has 1 mismatch
ttcttgttcaatggccgatcaacggtcgacaatcac # it has 2 mismatch
tccttgttcaatggccgatcaacggtcgacaatcac # it has 3 mismatch
我试图首先使用awk找到独特的线条,并寻找不匹配的内容,但是查找和找到它们对我来说非常繁琐。
awk 'NR%4==2' 1.fq |sort|uniq -c|awk '{print $1"\t"$2}' > out1.txt
我能找到任何快捷方式吗?
谢谢。
答案 0 :(得分:1)
使用Python:
strs = "atcttgttcaatggccgatcaacggtcgacaatcaa"
with open("1.fq") as f:
for line in f:
if line[20:24] == "aacg":
line = line.strip()
mismatches = sum(x!=y for x, y in zip(strs, line))
if mismatches <= 3:
print line, mismatches
atcttgttcaatggccgatcaacggtcgacaatcac 1
ttcttgttcaatggccgatcaacggtcgacaatcac 2
tccttgttcaatggccgatcaacggtcgacaatcac 3
答案 1 :(得分:0)
使用Python:
import re
seq="atcttgttcaatggccgatcaacggtcgacaatcaa"
D = [ c for c in seq ]
with open("input") as f:
for line in f:
line=line.rstrip('\n')
if re.match(".{20}aacg", line):
cnt = sum([ 1 for c,d in zip(line,D) if c != d])
if cnt < 4:
print cnt, line
答案 2 :(得分:0)
使用python regex模块可以指定不匹配的数量
import regex #intended as a replacement for re
from Bio import SeqIO
import collections
d = collections.defaultdict(list)
motif = r'((atcttgttcaatggccgatc)(....)(gtcgacaatcaa)){e<4}' #e<4 = less than 4 errors
records = list(SeqIO.parse(open(infile), "fastq"))
for record in records:
seq = str(record.seq)
match = regex.search(motif, seq, regex.BESTMATCH)
barcode = match.group(3)
sequence = match.group(0)
d[barcode].append(sequence) # store as a dictionary key = barcode, value = list of sequences
for k, v in d.items():
print("barcode = %s" % (k))
for i in v:
print("sequence = %s" % (i))
使用捕获组,第四组(3)将是条形码