-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_scalpel_calling_pipeline_makefile.pl
executable file
·344 lines (296 loc) · 8.08 KB
/
generate_scalpel_calling_pipeline_makefile.pl
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#!/usr/bin/perl -w
use warnings;
use strict;
use POSIX;
use Getopt::Long;
use File::Path;
use File::Basename;
use Pod::Usage;
=head1 NAME
generate_scalpel_calling_pipeline_makefile
=head1 SYNOPSIS
generate_scalpel_pipeline_makefile [options]
-s sample file list giving the location of each sample
column 1: sample name
column 2: path of bam file
-r reference genome file
-l sequence length file
-w interval width
-o output directory
-m make file name
=head1 DESCRIPTION
This script generates the make file to discovery and genotype a set of individuals.
=cut
my $help;
my $outputDir;
my $makeFile;
my $cluster;
my $sleep;
my $sampleFile;
my $intervalWidth = 1000000;
my $refGenomeFASTAFile;
#initialize options
Getopt::Long::Configure ('bundling');
if(!GetOptions ('h'=>\$help,
'o:s'=>\$outputDir,
'm:s'=>\$makeFile,
'p:s'=>\$partition,
'd:s'=>\$sleep,
's:s'=>\$sampleFile,
'i:s'=>\$intervalWidth,
'r:s'=>\$refGenomeFASTAFile
)
|| !defined($makeFile)
|| !defined($sampleFile)
|| !defined($refGenomeFASTAFile))
{
if ($help)
{
pod2usage(-verbose => 2);
}
else
{
pod2usage(1);
}
}
#programs
#you can set the maximum memory here to be whatever you want
my $scalpel = "/net/fantasia/home/atks/dev/vt/comparisons/programs/scalpel-0.3.2/scalpel";
my $vt = "$vtDir/vt";
printf("generate_scalpel_calling_pipeline_makefile.pl\n");
printf("\n");
printf("options: output dir %s\n", $outputDir);
printf(" make file %s\n", $makeFile);
printf(" partition %s\n", $partition);
printf(" sample file %s\n", $sampleFile);
printf(" interval width %s\n", $intervalWidth);
printf(" reference %s\n", $refGenomeFASTAFile);
printf("\n");
my $vcfOutDir = "$outputDir/vcf";
mkpath($vcfOutDir);
my $finalVCFOutDir = "$outputDir/final";
mkpath($finalVCFOutDir);
my $statsDir = "$outputDir/stats";
mkpath($statsDir);
my $logDir = "$outputDir/log";
mkpath($logDir);
my $auxDir = "$outputDir/aux";
mkpath($auxDir);
my $logFile = "$outputDir/run.log";
########################################
#Read file locations and name of samples
########################################
my %SAMPLE = ();
my @SAMPLES = ();
open(SA,"$sampleFile") || die "Cannot open $sampleFile\n";
my $bamFiles = "";
while (<SA>)
{
s/\r?\n?$//;
if(!/^#/)
{
my ($sampleID, $bamPath) = split(/\s+/, $_);
$SAMPLE{$sampleID} = $bamPath;
$bamFiles .= "$bamPath\n";
push(@SAMPLES, $sampleID);
}
}
close(SA);
my $bamListFile = "$auxDir/bam.list";
open(OUT,">$bamListFile") || die "Cannot open $bamListFile\n";
print OUT $bamFiles;
close(OUT);
print "read in " . scalar(keys(%SAMPLE)) . " samples\n";
###################
#Generate intervals
###################
my %intervalsByChrom = ();
my @intervals = ();
my @intervalNames = ();
my @intervalFiles = ();
my @CHROM = ();
open(SQ,"$refGenomeFASTAFile.fai") || die "Cannot open $refGenomeFASTAFile.fai\n";
while (<SQ>)
{
s/\r?\n?$//;
if(!/^#/)
{
my ($chrom, $len) = split('\t', $_);
print "processing $chrom\t$len ";
push(@CHROM, $chrom);
$intervalsByChrom{$chrom} = ();
my $count = 0;
for my $i (0 .. floor($len/$intervalWidth))
{
my $interval = "";
my $intervalName = "";
my $file = "";
if ($i<floor($len/$intervalWidth))
{
$interval = $chrom . ":" . ($intervalWidth*$i+1) . "-" . ($intervalWidth*($i+1));
$intervalName = $chrom . "_" . ($intervalWidth*$i+1) . "_" . ($intervalWidth*($i+1));
}
elsif ($i*$intervalWidth!=$len)
{
$interval = $chrom . ":" . ($intervalWidth*$i+1) . "-" . $len;
$intervalName = $chrom . "_" . ($intervalWidth*$i+1) . "_" . $len;
}
else
{
last;
}
push(@{$intervalsByChrom{$chrom}}, "$intervalName");
push(@intervals, $interval);
push(@intervalNames, $intervalName);
push(@intervalFiles, $file);
$count++;
}
print "added $count intervals\n";
}
}
close(SQ);
my @tgts = ();
my @deps = ();
my @cmds = ();
my $tgt;
my $dep;
my @cmd;
my $inputVCFFile;
my $outputVCFFile;
########
#Calling
########
#**************
#log start time
#**************
$tgt = "$logDir/start.calling.OK";
$dep = "";
@cmd = ("date | awk '{print \"scalpel variant calling pipeline\\n\\nstart calling: \"\$\$0}' > $logFile");
makeLocalStep($tgt, $dep, @cmd);
if ($intervalWidth!=0)
{
my $intervalVCFFilesOK = "";
for my $i (0 .. $#intervals)
{
for my $sampleID (@SAMPLES)
{
$outputVCFFile = "$vcfOutDir/$intervalNames[$i].genotypes.vcf.gz";
$tgt = "$outputVCFFile.OK";
$dep = "";
print "$scalpel --single --bam $SAMPLE{$sampleID} --ref $refGenomeFASTAFile --bed $intervals[$i] --dir $resultDir";
@cmd = ("$scalpel --single --bam $SAMPLE{$sampleID} --ref $refGenomeFASTAFile --bed $intervals[$i] --dir $resultDir");
makeStep($tgt, $dep, @cmd);
$intervalVCFFilesOK .= " $outputVCFFile.OK";
}
}
#************
#log end time
#************
$tgt = "$logDir/end.calling.OK";
$dep = "$intervalVCFFilesOK";
@cmd = ("date | awk '{print \"end: \"\$\$0}' >> $logFile");
makeLocalStep($tgt, $dep, @cmd);
}
else
{
$outputVCFFile = "$vcfOutDir/all.vcf.gz";
$tgt = "$outputVCFFile.OK";
$dep = "";
@cmd = ("$scalpel mpileup -ugf $refGenomeFASTAFile -b $bamListFile | $bcftools call -vmO z -o $outputVCFFile");
makeStep($tgt, $dep, @cmd);
#************
#log end time
#************
$tgt = "$logDir/end.calling.OK";
$dep = "$outputVCFFile.OK";
@cmd = ("date | awk '{print \"end calling: \"\$\$0}' >> $logFile");
makeLocalStep($tgt, $dep, @cmd);
}
#*******************
#Write out make file
#*******************
open(MAK,">$makeFile") || die "Cannot open $makeFile\n";
print MAK ".DELETE_ON_ERROR:\n\n";
print MAK "all: @tgts\n\n";
#clean
push(@tgts, "clean");
push(@deps, "");
push(@cmds, "\t-rm -rf $outputDir/*.* $vcfOutDir/*.* $vcfOutDir/*/*.* $finalVCFOutDir/*.* $statsDir/* $logDir/* $outputDir/intervals/*.*");
for(my $i=0; $i < @tgts; ++$i) {
print MAK "$tgts[$i] : $deps[$i]\n";
print MAK "$cmds[$i]\n";
}
close MAK;
##########
#Functions
##########
#run a job either locally or by slurm
sub makeJob
{
my ($method, $tgt, $dep, @cmd) = @_;
if ($method eq "local")
{
makeLocalStep($tgt, $dep, @cmd);
}
else
{
makeSlurm($partition, $tgt, $dep, @cmd);
}
}
#run slurm jobs
sub makeSlurm
{
my ($partition, $tgt, $dep, @cmd) = @_;
push(@tgts, $tgt);
push(@deps, $dep);
my $cmd = "";
for my $c (@cmd)
{
#contains pipe
if ($c=~/\|/)
{
++$slurmScriptNo;
my $slurmScriptFile = "$slurmScriptsDir/$slurmScriptNo.sh";
open(IN, ">$slurmScriptFile");
print IN "#!/bin/bash\n";
print IN "set -o pipefail; $c";
close(IN);
chmod(0755, $slurmScriptFile);
$cmd .= "\techo '" . $c . "'\n";
$cmd .= "\tsrun -p $partition $slurmScriptFile\n";
}
else
{
$cmd .= "\tsrun -p $partition " . $c . "\n";
}
}
$cmd .= "\ttouch $tgt\n";
push(@cmds, $cmd);
}
#run a local job
sub makeLocalStep
{
my ($tgt, $dep, @cmd) = @_;
push(@tgts, $tgt);
push(@deps, $dep);
my $cmd = "";
for my $c (@cmd)
{
$cmd .= "\tset -o pipefail; " . $c . "\n";
}
$cmd .= "\ttouch $tgt\n";
push(@cmds, $cmd);
}
#run a local phony job
sub makePhonyJob
{
my ($tgt, $dep, @cmd) = @_;
push(@tgts, $tgt);
push(@deps, $dep);
my $cmd = "";
for my $c (@cmd)
{
$cmd .= "\t" . $c . "\n";
}
push(@cmds, $cmd);
}