给定一个以三元组为键、氨基酸为值的dict(generate_translation_dict
(,我想将三元组列表与该字典中现有的键进行比较。如果它们相等,则应该打印值。
例如:
generate_translation_dict = {'ATA': 'I',
'ATC': 'I',
'ATT': 'I',
'ATG': 'M',
}
comparison_list = [
'ATG',
'TTT',
'GTT',
]
如果列表中的某个项(例如"ATG"(与dict中的键相同,则应打印"M"。比较列表是从文本文件导入的,包含约1200个项目。
到目前为止我的代码:
triplets = [] #This block imports the tripplets
with open('triplets.txt', 'r') as infile:
for line in infile:
line = line.strip('n') # um n zu entfernen
triplets.append(line)
aa = [] # aa for amino acids
with open('amino_acids.txt', 'r') as infile:
for line in infile:
line = line.strip('n') # um n zu entfernen
aa.append(line)
generate_translation_dict = {} # empty dictionary
for n in range(len(triplets)):
generate_translation_dict[triplets[n]] = aa[n] # adding elements to the dict # triplets added as keys/ aa added as value
print(generate_translation_dict)
tripletsequence = []
with open('triplet_sequence.txt', 'r') as infile:
for line in infile:
line = line.strip('n') # um n zu entfernen
tripletsequence.append(line)
正如您所看到的,三元组序列就是列表。比较应该是一个循环,直到列表完成。
一旦您有了翻译dict,您所需要做的就是在triplet_sequence
中迭代,并将三元组用作dict中的键。
您还可以通过zip
ping两个文件流并将结果放入dict理解中来极大地简化翻译dict的创建:
with open('triplets.txt') as triplets, open('amino_acids.txt') as aa:
translation = {
t.strip(): a.strip()
for t, a in zip(triplets, aa)
}
with open('triplet_sequence.txt') as f:
triplet_sequence = [
translation[t.strip()]
for t in f
]
print("n".join(triplet_sequence))
% cat triplets.txt
ATA
ATC
ATT
ATG
% cat amino_acids.txt
I
I
I
M
% cat triplet_sequence.txt
ATG
ATT
ATC
% python test.py
M
I
I
一旦你有了翻译词典(请检查最后的建议(:
translation_dict = {
'ATA': 'I',
'ATC': 'I',
'ATT': 'I',
'ATG': 'M',
'TTT': 'P',
'GTT': 'V'
}
密码子列表:
comparison_list = [
'ATG',
'TTT',
'GTT',
]
你可以通过这种方式解码到氨基酸序列:
seq = [translation_dict[k] for k in comparison_list]
print(seq) #=> ['M', 'P', 'V']
如果你觉得这是个好主意,使用get((方法可以避免丢失密钥的错误(在丢失密钥的情况下返回None
作为元素(:
seq = [translation_dict.get(k) for k in comparison_list]
或者,删除None
元素:
seq = list(filter(None, [translation_dict.get(k) for k in comparison_list]))
我建议将翻译dict存储为JSON文件,以便直接加载为dict,随时可用。