-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrun-unicall
executable file
·257 lines (231 loc) · 9.33 KB
/
run-unicall
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
#!/usr/bin/env perl
# The MIT License
#
# Copyright (c) 2016 Broad Institute
#
# 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.
use strict;
use warnings;
use Getopt::Std;
# chromosome lengths to determine human genome build
my (%hs37, %hg19, %hg38);
%hs37 = (1=>249250621, 2=>243199373, 3=>198022430, 4=>191154276, 5=>180915260, 6=>171115067, 7=>159138663, 8=>146364022, 9=>141213431, 10=>135534747,
11=>135006516, 12=>133851895, 13=>115169878, 14=>107349540, 15=>102531392, 16=>90354753, 17=>81195210, 18=>78077248, 19=>59128983, 20=>63025520,
21=>48129895, 22=>51304566, X=>155270560);
%hg38 = (chr1=>248956422, chr2=>242193529, chr3=>198295559, chr4=>190214555, chr5=>181538259, chr6=>170805979, chr7=>159345973, chr8=>145138636, chr9=>138394717, chr10=>133797422,
chr11=>135086622, chr12=>133275309, chr13=>114364328, chr14=>107043718, chr15=>101991189, chr16=>90338345, chr17=>83257441, chr18=>80373285, chr19=>58617616, chr20=>64444167,
chr21=>46709983, chr22=>50818468, chrX=>156040895);
$hg19{"chr$_"} = $hs37{$_} for (keys %hs37);
# command lines and options
my %opts = (a=>'fb', M=>'4g', e=>.01);
getopts("a:p:Ge:M:", \%opts);
die("Usage: run-unicall [options] <ref.fa> [prefix:]<aln.bam> [...]
General options:
-p FILE call regions in BED FILE in parallel [inferred]
-G don't re-call genotypes from PL/GL
-a STR variant calling algorithm [fb]
fb - freebayes, pp - platypus, st - samtools
hc - GATK-HaplotypeCaller v3, ug - UnifiedGenotyper
hc4 - GATK-HaplotypeCaller v4
FreeBayes options:
-e FLOAT coefficient to skip poorly mapped reads (0.01 recommended for human) [disabled]
GATK options:
-M STR max Java heap size [4g]
Example:
unicall.kit/run-unicall hg19.fa aln.bam > run-fb.mak && make -j8 -f run-fb.mak
unicall.kit/run-unicall -a hc hs37d5.fa aln.bam > run-hc.mak && make -j8 -f run-hc.mak
") if @ARGV < 2;
my $ref = shift(@ARGV);
my $algo = $opts{a};
my @mak = ();
# check path
my $exepath = $0 =~/^\S+\/[^\/\s]+/? $0 : &which($0);
my $root = $0 =~/^(\S+)\/[^\/\s]+/? $1 : undef;
$root = $exepath =~/^(\S+)\/[^\/\s]+/? $1 : undef if !defined($root);
die "ERROR: failed to locate the root directory\n" if !defined($root);
push(@mak, "ROOT=$root");
push(@mak, 'HAPDIP=$(ROOT)/k8 $(ROOT)/hapdip.js');
# test GATK
if ($algo eq 'hc' || $algo eq 'ug') {
die qq(ERROR: failed to locate the 'java' executable\n) unless &which('java');
die "ERROR: failed to locate file 'GenomeAnalysisTK.jar'. Please copy it to $root.\n" unless (-f "$root/GenomeAnalysisTK.jar");
}
# test GATK4
if ($algo eq 'hc4') {
die qq(ERROR: failed to locate the 'java' executable\n) unless &which('java');
die "ERROR: failed to locate file 'gatk-local.jar'. Please copy it to $root.\n" unless (-f "$root/gatk-local.jar");
}
# test FASTA index
die "ERROR: failed to locate the FASTA index.\n" unless (-f "$ref.fai");
if ($algo eq 'hc' || $algo eq 'ug' || $algo eq 'hc4') {
my $tmp = $ref;
$tmp =~ s/\.fa(sta)?$//g;
die qq(ERROR: failed to localte the FASTA dict file required by GATK. Run the following to generate:
awk '{print "\@SQ\\tSN:"\$1"\\tLN:"\$2}' $ref.fai > $tmp.dict\n) unless (-f "$tmp.dict");
}
push(@mak, "REF=$ref");
# find the genome version
unless (defined $opts{p}) {
my (%fai, $hs_build);
open(FH, "$ref.fai") || die "ERROR: failed to open the FASTA index.\n";
while (<FH>) {
chomp;
my @t = split("\t");
$fai{$t[0]} = $t[1];
}
close(FH);
if (&test_fai(\%fai, \%hg19)) { $opts{p} = 'hg19'; }
elsif (&test_fai(\%fai, \%hs37)) { $opts{p} = 'hs37'; }
elsif (&test_fai(\%fai, \%hg38)) { $opts{p} = 'hg38'; }
}
# read regions to parallelize
my @split = ();
if (defined $opts{p}) {
open(FH, $opts{p} =~ /\.gz$/? "gzip -dc $opts{p} |" : $opts{p}) || open(FH, "$root/$opts{p}-regions.bed") || die "ERROR: failed to open the region BED";
while (<FH>) {
chomp;
my @t = split("\t");
push(@split, [$t[0], $t[1], $t[2]]);
}
close(FH);
}
# generate prefix
my (@prefix, @bam);
for (my $i = 0; $i < @ARGV; ++$i) {
my $tmp = $ARGV[$i];
if (-f $tmp && $tmp =~ /\.(bam|cram)$/) {
$prefix[$i] = $bam[$i] = $tmp;
$prefix[$i] =~ s/\.(bam|cram)$//;
} elsif ($tmp =~ /^([^\s:]+):(\S+)/ && -f $2) {
$prefix[$i] = $1;
$bam[$i] = $2;
} else {
die "ERROR: failed to infer the prefix for input '$ARGV[$i]'\n";
}
unless (-f "$bam[$i].bai") {
my $tmp = $bam[$i];
$tmp =~ s/\.bam$/.bai/;
die "ERROR: failed to locate the BAM index of '$bam[$i]'\n" unless (-f $tmp);
}
}
# generate the main target
my @all_dep;
for (my $i = 0; $i < @prefix; ++$i) {
push(@all_dep, "$prefix[$i].flt.vcf.gz.tbi");
}
push(@mak, '', "all:" . join(" ", @all_dep), '');
# generate split VCFs
for (my $i = 0; $i < @bam; ++$i) {
my ($prefix, $bam) = ($prefix[$i], $bam[$i]);
# generate raw VCF
if (@split != 0) {
my $line = '';
for (my $i = 0; $i < @split; $i += 4) {
$line .= " \\\n" if $i;
$line .= $i == 0? "$prefix.raw.vcf.gz:" : ' ';
for (my $j = 0; $j < 4 && $i + $j < @split; ++$j) {
$line .= ' ' if $j;
$line .= "$prefix.split-" . join("_", @{$split[$i+$j]}) . '.vcf';
}
}
push(@mak, $line);
push(@mak, "\t" . q[(awk '/^#/{print}!/^#/{exit}' $<; echo $^ | xargs cat | grep -v "^#") | $(ROOT)/htsbox bgzip > $@], '');
for my $reg (@split) {
push(@mak, &call(\%opts, $prefix, $bam, $reg));
}
} else {
push(@mak, "$prefix.raw.vcf.gz:$prefix.raw.vcf");
push(@mak, "\t" . '$(ROOT)/htsbox bgzip $<', '');
push(@mak, &call(\%opts, $prefix, $bam));
}
# filter
my $cmd = !defined($opts{G})? ' | $(HAPDIP) upd1gt -q' : '';
push(@mak, "$prefix.anno.gz:$prefix.raw.vcf.gz");
push(@mak, "\t" . '$(HAPDIP) deovlp $<' . $cmd . ' | $(HAPDIP) anno | gzip -1 > $@', '');
push(@mak, "$prefix.flt.vcf.gz:$prefix.anno.gz");
push(@mak, "\t" . '$(HAPDIP) filter $< | $(ROOT)/htsbox bgzip > $@', '');
# tabix
push(@mak, "$prefix.flt.vcf.gz.tbi:$prefix.flt.vcf.gz");
push(@mak, ' $(ROOT)/htsbox tabix -fpvcf $<', "");
}
# print Makefile
print join("\n", @mak), "\n";
exit(0);
sub which {
my $file = shift;
my $path = (@_)? shift : $ENV{PATH};
return if (!defined($path));
foreach my $x (split(":", $path)) {
$x =~ s/\/$//;
return "$x/$file" if (-x "$x/$file");
}
return;
}
sub test_fai {
my $fai = shift;
my $src = shift;
for my $key (keys %$src) {
return 0 if !defined($fai->{$key}) || $src->{$key} != $fai->{$key};
}
return 1;
}
sub call {
my $opts = shift;
my $prefix = shift;
my $bam = shift;
my $reg = shift;
my $algo = $opts->{a};
my $vcf = defined($reg)? "$prefix.split-" . join("_", @$reg) . ".vcf" : "$prefix.raw.vcf";
my (@mak, $cmd);
push(@mak, "$vcf:" . $bam);
if ($algo eq 'hc4') {
$cmd = 'java -Xmx' .$opts{M};
$cmd .= ' -XX:ParallelGCThreads=1 -XX:ConcGCThreads=1';
$cmd .= ' -Dsamjdk.use_async_io_read_samtools=false -Dsamjdk.use_async_io_write_samtools=true -Dsamjdk.use_async_io_write_tribble=false -Dsamjdk.compression_level=2';
$cmd .= ' -jar $(ROOT)/gatk-local.jar HaplotypeCaller --native-pair-hmm-threads 1 -R $(REF) -I $< -O $@';
$cmd .= " -L $reg->[0]:" . ($reg->[1] + 1) . "-$reg->[2]" if defined($reg);
} elsif ($algo eq 'hc') {
$cmd = 'java -Xmx' . $opts{M} . ' -jar $(ROOT)/GenomeAnalysisTK.jar -T HaplotypeCaller -R $(REF) -gt_mode DISCOVERY -stand_call_conf 30 -stand_emit_conf 10 -o $@ -I $<';
$cmd .= " -L $reg->[0]:" . ($reg->[1] + 1) . "-$reg->[2]" if defined($reg);
} elsif ($algo eq 'ug') {
$cmd = 'java -Xmx' . $opts{M} . ' -jar $(ROOT)/GenomeAnalysisTK.jar -T UnifiedGenotyper -R $(REF) -glm BOTH -stand_call_conf 30 -stand_emit_conf 10 -o $@ -I $<';
$cmd .= " -L $reg->[0]:" . ($reg->[1] + 1) . "-$reg->[2]" if defined($reg);
} elsif ($algo eq 'st') {
$cmd = '$(ROOT)/samtools mpileup -ugf $(REF)';
$cmd .= " -r $reg->[0]:" . ($reg->[1] + 1) . "-$reg->[2]" if defined($reg);
$cmd .= ' $< | $(ROOT)/bcftools call -vmO v -o $@';
} elsif ($algo eq 'fb') {
if ($opts->{e} > 0. && $opts->{e} < 1.) {
$cmd = '$(ROOT)/htsbox samview -l0 -e' . $opts->{e} . ' $<';
$cmd .= " $reg->[0]:" . ($reg->[1] + 1) . "-$reg->[2]" if defined($reg);
$cmd .= ' | $(ROOT)/freebayes -f $(REF) -P.5 /dev/stdin > $@';
} else {
$cmd = '$(ROOT)/freebayes -f $(REF) -P.5';
$cmd .= " -r $reg->[0]:$reg->[1]-$reg->[2]" if defined($reg);
$cmd .= ' $< > $@';
}
} elsif ($algo eq 'pp') {
$cmd = '$(ROOT)/platypus.sh callVariants --refFile=$(REF) --bamFiles=$< --output=$@';
$cmd .= " --region=$reg->[0]:$reg->[1]-$reg->[2]" if defined($reg);
}
push(@mak, "\t$cmd", "");
return @mak;
}