[Bio] / FigKernelScripts / svr_reroot_tree.pl Repository:
ViewVC logotype

View of /FigKernelScripts/svr_reroot_tree.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (download) (as text) (annotate)
Fri Oct 15 17:54:29 2010 UTC (9 years, 4 months ago) by overbeek
Branch: MAIN
CVS Tags: mgrast_dev_08112011, mgrast_dev_08022011, rast_rel_2014_0912, myrast_rel40, mgrast_dev_05262011, mgrast_dev_04082011, mgrast_version_3_2, mgrast_dev_12152011, mgrast_dev_06072011, rast_rel_2014_0729, mgrast_dev_02212011, rast_rel_2010_1206, mgrast_release_3_0, mgrast_dev_03252011, rast_rel_2011_0119, mgrast_release_3_0_4, mgrast_release_3_0_2, mgrast_release_3_0_3, mgrast_release_3_0_1, mgrast_dev_03312011, mgrast_release_3_1_2, mgrast_release_3_1_1, mgrast_release_3_1_0, mgrast_dev_04132011, mgrast_dev_04012011, myrast_33, rast_rel_2011_0928, mgrast_dev_04052011, mgrast_dev_02222011, mgrast_dev_10262011, HEAD
Changes since 1.2: +1 -2 lines
fix typo

use strict;
use Data::Dumper;
use Carp;
use gjonewicklib;

#
# This is a SAS Component
#

=head1 svr_reroot_tree

Reroot a tree at a different node or a point on an internal arc.

=head2 Introduction

Examples:

    svr_reroot_tree -m < tree.nwk > rerooted.tree.wk [to midpoint of tree (an approximation of that)]
    svr_reroot_tree 'Deh.eth.19,Sph.the.DS,Deh.sp.CBD' < tree.nwk > rerooted.tree.nwk [at a node in an unrooted tree]
    svr_reroot_tree 'Deh.eth.19,Deh.sp.CBD' < tree.nwk > rerooted.tree.nwk [at an internal node in a rooted tree]
    svr_reroot_tree 'Deh.eth.19'            < tree.nwk > rerooted.tree.nwk [at a tip]
    svr_reroot_tree -t 'Deh.eth.19'            < tree.nwk > rerooted.tree.nwk [at a node next to a tip]

To reroot to a point on an arc between two nodes, use 

       svr_reroot_tree -f 0.5 NODE1 NODE2 < tree.nwk > rerooted.tree.nwk [NODE1 and NODE2 can be 1, 2,or 3 ids]
e.g.,  
       svr_reroot_tree -f 0.5 Deh.eth.19 'Deh.eth.19,Deh.sp.CBD' < tree.nwk > rerooted.tree.nwk [NODE1 and NODE2 can be 1, 2,or 3 ids]
or     svr_reroot_tree -d 1.5 Deh.eth.19 'Deh.eth.19,Deh.sp.CBD' < tree.nwk > rerooted.tree.nwk [NODE1 and NODE2 can be 1, 2,or 3 ids]


=head2 Command-Line Arguments

The program is invoked using

    svr_reroot_tree [-m] [-t] [-f Fraction] [-d Distance] Node(s)

    The operation may require one or two nodes to be specified; these
    immediately follow the optional arguments.  A node is specified
    as a string containing 1 to 3 IDs comma-separated.

=over 4

=item -m

Root at the midpoint of the tree (an approximation)

=item -t

Root at the parent node of the specified tip (i.e., there should be a single node given,
and it should be a single tip ID).

=item -f=Fraction

Specifies the fraction along the path from NODE1 to NODE2 that you wish to place the root at

=item -d=Distance

Specifies the distance along the path from NODE1 to NODE2 that you wish to place the root at

=back

=head2 Output

A rerooted tree in newick format.

=cut

use Getopt::Long;
my $midpoint = 0;
my $root_at_parent = 0;
my $fraction;
my $distance;
my $usage = "svr_corresponding_genes [-m] [-t] [-f Fraction] [-d Distance] Node(s) < tree > rerooted\n";

my $rc = GetOptions( "m" => \$midpoint,
		     "a"   => \$root_at_parent,
                     "f=f" => \$fraction,
                     "d=f" => \$distance
		   );

$rc or print STDERR $usage and exit;

if (($fraction || $distance) && (@ARGV != 2))
{
    print STDERR "You need to specify two nodes when rerooting to a point on a path\n";
    print STDERR $usage;
    exit;
}

if ((! defined($fraction)) && (! defined($distance)) && (@ARGV == 2))
{
    print STDERR "You need to specify fraction or distance when rerooting to a point on a path\n";
    print STDERR $usage;
    exit;
}

if ($root_at_parent && (@ARGV != 1))
{
    print STDERR "You need to specify a single tip node when rerooting to the parent of a tip\n";
    print STDERR $usage;
    exit;
}

if ($midpoint && (@ARGV != 0))
{
    print STDERR "You have extra arguments (rerooting to the midpoint of a tree requires no nodes\n";
    print STDERR $usage;
    exit;
}

if ((@ARGV == 0) && (! $midpoint))
{
    print STDERR "You are missing arguments (node(s) are required if not rerooting to a midpoint)\n";
    print STDERR $usage;
    exit;
}

my @args = map { [split(/,/,$_)] } @ARGV;

my $tree  = &gjonewicklib::read_newick_tree( );

my $rerooted = &gjonewicklib::reroot_tree($tree, { midpoint => $midpoint,
					           adjacent_to_tip => $root_at_parent,
					           fraction => $fraction,
					           distance => $distance,
					           nodes    => \@args
					         });

&gjonewicklib::writeNewickTree( $rerooted );

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3