如何在循环中使用子字符串制作字典 python
how to make dictionary using substrings in a loop python
我有一个包含数百万个 DNA 序列的 fasta 文件 - 对于每个 DNA 序列,还有一个 ID。
>seq1
AATCAG #----> 5mers of this line are AATCA and ATCAG
>seq3
AAATTACTACTCTCTA
>seq19
ATTACG #----> 5mers of this line are ATTAC and TTACG
我想做的是,如果 DNA 序列的长度是 6,那么我将那条线做成 5mers(如代码和上面所示)。所以我无法解决的事情是我想制作一本字典,字典显示哪个 5mers 来自哪个 sequence id
.
这是我的代码,但已经进行了一半:
from Bio import SeqIO
from Bio.Seq import Seq
with open(file, 'r') as f:
lst = []
dict = {}
for record in SeqIO.parse(f, 'fasta'): #reads the fast file
if len(record.seq) == 6: #considers the DNA sequence length
for i in range(len(record.seq)):
kmer = str(record.seq[i:i + 5])
if len(kmer) == 5:
lst.append(kmer)
dict[record.id] = kmer #record.id picks the ids of DNA sequences
#print(lst)
print(dict)
所需的词典应如下所示:
dict = {'seq1':'AATCA', 'seq1':'ATCAG', 'seq19':'ATTAC','seq19':'TTACG'}
按照@SulemanElahi Make a dictionary with duplicate keys in Python中的建议使用defaultdict
,因为字典中不能有重复键
from Bio import SeqIO
from collections import defaultdict
file = 'test.faa'
with open(file, 'r') as f:
dicti = defaultdict(list)
for record in SeqIO.parse(f, 'fasta'): #reads the fast file
if len(record.seq) == 6: #considers the DNA sequence length
kmer = str(record.seq[:5])
kmer2 = str(record.seq[1:])
dicti[record.id].extend((kmer,kmer2)) #record.id picks the ids of DNA sequences
print(dicti)
for i in dicti:
for ii in dicti[i]:
print(i, ' : ', ii)
输出:
defaultdict(<class 'list'>, {'seq1': ['AATCA', 'ATCAG'], 'seq19': ['ATTAC', 'TTACG']})
seq1 : AATCA
seq1 : ATCAG
seq19 : ATTAC
seq19 : TTACG
我有一个包含数百万个 DNA 序列的 fasta 文件 - 对于每个 DNA 序列,还有一个 ID。
>seq1
AATCAG #----> 5mers of this line are AATCA and ATCAG
>seq3
AAATTACTACTCTCTA
>seq19
ATTACG #----> 5mers of this line are ATTAC and TTACG
我想做的是,如果 DNA 序列的长度是 6,那么我将那条线做成 5mers(如代码和上面所示)。所以我无法解决的事情是我想制作一本字典,字典显示哪个 5mers 来自哪个 sequence id
.
这是我的代码,但已经进行了一半:
from Bio import SeqIO
from Bio.Seq import Seq
with open(file, 'r') as f:
lst = []
dict = {}
for record in SeqIO.parse(f, 'fasta'): #reads the fast file
if len(record.seq) == 6: #considers the DNA sequence length
for i in range(len(record.seq)):
kmer = str(record.seq[i:i + 5])
if len(kmer) == 5:
lst.append(kmer)
dict[record.id] = kmer #record.id picks the ids of DNA sequences
#print(lst)
print(dict)
所需的词典应如下所示:
dict = {'seq1':'AATCA', 'seq1':'ATCAG', 'seq19':'ATTAC','seq19':'TTACG'}
按照@SulemanElahi Make a dictionary with duplicate keys in Python中的建议使用defaultdict
,因为字典中不能有重复键
from Bio import SeqIO
from collections import defaultdict
file = 'test.faa'
with open(file, 'r') as f:
dicti = defaultdict(list)
for record in SeqIO.parse(f, 'fasta'): #reads the fast file
if len(record.seq) == 6: #considers the DNA sequence length
kmer = str(record.seq[:5])
kmer2 = str(record.seq[1:])
dicti[record.id].extend((kmer,kmer2)) #record.id picks the ids of DNA sequences
print(dicti)
for i in dicti:
for ii in dicti[i]:
print(i, ' : ', ii)
输出:
defaultdict(<class 'list'>, {'seq1': ['AATCA', 'ATCAG'], 'seq19': ['ATTAC', 'TTACG']})
seq1 : AATCA
seq1 : ATCAG
seq19 : ATTAC
seq19 : TTACG