-
Notifications
You must be signed in to change notification settings - Fork 0
/
Snakefile
executable file
·206 lines (134 loc) · 6.05 KB
/
Snakefile
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
import os
import pandas as pd
from collections import Counter
#change to the path of your ref genome
genome = '/mnt/d/2022/MOR/MOR_22_reanalyseTechnicalDataset/00_ref/chr7.fasta'
tmp = '/home/drukewitz/conda_tmp/'
bed = 'data/Twist-custom_hg19.bed'
DIRECTION=["1","2"]
GROUPS=set()
sample_sheet=pd.read_csv("samples.tsv", sep="\t",dtype=object)
def check_symlink(file1, file2):
try:
os.symlink(file1, file2)
except FileExistsError:
print("Link for " + file1 + " is already present in 01_raw")
SAMPLES=list(sample_sheet["ID"])
for b in set(SAMPLES):
os.makedirs("../01_raw/" +b+ "/fastqc", exist_ok=True)
check_symlink(sample_sheet[sample_sheet["ID"]==b]["forward reads"].values[0], "../01_raw/"+b +"/"+ b +"_1P.fastq.gz")
check_symlink(sample_sheet[sample_sheet["ID"]==b]["reverse reads"].values[0], "../01_raw/"+b +"/"+ b +"_2P.fastq.gz")
rule all:
input:
expand('../06_Vardict/{sample}.vcf', sample=SAMPLES),
rule Fastq2Sam:
input:
r1= '../01_raw/{sample}/{sample}_1P.fastq.gz',
r2= '../01_raw/{sample}/{sample}_2P.fastq.gz',
output:
unmappedBam = '../02_unmappedBam/{sample}.unmapped.bam'
conda:
"envs/picard_bwa.yaml"
threads:1
shell:
'picard FastqToSam TMP_DIR={tmp} FASTQ={input.r1} FASTQ2={input.r2} O={output.unmappedBam} SM=sample'
rule ExtractUmisFromBam:
input:
unmappedBam = '../02_unmappedBam/{sample}.unmapped.bam'
output:
unmappedBamwithUMI = '../02_unmappedBam/{sample}.unmappedUMI.bam'
conda:
"envs/fgbio.yaml"
threads:1
shell:
'fgbio -XX:-UseGCOverheadLimit -Xms750m -Xmx24g --tmp-dir={tmp} ExtractUmisFromBam --input={input.unmappedBam} --output={output.unmappedBamwithUMI} --read-structure=3M2S146T 3M2S146T --molecular-index-tags=ZA ZB --single-tag=RX'
rule Index:
input:
unmappedBamwithUMI = '../02_unmappedBam/{sample}.unmappedUMI.bam'
output:
unmappedBamwithUMIbai = '../02_unmappedBam/{sample}.unmappedUMI.bam.bai'
conda:
"envs/samtools.yaml"
threads:1
shell:
'samtools index {input.unmappedBamwithUMI}'
rule Mapping:
input:
unmappedBamwithUMIbai = '../02_unmappedBam/{sample}.unmappedUMI.bam.bai',
unmappedBamwithUMI = '../02_unmappedBam/{sample}.unmappedUMI.bam'
output:
mappedBAM = '../03_mappedBam/{sample}.mapped.bam'
conda:
"envs/picard_bwa.yaml"
threads:12
shell:
'picard SamToFastq TMP_DIR={tmp} I={input.unmappedBamwithUMI} F=/dev/stdout INTERLEAVE=true \
| bwa mem -p -t {threads} {genome} /dev/stdin \
| picard MergeBamAlignment \
UNMAPPED={input.unmappedBamwithUMI} ALIGNED=/dev/stdin O={output.mappedBAM} R={genome} \
SO=coordinate ALIGNER_PROPER_PAIR_FLAGS=true MAX_GAPS=-1 \
ORIENTATIONS=FR VALIDATION_STRINGENCY=SILENT CREATE_INDEX=true'
rule GroupReadsByUmi:
input:
mappedBAM = '../03_mappedBam/{sample}.mapped.bam'
output:
groupedmappedBAM = '../03_mappedBam/{sample}.grouped.mapped.bam'
threads:1
conda:
"envs/fgbio.yaml"
shell:
'fgbio -XX:-UseGCOverheadLimit -Xms750m -Xmx24g --tmp-dir={tmp} GroupReadsByUmi --input={input.mappedBAM} --output={output.groupedmappedBAM} --strategy=paired --edits=1 --min-map-q=2'
###################################################################################################################################################
rule CallMolecularConsensusReads:
input:
groupedmappedBAM = '../03_mappedBam/{sample}.grouped.mapped.bam'
output:
groupedUnmappedconsensusBAM = '../04_CallMolecularConsensusReads/{sample}.grouped.mapped.consensus.bam'
threads:1
conda:
"envs/fgbio.yaml"
shell:
'fgbio -XX:-UseGCOverheadLimit \
-Xms750m -Xmx24g --tmp-dir={tmp} \
CallMolecularConsensusReads --min-input-base-quality=10 --min-reads=2 --max-reads=1000000 --output-per-base-tags=false --sort-order=:none: -i {input.groupedmappedBAM} -o {output.groupedUnmappedconsensusBAM}'
###################################################################################################################################################
rule SortBam:
input:
groupedUnmappedconsensusBAM = '../04_CallMolecularConsensusReads/{sample}.grouped.mapped.consensus.bam'
output:
groupedUnmappedconsensusBAMsorted = '../04_CallMolecularConsensusReads/{sample}.grouped.mapped.consensus.sorted.bam'
threads:1
conda:
"envs/picard_bwa.yaml"
shell:
"picard SortSam -I {input.groupedUnmappedconsensusBAM} -O {output.groupedUnmappedconsensusBAMsorted} -SO queryname"
rule CallMappConsensusReads:
input:
groupedmappedconsensusBAM = '../04_CallMolecularConsensusReads/{sample}.grouped.mapped.consensus.sorted.bam'
output:
consensusmappedBAM= '../05_ConsensusMappedBam/{sample}.consensus.bam'
threads:12
conda:
"envs/picard_bwa.yaml"
shell:
'picard SamToFastq TMP_DIR={tmp} I={input.groupedmappedconsensusBAM} \
F=/dev/stdout INTERLEAVE=true \
| bwa mem -p -t {threads} {genome} /dev/stdin \
| picard MergeBamAlignment \
UNMAPPED={input.groupedmappedconsensusBAM} ALIGNED=/dev/stdin O={output.consensusmappedBAM} R={genome} \
SO=coordinate ALIGNER_PROPER_PAIR_FLAGS=true MAX_GAPS=-1 \
ORIENTATIONS=FR VALIDATION_STRINGENCY=SILENT CREATE_INDEX=true'
rule VardictCalling:
input:
consensusmappedBAM= '../05_ConsensusMappedBam/{sample}.consensus.bam'
threads: 6
output:
vcf= '../06_Vardict/{sample}.vcf'
conda:
"envs/vardict.yaml"
shell:
'vardict-java -G {genome} \
-th {threads} -f 0.001 -r 2 \
-b {input.consensusmappedBAM} \
-z -c 1 -S 2 -E 3 -g 4 \
--nosv {bed}| teststrandbias.R | var2vcf_valid.pl -E -f 0.001 > {output.vcf}'