Re: [R-sig-phylo] Extract all possible clades from a tree

2017-01-06 Thread Kamila Naxerova
This new code works perfectly for me. 

Thanks so much everyone for helping me to solve this issue in record time!

Kamila

> On Jan 6, 2017, at 6:39 AM, Emmanuel Paradis  wrote:
> 
> I paste below the new code of extract.clade for those who want to try it.
> 
> Best,
> 
> Emmanuel
> 
> 
> extract.clade <- function(phy, node, root.edge = 0, interactive = FALSE)
> {
>n <- length(phy$tip.label)
>if (interactive) {
>cat("Click close to the node...\n")
>node <- identify(phy)$nodes
>} else {
>if (length(node) > 1) {
>node <- node[1]
>warning("only the first value of 'node' has been considered")
>}
>if (is.character(node)) {
>if (is.null(phy$node.label))
>stop("the tree has no node labels")
>node <- match(node, phy$node.label) + n
>if (is.na(node)) stop("'node' not among the node labels.")
>}
>if (node <= n)
>stop("node number must be greater than the number of tips")
>}
>if (node == n + 1L) return(phy)
>keep <- prop.part(phy)[[node - n]]
>drop.tip(phy, (1:n)[-keep], root.edge = root.edge, rooted = TRUE)
> }
> 
> 
> Le 06/01/2017 à 10:17, Emmanuel Paradis a écrit :
>> Hi,
>> 
>> Klaus is right: extract.clade() fails if the tree has been rooted with
>> resolve.root = TRUE before. I'm going to rewrite the function calling
>> drop.tip (which will be safer). In the meantime, Klaus's function should
>> work for Kamila.
>> 
>> Best,
>> 
>> Emmanuel
>> 
>> Le 06/01/2017 à 05:17, Klaus Schliep a écrit :
>>> I had a look at the tree. There seems a bug in extract.clades, when the
>>> tree was rooted before with the root() function. extract.clade() seem to
>>> expect some ordering of nodes which seems not satisfied (and it looks as
>>> it was written a long time ago).
>>> 
>>> I just wrote a small function (easier than to debug the old one) which
>>> should do the same and it seems to work:
>>> 
>>> library(phangorn)
>>> 
>>> extract.clade2 <- function(phy, node){
>>>if(!(node %in% phy$edge[,1])) stop("node number must be greater than
>>> the number of tips")
>>>drop.tip(phy, setdiff(1:Ntip(phy), Descendants(phy, node)[[1]]))
>>> }
>>> 
>>> Klaus
>>> 
>>> 
>>> 
>>> On Jan 5, 2017 10:25 PM, "Joseph W. Brown" >> > wrote:
>>> 
>>>Hmm. Maybe something wonky with your tree? I simulated a 17-tip tree
>>>and tried things out:
>>> 
>>>phy <- rtree(17);
>>>rootID <- length(phy$tip.label) + 1;
>>>counter <- 1;
>>>for (i in rootID:max(phy$edge[,1])) {
>>>  clade <- extract.clade(phy, i);
>>>  # do something. just printing clade properties here
>>>  print(paste0("clade ", counter, " (node ", i, ") has ",
>>>length(clade$tip.label), " tips."));
>>>  counter <- counter + 1;
>>>}
>>> 
>>>[1] "clade 1 (node 18) has 17 tips."
>>>[1] "clade 2 (node 19) has 11 tips."
>>>[1] "clade 3 (node 20) has 10 tips."
>>>[1] "clade 4 (node 21) has 4 tips."
>>>[1] "clade 5 (node 22) has 3 tips."
>>>[1] "clade 6 (node 23) has 2 tips."
>>>[1] "clade 7 (node 24) has 6 tips."
>>>[1] "clade 8 (node 25) has 5 tips."
>>>[1] "clade 9 (node 26) has 3 tips."
>>>[1] "clade 10 (node 27) has 2 tips."
>>>[1] "clade 11 (node 28) has 2 tips."
>>>[1] "clade 12 (node 29) has 6 tips."
>>>[1] "clade 13 (node 30) has 2 tips."
>>>[1] "clade 14 (node 31) has 4 tips."
>>>[1] "clade 15 (node 32) has 3 tips."
>>>[1] "clade 16 (node 33) has 2 tips."
>>> 
>>>and had no problem. Maybe post your tree here?
>>> 
>>>JWB
>>>
>>>Joseph W. Brown
>>>Post-doctoral Researcher, Smith Laboratory
>>>University of Michigan
>>>Department of Ecology & Evolutionary Biology
>>>Room 2071, Kraus Natural Sciences Building
>>>Ann Arbor MI 48109-1079
>>>josep...@umich.edu 
>>> 
>>> 
>>> 
>>>> On 5 Jan, 2017, at 17:18, Kamila Naxerova >>> wrote:
>>>>
>>>> Dear Joseph,
>>>>
>>>> thanks so much. This is exactly what I need!
>>>>
>>>> I am running into some problems that I don’t understand though. In
>>>my case, rootID is 18, and max(phy$edge[,1]) is 33. When I try to
>>>execute your loop, this happens:
>>>>
>>>> > extract.clade(phy, 18)
>>>>
>>>> Phylogenetic tree with 17 tips and 16 internal nodes.
>>>>
>>>> Tip labels:
>>>>   X1, X8, X9, X10 ...
>>>>
>>>> Rooted; includes branch lengths.
>>>>
>>>> So far so good… but then I keep getting these errors:
>>>>
>>>> > extract.clade(phy, 19)
>>>> Error in phy$edge[, 2] : incorrect number of dimensions

Re: [R-sig-phylo] Extract all possible clades from a tree

2017-01-06 Thread Emmanuel Paradis

I paste below the new code of extract.clade for those who want to try it.

Best,

Emmanuel


extract.clade <- function(phy, node, root.edge = 0, interactive = FALSE)
{
n <- length(phy$tip.label)
if (interactive) {
cat("Click close to the node...\n")
node <- identify(phy)$nodes
} else {
if (length(node) > 1) {
node <- node[1]
warning("only the first value of 'node' has been considered")
}
if (is.character(node)) {
if (is.null(phy$node.label))
stop("the tree has no node labels")
node <- match(node, phy$node.label) + n
if (is.na(node)) stop("'node' not among the node labels.")
}
if (node <= n)
stop("node number must be greater than the number of tips")
}
if (node == n + 1L) return(phy)
keep <- prop.part(phy)[[node - n]]
drop.tip(phy, (1:n)[-keep], root.edge = root.edge, rooted = TRUE)
}


Le 06/01/2017 à 10:17, Emmanuel Paradis a écrit :

Hi,

Klaus is right: extract.clade() fails if the tree has been rooted with
resolve.root = TRUE before. I'm going to rewrite the function calling
drop.tip (which will be safer). In the meantime, Klaus's function should
work for Kamila.

Best,

Emmanuel

Le 06/01/2017 à 05:17, Klaus Schliep a écrit :

I had a look at the tree. There seems a bug in extract.clades, when the
tree was rooted before with the root() function. extract.clade() seem to
expect some ordering of nodes which seems not satisfied (and it looks as
it was written a long time ago).

I just wrote a small function (easier than to debug the old one) which
should do the same and it seems to work:

library(phangorn)

extract.clade2 <- function(phy, node){
if(!(node %in% phy$edge[,1])) stop("node number must be greater than
the number of tips")
drop.tip(phy, setdiff(1:Ntip(phy), Descendants(phy, node)[[1]]))
}

Klaus



On Jan 5, 2017 10:25 PM, "Joseph W. Brown" > wrote:

Hmm. Maybe something wonky with your tree? I simulated a 17-tip tree
and tried things out:

phy <- rtree(17);
rootID <- length(phy$tip.label) + 1;
counter <- 1;
for (i in rootID:max(phy$edge[,1])) {
  clade <- extract.clade(phy, i);
  # do something. just printing clade properties here
  print(paste0("clade ", counter, " (node ", i, ") has ",
length(clade$tip.label), " tips."));
  counter <- counter + 1;
}

[1] "clade 1 (node 18) has 17 tips."
[1] "clade 2 (node 19) has 11 tips."
[1] "clade 3 (node 20) has 10 tips."
[1] "clade 4 (node 21) has 4 tips."
[1] "clade 5 (node 22) has 3 tips."
[1] "clade 6 (node 23) has 2 tips."
[1] "clade 7 (node 24) has 6 tips."
[1] "clade 8 (node 25) has 5 tips."
[1] "clade 9 (node 26) has 3 tips."
[1] "clade 10 (node 27) has 2 tips."
[1] "clade 11 (node 28) has 2 tips."
[1] "clade 12 (node 29) has 6 tips."
[1] "clade 13 (node 30) has 2 tips."
[1] "clade 14 (node 31) has 4 tips."
[1] "clade 15 (node 32) has 3 tips."
[1] "clade 16 (node 33) has 2 tips."

and had no problem. Maybe post your tree here?

JWB

Joseph W. Brown
Post-doctoral Researcher, Smith Laboratory
University of Michigan
Department of Ecology & Evolutionary Biology
Room 2071, Kraus Natural Sciences Building
Ann Arbor MI 48109-1079
josep...@umich.edu 



> On 5 Jan, 2017, at 17:18, Kamila Naxerova > wrote:
>
> Dear Joseph,
>
> thanks so much. This is exactly what I need!
>
> I am running into some problems that I don’t understand though. In
my case, rootID is 18, and max(phy$edge[,1]) is 33. When I try to
execute your loop, this happens:
>
> > extract.clade(phy, 18)
>
> Phylogenetic tree with 17 tips and 16 internal nodes.
>
> Tip labels:
>   X1, X8, X9, X10 ...
>
> Rooted; includes branch lengths.
>
> So far so good… but then I keep getting these errors:
>
> > extract.clade(phy, 19)
> Error in phy$edge[, 2] : incorrect number of dimensions
> > extract.clade(phy, 20)
> Error in phy$edge[, 2] : incorrect number of dimensions
>
> Not sure why extract.clade produces these errors. 19-23 don’t
work, 24-26 work, 27 produces the error again, 28 works etc.
>
> Thanks again.
>
> Kamila
>
>
>> On Jan 5, 2017, at 4:12 PM, Joseph W. Brown  >> wrote:
>>
>> Not sure if I understand the problem completely, but this should
allow you to examine all of the clades (and should 

Re: [R-sig-phylo] Extract all possible clades from a tree

2017-01-06 Thread Emmanuel Paradis

Hi,

Klaus is right: extract.clade() fails if the tree has been rooted with 
resolve.root = TRUE before. I'm going to rewrite the function calling 
drop.tip (which will be safer). In the meantime, Klaus's function should 
work for Kamila.


Best,

Emmanuel

Le 06/01/2017 à 05:17, Klaus Schliep a écrit :

I had a look at the tree. There seems a bug in extract.clades, when the
tree was rooted before with the root() function. extract.clade() seem to
expect some ordering of nodes which seems not satisfied (and it looks as
it was written a long time ago).

I just wrote a small function (easier than to debug the old one) which
should do the same and it seems to work:

library(phangorn)

extract.clade2 <- function(phy, node){
if(!(node %in% phy$edge[,1])) stop("node number must be greater than
the number of tips")
drop.tip(phy, setdiff(1:Ntip(phy), Descendants(phy, node)[[1]]))
}

Klaus



On Jan 5, 2017 10:25 PM, "Joseph W. Brown" > wrote:

Hmm. Maybe something wonky with your tree? I simulated a 17-tip tree
and tried things out:

phy <- rtree(17);
rootID <- length(phy$tip.label) + 1;
counter <- 1;
for (i in rootID:max(phy$edge[,1])) {
  clade <- extract.clade(phy, i);
  # do something. just printing clade properties here
  print(paste0("clade ", counter, " (node ", i, ") has ",
length(clade$tip.label), " tips."));
  counter <- counter + 1;
}

[1] "clade 1 (node 18) has 17 tips."
[1] "clade 2 (node 19) has 11 tips."
[1] "clade 3 (node 20) has 10 tips."
[1] "clade 4 (node 21) has 4 tips."
[1] "clade 5 (node 22) has 3 tips."
[1] "clade 6 (node 23) has 2 tips."
[1] "clade 7 (node 24) has 6 tips."
[1] "clade 8 (node 25) has 5 tips."
[1] "clade 9 (node 26) has 3 tips."
[1] "clade 10 (node 27) has 2 tips."
[1] "clade 11 (node 28) has 2 tips."
[1] "clade 12 (node 29) has 6 tips."
[1] "clade 13 (node 30) has 2 tips."
[1] "clade 14 (node 31) has 4 tips."
[1] "clade 15 (node 32) has 3 tips."
[1] "clade 16 (node 33) has 2 tips."

and had no problem. Maybe post your tree here?

JWB

Joseph W. Brown
Post-doctoral Researcher, Smith Laboratory
University of Michigan
Department of Ecology & Evolutionary Biology
Room 2071, Kraus Natural Sciences Building
Ann Arbor MI 48109-1079
josep...@umich.edu 



> On 5 Jan, 2017, at 17:18, Kamila Naxerova > wrote:
>
> Dear Joseph,
>
> thanks so much. This is exactly what I need!
>
> I am running into some problems that I don’t understand though. In
my case, rootID is 18, and max(phy$edge[,1]) is 33. When I try to
execute your loop, this happens:
>
> > extract.clade(phy, 18)
>
> Phylogenetic tree with 17 tips and 16 internal nodes.
>
> Tip labels:
>   X1, X8, X9, X10 ...
>
> Rooted; includes branch lengths.
>
> So far so good… but then I keep getting these errors:
>
> > extract.clade(phy, 19)
> Error in phy$edge[, 2] : incorrect number of dimensions
> > extract.clade(phy, 20)
> Error in phy$edge[, 2] : incorrect number of dimensions
>
> Not sure why extract.clade produces these errors. 19-23 don’t
work, 24-26 work, 27 produces the error again, 28 works etc.
>
> Thanks again.
>
> Kamila
>
>
>> On Jan 5, 2017, at 4:12 PM, Joseph W. Brown  >> wrote:
>>
>> Not sure if I understand the problem completely, but this should
allow you to examine all of the clades (and should work if
polytomies are involved):
>>
>> # for tree phy
>> rootID <- length(phy$tip.label) + 1;
>> for (i in rootID:max(phy$edge[,1])) {
>>   clade <- extract.clade(phy, i);
>>   # do something
>> }
>>
>> This includes the root node (i.e. whole tree), but that can be
changed. This can be rewritten as an lapply if necessary.
>>
>> HTH.
>> JWB
>> 
>> Joseph W. Brown
>> Post-doctoral Researcher, Smith Laboratory
>> University of Michigan
>> Department of Ecology & Evolutionary Biology
>> Room 2071, Kraus Natural Sciences Building
>> Ann Arbor MI 48109-1079
>> josep...@umich.edu 
>
>>
>>
>>
>>> On 5 Jan, 2017, at 15:50, Kamila Naxerova

>> wrote:
>>>
>>> Hi all,
>>>
>>> I would like to break a phylogenetic tree into all possible
clades and then examine each one of them for 

Re: [R-sig-phylo] Extract all possible clades from a tree

2017-01-05 Thread Joseph W. Brown
Hmm. Maybe something wonky with your tree? I simulated a 17-tip tree and tried 
things out:

phy <- rtree(17);
rootID <- length(phy$tip.label) + 1;
counter <- 1;
for (i in rootID:max(phy$edge[,1])) {
  clade <- extract.clade(phy, i);
  # do something. just printing clade properties here
  print(paste0("clade ", counter, " (node ", i, ") has ", 
length(clade$tip.label), " tips."));
  counter <- counter + 1;
}

[1] "clade 1 (node 18) has 17 tips."
[1] "clade 2 (node 19) has 11 tips."
[1] "clade 3 (node 20) has 10 tips."
[1] "clade 4 (node 21) has 4 tips."
[1] "clade 5 (node 22) has 3 tips."
[1] "clade 6 (node 23) has 2 tips."
[1] "clade 7 (node 24) has 6 tips."
[1] "clade 8 (node 25) has 5 tips."
[1] "clade 9 (node 26) has 3 tips."
[1] "clade 10 (node 27) has 2 tips."
[1] "clade 11 (node 28) has 2 tips."
[1] "clade 12 (node 29) has 6 tips."
[1] "clade 13 (node 30) has 2 tips."
[1] "clade 14 (node 31) has 4 tips."
[1] "clade 15 (node 32) has 3 tips."
[1] "clade 16 (node 33) has 2 tips."

and had no problem. Maybe post your tree here?

JWB

Joseph W. Brown
Post-doctoral Researcher, Smith Laboratory
University of Michigan
Department of Ecology & Evolutionary Biology
Room 2071, Kraus Natural Sciences Building
Ann Arbor MI 48109-1079
josep...@umich.edu



> On 5 Jan, 2017, at 17:18, Kamila Naxerova  wrote:
> 
> Dear Joseph,
> 
> thanks so much. This is exactly what I need!
> 
> I am running into some problems that I don’t understand though. In my case, 
> rootID is 18, and max(phy$edge[,1]) is 33. When I try to execute your loop, 
> this happens:
> 
> > extract.clade(phy, 18)
> 
> Phylogenetic tree with 17 tips and 16 internal nodes.
> 
> Tip labels:
>   X1, X8, X9, X10 ...
> 
> Rooted; includes branch lengths.
> 
> So far so good… but then I keep getting these errors:
> 
> > extract.clade(phy, 19)
> Error in phy$edge[, 2] : incorrect number of dimensions
> > extract.clade(phy, 20)
> Error in phy$edge[, 2] : incorrect number of dimensions
> 
> Not sure why extract.clade produces these errors. 19-23 don’t work, 24-26 
> work, 27 produces the error again, 28 works etc. 
> 
> Thanks again. 
> 
> Kamila
> 
> 
>> On Jan 5, 2017, at 4:12 PM, Joseph W. Brown > > wrote:
>> 
>> Not sure if I understand the problem completely, but this should allow you 
>> to examine all of the clades (and should work if polytomies are involved):
>> 
>> # for tree phy
>> rootID <- length(phy$tip.label) + 1;
>> for (i in rootID:max(phy$edge[,1])) {
>>   clade <- extract.clade(phy, i);
>>   # do something
>> }
>> 
>> This includes the root node (i.e. whole tree), but that can be changed. This 
>> can be rewritten as an lapply if necessary.
>> 
>> HTH.
>> JWB
>> 
>> Joseph W. Brown
>> Post-doctoral Researcher, Smith Laboratory
>> University of Michigan
>> Department of Ecology & Evolutionary Biology
>> Room 2071, Kraus Natural Sciences Building
>> Ann Arbor MI 48109-1079
>> josep...@umich.edu 
>> 
>> 
>> 
>>> On 5 Jan, 2017, at 15:50, Kamila Naxerova >> > wrote:
>>> 
>>> Hi all, 
>>> 
>>> I would like to break a phylogenetic tree into all possible clades and then 
>>> examine each one of them for certain characteristics.
>>> 
>>> I am writing some code to do this from scratch, but it’s getting pretty 
>>> cumbersome quickly. 
>>> 
>>> I was wondering whether there are some functions out there already that 
>>> could help me with this task?
>>> 
>>> Thanks so much for any help.
>>> 
>>> Cheers,
>>> Kamila
>>> 
>>> 
>>> The information in this e-mail is intended only for the person to whom it is
>>> addressed. If you believe this e-mail was sent to you in error and the 
>>> e-mail
>>> contains patient information, please contact the Partners Compliance 
>>> HelpLine at
>>> http://www.partners.org/complianceline 
>>>  . If the e-mail was sent to you in 
>>> error
>>> but does not contain patient information, please contact the sender and 
>>> properly
>>> dispose of the e-mail.
>>> ___
>>> 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/ 
>>> 
> 


[[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] Extract all possible clades from a tree

2017-01-05 Thread Kamila Naxerova
Dear Joseph,

thanks so much. This is exactly what I need!

I am running into some problems that I don’t understand though. In my case, 
rootID is 18, and max(phy$edge[,1]) is 33. When I try to execute your loop, 
this happens:

> extract.clade(phy, 18)

Phylogenetic tree with 17 tips and 16 internal nodes.

Tip labels:
X1, X8, X9, X10 ...

Rooted; includes branch lengths.

So far so good… but then I keep getting these errors:

> extract.clade(phy, 19)
Error in phy$edge[, 2] : incorrect number of dimensions
> extract.clade(phy, 20)
Error in phy$edge[, 2] : incorrect number of dimensions

Not sure why extract.clade produces these errors. 19-23 don’t work, 24-26 work, 
27 produces the error again, 28 works etc. 

Thanks again. 

Kamila


> On Jan 5, 2017, at 4:12 PM, Joseph W. Brown  wrote:
> 
> Not sure if I understand the problem completely, but this should allow you to 
> examine all of the clades (and should work if polytomies are involved):
> 
> # for tree phy
> rootID <- length(phy$tip.label) + 1;
> for (i in rootID:max(phy$edge[,1])) {
>   clade <- extract.clade(phy, i);
>   # do something
> }
> 
> This includes the root node (i.e. whole tree), but that can be changed. This 
> can be rewritten as an lapply if necessary.
> 
> HTH.
> JWB
> 
> Joseph W. Brown
> Post-doctoral Researcher, Smith Laboratory
> University of Michigan
> Department of Ecology & Evolutionary Biology
> Room 2071, Kraus Natural Sciences Building
> Ann Arbor MI 48109-1079
> josep...@umich.edu 
> 
> 
> 
>> On 5 Jan, 2017, at 15:50, Kamila Naxerova > > wrote:
>> 
>> Hi all, 
>> 
>> I would like to break a phylogenetic tree into all possible clades and then 
>> examine each one of them for certain characteristics.
>> 
>> I am writing some code to do this from scratch, but it’s getting pretty 
>> cumbersome quickly. 
>> 
>> I was wondering whether there are some functions out there already that 
>> could help me with this task?
>> 
>> Thanks so much for any help.
>> 
>> Cheers,
>> Kamila
>> 
>> 
>> The information in this e-mail is intended only for t...{{dropped:24}}

___
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] Extract all possible clades from a tree

2017-01-05 Thread Joseph W. Brown
Not sure if I understand the problem completely, but this should allow you to 
examine all of the clades (and should work if polytomies are involved):

# for tree phy
rootID <- length(phy$tip.label) + 1;
for (i in rootID:max(phy$edge[,1])) {
  clade <- extract.clade(phy, i);
  # do something
}

This includes the root node (i.e. whole tree), but that can be changed. This 
can be rewritten as an lapply if necessary.

HTH.
JWB

Joseph W. Brown
Post-doctoral Researcher, Smith Laboratory
University of Michigan
Department of Ecology & Evolutionary Biology
Room 2071, Kraus Natural Sciences Building
Ann Arbor MI 48109-1079
josep...@umich.edu



> On 5 Jan, 2017, at 15:50, Kamila Naxerova  wrote:
> 
> Hi all, 
> 
> I would like to break a phylogenetic tree into all possible clades and then 
> examine each one of them for certain characteristics.
> 
> I am writing some code to do this from scratch, but it’s getting pretty 
> cumbersome quickly. 
> 
> I was wondering whether there are some functions out there already that could 
> help me with this task?
> 
> Thanks so much for any help.
> 
> Cheers,
> Kamila
> 
> 
> The information in this e-mail is intended only for th...{{dropped:21}}

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