Hi Vineet,

Since Perl provides strong feature of pattern matching of Regular Expn you should use that. Just read in the pattern GAGAGAGAGAAAAGA from the file and store it in a scalar variable.
You can refer to the following perl code snippet for getting  yout job done.
#! /usr/bin/perl
my $x = 'GAGAGAGAAAGG';
$x =~ s/G/GLY/g;  # Search for pattern G globally and replace it with 'GLY'
$x =~ s/A/ALA/g;  # Search for pattern A gloablly and replace it with 'ALA'
print $x;                  # Print replaced string

1. Read in the string from your file .
2. Search and replace the string as required
3. Write the string in another file
The code wont take more than 10-12 lines. And the beauty is you dont have to 
iterate over your string.

Regards,
Suvajit


Vineet Pande wrote:

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;

**********************************************

_________________________________________________________________
FREE pop-up blocking with the new MSN Toolbar - get it now! http://toolbar.msn.click-url.com/go/onm00200415ave/direct/01/




--
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