Skip to content

Instantly share code, notes, and snippets.

@winni2k
Created July 27, 2015 13:58
Show Gist options
  • Save winni2k/3ff0d31208b1bbe08075 to your computer and use it in GitHub Desktop.
Save winni2k/3ff0d31208b1bbe08075 to your computer and use it in GitHub Desktop.
Perl script for checking if a WTCCC style haps file has sites that are flipped in comparison to a genotype map file
#!/usr/bin/env perl
=head1 NAME
unflip.pl
=head1 SYNOPSIS
# unflip any sites that do not match between sites.map and haps
unflip.pl -m sites.map < haps > unflipped.haps
# allow a map file with extra columns
unflip.pl -m sites.map -p < haps > unflipped.haps
# drop sites that do not appear in sites.map
unflip.pl -m sites.map -d < haps > unflipped.haps
=head1 DESCRIPTION
sites.map is a tab separated file with four columns and without a header.
The columns are chromosome, position, ref allele, alt allele. For example:
20\t9000\tA\tG
...
The map shall contain all sites in haps. Some of the sites in haps may have had their
REF and ALT alleles flipped. In that case the REF and ALT alleles are
flipped and the alleles are twiddled (0->1,1->0).
=head1 AUTHOR
Warren Kretzschmar -- wkretzsch@gmail.com
=head1 LICENSE
The MIT License (MIT)
Copyright (c) 2015 Warren W. Kretzschmar
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
=cut
use strictures;
use warnings;
use Getopt::Std;
use autodie;
use Carp;
my %args;
getopts( 'm:pd', \%args );
my $permissiveMap = $args{p} || 0;
my $dropNonMatchingSites = $args{d} || 0;
# load map
my %map;
open( my $fh, '<', $args{m} );
while (<$fh>) {
chomp;
next unless m/./;
my @line = split( /\t/, $_, 5 );
croak "Map has too many columns (>4)" if ( @line > 4 && !$permissiveMap );
croak "Map has too few columns (<4)" if @line < 4;
$map{ join( "\t", @line ) } = 1;
}
# read in haplotypes from STDIN
my $numCols = undef;
my $numMonoFlipped = 0;
my $numPolyFlipped = 0;
my $numDropped = 0;
my $numProcessed = 0;
while (<>) {
chomp;
unless ( defined $numCols ) {
my @line = split(/ /);
$numCols = @line;
}
my @line = split( / /, $_, 6 );
# check if site exists in map
# see if flip exists if not
if ( !$map{ join( "\t", @line[ 0, 2 .. 4 ] ) } ) {
if ( $map{ join( "\t", @line[ 0, 2, 4, 3 ] ) } ) {
print join( ' ', @line[ 0 .. 2, 4, 3 ] );
my @alleles = split( / /, $line[5] );
my $poly = 0;
if (@alleles) {
my $firstAll = $alleles[0];
for my $all (@alleles) {
$poly = 1 if ( !$poly && $all != $firstAll );
$all = $all == 1 ? 0 : 1;
print ' ' . $all;
}
}
print "\n";
if ($poly) { ++$numPolyFlipped }
else { ++$numMonoFlipped }
}
elsif ($dropNonMatchingSites) {
++$numDropped;
}
else {
croak "Found unexpected site: @line[0..4]";
}
}
else {
print $_. "\n";
}
++$numProcessed;
}
print STDERR
"Processed/flipped monomorphic/flipped non-monomorphic/dropped lines: $numProcessed/$numMonoFlipped/$numPolyFlipped/$numDropped\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment