#!/usr/bin/perl

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

# >P1;d1reia_
# 
# IQMTQSPSS--LSASVGDRVTITCQASQDIIK----YLNWYQQTPG--KAPKLLIYEASN
# L---QAGVPSRFSG--SGSGTDYTFTISSLQPEDIATYYCQQYQS--LPYTFGQGTKLQ-
# -
# *
# >P1;m1fc1a1
# 
# PSVFLFPPKPKDTLMISRTPEVTCVVVDVSHEDPQVKFNWYVD--G---VQVHNAKTKPR
# E--------QQYNS---TYRVVSVLTVLHQNWLDGKEYKCKVSNKALPAPIEKTISKAKG
# Q
# *
# >P1;m1fc1a2
# 
# PQVYTLPPS--REEMTKNQVSLTCLVKGFYPSD--IAVEWESN--G---QPENNYKTTPP
# V--------LDSDG---SFFLYSKLTVDKSRWQQGNVFSCSVMHEALHNHYTQKSLSL--
# -
# *
#           0         0         0         0         0         0         0         0         0         1         1         1         1         1         1         1         1         1         1         2         2         2         2         2         2         2         2   
#           1         2         3         4         5         6         7         8         9         0         1         2         3         4         5         6         7         8         9         0         1         2         3         4         5         6         7   
# 0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890

# This is what is sent to STDOUT:

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


$ENV{"PERL_DEBUG"} > 4 && &hello ;

while (<>) {
    if (/^>P1;/) {
	if ($id ne "") {
	    printf "%-11s >%s\n",$id,$seq ;
	}
	chop ;
	s/ //g ;
	s/>P1;// ;
	$id = $_ ;
	$seq = "" ;
	$_ = <STDIN> ; # skip next line
    } else {
	chop ;
	s/\*$// ;
	$seq = $seq . $_ ;
    } 
}

printf "%-11s >%s\n",$id,$seq ;

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" ;
    }
}

