Created
February 4, 2015 15:47
-
-
Save obenshaindw/f2a2ba1168c9de0a3964 to your computer and use it in GitHub Desktop.
Extract genotypes from multisample VCF file using vcftools
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use warnings; | |
use Vcf; | |
my $filename = $ARGV[0]; | |
open ( my $handle, "<", $filename); | |
my $vcf = Vcf->new(fh=>$handle); | |
$vcf->parse_header(); | |
vcf_iterate(); | |
sub vcf_iterate | |
{ | |
while ( my $x=$vcf->next_data_hash() ) | |
{ | |
foreach my $sample ( keys $$x{gtypes} ) | |
{ | |
print "SAMPLE=$sample, genotype_raw=$$x{gtypes}{$sample}{GT}, "; | |
my $decoded_genotype = decode_genotype($x, $sample); | |
print "decoded_genotype=$decoded_genotype\n"; | |
} | |
} | |
} | |
sub decode_genotype | |
{ | |
my ( $x, $sample ) = ( $_[0], $_[1] ); | |
my $gt = $vcf->decode_genotype($$x{REF}, $$x{ALT}, $$x{gtypes}{$sample}{GT}); # returns 'G/G' | |
return $gt; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment