#!/usr/bin/perl
#########################################
# fas (reformat from a3m) to msa for DCA
#########################################

open FAS, "$ARGV[0]" or die $!;
my @ids;
my %fas;
my $cur_id = 0;
while(my $line = <FAS>){
  chomp $line; 
  #$line =~ s/^\s+//;
  #$line =~ s/\s+$//;
  if(substr($line,0,1) eq ">"){
    push(@ids, $line);
    $cur_id++;
  }else{
    $fas{$cur_id} .= $line;
  }
}
close FAS;

my @query = split(//, $fas{1});

my @valid_ids;
for(my $i=0; $i<=$#query; $i++){
  if($query[$i] ne "-"){
    push(@valid_ids, $i);
  }
}

for my $key (sort {$a<=>$b} keys %fas){
  my $seq;

  for(my $i=0; $i<=$#valid_ids; $i++){
    my $c = substr($fas{$key}, $valid_ids[$i], 1);
    $c = uc $c;
    $seq .= $c;
  }
  print $ids[$key-1]."\n";
  print "$seq\n";
}

