Skip to content

Instantly share code, notes, and snippets.

@mjclark
Created March 12, 2012 23:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mjclark/2025264 to your computer and use it in GitHub Desktop.
Save mjclark/2025264 to your computer and use it in GitHub Desktop.
Extract a specific ratio of reads from a BAM file
#This is probably not the best way to go about this, but it basically works.
#Keep in mind that PERL's "rand()" function isn't really random, but for this purpose it doesn't really matter.
#Suggested usage: samtools view -h <in.bam> | perl random_line_extraction.using_ratio.pl <ratio in decimal form> | samtools view -bS - > <out.bam>
$num=shift;
while($line=<STDIN>) {
chomp($line);
if($line =~ m/^@/) {
print "$line\n";
}
else {
my $rnum = rand();
if($rnum<=$num) {
print "$line\n";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment