Dear friends,

I am trying to assign node ages to a phylogenetic tree of subtropical trees
in southern Brazil without using Phylocom and its popular function bladj.
This is because for some reason Phylocom will not open my
Phylomatic-generated tree. I managed to open it with the rncl package as
indicated by François Michonneau.

I am now trying to use the AdjBrLens() function found at (
https://gist.github.com/sckott/938313#file-bladjing_twomethods-r) that uses
Gene Hunt's method (see code below). I disabled the part of the code
referring to Phylocom's bladj function because it won't read my tree, and
also changed line 90 from

tree <- read.tree(trees)

to

tree <- read_newick_phylo(trees)

in order to use the rncl read_newick_phylo function.

Yet, I am receiving an error message

Error in names(node.mins) <- ii :
  'names' attribute [80] must be the same length as the vector [54]

   Do anyone has any idea on how to circunvent this problem?

   Thank you very much in advance,

   Sincerely,

   Alexandre

# Run these functions first (scalePhylo, assign.ages, and assign.brlen)
# scalePhylo, assign.ages, and assign.brlen written by Gene Hunt (
http://paleobiology.si.edu/staff/individuals/hunt.cfm)
# AdjBrLens written by Scott Chamberlain (myrmecocys...@gmail.com)

scalePhylo<- function(tr, tip.ages, node.mins=NULL, min.diff=0.1)
## tr is a 'phylo' object
## tip.ages is a vector of the ages of terminal taxa (best if this vector
has names that match the taxa labels)
## tip.ages MUST BE A NAMED VECTOR, that is, e.g., names(tipages) <-
tree$tip.label [where tipages is a numeric vector of ## the tip ages]
## node.mins is a vector of optional constraints on nodes
## min.diff is the minimum branch length that will be imposed
{
aa<- assign.ages(tr, tip.ages, node.mins, min.diff)
trs<- assign.brlen(tr, aa)
return(trs)
}

assign.ages<- function(tr, tip.ages, node.mins, min.diff)
# Function to assign ages to internal nodes, given tree and
# ages of terminal modes
# --tr is phylo (from package ape)
# --tip.ages is strat ages of terminal taxa
# --node.mins are (optional) minimum ages for nodes (or a subset of nodes)
{
## do some error checking
# makes sure tips have labels
# check node labels?
if(is.null(node.mins)) {node.mins<- rep(NA, times=tr$Nnode)}
node.mins[is.na(node.mins)]<- -Inf # so nodes with no information have no
effect
#print(node.mins)

# change order of ages to match tip labels of tr
oo<- charmatch(tr$tip.label, names(tip.ages))
tip.ages<- tip.ages[oo]
tn<- tr$edge
max.term<- length(tr$tip.label)
min.anc<- max.term+1
max.anc<- min.anc + tr$Nnode -1
aa<- array(dim=max(tn))
names(aa)<- as.character(1:max(tn))
aa[1:max.term]<- tip.ages
ii<- min.anc:max.anc
names(node.mins)<- ii # note: this assumes tree nodes not originally
labelled!!

# go through internal nodes, assign ages to them
while (sum(is.na(aa))>=1) # loop through as long as ages for some inodes
not yet known
{
#print(aa)
for (i in ii)
{
ci<- as.character(i)
yy<- tn[,1]==i
dec<- tn[,2][yy] # direct descendants of i
aad<- aa[as.character(dec)] # ages of these direct descendants
#cat(ci, aad, '\n')
if (sum(is.na(aad))==0) # if all ages are known.
{
aa[ci]<- max(aad) + min.diff # assign age as max() of nodes within
if(aa[ci] < node.mins[ci]) aa[ci]<- node.mins[ci]
}
}
}
return (aa)
}

assign.brlen<- function (t1, all.ages)
# Function to assign branch lengths to all edges of tree
# --t1 is phylo
# --all.ages is vector of ages of terminal and internal nodes (as given by
assign.ages() )
{
ne<- nrow(t1$edge)
bl<- array(dim=ne)

for (i in 1:ne)
{
anc<- t1$edge[i,1]
dec<- t1$edge[i,2]
bl[i]<- all.ages[anc] - all.ages[dec]
}

t2<- t1
t2$edge.length<- bl
return(t2)
}

####
AdjBrLens <- function(trees, ages) {
require(ape)

# read, then write files to where phylocom executable is
tree <- read.tree(trees)
age <- read.table(ages)
write.tree(tree, "phylo")
write.table(age, file = "ages", sep = "\t", col.names = F, row.names = F,
quote = F)

# bladj method
#system("./phylocom bladj > phyloout.txt") # for windows users replace
#with: shell("phylocom bladj > phyloout.txt")
#bladjmethod <- read.tree("phyloout.txt")

# Gene Hunt method
age_ <- age[,2]
names(age_) <- age[,1]

# set node ages
diff_ <- rep(NA, length(setdiff(tree$node.label, names(age_))))
names(diff_) <- setdiff(tree$node.label, names(age_))
nodeages <- c(age_, diff_)

# tipages are set to zero as all species are extant
tipages <- rep(0, length(tree$tip.label))
names(tipages) <- tree$tip.label

# you can set min.diff to get different results
huntmethod <- scalePhylo(tree, tipages, nodeages, min.diff=0)

# plot trees and write to file
pdf(paste("",trees,"TheOG",".pdf","",sep=""))
plot(tree)
dev.off()

# phynodes <- data.frame(bladjmethod$node.label, #1:length(bladjmethod$
node.label))
#names(phynodes) <- c("V1", "order")
#newnodes <- merge(phynodes, age, by="V1", all=T)
#newnodes_sort <- newnodes[order(newnodes[,2]), ]
#bladjmethod$node.label <- newnodes_sort[, 3]

pdf(paste("",trees,"bladjmethod",".pdf","",sep=""))
plot(bladjmethod)
nodelabels(bladjmethod$node.label, cex = 0.6)
dev.off()

pdf(paste("",trees,"genehuntmethod",".pdf","",sep=""))
plot(huntmethod)
dev.off()

# write bladj tree to file
# bladjmethod$node.label <- NULL
# write.tree(bladjmethod, paste("",trees,"bladjtree",".txt","",sep=""))

# combine trees in list
trees_out <- list(tree, bladjmethod, huntmethod)
trees_out

}



My tree

((((((((((acacia_bonariensis,inga_vera),((dalbergia_frutescens,(machaerium_glabrum,machaerium_paraguariense,machaerium_stipitatum)machaerium),(lonchocarpus_campestris,lonchocarpus_nitidus)lonchocarpus)),((celtis_iguanaea,scutia_buxifolia),prunus_myrtifolia)rosales),(((((banara_parviflora,banara_tomentosa)banara,(casearia_decandra,casearia_obliqua,casearia_sylvestris)casearia,(xylosma_pseudosalzmannii,xylosma_tweedianum)xylosma)salicaceae,(sapium_glandulatum,(sebastiania_brasiliensis,sebastiania_commersoniana)sebastiania,stillingia_oppositifolia)euphorbiaceae)malpighiales,(lamanonia_ternata,weinmania_paulliniifolia)cunoniaceae),(maytenus_aquifolia,maytenus_evonymoides)maytenus)celastrales_to_malpighiales),(((((allophylus_edulis,cupania_vernalis,matayba_elaeagnoides)sapindaceae,(cedrela_fissilis,(helietta_apiculata,pilocarpus_pennatifolius,zanthoxylum_fagara)rutaceae,picramnia_parvifolia)),lithraea_brasiliensis),luehea_divaricata),((blepharocalyx_salicifolius,(((calyptranthes_concinna,((myrciaria_delicatula,myrciaria_floribunda,myrciaria_tenella)myrciaria,siphoneugena_reitzii)pliniagroup),(myrceugenia_cucullata,myrceugenia_miersiana,myrceugenia_myrcioides,myrceugenia_oxysepala)myrceugenia),((acca_sellowiana,myrrhinium_atropurpureum,(campomanesia_rhombea,campomanesia_xanthocarpa)campomanesia),((eugenia_involucrata,eugenia_pluriflora,eugenia_psidiiflora,eugenia_uruguayensis)eugenia,(myrcianthes_gigantea,myrcianthes_pungens)myrcianthes)eugeniagroup))),(myrcia_arborescens,myrcia_bombycina,myrcia_lajeana,myrcia_oligantha)myrcia)myrtaceae)malvids),((((((((cestrum_sp,(solanum_pabstii,solanum_pseudoquina,solanum_sanctae-catharinae,solanum_sp)solanum),(((citharexylum_myrianthum,duranta_vestita)verbenaceae,tabebuia_umbellata),cunila_incisa)),(coussarea_contracta,strychnos_brasiliensis)gentianales),(citronella_gongonha,citronella_paniculata)citronella)lamiids,((((dasyphyllum_spinescens,dasyphyllum_tomentosum)dasyphyllum,vernonia_discolor)asteraceae,oreopanax_fulvus),(ilex_brevicuspis,ilex_dumosa,ilex_microdonta,ilex_paraguariensis,ilex_sp)ilex)campanulids),(gordonia_acutifolia,(styrax_leprosus,(symplocos_pentandra,symplocos_tetrandra,symplocos_uniflora)symplocos)))ericales_to_asterales,griselinia_ruscifolia)asterids,seguieria_aculeata)),roupala_brasiliensis)sabiales_to_asterales,(((aiouea_saligna,(cinnamomum_amoenum,cinnamomum_glaziovii)cinnamomum,(cryptocarya_aschersoniana,cryptocarya_moschata)cryptocarya,nectandra_megapotamica,(ocotea_indecora,ocotea_lancifolia,ocotea_puberula,ocotea_pulchella)ocotea)lauraceae,(hennecartia_omphalandra,mollinedia_elegans)monimiaceae),(rollinia_rugulosa,rollinia_salicifolia,rollinia_sylvatica)rollinia))magnoliales_to_asterales,(araucaria_angustifolia,podocarpus_lambertii))seedplants,alsophila_sp)euphyllophyte;


-- 
Dr. Alexandre F. Souza
Professor Ajunto II
Universidade Federal do Rio Grande do Norte
CB, Departamento de Ecologia
Campus Universitário - Lagoa Nova
59072-970 - Natal, RN - Brasil
lattes: lattes.cnpq.br/7844758818522706
http://www.docente.ufrn.br/alexsouza

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-phylo mailing list - R-sig-phylo@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-phylo
Searchable archive at http://www.mail-archive.com/r-sig-phylo@r-project.org/

Reply via email to