Its time for perl problem -2
Write a program which takes a fasta file as input and prints the sequences (along with the headers) sorted by the length from smallest to largest.
Example
Input
>sequence 1
AGTC
>sequence 2
AGGTTTCTAC
output
>sequence2
AGGTTTCTAC
>sequence 1
AGTC
Thanks
Teja
I love your new blog here, Teja, and keep it up!
ReplyDeleteuse Bio::Perl;
use warnings;
use strict;
my $file='perl2.fna'; # fasta entries
my @seqObj; # array of sequence objects
my $seqout=Bio::SeqIO->newFh(-format=>'fasta'); #output file handle
my $seqio=Bio::SeqIO->new(-file=>$file,-format=>'fasta');
while(my $seq=$seqio->next_seq()){
push(@seqObj,$seq); # populate sequence object array
}
@seqObj=sort({ length($b->seq()) <=> length($a->seq()) } @seqObj);
for(my $i=0;$i<@seqObj;$i++){
print $seqout $seqObj[$i];
}
This is a very good example of using Bio perl to solve simple problems..Thankyou Lee
ReplyDeleteAs lee's answer is the Best one for this problem, I wouldn't be writing any other solution to this problem.
ReplyDelete