着急!求大神给帮忙或者给点思路。
文件说明:wt.txt文件第三列是id,第四列是起始位置x。output.txt,>开头的是行是id,下面是序列,
目的:对每一个wt.txt的id,在output.txt文件中找到后,从第x的序列开始,截取三个字母XXX,并统计次数。
最终得到这样子的文件:
XXX XXX XXX XXX .. .... ... XXX XXX
id1
id2
...
...
test.zip
(6.57 KB)
import re id_dict={} result={} import json with open ("output.txt") as f1: enstinfo=f1.read() it=re.finditer(r'\>([^\n]+)([^\>]+)',enstinfo,flags=re.S) for match in it: result[match.group(1)]={} id_dict[match.group(1)]=re.sub(r'\n','',match.group(2)) with open ("wt.txt") as f2: tablelines=f2.readlines() for tableline in tablelines: tablelist=re.split(r'\t',tableline) if tablelist[2] in id_dict: cutstring=id_dict[tablelist[2]][int(tablelist[3]):int(tablelist[3])+3] if cutstring in result[tablelist[2]]: result[tablelist[2]][cutstring]+=1 else: result[tablelist[2]][cutstring]=1 print(json.dumps(result)) {"ENST00000494424.1": {"GGC": 1, "GAG": 2, "GAA": 2, "TTT": 7, "AGA": 1, "TGT": 4, "GAT": 4, "GGT": 1, "AGG": 5, "TTA": 5, "AAT": 2, "CCT": 2, "TTC": 1, "GGG": 1, "GTA": 2, "TAT": 1, "ATT": 3, "TAG": 1, "TGG": 1, "GCC": 1, "TCT": 1, "GTG": 1, "GCA": 1, "TGC": 1, "TAA": 1, "CTA": 1, "GGA": 1, "CTC": 1}, "ENST00000496771.5": {"CGT": 1, "TCT": 5, "CAT": 1, "CTA": 2, "GAG": 5, "TGG": 5, "AGA": 8, "AGT": 5, "CTC": 2, "ATA": 2, "TTG": 4, "CCA": 2, "CAG": 2, "TAG": 2, "AGG": 1, "TGC": 6, "TAT": 5, "GTA": 2, "CAA": 3, "GAT": 2, "GGT": 3, "TTT": 5, "CTT": 2, "ATT": 6, "TCG": 2, "GAC": 1, "AAG": 3, "GGA": 3, "AGC": 1, "GTC": 1, "GGC": 2, "CCT": 1, "GAA": 3, "GCA": 3, "GCG": 2, "TGA": 7, "TAC": 3, "TTA": 7, "ACT": 7, "AAT": 1, "CTG": 2, "TAA": 1, "CAC": 1, "GCT": 1, "ATC": 1, "ATG": 1, "TGT": 1, "GGG": 2}}