r - 从模拟网格的历史中查找单元的合并时间
问题描述
我正在编写一个由 nxn 单元格组成的模拟。在模拟的不同时间,随机绘制一个单元格以“划分”。当一个细胞分裂时,它会死亡并产生两个子细胞。一个女儿替换原始单元格,另一个女儿随机替换网格上的 8 个邻居之一。
网格由开头有 n^2 行的数据帧编码,每个单元格一行(每个单元格的开始时间为birth_time=0,death_time=50 和 parent=0)。随着模拟的进行,为每个分裂事件添加代表子细胞的两行,并更新父细胞(和邻居前体细胞)的死亡时间。女儿们被分配了birth_time!=0、death_time=50 和父母(参见下面的示例)。
在模拟运行了一段指定的时间后(在下面的示例中为 50),我采集了一个具有相同 x 坐标的单元格样本。对于这些细胞,我想使用我的网格数据框中编码的历史信息来查找它们的合并时间,即作为最终样本中两个或多个细胞的祖先的所有细胞的死亡时间。我正在寻找一个在 R 中完成此任务的函数(或帮助构建我自己可以在 R 中编码的算法)。
下面是三个例子,我希望能清楚地说明我的要求:
测试1:
> grid1
cellID x_coordinate y_coordinate onEdge parent birth_time death_time
1 1 1 1 1 0 0 50
2 2 2 1 1 0 0 50
3 3 3 1 1 0 0 2
4 4 4 1 1 0 0 50
5 5 5 1 1 0 0 50
6 6 1 2 1 0 0 50
7 7 2 2 0 0 0 50
8 8 3 2 0 0 0 2
9 9 4 2 0 0 0 50
10 10 5 2 1 0 0 50
11 11 1 3 1 0 0 50
12 12 2 3 0 0 0 50
13 13 3 3 0 0 0 12
14 14 4 3 0 0 0 50
15 15 5 3 1 0 0 50
16 16 1 4 1 0 0 50
17 17 2 4 0 0 0 50
18 18 3 4 0 0 0 21
19 19 4 4 0 0 0 50
20 20 5 4 1 0 0 50
21 21 1 5 1 0 0 50
22 22 2 5 1 0 0 50
23 23 3 5 1 0 0 50
24 24 4 5 1 0 0 50
25 25 5 5 1 0 0 50
26 26 3 2 0 8 2 12
27 27 3 1 1 8 2 50
28 28 3 2 0 26 12 33
29 29 3 3 0 26 12 21
30 30 3 3 0 29 21 33
31 31 3 4 0 29 21 45
32 32 3 3 0 30 33 45
33 33 3 2 0 30 33 50
34 34 3 4 0 31 45 50
35 35 3 3 0 31 45 50
我对结束时间 (50) 存在的隐窝进行采样,并且 x 坐标 = 3。请注意,尽管我在此测试用例中对所有 5 个隐窝进行了采样,但在实际模拟中将采样一个子集。
> sample1
cellID x_coordinate y_coordinate onEdge parent birth_time death_time
23 23 3 5 1 0 0 50
27 27 3 1 1 8 2 50
33 33 3 2 0 30 33 50
34 34 3 4 0 31 45 50
35 35 3 3 0 31 45 50
在这个例子中,(3,5) 处的单元格与其他单元格无关(除了所有单元格 (0) 的伪父节点。其他四个单元格都是相关的,并且有 3 个分裂事件对这些是:
> res1
cellID x_coordinate y_coordinate onEdge parent birth_time death_time
1 8 3 2 0 0 0 2
3 29 3 3 0 26 12 21
5 31 3 4 0 29 21 45
以下是另外两个示例: Test2:
> grid2
cellID x_coordinate y_coordinate onEdge parent birth_time death_time
1 1 1 1 1 0 0 50
2 2 2 1 1 0 0 2
3 3 3 1 1 0 0 50
4 4 4 1 1 0 0 45
5 5 5 1 1 0 0 50
6 6 1 2 1 0 0 50
7 7 2 2 0 0 0 2
8 8 3 2 0 0 0 45
9 9 4 2 0 0 0 21
10 10 5 2 1 0 0 21
11 11 1 3 1 0 0 50
12 12 2 3 0 0 0 50
13 13 3 3 0 0 0 33
14 14 4 3 0 0 0 50
15 15 5 3 1 0 0 50
16 16 1 4 1 0 0 50
17 17 2 4 0 0 0 33
18 18 3 4 0 0 0 12
19 19 4 4 0 0 0 50
20 20 5 4 1 0 0 50
21 21 1 5 1 0 0 50
22 22 2 5 1 0 0 50
23 23 3 5 1 0 0 50
24 24 4 5 1 0 0 12
25 25 5 5 1 0 0 50
26 26 2 2 0 7 2 50
27 27 2 1 1 7 2 50
28 28 3 4 0 18 12 50
29 29 4 5 1 18 12 50
30 30 4 2 0 9 21 50
31 31 5 2 1 9 21 50
32 32 2 4 0 17 33 50
33 33 3 3 0 17 33 50
34 34 3 2 0 8 45 50
35 35 4 1 1 8 45 50
> sample2
cellID x_coordinate y_coordinate onEdge parent birth_time death_time
3 3 3 1 1 0 0 50
23 23 3 5 1 0 0 50
28 28 3 4 0 18 12 50
33 33 3 3 0 17 33 50
34 34 3 2 0 8 45 50
sample2 中的单元格完全不相关(它们最近的共同祖先是 0 伪节点)。该函数不应返回任何内容(或仅返回时间 0)。
测试3:
> grid3
cellID x_coordinate y_coordinate onEdge parent birth_time death_time
1 1 1 1 1 0 0 50
2 2 2 1 1 0 0 50
3 3 3 1 1 0 0 50
4 4 4 1 1 0 0 50
5 5 5 1 1 0 0 50
6 6 1 2 1 0 0 50
7 7 2 2 0 0 0 31
8 8 3 2 0 0 0 34
9 9 4 2 0 0 0 37
10 10 5 2 1 0 0 50
11 11 1 3 1 0 0 50
12 12 2 3 0 0 0 22
13 13 3 3 0 0 0 8
14 14 4 3 0 0 0 8
15 15 5 3 1 0 0 6
16 16 1 4 1 0 0 50
17 17 2 4 0 0 0 2
18 18 3 4 0 0 0 2
19 19 4 4 0 0 0 3
20 20 5 4 1 0 0 50
21 21 1 5 1 0 0 50
22 22 2 5 1 0 0 50
23 23 3 5 1 0 0 50
24 24 4 5 1 0 0 50
25 25 5 5 1 0 0 50
26 26 2 4 0 17 2 50
27 27 3 4 0 17 2 3
28 28 3 4 0 27 3 45
29 29 4 4 0 27 3 6
30 30 4 4 0 29 6 50
31 31 5 3 1 29 6 50
32 32 4 3 0 14 8 50
33 33 3 3 0 14 8 22
34 34 3 3 0 33 22 45
35 35 2 3 0 33 22 31
36 36 2 3 0 35 31 50
37 37 2 2 0 35 31 34
38 38 2 2 0 37 34 50
39 39 3 2 0 37 34 37
40 40 3 2 0 39 37 49
41 41 4 2 0 39 37 50
42 42 3 3 0 34 45 49
43 43 3 4 0 34 45 50
44 44 3 3 0 42 49 50
45 45 3 2 0 42 49 50
> sample3 <- subset(grid3, x_coordinate==3 & death_time==50)
> sample3
cellID x_coordinate y_coordinate onEdge parent birth_time death_time
3 3 3 1 1 0 0 50
23 23 3 5 1 0 0 50
43 43 3 4 0 34 45 50
44 44 3 3 0 42 49 50
45 45 3 2 0 42 49 50
这个网格有许多与 x 坐标 3 重叠的事件,但只有两个是有用的:
> res3
cellID x_coordinate y_coordinate onEdge parent birth_time death_time
1 42 3 3 0 34 45 49
2 34 3 3 0 33 22 45
如果有人觉得它有帮助,这是我在每个时间点的每个网格状态的半粗略图(忽略前两行):
非常感谢您的帮助!
解决方案
您的问题很难理解,我不完全理解您需要什么以及为什么选择每个数据行作为结果。我的函数检查在当地社区幸存的每一代人的祖先并返回他们的信息。也许这将为解决您的问题提供指导。
find.elders = function(x, dead, dat){
locals = dat[dat$x_coordinate == x & dat$death_time != dead,]
survivors = dat[dat$x_coordinate == x & dat$death_time == dead,]
anc = survivors$parent
res = NULL
while(any(anc != 0)){
anc = anc[anc > 0]
cat("Ancestors:", anc, "\n")
res = c(res, which(locals$parent %in% anc))
survivors = locals[locals$cellID %in% anc,]
anc = survivors$parent
}
#res = c(res, which(locals$parent %in% anc))
locals[res,]
}
find.elders(3, 50, grid1)
推荐阅读
- ios - 带点的 Swift 动画线条
- microsoft-graph-api - Graph Explorer 获取 Planner 任务
- python - 最小化 RDP(VM) 时自动化脚本失败
- ms-access-2013 - 记录集未更新
- java - 将日期与 Arraylist 中的下一个对象日期进行比较未按预期工作
- c++ - Qt3D SceneLoader 实体未呈现
- file - 将 mws 文件转换为没有 Maple 的文本
- android - Nativecript Fontawesome 5 iphone 不工作
- c++ - How to check if a class is declared in C++?
- c# - 如何使用视图模型中的 IFormFile 解析模型中的字节 [] 数组以进行文件上传控制器?