Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Print the data following few specific lines in perl

by BioLion (Curate)
on Jul 01, 2010 at 16:36 UTC ( #847551=note: print w/ replies, xml ) Need Help??


in reply to Print the data following few specific lines in perl

A different approach might be to use the $/ variable ( see perlvar for details ) to read in each record and then test whether it has the 'pairs' field:

use warnings; use strict; { ## modify input record separator to read ## each FASTA record separately local $/ = '>'; ## test each record, printing out if it has ## pair field while (<DATA>){ print $_ if ($_ =~ /pair:\d+/); } } __DATA__ >CVSF43567.d1 bg|346280 CGTAGCTGATGCTGTGCTGTTGTGTCGGGGGGATATATATATATATATGGGGTCGTAGTCGTAGCGCTAG TATGCTAGCAGCGTAGATGCTGATCGATGCTGATGCTGATCGTAGTCGTAGGCTAGTGCGATCGTAGTCG TAGTCGATGCTGATGCGTAGCTGATGTGCTGCTGATGCTAGTCGTCGTAGCTGATGCATGCTGATCGTAG TGCTCGATGCTAGTCGTAGTCGTAGTCGTAGCGACTGATGCGATCGTAGTCGGATGCTAGCACGTAGCTG GCTCGATGCTGATGCTGAT >CVSF10000.x1 bg|356789 pair:789860 ATGCGTAGCTGATGTGCTGCTGATGCTAGTCGTCGTAGCTGATGCATGCTGATCGTAGTGCTCGATGCTA GTCGTAGTCGTAGTCGTAGCGACTGATGCGATCGTAGTCGGATGATGCTGACTGATGCTGATCTGTACGT CGTAGCTGATGCATGCGCTAGTAGCT >CVSF10000.y1 bg|356790 pair:789859 GCTAGTCGATGCTGATGCTGTAGCTAGCGTAGTCGTACGCGCGCGCGCGCGTTTTTTGTGACGTCGTAGT CCGTAGCTGATGCGATGCTAGTGCTGTGTCAGCTGATGTCGTGTGTAGCTGATGCTGATCGTTCGTGTGT CGATGCTGATGCTAGTCGTAGTGTAT

In this case I am simply testing for the presence of the 'pair' field using a regex (see perlre). Hope this helps!

Just a something something...


Comment on Re: Print the data following few specific lines in perl
Select or Download Code
Re^2: Print the data following few specific lines in perl
by ad23 (Acolyte) on Jul 01, 2010 at 17:47 UTC

    Thank you for your help.

    The code which I am trying to run - what is going wrong in there?? I am a beginner, and it would really help if I know how to modify my code to get the desired output??

    Cheers!

      It isn't really a case of right or wrong - you code is doing exactly what you are telling it to - sorting the header lines based on the descriptor field. As you will see all over the place, TIMTOWTDI (there is more than one way to do it) is a core part of Perl programming, so while your method could be made to work, it would be much more complicated than needed (you would need to buffer the squence associated with each header and print them out together, based on testing the header) and ultimately much harder to maintain etc...

      Just try to work out (on paper if needs be) what steps you need to achieve, than code each one of those steps - shortcuts/better/different solutions may occur to you as you work, but the important thing is that the code is clear and well documented.

      Maybe this doesn't answer your question, but really it is all about practice and experience, so the fact that you have made a good attempt shows that in time you'll be laughing at these sort of problems. Keep plugging away and if you get stuck again, come to the Monastery!

      Just a something something...

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://847551]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2014-08-02 06:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Who would be the most fun to work for?















    Results (55 votes), past polls