No problem Changrong, glad to be of help to a fellow scientist. And thanks to John and the other for showing much more elegant ways of solving the problem. Cheers! Anjan
On Thu, Dec 2, 2010 at 3:40 AM, John W. Krahn <jwkr...@shaw.ca> wrote: > Changrong Ge wrote: > >> Hi guys, >> > > Hello, > > > Thanks a lot for this issue and with Anjan's help here I got the >> wonderful script for my work and I post below, hopefully it would be >> helpful >> for others--works perfectly for fasta file. >> Also thanks to others who are interested in the discussion, but I >> think >> it takes time for me to digest those "weird" symbols-----Tack Så Mycket!!! >> Have a nice day >> Changrong >> >> >> >> usage: perl seqComp.pl<input_fasta_file> > <output_file> >> >> >> #! /usr/bin/perl -w >> use strict; >> >> open (S, "$ARGV[0]") || die "cannot open FASTA file to read: $!"; >> >> my %s;# a hash of arrays, to hold each line of sequence >> my %seq; #a hash to hold the AA sequences. >> my $key; >> >> while (<S>){ #Read the FASTA file. >> chomp; >> >> if (/>/){ >> s/>//; >> $key= $_; >> }else{ >> push (@{$s{$key}}, $_); >> } >> >> } >> >> foreach my $a (keys %s){ >> my $s= join("", @{$s{$a}}); >> $seq{$a}=$s; >> #print("$a\t$s\n"); >> } >> > > You don't need two loops for that, you could just do: > > > while ( <S> ) { #Read the FASTA file. > chomp; > > if ( s/>// ) { > $key = $_; > } > else { > $seq{ $key } .= $_; > > } > } > > > > > John > -- > Any intelligent fool can make things bigger and > more complex... It takes a touch of genius - > and a lot of courage to move in the opposite > direction. -- Albert Einstein > > -- > To unsubscribe, e-mail: beginners-unsubscr...@perl.org > For additional commands, e-mail: beginners-h...@perl.org > http://learn.perl.org/ > > > -- =================================== anjan purkayastha, phd. research associate fas center for systems biology, harvard university 52 oxford street cambridge ma 02138 phone-703.740.6939 ===================================