-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.py
More file actions
304 lines (245 loc) · 11.4 KB
/
Copy pathmain.py
File metadata and controls
304 lines (245 loc) · 11.4 KB
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
import gc
import logging
import os
import time
from concurrent.futures import ProcessPoolExecutor, as_completed
import enlighten
from Bio.Data import IUPACData
from Bio.Seq import reverse_complement
from helperlibs.bio import seqio
from geneml.args import parse_args
from geneml.logger import setup_logger, write_setup_info
from geneml.model_loader import get_cached_gene_ml_model
from geneml.outputs import build_cds_sequences, build_prediction_scores_seg, write_fasta, write_gff_file
from geneml.parallelism import compute_optimal_num_parallelism
from geneml.params import Params, Strand, build_params_namedtuple
from geneml.produce_genes import (
Transcript,
assign_transcripts_to_genes,
build_transcripts,
filter_by_dynamic_threshold,
run_model,
)
args = parse_args()
params = build_params_namedtuple(args)
if params.cpu_only:
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
else:
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "1"
os.environ["GLOG_minloglevel"] = "2"
# TF needs to be imported after environment variables are set
import tensorflow as tf # noqa: E402, I001
logger = logging.getLogger("geneml")
AMBIGUOUS_DNA_LETTERS = frozenset(IUPACData.ambiguous_dna_letters) - frozenset('ACGTN')
AMBIGUOUS_DNA_TO_N = str.maketrans({base: 'N' for base in AMBIGUOUS_DNA_LETTERS})
def parse_contigs(inpath: str, contigs_filter: list[str] | None) -> tuple[dict[str, str], int]:
"""Parse and validate contig sequences from an input file.
Reads genome records, restricted to IDs in contigs_filter if provided.
Sequences are converted to uppercase, ambiguous IUPAC DNA codes are mapped to N,
and the result is validated to contain only valid nucleotide characters.
Args:
inpath: Path to an input sequence file in FASTA/GenBank/EMBL format
contigs_filter: Optional list of contig IDs to include
Returns:
Tuple of (contigs, genome_size) where contigs is a dictionary mapping contig ID to
sequence and genome_size is the total number of bases
"""
contigs = {}
genome_size = 0
to_process = set(contigs_filter or [])
for record in seqio.parse(inpath):
if contigs_filter and record.id not in to_process:
# Skip this record
continue
to_process.discard(record.id)
seq = str(record.seq).upper()
ambiguous_dna_letters = sorted(set(seq) & AMBIGUOUS_DNA_LETTERS)
if ambiguous_dna_letters:
logger.warning(
'Contig %s contains ambiguous DNA codes %s; converting them to N.',
record.id,
', '.join(ambiguous_dna_letters),
)
seq = seq.translate(AMBIGUOUS_DNA_TO_N)
# Check if sequence is valid
if not seq:
raise ValueError(f"Contig {record.id} has no sequence.")
invalid_chars = set(seq) - set('ACGTN')
if invalid_chars:
raise ValueError(
f"Sequence of contig {record.id} contains invalid characters: "
f"{', '.join(sorted(invalid_chars))}."
)
contigs[record.id] = seq
genome_size += len(seq)
if to_process:
raise ValueError(
f"The following contig IDs were specified with --contigs-filter "
f"but not found in input file: {', '.join(sorted(to_process))}"
)
return contigs, genome_size
def process_contig(contig_id: str, seq: str, params: Params, tensorflow_thread_count=None) -> tuple[str, list[Transcript], str | None]:
"""Process one contig and return transcript predictions or raw score segments.
Returns only Python-native structures so the result is safely picklable
across process boundaries.
Args:
contig_id: Contig identifier.
seq: Contig DNA sequence.
params: Runtime parameters controlling prediction behavior.
tensorflow_thread_count: Optional thread count for TensorFlow ops.
Returns:
Tuple of contig ID, transcript list, and optional SEG-formatted scores.
"""
start_time = time.time()
tf.config.threading.set_inter_op_parallelism_threads(tensorflow_thread_count)
tf.config.threading.set_intra_op_parallelism_threads(tensorflow_thread_count)
model = get_cached_gene_ml_model(params.model_path, params.context_length)
logger.info('Processing contig %s of size %d', contig_id, len(seq))
preds = None
rc_preds = None
rc_seq = None
if params.strand is not Strand.REVERSE:
logger.info('%s 1/5: Running model on forward strand', contig_id)
preds = run_model(model, seq, params.context_length)
if params.strand is not Strand.FORWARD:
logger.info('%s 2/5: Running model on reverse strand', contig_id)
rc_seq = reverse_complement(seq)
rc_preds = run_model(model, rc_seq, params.context_length)
if params.output_segs:
segs = str(build_prediction_scores_seg(contig_id, preds, rc_preds))
return contig_id, [], segs
transcripts = build_transcripts(preds, rc_preds, seq, rc_seq, contig_id, params)
# explicitly clean up memory after finishing a contig
del preds
del rc_preds
gc.collect()
elapsed = time.time() - start_time
logger.info('Finished processing contig %s in %.2f seconds, %.2f bp/s',
contig_id, elapsed, len(seq)/elapsed)
return contig_id, transcripts, None
def reorder_contigs(contigs, num_cores) -> list[tuple[str, str]]:
"""Reorder contigs to better balance parallel work.
Args:
contigs: Mapping of contig IDs to sequences.
num_cores: Number of worker processes.
Returns:
Ordered list of (contig_id, sequence) tuples.
"""
contigs_by_size = sorted(contigs.items(), key=lambda x: len(x[1]), reverse=False)
if len(contigs_by_size) < num_cores * 2:
return contigs_by_size
reordered_contigs = []
num_groups = max(num_cores, 8)
offset = len(contigs_by_size) // num_groups + 1
for i in range(offset):
for j in range(0, len(contigs_by_size), offset):
if j + i < len(contigs_by_size):
reordered_contigs.append(contigs_by_size[j + i])
assert len(reordered_contigs) == len(contigs_by_size), f'failed to reorder contigs, {len(reordered_contigs)} != {len(contigs_by_size)}'
return reordered_contigs
def process_genome(params: Params) -> None:
"""Run the end-to-end prediction pipeline for one input genome.
Args:
params: Runtime parameters controlling model execution and outputs.
Returns:
None.
"""
num_cores = params.num_cores
genome_start_time = time.time()
contigs, genome_size = parse_contigs(params.inpath, params.contigs_filter)
contig_order = list(contigs.keys())
# Disable dynamic scoring if the input sequence is too short
if params.dynamic_scoring and genome_size < 100_000:
logger.warning(
'Input sequence is too small (%d bp) for dynamic scoring. '
'Using fixed threshold of %.2f instead. '
'Consider specifying a custom threshold with --min-gene-score.',
genome_size, params.min_gene_score
)
# Create new Params with dynamic_scoring disabled
params = params._replace(dynamic_scoring=False)
if num_cores is None:
num_cores, tensorflow_thread_count = compute_optimal_num_parallelism(num_contigs=len(contigs))
logger.info('Based on available memory, setting parallelism to %d parallel processes '
'and tensorflow threads to %s',
num_cores, tensorflow_thread_count or 'all available')
else:
tensorflow_thread_count = None
reordered_contigs = reorder_contigs(contigs, num_cores)
transcripts_by_contig_id = {}
segs_by_contig_id = {}
manager = enlighten.get_manager()
progress = manager.counter(desc=f'Processing {params.inpath}', total=genome_size, unit='bp', color='green')
if num_cores == 1:
logger.info('Running from main thread, parallelism only for tensorflow')
for contig_id, seq in reordered_contigs:
_, r, segs = process_contig(contig_id, seq, params, tensorflow_thread_count)
seq_len = len(contigs[contig_id])
progress.update(seq_len)
transcripts_by_contig_id[contig_id] = r
if segs:
segs_by_contig_id[contig_id] = segs
else:
with ProcessPoolExecutor(max_workers=num_cores) as pool:
future_to_contig = {}
for contig_id, seq in reordered_contigs:
future = pool.submit(process_contig, contig_id, seq, params, tensorflow_thread_count)
future_to_contig[future] = contig_id
for future in as_completed(future_to_contig):
contig_id = future_to_contig[future]
seq_len = len(contigs[contig_id])
progress.update(seq_len)
_, r, segs = future.result()
transcripts_by_contig_id[contig_id] = r
if segs:
segs_by_contig_id[contig_id] = segs
# Reorder transcripts and segs to match original contig order
transcripts_by_contig_id = {
contig_id: transcripts_by_contig_id[contig_id]
for contig_id in contig_order
if contig_id in transcripts_by_contig_id
}
all_segs = [segs_by_contig_id[contig_id] for contig_id in contig_order
if contig_id in segs_by_contig_id]
logger.info('Finished processing all contigs')
if params.dynamic_scoring:
logger.info('Filtering gene calls by dynamic threshold')
transcripts_by_contig_id = filter_by_dynamic_threshold(transcripts_by_contig_id,
params.min_gene_score)
genes_by_contig_id, mean_gene_score = assign_transcripts_to_genes(transcripts_by_contig_id,
params.gene_id_prefix)
if all_segs:
logger.info('Writing raw scores to %s', params.basepath+'.seg')
with open(params.basepath+'.seg', 'w', encoding='utf-8') as f:
f.write('#track graphType=heatmap maxHeightPixels=20:20:20 color=0,0,255 altColor=255,0,0\n')
for segs in all_segs:
f.write(f'{segs}\n')
else:
logger.info('Writing gene predictions to %s', params.outpath)
write_gff_file(contigs, genes_by_contig_id, params.outpath,
mean_gene_score=mean_gene_score)
if params.output_genes or params.output_proteins:
cdses_by_transcript = build_cds_sequences(contigs, genes_by_contig_id)
if params.output_genes:
logger.info('Writing gene sequences to %s', params.output_genes)
write_fasta(cdses_by_transcript, params.output_genes, sequence_type = 'cds')
if params.output_proteins:
logger.info('Writing protein sequences to %s', params.output_proteins)
write_fasta(cdses_by_transcript, params.output_proteins, sequence_type = 'protein')
elapsed = time.time() - genome_start_time
logger.info('Finished processing %s, %.1fMB, in %.2f minutes',
params.inpath, genome_size/1e6, elapsed/60)
def main() -> None:
"""Parse CLI arguments, configure logging, and run genome processing.
Args:
None.
Returns:
None.
"""
logfile = ''.join([params.basepath, '.log'])
setup_logger(logfile, debug = params.debug, verbose= params.verbose)
write_setup_info(params)
process_genome(params)
if __name__ == "__main__":
main()