[英]Using Dictionary to match codons
I am trying to make a function codon_pairs(pairs, codonsA, codonsB)
that takes in three arguments;我正在尝试创建一个函数
codon_pairs(pairs, codonsA, codonsB)
,它接受三个参数; a dictionary pairs
and two lists, codonsA
and codonsB
.字典
pairs
和两个列表, codonsA
和codonsB
。 The dictionary contains the base pairs and the codon lists contain codon sequences.字典包含碱基对,密码子列表包含密码子序列。 I am trying to find the complementary codon sequence in
codonsB
for each codon sequence in codonsA
, and return matching pairs like the following:我正在尝试为
codonsB
中的每个密码子序列在codonsA
找到互补密码子序列,并返回如下匹配对:
pairs = {'A':'T', 'C':'G', 'T':'A', 'G':'C'}
codonsA = ['AAG', 'TAC', 'CGG', 'GAT', 'TTG', 'GTG', 'CAT', 'GGC', 'ATT', 'TCT']
codonsB = ['TAA', 'CTA', 'AAC', 'TTC', 'AGA', 'CCC', 'CCG', 'GTA']
print(codons_pairs(pairs, condonsA, codonsB))
[('AAG', 'TTC'), ('GAT', 'CTA'), ('TTG', 'AAC'), ('CAT', 'GTA'), ('GGC', 'CCG'), ('ATT', 'TAA'), ('TCT', 'AGA')]
The first item in the 2-tuple is a codon from codonsA
and the second item is a matching codon from codonsB
.在2元组中的第一项是从一个密码子
codonsA
和第二项是从密码子的匹配codonsB
。 For example, the sequences AAG ( codonsA[0]
) and TCC ( codonsB[3]
) are matching pairs, as the base pair of A is T, and the base pair of G is C, highlighted in the pairs
dictionary.例如,序列AAG (
codonsA[0]
) 和TCC ( codonsB[3]
) 是匹配对,因为 A 的碱基对是 T,G 的碱基对是 C,在pairs
字典中突出显示。
On the flip side, if a matching pair cannot be found, it will be omitted from the final result.另一方面,如果找不到匹配对,它将从最终结果中省略。
This is what I have so far:这是我到目前为止:
pairs = {'A':'T', 'C':'G', 'T':'A', 'G':'C'}
codonsA = ['AAG', 'TAC', 'CGG', 'GAT', 'TTG', 'GTG', 'CAT', 'GGC', 'ATT', 'TCT']
codonsB = ['TAA', 'CTA', 'AAC', 'TTC', 'AGA', 'CCC', 'CCG', 'GTA']
def codons_pairs(pairs, codonsA, codonsB):
for A in codonsA:
for B in codonsB:
for i in A:
for j in B:
I'm just not sure how to check pairs between codonsA
and codonsB
, with regard to a dictionary.关于字典,我只是不确定如何检查
codonsA
和codonsB
之间的对。 Any help would be greatly appreciated.任何帮助将不胜感激。
Convert codonsB
to a set()
for fast checking (O(1) membership tests, no need for nested loops), then map each codon from A
through your pairs
mapping and test the result against the set:将
codonsB
转换为set()
以进行快速检查(O(1) 成员资格测试,不需要嵌套循环),然后通过您的pairs
映射映射来自A
每个密码子并针对该集合测试结果:
def codons_pair(pairs, codonsA, codonsB):
codonsB = set(codonsB)
for codon in codonsA:
complement = ''.join([pairs[base] for base in codon])
if complement in codonsB:
yield (codon, complement)
The above is a generator function;以上是一个生成器函数; it'll yield each match as it finds them.
它会在找到每个匹配项时产生它们。 You could convert the resulting generator to a list with the
list()
function, or just iterate over the function.您可以使用
list()
函数将生成的生成器转换为列表,或者只是迭代该函数。
Demo:演示:
>>> pairs = {'A':'T', 'C':'G', 'T':'A', 'G':'C'}
>>> codonsA = ['AAG', 'TAC', 'CGG', 'GAT', 'TTG', 'GTG', 'CAT', 'GGC', 'ATT', 'TCT']
>>> codonsB = ['TAA', 'CTA', 'AAC', 'TTC', 'AGA', 'CCC', 'CCG', 'GTA']
>>> list(codons_pair(pairs, codonsA, codonsB))
[('AAG', 'TTC'), ('GAT', 'CTA'), ('TTG', 'AAC'), ('CAT', 'GTA'), ('GGC', 'CCG'), ('ATT', 'TAA'), ('TCT', 'AGA')]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.