#!/usr/bin/env perl
use strict;
use warnings;
use rlib '../lib';
use v5.10;
use Bio::BPWrapper;
use Bio::BPWrapper::TreeManipulations;
use Data::Dumper;
use Getopt::Long qw( :config bundling permute no_getopt_compat );
use Pod::Usage;

use constant PROGRAM => File::Basename::basename(__FILE__);

####################### Option parsing ######################
pod2usage(1) if scalar(@ARGV) < 1;
my %opts;
GetOptions(\%opts,
	   "help|h",
	   "man",
	   "distance|d=s",
	   "input|i:s",
	   "length|l",
	   "numOTU|n",
	   "output|o:s",
	   "reroot|r=s",
	   "subset|s=s",
	   "otu|u",
	   "lca|A=s",
	   "labelnodes|B",
	   "distanceall|D",
	   "ltt|G=s",
	   "lengthall|L",
	   "random|M:i",
	   "depth|P=s",
	   "rmbl|R",
	   "allchildOTU|U=s",
	   "version|V",
	   "walk|W=s",
	   "multi2bi",
	   "cleanbr",
	   "cleanboot",
	   "delete-otus=s",
	   "sis-pairs", # pairwise OTU togetherness
	   "swap-otus=s", # output trees with each possible pairs (with the designated one) swapped
	   "ead", # edge-length abundance distribution; ODwyer et al. PNAS (2015)
	   "tree-shape", # for apTreeshape
	   #    "bootclean|b:f",
	   #    "collapse|c=s@",
	   #    "getroot|g",
	   #    "prune|p=s@",
	   #    "compnames|x",
#	   "collabel|C:s",
#	   "tree2tableid|I:s",
#	   "joindata|J=s@",
#	   "rename|N",
#	   "tree2table|T",
	   #    "comptrees|X",
	  ) or pod2usage(2);

Bio::BPWrapper::print_version(PROGRAM) if $opts{"version"};

# Create a new BioTree object and initialize that.
unshift @ARGV, \%opts;
initialize(@ARGV);
write_out(\%opts);

################# POD Documentation ##################
__END__
=encoding utf8

=head1 NAME

biotree - FASTA L<Bio::TreeIO> tools

=head1 SYNOPSIS

B<biotree> [options] [<tree file>]

B<biotree> [C<-h> | C<--help> | --C<v> | C<--version> C<--man>]

=head1 DESCRIPTION

B<biotree> will read a tree file and do reformating of branches
and nodes.

Trees can be in any format supported by L<Bio::TreeIO> in
L<Bio::Perl>. However, tree-manipulations has not been tested on all
possible formats, so behavior may be unexpected with some.

Currently, tree-manipulations does not support multiple trees per file or the ability to read from standard input.

=head2 OPTIONS

=over 4

=item --cleanbr

=item --distance, -d 'node1' -d 'node2'

Prints the distance between a pair of nodes or leaves.

=item --delete-otus

=item --input, -i 'format'

Input file format. Accepts newick and nhx.

=item --length, -l

Print total branch length.

=item --numOTU, -n

Print total number of OTUs (leaves).

=item --output, -o 'format'

Output file format. Accepts newick, nhx, and tabtree.

=item --reroot, -r 'newroot'

Reroot tree to specified node by creating new branch.

=item --subset, -s 'node1,node2,node3'

Creates a tree of only the specified leaves/nodes and their descendants. Specifying a single internal node produces a subtree from that node.

=item --otu, -u

Print leaf nodes with branch lengths.

=item --lca, -A 'node1,node2,node3,...'

Returns ID of most recent common ancestor across provided nodes. Returns direct ancestor if single leaf/node provided.

=item --labelnodes, -B

Prepends ID to each leaf/node label. Useful when identifying unlabed nodes, such as when using --prune.

=item --distanceall, -D

Prints half-matrix list of distances between ALL leaves.

=item --ltt, -G 'number_of_bins'

Divides tree into number of specified segments and counts branches up to height the segment. Returns: bin_number, branch_count, bin_floor, bin_ceiling.

llt: Linear through time.

=item --lengthall, -L

Prints all nodes and branch lengths.

=item --random, -M [sample_size]

Builds a tree of a random subset of the original tree's OTUs. Defults to selecting half of the original tree's nodes.

=item --depth, -P 'node' [-D 'node'] [-D 'node']...

Prints depth to root. Accepts node names and/or IDs.

=item --rmbl, -R

Remove branch lengths from tree.

=item --allchildOTU, -U 'internal_node_id' | 'all'

Prints all OTU's that are descended from the given internal node. If no node is provided, a complete list of all internal nodes and their descendents is returned instead (given in the order of "walking" through the tree from the root node).

=item --sis-pairs

=item --walk, -W 'otu'

Walks along the tree starting from the specified OTU and prints the total distance traveled while reaching each other OTU. Does not count any segment more than once.

=back

=head3 Common Options

=over 4

=item --help, -h

Print a brief help message and exit.

=item --man

Print the manual page and exit.

=item --version, -V

Print current release version of this command and exit.

=back

=head1 SEE ALSO

=over 4

=item *

L<Bio::BPWrapper::TreeManipulations>, the underlying Perl Module

=item *

L<Qiu Lab wiki page|http://diverge.hunter.cuny.edu/labwiki/Bioutils>

=item *

L<Github project wiki page|https://github.com/bioperl/p5-bpwrapper/wiki>

=back

=head1 CONTRIBUTORS

=over 4

=item  *
Yözen Hernández yzhernand at gmail dot com

=item *
Pedro Pegan

=item  *
L<Weigang Qiu | mailto:weigang@genectr.hunter.cuny.edu> (Maintainer)

=item *
Rocky Bernstein

=back

=cut
