Vineet Pande am Dienstag, 13. September 2005 13.00:
> Dear Perl Guru:
>
> I want to write a script which takes input a text file with sequence of
> alphabets (amino acids) like GAGAGAGAGAAAAGA etc. and converts them to a
> three letter corresponding code [G= GLY, A= ALA]. The output should be then
> written to another file. I wrote the following script, but it seems it goes
> in an infinite loop and only prints GLY GLY GLY....infinitely for an input
> of GAAAGAGAAGA etc. Any suggestions? Thanks in advance
> -Vineet
>
> ****************************************
> #!/usr/bin/perl
> use warnings;
>
> my $fasta_file = "fasta.txt"; #this contains single letter code#
> chomp $fasta_file;
> my $charmm_file = "charmm.txt"; #this is expected to have three letter
> converted#
> unless (open (IN, $fasta_file) )
> {
> print "Cannot open file \"fasta_file\"\n\n";
> exit;
> }
>
> open OUT, ">$charmm_file" or die;
> @protein = <IN>;
> close IN;
> $protein = join( '', @protein);
> $protein =~ s/\s//g;
>
> for ( $position=0; $position < length $protein; ++$protein )
>
> {
> $aa = substr($protein, $position, 1);
>
>
>                     if ($aa eq 'G') {
>                             $praa = "GLY ";
>                             print OUT $praa;
>                                     }
>                  elsif ($aa eq 'A') {
>                             $prab = "ALA ";
>                             print OUT $prab;
>                                     }
>                  else {
>                          exit;
>                       }
>                        }
>
>                        close OUT;
>

Here another way (you have to integrate it), avoiding multiple print 
statements and a bit shorter:

[tested]

use strict;
use warnings;

# provide translation map:
my %map=(A=>'ALA ', G=>'GLY ');

# ...get input into $in...

my $txt='GAGAGAGAGAAAAGA'; # just an example

# not the fastest way, but adapts to the contents of %map:
$txt=~s/(.)/$map{$1}/gs;

print $txt;


# output:
GLY ALA GLY ALA GLY ALA GLY ALA GLY ALA ALA ALA ALA GLY ALA



joe

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to