Skip to content

Instantly share code, notes, and snippets.

@mjclark
mjclark / random_line_extraction.using_ratio.pl
Created March 12, 2012 23:01
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";
}
@mjclark
mjclark / windowize-bed.pl
Created July 30, 2011 22:34
Create a BED file of windows of any size from another BED file.
#Command: cat in.bed | perl windowize-bed.pl <windowsize> > out.bed
$windowsize=shift;
$ctr=0;
print STDERR "$ctr";
while($line=<STDIN>) {
$ctr++;
print STDERR "\r$ctr";
chomp($line);
@line=split(/\t/, $line);
for($i=$line[1]; $i<$line[2]-$windowsize+1; $i+=$windowsize) {
@mjclark
mjclark / mean_allele_balance.sh
Created July 1, 2011 03:54
Calculate mean heterozygous allele balance from VCF4 files
#Automatically calculates mean heterozygous allele balance for heterozygous alleles in VCF4 files
#Allele balance must be pre-caluclated and defined for each variant in advance and given the INFO ID flag "AB" (default for GATK, etc.)
usage="usage: sh mean_allele_balance.sh <input.vcf>";
if (( $# != 1 )); then echo $usage; exit; fi
#input
VCF=$1;
#calculations