#!/usr/bin/perl -w

# Copyright 2013, Naoki Takebayashi <ntakebayashi@alaska.edu>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

# Version: 20130612

use Bio::TreeIO;

my $usage="Usage: $0 [-hn] [treeFile ...]\n".
 " -n: input is in newick (phylip) format: default = nexus (paup*) format\n";

use Getopt::Std;
my %opts=();
getopts('hb:n',\%opts) || die "$usage\n";

die "$usage\n" if ($opts{h});

@ARGV = ('-') unless @ARGV;  # take STDIN when no arg.

my $treeOut = Bio::TreeIO->new('-format' => 'lintree', 'file' => '>-');

my $infile;
while ($infile = shift @ARGV) {
    my $inFormat = ($opts{n}) ? 'newick' : 'nexus';
    my $treeIN = new Bio::TreeIO(-file => $infile, -format => $inFormat);

    my $cntr = 1;
    while (my $tree = $treeIN->next_tree) {
	
	$treeOut->write_tree($tree);
    }
}

exit(0);
