Thursday, June 4, 2009

Perl problem -2

hi everyone,
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


3 comments:

  1. I love your new blog here, Teja, and keep it up!

    use 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];
    }

    ReplyDelete
  2. This is a very good example of using Bio perl to solve simple problems..Thankyou Lee

    ReplyDelete
  3. As lee's answer is the Best one for this problem, I wouldn't be writing any other solution to this problem.

    ReplyDelete