-
Notifications
You must be signed in to change notification settings - Fork 7
/
simple_feature_utils.py
340 lines (300 loc) · 10.9 KB
/
simple_feature_utils.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
# See the NOTICE file distributed with this work for additional information #pylint: disable=missing-module-docstring
# regarding copyright ownership.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import json
import logging
import multiprocessing
import os
import pathlib
import re
import subprocess
import tempfile
import typing
import utils
logger = logging.getLogger(__name__)
with open(os.environ["ENSCODE"] + "/ensembl-anno/config.json", "r") as f:
config = json.load(f)
def run_eponine_regions( # pylint: disable=too-many-locals
genome_file: typing.Union[pathlib.Path, str],
java_path: str,
eponine_path: str,
main_output_dir: str,
num_threads: int,
):
"""
Run Eponine on genomic slices
Args:
genome_file : pathlib.Path
java_path : str
eponine_path : str
main_output_dir : pathlib.Path
num_threads: int
Return:
gtfs with the simple feature evidence for each genome slice
"""
if not java_path:
java_path = config["java"]["path"]
if not eponine_path:
eponine_path = config["eponine"]["software"]
utils.check_file(pathlib.Path(eponine_path))
utils.check_exe(java_path)
eponine_output_dir = pathlib.Path(utils.create_dir(main_output_dir, "eponine_output"))
logger.info("Skip analysis if the gtf file already exists")
output_file = eponine_output_dir / "annotation.gtf"
if output_file.is_file():
transcript_count = utils.check_gtf_content(output_file, "simple_feature")
if transcript_count > 0:
logger.info("Eponine gtf file exists")
return 0
logger.info("Creating list of genomic slices")
seq_region_lengths = utils.get_seq_region_lengths(genome_file, 5000)
logger.info("Creating list of genomic slices %s", seq_region_lengths)
slice_ids = utils.create_slice_ids(
seq_region_lengths, slice_size=1000000, overlap=0, min_length=5000
)
threshold = config["eponine"]["threshold"]
generic_eponine_cmd = [
java_path,
"-jar",
eponine_path,
"-threshold",
threshold,
"-seq",
]
logger.info("Running Eponine")
pool = multiprocessing.Pool(int(num_threads))
# tasks = []
for slice_id in slice_ids:
pool.apply_async(
multiprocess_eponine,
args=(
generic_eponine_cmd,
slice_id,
genome_file,
eponine_output_dir,
),
)
pool.close()
pool.join()
utils.slice_output_to_gtf(
str(eponine_output_dir), ".epo.gtf", 1, "feature_id", "eponine"
)
for gtf_file in pathlib.Path(eponine_output_dir).glob("*.epo.gtf"):
gtf_file.unlink()
return 0
def multiprocess_eponine( # pylint: disable=too-many-locals
generic_eponine_cmd: list,
slice_id: str,
genome_file: pathlib.Path,
eponine_output_dir: pathlib.Path,
):
"""
Run Eponine on multiprocess on genomic slices
Args:
generic_eponine_cmd:list
slice_id: list
genome_file : pathlib.Path
eponine_output_dir : pathlib.Path
"""
region_name = slice_id[0]
start = slice_id[1]
end = slice_id[2]
logger.info(
"Processing slice to find transcription start sites with Eponine: %s:%s:%s",
region_name,
start,
end,
)
seq = utils.get_sequence(
region_name, start, end, 1, str(genome_file), str(eponine_output_dir)
)
slice_file_name = f"{region_name}.rs{start}.re{end}"
with tempfile.TemporaryDirectory(dir=eponine_output_dir) as tmpdirname:
region_fasta_file_path = eponine_output_dir / tmpdirname / f"{slice_file_name}.fa"
with open(region_fasta_file_path, "w+") as region_fasta_out:
region_fasta_out.write(f">{region_name}\n{seq}\n")
region_results_file_path = eponine_output_dir / f"{slice_file_name}.epo.gtf"
eponine_output_file_path = f"{region_fasta_file_path}.epo"
eponine_cmd = generic_eponine_cmd.copy()
eponine_cmd.append(str(region_fasta_file_path))
with open(eponine_output_file_path, "w+") as eponine_out:
subprocess.run(eponine_cmd, stdout=eponine_out, check=True)
create_eponine_gtf(
eponine_output_file_path, region_results_file_path, region_name
)
def create_eponine_gtf(
eponine_output_file_path: pathlib.Path,
region_results_file_path: pathlib.Path,
region_name: str,
):
"""
Read the fasta file and save the content in gtf format
All the genomic slices are collected in a single gtf output
Args:
eponine_output_file_path : pathlib.Path
region_results_file_path : pathlib.Path
region_name :str
"""
with open(eponine_output_file_path, "r") as eponine_in, open(
region_results_file_path, "w+"
) as eponine_out:
feature_count = 1
for line in eponine_in:
result_match = re.search(r"^" + region_name, line)
if result_match:
results = line.split()
start = int(results[3])
end = int(results[4])
score = float(results[5])
strand = results[6]
# There's a one base offset on the reverse strand
if strand == "-":
start -= 1
end -= 1
gtf_line = (
f"{region_name}\tEponine\tsimple_feature\t"
f"{start}\t{end}\t.\t{strand}\t.\t"
f'feature_id "{feature_count}"; score "{score}";\n'
)
eponine_out.write(gtf_line)
feature_count += 1
def run_cpg_regions(
genome_file: typing.Union[pathlib.Path, str],
cpg_path: str,
main_output_dir: str,
num_threads: int,
):
"""
Run CpG islands on genomic slices
Args:
genome_file : pathlib.Path
cpg_path : str
main_output_dir : pathlib.Path
num_threads: int
Return:
gtfs with the cpg evidence for each genome slice
"""
if not cpg_path:
cpg_path = config["cpg"]["software"]
utils.check_exe(cpg_path)
cpg_output_dir = pathlib.Path(utils.create_dir(main_output_dir, "cpg_output"))
output_file = cpg_output_dir / "annotation.gtf"
if output_file.is_file():
transcript_count = utils.check_gtf_content(output_file, "simple_feature")
if transcript_count > 0:
logger.info("Cpg gtf file exists")
return 0
logger.info("Creating list of genomic slices")
seq_region_lengths = utils.get_seq_region_lengths(genome_file, 5000)
slice_ids = utils.create_slice_ids(
seq_region_lengths, slice_size=1000000, overlap=0, min_length=5000
)
logger.info("Running CpG")
pool = multiprocessing.Pool(int(num_threads))
# tasks = []
for slice_id in slice_ids:
pool.apply_async(
multiprocess_cpg,
args=(
cpg_path,
slice_id,
genome_file,
cpg_output_dir,
),
)
pool.close()
pool.join()
utils.slice_output_to_gtf(str(cpg_output_dir), ".cpg.gtf", 1, "feature_id", "cpg")
for gtf_file in pathlib.Path(cpg_output_dir).glob("*.cpg.gtf"):
gtf_file.unlink()
return 0
def multiprocess_cpg( # pylint: disable=too-many-locals
cpg_path: str, slice_id: str, genome_file: pathlib.Path, cpg_output_dir: pathlib.Path
):
"""
Annotation of CpG islands on multiprocess on genomic slices
Args:
cpg_path:str
slice_id:str
genome_file : pathlib.Path
cpg_output_dir : pathlib.Path
"""
region_name = slice_id[0]
start = slice_id[1]
end = slice_id[2]
logger.info(
"Processing slice to find CpG islands with cpg_lh: %s:%s:%s",
region_name,
start,
end,
)
seq = utils.get_sequence(
region_name, start, end, 1, str(genome_file), str(cpg_output_dir)
)
slice_file_name = f"{region_name}.rs{start}.re{end}"
with tempfile.TemporaryDirectory(dir=cpg_output_dir) as tmpdirname:
region_fasta_file_path = cpg_output_dir / tmpdirname / f"{slice_file_name}.fa"
with open(region_fasta_file_path, "w+") as region_fasta_out:
region_fasta_out.write(f">{region_name}\n{seq}\n")
region_results_file_path = cpg_output_dir / f"{slice_file_name}.cpg.gtf"
cpg_output_file_path = f"{region_fasta_file_path}.cpg"
cpg_cmd = [cpg_path, str(region_fasta_file_path)]
with open(cpg_output_file_path, "w+") as cpg_out:
subprocess.run(cpg_cmd, stdout=cpg_out, check=True)
create_cpg_gtf(cpg_output_file_path, region_results_file_path, region_name)
def create_cpg_gtf( # pylint: disable=too-many-locals
cpg_output_file_path: pathlib.Path,
region_results_file_path: pathlib.Path,
region_name: str,
):
"""
Read the fasta file and save the content in gtf format
All the genomic slices are collected in a single gtf output
Args:
cpg_output_file_path : pathlib.Path
region_results_file_path : pathlib.Path
region_name :str
"""
cpg_min_length = config["cpg"]["cpg_min_length"]
cpg_min_gc_content = config["cpg"]["cpg_min_gc_content"]
cpg_min_oe = config["cpg"]["cpg_min_oe"]
with open(cpg_output_file_path, "r") as cpg_in, open(
region_results_file_path, "w+"
) as cpg_out:
feature_count = 1
for line in cpg_in:
result_match = re.search(r"^" + region_name, line)
if result_match:
results = line.split()
start = int(results[1])
end = int(results[2])
length = end - start + 1
score = float(results[3])
gc_content = float(results[6])
oe_score = results[7]
if oe_score in ("-", "inf"):
oe_score = 0
else:
oe_score = float(oe_score)
if (
int(length) >= int(cpg_min_length)
and gc_content >= int(cpg_min_gc_content)
and oe_score >= float(cpg_min_oe)
):
gtf_line = (
f"{region_name}\tCpG\tsimple_feature\t{start}\t"
f'{end}\t.\t+\t.\tfeature_id "{feature_count}"; score "{score}";\n'
)
cpg_out.write(gtf_line)
feature_count += 1