#!/usr/bin/perl

require "lib.simple-io.pl" ;

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"} && print STDERR "$i IDS=$ids[$i]  SEQS=$seqs[$i]\n" ;
    }
    return($n) ;
}

sub ReadMultAliStandardize {
    local(*ids,*seqs) = @_ ;
    local($i) =0;
    local($n) = 0;
    while (<STDIN>) {
	if (/ >/) {
	    local($id) = $`;
	    $seqs[$n] = $';
	    $id =~ s/[ \t]+//g ;
	    $ids[$n] = $id ;
	    $n++ ;
	}
    }
    $seqs[0] =~ s/\*/\#/g;
    
    for ($i=0; $i<$n; $i++) {
	$seqs[$i] =~ s/ /-/g;
	$seqs[$i] =~ tr/a-z/A-Z/;
	&msg3("$i IDS=$ids[$i]  SEQS=$seqs[$i]") ;
    }
    return($n) ;
}


sub count_hash {
    local($_) = @_;
    local(@a) = split (//,$_);
    local($nhash,$ncolon) = (0,0);
    foreach $_ (@a) {
	if (/\#/) {
	    $nhash++;
	} elsif (/\:/) {
	    $ncolon++ ;
	} elsif (/\*/) {
	    $nhash++ ;
	}
    }
    return($nhash,$ncolon);
}

sub mk_common_line {
    local(@list) = @_ ;
    local($len) = length($list[0]); # first line containing amino acid symbols
    local($i,$j,$common);

    for ($j = 0; $j < $len; $j++) {
	substr($common, $j, 1) = "#"; # set 'common' to all '#' chars
    }

    for ($i=0; $i<=$#list; $i++) {
	for ($j = 0; $j < $len; $j++) {
	    if ((substr($list[$i], $j, 1) !~ /\w/)) {
		substr($common, $j, 1) = "-";
	    }
	}
    }
    if (substr($list[0],0,1) eq ">") {
	substr($common,0,1) = ">";
    }
    return $common ;
}


sub NReadMultAli {
    local(*ids,*seqs,$in,@array) = @_ ;
    &msg3("NReadMultAli(): BEGINS with in=$in");
    local($i,$j,$k) =0;
    local($n) = 0;
    if ($in eq "") { 
	@array = <STDIN> ; 
	&msg3("NReadMultAli() reading from STDIN");
    } elsif ($in eq "file") {
	open(IN_NREAD,"$array[0]") || die "Can't open MultAli ";
	@array = <IN_NREAD> ;
	close IN_NREAD ;
	&msg3("NReadMultAli() reading from file");
    } elsif ($in ne "array") { 
	@array = <$in> ; 
	&msg3("NReadMultAli() reading from stream");
    } else {
	&msg3("NReadMultAli() reading from array");
    }	
    foreach $_  (@array) {
	&msg5($_);
	next if (/^ATOM/) ;
	next if (/^\#/) ; #
	next if (/^_/) ; 
	if (/ >/) {
	    $seqs[$n] = $';
	    $ids[$n] = $` ;
	    $n++ ;
	}
    }
    &msg3("Got $n-1 sequences");
    $seqs[0] =~ s/\*/\#/g;
    local( $maxlen ) = 0;
    for ($i=0; $i<$n; $i++) {
	$ids[$i] =~ s/[ \t]+//g ;
	chop $seqs[$i];
	$seqs[$i] =~ s/ /-/g;
	$seqs[$i] =~ tr/a-z/A-Z/;
	if (length($seqs[$i]) > $maxlen) {
	    $maxlen = length($seqs[$i]);
	}
    }
    &msg3("maxlen=$maxlen");
    for ($j=0; $j<$maxlen; $j++) {
	$all_dash = 1 ;
	for ($k=0; $k<$n; $k++) {
	    if (substr($seqs[$k],$j,1) eq "") {
		&msg1("Padding end of alignment with dashes.");
		$seqs[$k] .= "-";
	    }
	    if (substr($seqs[$k],$j,1) ne "-") {
		$all_dash = 0;
	    }
	}
	if ($all_dash == 1) {
	    &msg1("Skipping a column of blanks in alignment");
	    for ($k=0; $k<$n; $k++) {
		substr($seqs[$k],$j,1) = "";
	    }
	}
    }
    
    for ($i=0; $i<$n; $i++) {
	&msg3("ids[$i]=$ids[$i]") ;
    }
    for ($i=0; $i<$n; $i++) {
	&msg3("seqs[$i]=$seqs[$i]") ;
    }
    return($n) ;

}

sub PrintMultAli {
    local(*ids,*seqs,$in) = @_ ;
    local($i);
    if ($in eq "") { $in = STDOUT ; }
    for ($i=0; $i<=$#ids; $i++) {
	printf($in "%-11s >%s\n",$ids[$i],$seqs[$i]);
    }
}

1 ;
