#!/usr/bin/perl

# 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

my $usage =  "Usage: $0 [-h] [-v] [-c numChar] [infile]\n" .
    "  -h: help\n" .
    "  -c: long seq names are shortened to 10 char, default: 7 chars from the\n".
    "      beggining is combined with the last 3 chars.  You can change the\n".
    "      behavior by this option.  E.g., -c 10 will shorten the long name\n" .
    "      by taking the first 10 characters of the name.\n".
    " infile should be an aligned fasta, " .
    "STDIN is used if no infile is given\n";

use Getopt::Std;
getopts('hc:') || die "$usage\n";
die "$usage\n" if (defined($opt_h));

my $args = join " ", @ARGV;

if (defined ($opt_c)) {
    $args = "-c $opt_c $args";
}

if (@ARGV) {
    system("fasta2phylip.pl $args | phylip2paml.pl");
} else {  # input is piped stdin
    open OUT, "|fasta2phylip.pl $args | phylip2paml.pl" || die "Can't open fasta2phylip.pl pipe\n";
    while(<>) {
	print OUT $_;
    }
    close OUT;
}

exit;
