-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalignment.smk
More file actions
209 lines (189 loc) · 6.9 KB
/
Copy pathalignment.smk
File metadata and controls
209 lines (189 loc) · 6.9 KB
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
import os
import sys
import yaml
# Create log directories
os.makedirs('logs/cluster/aln',exist_ok=True)
os.makedirs('logs/cluster/metrics',exist_ok=True)
### FUNCTIONS ###
def map_input(wildcards):
inputs=[]
for RUN,_run in list(FILES[wildcards.sample].items()):
run,lane,index=_run['PU'].split('-',2)#this causes annoyances when - is used in run id. Not fixed by rsplit.
inputs.append(f'bam_input/work/{wildcards.sample}/{wildcards.reference}/{run}/{lane}/{index}/5.markdup.bam')
assert len(inputs)>0
return sorted(inputs)
def get_fastqs(wildcards):
key=f'{wildcards.run}-{wildcards.lane}-{wildcards.index}'
entry=FILES[wildcards.sample][key]
r1,r2=entry['files'][0],entry['files'][1]
def p(x):
return x if x.startswith('FASTQ') or x.startswith('/') else 'FASTQ/'+x
return {'R1':p(r1),'R2':p(r2)}
### ### PYTHON ### ###
with open(config['project']['fastq_config']) as file:
FILES=yaml.load(file,Loader=yaml.BaseLoader)
for sample in list(FILES.keys()):
val=FILES[sample]
flist=sorted(val['files'])
r1_by_base={}
r2_by_base={}
for f in flist:
base=os.path.basename(f)
if '_R1' in base:
base_stem=base.replace('_R1.fastq.gz','').replace('_R1.fq.gz','')
r1_by_base[base_stem]=f
elif '_R2' in base:
base_stem=base.replace('_R2.fastq.gz','').replace('_R2.fq.gz','')
r2_by_base[base_stem]=f
paired=[]
for base_stem in sorted(set(r1_by_base)|set(r2_by_base)):
r1=r1_by_base.get(base_stem)
r2=r2_by_base.get(base_stem)
if r1 is None:
print(f"WARNING: unpaired R2 (no R1): {r2}",file=sys.stderr)
continue
if r2 is None:
print(f"WARNING: unpaired R1 (no R2): {r1}",file=sys.stderr)
continue
paired.append((base_stem,r1,r2))
if not paired:
print(f"WARNING: no paired FASTQs for sample {sample}, skipping",file=sys.stderr)
continue
new_val={}
for base_stem,r1,r2 in paired:
parts=base_stem.split('_')
run,lane,index=(parts[-3],parts[-2],parts[-1]) if len(parts)>=3 else ('run','0','0')
key=f'{run}-{lane}-{index}'
new_val[key]={'PU':f'{run}-{lane}-{index}','files':[r1,r2]}
FILES[sample]=new_val
with open(config['project']['sample_list']) as file:
SAMPLES=file.read().splitlines()
for sample in SAMPLES:
if sample not in FILES:
print(f"WARNING: sample {sample} in sample.list not in fastq config, skipping",file=sys.stderr)
SAMPLES=[s for s in SAMPLES if s in FILES]
for sample in SAMPLES:
os.makedirs(f'logs/cluster/{sample}',exist_ok=True)
### ### ### RULES ### ### ###
localrules:aln_all,bam_table,write_bam_table
rule aln_all:
input:
expand("bam_input/final/{sample}/{sample}.{reference}.bam",sample=SAMPLES,reference=config['reference']['key'])
rule bam_table:
input:
"bam.table"
rule bwa_mem:
input:
unpack(get_fastqs)
output:
temp("bam_input/work/{sample}/{reference}/{run}/{lane}/{index}/1.mapped.bam"),
params:
fasta=config['reference']['fasta'],
outdir="bam_input/work/{sample}/{reference}/{run}/{lane}/{index}"
threads:
4
shell:
#I started having weird errors that it couldnt find the file,
#Like it wasnt making the output directory.
"""
mkdir -p {params.outdir}
bwa mem -M -t {threads} {params.fasta} {input.R1} {input.R2} | samtools view -bS -o {output}
"""
rule samtools_readgroup:
input:
"bam_input/work/{sample}/{reference}/{run}/{lane}/{index}/1.mapped.bam"
output:
rg=temp("bam_input/work/{sample}/{reference}/{run}/{lane}/{index}/2.readgroup.bam"),
fm=temp("bam_input/work/{sample}/{reference}/{run}/{lane}/{index}/3.fixmate.bam"),
qs=temp("bam_input/work/{sample}/{reference}/{run}/{lane}/{index}/4.qsort.bam"),
params:
LB=config['resources']['library_key']
threads:
4
shell:
"""
samtools addreplacerg -@ {threads} -r 'ID:{wildcards.run}.{wildcards.lane}' -r 'PU:{wildcards.run}.{wildcards.lane}.{wildcards.index}' -r 'PL:illumina' -r 'LB:{params.LB}' -r 'SM:{wildcards.sample}' -o {output.rg} {input}
samtools fixmate -m -@ {threads} {output.rg} {output.fm}
samtools sort -@ {threads} -o {output.qs} {output.fm}
"""
rule samtools_markdup:
input:
"bam_input/work/{sample}/{reference}/{run}/{lane}/{index}/4.qsort.bam"
output:
temp("bam_input/work/{sample}/{reference}/{run}/{lane}/{index}/5.markdup.bam")
params:
stats="bam_input/work/{sample}/{reference}/{run}/{lane}/{index}/5.stats.txt"
threads:
4
shell:
"""
samtools markdup -s -f {params.stats} -@ {threads} {input} {output}
"""
rule input_ready:
input:
map_input
output:
temp("bam_input/work/{sample}/{reference}/input.bam")
threads:
4
shell:
"""
samtools merge -f -@ {threads} {output} {input}
samtools index {output}
"""
rule ValidateSamFile:
input:
"bam_input/work/{sample}/{reference}/input.bam"
output:
"metrics/{reference}/{sample}/validation_data.table"
params:
memory="10240m"
shell:
"""
set +e
java -Xmx{params.memory} -jar $HOME/software/picard/2.20.7/picard.jar ValidateSamFile I={input} O={output} MODE=SUMMARY
exitcode=$?
if [ $exitcode -ne 0 ]; then exit 1; fi
exit 0
"""
#Is this also in GATK? Then I could drop picard from this pipeline.
#This is very strict
#MATE_CIGAR_STRING_INVALID_PRESENCE
#May need case when to allow some errors.
#rule validation_pass:
# input:
# "bam_input/work/{sample}/{reference}/validation_data.table"
# output:
# "metrics/{reference}/{sample}/validation_data.table"
# shell:
# """
# set +H
# if egrep -q 'No errors found' {input[0]}; then
# cp {input[0]} {output[0]}
# else
# egrep '^ERROR' {input[0]}
# exit 1
# fi
# """
rule ready_bam:
input:
bam="bam_input/work/{sample}/{reference}/input.bam",
table="metrics/{reference}/{sample}/validation_data.table"
output:
"bam_input/final/{sample}/{sample}.{reference}.bam"
shell:
"""
rsync -v {input.bam} {output}
samtools index {output}
"""
rule write_bam_table:
input:
expand("bam_input/final/{sample}/{sample}.{reference}.bam",sample=SAMPLES,reference=config['reference']['key'])
output:
"bam.table"
params:
ref=config['reference']['key']
run:
with open(output[0],'w') as file:
for sample in SAMPLES:
file.write(f"{sample}\tbam_input/final/{sample}/{sample}.{params.ref}.bam\n")