Punnett Square功能:列表中的匹配项

时间:2013-03-02 04:00:48

标签: python list function

我想找到特定十字架的所有可能的配子。

例如:'AABB' x 'aabb'将分为['AA', 'BB']['aa', 'bb']。我完成了这一部分。在此之后,它应该返回: [['Aa', 'Aa', 'Aa', 'Aa'], ['Bb', 'Bb', 'Bb', 'Bb']](对于父母'A'中的每个等位基因,它与父母'B'中的等位基因匹配;这是一个简化的Punnett广场。

这是我到目前为止所做的:

def punnett(a, b):
    n = int(len(a)/2)
    x = int(float(len(a)) / n)
    partsA, partsB, gametes = [a[i * x : i * x + x] for i in range(n)], [b[i * x : i * x + x] for i in range(n)], []
    for y in range(1, n):
        g = []
        for index in range(0, n/2 + y):
            for i in partsA[index]:
                for j in partsB[index]:
                    g.append(i+j)
        gametes.append(g)
    return gametes

但这并不会导致我的预期:

>>> punnett('AaBb', 'AaBb')
[['AA', 'Aa', 'aA', 'aa', 'BB', 'Bb', 'bB', 'bb']]

此外,三重杂交不会给出我预期的结果:

>>> punnett('AaBbCc', 'AaBbCc')
[['AA', 'Aa', 'aA', 'aa', 'BB', 'Bb', 'bB', 'bb'], ['AA', 'Aa', 'aA', 'aa', 'BB', 'Bb', 'bB', 'bb', 'CC', 'Cc', 'cC', 'cc']]

如果我能得到关于我做错了什么以及如何改进它的意见,那就太好了。谢谢!

1 个答案:

答案 0 :(得分:2)

好的,我刚刚阅读了一些关于你可能想要实现的内容的材料,这里是更改

>>> from itertools import product, izip

如果我理解正确,等位基因是忽略大小写的连续字符。在“AaBb”的情况下,它是['Aa'和'Bb']

>>> def allele(e):
    return [list(v) for _, v in groupby(e, key = str.lower)]

十字架被定义为材料与父母之间等位基因的所有可能交叉

随后从交叉选择新的基因组

>>> def punnett(a, b):
    return [''.join(e)
        for e in product(*([''.join(e) for e in product(*e)]
                    for e in izip(allele(a), allele(b))))]

>>> punnett('AaBbCc', 'AaBbCc')
['AABBCC', 'AABBCc', 'AABBcC', 'AABBcc', 'AABbCC', 'AABbCc', 'AABbcC', 'AABbcc', 'AAbBCC', 'AAbBCc', 'AAbBcC', 'AAbBcc', 'AAbbCC', 'AAbbCc', 'AAbbcC', 'AAbbcc', 'AaBBCC', 'AaBBCc', 'AaBBcC', 'AaBBcc', 'AaBbCC', 'AaBbCc', 'AaBbcC', 'AaBbcc', 'AabBCC', 'AabBCc', 'AabBcC', 'AabBcc', 'AabbCC', 'AabbCc', 'AabbcC', 'Aabbcc', 'aABBCC', 'aABBCc', 'aABBcC', 'aABBcc', 'aABbCC', 'aABbCc', 'aABbcC', 'aABbcc', 'aAbBCC', 'aAbBCc', 'aAbBcC', 'aAbBcc', 'aAbbCC', 'aAbbCc', 'aAbbcC', 'aAbbcc', 'aaBBCC', 'aaBBCc', 'aaBBcC', 'aaBBcc', 'aaBbCC', 'aaBbCc', 'aaBbcC', 'aaBbcc', 'aabBCC', 'aabBCc', 'aabBcC', 'aabBcc', 'aabbCC', 'aabbCc', 'aabbcC', 'aabbcc']
>>> punnett('AaBb', 'AaBb')
['AABB', 'AABb', 'AAbB', 'AAbb', 'AaBB', 'AaBb', 'AabB', 'Aabb', 'aABB', 'aABb', 'aAbB', 'aAbb', 'aaBB', 'aaBb', 'aabB', 'aabb']
>>>