Formatted output for string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kumarboston
    New Member
    • Sep 2007
    • 55

    Formatted output for string

    Hi All,
    I have a sequence of character like "ACDFGEYRTWJSHD GKSKSDHFCNDHD" and I am trying to format in a way such that the output looks like this
    A C D F G E Y R T W
    J S H D G K S K S D
    H F C N D H D

    ie, one space between each character and 10 character in one line.
    I am getting the sequence in tab separated list but not able to format 10 character per line with one spacing.
    Any help will be appreciated.

    Thanks
    Kumar
  • nithinpes
    Recognized Expert Contributor
    • Dec 2007
    • 410

    #2
    What have you tried so far?
    All you need to do is to split the string into characters and use the indices of resulting array for character count to format it accordingly...

    Comment

    • kumarboston
      New Member
      • Sep 2007
      • 55

      #3
      Thanks for the reply,
      here is my code
      Code:
      #!/usr/bin/perl
      use strict;
      use warnings;
      my %one2three = ( 'G'=>'GLY','A'=>'ALA','V'=>'VAL','L'=>'LEU','I'=>'ILE',          
                        'S'=>'SER','T'=>'THR','C'=>'CYS','M'=>'MET','P'=>'PRO',          
                        'D'=>'ASP','N'=>'ASN','E'=>'GLU','Q'=>'GLN','K'=>'LYS',          
                        'R'=>'ARG','H'=>'HIS','F'=>'PHE','Y'=>'TYR','W'=>'TRP',          
                        'B'=>'ASX','Z'=>'GLX','X'=>'UNK');
      my $count = 0;my @val;my ($line,$seq);
      
      open (FH, "GHHDP") or die "Check for the file";
      my @prot = <FH>;
      close FH;
      
      foreach $line (@prot)
      {
          if($line =~/^\s*#/)
             {next;}
          elsif($line =~/^>(.*)/)
             { my $title = $1;
               print "* $title\n*\n";
           }
          else{$seq .= $line;}
      }
      $seq=~ s/\s//g;
      my @ss = split(//,$seq);
      foreach my $rr(@ss)
      {
           $rrr = $one2three{$rr};
           print "$rrr\t";
           $count +=1;
      }
      print "$count\n";
      thanks
      Kumar

      Comment

      • nithinpes
        Recognized Expert Contributor
        • Dec 2007
        • 410

        #4
        You are printing out three-letter representations of the amino acids and not single characters as in your initial posting. However, logic remains the same. You are formatting your output with tab(\t). Replace that with a single space.

        Code:
        foreach my $rr(@ss)
        {
             my  $rrr= $one2three{$rr};
             print "$rrr ";   ##replace \t with single space
             $count +=1;
             print "\n" if($count%10==0); ## insert newline after every 10 records
        }
        - Nithin

        Comment

        • kumarboston
          New Member
          • Sep 2007
          • 55

          #5
          Thanks Nithin for the reply,
          I was getting confused by using the sprintf function but now its working fine. :)

          Thanks
          Kumar

          Comment

          Working...