-
Notifications
You must be signed in to change notification settings - Fork 2
/
Snakefile_tools
231 lines (193 loc) · 9.69 KB
/
Snakefile_tools
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
# Generic rules
## Alignments (for debugging and other pipeline)
### GMAP
rule gmap_build_genome_index:
output: gmap_genome="data/gmap_genomes/{IDXGENOMENAME}/{IDXGENOMENAME}.version"
shell:"""
gmap_build -d {IDXGENOMENAME} {REFFASTA} -D data/gmap_genomes/
"""
rule gmap_align_sample:
input: fastq=XPDIR+"reads/{sample}.fastq",\
gmap_genome="data/gmap_genomes/{IDXGENOMENAME}/{IDXGENOMENAME}.version"
params: sample=XPDIR+"reads/{sample}",\
sorted_bam_prefix = XPDIR+"alignments/GMAP/{sample}_on_{IDXGENOMENAME}.sorted"
log: "exec_logs/gmap_log_{sample}.txt"
output: temp_sam = XPDIR+"alignments/GMAP/{sample}_on_{IDXGENOMENAME}.sam",\
sorted_bam = XPDIR+"alignments/GMAP/{sample}_on_{IDXGENOMENAME}.sorted.bam",\
sorted_bam_index = XPDIR+"alignments/GMAP/{sample}_on_{IDXGENOMENAME}.sorted.bam.bai",\
temp_bam = XPDIR+"alignments/GMAP/{sample}_on_{IDXGENOMENAME}.bam"
shell:"""
/usr/bin/time -l gmap --min-intronlength=15000 -t 32 -D data/gmap_genomes/{IDXGENOMENAME} \
-d {IDXGENOMENAME} -f samse --read-group-id=EORTC10994 \
--read-group-name=GemSim_test --read-group-library=MWG1 \
--read-group-platform=PACBIO {input.fastq} > {output.temp_sam} 2> {log}
/usr/bin/time -l samtools view -b -S {output.temp_sam} > {output.temp_bam} 2>> {log}
/usr/bin/time -l samtools sort {output.temp_bam} {params.sorted_bam_prefix} 2>> {log}
/usr/bin/time -l samtools index {output.sorted_bam} 2>> {log}
"""
### STAR
#
#rule STAR_build_genome_index:
# input:
# ref_fasta=REFFASTA
# output:
# star_index_dir='data/STAR_genomes/{IDXGENOMENAME}',star_index_file='data/STAR_genomes/{IDXGENOMENAME}/SAindex'
# shell:"""
# {STAR} --runMode genomeGenerate --genomeFastaFiles {input.ref_fasta} --genomeDir {output.star_index_dir}
# """
rule STAR_align_sample:
input: star_index_file='data/STAR_genomes/{IDXGENOMENAME}/SAindex',\
star_genome_dir='data/STAR_genomes/{IDXGENOMENAME}',\
reads=XPDIR+"reads/{sample}.fastq"
params:
sample = XPDIR+"reads/{sample}",\
alignment_ouput = XPDIR+"alignments/STAR/",\
sorted_bam_prefix = XPDIR+"alignments/STAR/{sample}_on_{IDXGENOMENAME}.sorted"
output: sam=XPDIR+"alignments/STAR/{sample}_on_{IDXGENOMENAME}.sam",\
bam=XPDIR+"alignments/STAR/{sample}_on_{IDXGENOMENAME}.bam", \
sorted_bam=XPDIR+"alignments/STAR/{sample}_on_{IDXGENOMENAME}.sorted.bam",\
indexed_bam=XPDIR+"alignments/STAR/{sample}_on_{IDXGENOMENAME}.sorted.bam.bai"
shell:"""
mkdir -p {XPDIR}/alignments/STAR/
{STAR} --genomeDir {input.star_genome_dir} --readFilesIn {input.reads} --outFileNamePrefix {params.alignment_ouput} \
--outSAMattributes All \
--outSAMmapqUnique 40
mv {params.alignment_ouput}/Aligned.out.sam {output.sam}
samtools view -b -S {output.sam} > {output.bam}
samtools sort {output.bam} {params.sorted_bam_prefix}
samtools index {output.sorted_bam}
"""
# MICADO
rule run_micado:
priority :2
input : fasta_ref=REFFASTA,\
random_sample=XPDIR+"reads/{sample}.fastq",\
snp_data=SNPDATA
params : sample_name= XPDIR+"reads/{sample}"
log : "exec_logs/micado_log_{sample}.txt"
output:
micado_results=XPDIR+"results/micado/{sample}.significant_alterations.json",\
shell:"""
source ~/.virtualenvs/micado/bin/activate
export PYTHONPATH=`pwd`/src
# run micado
/usr/bin/time -l python src/MICADo.py --fastq {input.random_sample} --experiment {XPCODE} \
--kmer_length 18 \
--fasta {input.fasta_ref} \
--samplekey {SAMPLEKEY} \
--snp {input.snp_data} \
--npermutations {MICADO_N_PERMUTATIONS} --pvalue 0.01 \
--results {output.micado_results} {MICADO_FLAGS} 2> {log}
"""
rule combine_json:
priority : 50
input : micado_results=XPDIR+"results/micado/{sample}.significant_alterations.json",\
sampler_results=XPDIR+"results/sampler/{sample}.alterations.json"
output:combined_json=XPDIR+"results/micado/{sample}.combined_alterations.temp.json",
cleaned_json=XPDIR+"results/micado/{sample}.combined_alterations.json"
shell:"""
# merge known alterations and identified alterations
source ~/.virtualenvs/micado/bin/activate
export PYTHONPATH=`pwd`/src
bin/merge_json_objects.py {input.sampler_results} {input.micado_results} > {output.combined_json}
jq "." < {output.combined_json} > {output.cleaned_json}
"""
# other variant caller
## VARSCAN
rule varscan_call:
input: aligned_reads=XPDIR+"alignments/GMAP/{sample}.sorted.bam"
output: vcf=XPDIR+"results/varscan/{sample}.vcf", \
pileup=XPDIR+"pileups/{sample}.pileup.txt"
log : "exec_logs/varscan_{sample}.txt"
shell:"""
/usr/bin/time -l samtools mpileup -B -f {REFFASTA} -Q 3 -d 20000 {input.aligned_reads} > {output.pileup} 2> {log}
/usr/bin/time -l {VARSCAN} mpileup2cns {output.pileup} \
--strand-filter 0 --min-coverage 5 --min-reads2 5 --min-avg-qual 60 --min-var-freq 0.05 \
--p-value 0.001 --output-vcf 1 --variants 1 > {output.vcf} 2>> {log}
"""
## GATK
rule picard_dict_for_ref:
input:REFFASTA
output:REFFASTADICT
shell:"""
# Required for picard tool who cowardly refuses to overwrite an existing file
if [ -f {REFFASTADICT} ];
then
rm {REFFASTADICT}
fi
rm
{PICARD_DICT} R= {REFFASTA} O={REFFASTADICT}
"""
rule add_read_group:
input: aligned_reads=XPDIR+"alignments/GMAP/{sample}.sorted.bam"
params: sample=XPDIR+"alignments/GMAP/{sample}.sorted.bam"
log : "exec_logs/gatk_{sample}.txt"
output: aligned_reads_w_rg=XPDIR+"gatk_temp/{sample}.rg.sorted.bam"
shell:"""
/usr/bin/time -l {PICARD_RG} I= {input.aligned_reads} O= {output.aligned_reads_w_rg} ID=1 RGLB={SAMPLEKEY} RGPL=solid RGPU={SAMPLEKEY} RGSM={params.sample} 2> {log}
"""
rule index_for_gatk:
input:aligned_reads_w_rg=XPDIR+"gatk_temp/{sample}.rg.sorted.bam"
output:aligned_reads_w_rg_idx=XPDIR+"gatk_temp/{sample}.rg.sorted.bam.bai"
log : "exec_logs/gatk_{sample}.txt"
shell:"""
/usr/bin/time -l samtools index {input.aligned_reads_w_rg} 2>> {log}
"""
#
#rule gatk_call:
# input: aligned_reads = XPDIR+"gatk_temp/{sample}.rg.sorted.bam",fa_dict=REFFASTADICT,\
# indexed_reads = XPDIR+"gatk_temp/{sample}.rg.sorted.bam.bai"
# log : "exec_logs/gatk_{sample}.txt"
# output: nsplitted=XPDIR+"gatk_temp/{sample}.Nsplitted.bam", \
# intervals=XPDIR+"gatk_temp/{sample}forIndelRealigner.intervals", \
# realigned_bam=XPDIR+"gatk_temp/{sample}_realigned.bam", \
# raw_vcfs=XPDIR+"results/gatk/{sample}_raw.vcf"
# shell:"""
#
# ## Split'N'Trim
# /usr/bin/time -l {GATK} -T SplitNCigarReads -R {REFFASTA} -I {input.aligned_reads} -o {output.nsplitted} \
# -U ALLOW_N_CIGAR_READS --allow_potentially_misencoded_quality_scores 2>> {log}
#
# ## RealignerTargetCreator
# /usr/bin/time -l {GATK} -T RealignerTargetCreator -R {REFFASTA} -I {output.nsplitted} -o {output.intervals} \
# --allow_potentially_misencoded_quality_scores 2>> {log}
#
# ## IndelRealigner
# /usr/bin/time -l {GATK} -T IndelRealigner -R {REFFASTA} -I {output.nsplitted} -targetIntervals {output.intervals} -o {output.realigned_bam} \
# --allow_potentially_misencoded_quality_scores 2>> {log}
#
# ## HaplotypeCaller
# /usr/bin/time -l {GATK} -T HaplotypeCaller -R {REFFASTA} -I {output.realigned_bam} -o {output.raw_vcfs} \
# --emitRefConfidence GVCF --variant_index_type LINEAR --variant_index_parameter 128000 \
# --allow_potentially_misencoded_quality_scores -dontUseSoftClippedBases \
# --maxReadsInRegionPerSample 10000 --min_base_quality_score 30 --forceActive 2>> {log}
# # Check with justine if this is actually needed
# # --intervals 17 \
# """
rule gatk_call:
input: aligned_reads = XPDIR+"gatk_temp/{sample}.rg.sorted.bam",fa_dict=REFFASTADICT,\
indexed_reads = XPDIR+"gatk_temp/{sample}.rg.sorted.bam.bai"
log : "exec_logs/gatk_{sample}.txt"
output: nsplitted=XPDIR+"gatk_temp/{sample}.Nsplitted.bam", \
intervals=XPDIR+"gatk_temp/{sample}forIndelRealigner.intervals", \
realigned_bam=XPDIR+"gatk_temp/{sample}_realigned.bam", \
raw_vcfs=XPDIR+"results/gatk/{sample}_raw.vcf"
shell:"""
## Split'N'Trim
/usr/bin/time -l {GATK} -T SplitNCigarReads -R {REFFASTA} -I {input.aligned_reads} -o {output.nsplitted} \
-U ALLOW_N_CIGAR_READS --allow_potentially_misencoded_quality_scores 2>> {log}
## RealignerTargetCreator
/usr/bin/time -l {GATK} -T RealignerTargetCreator -R {REFFASTA} -I {output.nsplitted} -o {output.intervals} \
--allow_potentially_misencoded_quality_scores 2>> {log}
## IndelRealigner
/usr/bin/time -l {GATK} -T IndelRealigner -R {REFFASTA} -I {output.nsplitted} -targetIntervals {output.intervals} -o {output.realigned_bam} \
--allow_potentially_misencoded_quality_scores 2>> {log}
## HaplotypeCaller
/usr/bin/time -l {GATK} -T HaplotypeCaller -R {REFFASTA} -I {output.realigned_bam} -o {output.raw_vcfs} \
--emitRefConfidence GVCF --variant_index_type LINEAR --variant_index_parameter 128000 \
--allow_potentially_misencoded_quality_scores -dontUseSoftClippedBases \
--maxReadsInRegionPerSample 10000 --min_base_quality_score 30 --forceActive 2>> {log}
# Check with justine if this is actually needed
# --intervals 17 \
"""
# helpers