philm 0.0.1__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.
Files changed (41) hide show
  1. philm-0.0.1/LICENSE +21 -0
  2. philm-0.0.1/MANIFEST.in +5 -0
  3. philm-0.0.1/PKG-INFO +386 -0
  4. philm-0.0.1/README.md +354 -0
  5. philm-0.0.1/env/requirements.cpu.txt +5 -0
  6. philm-0.0.1/env/requirements.gpu-cu121.txt +5 -0
  7. philm-0.0.1/env/requirements.txt +11 -0
  8. philm-0.0.1/img/Figure1.jpg +0 -0
  9. philm-0.0.1/pyproject.toml +74 -0
  10. philm-0.0.1/setup.cfg +4 -0
  11. philm-0.0.1/src/philm/__init__.py +1 -0
  12. philm-0.0.1/src/philm/config/config_repr.test.yaml +7 -0
  13. philm-0.0.1/src/philm/config/config_repr.train.yaml +7 -0
  14. philm-0.0.1/src/philm/config/config_repr.val.yaml +7 -0
  15. philm-0.0.1/src/philm/config/config_test.train.yaml +7 -0
  16. philm-0.0.1/src/philm/config/config_test.val.yaml +7 -0
  17. philm-0.0.1/src/philm/config/config_test.yaml +7 -0
  18. philm-0.0.1/src/philm/config/config_train.yaml +12 -0
  19. philm-0.0.1/src/philm/config/config_train_perm.yaml +44 -0
  20. philm-0.0.1/src/philm/model/__init__.py +0 -0
  21. philm-0.0.1/src/philm/model/data_loader.py +27 -0
  22. philm-0.0.1/src/philm/model/evaluate.py +104 -0
  23. philm-0.0.1/src/philm/model/extract_repr.py +75 -0
  24. philm-0.0.1/src/philm/model/model.py +48 -0
  25. philm-0.0.1/src/philm/model/optimize.py +60 -0
  26. philm-0.0.1/src/philm/model/predict.py +72 -0
  27. philm-0.0.1/src/philm/model/train.py +75 -0
  28. philm-0.0.1/src/philm/model/train_permutation.py +377 -0
  29. philm-0.0.1/src/philm/model/trainer.py +142 -0
  30. philm-0.0.1/src/philm/model/utils.py +111 -0
  31. philm-0.0.1/src/philm/scripts/__init__.py +0 -0
  32. philm-0.0.1/src/philm/scripts/infer_interactions.py +545 -0
  33. philm-0.0.1/src/philm/scripts/permutation_pvalues.py +209 -0
  34. philm-0.0.1/src/philm/scripts/split_data.py +328 -0
  35. philm-0.0.1/src/philm/scripts/summarize.py +107 -0
  36. philm-0.0.1/src/philm.egg-info/PKG-INFO +386 -0
  37. philm-0.0.1/src/philm.egg-info/SOURCES.txt +39 -0
  38. philm-0.0.1/src/philm.egg-info/dependency_links.txt +1 -0
  39. philm-0.0.1/src/philm.egg-info/entry_points.txt +9 -0
  40. philm-0.0.1/src/philm.egg-info/requires.txt +18 -0
  41. philm-0.0.1/src/philm.egg-info/top_level.txt +1 -0
philm-0.0.1/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Yiyan Yang
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,5 @@
1
+ include README.md
2
+ include LICENSE
3
+ recursive-include src/philm/config *.yaml *.yml
4
+ recursive-include img *.png *.jpg *.jpeg *.svg
5
+ recursive-include env *.txt
philm-0.0.1/PKG-INFO ADDED
@@ -0,0 +1,386 @@
1
+ Metadata-Version: 2.4
2
+ Name: philm
3
+ Version: 0.0.1
4
+ Summary: PHILM: Phage-Host Interaction Learning from Metagenomic profiles
5
+ Author: Yiyan Yang
6
+ Project-URL: Homepage, https://github.com/YiyanYang0728/PHILM
7
+ Project-URL: Repository, https://github.com/YiyanYang0728/PHILM
8
+ Project-URL: Issues, https://github.com/YiyanYang0728/PHILM/issues
9
+ Keywords: phage,host prediction,metagenomics,microbiome,virome,deep learning
10
+ Classifier: Programming Language :: Python :: 3
11
+ Classifier: Operating System :: POSIX :: Linux
12
+ Classifier: Topic :: Scientific/Engineering :: Bio-Informatics
13
+ Requires-Python: >=3.10
14
+ Description-Content-Type: text/markdown
15
+ License-File: LICENSE
16
+ Requires-Dist: numpy
17
+ Requires-Dist: pandas
18
+ Requires-Dist: scipy
19
+ Requires-Dist: scikit-learn
20
+ Requires-Dist: pyyaml
21
+ Requires-Dist: tqdm
22
+ Requires-Dist: matplotlib
23
+ Provides-Extra: cpu
24
+ Requires-Dist: torch; extra == "cpu"
25
+ Provides-Extra: gpu
26
+ Requires-Dist: torch; extra == "gpu"
27
+ Provides-Extra: dev
28
+ Requires-Dist: build; extra == "dev"
29
+ Requires-Dist: twine; extra == "dev"
30
+ Requires-Dist: pytest; extra == "dev"
31
+ Dynamic: license-file
32
+
33
+ # PHILM: Phage-Host Interaction Learning from Metagenomic Profiles
34
+
35
+ ## Overview
36
+
37
+ **PHILM** is a deep learning framework designed to predict phage-host interactions (PHIs) directly from metagenomic profiles and classify sample status using model-derived latent representations.
38
+
39
+ **Workflow**
40
+
41
+ ![Workflow](img/Figure1.jpg)
42
+
43
+ ## Table of Contents
44
+
45
+ - [PHILM: Phage-Host Interaction Learning from Metagenomic Profiles](#philm-phage-host-interaction-learning-from-metagenomic-profiles)
46
+ - [Overview](#overview)
47
+ - [Table of Contents](#table-of-contents)
48
+ - [Installation](#installation)
49
+ - [Clone the Repository](#clone-the-repository)
50
+ - [Create a Conda Environment](#create-a-conda-environment)
51
+ - [Option 1. Install using conda environment files](#option-1-install-using-conda-environment-files)
52
+ - [Option 2. Install with pip inside a conda environment](#option-2-install-with-pip-inside-a-conda-environment)
53
+ - [Usage](#usage)
54
+ - [1. Data Preparation](#1-data-preparation)
55
+ - [2. Model Training](#2-model-training)
56
+ - [3. Evaluation](#3-evaluation)
57
+ - [4. Interaction Inference](#4-interaction-inference)
58
+ - [5. Permutation-Based p-Value Estimation for Interactions (Optional)](#5-permutation-based-p-value-estimation-for-interactions-optional)
59
+ - [6. Latent Representation Extraction (Optional)](#6-latent-representation-extraction-optional)
60
+ - [Dependencies](#dependencies)
61
+ - [License](#license)
62
+ - [Citation](#citation)
63
+
64
+ ## Installation
65
+
66
+ ### Clone the Repository
67
+
68
+ ```bash
69
+ git clone git@github.com:YiyanYang0728/PHILM.git
70
+ cd PHILM
71
+ ```
72
+
73
+ ### Create a Conda Environment
74
+
75
+ #### Option 1. Install using conda environment files
76
+
77
+ For NVIDIA GPU systems with CUDA 12.1-compatible drivers:
78
+
79
+ ```bash
80
+ conda env create -f env/environment.yml
81
+ conda activate PHILM
82
+ ```
83
+
84
+ For CPU-only systems:
85
+
86
+ ```bash
87
+ conda env create -f env/environment.cpu.yml
88
+ conda activate PHILM-cpu
89
+ ```
90
+ If preferred, `mamba` can be used instead of `conda` in the commands above.
91
+
92
+ #### Option 2. Install with pip inside a conda environment
93
+
94
+ Create a clean conda environment:
95
+
96
+ ```bash
97
+ conda create -n PHILM python=3.12
98
+ conda activate PHILM
99
+ pip install dist/philm-0.0.1-py3-none-any.whl # need to update
100
+ ```
101
+
102
+ Install dependencies for NVIDIA GPU systems with CUDA 12.1-compatible drivers:
103
+
104
+ ```bash
105
+ pip install -r env/requirements.gpu-cu121.txt
106
+ ```
107
+
108
+ Install dependencies for CPU-only systems:
109
+
110
+ ```bash
111
+ pip install -r env/requirements.cpu.txt
112
+ ```
113
+
114
+ PHILM can run on CPU-only Linux systems and NVIDIA CUDA-enabled Linux systems. Separate requirement files are provided because PyTorch uses different installation packages for CUDA-enabled and CPU-only environments.
115
+
116
+ ## Usage
117
+
118
+ ### 1. Data Preparation
119
+
120
+ Prepare three inputs for `scripts/split_data.py`: a prokaryotic relative abundance profile, a phage relative abundance profile, and an output directory.
121
+
122
+ Cross-kingdom relative abundance profiles, including prokaryotic and phage profiles, can be generated using tools such as [sylph](https://github.com/bluenote-1577/sylph) and [phanta](https://github.com/bhattlab/phanta).
123
+
124
+ ```bash
125
+ philm-split --bact-arc raw_data/Bact_arc_profile.tsv --phage raw_data/Phage_profile.tsv --outdir data
126
+ ```
127
+
128
+ **Outputs:**
129
+
130
+ * Raw split data without CLR transformation:
131
+ `data/<Phage/Bact_arc>_<train/validation/test>_no_clr.tsv`
132
+
133
+ * CLR-transformed split data:
134
+ `data/<Phage/Bact_arc>_<train/validation/test>.tsv`
135
+
136
+ * Feature names:
137
+ `data/<Phage/Bact_arc>_feature_names.txt`
138
+
139
+ * Sample names:
140
+ `data/<train/validation/test>_samples.txt`
141
+
142
+ ### 2. Model Training
143
+
144
+ Configure training parameters and file paths in a YAML file.
145
+
146
+ ```bash
147
+ # Copy the template configuration file and customize it
148
+ cp src/philm/config/config_train.yaml my_config_train.yaml
149
+
150
+ # Run model training
151
+ philm-train -c my_config_train.yaml
152
+ ```
153
+
154
+ Real-time training progress, checkpoints, and logs are saved in `checkpoints/`.
155
+
156
+ **Outputs:**
157
+
158
+ * Best model:
159
+ `results/PHILM_best_model.pth`
160
+
161
+ * Best parameters:
162
+ `results/PHILM_best_params.yaml`
163
+
164
+ * Checkpoints and logs:
165
+ `checkpoints/`
166
+
167
+ ### 3. Evaluation
168
+
169
+ Generate predictions for the test data.
170
+
171
+ ```bash
172
+ # Prepare the evalutaion configuration file by merging the best parameters
173
+ awk '{print " "$0}' results/PHILM_best_params.yaml \
174
+ | cat src/philm/config/config_test.yaml - \
175
+ > my_config_test.yaml
176
+ # Run evalutaion on test dataset
177
+ philm-evaluate -c my_config_test.yaml
178
+ # Summarize metrics
179
+ philm-summarize results/PHILM_predict_test &> results/PHILM_predict_test.ft.metrics
180
+ ```
181
+
182
+ > **Tip:** Repeat the steps above for the validation and training sets by using `config_test.val.yaml` and `config_test.train.yaml`, respectively.
183
+
184
+ ```bash
185
+ # Run evalutaion on validation dataset
186
+ awk '{print " "$0}' results/PHILM_best_params.yaml \
187
+ | cat src/philm/config/config_test.val.yaml - \
188
+ > my_config_test.val.yaml
189
+ philm-evaluate -c my_config_test.val.yaml
190
+ philm-summarize results/PHILM_predict_val &> results/PHILM_predict_val.ft.metrics
191
+ # Run evalutaion on training dataset
192
+ awk '{print " "$0}' results/PHILM_best_params.yaml \
193
+ | cat src/philm/config/config_test.train.yaml - \
194
+ > my_config_test.train.yaml
195
+ philm-evaluate -c my_config_test.train.yaml
196
+ philm-summarize results/PHILM_predict_train &> results/PHILM_predict_train.ft.metrics
197
+ ```
198
+
199
+ ### 4. Interaction Inference
200
+
201
+ Infer host-sensitivity scores for each phage-prokaryote feature pair.
202
+
203
+ ```bash
204
+ # Infer interactions on the training dataset
205
+ # Note: my_config_test.train.yaml must already exist
206
+ philm-interaction -c my_config_test.train.yaml --data-dir data --split train \
207
+ --score-mode gradient --phage-normalize signed_zscore \
208
+ --out results/PHILM_interactions.tsv
209
+ ```
210
+
211
+ Optionally, interactions can be inferred using the combined training, validation, and test datasets. This result is expected to be similar to the training-set-based result.
212
+
213
+ ```bash
214
+ philm-interaction -c my_config_test.train.yaml --data-dir data --split all \
215
+ --score-mode gradient --phage-normalize signed_zscore \
216
+ --out results/PHILM_interactions.all.tsv
217
+ ```
218
+
219
+ **Output:**
220
+
221
+ * Predicted interaction scores:
222
+ `results/PHILM_interactions.tsv`
223
+
224
+ ### 5. Permutation-Based p-Value Estimation for Interactions (Optional)
225
+
226
+ This optional step estimates empirical p-values and adjusted p-values for PHILM-inferred interactions using permutation-based null models.
227
+
228
+ This step is computationally intensive and is recommended only when formal false discovery rate control is required. It is best suited for high-performance computing environments or systems with sufficient CPU resources. For exploratory analyses, users may first apply a heuristic cutoff, such as `normalized_score >= 3.5`, to prioritize candidate PHIs.
229
+
230
+ ```bash
231
+ # Step 1: Retrain models using permuted data
232
+
233
+ cat src/philm/config/config_train_perm.yaml \
234
+ <(awk '{print " "$0}' results/PHILM_best_params.yaml) \
235
+ > train_perm.yaml
236
+ N=99
237
+ # Use N=999 for a larger permutation null when computational resources allow.
238
+ d=$PWD
239
+ seq 0 $N \
240
+ | awk -v d=$d '{print "philm-permutate -c train_perm.yaml --perm-id "$1}' \
241
+ > jobs.1.txt
242
+ # Run the commands in jobs.1.txt in parallel or on an HPC cluster.
243
+ ```
244
+
245
+ ```bash
246
+ # Step 2: Infer PHIs from permutation-trained models
247
+
248
+ mkdir -p permutation_infer_configs
249
+
250
+ for i in $(seq -f "%03g" 0 $N); do
251
+ cat > permutation_infer_configs/config_infer_gradient.perm_${i}.yaml <<EOF
252
+ data:
253
+ train_X: data/Phage_train.tsv
254
+ train_Y: data/Bact_arc_train.tsv
255
+ val_X: data/Phage_val.tsv
256
+ val_Y: data/Bact_arc_val.tsv
257
+ test_X: data/Phage_test.tsv
258
+ test_Y: data/Bact_arc_test.tsv
259
+
260
+ model:
261
+ path: results/permutation_null/perm_${i}/PHILM_perm_${i}.pth
262
+ batch_size: 512
263
+ EOF
264
+
265
+ awk '$0~/hidden_size/{print " "$0}' results/PHILM_best_params.yaml \
266
+ >> permutation_infer_configs/config_infer_gradient.perm_${i}.yaml
267
+ done
268
+ ```
269
+
270
+ ```bash
271
+ > jobs.2.txt
272
+
273
+ d=$PWD
274
+
275
+ for f in permutation_infer_configs/config_infer_gradient.perm_*.yaml; do
276
+ i=$(basename "$f" .yaml | cut -f2 -d. | cut -f2 -d_)
277
+
278
+ echo "philm-interaction \
279
+ -c permutation_infer_configs/config_infer_gradient.perm_${i}.yaml \
280
+ --data-dir data \
281
+ --split train \
282
+ --score-mode gradient \
283
+ --sort-by none \
284
+ --phage-normalize signed_zscore \
285
+ --out results/permutation_null/perm_${i}/PHILM_perm_${i}.raw_gradient.tsv" \
286
+ >> jobs.2.txt
287
+ done
288
+
289
+ # Run the commands in jobs.2.txt in parallel or on an HPC cluster.
290
+ ```
291
+
292
+ ```bash
293
+ # Step 3: Calculate empirical p-values and adjusted p-values
294
+
295
+ philm-pval --norm_mode off \
296
+ --observed results/PHILM_interactions.tsv \
297
+ --perm_glob "results/permutation_null/perm_*/PHILM_perm_*.raw_gradient.tsv" \
298
+ --out results/PHILM_interactions.pvalues.tsv
299
+ ```
300
+
301
+ Filter significant interactions by adjusted p-value.
302
+
303
+ ```bash
304
+ awk -F"\t" 'NR==1 || $5<0.05' \
305
+ results/PHILM_interactions.pvalues.tsv \
306
+ | cut -f1,2,3,5 \
307
+ > results/PHILM_interactions.pvalues.filtered.tsv
308
+ ```
309
+
310
+ ### 6. Latent Representation Extraction (Optional)
311
+
312
+ PHILM can extract sample-level latent representations from a trained model. These representations can be used for downstream analyses such as sample classification, clustering, and visualization.
313
+
314
+ ```bash
315
+ # Determine the output dimension
316
+ outdim=$(wc -l < data/Bact_arc_feature_names.txt)
317
+
318
+ # Prepare the representation extraction configuration file
319
+ awk '{print " "$0}' results/PHILM_best_params.yaml \
320
+ | cat src/philm/config/config_repr.train.yaml - \
321
+ > my_config_repr.train.yaml
322
+ sed -i "s|#OUTDIM#|$outdim|g" my_config_repr.train.yaml
323
+ # Extract representations for the training data
324
+ philm-repr -c my_config_repr.train.yaml
325
+ ```
326
+
327
+ > **Tip:** Apply the same process for the validation and test datasets by using `config_repr.val.yaml` and `config_repr.test.yaml`.
328
+
329
+ ```bash
330
+ awk '{print " "$0}' results/PHILM_best_params.yaml \
331
+ | cat src/philm/config/config_repr.val.yaml - \
332
+ > my_config_repr.val.yaml
333
+ sed -i "s|#OUTDIM#|$outdim|g" my_config_repr.val.yaml
334
+ philm-repr -c my_config_repr.val.yaml
335
+ ```
336
+
337
+ ```bash
338
+ awk '{print " "$0}' results/PHILM_best_params.yaml \
339
+ | cat src/philm/config/config_repr.test.yaml - \
340
+ > my_config_repr.test.yaml
341
+ sed -i "s|#OUTDIM#|$outdim|g" my_config_repr.test.yaml
342
+ philm-repr -c my_config_repr.test.yaml
343
+ ```
344
+
345
+ Merge representations across splits.
346
+
347
+ ```bash
348
+ cat results/PHILM_train_repr1.tsv \
349
+ results/PHILM_val_repr1.tsv \
350
+ results/PHILM_test_repr1.tsv \
351
+ > results/repr1_all.tsv
352
+
353
+ cat results/PHILM_train_repr2.tsv \
354
+ results/PHILM_val_repr2.tsv \
355
+ results/PHILM_test_repr2.tsv \
356
+ > results/repr2_all.tsv
357
+ ```
358
+
359
+ The `repr1_all.tsv` and `repr2_all.tsv` files are tab-separated files without row names or column names. The number of columns corresponds to the dimensionality of the PHILM-derived representations. Samples are organized as rows and are ordered consistently with `data/train_samples.txt`, `data/val_samples.txt`, and `data/test_samples.txt`.
360
+
361
+ ## Dependencies
362
+
363
+ PHILM requires the following major Python packages:
364
+
365
+ * PyTorch
366
+ * torchdiffeq
367
+ * NumPy
368
+ * pandas
369
+ * SciPy
370
+ * scikit-learn
371
+ * scikit-bio
372
+ * PyYAML
373
+ * Optuna
374
+
375
+ Please see the requirement files in `env/` for the full dependency lists.
376
+
377
+ ## License
378
+
379
+ This project is released under the [MIT License](./LICENSE).
380
+
381
+ ## Citation
382
+
383
+ Please cite PHILM as follows:
384
+
385
+ Yang, Y., Wang, T., Huang, D., Wang, X. W., Weiss, S. T., Korzenik, J., & Liu, Y. Y. (2025). *Deep Learning Transforms Phage-Host Interaction Discovery from Metagenomic Data*. bioRxiv.
386
+