-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrnaseq.smk
173 lines (153 loc) · 4.68 KB
/
rnaseq.smk
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
from os import path
# Load paths for input and output from config
configfile: "config/config.yaml"
dataset = config["dataset"]
fq_dir = config["fq_dir"]
proj_dir = path.join(config["out_dir"], dataset)
ref = config["ref"]
threads = config["threads"]
out_data_dir = path.join(proj_dir, "data")
qc_dir = path.join(out_data_dir, "qc_fq")
reports_dir = path.join(proj_dir, "reports")
results_dir = path.join(proj_dir, "results")
samples, = glob_wildcards(fq_dir + "/{sample}.fastq.gz")
wildcard_constraints:
sample = "[^\.\/]+"
def get_pe_fq(wc):
return [path.join(fq_dir, wc.sample + end + ".fastq.gz") for end in ["_R1", "_R2"]]
def get_se_fq(wc):
return path.join(fq_dir, wc.sample + ".fastq.gz")
rule all:
input:
count = expand(
results_dir + "/count/{dataset}.count", dataset=dataset)
rule build_idx:
input:
ref = ref
output:
fa_idx = ref + ".fai",
bwa_idx = ref + ".bwt"
conda:
"config/conda.rnaseq.yaml"
shell:
"""
samtools faidx {input.ref}
bwa index {input.ref}
"""
# If it is not long read seq data
if not config["is_long_read"]:
rule fastp:
input:
get_se_fq
output:
reads = qc_dir + "/{sample}.qc.fq.gz",
html = reports_dir + "/fastp/{sample}.qc.report.html"
threads: 8
conda:
"config/conda.rnaseq.yaml"
shell:
"""
fastp -i {input} -o {output.reads} -5 20 -3 20 -l 30 -n 3 -h {output.html} -w {threads}
"""
rule bwa:
input:
reads = rules.fastp.output.reads,
ref = ref,
ref_idx = rules.build_idx.output.bwa_idx
output:
out_data_dir + "/bam/{sample}.bam"
conda:
"config/conda.rnaseq.yaml"
benchmark:
reports_dir + "/benchmarks/{sample}.bwa.txt"
log:
reports_dir + "/bwa/{sample}.log"
threads: threads
shell:
"""
bwa mem -t {threads} {input.ref} {input.reads} |\
samtools view -@ {threads} -Shb - 2>> {log} |\
samtools sort -@ {threads} -m 20G - -o {output} >> {log} 2>&1
"""
rule bam_filter:
input:
rules.bwa.output
output:
out_data_dir + "/bam/{sample}.filtered.bam"
conda:
"config/conda.rnaseq.yaml"
log:
reports_dir + "/samtools/{sample}.log"
threads: threads
shell:
"""
samtools view -@ {threads} -Shb -F 4 -q 10 {input} 2>> {log} |\
samtools sort -@ {threads} -m 20G - -o {output} >> {log} 2>&1
samtools index {output}
"""
# If long read seq data
else:
rule porechop:
input:
get_se_fq
output:
reads = qc_dir + "/{sample}.qc.fq.gz",
#html = reports_dir + "/fastp/{sample}.qc.report.html"
threads: 8
conda:
"config/conda.rnaseq.yaml"
shell:
"""
porechop -i {input} -o {output.reads}
"""
rule graphmap:
input:
reads = rules.porechop.output.reads,
ref = ref
output:
out_data_dir + "/bam/{sample}.bam"
conda:
"config/conda.rnaseq.yaml"
benchmark:
reports_dir + "/benchmarks/{sample}.graphmap.txt"
log:
reports_dir + "/graphmap/{sample}.log"
threads: threads
shell:
"""
graphmap align -r {input.ref} -d {input.reads} -t {threads} |\
samtools view -@ {threads} -Shb - 2>> {log} |\
samtools sort -@ {threads} -m 20G - -o {output} >> {log} 2>&1
samtools index {output}
"""
rule samcount:
input:
bam = rules.bam_filter.output if \
not config["is_long_read"] else \
rules.graphmap.output
output:
results_dir + "/count/{sample}.samcount"
conda:
"config/conda.rnaseq.yaml"
threads: threads
shell:
"""
samtools idxstats {input.bam} > {output}
"""
rule cat_count:
input:
expand(
results_dir + "/count/{sample}.samcount", sample=samples)
output:
results_dir + "/count/{dataset}.count"
conda:
"config/conda.rnaseq.yaml"
params:
cols = ",".join(["1"] + [str(3 * i)
for i in range(1, len(samples) + 1)])
threads: threads
shell:
"""
cat <(printf "gene\t";sed 's/[^ ]\+\/\|\.samcount//g;s/ \+/\t/g' <(echo {input})) \
<(csvtk join -TtHf 1 {input}|csvtk cut -TtHf {params.cols}) > {output}
"""