perl, extract every 40th seg but stop after getting 300,000 sequence
1
0
Entering edit mode
9.4 years ago
cara78 ▴ 10

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.7k views
ADD COMMENT
1
Entering edit mode
9.4 years ago

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

ADD COMMENT

Login before adding your answer.

Traffic: 3077 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6