Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^3: Private Utilities

by l3v3l (Monk)
on Dec 01, 2005 at 22:36 UTC ( #513459=note: print w/replies, xml ) Need Help??


in reply to Re^2: Private Utilities
in thread Private Utilities

no problem!! round robin is fine - here is a way I have done it in a liner without having to read the entire file into mem (just an example) :
# Choose first N FASTA records perl -ne 'BEGIN {$/=">";$o=0}{chomp;$o<N?(/^$/?next:print">$_"):last;$ +o++}' EXAMPLE.fa
example usage:
perl -ne 'BEGIN{$/=">";$o=0}{chomp;$o<22?(/^$/?next:print">$_"):last; +$o++}' b4x_est100.fa # print the first 22 records

Replies are listed 'Best First'.
Re^4: Private Utilities
by thor (Priest) on Dec 02, 2005 at 04:57 UTC
    Alright...here's a simple multiplexer:
    use strict; use warnings; use Getopt::Long; my ($number, $format) = (2, "output"); GetOptions( "number=i" => \$number, "format=s" => \$format, ); my @output_file; foreach my $num (1..$number) { my $file = "$format.$num"; open( $output_file[$num-1], ">", $file ) or die "Couldn't open + '$file' for write: $!\n"; } my $file_num = 0; while(<>) { print {$output_file[$file_num]} $_; $file_num += 1; $file_num %= $number; }
    Save it in a file of your choosing, and call it like:
    perl script.pl --format out --number 7 input1 input2 ...

    thor

    The only easy day was yesterday

      Right on! thank you for the code thor. If you are interested or have any ideas on how to improve - here are some other (bioinformatics) liners that I have been working with and have put on the site l3v3l's scratchpad
        # Choose the Nth FASTA record # Called as "perl script_name.pl line_number file_name" use warnings; use strict; $/= '>'; my $line_number = shift; while(<>) chomp; if ($.-1 == $line_number ) { print; exit 0; } }
        # Choose any Range of FASTA records # Called as perl script_name.pl --start start_line --end end_line file +_name # both --start and --end are optional; # no --start => start reading from end of file, end at --end # no --end => start reading from --start, read to end of file # neither => print every line use warnings; use strict; use Getopt::Long; my ($start, $end) = (undef, undef); GetOptions( "start=i" => \$start, "end=i" => \$end, ); $/='>'; while(<>) { if( (!defined($start) || $start <= $.) && (!defined($end) || $. <= $end) ) { print; }
        In reality, the second script subsumes the first as a special case (set start and end to the same value).

        As for your "show the number of lines" example, I don't understand it. From the looks of it, it looks like it counts the number of lines in the file that have a whitespace character in it. Is this really what you want?

        thor

        The only easy day was yesterday

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2022-05-18 10:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (70 votes). Check out past polls.

    Notices?