By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,835 Members | 2,333 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,835 IT Pros & Developers. It's quick & easy.

Formatted output for string

P: 55
Hi All,
I have a sequence of character like "ACDFGEYRTWJSHDGKSKSDHFCNDHD" 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
Aug 21 '08 #1
Share this Question
Share on Google+
4 Replies


nithinpes
Expert 100+
P: 410
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...
Aug 21 '08 #2

P: 55
Thanks for the reply,
here is my code
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. my %one2three = ( 'G'=>'GLY','A'=>'ALA','V'=>'VAL','L'=>'LEU','I'=>'ILE',          
  5.                   'S'=>'SER','T'=>'THR','C'=>'CYS','M'=>'MET','P'=>'PRO',          
  6.                   'D'=>'ASP','N'=>'ASN','E'=>'GLU','Q'=>'GLN','K'=>'LYS',          
  7.                   'R'=>'ARG','H'=>'HIS','F'=>'PHE','Y'=>'TYR','W'=>'TRP',          
  8.                   'B'=>'ASX','Z'=>'GLX','X'=>'UNK');
  9. my $count = 0;my @val;my ($line,$seq);
  10.  
  11. open (FH, "GHHDP") or die "Check for the file";
  12. my @prot = <FH>;
  13. close FH;
  14.  
  15. foreach $line (@prot)
  16. {
  17.     if($line =~/^\s*#/)
  18.        {next;}
  19.     elsif($line =~/^>(.*)/)
  20.        { my $title = $1;
  21.          print "* $title\n*\n";
  22.      }
  23.     else{$seq .= $line;}
  24. }
  25. $seq=~ s/\s//g;
  26. my @ss = split(//,$seq);
  27. foreach my $rr(@ss)
  28. {
  29.      $rrr = $one2three{$rr};
  30.      print "$rrr\t";
  31.      $count +=1;
  32. }
  33. print "$count\n";
  34.  
thanks
Kumar
Aug 21 '08 #3

nithinpes
Expert 100+
P: 410
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.

Expand|Select|Wrap|Line Numbers
  1. foreach my $rr(@ss)
  2. {
  3.      my  $rrr= $one2three{$rr};
  4.      print "$rrr ";   ##replace \t with single space
  5.      $count +=1;
  6.      print "\n" if($count%10==0); ## insert newline after every 10 records
  7. }
  8.  
- Nithin
Aug 21 '08 #4

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

Thanks
Kumar
Aug 21 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.