I'm trying to sort a multiFASTA file by length. I have the alphabetical sort figured out but I can't seem to get the numerical sort. The output should be a sorted multiFASTA file. This is an option to another program. Here is the code.
sub sort {
my $length;
my $key;
my $id;
my %seqs;
my $seq;
my $action = shift;
my $match = $opts{$action};
$match =~ /[l|id]/ || die "not the right parameters\n";
my $in = Bio::SeqIO->new(-file=>"$filename", -format=>'fasta');
while(my $seqobj = $in->next_seq()){
my $id = $seqobj->display_id();
my $length=$seqobj->length();
#$seq =~s/.{1,60}\K/\n/sg;
$seqs{$id} = $seqobj, unless $match eq 'l';
$seqs{$length}=$seqobj, unless $match eq 'id';
}
if($match eq 'id'){
foreach my $id (sort keys %seqs) {
printf ">%-9s \n%-s\n", $id, $seqs{$id}->seq;
}
}
elsif($match eq 'l'){
foreach my $length ( sort keys %seqs){
printf "%-10s\n%-s\n",$length, $seqs{$length}->seq;
}
}
}