/usr/bin/sam2vcf.pl is in samtools 1.7-1.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 | #!/usr/bin/env perl
#
# Copyright (C) 2009, 2010 Genome Research Ltd.
#
# Author: Petr Danecek <pd3@sanger.ac.uk>
#
# 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.
# VCF specs: http://www.1000genomes.org/wiki/doku.php?id=1000_genomes:analysis:vcf3.3
use strict;
use warnings;
use Carp;
my $opts = parse_params();
do_pileup_to_vcf($opts);
exit;
#---------------
sub error
{
my (@msg) = @_;
if ( scalar @msg ) { croak(@msg); }
die
"Usage: sam2vcf.pl [OPTIONS] < in.pileup > out.vcf\n",
"Options:\n",
" -h, -?, --help This help message.\n",
" -i, --indels-only Ignore SNPs.\n",
" -r, --refseq <file.fa> The reference sequence, required when indels are present.\n",
" -R, --keep-ref Print reference alleles as well.\n",
" -s, --snps-only Ignore indels.\n",
" -t, --column-title <string> The column title.\n",
"\n";
}
sub parse_params
{
my %opts = ();
$opts{fh_in} = *STDIN;
$opts{fh_out} = *STDOUT;
while (my $arg=shift(@ARGV))
{
if ( $arg eq '-R' || $arg eq '--keep-ref' ) { $opts{keep_ref}=1; next; }
if ( $arg eq '-r' || $arg eq '--refseq' ) { $opts{refseq}=shift(@ARGV); next; }
if ( $arg eq '-t' || $arg eq '--column-title' ) { $opts{title}=shift(@ARGV); next; }
if ( $arg eq '-s' || $arg eq '--snps-only' ) { $opts{snps_only}=1; next; }
if ( $arg eq '-i' || $arg eq '--indels-only' ) { $opts{indels_only}=1; next; }
if ( $arg eq '-?' || $arg eq '-h' || $arg eq '--help' ) { error(); }
error("Unknown parameter \"$arg\". Run -h for help.\n");
}
return \%opts;
}
sub iupac_to_gtype
{
my ($ref,$base) = @_;
my %iupac = (
'K' => ['G','T'],
'M' => ['A','C'],
'S' => ['C','G'],
'R' => ['A','G'],
'W' => ['A','T'],
'Y' => ['C','T'],
);
if ( !exists($iupac{$base}) )
{
if ( $base ne 'A' && $base ne 'C' && $base ne 'G' && $base ne 'T' ) { error("FIXME: what is this [$base]?\n"); }
if ( $ref eq $base ) { return ('.','0/0'); }
return ($base,'1/1');
}
my $gt = $iupac{$base};
if ( $$gt[0] eq $ref ) { return ($$gt[1],'0/1'); }
elsif ( $$gt[1] eq $ref ) { return ($$gt[0],'0/1'); }
return ("$$gt[0],$$gt[1]",'1/2');
}
sub parse_indel
{
my ($cons) = @_;
if ( $cons=~/^-/ )
{
my $len = length($');
return "D$len";
}
elsif ( $cons=~/^\+/ ) { return "I$'"; }
elsif ( $cons eq '*' ) { return undef; }
error("FIXME: could not parse [$cons]\n");
}
# An example of the pileup format:
# 1 3000011 C C 32 0 98 1 ^~, A
# 1 3002155 * +T/+T 53 119 52 5 +T * 4 1 0
# 1 3003094 * -TT/-TT 31 164 60 11 -TT * 5 6 0
# 1 3073986 * */-AAAAAAAAAAAAAA 3 3 45 9 * -AAAAAAAAAAAAAA 7 2 0
#
sub do_pileup_to_vcf
{
my ($opts) = @_;
my $fh_in = $$opts{fh_in};
my $fh_out = $$opts{fh_out};
my ($prev_chr,$prev_pos,$prev_ref);
my $refseq;
my $ignore_indels = $$opts{snps_only} ? 1 : 0;
my $ignore_snps = $$opts{indels_only} ? 1 : 0;
my $keep_ref = $$opts{keep_ref} ? 1 : 0;
my $title = exists($$opts{title}) ? $$opts{title} : 'data';
print $fh_out
qq[##fileformat=VCFv3.3\n],
qq[##INFO=DP,1,Integer,"Total Depth"\n],
qq[##FORMAT=GT,1,String,"Genotype"\n],
qq[##FORMAT=GQ,1,Integer,"Genotype Quality"\n],
qq[##FORMAT=DP,1,Integer,"Read Depth"\n],
qq[#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\tFORMAT\t$title\n]
;
while (my $line=<$fh_in>)
{
chomp($line);
my (@items) = split(/\t/,$line);
if ( scalar @items<8 )
{
error("\nToo few columns, does not look like output of 'samtools pileup -c': $line\n");
}
my ($chr,$pos,$ref,$cons,$cons_qual,$snp_qual,$rms_qual,$depth,$a1,$a2) = @items;
$ref = uc($ref);
$cons = uc($cons);
my ($alt,$gt);
if ( $ref eq '*' )
{
# An indel is involved.
if ( $ignore_indels )
{
$prev_ref = $ref;
$prev_pos = $pos;
$prev_chr = $chr;
next;
}
if (!defined $prev_chr || $chr ne $prev_chr || $pos ne $prev_pos)
{
if ( !$$opts{refseq} ) { error("Cannot do indels without the reference.\n"); }
if ( !$refseq ) { $refseq = Fasta->new(file=>$$opts{refseq}); }
$ref = $refseq->get_base($chr,$pos);
$ref = uc($ref);
}
else { $ref = $prev_ref; }
# One of the alleles can be a reference and it can come in arbitrary order. In some
# cases */* can be encountered. In such a case, look in the additional columns.
my ($al1,$al2) = split(m{/},$cons);
if ( $al1 eq $al2 && $al1 eq '*' ) { $al1=$a1; $al2=$a2; }
my $alt1 = parse_indel($al1);
my $alt2 = parse_indel($al2);
if ( !$alt1 && !$alt2 ) { error("FIXME: could not parse indel:\n", $line); }
if ( !$alt1 )
{
$alt=$alt2;
$gt='0/1';
}
elsif ( !$alt2 )
{
$alt=$alt1;
$gt='0/1';
}
elsif ( $alt1 eq $alt2 )
{
$alt="$alt1";
$gt='1/1';
}
else
{
$alt="$alt1,$alt2";
$gt='1/2';
}
}
else
{
if ( $ignore_snps || (!$keep_ref && $ref eq $cons) )
{
$prev_ref = $ref;
$prev_pos = $pos;
$prev_chr = $chr;
next;
}
# SNP
($alt,$gt) = iupac_to_gtype($ref,$cons);
}
print $fh_out "$chr\t$pos\t.\t$ref\t$alt\t$snp_qual\t0\tDP=$depth\tGT:GQ:DP\t$gt:$cons_qual:$depth\n";
$prev_ref = $ref;
$prev_pos = $pos;
$prev_chr = $chr;
}
}
#------------- Fasta --------------------
#
# Uses samtools to get a requested base from a fasta file. For efficiency, preloads
# a chunk to memory. The size of the cached sequence can be controlled by the 'size'
# parameter.
#
package Fasta;
use strict;
use warnings;
use Carp;
sub Fasta::new
{
my ($class,@args) = @_;
my $self = {@args};
bless $self, ref($class) || $class;
if ( !$$self{file} ) { $self->throw(qq[Missing the parameter "file"\n]); }
$$self{chr} = undef;
$$self{from} = undef;
$$self{to} = undef;
if ( !$$self{size} ) { $$self{size}=10_000_000; }
bless $self, ref($class) || $class;
return $self;
}
sub read_chunk
{
my ($self,$chr,$pos) = @_;
my $to = $pos + $$self{size};
my $cmd = "samtools faidx $$self{file} $chr:$pos-$to";
my @out = `$cmd`;
if ( $? ) { $self->throw("$cmd: $!"); }
my $line = shift(@out);
if ( !($line=~/^>$chr:(\d+)-(\d+)/) ) { $self->throw("Could not parse: $line"); }
$$self{chr} = $chr;
$$self{from} = $1;
$$self{to} = $2;
my $chunk = '';
while ($line=shift(@out))
{
chomp($line);
$chunk .= $line;
}
$$self{chunk} = $chunk;
return;
}
sub get_base
{
my ($self,$chr,$pos) = @_;
if ( !$$self{chr} || $chr ne $$self{chr} || $pos<$$self{from} || $pos>$$self{to} )
{
$self->read_chunk($chr,$pos);
}
my $idx = $pos - $$self{from};
return substr($$self{chunk},$idx,1);
}
sub throw
{
my ($self,@msg) = @_;
croak(@msg);
}
|