#!/usr/bin/perl

# This is what's expected on STDIN (first line highlights atoms to be fit):

# COMMON  >--------##########################################################################-###--#####################-########################---------------------------#---------------------------#################################-----#########-##############---------------------
# d1akea_ >--------MRIILLGAPGAGKGTQAQFIMEKYGIPQISTGDMLRAAVKSGSELGKQAKDIMDAGKLVTDELVIALVKERIAQEDCR--NGFLLDGFPRTIPQADAMKEAGINVDYVLEFDVPDELIVDRIVGRRVHAPSGRVYHVKFNPPKVEGKDDVTGEE---------------------------LTTRKDDQEETVRKRLVEYHQMTAPLIGYYSKEAEAGNTKYAKVDGTKPVAEVRADLEKILG---------------------
# d2ak3a_ >--GASARLLRAAIMGAPGSGKGTVSSRITKHFELKHLSSGDLLRDNMLRGTEIGVLAKTFIDQGKLIPDDVMTRLVLHELKN-LTQ--YNWLLDGFPRTLPQAEALDRA-YQIDTVINLNVPFEVIKQRLTARW---------------------------IHPGSGRVYNIEFNPPKTMGIDDLTGEPLVQREDDRPETVVKRLKAYEAQTEPVLEYYRKK-----GVLETFSGT-ETNKIWPHVYAFLQTKLPQRSQETSVTP-------

$ENV{"PERL_DEBUG"} > 3 && &hello ;
local(@ids) = ();
local(@seqs) = ();

local($n) = &ReadMultAli(*ids,*seqs);

if ($ids[0] =~ /COMMON/) {
    splice(@ids,0,1);
    splice(@seqs,0,1);
    $n-- ;
} 

open(OUT,">tmp.pir-in");
for ($i=0; $i<$n; $i++) {
    print OUT ">P1;$ids[$i]\n\n" ;
    local($s) = $seqs[$i];
    $s =~ s/\n//g ;
    $s =~ s/-//g ;
    $s =~ s/ //g ;
    for ($j=0; $j<length($s); $j++) {
	print OUT substr($s,$j,1);
	if ($j % 60 == 0 && $j != 0) { print OUT "\n" ; }
    }
    print OUT "\n*\n" ;
}
close(OUT);
system "clustalw /outorder=input /infile=tmp.pir-in /align /output=pir > tmp.clustal.out" ;
system "pir-to-MultAli.pl < tmp.pir " ;

sub Option {
    local($Option) = @_;
    &ArgvMatch($Option,@ARGV);
}

sub ArgvMatch {
    local($Option,@argv) = @_;
    local($r)=0;
    local($i);
    local($b) = "-" . $Option;
    for ($i=0; $i <= $#argv ; $i++) {
        if ($argv[$i] eq $b) {
            $r = 1 ;
        }
    }                            
    $ENV{"PERL_DEBUG"} > 2 && printf(STDERR "[T/F Option    ] -%s = %d\n",$Option,$r);
    return $r;
}                               

sub hello {
    local($host) = `hostname` ;
    chop $host ;
    local($date) = `date` ;
    chop $date ;
    local($r) = &rootname ;

    print STDERR "Hello:  Script $r being interpreted by $ \n" ;
    print STDERR "Hello:  at $date with pid $$ on $host.\n";
}


sub ReadMultAli {
    local(*ids,*seqs) = @_ ;
    local($i) =0;
    local($n) = 0;
    while (<STDIN>) {
	if (/ >/) {
	    local($id) = $`;
	    $seqs[$n] = $';
	    $id =~ s/[ \t]+//g ;
	    $ids[$n] = $id ;
	    $n++ ;
	}
    }
    for ($i=0; $i<$n; $i++) {
	$ENV{"PERL_DEBUG"} > 4 && print STDERR "$i IDS=$ids[$i]  SEQS=$seqs[$i]\n" ;
    }
    return($n) ;
}


sub msg {
    local($s) = @_ ;
    if (!($bp_name_global)) {
	local(@a) = split (/\//,$0);
	$bp_name_global = $a[$#a];
    }

    print STDERR "[ $bp_name_global ] $s\n" ;
}

sub bp {
    local($s) = @_ ;
    if (!($bp_name_global)) {
	local(@a) = split (/\//,$0);
	$bp_name_global = $a[$#a];
    }

    if ($ENV{"PERL_DEBUG"} > 2) {
	print STDERR "[ $bp_name_global ] $s\n" ;
    }
}

