-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnakefile
215 lines (180 loc) · 7.29 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
##############################################################################
#
# TANDEM PAS:
# "Small set of rules to produce an approriate set of tandem poly(A) sites
# from the full annotation of sites from PolyASite and a provided gtf file"
#
# AUTHOR: Ralf_Schmidt
# MODIFICATIONS: Maciej_Bak, C.J. Herrmann, M. Zavolan
# AFFILIATION: University_of_Basel
# AFFILIATION: Swiss_Institute_of_Bioinformatics
# CONTACT: [email protected]
# CREATED: 28-03-2020
# MODIFIED: 21-09-2021
# LICENSE: Apache_2.0
#
##############################################################################
import os
import traceback
from snakemake.utils import makedirs
################################################################################
### Local rules
################################################################################
localrules: all, create_log_dir, select_terminal_exon_pas
logs = config["logdir"]
cluster_logs = os.path.join(logs, "cluster_logs")
##############################################################################
### Target rule with final output of the pipeline
##############################################################################
rule all:
"""
Gathering all output
"""
input:
tandem_pas_TE = expand(os.path.join(config["outdir"],
str(config["atlas_version"]) + ".tandem_pas.terminal_exons.{strandedness}.bed"),
strandedness = config["strandedness"]),
rule create_log_dir:
## LOCAL ##
''' This step creates the log directory, if necessary.
This is required when jobs are submitted to cluster and the
job output should be written to these files.
'''
output:
TMP_out = temp(os.path.join(logs,"created_dirs.tmp"))
run:
makedirs( cluster_logs )
shell("touch {output.TMP_out}")
##############################################################################
### Select tandem poly(A) sites
##############################################################################
rule select_tandem_pas:
"""
Selecting only pas which may be categorized as proximal/distal.
"""
input:
created_dirs = os.path.join(logs,"created_dirs.tmp"),
BED_pas_atlas = config['polyasites'],
GTF_annotation = config['gtf'],
SCRIPT_ = os.path.join(
config['scriptsdir'],
"mz-select-pas-subset.pl")
output:
TSV_tandem_pas = os.path.join(config["outdir"],
str(config["atlas_version"]) + ".tandem_pas.tsv")
params:
STR_biotype_key = lambda wildcards:
"--type_id=" + config['biotype_key'] if 'biotype_key' in config else "",
STR_biotype_values = lambda wildcards:
" ".join(["--type=" + i for i in config['biotype_values']]) if "biotype_values" in config else "",
FLT_min_support = config['min_support'],
INT_three_prime_offset = config["three_prime_offset"],
INT_locus_extension = config["locus_extension"],
cluster_log = os.path.join(cluster_logs,
str(config["atlas_version"]) + ".select_tandem_pas.log"
)
log:
os.path.join( logs,
str(config["atlas_version"]) + ".select_tandem_pas.log"
)
singularity:
"docker://perl:5.26.2"
conda:
"env/perl.yaml"
shell:
"""
perl {input.SCRIPT_} \
--minLevel={params.FLT_min_support} \
--annotation={input.GTF_annotation} \
{params.STR_biotype_key} \
{params.STR_biotype_values} \
--offset={params.INT_three_prime_offset} \
--locusExtension={params.INT_locus_extension} \
--nonredundant \
{input.BED_pas_atlas} \
1> {output.TSV_tandem_pas} \
2> {log}
"""
##############################################################################
### select tandem poly(A) sites of terminal exons
##############################################################################
rule select_terminal_exon_pas:
"""
Filter pas and select only those on terminal exons
"""
input:
TSV_tandem_pas = os.path.join(config["outdir"],
str(config["atlas_version"]) + ".tandem_pas.tsv")
output:
TSV_tandem_pas_terminal_exons = os.path.join(config["outdir"],
str(config["atlas_version"]) + ".tandem_pas.terminal_exons.tsv")
params:
cluster_log = os.path.join(cluster_logs,
str(config["atlas_version"]) + ".select_terminal_exon_pas.log"
)
log:
os.path.join( logs,
str(config["atlas_version"]) + ".select_terminal_exon_pas.log"
)
run:
with open(input.TSV_tandem_pas, "r") as ifile, open(output.TSV_tandem_pas_terminal_exons, "w") as ofile, open(log[0], "w") as logfile:
try:
for line in ifile:
# check if the exon that contains a given pas is
# indeed the last exon of a transcript
F = line.rstrip().split("\t")
ex_id, ex_nr, total_exons, ex_start, ex_end = F[8].split(":")
if int(total_exons) == int(ex_nr):
ofile.write(line)
except Exception:
traceback.print_exc(file = logfile)
raise Exception(
"Workflow error at rule: select_terminal_exon_pas"
)
##############################################################################
### Filter pas based on the annotation
##############################################################################
rule filter_on_ambiguous_annotation:
"""
Intersect tandem poly(A) sites of terminal exons with the annotation.
Only retain exons that can be associated to a gene unambiguously.
Create separate files to be used with stranded and unstranded input data
"""
input:
TSV_tandem_pas_terminal_exons = os.path.join(config["outdir"],
str(config["atlas_version"]) + ".tandem_pas.terminal_exons.tsv"),
GTF_annotation = config['gtf'],
SCRIPT_ = os.path.join(
config["scriptsdir"],
"mz-remove-overlapping-genes_{strandedness}.pl")
output:
BED_tandem_pas_terminal_exons_clean = os.path.join(config["outdir"],
str(config["atlas_version"]) + ".tandem_pas.terminal_exons.{strandedness}.bed")
params:
INT_downstream_extend = config['downstream_extend'],
cluster_log = os.path.join(cluster_logs,
str(config["atlas_version"]) + "filter_{strandedness}.log"
)
log:
os.path.join( logs,
str(config["atlas_version"]) + ".filter_{strandedness}.log")
singularity:
"docker://perl:5.26.2"
conda:
"env/perl.yaml"
shell:
"""
perl {input.SCRIPT_} \
--tandemPAS={input.TSV_tandem_pas_terminal_exons} \
--downstream_extend={params.INT_downstream_extend} \
{input.GTF_annotation} \
1> {output.BED_tandem_pas_terminal_exons_clean} \
2> {log}
"""
#-------------------------------------------------------------------------------
# How did it go?
#-------------------------------------------------------------------------------
onsuccess:
print("Workflow finished, no error")
onerror:
print("An error occurred, check log at %s." % {log})