This is a test version of Biostars. For the public version, visit https://www.biostars.org.
perl, extract every 40th seg but stop after getting 300,000 sequence

Hi

I have a perl script that gets every 40th sequence out of a big fasta file. But now I need it to stop after getting 300,000 sequences to the subset file.

I have a while loop and tried other loops but I cant seem to get it.

use strict;
use warnings;
#extracts sequences to create subset.

open (my $fh,"$ARGV[0]") or die "Failed to open file: $!\n";
open (my $out_fh, ">$ARGV[0]_subset.fasta");

my $count = 0;

while(<$fh>) {

    while($count < 300000){
     $count ++;

#$. holds the number of the last read line
 next if $. % 160 != 1;

   my $header_line = $_;
   my $seq = <$fh>;

   print $out_fh "$header_line$seq";

    }
}

close $fh;
close $out_fh;

Could anyone please advise how to fix the loop

Thanks

perl subset

1 answer

although I think this question would be more appropriate at stackoverflow, I can't help not addressing it: just replace the while($count < 300000){ line (and its corresponding closing curly bracket) by a last if $count == 300000

Log in to answer this question.