distances between leaf nodes

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

distances between leaf nodes

jdetras (Bugzilla)
Hi,

I am new at using the Bio::TreeIO module specifically using the newick
format for a phylogenetic analysis. The sample_tree attached is
Newick-formatted tree. My objective is to get all the distances between all
the leaf nodes. I copied examples of the code from
http://www.bioperl.org/wiki/HOWTO:Trees but it does not tell me much (to my
knowledge) so that I understand how to assign the right array value for the
nodes/leaves. The message would say must provide 2 root nodes.

Here is what I have right now:

#!/usr/bin/perl -w
 use strict;

 my $treefile = 'sample_tree';
 use Bio::TreeIO;
 my $treeio = Bio::TreeIO->new(-format => 'newick',
                                 -file => $treefile);

 while (my $tree = $treeio->next_tree) {
         my @leaves = $tree->get_leaf_nodes;
         for (my $dist = $tree->distance(-nodes => \@leaves)){
                 print "Distance between trees is $dist\n";
         }
 }

Thanks,
Jeff

_______________________________________________
Bioperl-l mailing list
[hidden email]
http://lists.open-bio.org/mailman/listinfo/bioperl-l

sample_tree (580 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: distances between leaf nodes

Jason Stajich-3
You should only have TWO nodes in the array not all the leaves.

=head2 distance

  Title   : distance
  Usage   : distance(-nodes => \@nodes )
  Function: returns the distance between TWO given nodes
  Returns : numerical distance
  Args    : -nodes => arrayref of nodes to test
            or ($node1, $node2)

=cut


Jeffrey Detras wrote, On 3/4/10 10:17 PM:

> Hi,
>
> I am new at using the Bio::TreeIO module specifically using the newick
> format for a phylogenetic analysis. The sample_tree attached is
> Newick-formatted tree. My objective is to get all the distances between all
> the leaf nodes. I copied examples of the code from
> http://www.bioperl.org/wiki/HOWTO:Trees but it does not tell me much (to my
> knowledge) so that I understand how to assign the right array value for the
> nodes/leaves. The message would say must provide 2 root nodes.
>
> Here is what I have right now:
>
> #!/usr/bin/perl -w
>   use strict;
>
>   my $treefile = 'sample_tree';
>   use Bio::TreeIO;
>   my $treeio = Bio::TreeIO->new(-format =>  'newick',
>                                   -file =>  $treefile);
>
>   while (my $tree = $treeio->next_tree) {
>           my @leaves = $tree->get_leaf_nodes;
>           for (my $dist = $tree->distance(-nodes =>  \@leaves)){
>                   print "Distance between trees is $dist\n";
>           }
>   }
>
> Thanks,
> Jeff
>
>    
> ------------------------------------------------------------------------
>
> _______________________________________________
> Bioperl-l mailing list
> [hidden email]
> http://lists.open-bio.org/mailman/listinfo/bioperl-l
_______________________________________________
Bioperl-l mailing list
[hidden email]
http://lists.open-bio.org/mailman/listinfo/bioperl-l
Reply | Threaded
Open this post in threaded view
|

Re: distances between leaf nodes

Mark Jensen
In reply to this post by jdetras (Bugzilla)
along with Jason's comment then you'll need to
loop through the node pairs by hand:

my @leaves = $tree->get_leaf_nodes;
my @dists;
while (my $l = shift @leaves) {
  foreach my $m (@leaves) {
    push @dists, $tree->distance( -nodes => [$l, $m] );
  }
}

should give you all n(n-1)/2 pairwise distances.

----- Original Message -----
From: "Jeffrey Detras" <[hidden email]>
To: <[hidden email]>
Sent: Friday, March 05, 2010 1:17 AM
Subject: [Bioperl-l] distances between leaf nodes


> Hi,
>
> I am new at using the Bio::TreeIO module specifically using the newick
> format for a phylogenetic analysis. The sample_tree attached is
> Newick-formatted tree. My objective is to get all the distances between all
> the leaf nodes. I copied examples of the code from
> http://www.bioperl.org/wiki/HOWTO:Trees but it does not tell me much (to my
> knowledge) so that I understand how to assign the right array value for the
> nodes/leaves. The message would say must provide 2 root nodes.
>
> Here is what I have right now:
>
> #!/usr/bin/perl -w
> use strict;
>
> my $treefile = 'sample_tree';
> use Bio::TreeIO;
> my $treeio = Bio::TreeIO->new(-format => 'newick',
>                                 -file => $treefile);
>
> while (my $tree = $treeio->next_tree) {
>         my @leaves = $tree->get_leaf_nodes;
>         for (my $dist = $tree->distance(-nodes => \@leaves)){
>                 print "Distance between trees is $dist\n";
>         }
> }
>
> Thanks,
> Jeff
>


--------------------------------------------------------------------------------


> _______________________________________________
> Bioperl-l mailing list
> [hidden email]
> http://lists.open-bio.org/mailman/listinfo/bioperl-l 

_______________________________________________
Bioperl-l mailing list
[hidden email]
http://lists.open-bio.org/mailman/listinfo/bioperl-l
Reply | Threaded
Open this post in threaded view
|

Re: distances between leaf nodes

Arnau Montagud
Hello, I am new to Bioperl and looking for a solution to this specific
problem, I found this mailing list.
I am trying to know distances between all the leaves of a given
extended newick tree. Thanks to your script I can have a vector
(@dists) with all the distances, but I would like to know from what
pair of nodes are those distances from (!).
Thanks!

My current script is:

use Bio::TreeIO;

$tree = Bio::TreeIO->new(
        -file=>'tree',
        -format=>'nhx'
        )->next_tree;

my @nodes = $tree->get_leaf_nodes;
my @dists;
while (my $l = shift @nodes) {
  foreach my $m (@nodes) {
    push @dists, $tree->distance( -nodes => [$l, $m] );
        }
}

foreach (@dists) {
  print "$_\n";
}


On 12 mar, 16:45, "Mark A. Jensen" <[hidden email]> wrote:

> along with Jason's comment then you'll need to
> loop through the node pairs by hand:
>
> my @leaves = $tree->get_leaf_nodes;
> my @dists;
> while (my $l = shift @leaves) {
>   foreach my $m (@leaves) {
>     push @dists, $tree->distance( -nodes=> [$l, $m] );
>   }
>
> }
>
> should give you all n(n-1)/2 pairwisedistances.
>
>
>
>
>
>
>
>
>
> ----- Original Message -----
> From: "Jeffrey Detras" <[hidden email]>
> To: <[hidden email]>
> Sent: Friday, March 05, 2010 1:17 AM
> Subject: [Bioperl-l]distancesbetweenleafnodes
>
> > Hi,
>
> > I am new at using the Bio::TreeIO module specifically using the newick
> > format for a phylogenetic analysis. The sample_tree attached is
> > Newick-formatted tree. My objective is to get all thedistancesbetweenall
> > theleafnodes. I copied examples of the code from
> >http://www.bioperl.org/wiki/HOWTO:Treesbut it does not tell me much (to my
> > knowledge) so that I understand how to assign the right array value for the
> >nodes/leaves. The message would say must provide 2 rootnodes.
>
> > Here is what I have right now:
>
> > #!/usr/bin/perl -w
> > use strict;
>
> > my $treefile = 'sample_tree';
> > use Bio::TreeIO;
> > my $treeio = Bio::TreeIO->new(-format => 'newick',
> >                                 -file => $treefile);
>
> > while (my $tree = $treeio->next_tree) {
> >         my @leaves = $tree->get_leaf_nodes;
> >         for (my $dist = $tree->distance(-nodes=> \@leaves)){
> >                 print "Distancebetweentrees is $dist\n";
> >         }
> > }
>
> > Thanks,
> > Jeff
>
> --------------------------------------------------------------------------- -----
>
> > _______________________________________________
> > Bioperl-l mailing list
> > [hidden email]
> >http://lists.open-bio.org/mailman/listinfo/bioperl-l
>
> _______________________________________________
> Bioperl-l mailing list
> [hidden email]://lists.open-bio.org/mailman/listinfo/bioperl-l

_______________________________________________
Bioperl-l mailing list
[hidden email]
http://lists.open-bio.org/mailman/listinfo/bioperl-l
Reply | Threaded
Open this post in threaded view
|

Re: distances between leaf nodes

Roy Chaudhuri-3
Hi Arnau,

Looks pretty simple, don't you just need to print out the ids of the
leaf nodes? So your loop would be something like:

while (my $l = shift @nodes) {
    foreach my $m (@nodes) {
      print join("\t", $l->id, $m->id, $tree->distance( -nodes =>  [$l,
$m] )), "\n";
    }
}

Cheers,
Roy.

On 17/12/2010 16:05, Arnau Montagud wrote:

> Hello, I am new to Bioperl and looking for a solution to this specific
> problem, I found this mailing list.
> I am trying to know distances between all the leaves of a given
> extended newick tree. Thanks to your script I can have a vector
> (@dists) with all the distances, but I would like to know from what
> pair of nodes are those distances from (!).
> Thanks!
>
> My current script is:
>
> use Bio::TreeIO;
>
> $tree = Bio::TreeIO->new(
> -file=>'tree',
> -format=>'nhx'
> )->next_tree;
>
> my @nodes = $tree->get_leaf_nodes;
> my @dists;
> while (my $l = shift @nodes) {
>    foreach my $m (@nodes) {
>      push @dists, $tree->distance( -nodes =>  [$l, $m] );
> }
> }
>
> foreach (@dists) {
>    print "$_\n";
> }
>
>
> On 12 mar, 16:45, "Mark A. Jensen"<[hidden email]>  wrote:
>> along with Jason's comment then you'll need to
>> loop through the node pairs by hand:
>>
>> my @leaves = $tree->get_leaf_nodes;
>> my @dists;
>> while (my $l = shift @leaves) {
>>    foreach my $m (@leaves) {
>>      push @dists, $tree->distance( -nodes=>  [$l, $m] );
>>    }
>>
>> }
>>
>> should give you all n(n-1)/2 pairwisedistances.
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> ----- Original Message -----
>> From: "Jeffrey Detras"<[hidden email]>
>> To:<[hidden email]>
>> Sent: Friday, March 05, 2010 1:17 AM
>> Subject: [Bioperl-l]distancesbetweenleafnodes
>>
>>> Hi,
>>
>>> I am new at using the Bio::TreeIO module specifically using the newick
>>> format for a phylogenetic analysis. The sample_tree attached is
>>> Newick-formatted tree. My objective is to get all thedistancesbetweenall
>>> theleafnodes. I copied examples of the code from
>>> http://www.bioperl.org/wiki/HOWTO:Treesbut it does not tell me much (to my
>>> knowledge) so that I understand how to assign the right array value for the
>>> nodes/leaves. The message would say must provide 2 rootnodes.
>>
>>> Here is what I have right now:
>>
>>> #!/usr/bin/perl -w
>>> use strict;
>>
>>> my $treefile = 'sample_tree';
>>> use Bio::TreeIO;
>>> my $treeio = Bio::TreeIO->new(-format =>  'newick',
>>>                                  -file =>  $treefile);
>>
>>> while (my $tree = $treeio->next_tree) {
>>>          my @leaves = $tree->get_leaf_nodes;
>>>          for (my $dist = $tree->distance(-nodes=>  \@leaves)){
>>>                  print "Distancebetweentrees is $dist\n";
>>>          }
>>> }
>>
>>> Thanks,
>>> Jeff
>>
>> --------------------------------------------------------------------------- -----
>>
>>> _______________________________________________
>>> Bioperl-l mailing list
>>> [hidden email]
>>> http://lists.open-bio.org/mailman/listinfo/bioperl-l
>>
>> _______________________________________________
>> Bioperl-l mailing list
>> [hidden email]://lists.open-bio.org/mailman/listinfo/bioperl-l
>
> _______________________________________________
> Bioperl-l mailing list
> [hidden email]
> http://lists.open-bio.org/mailman/listinfo/bioperl-l

_______________________________________________
Bioperl-l mailing list
[hidden email]
http://lists.open-bio.org/mailman/listinfo/bioperl-l
Reply | Threaded
Open this post in threaded view
|

Re: distances between leaf nodes

Arnau Montagud
Thank you so much Roy!
It works perfectly!
Arnau

2010/12/17 Roy Chaudhuri <[hidden email]>

> Hi Arnau,
>
> Looks pretty simple, don't you just need to print out the ids of the leaf
> nodes? So your loop would be something like:
>
> while (my $l = shift @nodes) {
>   foreach my $m (@nodes) {
>     print join("\t", $l->id, $m->id, $tree->distance( -nodes =>  [$l, $m]
> )), "\n";
>   }
> }
>
> Cheers,
> Roy.
>
> On 17/12/2010 16:05, Arnau Montagud wrote:
>
>> Hello, I am new to Bioperl and looking for a solution to this specific
>> problem, I found this mailing list.
>> I am trying to know distances between all the leaves of a given
>> extended newick tree. Thanks to your script I can have a vector
>> (@dists) with all the distances, but I would like to know from what
>> pair of nodes are those distances from (!).
>> Thanks!
>>
>> My current script is:
>>
>> use Bio::TreeIO;
>>
>> $tree = Bio::TreeIO->new(
>>        -file=>'tree',
>>        -format=>'nhx'
>>        )->next_tree;
>>
>> my @nodes = $tree->get_leaf_nodes;
>> my @dists;
>> while (my $l = shift @nodes) {
>>   foreach my $m (@nodes) {
>>     push @dists, $tree->distance( -nodes =>  [$l, $m] );
>>        }
>> }
>>
>> foreach (@dists) {
>>   print "$_\n";
>> }
>>
>>
>> On 12 mar, 16:45, "Mark A. Jensen"<[hidden email]>  wrote:
>>
>>> along with Jason's comment then you'll need to
>>> loop through the node pairs by hand:
>>>
>>> my @leaves = $tree->get_leaf_nodes;
>>> my @dists;
>>> while (my $l = shift @leaves) {
>>>   foreach my $m (@leaves) {
>>>     push @dists, $tree->distance( -nodes=>  [$l, $m] );
>>>   }
>>>
>>> }
>>>
>>> should give you all n(n-1)/2 pairwisedistances.
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>> ----- Original Message -----
>>> From: "Jeffrey Detras"<[hidden email]>
>>> To:<[hidden email]>
>>> Sent: Friday, March 05, 2010 1:17 AM
>>> Subject: [Bioperl-l]distancesbetweenleafnodes
>>>
>>>  Hi,
>>>>
>>>
>>>  I am new at using the Bio::TreeIO module specifically using the newick
>>>> format for a phylogenetic analysis. The sample_tree attached is
>>>> Newick-formatted tree. My objective is to get all thedistancesbetweenall
>>>> theleafnodes. I copied examples of the code from
>>>> http://www.bioperl.org/wiki/HOWTO:Treesbut it does not tell me much (to
>>>> my
>>>> knowledge) so that I understand how to assign the right array value for
>>>> the
>>>> nodes/leaves. The message would say must provide 2 rootnodes.
>>>>
>>>
>>>  Here is what I have right now:
>>>>
>>>
>>>  #!/usr/bin/perl -w
>>>> use strict;
>>>>
>>>
>>>  my $treefile = 'sample_tree';
>>>> use Bio::TreeIO;
>>>> my $treeio = Bio::TreeIO->new(-format =>  'newick',
>>>>                                 -file =>  $treefile);
>>>>
>>>
>>>  while (my $tree = $treeio->next_tree) {
>>>>         my @leaves = $tree->get_leaf_nodes;
>>>>         for (my $dist = $tree->distance(-nodes=>  \@leaves)){
>>>>                 print "Distancebetweentrees is $dist\n";
>>>>         }
>>>> }
>>>>
>>>
>>>  Thanks,
>>>> Jeff
>>>>
>>>
>>> ---------------------------------------------------------------------------
>>> -----
>>>
>>>  _______________________________________________
>>>> Bioperl-l mailing list
>>>> [hidden email]
>>>> http://lists.open-bio.org/mailman/listinfo/bioperl-l
>>>>
>>>
>>> _______________________________________________
>>> Bioperl-l mailing list
>>> [hidden email]://
>>> lists.open-bio.org/mailman/listinfo/bioperl-l
>>>
>>
>> _______________________________________________
>> Bioperl-l mailing list
>> [hidden email]
>> http://lists.open-bio.org/mailman/listinfo/bioperl-l
>>
>
>
_______________________________________________
Bioperl-l mailing list
[hidden email]
http://lists.open-bio.org/mailman/listinfo/bioperl-l