Skip to content

Instantly share code, notes, and snippets.

@obenshaindw
Created February 4, 2015 15:47
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 obenshaindw/f2a2ba1168c9de0a3964 to your computer and use it in GitHub Desktop.
Save obenshaindw/f2a2ba1168c9de0a3964 to your computer and use it in GitHub Desktop.
Extract genotypes from multisample VCF file using vcftools
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