Re: [R-sig-phylo] Count the number of character/state changes in a tree

2015-03-03 Thread f.k...@mailbox.org
Hi Luiz,

didn’t try that out, but here is some code I wrote for some of the 
megaphylogenies I produced where you don’t want to search in a pdf or so where 
the transitions are…

It’s two months ago so don’t know if it was totally save
I think you should make sure the order of the tree and tip.states fit.
If I look at it now there might be shorter solutions ;-)

Have fun,
Franz



# Franz Krah
# count origins
# 02 - 04 - 2015

origins - function(phy, tip.states, node.states, threshold.states=75, 
threshold.nodes=80) 
  {
  if (!is.rooted(phy))
stop( phy is not rooted )
  if (!any(as.numeric(tip.states)1))
stop( tips need to be a binary in form O/1 )
  nodes - data.frame(node.states)
  names(nodes) - c(0:(length(nodes[1,])-1))
  # threshold states
sortStates - function(x, threshold){
  yy - (x  threshold)
  states - matrix(,length(x[,1]))
  for (i in 1:length(x[,1]))
  {
# if both  threshold
if (sum(yy[1,])==0)
  states[i] - NA
# if one is  threshold
if (!sum(yy[i,])==0)
  states[i] - which(x[i,]threshold)-1
  }
  return(states)
}
  states - sortStates(nodes, threshold=threshold.states)
  states - data.frame(states, tn=phy$node.label)
  # threshold nodes
  states - ifelse(states$tn  threshold.nodes,NA, states$states)
  cstates - data.frame(states=c(as.vector(tip.states),states))
  if (length(cstates$states)!=(length(phy$edge[,1])+1))
stop( phy and edge+tips length are not the same )
  l_nodes - cstates$states[phy$edge[,1]]
  r_nodes - cstates$states[phy$edge[,2]]
  diff - (as.numeric(l_nodes) - as.numeric(r_nodes))
  wins - sum(diff  0, na.rm=T)
  loss - sum(diff  0, na.rm=T)
  equal - sum(diff==0, na.rm=T)
  dep_nod - which(!is.na(states)) # depest node above threshold
  dep_nod - states[dep_nod[1]]
  res - c(wins, loss, equal)
  names(res) - c(won,lost,equal)
  out - paste(State of deepest node above threshold:, dep_nod, sep= )
  res - list(out, res)
  print(res)
  }

 On 03 Mar 2015, at 14:04, Luiz Max Carvalho luizepidemiolo...@gmail.com 
 wrote:
 
 Dear Phylofolks,
 
 Suppose I have a phylo object phy and the states at all nodes (internal and
 external). Would the following function give me the number of state changes?
 
 get.changes - function(tree, states){
  sum(apply(phy$edge, 1, function(x)  states[x[1]]!=states[x[2]]))
 }
 
 If not, what would?
 
 Thanks in advance,
 
 Luiz
 -- 
 Luiz Max Fagundes de Carvalho
 PhD student, Institute of Evolutionary Biology,  School of Biological
 Sciences,
 Ashworth Laboratories, Ash 2, office 123
 University of Edinburgh, United Kingdom.
 http://br.linkedin.com/pub/luiz-max-carvalho/49/687/283
 
   [[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/

___
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/

[R-sig-phylo] Count the number of character/state changes in a tree

2015-03-03 Thread Luiz Max Carvalho
Dear Phylofolks,

Suppose I have a phylo object phy and the states at all nodes (internal and
external). Would the following function give me the number of state changes?

get.changes - function(tree, states){
  sum(apply(phy$edge, 1, function(x)  states[x[1]]!=states[x[2]]))
}

If not, what would?

Thanks in advance,

Luiz
-- 
Luiz Max Fagundes de Carvalho
PhD student, Institute of Evolutionary Biology,  School of Biological
Sciences,
Ashworth Laboratories, Ash 2, office 123
University of Edinburgh, United Kingdom.
http://br.linkedin.com/pub/luiz-max-carvalho/49/687/283

[[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/


Re: [R-sig-phylo] Count the number of character/state changes in a tree

2015-03-03 Thread Liam J. Revell
If the vector states is indexed by the node numbers of phy$edge, then 
the following seems like it should work:


get.changes-function(tree,states)
  sum(apply(tree$edge,1,function(x,s) s[x[1]]!=s[x[2]],s=states))

This will of course only be the minimum number of changes given your 
states at tips  internal nodes  it will not account for multiple 
changes along a single edge. Where do your states at internal nodes come 
from?


All the best, Liam

Liam J. Revell, Assistant Professor of Biology
University of Massachusetts Boston
web: http://faculty.umb.edu/liam.revell/
email: liam.rev...@umb.edu
blog: http://blog.phytools.org

On 3/3/2015 2:04 PM, Luiz Max Carvalho wrote:

Dear Phylofolks,

Suppose I have a phylo object phy and the states at all nodes (internal and
external). Would the following function give me the number of state changes?

get.changes - function(tree, states){
   sum(apply(phy$edge, 1, function(x)  states[x[1]]!=states[x[2]]))
}

If not, what would?

Thanks in advance,

Luiz



___
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/


[R-sig-phylo] Stuck with Phylomatic tree!

2015-03-03 Thread Alexandre F. Souza
Dear friends,

   I am trying to adjust the leghts of a tree I created using 113 tree
species in Phylomatic using the bladj function in Phylocom. Unfortunately,
the program crashes when I use the tree without the final note about the
impossibility of matching four of the species (when I exclude the final
note), or produces an empty output file when I use the newick file with the
final note.

   I tried to open it directly in R using the read.newick() function
written by Liam J. Revell (
http://www.phytools.org/read.newick/v0.4/read.newick.R) since the tree
probably has single nodes. However, it did not work and produced an eternal
loop I guess (working simbol never turned off).

   I plan to use the tree in further ecological analysis and the steps
would be to adjust ages with Phylocom, then cut off single nodes in R and
then to proceed with analyses. I am, however, stuck.

   Could you plase help me? I am new to phylogenetic analyses so I
apologize if I be repetitive, but I could not find an answer searching
previous correspondence. What should I do? Is there a way to produce a tree
directly in R?

   Here is the newick tree, in case you may find it useful to take a look.

   Thank you very much in advance,

   Alexandre