【问题标题】:Writing a function to Trace bacterial lineage in python在 python 中编写一个跟踪细菌谱系的函数
【发布时间】:2020-10-21 22:16:47
【问题描述】:

我正在处理跟踪细胞分裂的生物学数据。现在我想为每个细胞建立一个谱系列表。对于所有单元格,我将 cell_number 和产生它的“母亲”存储在一个列表中(见下文)。如果mother_nb = 0,则表示它们是原始单元格。

现在我要做的是创建一个函数,以便它返回一个完整的沿袭列表,如下所示:

def find_lineage(cell_nb, full_ancestry):
   (code goes here)
   returns lineage

这样当我运行这个函数时find_lineage(13, full_ancestry)

我得到一个清单 [11, 1, 0]

experiment[0].family_tree
[{'cell_nb': 1, 'mother_nb': 0},
 {'cell_nb': 2, 'mother_nb': 0},
 {'cell_nb': 3, 'mother_nb': 0},
 {'cell_nb': 4, 'mother_nb': 0},
 {'cell_nb': 5, 'mother_nb': 0},
 {'cell_nb': 6, 'mother_nb': 2},
 {'cell_nb': 7, 'mother_nb': 3},
 {'cell_nb': 8, 'mother_nb': 1},
 {'cell_nb': 9, 'mother_nb': 6},
 {'cell_nb': 10, 'mother_nb': 2},
 {'cell_nb': 11, 'mother_nb': 1},
 {'cell_nb': 12, 'mother_nb': 8},
 {'cell_nb': 13, 'mother_nb': 11},
 {'cell_nb': 14, 'mother_nb': 8},
 {'cell_nb': 15, 'mother_nb': 1},
 {'cell_nb': 16, 'mother_nb': 12},
 {'cell_nb': 17, 'mother_nb': 13},
 {'cell_nb': 18, 'mother_nb': 15},
 {'cell_nb': 19, 'mother_nb': 11},
 {'cell_nb': 20, 'mother_nb': 1},
 {'cell_nb': 21, 'mother_nb': 11},
 {'cell_nb': 22, 'mother_nb': 18},
 {'cell_nb': 23, 'mother_nb': 20},
 {'cell_nb': 24, 'mother_nb': 15},
 {'cell_nb': 25, 'mother_nb': 1},
 {'cell_nb': 26, 'mother_nb': 25},
 {'cell_nb': 27, 'mother_nb': 1},
 {'cell_nb': 28, 'mother_nb': 20},
 {'cell_nb': 29, 'mother_nb': 26},
 {'cell_nb': 30, 'mother_nb': 1},
 {'cell_nb': 31, 'mother_nb': 27},
 {'cell_nb': 32, 'mother_nb': 25},
 {'cell_nb': 33, 'mother_nb': 1},
 {'cell_nb': 34, 'mother_nb': 30},
 {'cell_nb': 35, 'mother_nb': 27},
 {'cell_nb': 36, 'mother_nb': 33},
 {'cell_nb': 37, 'mother_nb': 1},
 {'cell_nb': 38, 'mother_nb': 30},
 {'cell_nb': 39, 'mother_nb': 33},
 {'cell_nb': 40, 'mother_nb': 1},
 {'cell_nb': 41, 'mother_nb': 37},
 {'cell_nb': 42, 'mother_nb': 37},
 {'cell_nb': 43, 'mother_nb': 40},
 {'cell_nb': 44, 'mother_nb': 1},
 {'cell_nb': 45, 'mother_nb': 40},
 {'cell_nb': 46, 'mother_nb': 44},
 {'cell_nb': 47, 'mother_nb': 1},
 {'cell_nb': 48, 'mother_nb': 1},
 {'cell_nb': 49, 'mother_nb': 44},
 {'cell_nb': 50, 'mother_nb': 47},
 {'cell_nb': 51, 'mother_nb': 1},
 {'cell_nb': 52, 'mother_nb': 47},
 {'cell_nb': 53, 'mother_nb': 48},
 {'cell_nb': 54, 'mother_nb': 47},
 {'cell_nb': 55, 'mother_nb': 1},
 {'cell_nb': 56, 'mother_nb': 48},
 {'cell_nb': 57, 'mother_nb': 51},
 {'cell_nb': 58, 'mother_nb': 1},
 {'cell_nb': 59, 'mother_nb': 55},
 {'cell_nb': 60, 'mother_nb': 1},
 {'cell_nb': 61, 'mother_nb': 58},
 {'cell_nb': 62, 'mother_nb': 1},
 {'cell_nb': 63, 'mother_nb': 60},
 {'cell_nb': 64, 'mother_nb': 63},
 {'cell_nb': 65, 'mother_nb': 60},
 {'cell_nb': 66, 'mother_nb': 62},
 {'cell_nb': 67, 'mother_nb': 1},
 {'cell_nb': 68, 'mother_nb': 1},
 {'cell_nb': 69, 'mother_nb': 67},
 {'cell_nb': 70, 'mother_nb': 1},
 {'cell_nb': 71, 'mother_nb': 68},
 {'cell_nb': 72, 'mother_nb': 70},
 {'cell_nb': 73, 'mother_nb': 68},
 {'cell_nb': 74, 'mother_nb': 1},
 {'cell_nb': 75, 'mother_nb': 74},
 {'cell_nb': 76, 'mother_nb': 70},
 {'cell_nb': 77, 'mother_nb': 1},
 {'cell_nb': 78, 'mother_nb': 74},
 {'cell_nb': 79, 'mother_nb': 0},
 {'cell_nb': 80, 'mother_nb': 77},
 {'cell_nb': 81, 'mother_nb': 1},
 {'cell_nb': 82, 'mother_nb': 1},
 {'cell_nb': 83, 'mother_nb': 82},
 {'cell_nb': 84, 'mother_nb': 1},
 {'cell_nb': 85, 'mother_nb': 1},
 {'cell_nb': 86, 'mother_nb': 1}]```

【问题讨论】:

  • SO 不是免费的编码网站。请尝试一下
  • 您好,感谢您的反馈。我尝试解决这个问题几个小时,但无法解决。我确实编写了一个在非常有限的条件下工作的递归函数。我决定不在这里发布我自己的代码,因为我认为这会影响问题。
  • 恰恰相反。放下失败的尝试总比没有好
  • 好的,下次我会的。感谢您帮助我改进我的 SO 问题。

标签: python function


【解决方案1】:

如果full_ancestry 是一个按cell_nb 升序排序的列表,并且没有跳过任何数字,则以下方法可行。

def find_lineage(cell_nb, full_ancestry):
    ancestry = []
    while cell_nb:
        cell_nb = full_ancestry[cell_nb - 1]['mother_nb']
        ancestry.append(cell_nb)
    return ancestry

print(find_lineage(13, full_ancestry))

输出:

[11, 1, 0]

如果列表项可以按任意顺序排列,请考虑创建地图并使用它。

ancestry_map = {}
for ancestry in full_ancestry:
    ancestry_map[ancestry['cell_nb']] = ancestry['mother_nb']

def find_lineage(cell_nb, ancestry_map):
    ancestry = []
    while cell_nb:
        cell_nb = ancestry_map[cell_nb]
        ancestry.append(cell_nb)
    return ancestry

print(find_lineage(13, ancestry_map))

输出:

[11, 1, 0]

【讨论】:

    猜你喜欢
    • 2020-11-16
    • 1970-01-01
    • 2010-12-08
    • 2020-11-20
    • 2020-02-02
    • 2019-01-12
    • 1970-01-01
    • 1970-01-01
    • 2020-11-08
    相关资源
    最近更新 更多