forked from qbicsoftware-archive/rnaseq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnakefile
295 lines (252 loc) · 9.54 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
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
import os
import sys
import subprocess
import json
from os.path import join as pjoin
from os.path import exists as pexists
import glob
configfile: "config.json"
workdir: config["var"]
DATA = config['data']
RESULT = config['result']
LOGS = config['logs']
REF = config['ref']
ETC = config['etc']
SNAKEDIR = config['src']
def data(path):
return os.path.join(DATA, path)
def ref(path):
return os.path.join(REF, path)
def log(path):
return os.path.join(LOGS, path)
def result(path):
return os.path.join(RESULT, path)
def etc(path):
return os.path.join(ETC, path)
try:
with open(etc("params.json")) as f:
parameters = json.load(f)
except OSError as e:
print("Could not read parameter file: " + str(e), file=sys.stderr)
sys.exit(1)
except ValueError as e:
print("Invalid parameter file: " + str(e), file=sys.stderr)
sys.exit(1)
default_params = {
"stranded": 'no',
"overlap_mode": 'union',
"normalize_counts": "deseq2",
"gff_attribute": 'gene_id',
"feature_type": 'exon',
}
default_params.update(parameters)
parameters = default_params
for key in ['gtf', 'stranded', 'overlap_mode', 'indexed_genome',
'gff_attribute', 'feature_type', 'normalize_counts']:
if key not in parameters:
print("Missing parameter %s in etc/params.json" % key, file=sys.stderr)
exit(1)
parameters['indexed_genome'] = ref(parameters['indexed_genome'])
parameters['gtf'] = ref(parameters['gtf'])
indexed_genome = parameters["indexed_genome"]
if not os.path.exists(indexed_genome + '.fa'):
raise ValueError("Could not find indexed genome file %s" % indexed_genome)
INPUT_FILES = []
for name in os.listdir(DATA):
if name.lower().endswith('.sha256sum'):
continue
if name.lower().endswith('.fastq'):
if not name.endswith('.fastq'):
print("Extension fastq is case sensitive.", file=sys.stderr)
exit(1)
INPUT_FILES.append(os.path.basename(name)[:-6])
elif name.lower().endswith('.fastq.gz'):
if not name.endswith('.fastq.gz'):
print("Extension fastq is case sensitive.", file=sys.stderr)
exit(1)
INPUT_FILES.append(os.path.basename(name)[:-len('.fastq.gz')])
else:
print("Unknown data file: %s" % name, file=sys.stderr)
exit(1)
if len(set(INPUT_FILES)) != len(INPUT_FILES):
print("Some input file names are not unique")
exit(1)
OUTPUT_FILES = []
OUTPUT_FILES.extend(expand("Summary/NumReads/Original/{name}.txt", name=INPUT_FILES, result=RESULT))
OUTPUT_FILES.extend(expand("Summary/NumReads/PreFilter/{name}.txt", name=INPUT_FILES, result=RESULT))
OUTPUT_FILES.extend(expand("{result}/FastQC_{name}.zip", name=INPUT_FILES, result=RESULT))
OUTPUT_FILES.extend(expand("{result}/FastQCcut_{name}.zip", name=INPUT_FILES, result=RESULT))
OUTPUT_FILES.extend(expand("Summary/NumReads/CutAdaptMerge/{name}.txt", name=INPUT_FILES, result=RESULT))
OUTPUT_FILES.extend(expand("{result}/HTSeqCounts_{name}.txt", name=INPUT_FILES, result=RESULT))
OUTPUT_FILES.extend(expand("TopHat2/{name}/accepted_hits.bai", name=INPUT_FILES, result=RESULT))
OUTPUT_FILES.extend(expand("Summary/MappingStats/{name}.txt", name=INPUT_FILES, result=RESULT))
#OUTPUT_FILES.append("checksums.ok")
OUTPUT_FILES.append(result('all_counts.csv'))
rule all:
input: OUTPUT_FILES
rule checksums:
output: "checksums.ok"
run:
out = os.path.abspath(str(output))
if glob.glob(data("*.sha256sum")):
shell("cd %s; "
"sha256sum -c *.sha256sum && "
"touch %s" % (data('.'), out))
else:
shell("touch %s" % out)
rule LinkUncompressed:
input: data("{name}.fastq")
output: "fastq/{name}.fastq"
shell: "ln -s {input} {output}"
rule Uncompress:
input: data("{name}.fastq.gz")
output: "fastq/{name}.fastq"
shell: "zcat {input} > {output}"
rule PreFilterReads:
input: "fastq/{name}.fastq"
output: "PreFilterReads/{name}.fastq"
run:
with open(str(input)) as infile, open(str(output), 'w') as outfile:
num_ok = 0
num_filtered = 0
line = infile.readline()
while line:
assert line.startswith('@')
body = ''.join(infile.readline() for _ in range(3))
if all(':Y:' not in part for part in line.split(' ')[1:]):
num_ok += 1
outfile.write(line)
outfile.write(body)
else:
num_filtered += 1
line = infile.readline()
num_all = num_ok + num_filtered
if num_filtered / num_all > .1:
raise ValueError("More than 10% of reads were filtered in "
"PreFilterReads. This probably indicates a bug.")
rule FastQC:
input: "PreFilterReads/{name}.fastq"
output: "FastQC/{name}"
shell: 'mkdir -p {output} && (fastqc {input} -o {output} --extract || (rm -rf {output} && exit 1))'
rule FastQCCpToResult:
input: "FastQC/{name}"
output: result("FastQC_{name}.zip")
shell: "cp {input}/{wildcards.name}_fastqc.zip {output}"
rule FastQCcut:
input: "CutAdaptMerge/{name}.fastq"
output: "FastQCcut/{name}"
shell: 'mkdir -p {output} && (fastqc {input} -o {output} --extract || (rm -rf {output} && exit 1))'
rule FastQCcutCpToResult:
input: "FastQCcut/{name}"
output: result("FastQCcut_{name}.zip")
shell: "cp {input}/{wildcards.name}_fastqc.zip {output}"
rule Overrepresented:
input: "FastQC/{name}"
output: "Overrepresented/{name}.txt"
run:
f = open(str(input) + "/" + wildcards['name'] + "_fastqc/fastqc_data.txt")
out = open(str(output), "w")
sw = False
for line in f:
if line.startswith(">>Overrepresented"):
sw = True
if not line.startswith(">>END_MODULE") and sw:
out.write(line)
else:
sw = False
f.close()
out.close()
rule OverrepTxtFasta:
input: "Overrepresented/{name}.txt"
output: "Overrepresented/{name}.fasta"
run:
f = open(str(input))
out = open(str(output), "w")
for line in f:
if not (line.startswith("#") or line.startswith(">>")):
tokens1 = line.split("\t")
print(tokens1)
fseq = tokens1[0]
fheader = '>' + tokens1[3]
out.write(fheader)#+'\n')#it just happens that is the last column
out.write(fseq+'\n')
f.close()
out.close()
rule MergeAdapters:
input: expand("Overrepresented/{name}.fasta", name=INPUT_FILES)
output: "MergeAdapters/merged.fasta"
shell: "cat {input} > {output}"
rule subset_Adapters:
input: "MergeAdapters/merged.fasta",
output: "MergeAdapters/merged.subset.fasta"
shell:
"""
awk '/^>/ {{P=index($0,"No Hit")==0}} {{if(P) print}} ' {input} > {output}
"""
rule CutAdapt:
input: "MergeAdapters/merged.subset.fasta", "PreFilterReads/{name}.fastq"
output: "CutAdaptMerge/{name}.fastq"
run:
with open(str(input[0])) as f:
skip = not bool(f.read(1))
if skip:
os.symlink(os.path.abspath(str(input[1])), str(output))
else:
shell('cutadapt --discard-trimmed -a file:{input[0]} -o {output} {input[1]}')
rule TopHat2:
input: "CutAdaptMerge/{name}.fastq"
output: "TopHat2/{name}"
run:
gtf = parameters['gtf']
genome = parameters['indexed_genome']
shell('tophat --no-coverage-search -o {output} -p 2 -G %s %s {input}'
% (gtf, genome))
rule HTSeqCounts:
input: "TopHat2/{name}"
output: result("HTSeqCounts_{name}.txt")
run:
sam_command = "samtools view {input}/accepted_hits.bam"
htseq = ("htseq-count -i {gff_attribute} -t {feature_type} "
"-m {overlap_mode} -s {stranded} - {gtf}").format(**parameters)
shell("%s | %s > {output}" % (sam_command, htseq))
rule CombineCounts:
input:
expand("{result}/HTSeqCounts_{name}.txt",
name=INPUT_FILES, result=RESULT)
output: result("all_counts.csv")
run:
import pandas as pd
import re
pattern = "HTSeqCounts_([0-9a-zA-Z_\- ]*).txt"
names = [re.search(pattern, str(name)).groups()[0] for name in input]
data = {}
for name, file in zip(names, input):
file = str(file)
data[name] = pd.Series.from_csv(file, sep='\t')
df = pd.DataFrame(data)
df.index.name = parameters['gff_attribute']
df.to_csv(str(output))
rule IndexBAM:
input: "TopHat2/{name}"
output: "TopHat2/{name}/accepted_hits.bai"
shell: "samtools index {input}/accepted_hits.bam && mv -f {input}/accepted_hits.bam.bai {output}"
rule CpAlignSummary:
input: "TopHat2/{name}"
output: "Summary/MappingStats/{name}.txt"
shell: "cp {input}/align_summary.txt {output}"
rule PerBaseCoverage:
input: "TopHat2/{name}"
output: "Statistics/{name}_PerBaseCoverage.txt"
shell: "samtools depth {input}/accepted_hits.bam > {output}"
rule Numreads:
input: "PreFilterReads/{name}.fastq"
output: "Summary/NumReads/PreFilter/{name}.txt"
shell: '''dc -e "$(wc -l {input} | cut -f1 -d' ') 4 / p" > {output}'''
rule NumreadsCut:
input: "CutAdaptMerge/{name}.fastq"
output: "Summary/NumReads/CutAdaptMerge/{name}.txt"
shell: '''dc -e "$(wc -l {input} | cut -f1 -d' ') 4 / p" > {output}'''
rule NumreadsOrig:
input: "fastq/{name}.fastq"
output: "Summary/NumReads/Original/{name}.txt"
shell: '''dc -e "$(wc -l {input} | cut -f1 -d' ') 4 / p" > {output}'''