Haptotab.pl
Appearance
#!/usr/bin/perl
use strict;
open FILE, "<$ARGV[0]" or die "cannot open $ARGV[0] you jerkwad!\n\n";
#define stuff
my $teomz="crap";
my $window_by_snp=0;
my $maxdip=0;
my $snp_window_size;
my $bp_window_size;
die "Hey dufus, I Need to know window size: use --snp or --bp\n\n" unless $#ARGV>1;
for my $arg (1..$#ARGV){
if( $ARGV[$arg] eq "--snp" ){ $window_by_snp=1; $snp_window_size=$ARGV[$arg+1]; }
if( $ARGV[$arg] eq "--bp" ){ $window_by_snp=0; $bp_window_size=$ARGV[$arg+1]; }
if( $ARGV[$arg] eq "--teo" ){ $teomz="teo"; }
if( $ARGV[$arg] eq "--mz" ){ $teomz="mz"; }
if( $ARGV[$arg] eq "--joint" ){ $teomz="joint"; }
if( $ARGV[$arg] eq "--maxdip" ){ $maxdip=1; }
}
#grab header, ditch first 11 columns to get only individual line names
#assumes infile has been tweaked with cut to include only correct lines
my ($header, @lines)=<FILE>; close FILE;
chomp $header;
my @inds=split(/\t/,$header);
for my $i (1..11){
shift(@inds);
}
my $num_inds=$#inds+1;
my $snp_counter=0; #snp iterator
my $bp_start=0; #window bp iterator
my $bp_counter=0; #window bp length
my %window=();
my $snp;
my $csome;
my $pos;
my @alleles;
my $win=1;
my $pos_old=0;
#run through lines
foreach ( @lines ){
next if $_=~m/\*/ || $_=~m/\-\/\+/ || $_=~m/\+\/\-/; # all indels appear to have a "*" or "-/+" combo in the allele, so regex 'em out.
chomp; #you know it
#regex to grab alleles -- all rows have the "QC+" in them
$_=~m/(\S+)\t\w\/\w\t(\d+)\t(\d+)\t\S\t.*\tQC\+\t(\w\t.*)/;
$snp=$1;
$csome=$2;
$pos=$3;
@alleles=split(/\t/,$4);
next if $pos == $pos_old;
#iterate stuff
$bp_counter = $pos-$bp_start;
$snp_counter++;
#reached snp_window_size snps
if( $window_by_snp == 1 && $snp_counter == $snp_window_size ){
#since ended on this line, add this line, then print
$window{$pos}=[@alleles];
&poly_make(\%window,$snp_counter, $bp_start+1, $pos);
#clean shit up
$bp_start=$pos; $bp_counter=0; $snp_counter=0; %window=(); $pos_old=$pos;
}
#THIS line pushes over bp_window_size bp
elsif( $window_by_snp == 0 && $bp_counter > $bp_window_size ){
#since window ended on last line, print, then add this line
&poly_make(\%window, $snp_counter-1, $bp_start+1, $bp_start+$bp_window_size );
#clean shit up
$bp_start=$bp_start+$bp_window_size; $bp_counter=0; $snp_counter=1; %window=();
#add in this line after cleanup
$window{$pos}=[@alleles];
$pos_old=$pos;
}
#if window not ended, add stuff;
else{
$window{$pos}=[@alleles];
$pos_old=$pos;
}
}
#DO NOT push out last window... drop these SNPs since window will not be comparable.
#&poly_make(\%window, $snp_counter-1, $bp_start, $pos_old);
#make a polytable
sub poly_make
{
my ($window_ref, $num_snps, $start, $end)=@_;
my %window= %{ $window_ref };
my @positions = sort {$a <=> $b} keys(%window);
my $poly = $window_by_snp == 1 ? "poly.snp" : "poly.bp";
my $file="$poly.$teomz.$csome.$start.$end.$win";
open OUT, ">$file";
my $howmany = $maxdip == 1 ? $num_inds : 2*$num_inds;
print OUT "$howmany\t$num_snps\n\t", join("\t",@positions),"\nANC\t";
for my $i (0..$#positions){ print OUT "?\t"; }
print OUT "\n";
for my $counter (0..$#inds){
#take allele and make it diploid
for my $copy (0..1){
print OUT "$inds[$counter]\t";
foreach my $p ( @positions ){
my $allele=$window{$p}->[$counter];
my $gamete=&disambiguity( $allele );
print OUT substr($gamete,$copy,1),"\t";
}
print OUT "\n";
}
}
close OUT;
$win++;
}
sub disambiguity
{
my $allele=@_[0];
#unambiguize them damn codes
$allele=~s/K/GT/; $allele=~s/M/AC/; $allele=~s/R/AG/; $allele=~s/Y/CT/; $allele=~s/S/CG/; $allele=~s/W/AT/;
#duplicate if only one nuke
length($allele) <2 ? return( $allele . $allele) : return($allele);
}