2012-07-06 45 views
0

我在着色與「猿」(慢邊着色)如何避免循環所有tip.labels(包APE)

自從我用C編程始終,我還是有些功能產生phylo樹邊發現很難以循環式的方式停止思考。

我能想到做到這一點的唯一方法是(1)循環所有tip.labels(ID),(2)找出哪些邊緣屬於它們,(3)設置所需的顏色。

這樣做是1加1,因此是非常緩慢的大樹:

tsampltime.rooted=structure(list(edge = structure(c(24L, 24L, 24L, 24L, 24L, 25L, 
26L, 26L, 27L, 27L, 28L, 28L, 25L, 29L, 29L, 30L, 30L, 30L, 30L, 
24L, 31L, 31L, 32L, 32L, 32L, 33L, 33L, 34L, 35L, 35L, 34L, 36L, 
36L, 34L, 37L, 37L, 1L, 2L, 12L, 23L, 25L, 26L, 6L, 27L, 5L, 
28L, 3L, 4L, 29L, 7L, 30L, 8L, 9L, 10L, 11L, 31L, 13L, 32L, 21L, 
22L, 33L, 20L, 34L, 35L, 14L, 15L, 36L, 16L, 17L, 37L, 18L, 19L 
), .Dim = c(36L, 2L)), Nnode = 14L, tip.label = c("0", "2325", 
"55304", "124953", "72254", "66507", "85089", "110256", "123265", 
"97350", "123721", "36770", "48692", "110612", "97224", "104337", 
"124625", "128499", "120928", "88404", "73335", "75059", "17928" 
), edge.length = c(0, 0.953297, 8.054944, 4.4120893, 9.173083, 
1.409346, 3.752752, 0.483517, 4.620875, 0.582417, 0.510989, 12.4862723, 
6.291209, 1.920329, 3.071429, 4.5027528, 5.497248, 2.777472, 
5.5274749, 8.414843, 2.5467017, 3.79121, 3.824171, 3.961538, 
3.804944, 2.126375, 1.75275, 1.93956, 3.3516546, 1.57418, 2.31319, 
2.22528, 4.0384651, 3.898348, 2.722523, 1.87088)), .Names = c("edge", 
"Nnode", "tip.label", "edge.length"), class = "phylo", order = "cladewise") 
    ... 
#distValuesPerId[,] has [LABELID,COLOR] 
distValuesPerId=source('http://ubuntuone.com/5y7ZYCWfE73T5lhnUpmeXc') 
... 
uniqueIDs=unique(tree$tip.label) 
distTrdsampledcol <-rep("black", length(tree$edge)) #init in black 
for(i in uniqueIDs) { #(1) 
    a= c(which(tree$tip.label==i)) 
    b= which(tree$edge[,2]== a) #(2) 
    distTrdsampledcol [ b ] <- distValuesPerId[i,2] #(3) 
} 
... 
#plot(tree, edge.color=distTrdsampledcol) 

誰能幫助我重新考慮這一點?這樣做更有效率嗎?

enter image description here

提前感謝!

j

+0

使用'dput(樹)'一棵小樹上給我們一些樣本數據。 – nograpes 2012-07-06 19:32:53

+0

@nograpes,在這裏它是: [dput(distValuesPerId)](http://ubuntuone.com/5y7ZYCWfE73T5lhnUpmeXc) [dput(樹)](http://ubuntuone.com/3GdyoX4ETlU5cDuRpPMfcN) [圖(樹)(http://ubuntuone.com/5ikA8OIrN9nQrpIgpzTehg) 感謝您的答覆 – lourencoj 2012-07-09 13:45:54

+0

實際上,你可以只發布圖片你的問題裏面,你也應該複製的代碼來創建你的樹有好。我現在就爲你做。它不是張貼'data.frame' 12萬行是個好主意。你應該採取只需要指出data.frame的顏色,並張貼。 – nograpes 2012-07-09 14:34:31

回答

2

您可能會過度譴責這一點。只需從您的巨型顏色data.frame中選擇所需的顏色即可。

plot(tree,edge.color=distValuesPerId[tree$tip.label,2]) 

嘗試一下例子名單上?plot.phylo,他們有一噸的很酷的東西,你可以用你的樹,包括着色做例子。它可能會給你一些想法。


看到您的評論後,我意識到,我誤解了這個問題。這應該做你想要什麼沒有循環:

cols=distValuesPerId[match(tree$tip.label[tree$edge[,2]],distValuesPerId[,1]),2] 
my.cols=ifelse(is.na(cols),'black',cols) 
plot(tree, edge.color=my.cols) 

其分解:

# Find the tip labels associated with each edge, NA if it is not an edge to a tip 
edge.tip.labels=tree$tip.label[tree$edge[,2]] 
# Match each of those tip labels to the label column in your colur data frame 
edge.rows=match(edge.tip.labels,distValuesPerId[,1]) 
# Find the colour for each of those rows 
cols=distValuesPerId[edge.rows,2] 
# Where it is NA, convert it to 'black' (where it is not a 'tip edge') 
my.cols=ifelse(is.na(cols),'black',cols) 
+0

您的建議假定distValuesPerId給力「指數」給出的ID(標籤),但事實並非如此......人們必須尋找對應所需ID 我同意我可能會在邊緣指數想,但是不能進行排序更好的辦法 生病看看例子 感謝 – lourencoj 2012-07-09 16:43:48

+0

對不起,我誤解了這個問題。看到我更新的回覆。 – nograpes 2012-07-09 18:15:29

+0

嘿嘿,謝謝!那工作 – lourencoj 2012-07-18 10:53:00