-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_regulatory_regions_bed.py
executable file
·373 lines (333 loc) · 12.7 KB
/
create_regulatory_regions_bed.py
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#!/usr/bin/env python3
import argparse
import os
import sqlite3
import sys
from interval import Interval
from transcript import Transcript
# Attention! 08/06/2011: Different regulatory region delineation for non-coding sequences ...
LOAD_GENE_IDS_STATEMENT = r"""SELECT geneID FROM genes"""
GET_CHROMOSOME_LENGTHS_STATEMENT = r"""SELECT name, length FROM chromosomes"""
class Modes:
FULL_TRANSCRIPT = 0
ALL_INTRONS = 1
UTR5_INTRON1 = 2
NO_TRANSCRIPT = 3
UTR5 = 4
CDS_START_NO_TRANSCRIPT = 5
def load_chromosome_lengths(connection):
chromosome2length = {}
cursor = connection.cursor()
cursor.execute(GET_CHROMOSOME_LENGTHS_STATEMENT)
for name, length in cursor:
chromosome2length[name] = length
cursor.close()
return chromosome2length
def chromosome_iterator(filename, exclude_alt=True, exclude_random=True):
with open(filename, "r") as handle:
for line in handle:
if line.startswith("#"):
# Skip comment lines.
continue
line = line.rstrip()
if line:
# Get first column as chromosome name.
chrom = line.split("\t", 1)[0]
# Skip alternative chromosomes, if requested.
if exclude_alt and chrom.endswith("_alt"):
continue
# Skip random chromosomes, if requested.
if exclude_random and chrom.endswith("random"):
continue
yield chrom
def gene_ids_iterator(connection):
cursor = connection.cursor()
cursor.execute(LOAD_GENE_IDS_STATEMENT)
for (gene_id,) in cursor:
yield gene_id
cursor.close()
def find_unique_transcript(transcripts, chromosomes):
transcripts = list(filter(lambda tx: tx.chromosome in chromosomes, transcripts))
return transcripts[0] if len(transcripts) == 1 else None
def regulatory_regions_iterator(
connection,
chromosomes,
chromosome2length,
upstream_extension,
downstream_extension,
intronic_extension,
mode,
):
gene_ids = set(gene_ids_iterator(connection))
gene_name2locations = {}
for gene_id in gene_ids:
# If gene_id is "NM_*", do not restrict regulatory locations by nearby "NR_*" but only by other "NM_*".
is_NM = gene_id.startswith("NM_")
tx = find_unique_transcript(
Transcript.load_by_gene_id(connection, gene_id), chromosomes
)
if not tx:
print(f"Skipped {gene_id:s}: no unique transcript.", file=sys.stderr)
continue
if mode == Modes.FULL_TRANSCRIPT:
# Intragenic location = full transcript ...
intragenic_location = tx.transcript()
elif mode == Modes.ALL_INTRONS:
# Intragenic location = only introns ...
introns = tx.introns_in_cds()
if len(introns) > 0:
intragenic_location = introns.location(0)
for idx in range(1, len(introns)):
intragenic_location += introns.location(idx)
else:
intragenic_location = tx.empty_interval()
elif mode == Modes.UTR5_INTRON1:
# Intragenic location = 5'UTR and 1st intron in CDS ...
intragenic_location = tx.five_prime_utr()
introns = tx.introns_in_cds()
if len(introns) > 0:
if tx.on_positive_strand():
intragenic_location += introns.location(0)
else:
intragenic_location += introns.location(len(introns) - 1)
elif mode == Modes.UTR5:
# Intragenic location = 5'UTR ...
intragenic_location = tx.five_prime_utr()
elif mode == Modes.NO_TRANSCRIPT:
# No transcript ...
intragenic_location = tx.empty_interval()
elif mode == Modes.CDS_START_NO_TRANSCRIPT:
# CDS start and no transcript ...
intragenic_location = tx.empty_interval()
if not intragenic_location.isempty() and intronic_extension > 0:
if tx.on_positive_strand():
filter_interval = Interval(
tx.tss_as_bp_location(),
tx.tss_as_bp_location() + intronic_extension,
)
intragenic_location = intragenic_location.interval_limit(
filter_interval
)
else:
filter_interval = Interval(
tx.tss_as_bp_location() - intronic_extension + 1,
tx.tss_as_bp_location() + 1,
)
intragenic_location = intragenic_location.interval_limit(
filter_interval
)
# Remove coding sequences from all transcripts, including the current transcript
# itself. The non-coding transcripts are not removed ...
if not intragenic_location.isempty():
for rtx in Transcript.load_by_location(
connection,
intragenic_location.chromosome,
intragenic_location.span(),
only_NMs=is_NM,
):
intragenic_location -= rtx.coding_exons()
regulatory_location = intragenic_location
# Upstream region ...
if upstream_extension > 0:
upstream_location = (
tx.tss_shifted_1bp_upstream().extend_upstream(
upstream_extension, chromosome2length
)
if mode != Modes.CDS_START_NO_TRANSCRIPT
else tx.cds_start_shifted_1bp_upstream().extend_upstream(
upstream_extension, chromosome2length
)
)
# Changed on 02/09/2011: check if upstream location is not empty ...
if not upstream_location.isempty():
for rtx in Transcript.load_by_location(
connection,
upstream_location.chromosome,
upstream_location.span(),
only_NMs=is_NM,
):
upstream_location -= (
rtx.coding_exons()
if tx.tss_as_bp_location() in rtx.transcript()
else rtx.transcript()
)
upstream_location = upstream_location.filter(
tx.tss_shifted_1bp_upstream_as_bp_location()
if mode != Modes.CDS_START_NO_TRANSCRIPT
else tx.cds_start_shifted_1bp_upstream_as_bp_location()
)
regulatory_location += upstream_location
# Downstream region ...
if downstream_extension > 0:
downstream_location = (
tx.tes_shifted_1bp_downstream().extend_downstream(
downstream_extension, chromosome2length
)
if mode != Modes.CDS_START_NO_TRANSCRIPT
else tx.cds_start_location().extend_downstream(
downstream_extension, chromosome2length
)
)
# Changed on 02/09/2011: check if downstream location is not empty ...
if not downstream_location.isempty():
for rtx in Transcript.load_by_location(
connection,
downstream_location.chromosome,
downstream_location.span(),
only_NMs=is_NM,
):
downstream_location -= (
rtx.coding_exons()
if tx.tes_as_bp_location() in rtx.transcript()
else rtx.transcript()
)
downstream_location = downstream_location.filter(
tx.tes_shifted_1bp_downstream_as_bp_location()
)
downstream_location = downstream_location.filter(
tx.tes_shifted_1bp_downstream_as_bp_location()
if mode != Modes.CDS_START_NO_TRANSCRIPT
else tx.cds_start_as_bp_location()
)
regulatory_location += downstream_location
gene_name2locations.setdefault(tx.gene_name, []).append(regulatory_location)
for gene_name in sorted(gene_name2locations.keys()):
locations = gene_name2locations[gene_name]
if (
len({loc.chromosome for loc in locations}) != 1
or len({loc.on_positive_strand for loc in locations}) != 1
):
print(
f"Skipped {gene_name:s}: cannot combine regulatory regions.",
file=sys.stderr,
)
continue
combined_location = None
for location in locations:
if combined_location is None:
combined_location = location
else:
combined_location += location
if combined_location.isempty():
print(
f"Skipped {gene_name:s}: no regulatory region remains.",
file=sys.stderr,
)
else:
for idx, interval in enumerate(combined_location):
yield (
combined_location.chromosome,
interval.start,
interval.end,
f"{gene_name:s}#{idx + 1:d}",
"+" if combined_location.on_positive_strand else "-",
)
def main():
parser = argparse.ArgumentParser(
description="""
Create gene regulatory regions BED file.
""",
)
parser.add_argument(
"--db",
dest="database_filename",
action="store",
type=str,
required=True,
help='Gene SQLite3 database filename, created with "create_genes_database.sh".',
)
parser.add_argument(
"--chrom",
dest="chromosome_sizes_filename",
action="store",
type=str,
required=True,
help="Chromosome sizes filename with list of chromosome names in first column (UCSC chrom sizes, FAIDX index or plain list).",
)
parser.add_argument(
"--up",
dest="upstream_extend",
action="store",
type=int,
required=True,
help="Extend regulatory region x bp upstream of TSS.",
)
parser.add_argument(
"--down",
dest="downstream_extend",
action="store",
type=int,
required=True,
help="Extend regulatory region x bp downstream of TSS.",
)
parser.add_argument(
"--intronic",
dest="intronic_extend",
action="store",
type=int,
required=True,
help="Extend regulatory region x bp from introns.",
)
parser.add_argument(
"--mode",
dest="mode",
action="store",
type=str,
required=True,
choices={"FullTx", "AllIntrons", "5utrIntron1", "NoTx", "5utr", "CDSStartNoTx"},
help="""
Extend regulatory regions (determined by up, down and intronic arguments) for each gene by including:
- FullTx: Full transcript (5UTR, all exons and all introns).
- AllIntrons: All introns (no exons).
- 5utrIntron1: 5'UTR and 1st intron in CDS
- NoTx: No transcript.
- 5utr: 5'UTR.
- CDSStartNoTx: CDS start, no transcript.
""",
)
args = parser.parse_args()
if not os.path.isfile(args.database_filename):
print(
"""Database file "{args.database_filename}" doesn't exist.""",
file=sys.stderr,
)
sys.exit(1)
if not os.path.isfile(args.chromosome_sizes_filename):
print(
"""Chromosome sizes file "{args.chromosome_sizes_filename}" doesn't exist.""",
file=sys.stderr,
)
sys.exit(1)
if args.mode == "FullTx":
mode = Modes.FULL_TRANSCRIPT
elif args.mode == "AllIntrons":
mode = Modes.ALL_INTRONS
elif args.mode == "5utrIntron1":
mode = Modes.UTR5_INTRON1
elif args.mode == "NoTx":
mode = Modes.NO_TRANSCRIPT
elif args.mode == "5utr":
mode = Modes.UTR5
elif args.mode == "CDSStartNoTx":
mode = Modes.CDS_START_NO_TRANSCRIPT
chromosomes = set(
chromosome_iterator(
filename=args.chromosome_sizes_filename,
exclude_alt=True,
exclude_random=True,
)
)
with sqlite3.connect(args.database_filename) as connection:
chromosome2length = load_chromosome_lengths(connection)
for columns in regulatory_regions_iterator(
connection,
chromosomes,
chromosome2length,
args.upstream_extend,
args.downstream_extend,
args.intronic_extend,
mode,
):
print("{0:s}\t{1:d}\t{2:d}\t{3:s}\t0\t{4:s}".format(*columns))
if __name__ == "__main__":
main()