Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
How do you define a 'sequence'? How can you tell when you've moved from one to another? Are the sequences listed in series, or are they mixed together in some way? And while I'm at it, what have you tried and what didn't work? See How do I post a question effectively?.

In terms of transforming one file to a series of files, your code might look something like:

my $fh; my $i = 0; while (my $line = <$in>) { if (new_sequence_test($line)) { $i++; open $fh, '>', "seq_$i" or die "Open fail seq_$i: $!\n"; } print $fh $line; }

Update: Now that you've added input and code, I can say a little more. First, while not strictly necessary, strict will help you catch a lot of potential issues and will make variable scoping intent more obvious. See Use strict warnings and diagnostics or die.

When you say while($seq=<IN>), you read in one line of your file. Your split assumes you are slurping your whole file. And especially if file is 'huge', you probably don't want to store it all in memory. Assuming your sequences are split by empty lines, you could modify my already posted code to accomplish your task.

my $fh; my $i = 0; while (my $line = <$in>) { if (!$fh or $line !~ /\S/) { $i++; open $fh, '>', "seq_$i" or die "Open fail seq_$i: $!\n"; } print $fh $line; }

You could also do this very simply by modifying $/, but I suspect that solution would be unclear to you.


#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.


In reply to Re: put every sequence of a file in a different output file by kennethk
in thread put every sequence of a file in a different output file by bingalee

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2024-03-28 12:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found