phu 0.4.2__tar.gz → 0.4.4__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {phu-0.4.2/src/phu.egg-info → phu-0.4.4}/PKG-INFO +3 -1
- {phu-0.4.2 → phu-0.4.4}/pyproject.toml +3 -1
- {phu-0.4.2 → phu-0.4.4}/src/phu/cli.py +4 -0
- {phu-0.4.2 → phu-0.4.4}/src/phu/screen.py +232 -189
- {phu-0.4.2 → phu-0.4.4/src/phu.egg-info}/PKG-INFO +3 -1
- {phu-0.4.2 → phu-0.4.4}/src/phu.egg-info/requires.txt +2 -0
- {phu-0.4.2 → phu-0.4.4}/LICENSE +0 -0
- {phu-0.4.2 → phu-0.4.4}/README.md +0 -0
- {phu-0.4.2 → phu-0.4.4}/setup.cfg +0 -0
- {phu-0.4.2 → phu-0.4.4}/src/phu/__init__.py +0 -0
- {phu-0.4.2 → phu-0.4.4}/src/phu/_exec.py +0 -0
- {phu-0.4.2 → phu-0.4.4}/src/phu/cluster.py +0 -0
- {phu-0.4.2 → phu-0.4.4}/src/phu/simplify_vcontact_taxa.py +0 -0
- {phu-0.4.2 → phu-0.4.4}/src/phu.egg-info/SOURCES.txt +0 -0
- {phu-0.4.2 → phu-0.4.4}/src/phu.egg-info/dependency_links.txt +0 -0
- {phu-0.4.2 → phu-0.4.4}/src/phu.egg-info/entry_points.txt +0 -0
- {phu-0.4.2 → phu-0.4.4}/src/phu.egg-info/top_level.txt +0 -0
- {phu-0.4.2 → phu-0.4.4}/tests/test_seqclust_cli.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: phu
|
|
3
|
-
Version: 0.4.
|
|
3
|
+
Version: 0.4.4
|
|
4
4
|
Summary: Phage bioinformatics utilities (seqclust runner and friends).
|
|
5
5
|
Author-email: Camilo García-Botero <ca.garcia2@uniandes.edu.co>
|
|
6
6
|
License: MIT
|
|
@@ -8,7 +8,9 @@ Requires-Python: >=3.10
|
|
|
8
8
|
Description-Content-Type: text/markdown
|
|
9
9
|
License-File: LICENSE
|
|
10
10
|
Requires-Dist: pandas>=2.3.2
|
|
11
|
+
Requires-Dist: pyhmmer>=0.11.1
|
|
11
12
|
Requires-Dist: pyrodigal>=3.6.3.post1
|
|
13
|
+
Requires-Dist: pyrodigal-gv>=0.3.2
|
|
12
14
|
Requires-Dist: typer>=0.17.3
|
|
13
15
|
Dynamic: license-file
|
|
14
16
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "phu"
|
|
3
|
-
version = "0.4.
|
|
3
|
+
version = "0.4.4"
|
|
4
4
|
description = "Phage bioinformatics utilities (seqclust runner and friends)."
|
|
5
5
|
readme = "README.md"
|
|
6
6
|
license = { text = "MIT" }
|
|
@@ -10,7 +10,9 @@ authors = [
|
|
|
10
10
|
requires-python = ">=3.10"
|
|
11
11
|
dependencies = [
|
|
12
12
|
"pandas>=2.3.2",
|
|
13
|
+
"pyhmmer>=0.11.1",
|
|
13
14
|
"pyrodigal>=3.6.3.post1",
|
|
15
|
+
"pyrodigal-gv>=0.3.2",
|
|
14
16
|
"typer>=0.17.3",
|
|
15
17
|
]
|
|
16
18
|
|
|
@@ -185,6 +185,9 @@ def screen(
|
|
|
185
185
|
False, "--save-target-proteins/--no-save-target-proteins",
|
|
186
186
|
help="Save matched proteins per HMM model in target_proteins/ subfolder"
|
|
187
187
|
),
|
|
188
|
+
save_target_hmms: bool = typer.Option(
|
|
189
|
+
False, "--save-target-hmms/--no-save-target-hmms", help="Save HMMs built from target proteins in target_hmms/ subfolder"
|
|
190
|
+
),
|
|
188
191
|
hmm_mode: str = typer.Option(
|
|
189
192
|
"pure", "--hmm-mode", help="HMM file type: 'pure' (one model per file) or 'mixed' (pressed/concatenated HMMs)"
|
|
190
193
|
)
|
|
@@ -238,6 +241,7 @@ def screen(
|
|
|
238
241
|
combine_mode=combine_mode,
|
|
239
242
|
min_hmm_hits=min_hmm_hits,
|
|
240
243
|
save_target_proteins=save_target_proteins,
|
|
244
|
+
save_target_hmms=save_target_hmms,
|
|
241
245
|
hmm_mode=hmm_mode, # New parameter
|
|
242
246
|
)
|
|
243
247
|
|
|
@@ -10,13 +10,19 @@ from dataclasses import dataclass, field
|
|
|
10
10
|
from pathlib import Path
|
|
11
11
|
from typing import Dict, Iterable, List, Optional, Tuple
|
|
12
12
|
from multiprocessing.pool import ThreadPool
|
|
13
|
+
from collections import defaultdict
|
|
13
14
|
|
|
14
15
|
import typer
|
|
15
16
|
from pyrodigal import GeneFinder # pyrodigal>=3
|
|
17
|
+
from pyrodigal_gv import ViralGeneFinder # New import for viral gene prediction
|
|
18
|
+
|
|
19
|
+
import pyhmmer
|
|
20
|
+
import pyhmmer.plan7
|
|
21
|
+
import pyhmmer.easel
|
|
16
22
|
|
|
17
23
|
from ._exec import run, _executable, CmdNotFound
|
|
18
24
|
|
|
19
|
-
app = typer.Typer(help="Screen contigs for a protein family using
|
|
25
|
+
app = typer.Typer(help="Screen contigs for a protein family using pyHMMER on predicted CDS.")
|
|
20
26
|
|
|
21
27
|
|
|
22
28
|
# ---------- Utilities ----------
|
|
@@ -42,6 +48,7 @@ class ScreenConfig:
|
|
|
42
48
|
combine_mode: str = "any" # New: how to combine hits from multiple HMMs
|
|
43
49
|
min_hmm_hits: int = 1 # New: minimum number of HMMs that must hit a contig
|
|
44
50
|
save_target_proteins: bool = False # New: save matched proteins per HMM
|
|
51
|
+
save_target_hmms: bool = False # New: build and save HMMs from target proteins
|
|
45
52
|
hmm_mode: str = "pure" # New: "pure" for single models, "mixed" for pressed/concatenated HMMs
|
|
46
53
|
|
|
47
54
|
def __post_init__(self):
|
|
@@ -58,7 +65,9 @@ class ScreenConfig:
|
|
|
58
65
|
raise ValueError("combine_mode must be 'any', 'all', or 'threshold'")
|
|
59
66
|
if self.hmm_mode not in {"pure", "mixed"}:
|
|
60
67
|
raise ValueError("hmm_mode must be 'pure' or 'mixed'")
|
|
61
|
-
|
|
68
|
+
if self.save_target_hmms and not self.save_target_proteins:
|
|
69
|
+
raise ValueError("save_target_hmms requires save_target_proteins to be True")
|
|
70
|
+
|
|
62
71
|
def plan(self) -> "ScreenPlan":
|
|
63
72
|
"""Create execution plan from configuration."""
|
|
64
73
|
if self.mode not in {"meta", "single"}:
|
|
@@ -90,6 +99,7 @@ class ScreenConfig:
|
|
|
90
99
|
combine_mode=self.combine_mode,
|
|
91
100
|
min_hmm_hits=self.min_hmm_hits,
|
|
92
101
|
save_target_proteins=self.save_target_proteins,
|
|
102
|
+
save_target_hmms=self.save_target_hmms,
|
|
93
103
|
hmm_mode=self.hmm_mode,
|
|
94
104
|
proteins_fa=self.outdir / "proteins.faa",
|
|
95
105
|
domtbl_paths=domtbl_paths,
|
|
@@ -118,19 +128,20 @@ class ScreenPlan:
|
|
|
118
128
|
combine_mode: str
|
|
119
129
|
min_hmm_hits: int
|
|
120
130
|
save_target_proteins: bool # New
|
|
131
|
+
save_target_hmms: bool # New
|
|
121
132
|
hmm_mode: str # New
|
|
122
133
|
proteins_fa: Path
|
|
123
134
|
domtbl_paths: Dict[str, Path] # Changed from domtbl: Path
|
|
124
135
|
kept_ids: Path
|
|
125
136
|
out_contigs: Path
|
|
126
137
|
|
|
127
|
-
def _binaries() ->
|
|
138
|
+
def _binaries() -> str:
|
|
128
139
|
"""
|
|
129
140
|
Discover required binaries for screening.
|
|
141
|
+
Only seqkit is needed since we use pyHMMER instead of HMMER binary.
|
|
130
142
|
"""
|
|
131
|
-
hmmer = _executable(["hmmsearch"])
|
|
132
143
|
seqkit = _executable(["seqkit"])
|
|
133
|
-
return
|
|
144
|
+
return seqkit
|
|
134
145
|
|
|
135
146
|
def _read_fasta(fp: Path) -> Iterable[Tuple[str, str]]:
|
|
136
147
|
"""Tiny FASTA reader (header up to first whitespace is the id)."""
|
|
@@ -155,57 +166,6 @@ class Hit:
|
|
|
155
166
|
bitscore: float
|
|
156
167
|
evalue: float
|
|
157
168
|
|
|
158
|
-
def _parse_domtblout(domtbl_path: Path, hmm_file_name: str, hmm_mode: str) -> Iterable[Hit]:
|
|
159
|
-
"""
|
|
160
|
-
Parse HMMER --domtblout (hmmsearch). Returns domain-level hits.
|
|
161
|
-
Format spec: https://hmmer-web-docs.readthedocs.io/en/latest/output-files/domtab.html
|
|
162
|
-
|
|
163
|
-
Args:
|
|
164
|
-
hmm_mode: "pure" for single-model HMMs, "mixed" for concatenated HMMs
|
|
165
|
-
|
|
166
|
-
Filtering logic:
|
|
167
|
-
- Individual hits are filtered by bitscore/evalue thresholds regardless of hmm_mode
|
|
168
|
-
- Model counting for combine_mode depends on hmm_mode:
|
|
169
|
-
* "pure": each file = 1 model, use filename as identifier
|
|
170
|
-
* "mixed": each target_name = 1 model, use actual HMM name as identifier
|
|
171
|
-
"""
|
|
172
|
-
with domtbl_path.open() as f:
|
|
173
|
-
for line in f:
|
|
174
|
-
if not line or line.startswith("#"):
|
|
175
|
-
continue
|
|
176
|
-
cols = re.split(r"\s+", line.strip())
|
|
177
|
-
try:
|
|
178
|
-
target_name = cols[0] # our protein sequence ID
|
|
179
|
-
query_name = cols[3] # HMM model name
|
|
180
|
-
i_evalue = float(cols[12]) # independent E-value
|
|
181
|
-
bits = float(cols[13])
|
|
182
|
-
except Exception:
|
|
183
|
-
# Fallback for slight index shifts (older HMMER versions)
|
|
184
|
-
try:
|
|
185
|
-
i_evalue = float(cols[11])
|
|
186
|
-
bits = float(cols[12])
|
|
187
|
-
except Exception:
|
|
188
|
-
continue
|
|
189
|
-
|
|
190
|
-
# Extract contig ID from protein sequence ID (query_name is the protein ID)
|
|
191
|
-
prot_id = target_name # This should be like "contig123|gene1"
|
|
192
|
-
if "|" in prot_id:
|
|
193
|
-
contig_id = prot_id.split("|", 1)[0]
|
|
194
|
-
else:
|
|
195
|
-
contig_id = prot_id
|
|
196
|
-
|
|
197
|
-
# Handle model naming based on HMM mode
|
|
198
|
-
if hmm_mode == "pure":
|
|
199
|
-
# For pure HMMs, use filename as the model identifier for consistency
|
|
200
|
-
# This ensures each file is treated as a separate "model" for combine logic
|
|
201
|
-
model_id = hmm_file_name
|
|
202
|
-
else: # mixed
|
|
203
|
-
# For mixed/pressed HMMs, use the actual HMM model name (query_name)
|
|
204
|
-
# This allows proper identification of individual models within the file
|
|
205
|
-
model_id = query_name
|
|
206
|
-
|
|
207
|
-
yield Hit(contig=contig_id, prot_id=prot_id, model=model_id, bitscore=bits, evalue=i_evalue)
|
|
208
|
-
|
|
209
169
|
|
|
210
170
|
# ---------- Core pipeline ----------
|
|
211
171
|
|
|
@@ -225,7 +185,7 @@ def _predict_proteins_pyrodigal(
|
|
|
225
185
|
Uses ThreadPool for parallel processing of contigs when threads > 1.
|
|
226
186
|
"""
|
|
227
187
|
# Initialize GeneFinder according to the API
|
|
228
|
-
gf =
|
|
188
|
+
gf = ViralGeneFinder(meta=(mode == "meta"), min_gene=min_len)
|
|
229
189
|
|
|
230
190
|
# Read all contigs first
|
|
231
191
|
contigs = list(_read_fasta(contigs_fa))
|
|
@@ -270,58 +230,84 @@ def _predict_proteins_pyrodigal(
|
|
|
270
230
|
return n_prot
|
|
271
231
|
|
|
272
232
|
def _hmmsearch(
|
|
273
|
-
|
|
233
|
+
hmm_paths: List[Path],
|
|
274
234
|
proteins_fa: Path,
|
|
275
|
-
|
|
235
|
+
domtbl_paths: Dict[str, Path],
|
|
276
236
|
threads: int = 1,
|
|
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
|
-
if
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
237
|
+
hmm_mode: str = "pure",
|
|
238
|
+
keep_domtbl: bool = True,
|
|
239
|
+
) -> Iterable[Hit]:
|
|
240
|
+
"""
|
|
241
|
+
Run pyhmmer.hmmsearch on loaded HMMs and proteins.
|
|
242
|
+
Returns hits directly as Hit objects and optionally writes domtbl files.
|
|
243
|
+
"""
|
|
244
|
+
# Load all HMMs into memory
|
|
245
|
+
hmms = []
|
|
246
|
+
hmm_names = []
|
|
247
|
+
for hmm_path in hmm_paths:
|
|
248
|
+
with pyhmmer.plan7.HMMFile(hmm_path) as hmm_file:
|
|
249
|
+
for hmm in hmm_file:
|
|
250
|
+
hmms.append(hmm)
|
|
251
|
+
hmm_names.append(hmm_path.stem) # Use filename for pure mode
|
|
252
|
+
|
|
253
|
+
# Load proteins into memory
|
|
254
|
+
with pyhmmer.easel.SequenceFile(proteins_fa, digital=True) as seq_file:
|
|
255
|
+
proteins = seq_file.read_block()
|
|
256
|
+
|
|
257
|
+
# Run hmmsearch with pyHMMER
|
|
258
|
+
hits_list = list(pyhmmer.hmmsearch(hmms, proteins, cpus=threads, bit_cutoffs=None))
|
|
259
|
+
|
|
260
|
+
# Write domtbl files if requested
|
|
261
|
+
if keep_domtbl:
|
|
262
|
+
for i, top_hits in enumerate(hits_list):
|
|
263
|
+
# Map back to original HMM file for domtbl naming
|
|
264
|
+
hmm_name = hmm_names[i] if i < len(hmm_names) else f"hmm_{i}"
|
|
265
|
+
if hmm_name in domtbl_paths:
|
|
266
|
+
domtbl_path = domtbl_paths[hmm_name]
|
|
267
|
+
with domtbl_path.open("wb") as f:
|
|
268
|
+
top_hits.write(f, format="domains")
|
|
269
|
+
|
|
270
|
+
# Process hits and yield Hit objects
|
|
271
|
+
for i, top_hits in enumerate(hits_list):
|
|
272
|
+
model_name = top_hits.query.name.decode()
|
|
273
|
+
|
|
274
|
+
# Determine model identifier based on hmm_mode
|
|
275
|
+
if hmm_mode == "pure":
|
|
276
|
+
# Use filename as model ID for pure mode
|
|
277
|
+
model_id = hmm_names[i] if i < len(hmm_names) else model_name
|
|
278
|
+
else:
|
|
279
|
+
# Use actual HMM name for mixed mode
|
|
280
|
+
model_id = model_name
|
|
281
|
+
|
|
282
|
+
for hit in top_hits:
|
|
283
|
+
if hit.included: # pyHMMER's inclusion check
|
|
284
|
+
prot_id = hit.name.decode()
|
|
285
|
+
|
|
286
|
+
# Extract contig from prot_id with robust handling of multiple "|" characters
|
|
287
|
+
# Expected format: "contig_name|gene<idx>" where contig_name may contain "|"
|
|
288
|
+
# Use regex to find the last "|gene" pattern
|
|
289
|
+
import re
|
|
290
|
+
gene_pattern = r'\|gene\d+$'
|
|
291
|
+
match = re.search(gene_pattern, prot_id)
|
|
292
|
+
if match:
|
|
293
|
+
# Split at the position where "|gene" starts
|
|
294
|
+
contig = prot_id[:match.start()]
|
|
295
|
+
else:
|
|
296
|
+
# Fallback: if no "|gene" pattern found, try simple split
|
|
297
|
+
if "|" in prot_id:
|
|
298
|
+
# Take everything before the last "|" as contig ID
|
|
299
|
+
contig = prot_id.rsplit("|", 1)[0]
|
|
300
|
+
else:
|
|
301
|
+
# No "|" found, use entire protein ID as contig ID
|
|
302
|
+
contig = prot_id
|
|
303
|
+
|
|
304
|
+
yield Hit(
|
|
305
|
+
contig=contig,
|
|
306
|
+
prot_id=prot_id,
|
|
307
|
+
model=model_id,
|
|
308
|
+
bitscore=hit.score,
|
|
309
|
+
evalue=hit.evalue
|
|
310
|
+
)
|
|
325
311
|
|
|
326
312
|
def _choose_best_contigs(
|
|
327
313
|
hits: Iterable[Hit],
|
|
@@ -336,33 +322,11 @@ def _choose_best_contigs(
|
|
|
336
322
|
Filter by thresholds, then pick top N hits per contig by bitscore.
|
|
337
323
|
For multiple HMMs, apply combination logic.
|
|
338
324
|
|
|
339
|
-
Filtering criteria with hmm_mode:
|
|
340
|
-
|
|
341
|
-
1. Individual hit filtering (same for both modes):
|
|
342
|
-
- min_bitscore: minimum score threshold per hit
|
|
343
|
-
- max_evalue: maximum E-value threshold per hit
|
|
344
|
-
|
|
345
|
-
2. Model counting for combine logic (depends on hmm_mode):
|
|
346
|
-
- "pure" mode: each HMM file = 1 model
|
|
347
|
-
* "any": contig needs hits from ≥1 files
|
|
348
|
-
* "all": contig needs hits from ALL files
|
|
349
|
-
* "threshold": contig needs hits from ≥min_hmm_hits files
|
|
350
|
-
|
|
351
|
-
- "mixed" mode: each unique target name = 1 model
|
|
352
|
-
* "any": contig needs hits from ≥1 unique models
|
|
353
|
-
* "all": contig needs hits from ALL unique models found across all files
|
|
354
|
-
* "threshold": contig needs hits from ≥min_hmm_hits unique models
|
|
355
|
-
|
|
356
|
-
3. Hit selection per contig:
|
|
357
|
-
- For "any" and "threshold": top_per_contig best hits overall
|
|
358
|
-
- For "all": exactly 1 best hit per model (ensures balanced output)
|
|
359
|
-
|
|
360
325
|
Returns (kept_hits, list_of_contig_ids).
|
|
361
326
|
"""
|
|
362
|
-
from collections import defaultdict
|
|
363
327
|
per_contig: Dict[str, List[Hit]] = defaultdict(list)
|
|
364
328
|
|
|
365
|
-
# Filter hits by thresholds
|
|
329
|
+
# Filter hits by thresholds
|
|
366
330
|
for h in hits:
|
|
367
331
|
if min_bitscore is not None and h.bitscore < min_bitscore:
|
|
368
332
|
continue
|
|
@@ -370,17 +334,12 @@ def _choose_best_contigs(
|
|
|
370
334
|
continue
|
|
371
335
|
per_contig[h.contig].append(h)
|
|
372
336
|
|
|
373
|
-
# Apply combination logic
|
|
337
|
+
# Apply combination logic
|
|
374
338
|
kept: List[Hit] = []
|
|
375
339
|
kept_contigs: List[str] = []
|
|
376
340
|
|
|
377
341
|
for contig, contig_hits in per_contig.items():
|
|
378
342
|
if combine_mode == "any":
|
|
379
|
-
# Keep contigs that have hits from any model.
|
|
380
|
-
# Instead of keeping only the single best hit across all models, keep
|
|
381
|
-
# the best hit per model/file. This recovers one (or up to
|
|
382
|
-
# top_per_contig) protein per model for contigs that matched
|
|
383
|
-
# multiple models.
|
|
384
343
|
if contig_hits:
|
|
385
344
|
hits_per_model = defaultdict(list)
|
|
386
345
|
for hit in contig_hits:
|
|
@@ -395,17 +354,15 @@ def _choose_best_contigs(
|
|
|
395
354
|
elif combine_mode == "all":
|
|
396
355
|
# Keep contigs that have hits from ALL models
|
|
397
356
|
model_names = set(hit.model for hit in contig_hits)
|
|
398
|
-
if len(model_names) == total_hmm_models:
|
|
399
|
-
# For "all" mode, ensure we get exactly one hit per model per contig
|
|
357
|
+
if len(model_names) == total_hmm_models:
|
|
400
358
|
hits_per_model = defaultdict(list)
|
|
401
359
|
for hit in contig_hits:
|
|
402
|
-
|
|
403
|
-
hits_per_model[model_name].append(hit)
|
|
360
|
+
hits_per_model[hit.model].append(hit)
|
|
404
361
|
|
|
405
|
-
# Take the best hit for each model
|
|
406
|
-
for
|
|
362
|
+
# Take the best hit for each model
|
|
363
|
+
for model_hits in hits_per_model.values():
|
|
407
364
|
model_hits.sort(key=lambda x: (x.bitscore, -x.evalue), reverse=True)
|
|
408
|
-
kept.extend(model_hits[:1])
|
|
365
|
+
kept.extend(model_hits[:1])
|
|
409
366
|
|
|
410
367
|
kept_contigs.append(contig)
|
|
411
368
|
|
|
@@ -414,8 +371,7 @@ def _choose_best_contigs(
|
|
|
414
371
|
model_names = set(hit.model for hit in contig_hits)
|
|
415
372
|
if len(model_names) >= min_hmm_hits:
|
|
416
373
|
contig_hits.sort(key=lambda x: (x.bitscore, -x.evalue), reverse=True)
|
|
417
|
-
|
|
418
|
-
kept.extend(slice_)
|
|
374
|
+
kept.extend(contig_hits[:max(1, top_per_contig)])
|
|
419
375
|
kept_contigs.append(contig)
|
|
420
376
|
|
|
421
377
|
return kept, kept_contigs
|
|
@@ -450,15 +406,7 @@ def _extract_target_proteins(
|
|
|
450
406
|
) -> None:
|
|
451
407
|
"""
|
|
452
408
|
Extract matched proteins per HMM model from the final kept contigs only.
|
|
453
|
-
This ensures the proteins match those from contigs in screened_contigs.fasta
|
|
454
|
-
and respects the combine_mode filtering.
|
|
455
|
-
|
|
456
|
-
Protein extraction behavior:
|
|
457
|
-
- "pure" mode: proteins grouped by HMM filename (one group per file)
|
|
458
|
-
- "mixed" mode: proteins grouped by actual model names (multiple groups per file possible)
|
|
459
409
|
"""
|
|
460
|
-
from collections import defaultdict
|
|
461
|
-
|
|
462
410
|
# Create a set of kept contig IDs for fast lookup
|
|
463
411
|
kept_contig_set = set(kept_contig_ids)
|
|
464
412
|
|
|
@@ -466,10 +414,8 @@ def _extract_target_proteins(
|
|
|
466
414
|
proteins_per_model: Dict[str, List[str]] = defaultdict(list)
|
|
467
415
|
|
|
468
416
|
for hit in kept_hits:
|
|
469
|
-
# Only include proteins from contigs that actually made it to the final output
|
|
470
417
|
if hit.contig in kept_contig_set:
|
|
471
|
-
|
|
472
|
-
proteins_per_model[model_id].append(hit.prot_id)
|
|
418
|
+
proteins_per_model[hit.model].append(hit.prot_id)
|
|
473
419
|
|
|
474
420
|
# Create target_proteins directory
|
|
475
421
|
target_proteins_dir = outdir / "target_proteins"
|
|
@@ -482,7 +428,6 @@ def _extract_target_proteins(
|
|
|
482
428
|
output_path = target_proteins_dir / f"{safe_model_name}_proteins.mfa"
|
|
483
429
|
|
|
484
430
|
if not protein_ids:
|
|
485
|
-
# Write empty file
|
|
486
431
|
output_path.write_text("")
|
|
487
432
|
continue
|
|
488
433
|
|
|
@@ -511,14 +456,112 @@ def _extract_target_proteins(
|
|
|
511
456
|
# Cleanup temporary file
|
|
512
457
|
tmp_ids_file.unlink()
|
|
513
458
|
|
|
459
|
+
def _build_target_hmms(
|
|
460
|
+
target_proteins_dir: Path,
|
|
461
|
+
outdir: Path,
|
|
462
|
+
threads: int = 1,
|
|
463
|
+
) -> None:
|
|
464
|
+
"""
|
|
465
|
+
Build HMM models from target protein sequences using pyHMMER.
|
|
466
|
+
Creates one HMM file per model from the corresponding protein FASTA files.
|
|
467
|
+
|
|
468
|
+
For single sequences, builds HMM directly using builder.build().
|
|
469
|
+
For multiple sequences, aligns them by padding to the same length before MSA creation.
|
|
470
|
+
"""
|
|
471
|
+
# Create target_hmms directory
|
|
472
|
+
target_hmms_dir = outdir / "target_hmms"
|
|
473
|
+
target_hmms_dir.mkdir(parents=True, exist_ok=True)
|
|
474
|
+
|
|
475
|
+
# Find all protein FASTA files in target_proteins directory
|
|
476
|
+
protein_files = list(target_proteins_dir.glob("*_proteins.mfa"))
|
|
477
|
+
|
|
478
|
+
if not protein_files:
|
|
479
|
+
print(" No target protein files found for HMM building")
|
|
480
|
+
return
|
|
481
|
+
|
|
482
|
+
# Initialize HMM builder and background
|
|
483
|
+
alphabet = pyhmmer.easel.Alphabet.amino()
|
|
484
|
+
builder = pyhmmer.plan7.Builder(alphabet)
|
|
485
|
+
background = pyhmmer.plan7.Background(alphabet)
|
|
486
|
+
|
|
487
|
+
print(f" Building HMMs for {len(protein_files)} protein sets...")
|
|
488
|
+
|
|
489
|
+
for protein_file in protein_files:
|
|
490
|
+
model_name = protein_file.stem.replace("_proteins", "")
|
|
491
|
+
hmm_output_path = target_hmms_dir / f"{model_name}.hmm"
|
|
492
|
+
|
|
493
|
+
try:
|
|
494
|
+
# Read protein sequences as text first
|
|
495
|
+
sequences = []
|
|
496
|
+
with open(protein_file, 'r') as f:
|
|
497
|
+
seq_id, seq_chunks = None, []
|
|
498
|
+
for line in f:
|
|
499
|
+
line = line.strip()
|
|
500
|
+
if line.startswith('>'):
|
|
501
|
+
if seq_id is not None and seq_chunks:
|
|
502
|
+
sequence = ''.join(seq_chunks)
|
|
503
|
+
if sequence: # Only add non-empty sequences
|
|
504
|
+
text_seq = pyhmmer.easel.TextSequence(name=seq_id.encode(), sequence=sequence)
|
|
505
|
+
sequences.append(text_seq)
|
|
506
|
+
seq_id = line[1:].split()[0]
|
|
507
|
+
seq_chunks = []
|
|
508
|
+
else:
|
|
509
|
+
seq_chunks.append(line)
|
|
510
|
+
|
|
511
|
+
# Don't forget the last sequence
|
|
512
|
+
if seq_id is not None and seq_chunks:
|
|
513
|
+
sequence = ''.join(seq_chunks)
|
|
514
|
+
if sequence:
|
|
515
|
+
text_seq = pyhmmer.easel.TextSequence(name=seq_id.encode(), sequence=sequence)
|
|
516
|
+
sequences.append(text_seq)
|
|
517
|
+
|
|
518
|
+
if len(sequences) == 0:
|
|
519
|
+
print(f" Skipping {model_name}: no valid sequences found")
|
|
520
|
+
continue
|
|
521
|
+
elif len(sequences) == 1:
|
|
522
|
+
# For single sequence, use builder.build() method
|
|
523
|
+
digital_seq = sequences[0].digitize(alphabet)
|
|
524
|
+
hmm = builder.build(digital_seq, background)
|
|
525
|
+
hmm.name = model_name.encode()
|
|
526
|
+
print(f" Built HMM from 1 sequence: {model_name}")
|
|
527
|
+
else:
|
|
528
|
+
# For multiple sequences, align by padding to same length
|
|
529
|
+
max_len = max(len(seq.sequence) for seq in sequences)
|
|
530
|
+
|
|
531
|
+
# Pad sequences to same length with gaps
|
|
532
|
+
aligned_sequences = []
|
|
533
|
+
for seq in sequences:
|
|
534
|
+
padded_seq = seq.sequence + '-' * (max_len - len(seq.sequence))
|
|
535
|
+
aligned_seq = pyhmmer.easel.TextSequence(name=seq.name, sequence=padded_seq)
|
|
536
|
+
aligned_sequences.append(aligned_seq)
|
|
537
|
+
|
|
538
|
+
# Create MSA from aligned sequences
|
|
539
|
+
text_msa = pyhmmer.easel.TextMSA(name=model_name.encode(), sequences=aligned_sequences)
|
|
540
|
+
digital_msa = text_msa.digitize(alphabet)
|
|
541
|
+
hmm, _, _ = builder.build_msa(digital_msa, background)
|
|
542
|
+
print(f" Built HMM from {len(sequences)} aligned sequences: {model_name}")
|
|
543
|
+
|
|
544
|
+
# Ensure HMM has proper name
|
|
545
|
+
if not hmm.name:
|
|
546
|
+
hmm.name = model_name.encode()
|
|
547
|
+
|
|
548
|
+
# Write HMM to file
|
|
549
|
+
with hmm_output_path.open("wb") as f:
|
|
550
|
+
hmm.write(f)
|
|
551
|
+
|
|
552
|
+
except Exception as e:
|
|
553
|
+
print(f" Warning: Failed to build HMM for {model_name}: {e}")
|
|
554
|
+
import traceback
|
|
555
|
+
traceback.print_exc()
|
|
556
|
+
continue
|
|
514
557
|
|
|
515
558
|
def _screen(cfg: ScreenConfig) -> ScreenPlan:
|
|
516
559
|
"""
|
|
517
|
-
Screen contigs for protein families using
|
|
560
|
+
Screen contigs for protein families using pyHMMER.
|
|
518
561
|
|
|
519
562
|
Main workflow:
|
|
520
563
|
1. Predict proteins with pyrodigal
|
|
521
|
-
2. Search proteins against
|
|
564
|
+
2. Search proteins against HMMs with pyhmmer.hmmsearch
|
|
522
565
|
3. Parse results, combine, and select best hits per contig
|
|
523
566
|
4. Extract screened contigs
|
|
524
567
|
5. Optionally extract matched proteins per HMM model from screened contigs only
|
|
@@ -531,10 +574,10 @@ def _screen(cfg: ScreenConfig) -> ScreenPlan:
|
|
|
531
574
|
if not hmm.exists():
|
|
532
575
|
raise FileNotFoundError(f"HMM file not found: {hmm}")
|
|
533
576
|
|
|
534
|
-
# Discover binaries
|
|
535
|
-
|
|
577
|
+
# Discover binaries (only seqkit needed)
|
|
578
|
+
seqkit_bin = _binaries()
|
|
536
579
|
plan = cfg.plan()
|
|
537
|
-
plan.hmmer_bin =
|
|
580
|
+
plan.hmmer_bin = "" # Not used with pyHMMER
|
|
538
581
|
plan.seqkit_bin = seqkit_bin
|
|
539
582
|
|
|
540
583
|
# Create output directory
|
|
@@ -547,7 +590,7 @@ def _screen(cfg: ScreenConfig) -> ScreenPlan:
|
|
|
547
590
|
mode=plan.mode,
|
|
548
591
|
min_len=plan.min_gene_len,
|
|
549
592
|
translation_table=plan.translation_table,
|
|
550
|
-
threads=plan.threads,
|
|
593
|
+
threads=plan.threads,
|
|
551
594
|
)
|
|
552
595
|
print(f" Proteins predicted: {n_prot}")
|
|
553
596
|
|
|
@@ -559,33 +602,19 @@ def _screen(cfg: ScreenConfig) -> ScreenPlan:
|
|
|
559
602
|
plan.proteins_fa.unlink()
|
|
560
603
|
return plan
|
|
561
604
|
|
|
562
|
-
print(f"Running hmmsearch for {len(plan.hmms)} HMM file(s) (mode: {plan.hmm_mode})…")
|
|
563
|
-
all_hits = []
|
|
564
|
-
unique_model_ids = set()
|
|
605
|
+
print(f"Running pyhmmer.hmmsearch for {len(plan.hmms)} HMM file(s) (mode: {plan.hmm_mode})…")
|
|
565
606
|
|
|
566
|
-
for
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
threads=plan.threads,
|
|
576
|
-
hmmer_bin=plan.hmmer_bin,
|
|
577
|
-
extra_args=["--noali"],
|
|
578
|
-
)
|
|
579
|
-
|
|
580
|
-
hits = list(_parse_domtblout(domtbl_path, hmm_file_name, plan.hmm_mode))
|
|
581
|
-
all_hits.extend(hits)
|
|
582
|
-
|
|
583
|
-
# Collect unique model identifiers
|
|
584
|
-
for hit in hits:
|
|
585
|
-
unique_model_ids.add(hit.model)
|
|
586
|
-
|
|
587
|
-
print(f" Found {len(hits)} hits")
|
|
607
|
+
# Use pyHMMER for all searches
|
|
608
|
+
all_hits = list(_hmmsearch(
|
|
609
|
+
hmm_paths=plan.hmms,
|
|
610
|
+
proteins_fa=plan.proteins_fa,
|
|
611
|
+
domtbl_paths=plan.domtbl_paths,
|
|
612
|
+
threads=plan.threads,
|
|
613
|
+
hmm_mode=plan.hmm_mode,
|
|
614
|
+
keep_domtbl=plan.keep_domtbl,
|
|
615
|
+
))
|
|
588
616
|
|
|
617
|
+
unique_model_ids = set(hit.model for hit in all_hits)
|
|
589
618
|
if plan.hmm_mode == "pure":
|
|
590
619
|
total_models = len(plan.hmms) # Each file is one model
|
|
591
620
|
print(f" Pure HMM mode: {total_models} models from {len(plan.hmms)} files")
|
|
@@ -593,6 +622,8 @@ def _screen(cfg: ScreenConfig) -> ScreenPlan:
|
|
|
593
622
|
total_models = len(unique_model_ids) # Count actual models found
|
|
594
623
|
print(f" Mixed HMM mode: {total_models} unique models found from {len(plan.hmms)} files")
|
|
595
624
|
|
|
625
|
+
print(f" Found {len(all_hits)} hits")
|
|
626
|
+
|
|
596
627
|
print(f"Parsing results and selecting best hits per contig (combine_mode: {plan.combine_mode})…")
|
|
597
628
|
kept_hits, contig_ids = _choose_best_contigs(
|
|
598
629
|
all_hits,
|
|
@@ -613,10 +644,20 @@ def _screen(cfg: ScreenConfig) -> ScreenPlan:
|
|
|
613
644
|
kept_hits,
|
|
614
645
|
contig_ids,
|
|
615
646
|
plan.proteins_fa,
|
|
616
|
-
plan.outdir,
|
|
647
|
+
plan.outdir,
|
|
617
648
|
plan.hmm_mode,
|
|
618
649
|
plan.seqkit_bin,
|
|
619
650
|
)
|
|
651
|
+
|
|
652
|
+
# Build HMMs from target proteins if requested
|
|
653
|
+
if plan.save_target_hmms:
|
|
654
|
+
print("Building HMMs from target protein sequences…")
|
|
655
|
+
target_proteins_dir = plan.outdir / "target_proteins"
|
|
656
|
+
_build_target_hmms(
|
|
657
|
+
target_proteins_dir,
|
|
658
|
+
plan.outdir,
|
|
659
|
+
threads=plan.threads,
|
|
660
|
+
)
|
|
620
661
|
|
|
621
662
|
print(f"Extracting {len(contig_ids)} contig(s) with seqkit…")
|
|
622
663
|
_seqkit_extract(
|
|
@@ -640,6 +681,8 @@ def _screen(cfg: ScreenConfig) -> ScreenPlan:
|
|
|
640
681
|
files_msg += f" and {len(plan.domtbl_paths)} domtblout files"
|
|
641
682
|
if plan.save_target_proteins:
|
|
642
683
|
files_msg += f" and target proteins in target_proteins/ folder"
|
|
684
|
+
if plan.save_target_hmms:
|
|
685
|
+
files_msg += f" and target HMMs in target_hmms/ folder"
|
|
643
686
|
print(f"{files_msg}.")
|
|
644
687
|
|
|
645
688
|
return plan
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: phu
|
|
3
|
-
Version: 0.4.
|
|
3
|
+
Version: 0.4.4
|
|
4
4
|
Summary: Phage bioinformatics utilities (seqclust runner and friends).
|
|
5
5
|
Author-email: Camilo García-Botero <ca.garcia2@uniandes.edu.co>
|
|
6
6
|
License: MIT
|
|
@@ -8,7 +8,9 @@ Requires-Python: >=3.10
|
|
|
8
8
|
Description-Content-Type: text/markdown
|
|
9
9
|
License-File: LICENSE
|
|
10
10
|
Requires-Dist: pandas>=2.3.2
|
|
11
|
+
Requires-Dist: pyhmmer>=0.11.1
|
|
11
12
|
Requires-Dist: pyrodigal>=3.6.3.post1
|
|
13
|
+
Requires-Dist: pyrodigal-gv>=0.3.2
|
|
12
14
|
Requires-Dist: typer>=0.17.3
|
|
13
15
|
Dynamic: license-file
|
|
14
16
|
|
{phu-0.4.2 → phu-0.4.4}/LICENSE
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|