Upload folder using huggingface_hub
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- .gitattributes +1 -0
- .gitignore +174 -0
- README.md +48 -0
- dataproc.sh +5 -0
- graphabl/dataset.py +404 -0
- graphabl/eval.py +209 -0
- graphabl/loss.py +123 -0
- graphabl/main.py +245 -0
- graphabl/network.py +367 -0
- graphabl/run/0-bce256-0.001-50af/config.json +1 -0
- graphabl/run/0-bce256-0.001-50af/model_1.pth +3 -0
- graphabl/run/0-bce256-0.001-50af/model_2.pth +3 -0
- graphabl/run/0-bce256-0.001-50af/model_3.pth +3 -0
- graphabl/run/0-bce256-0.001-50af/model_4.pth +3 -0
- graphabl/run/0-bce256-0.001-50af/model_5.pth +3 -0
- graphabl/run/0-bce256-0.001-50af/result.txt +9 -0
- graphabl/run/1-bce256-0.001-50af/config.json +1 -0
- graphabl/run/1-bce256-0.001-50af/model_1.pth +3 -0
- graphabl/run/1-bce256-0.001-50af/model_2.pth +3 -0
- graphabl/run/1-bce256-0.001-50af/model_3.pth +3 -0
- graphabl/run/1-bce256-0.001-50af/model_4.pth +3 -0
- graphabl/run/1-bce256-0.001-50af/model_5.pth +3 -0
- graphabl/run/1-bce256-0.001-50af/result.txt +9 -0
- graphabl/run/2-bce256-0.001-50af/config.json +1 -0
- graphabl/run/2-bce256-0.001-50af/model_1.pth +3 -0
- graphabl/run/2-bce256-0.001-50af/model_2.pth +3 -0
- graphabl/run/2-bce256-0.001-50af/model_3.pth +3 -0
- graphabl/run/2-bce256-0.001-50af/model_4.pth +3 -0
- graphabl/run/2-bce256-0.001-50af/model_5.pth +3 -0
- graphabl/run/2-bce256-0.001-50af/result.txt +9 -0
- graphabl/summary.py +59 -0
- graphabl/train.py +56 -0
- graphabl/utils.py +32 -0
- pics/benchmark.png +3 -0
- t3.1/dataset.py +260 -0
- t3.1/eval.py +203 -0
- t3.1/gen_simi.py +38 -0
- t3.1/infer.py +133 -0
- t3.1/loss.py +123 -0
- t3.1/main.py +229 -0
- t3.1/network.py +703 -0
- t3.1/run/gru-bce256-0.001-50/config.json +1 -0
- t3.1/run/gru-bce256-0.001-50/model_1.pth +3 -0
- t3.1/run/gru-bce256-0.001-50/model_2.pth +3 -0
- t3.1/run/gru-bce256-0.001-50/model_3.pth +3 -0
- t3.1/run/gru-bce256-0.001-50/model_4.pth +3 -0
- t3.1/run/gru-bce256-0.001-50/model_5.pth +3 -0
- t3.1/run/gru-bce256-0.001-50/result.txt +9 -0
- t3.1/run/lstm-bce256-0.001-50/config.json +1 -0
- t3.1/run/lstm-bce256-0.001-50/model_1.pth +3 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
pics/benchmark.png filter=lfs diff=lfs merge=lfs -text
|
.gitignore
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Byte-compiled / optimized / DLL files
|
| 2 |
+
__pycache__/
|
| 3 |
+
*.py[cod]
|
| 4 |
+
*$py.class
|
| 5 |
+
|
| 6 |
+
# C extensions
|
| 7 |
+
*.so
|
| 8 |
+
|
| 9 |
+
# Distribution / packaging
|
| 10 |
+
.Python
|
| 11 |
+
build/
|
| 12 |
+
develop-eggs/
|
| 13 |
+
dist/
|
| 14 |
+
downloads/
|
| 15 |
+
eggs/
|
| 16 |
+
.eggs/
|
| 17 |
+
lib/
|
| 18 |
+
lib64/
|
| 19 |
+
parts/
|
| 20 |
+
sdist/
|
| 21 |
+
var/
|
| 22 |
+
wheels/
|
| 23 |
+
share/python-wheels/
|
| 24 |
+
*.egg-info/
|
| 25 |
+
.installed.cfg
|
| 26 |
+
*.egg
|
| 27 |
+
MANIFEST
|
| 28 |
+
|
| 29 |
+
# PyInstaller
|
| 30 |
+
# Usually these files are written by a python script from a template
|
| 31 |
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
| 32 |
+
*.manifest
|
| 33 |
+
*.spec
|
| 34 |
+
|
| 35 |
+
# Installer logs
|
| 36 |
+
pip-log.txt
|
| 37 |
+
pip-delete-this-directory.txt
|
| 38 |
+
|
| 39 |
+
# Unit test / coverage reports
|
| 40 |
+
htmlcov/
|
| 41 |
+
.tox/
|
| 42 |
+
.nox/
|
| 43 |
+
.coverage
|
| 44 |
+
.coverage.*
|
| 45 |
+
.cache
|
| 46 |
+
nosetests.xml
|
| 47 |
+
coverage.xml
|
| 48 |
+
*.cover
|
| 49 |
+
*.py,cover
|
| 50 |
+
.hypothesis/
|
| 51 |
+
.pytest_cache/
|
| 52 |
+
cover/
|
| 53 |
+
|
| 54 |
+
# Translations
|
| 55 |
+
*.mo
|
| 56 |
+
*.pot
|
| 57 |
+
|
| 58 |
+
# Django stuff:
|
| 59 |
+
*.log
|
| 60 |
+
local_settings.py
|
| 61 |
+
db.sqlite3
|
| 62 |
+
db.sqlite3-journal
|
| 63 |
+
|
| 64 |
+
# Flask stuff:
|
| 65 |
+
instance/
|
| 66 |
+
.webassets-cache
|
| 67 |
+
|
| 68 |
+
# Scrapy stuff:
|
| 69 |
+
.scrapy
|
| 70 |
+
|
| 71 |
+
# Sphinx documentation
|
| 72 |
+
docs/_build/
|
| 73 |
+
|
| 74 |
+
# PyBuilder
|
| 75 |
+
.pybuilder/
|
| 76 |
+
target/
|
| 77 |
+
|
| 78 |
+
# Jupyter Notebook
|
| 79 |
+
.ipynb_checkpoints
|
| 80 |
+
|
| 81 |
+
# IPython
|
| 82 |
+
profile_default/
|
| 83 |
+
ipython_config.py
|
| 84 |
+
|
| 85 |
+
# pyenv
|
| 86 |
+
# For a library or package, you might want to ignore these files since the code is
|
| 87 |
+
# intended to run in multiple environments; otherwise, check them in:
|
| 88 |
+
# .python-version
|
| 89 |
+
|
| 90 |
+
# pipenv
|
| 91 |
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
| 92 |
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
| 93 |
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
| 94 |
+
# install all needed dependencies.
|
| 95 |
+
#Pipfile.lock
|
| 96 |
+
|
| 97 |
+
# UV
|
| 98 |
+
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
|
| 99 |
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
| 100 |
+
# commonly ignored for libraries.
|
| 101 |
+
#uv.lock
|
| 102 |
+
|
| 103 |
+
# poetry
|
| 104 |
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
| 105 |
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
| 106 |
+
# commonly ignored for libraries.
|
| 107 |
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
| 108 |
+
#poetry.lock
|
| 109 |
+
|
| 110 |
+
# pdm
|
| 111 |
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
| 112 |
+
#pdm.lock
|
| 113 |
+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
| 114 |
+
# in version control.
|
| 115 |
+
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control
|
| 116 |
+
.pdm.toml
|
| 117 |
+
.pdm-python
|
| 118 |
+
.pdm-build/
|
| 119 |
+
|
| 120 |
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
| 121 |
+
__pypackages__/
|
| 122 |
+
|
| 123 |
+
# Celery stuff
|
| 124 |
+
celerybeat-schedule
|
| 125 |
+
celerybeat.pid
|
| 126 |
+
|
| 127 |
+
# SageMath parsed files
|
| 128 |
+
*.sage.py
|
| 129 |
+
|
| 130 |
+
# Environments
|
| 131 |
+
.env
|
| 132 |
+
.venv
|
| 133 |
+
env/
|
| 134 |
+
venv/
|
| 135 |
+
ENV/
|
| 136 |
+
env.bak/
|
| 137 |
+
venv.bak/
|
| 138 |
+
|
| 139 |
+
# Spyder project settings
|
| 140 |
+
.spyderproject
|
| 141 |
+
.spyproject
|
| 142 |
+
|
| 143 |
+
# Rope project settings
|
| 144 |
+
.ropeproject
|
| 145 |
+
|
| 146 |
+
# mkdocs documentation
|
| 147 |
+
/site
|
| 148 |
+
|
| 149 |
+
# mypy
|
| 150 |
+
.mypy_cache/
|
| 151 |
+
.dmypy.json
|
| 152 |
+
dmypy.json
|
| 153 |
+
|
| 154 |
+
# Pyre type checker
|
| 155 |
+
.pyre/
|
| 156 |
+
|
| 157 |
+
# pytype static type analyzer
|
| 158 |
+
.pytype/
|
| 159 |
+
|
| 160 |
+
# Cython debug symbols
|
| 161 |
+
cython_debug/
|
| 162 |
+
|
| 163 |
+
# PyCharm
|
| 164 |
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
| 165 |
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
| 166 |
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
| 167 |
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
| 168 |
+
#.idea/
|
| 169 |
+
|
| 170 |
+
# Ruff stuff:
|
| 171 |
+
.ruff_cache/
|
| 172 |
+
|
| 173 |
+
# PyPI configuration file
|
| 174 |
+
.pypirc
|
README.md
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# AMPRecognitionBenchmark
|
| 2 |
+
|
| 3 |
+
The official code for the paper "A Benchmark for Antimicrobial Peptide Recognition Based on Structure and Sequence Representation"
|
| 4 |
+
|
| 5 |
+
Antimicrobial peptides (AMPs) serve as potent therapeutic agents against drug-resistant (DR) microbes; however, their clinical application is constrained by limitations in activity. Recently, machine learning has shown significant promise in recognizing high-activity AMPs. Nevertheless, these activity datasets about AMPs are aggregated from thousands of publications, which employ varying wet-lab experimental setups and focus on only one or a few types of DR bacteria. This heterogeneity restricts the advancement of AI methods for fair evaluation of AMP recognition. Additionally, while AlphaFold has revolutionized drug discovery through accurate protein structure predictions, the integration of these predicted structures into AMP discovery remains unexplored.
|
| 6 |
+
|
| 7 |
+
To address these challenges, we present two key contributions:
|
| 8 |
+
|
| 9 |
+
**(a) DRAMPAtlas 1.0**: We introduce **DRAMPAtlas 1.0**, comprising a training set sourced from public databases and a testing set derived from our wet-lab experiments. Each AMP sequence in the atlas is annotated with its 3D structure, activity data against six types of DR bacteria, and toxicity profiles.
|
| 10 |
+
|
| 11 |
+
**(b) Comprehensive AMP recognition Experiments**: We perform extensive experiments on AMP recognition by modeling the 3D structures as voxels or graphs, either in combination with sequence information or using structure or sequence data exclusively. Our experiments reveal several insightful findings that enhance our understanding of AMP activity prediction.
|
| 12 |
+
|
| 13 |
+
We anticipate that our benchmark and findings will aid the research community in designing more effective algorithms for discovering high-activity AMPs.
|
| 14 |
+
|
| 15 |
+

|
| 16 |
+
|
| 17 |
+
## DRAMPAtlas 1.0 Dataset
|
| 18 |
+
[Harvard Dataverse](https://dataverse.harvard.edu/dataset.xhtml?persistentId=doi%3A10.7910%2FDVN%2FE9A88D)(wet-lab data not included for now, reveal after publication)
|
| 19 |
+
|
| 20 |
+
## Instruction
|
| 21 |
+
### Download the code
|
| 22 |
+
```bash
|
| 23 |
+
git clone https://github.com/EricwanAR/AMPRecognitionBenchmark.git
|
| 24 |
+
cd AMPRecognitionBenchmark
|
| 25 |
+
```
|
| 26 |
+
|
| 27 |
+
### Dataset Preparation
|
| 28 |
+
1. Put the Downloaded `dataverse_files.zip` in the root directory of this project `AMPRecognitionBenchmark/`.
|
| 29 |
+
2. Run `bash dataproc.sh` in terminal
|
| 30 |
+
3. Check if there are 2 new folders `metadata/` and `pdb/`
|
| 31 |
+
```bash
|
| 32 |
+
metadata/
|
| 33 |
+
├── data_simi.csv
|
| 34 |
+
├── data_0920_i.csv
|
| 35 |
+
└── ...
|
| 36 |
+
|
| 37 |
+
pdb/
|
| 38 |
+
├── pdb_af/ #AlphaFold Predicts
|
| 39 |
+
│ └── (pdb files ...)
|
| 40 |
+
└── pdb_dbassp/ #HelixFold Predicts
|
| 41 |
+
└── (pdb files ...)
|
| 42 |
+
```
|
| 43 |
+
|
| 44 |
+
### Training and Inferencing
|
| 45 |
+
1. `cd` to any unit folder, eg `cd t3.1` or `cd voxabl`
|
| 46 |
+
2. `python main.py [args]`, full arguments can be found in `main.py`, refer to `runthro.sh` for experiments in our paper.
|
| 47 |
+
3. Checkpoints will be saved in `run/`
|
| 48 |
+
4. For inferencing, simply replace `main.py` with `infer.py`, eg `python infer.py [training args]`
|
dataproc.sh
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/bin/bash
|
| 2 |
+
unzip -o -q dataverse_files.zip
|
| 3 |
+
mkdir pdb
|
| 4 |
+
unzip -q pdb.zip -d pdb
|
| 5 |
+
rm pdb.zip
|
graphabl/dataset.py
ADDED
|
@@ -0,0 +1,404 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import networkx as nx
|
| 3 |
+
import torch
|
| 4 |
+
from Bio.PDB import PDBParser, is_aa
|
| 5 |
+
from Bio.PDB.Polypeptide import three_to_one
|
| 6 |
+
from torch.utils.data import Dataset
|
| 7 |
+
import pandas as pd
|
| 8 |
+
import numpy as np
|
| 9 |
+
from tqdm import tqdm
|
| 10 |
+
from Bio.Align import PairwiseAligner
|
| 11 |
+
from torch_geometric.data import Data
|
| 12 |
+
from torch_geometric.utils import from_networkx
|
| 13 |
+
from Bio.SeqUtils.ProtParam import ProteinAnalysis
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
aligner = PairwiseAligner()
|
| 17 |
+
# Define the scoring system
|
| 18 |
+
aligner.mode = 'global' # Global alignment
|
| 19 |
+
aligner.match_score = 1
|
| 20 |
+
aligner.mismatch_score = -1
|
| 21 |
+
aligner.open_gap_score = -0.5
|
| 22 |
+
aligner.extend_gap_score = -0.1
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def calculate_similarity(seq_gen, seq_tem):
|
| 26 |
+
|
| 27 |
+
# Perform the alignment
|
| 28 |
+
score = aligner.score(seq_gen, seq_tem)
|
| 29 |
+
return score / len(seq_gen)
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def parse_ll37_testset(path='metadata/LL37_v0.csv'):
|
| 33 |
+
files = open(path, 'r').readlines()
|
| 34 |
+
seqs = []
|
| 35 |
+
for file in files:
|
| 36 |
+
seqs.append(str(file.strip().split(',')[-1].strip().upper()))
|
| 37 |
+
return seqs
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def gmean(labels):
|
| 41 |
+
# Assuming 'labels' is a NumPy array of shape (n, m)
|
| 42 |
+
# Calculate the geometric mean across axis 1
|
| 43 |
+
|
| 44 |
+
# Take the product of elements along axis=1
|
| 45 |
+
product = np.prod(labels, axis=1)
|
| 46 |
+
|
| 47 |
+
# nth root of product, where n is the number of elements along axis=1
|
| 48 |
+
geometric_means = product ** (1.0 / labels.shape[1])
|
| 49 |
+
return geometric_means
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
def clamp(n, smallest, largest):
|
| 53 |
+
return sorted([smallest, n, largest])[1]
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
ATOMS = {'H': 1, 'C': 12, 'N': 14, 'O': 16, 'S': 30}
|
| 57 |
+
ATOMS_R = {'H': 1, 'C': 1.5, 'N': 1.5, 'O': 1.5, 'S': 2}
|
| 58 |
+
AMINO_ACID_WATER = {'A': 255, 'V': 255, 'P': 255, 'F': 255, 'W': 255, 'I': 255, 'L': 255, 'G': 155, 'M': 155,
|
| 59 |
+
'Y': 55, 'S': 55, 'T': 55, 'C': 55, 'N': 55, 'Q': 55, 'D': 55, 'E': 55, 'K': 55, 'R': 55, 'H': 55}
|
| 60 |
+
AMINO_ACID_CHARGE = {'D': 55, 'E': 55, 'A': 155, 'V': 155, 'P': 155, 'F': 155, 'W': 155, 'I': 155, 'L': 155, 'G': 155,
|
| 61 |
+
'M': 155, 'Y': 155, 'S': 155, 'T': 155, 'C': 155, 'N': 155, 'Q': 155, 'K': 255, 'R': 255, 'H': 255}
|
| 62 |
+
# AMAs = {'G': 20, 'A': 1, 'V': 2, 'L': 3, 'I': 4, 'P': 5, 'F': 6, 'Y': 7, 'W': 8, 'S': 9, 'T': 10, 'C': 11,
|
| 63 |
+
# 'M': 12, 'N': 13, 'Q': 14, 'D': 15, 'E': 16, 'K': 17, 'R': 18, 'H': 19}
|
| 64 |
+
AMAs = {'G': 20, 'A': 1, 'V': 2, 'L': 3, 'I': 4, 'P': 5, 'F': 6, 'Y': 7, 'W': 8, 'S': 9, 'T': 10, 'C': 11,
|
| 65 |
+
'M': 12, 'N': 13, 'Q': 14, 'D': 15, 'E': 16, 'K': 17, 'R': 18, 'H': 19, 'X': 21}
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
def pdb_parser(structure):
|
| 69 |
+
"""
|
| 70 |
+
|
| 71 |
+
"""
|
| 72 |
+
voxel = np.zeros((4, 64, 64, 64), dtype=np.int8)
|
| 73 |
+
id = ''
|
| 74 |
+
seq_str = ''
|
| 75 |
+
for i in structure[0]:
|
| 76 |
+
id = i.id
|
| 77 |
+
chain = structure[0][id]
|
| 78 |
+
for res in chain:
|
| 79 |
+
if is_aa(res.get_resname(), standard=True):
|
| 80 |
+
resname = res.get_resname()
|
| 81 |
+
amino = three_to_one(resname)
|
| 82 |
+
seq_str += str(amino)
|
| 83 |
+
ATOM_WATER = AMINO_ACID_WATER[amino]
|
| 84 |
+
ATOM_CHARGE = AMINO_ACID_CHARGE[amino]
|
| 85 |
+
ATOM_CATEGORY = AMAs[amino] * 20
|
| 86 |
+
|
| 87 |
+
for i in res:
|
| 88 |
+
if i.id not in ATOMS.keys():
|
| 89 |
+
continue
|
| 90 |
+
x, y, z = i.get_coord()
|
| 91 |
+
if abs(x) > 32:
|
| 92 |
+
x = clamp(x, -31, 31)
|
| 93 |
+
if abs(y) > 32:
|
| 94 |
+
y = clamp(x, -31, 31)
|
| 95 |
+
if abs(z) > 32:
|
| 96 |
+
z = clamp(x, -31, 31)
|
| 97 |
+
x_i, y_i, z_i = int(x) + 32, int(y) + 32, int(z) + 32
|
| 98 |
+
ATOM_WEIGHT = ATOMS[i.id]
|
| 99 |
+
ATOM_R = ATOMS_R[i.id]
|
| 100 |
+
|
| 101 |
+
if ATOM_R <= 1.5:
|
| 102 |
+
voxel[0, x_i - 1:x_i + 1, y_i - 1:y_i + 1, z_i - 1:z_i + 1] = ATOM_WEIGHT
|
| 103 |
+
voxel[1, x_i - 1:x_i + 1, y_i - 1:y_i + 1, z_i - 1:z_i + 1] = ATOM_WATER
|
| 104 |
+
voxel[2, x_i - 1:x_i + 1, y_i - 1:y_i + 1, z_i - 1:z_i + 1] = ATOM_CHARGE
|
| 105 |
+
voxel[3, x_i - 1:x_i + 1, y_i - 1:y_i + 1, z_i - 1:z_i + 1] = ATOM_CATEGORY
|
| 106 |
+
else:
|
| 107 |
+
voxel[0, x_i - ATOM_R: x_i + ATOM_R, x_i - ATOM_R: x_i + ATOM_R,
|
| 108 |
+
x_i - ATOM_R: x_i + ATOM_R] = ATOM_WEIGHT
|
| 109 |
+
voxel[1, x_i - ATOM_R: x_i + ATOM_R, x_i - ATOM_R: x_i + ATOM_R,
|
| 110 |
+
x_i - ATOM_R: x_i + ATOM_R] = ATOM_WATER
|
| 111 |
+
voxel[2, x_i - ATOM_R: x_i + ATOM_R, x_i - ATOM_R: x_i + ATOM_R,
|
| 112 |
+
x_i - ATOM_R: x_i + ATOM_R] = ATOM_CHARGE
|
| 113 |
+
voxel[3, x_i - ATOM_R: x_i + ATOM_R, x_i - ATOM_R: x_i + ATOM_R,
|
| 114 |
+
x_i - ATOM_R: x_i + ATOM_R] = ATOM_CATEGORY
|
| 115 |
+
return voxel
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
def read_scoring_functions(pdb):
|
| 119 |
+
scoring = False
|
| 120 |
+
profile = []
|
| 121 |
+
for line in open(pdb):
|
| 122 |
+
if line.startswith("#END_POSE_ENERGIES_TABLE"):
|
| 123 |
+
scoring = False
|
| 124 |
+
if scoring:
|
| 125 |
+
data = [float(v) for v in line.split()[1:]]
|
| 126 |
+
profile.append(data)
|
| 127 |
+
if line.startswith("pose"):
|
| 128 |
+
scoring = True
|
| 129 |
+
return profile
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
def load_aa_features(feature_path):
|
| 133 |
+
aa_features = {}
|
| 134 |
+
for line in open(feature_path):
|
| 135 |
+
line = line.strip().split()
|
| 136 |
+
aa, features = line[0], [float(feature) for feature in line[1:]]
|
| 137 |
+
aa_features[aa] = features
|
| 138 |
+
return aa_features
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
class PairData(Data):
|
| 142 |
+
def __init__(self, edge_index_s, x_s):
|
| 143 |
+
super(PairData, self).__init__()
|
| 144 |
+
self.edge_index_s = edge_index_s
|
| 145 |
+
self.x_s = x_s
|
| 146 |
+
|
| 147 |
+
def __inc__(self, key, value, *args):
|
| 148 |
+
if key == 'edge_index_s':
|
| 149 |
+
return self.x_s.size(0)
|
| 150 |
+
if key == 'wide_nodes':
|
| 151 |
+
return self.x_s.num_nodes
|
| 152 |
+
else:
|
| 153 |
+
return super().__inc__(key, value, *args)
|
| 154 |
+
|
| 155 |
+
|
| 156 |
+
def calculate_property(seq):
|
| 157 |
+
# print(seq)
|
| 158 |
+
analysed_seq = ProteinAnalysis(seq)
|
| 159 |
+
aa_counts = analysed_seq.count_amino_acids()
|
| 160 |
+
aliphatic_index = ((aa_counts['A'] + 2.9 * aa_counts['V'] + 3.9 * (aa_counts['I'] + aa_counts['L'])) / len(seq))
|
| 161 |
+
|
| 162 |
+
charged_amino_acids = ['D', 'E', 'R', 'K', 'H']
|
| 163 |
+
total_charged = sum(aa_counts[aa] for aa in charged_amino_acids)
|
| 164 |
+
charge_density = total_charged / len(seq)
|
| 165 |
+
alpha_helix, beta_helix, turn_helix = analysed_seq.secondary_structure_fraction()
|
| 166 |
+
|
| 167 |
+
return list(
|
| 168 |
+
[round(analysed_seq.gravy(), 3) * 10, round(aliphatic_index, 3) * 10, round(analysed_seq.aromaticity(), 3) * 10,
|
| 169 |
+
round(analysed_seq.instability_index(), 3), round(alpha_helix * 10, 3), round(beta_helix * 10, 3),
|
| 170 |
+
round(turn_helix * 10, 3), round(analysed_seq.charge_at_pH(7), 3), round(analysed_seq.isoelectric_point(), 3),
|
| 171 |
+
round(charge_density, 3) * 10])
|
| 172 |
+
|
| 173 |
+
|
| 174 |
+
def construct_graph(structure, seq, gt, pdb_path, threshold, max_length):
|
| 175 |
+
# Initialize an empty graph
|
| 176 |
+
G = nx.Graph()
|
| 177 |
+
|
| 178 |
+
# Check if chain 'A' is present in the structure, return None if not
|
| 179 |
+
if 'A' not in structure[0]:
|
| 180 |
+
return None
|
| 181 |
+
chain = structure[0]['A']
|
| 182 |
+
|
| 183 |
+
# Add nodes to the graph for each amino acid in the chain with their 3D positions
|
| 184 |
+
for res in chain:
|
| 185 |
+
if is_aa(res.get_resname(), standard=True):
|
| 186 |
+
resname = res.get_resname()
|
| 187 |
+
alpha_c_atom = res["CA"]
|
| 188 |
+
x, y, z = alpha_c_atom.get_coord() # Get 3D coordinates
|
| 189 |
+
G.add_node(res.id[1], name=resname, position=(x, y, z))
|
| 190 |
+
|
| 191 |
+
# Add edges between nodes if the distance between alpha carbons is <= 5Å
|
| 192 |
+
for i, m in enumerate(G.nodes):
|
| 193 |
+
for n in list(G.nodes)[i + 1:]:
|
| 194 |
+
distance = chain[m]["CA"] - chain[n]["CA"]
|
| 195 |
+
if distance <= 5:
|
| 196 |
+
G.add_edge(m, n, weight=5 / distance)
|
| 197 |
+
|
| 198 |
+
# Load scoring functions and features
|
| 199 |
+
scoring = read_scoring_functions(pdb_path)
|
| 200 |
+
# print(pdb_path)
|
| 201 |
+
aa_features = load_aa_features('metadata/features.txt')
|
| 202 |
+
|
| 203 |
+
# Assign features to nodes in the graph, including the 3D position
|
| 204 |
+
for node_id, node_data in G.nodes(data=True):
|
| 205 |
+
res = chain[node_id]
|
| 206 |
+
aa_feature = aa_features[three_to_one(res.get_resname())]
|
| 207 |
+
energy_feature = scoring[node_id - 1]
|
| 208 |
+
position_feature = list(node_data['position']) # Convert position tuple to list
|
| 209 |
+
node_data['x'] = aa_feature + energy_feature + position_feature
|
| 210 |
+
# node_data['x'][20:40]=[0 for i in range(20)]
|
| 211 |
+
# node_data['x'][40:43]=[0 for i in range(3)]
|
| 212 |
+
|
| 213 |
+
# Convert graph to data format used in ML models
|
| 214 |
+
G = nx.convert_node_labels_to_integers(G)
|
| 215 |
+
data_wt = from_networkx(G)
|
| 216 |
+
data_graph = PairData(data_wt.edge_index, data_wt.x)
|
| 217 |
+
data_graph.gt = (gt < threshold).astype(int)
|
| 218 |
+
global_properties = calculate_property(seq)
|
| 219 |
+
|
| 220 |
+
# Encode the sequence and pad it to a fixed length
|
| 221 |
+
seq_emb = [AMAs[char] for char in seq] + [0] * (max_length - len(seq))
|
| 222 |
+
|
| 223 |
+
# print(seq_emb)
|
| 224 |
+
data_graph.seq = seq_emb
|
| 225 |
+
data_graph.global_f = global_properties
|
| 226 |
+
return data_graph
|
| 227 |
+
|
| 228 |
+
|
| 229 |
+
def remove_features(x, features_to_remove):
|
| 230 |
+
# 创建一个掩码来保留特征
|
| 231 |
+
num_features = x.size(1)
|
| 232 |
+
mask = torch.ones(num_features, dtype=torch.bool)
|
| 233 |
+
mask[features_to_remove] = False
|
| 234 |
+
|
| 235 |
+
# 更新节点特征矩阵
|
| 236 |
+
x = x[:, mask]
|
| 237 |
+
|
| 238 |
+
return x
|
| 239 |
+
|
| 240 |
+
|
| 241 |
+
class MDataset(Dataset):
|
| 242 |
+
'''
|
| 243 |
+
x: Features.
|
| 244 |
+
y: Targets, if none, do prediction.
|
| 245 |
+
'''
|
| 246 |
+
|
| 247 |
+
def __init__(self, threshold=32, mode='train', max_length=50, pdb_src='af', data_ver='0922', exclude_feature=None):
|
| 248 |
+
self.num_classes = 6
|
| 249 |
+
self.num_features = 43
|
| 250 |
+
exclude_flag = True
|
| 251 |
+
if exclude_feature == 0:
|
| 252 |
+
self.num_features = 23
|
| 253 |
+
exclude_index = torch.arange(0, 20)
|
| 254 |
+
elif exclude_feature == 1:
|
| 255 |
+
self.num_features = 23
|
| 256 |
+
exclude_index = torch.arange(20, 40)
|
| 257 |
+
elif exclude_feature == 2:
|
| 258 |
+
self.num_features = 40
|
| 259 |
+
exclude_index = torch.arange(40, 43)
|
| 260 |
+
else:
|
| 261 |
+
exclude_flag = False
|
| 262 |
+
exclude_list = pd.read_csv('../metadata/data_simi.csv', encoding="unicode_escape")['Seq'].str.upper().str.strip().tolist()
|
| 263 |
+
exclude_filter = False
|
| 264 |
+
|
| 265 |
+
p = PDBParser(QUIET=True)
|
| 266 |
+
|
| 267 |
+
if mode == 'train':
|
| 268 |
+
all_data = pd.read_csv(f'../metadata/data_{data_ver}_i.csv', encoding="unicode_escape").values
|
| 269 |
+
exclude_filter = True
|
| 270 |
+
elif mode == 'qlx':
|
| 271 |
+
all_data = pd.read_csv('../metadata/data_qlx.csv', encoding="unicode_escape").values
|
| 272 |
+
elif mode == 'saap':
|
| 273 |
+
all_data = pd.read_csv('../metadata/data_saap.csv', encoding="unicode_escape").values
|
| 274 |
+
else:
|
| 275 |
+
raise NotImplementedError
|
| 276 |
+
idx_list, seq_list, labels = all_data[:, 0], all_data[:, 1], np.concatenate((all_data[:, 4:9], all_data[:, 10:11]), axis=1)
|
| 277 |
+
|
| 278 |
+
filter_idx_list = []
|
| 279 |
+
seq_new_list = []
|
| 280 |
+
label_list = []
|
| 281 |
+
for idx in range(len(idx_list)):
|
| 282 |
+
seq = seq_list[idx].upper().strip()
|
| 283 |
+
if 'X' in seq or 'B' in seq or 'J' in seq or 'Z' in seq or 'U' in seq or 'O' in seq or len(seq) > max_length or len(seq) < 6:
|
| 284 |
+
continue
|
| 285 |
+
if exclude_filter:
|
| 286 |
+
if seq in exclude_list:
|
| 287 |
+
continue
|
| 288 |
+
|
| 289 |
+
filter_idx_list.append(idx)
|
| 290 |
+
seq_new_list.append(seq)
|
| 291 |
+
label_list.append(labels[idx])
|
| 292 |
+
|
| 293 |
+
if pdb_src == 'af':
|
| 294 |
+
pdb_root = '../pdb/pdb_af/'
|
| 295 |
+
elif pdb_src == 'hf':
|
| 296 |
+
pdb_root = '../pdb/pdb_dbassp/'
|
| 297 |
+
else:
|
| 298 |
+
raise NotImplementedError
|
| 299 |
+
self.data_list = []
|
| 300 |
+
for i in tqdm(range(len(filter_idx_list))):
|
| 301 |
+
idx = filter_idx_list[i]
|
| 302 |
+
seq = seq_new_list[i]
|
| 303 |
+
label = label_list[i]
|
| 304 |
+
|
| 305 |
+
# if os.path.exists("./pdb/pdb_dbassp/" + seq + ".pdb"):
|
| 306 |
+
# pdb_path = "./pdb/pdb_dbassp/" + seq + ".pdb"
|
| 307 |
+
# elif os.path.exists("./pdb/pdb_gen/" + seq + ".pdb"):
|
| 308 |
+
# pdb_path = "./pdb/pdb_gen/" + seq + ".pdb"
|
| 309 |
+
if os.path.exists(pdb_root + seq + ".pdb"):
|
| 310 |
+
pdb_path = pdb_root + seq + ".pdb"
|
| 311 |
+
else:
|
| 312 |
+
continue
|
| 313 |
+
# raise FileNotFoundError
|
| 314 |
+
|
| 315 |
+
structure = p.get_structure(idx, pdb_path)
|
| 316 |
+
voxel1 = construct_graph(structure, seq, label, pdb_path, threshold, max_length)
|
| 317 |
+
if exclude_flag:
|
| 318 |
+
voxel1.x_s = remove_features(voxel1.x_s, exclude_index)
|
| 319 |
+
self.data_list.append(voxel1)
|
| 320 |
+
|
| 321 |
+
def __getitem__(self, idx):
|
| 322 |
+
voxel1 = self.data_list[idx]
|
| 323 |
+
|
| 324 |
+
return voxel1
|
| 325 |
+
|
| 326 |
+
def __len__(self):
|
| 327 |
+
return len(self.data_list)
|
| 328 |
+
|
| 329 |
+
|
| 330 |
+
# class SDataset(Dataset):
|
| 331 |
+
# '''
|
| 332 |
+
# x: Features.
|
| 333 |
+
# y: Targets, if none, do prediction.
|
| 334 |
+
# '''
|
| 335 |
+
|
| 336 |
+
# def __init__(self, threshold=32, mode='train', task='0', max_length=50, pdb_src='af', data_ver='0922'):
|
| 337 |
+
# self.num_classes = 1
|
| 338 |
+
# self.task = int(task)
|
| 339 |
+
# exclude_list = pd.read_csv('metadata/data_simi.csv', encoding="unicode_escape")['Seq'].str.upper().str.strip().tolist()
|
| 340 |
+
# exclude_filter = False
|
| 341 |
+
|
| 342 |
+
# p = PDBParser(QUIET=True)
|
| 343 |
+
|
| 344 |
+
# if mode == 'train':
|
| 345 |
+
# all_data = pd.read_csv(f'metadata/data_{data_ver}_i.csv', encoding="unicode_escape").values
|
| 346 |
+
# exclude_filter = True
|
| 347 |
+
# elif mode == 'qlx':
|
| 348 |
+
# all_data = pd.read_csv('metadata/data_qlx.csv', encoding="unicode_escape").values
|
| 349 |
+
# elif mode == 'saap':
|
| 350 |
+
# all_data = pd.read_csv('metadata/data_saap.csv', encoding="unicode_escape").values
|
| 351 |
+
# else:
|
| 352 |
+
# raise NotImplementedError
|
| 353 |
+
# idx_list, seq_list, labels = all_data[:, 0], all_data[:, 1], np.concatenate((all_data[:, 4:9], all_data[:, 10:11]), axis=1)
|
| 354 |
+
# labels = labels[:, self.task:self.task+1]
|
| 355 |
+
|
| 356 |
+
# filter_idx_list = []
|
| 357 |
+
# seq_new_list = []
|
| 358 |
+
# label_list = []
|
| 359 |
+
# for idx in range(len(idx_list)):
|
| 360 |
+
# seq = seq_list[idx].upper().strip()
|
| 361 |
+
# if 'X' in seq or 'B' in seq or 'J' in seq or 'Z' in seq or 'U' in seq or 'O' in seq or len(seq) > max_length or len(seq) < 6:
|
| 362 |
+
# continue
|
| 363 |
+
# if exclude_filter:
|
| 364 |
+
# if seq in exclude_list:
|
| 365 |
+
# continue
|
| 366 |
+
|
| 367 |
+
# filter_idx_list.append(idx)
|
| 368 |
+
# seq_new_list.append(seq)
|
| 369 |
+
# label_list.append(labels[idx])
|
| 370 |
+
|
| 371 |
+
# if pdb_src == 'af':
|
| 372 |
+
# pdb_root = './pdb/pdb_af/'
|
| 373 |
+
# elif pdb_src == 'hf':
|
| 374 |
+
# pdb_root = './pdb/pdb_dbassp/'
|
| 375 |
+
# else:
|
| 376 |
+
# raise NotImplementedError
|
| 377 |
+
# self.data_list = []
|
| 378 |
+
# for i in tqdm(range(len(filter_idx_list))):
|
| 379 |
+
# idx = filter_idx_list[i]
|
| 380 |
+
# seq = seq_new_list[i]
|
| 381 |
+
# label = label_list[i]
|
| 382 |
+
|
| 383 |
+
# # if os.path.exists("./pdb/pdb_dbassp/" + seq + ".pdb"):
|
| 384 |
+
# # pdb_path = "./pdb/pdb_dbassp/" + seq + ".pdb"
|
| 385 |
+
# # elif os.path.exists("./pdb/pdb_gen/" + seq + ".pdb"):
|
| 386 |
+
# # pdb_path = "./pdb/pdb_gen/" + seq + ".pdb"
|
| 387 |
+
# if os.path.exists(pdb_root + seq + ".pdb"):
|
| 388 |
+
# pdb_path = pdb_root + seq + ".pdb"
|
| 389 |
+
# else:
|
| 390 |
+
# continue
|
| 391 |
+
# # raise FileNotFoundError
|
| 392 |
+
|
| 393 |
+
# structure = p.get_structure(idx, pdb_path)
|
| 394 |
+
# voxel1 = construct_graph(structure, seq, label, pdb_path, threshold, max_length)
|
| 395 |
+
# self.data_list.append(voxel1)
|
| 396 |
+
|
| 397 |
+
# def __getitem__(self, idx):
|
| 398 |
+
# voxel1 = self.data_list[idx]
|
| 399 |
+
|
| 400 |
+
# return voxel1
|
| 401 |
+
|
| 402 |
+
# def __len__(self):
|
| 403 |
+
# return len(self.data_list)
|
| 404 |
+
|
graphabl/eval.py
ADDED
|
@@ -0,0 +1,209 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import argparse
|
| 2 |
+
import json
|
| 3 |
+
import logging
|
| 4 |
+
import os
|
| 5 |
+
import time
|
| 6 |
+
|
| 7 |
+
from dataset import SDataset, MDataset
|
| 8 |
+
from network import MMGraph
|
| 9 |
+
from sklearn.model_selection import KFold
|
| 10 |
+
from train import train
|
| 11 |
+
import torch
|
| 12 |
+
import torch.nn as nn
|
| 13 |
+
from torch.utils.data import Subset
|
| 14 |
+
from torch_geometric.loader import DataLoader
|
| 15 |
+
import numpy as np
|
| 16 |
+
from loss import MLCE, SuperLoss, LogCoshLoss
|
| 17 |
+
from utils import set_seed
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
parser = argparse.ArgumentParser(description='resnet26')
|
| 21 |
+
# model setting
|
| 22 |
+
parser.add_argument('--model', type=str, default='mm',
|
| 23 |
+
help='model resnet26, bi-gru')
|
| 24 |
+
parser.add_argument('--fusion', type=str, default='1',
|
| 25 |
+
help="Seed for splitting dataset (default 1)")
|
| 26 |
+
|
| 27 |
+
# task & dataset setting
|
| 28 |
+
parser.add_argument('--pdb-src', type=str, dest='pdb_src', default='af',
|
| 29 |
+
help='af or hf')
|
| 30 |
+
parser.add_argument('--task-type', type=str, dest='task_type', default='mlc',
|
| 31 |
+
help='mlc or slc')
|
| 32 |
+
parser.add_argument('--task', type=str, default='all',
|
| 33 |
+
help='task: anti toxin anti-all mechanism anti-binary anti-regression mic')
|
| 34 |
+
parser.add_argument('--classes', type=int, default=6,
|
| 35 |
+
help='model')
|
| 36 |
+
parser.add_argument('--max-length', dest='max_length', type=int, default=30,
|
| 37 |
+
help='Max length for sequence filtering')
|
| 38 |
+
parser.add_argument('--split', type=int, default=5,
|
| 39 |
+
help="Split k fold in cross validation (default: 5)")
|
| 40 |
+
parser.add_argument('--seed', type=int, default=1,
|
| 41 |
+
help="Seed for splitting dataset (default: 1)")
|
| 42 |
+
parser.add_argument('--threshold', type=float, default=64,
|
| 43 |
+
help="MIC threshold for determine labels (default: 64)")
|
| 44 |
+
parser.add_argument('--num-layer', type=int, dest='num_layer', default=2,
|
| 45 |
+
help='number of GNN message passing layers (default: 2)')
|
| 46 |
+
parser.add_argument('--emb-dim', type=int, dest='emb_dim', default=90,
|
| 47 |
+
help='embedding dimensions (default: 200)')
|
| 48 |
+
parser.add_argument('--dropout-ratio', type=float, dest='dropout_ratio', default=0.5,
|
| 49 |
+
help='dropout ratio (default: 0.5)')
|
| 50 |
+
parser.add_argument('--graph-pooling', type=str, dest='graph_pooling', default="attention",
|
| 51 |
+
help='graph level pooling (sum, mean, max, attention)')
|
| 52 |
+
parser.add_argument('--gnn-type', type=str, dest='gnn_type', default="gatv2",
|
| 53 |
+
help='gnn type (gin, gcn, gat, graphsage)')
|
| 54 |
+
|
| 55 |
+
# training setting
|
| 56 |
+
parser.add_argument('--gpu', type=int, default=0,
|
| 57 |
+
help='GPU index to use, -1 for CPU (default: 0)')
|
| 58 |
+
parser.add_argument('--batch-size', type=int, dest='batch_size', default=256,
|
| 59 |
+
help='input batch size for training (default: 128)')
|
| 60 |
+
parser.add_argument('--epochs', type=int, default=110,
|
| 61 |
+
help='number of epochs to train (default: 50)')
|
| 62 |
+
parser.add_argument('--lr', type=float, default=0.002,
|
| 63 |
+
help='learning rate (default: 0.002)')
|
| 64 |
+
parser.add_argument('--decay', type=float, default=0.0005,
|
| 65 |
+
help='weight decay (default: 0.0005)')
|
| 66 |
+
parser.add_argument('--warm-steps', type=int, dest='warm_steps', default=0,
|
| 67 |
+
help='number of warm start steps for learning rate (default: 10)')
|
| 68 |
+
parser.add_argument('--patience', type=int, default=10,
|
| 69 |
+
help='patience for early stopping (default: 10)')
|
| 70 |
+
parser.add_argument('--pretrain', type=str, dest='pretrain', default='',
|
| 71 |
+
help='path of the pretrain model') # /home/duadua/Desktop/fetal/3dpretrain/runs/e50.pth
|
| 72 |
+
parser.add_argument('--metric-avg', type=str, dest='metric_avg', default='micro',
|
| 73 |
+
help='metric average type')
|
| 74 |
+
|
| 75 |
+
# args for losses
|
| 76 |
+
parser.add_argument('--loss', type=str, default='mlce',
|
| 77 |
+
help='loss function (mlce, sl, mix)')
|
| 78 |
+
|
| 79 |
+
parser.add_argument('--bias-curri', dest='bias_curri', action='store_true', default=False,
|
| 80 |
+
help='directly use loss as the training data (biased) or not (unbiased)')
|
| 81 |
+
parser.add_argument('--anti-curri', dest='anti_curri', action='store_true', default=False,
|
| 82 |
+
help='easy to hard (curri), hard to easy (anti)')
|
| 83 |
+
parser.add_argument('--std-coff', dest='std_coff', type=float, default=1,
|
| 84 |
+
help='the hyper-parameter of std')
|
| 85 |
+
|
| 86 |
+
args = parser.parse_args()
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
def main():
|
| 90 |
+
set_seed(args.seed)
|
| 91 |
+
|
| 92 |
+
if args.task_type == 'slc':
|
| 93 |
+
if args.task == 'all':
|
| 94 |
+
raise ValueError('Choose one task number to run single label classification')
|
| 95 |
+
args.classes = 1
|
| 96 |
+
elif args.task_type == 'mlc':
|
| 97 |
+
pass
|
| 98 |
+
else:
|
| 99 |
+
raise NotImplementedError
|
| 100 |
+
|
| 101 |
+
weight_dir = "./run/" + args.task_type + '-' + args.task + "-m-" + args.model + '-' + args.loss + str(args.batch_size) + str(args.lr) + str(args.epochs) + args.pdb_src
|
| 102 |
+
if not os.path.exists(weight_dir):
|
| 103 |
+
raise ValueError
|
| 104 |
+
|
| 105 |
+
logging.basicConfig(handlers=[
|
| 106 |
+
# logging.FileHandler(filename=os.path.join(weight_dir, "eval.log"), encoding='utf-8', mode='w+'),
|
| 107 |
+
logging.StreamHandler()],
|
| 108 |
+
format="%(asctime)s: %(message)s", datefmt="%F %T", level=logging.INFO)
|
| 109 |
+
|
| 110 |
+
logging.info(f'saving_dir: {weight_dir}')
|
| 111 |
+
|
| 112 |
+
# with open(os.path.join(weight_dir, "config.json"), "w") as f:
|
| 113 |
+
# f.write(json.dumps(vars(args)))
|
| 114 |
+
|
| 115 |
+
device = torch.device("cpu" if args.gpu == -1 or not torch.cuda.is_available() else f"cuda:{args.gpu}")
|
| 116 |
+
|
| 117 |
+
logging.info('Loading Training Dataset')
|
| 118 |
+
if args.task_type == 'mlc':
|
| 119 |
+
set_all = MDataset(threshold=args.threshold, mode='train', max_length=args.max_length, pdb_src=args.pdb_src)
|
| 120 |
+
else:
|
| 121 |
+
set_all = SDataset(threshold=args.threshold, mode='train', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src)
|
| 122 |
+
logging.info('Loading Test Dataset')
|
| 123 |
+
if args.task_type == 'mlc':
|
| 124 |
+
qlx_set = MDataset(threshold=args.threshold, mode='qlx', max_length=args.max_length, pdb_src=args.pdb_src)
|
| 125 |
+
saap_set = MDataset(threshold=args.threshold, mode='saap', max_length=args.max_length, pdb_src=args.pdb_src)
|
| 126 |
+
else:
|
| 127 |
+
qlx_set = SDataset(threshold=args.threshold, mode='qlx', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src)
|
| 128 |
+
saap_set = SDataset(threshold=args.threshold, mode='saap', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src)
|
| 129 |
+
|
| 130 |
+
best_perform_list = [[] for i in range(5)]
|
| 131 |
+
qlx_perform_list = [[] for i in range(5)]
|
| 132 |
+
saap_perform_list = [[] for i in range(5)]
|
| 133 |
+
|
| 134 |
+
kf = KFold(n_splits=5, shuffle=True, random_state=42)
|
| 135 |
+
|
| 136 |
+
for fold, (train_idx, val_idx) in enumerate(kf.split(set_all)):
|
| 137 |
+
# train_set= Subset(set_all, train_idx)
|
| 138 |
+
valid_set = Subset(set_all, val_idx)
|
| 139 |
+
|
| 140 |
+
# train_loader = DataLoader(train_set, batch_size=args.batch_size, follow_batch=['x_s'], shuffle=True, drop_last=True)
|
| 141 |
+
valid_loader = DataLoader(valid_set, batch_size=args.batch_size, follow_batch=['x_s'], shuffle=False)
|
| 142 |
+
qlx_loader = DataLoader(qlx_set, batch_size=1, follow_batch=['x_s'], shuffle=False)
|
| 143 |
+
saap_loader = DataLoader(saap_set, batch_size=1, follow_batch=['x_s'], shuffle=False)
|
| 144 |
+
|
| 145 |
+
if args.model == 'mm':
|
| 146 |
+
model = MMGraph(num_layer=args.num_layer, input_dim=43, emb_dim=args.emb_dim, out_dim=set_all.num_classes, JK="last",
|
| 147 |
+
drop_ratio=args.dropout_ratio, graph_pooling=args.graph_pooling, gnn_type=args.gnn_type, max_length=args.max_length)
|
| 148 |
+
|
| 149 |
+
if len(args.pretrain) != 0:
|
| 150 |
+
logging.info('loading pretrain model')
|
| 151 |
+
# model = load_pretrain_model(model, torch.load(args.pretrain))
|
| 152 |
+
model_state = model.state_dict()
|
| 153 |
+
pretrained_state = torch.load(args.pretrain)
|
| 154 |
+
pretrained_state = {k: v for k, v in pretrained_state.items() if
|
| 155 |
+
k in model_state and v.size() == model_state[k].size()}
|
| 156 |
+
model_state.update(pretrained_state)
|
| 157 |
+
model.load_state_dict(model_state)
|
| 158 |
+
# model.load_state_dict(torch.load(args.pretrain), strict=False)
|
| 159 |
+
model.to(device)
|
| 160 |
+
weights_path = f"{weight_dir}/model_{fold + 1}.pth"
|
| 161 |
+
model.load_state_dict(torch.load(weights_path))
|
| 162 |
+
# early_stopping = EarlyStopping(patience=args.patience, path=weights_path)
|
| 163 |
+
logging.info(f'Running Cross Validation {fold + 1}')
|
| 164 |
+
logging.info(f'Fold {fold + 1} Valid set:{len(valid_set)}, Test set:qlx {len(qlx_set)} saap {len(saap_set)}')
|
| 165 |
+
|
| 166 |
+
|
| 167 |
+
if args.task_type in ('mlc', 'slc') :
|
| 168 |
+
_, ap, f1, acc, auc = train(args, None, model, None, valid_loader, device, None, None)
|
| 169 |
+
logging.info(f'ap: {ap:.3f}, f1: {f1:.3f}, acc: {acc:.3f}, auc: {auc:.3f}')
|
| 170 |
+
best_perform_list[fold] = np.asarray([ap, f1, acc, auc])
|
| 171 |
+
|
| 172 |
+
_, qlx_ap, qlx_f1, qlx_acc, qlx_auc = train(args, None, model, None, qlx_loader, device, None, None)
|
| 173 |
+
logging.info(f'QLX results, ap: {qlx_ap:.3f}, f1: {qlx_f1:.3f}, acc: {qlx_acc:.3f}, auc: {qlx_auc:.3f}')
|
| 174 |
+
qlx_perform_list[fold] = np.asarray([qlx_ap, qlx_f1, qlx_acc, qlx_auc])
|
| 175 |
+
|
| 176 |
+
_, saap_ap, saap_f1, saap_acc, saap_auc = train(args, None, model, None, saap_loader, device, None, None)
|
| 177 |
+
logging.info(f'SAAP results, ap: {saap_ap:.3f}, f1: {saap_f1:.3f}, acc: {saap_acc:.3f}, auc: {saap_auc:.3f}')
|
| 178 |
+
saap_perform_list[fold] = np.asarray([saap_ap, saap_f1, saap_acc, saap_auc])
|
| 179 |
+
|
| 180 |
+
else:
|
| 181 |
+
raise NotImplementedError
|
| 182 |
+
|
| 183 |
+
logging.info(f'Cross Validation Finished!')
|
| 184 |
+
best_perform_list = np.asarray(best_perform_list)
|
| 185 |
+
qlx_perform_list = np.asarray(qlx_perform_list)
|
| 186 |
+
saap_perform_list = np.asarray(saap_perform_list)
|
| 187 |
+
logging.info('Best validation perform list\n%s', best_perform_list)
|
| 188 |
+
logging.info('mean: %s', np.round(np.mean(best_perform_list, 0), 3))
|
| 189 |
+
logging.info('std: %s', np.round(np.std(best_perform_list, 0), 3))
|
| 190 |
+
logging.info('Best qlx perform list\n%s', qlx_perform_list)
|
| 191 |
+
logging.info('mean: %s', np.round(np.mean(qlx_perform_list, 0), 3))
|
| 192 |
+
logging.info('std: %s', np.round(np.std(qlx_perform_list, 0), 3))
|
| 193 |
+
logging.info('Best saap perform list\n%s', saap_perform_list)
|
| 194 |
+
logging.info('mean: %s', np.round(np.mean(saap_perform_list, 0), 3))
|
| 195 |
+
logging.info('std: %s', np.round(np.std(saap_perform_list, 0), 3))
|
| 196 |
+
perform = open(weight_dir+'/eval.txt', 'w')
|
| 197 |
+
perform.write('Valid\n')
|
| 198 |
+
perform.write(','.join([str(i) for i in np.mean(best_perform_list, 0)])+'\n')
|
| 199 |
+
perform.write(','.join([str(i) for i in np.std(best_perform_list, 0)])+'\n')
|
| 200 |
+
perform.write('qlx\n')
|
| 201 |
+
perform.write(','.join([str(i) for i in np.mean(qlx_perform_list, 0)])+'\n')
|
| 202 |
+
perform.write(','.join([str(i) for i in np.std(qlx_perform_list, 0)])+'\n')
|
| 203 |
+
perform.write('saap\n')
|
| 204 |
+
perform.write(','.join([str(i) for i in np.mean(saap_perform_list, 0)])+'\n')
|
| 205 |
+
perform.write(','.join([str(i) for i in np.std(saap_perform_list, 0)])+'\n')
|
| 206 |
+
|
| 207 |
+
|
| 208 |
+
if __name__ == "__main__":
|
| 209 |
+
main()
|
graphabl/loss.py
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from torch import nn
|
| 3 |
+
import torch.nn.functional as F
|
| 4 |
+
from math import cos, pi, sin
|
| 5 |
+
import math
|
| 6 |
+
import numpy as np
|
| 7 |
+
from scipy.special import lambertw
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def mixup_criterion(criterion, pred, y_a, y_b, lam, pow=2):
|
| 11 |
+
y = lam ** pow * y_a + (1 - lam) ** pow * y_b
|
| 12 |
+
return criterion(pred, y)
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def mixup_data(v, q, a):
|
| 16 |
+
'''Returns mixed inputs, pairs of targets, and lambda without organ constraint'''
|
| 17 |
+
lam = np.random.beta(1, 1)
|
| 18 |
+
|
| 19 |
+
batch_size = v.shape[0]
|
| 20 |
+
index = torch.randperm(batch_size)
|
| 21 |
+
|
| 22 |
+
mixed_v = lam * v + (1 - lam) * v[index, :]
|
| 23 |
+
mixed_q = lam * q + (1 - lam) * q[index, :]
|
| 24 |
+
|
| 25 |
+
a_1, a_2 = a, a[index]
|
| 26 |
+
return mixed_v, mixed_q, a_1, a_2, lam
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def linear(epoch, nepoch):
|
| 30 |
+
return 1 - epoch / nepoch
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def convex(epoch, nepoch):
|
| 34 |
+
return epoch / (2 - nepoch)
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def concave(epoch, nepoch):
|
| 38 |
+
return 1 - sin((epoch / nepoch) * (pi / 2))
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def composite(epoch, nepoch):
|
| 42 |
+
return 0.5 * cos((epoch / nepoch) * pi) + 0.5
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
class LogCoshLoss(nn.Module):
|
| 46 |
+
def __init__(self):
|
| 47 |
+
super().__init__()
|
| 48 |
+
|
| 49 |
+
def forward(self, y_t, y_prime_t):
|
| 50 |
+
ey_t = y_t - y_prime_t
|
| 51 |
+
return torch.mean(torch.log(torch.cosh(ey_t + 1e-12)))+F.mse_loss(y_t, y_prime_t)
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
class MLCE(nn.Module):
|
| 55 |
+
def __init__(self):
|
| 56 |
+
super(MLCE, self).__init__()
|
| 57 |
+
|
| 58 |
+
def _mlcce(self, y_pred, y_true):
|
| 59 |
+
y_pred = (1 - 2 * y_true) * y_pred
|
| 60 |
+
y_pred_neg = y_pred - y_true * 1e12
|
| 61 |
+
y_pred_pos = y_pred - (1 - y_true) * 1e12
|
| 62 |
+
zeros = torch.zeros_like(y_pred[..., :1])
|
| 63 |
+
y_pred_neg = torch.cat([y_pred_neg, zeros], dim=-1)
|
| 64 |
+
y_pred_pos = torch.cat([y_pred_pos, zeros], dim=-1)
|
| 65 |
+
neg_loss = torch.logsumexp(y_pred_neg, dim=-1)
|
| 66 |
+
pos_loss = torch.logsumexp(y_pred_pos, dim=-1)
|
| 67 |
+
loss = torch.mean(neg_loss + pos_loss)
|
| 68 |
+
return loss
|
| 69 |
+
|
| 70 |
+
def __call__(self, y_pred, y_true):
|
| 71 |
+
return self._mlcce(y_pred, y_true)
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
class SuperLoss(nn.Module):
|
| 75 |
+
def __init__(self, C=10, lam=1, batch_size=128):
|
| 76 |
+
super(SuperLoss, self).__init__()
|
| 77 |
+
self.tau = math.log(C)
|
| 78 |
+
self.lam = lam # set to 1 for CIFAR10 and 0.25 for CIFAR100
|
| 79 |
+
self.batch_size = batch_size
|
| 80 |
+
|
| 81 |
+
def forward(self, logits, targets):
|
| 82 |
+
l_i = F.mse_loss(logits, targets, reduction='none').detach()
|
| 83 |
+
sigma = self.sigma(l_i)
|
| 84 |
+
loss = (F.mse_loss(logits, targets, reduction='none') - self.tau) * sigma + self.lam * (
|
| 85 |
+
torch.log(sigma) ** 2)
|
| 86 |
+
loss = loss.sum() / self.batch_size
|
| 87 |
+
return loss
|
| 88 |
+
|
| 89 |
+
def sigma(self, l_i):
|
| 90 |
+
x = torch.ones(l_i.size()) * (-2 / math.exp(1.))
|
| 91 |
+
x = x.cuda()
|
| 92 |
+
y = 0.5 * torch.max(x, (l_i - self.tau) / self.lam)
|
| 93 |
+
y = y.cpu().numpy()
|
| 94 |
+
sigma = np.exp(-lambertw(y))
|
| 95 |
+
sigma = sigma.real.astype(np.float32)
|
| 96 |
+
sigma = torch.from_numpy(sigma).cuda()
|
| 97 |
+
return sigma
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
def unbiased_curriculum_loss(out, data, args, epoch, epochs, scheduler='linear'):
|
| 101 |
+
losses = []
|
| 102 |
+
scheduler = linear if scheduler == 'linear' else concave
|
| 103 |
+
|
| 104 |
+
# calculate difficulty measurement function
|
| 105 |
+
adjusted_losses = []
|
| 106 |
+
for idx in range(out.shape[0]):
|
| 107 |
+
ground_truth = max(1, abs(data[idx].item()))
|
| 108 |
+
loss = F.mse_loss(out[idx], data[idx])
|
| 109 |
+
losses.append(loss)
|
| 110 |
+
adjusted_losses.append(loss.item() / ground_truth)
|
| 111 |
+
|
| 112 |
+
mean_loss, std_loss = np.mean(adjusted_losses), np.std(adjusted_losses)
|
| 113 |
+
|
| 114 |
+
# re-weight losses
|
| 115 |
+
total_loss = 0
|
| 116 |
+
for i, loss in enumerate(losses):
|
| 117 |
+
if adjusted_losses[i] > mean_loss + 1 * std_loss:
|
| 118 |
+
schedule_factor = scheduler(epoch, args.epochs)
|
| 119 |
+
total_loss += schedule_factor * loss
|
| 120 |
+
else:
|
| 121 |
+
total_loss += loss
|
| 122 |
+
|
| 123 |
+
return total_loss
|
graphabl/main.py
ADDED
|
@@ -0,0 +1,245 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import argparse
|
| 2 |
+
import json
|
| 3 |
+
import logging
|
| 4 |
+
import os
|
| 5 |
+
import time
|
| 6 |
+
|
| 7 |
+
from dataset import MDataset
|
| 8 |
+
from network import GraphGNN
|
| 9 |
+
from sklearn.model_selection import KFold
|
| 10 |
+
from train import train
|
| 11 |
+
import torch
|
| 12 |
+
import torch.nn as nn
|
| 13 |
+
from torch.utils.data import Subset
|
| 14 |
+
from torch_geometric.loader import DataLoader
|
| 15 |
+
import numpy as np
|
| 16 |
+
from loss import MLCE, SuperLoss, LogCoshLoss
|
| 17 |
+
from utils import set_seed
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
parser = argparse.ArgumentParser(description='resnet26')
|
| 21 |
+
# model setting
|
| 22 |
+
parser.add_argument('--model', type=str, default='mm',
|
| 23 |
+
help='model resnet26, bi-gru')
|
| 24 |
+
parser.add_argument('--fusion', type=str, default='1',
|
| 25 |
+
help="Seed for splitting dataset (default 1)")
|
| 26 |
+
parser.add_argument('--num-layer', type=int, dest='num_layer', default=2,
|
| 27 |
+
help='number of GNN message passing layers (default: 2)')
|
| 28 |
+
parser.add_argument('--emb-dim', type=int, dest='emb_dim', default=128,
|
| 29 |
+
help='embedding dimensions (default: 128)')
|
| 30 |
+
parser.add_argument('--dropout-ratio', type=float, dest='dropout_ratio', default=0.3,
|
| 31 |
+
help='dropout ratio (default: 0.3)')
|
| 32 |
+
parser.add_argument('--graph-pooling', type=str, dest='graph_pooling', default="attention",
|
| 33 |
+
help='graph level pooling (sum, mean, max, attention)')
|
| 34 |
+
parser.add_argument('--gnn-type', type=str, dest='gnn_type', default="gat",
|
| 35 |
+
help='gnn type (gin, gcn, gat, graphsage)')
|
| 36 |
+
|
| 37 |
+
# task & dataset setting
|
| 38 |
+
parser.add_argument('--pdb-src', type=str, dest='pdb_src', default='af',
|
| 39 |
+
help='af or hf')
|
| 40 |
+
parser.add_argument('--task-type', type=str, dest='task_type', default='mlc',
|
| 41 |
+
help='mlc or slc')
|
| 42 |
+
parser.add_argument('--data-ver', type=str, dest='data_ver', default='0920',
|
| 43 |
+
help='data version')
|
| 44 |
+
parser.add_argument('--task', type=str, default='all',
|
| 45 |
+
help='task: anti toxin anti-all mechanism anti-binary anti-regression mic')
|
| 46 |
+
parser.add_argument('--classes', type=int, default=6,
|
| 47 |
+
help='model')
|
| 48 |
+
parser.add_argument('--max-length', dest='max_length', type=int, default=30,
|
| 49 |
+
help='Max length for sequence filtering')
|
| 50 |
+
parser.add_argument('--split', type=int, default=5,
|
| 51 |
+
help="Split k fold in cross validation (default: 5)")
|
| 52 |
+
parser.add_argument('--seed', type=int, default=1,
|
| 53 |
+
help="Seed for splitting dataset (default: 1)")
|
| 54 |
+
parser.add_argument('--threshold', type=float, default=128,
|
| 55 |
+
help="MIC threshold for determine labels (default: 128)")
|
| 56 |
+
parser.add_argument('--exclude-feature', dest='exclude_feature', type=int, default=None)
|
| 57 |
+
|
| 58 |
+
# training setting
|
| 59 |
+
parser.add_argument('--gpu', type=int, default=0,
|
| 60 |
+
help='GPU index to use, -1 for CPU (default: 0)')
|
| 61 |
+
parser.add_argument('--batch-size', type=int, dest='batch_size', default=256,
|
| 62 |
+
help='input batch size for training (default: 128)')
|
| 63 |
+
parser.add_argument('--epochs', type=int, default=50,
|
| 64 |
+
help='number of epochs to train (default: 50)')
|
| 65 |
+
parser.add_argument('--lr', type=float, default=0.001,
|
| 66 |
+
help='learning rate (default: 0.001)')
|
| 67 |
+
parser.add_argument('--decay', type=float, default=0.0005,
|
| 68 |
+
help='weight decay (default: 0.0005)')
|
| 69 |
+
parser.add_argument('--warm-steps', type=int, dest='warm_steps', default=0,
|
| 70 |
+
help='number of warm start steps for learning rate (default: 10)')
|
| 71 |
+
parser.add_argument('--patience', type=int, default=10,
|
| 72 |
+
help='patience for early stopping (default: 10)')
|
| 73 |
+
parser.add_argument('--pretrain', type=str, dest='pretrain', default='',
|
| 74 |
+
help='path of the pretrain model') # /home/duadua/Desktop/fetal/3dpretrain/runs/e50.pth
|
| 75 |
+
parser.add_argument('--metric-avg', type=str, dest='metric_avg', default='macro',
|
| 76 |
+
help='metric average type')
|
| 77 |
+
|
| 78 |
+
# args for losses
|
| 79 |
+
parser.add_argument('--loss', type=str, default='bce',
|
| 80 |
+
help='loss function (mlce, sl, mix)')
|
| 81 |
+
|
| 82 |
+
parser.add_argument('--bias-curri', dest='bias_curri', action='store_true', default=False,
|
| 83 |
+
help='directly use loss as the training data (biased) or not (unbiased)')
|
| 84 |
+
parser.add_argument('--anti-curri', dest='anti_curri', action='store_true', default=False,
|
| 85 |
+
help='easy to hard (curri), hard to easy (anti)')
|
| 86 |
+
parser.add_argument('--std-coff', dest='std_coff', type=float, default=1,
|
| 87 |
+
help='the hyper-parameter of std')
|
| 88 |
+
|
| 89 |
+
args = parser.parse_args()
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
def main():
|
| 93 |
+
set_seed(args.seed)
|
| 94 |
+
|
| 95 |
+
if args.task_type == 'slc':
|
| 96 |
+
if args.task == 'all':
|
| 97 |
+
raise ValueError('Choose one task number to run single label classification')
|
| 98 |
+
args.classes = 1
|
| 99 |
+
elif args.task_type == 'mlc':
|
| 100 |
+
pass
|
| 101 |
+
else:
|
| 102 |
+
raise NotImplementedError
|
| 103 |
+
|
| 104 |
+
if args.loss == 'mlce' and args.task_type != 'slc':
|
| 105 |
+
criterion = MLCE()
|
| 106 |
+
elif args.loss == "bce" or args.task_type == 'slc':
|
| 107 |
+
args.loss = "bce"
|
| 108 |
+
criterion = nn.BCEWithLogitsLoss()
|
| 109 |
+
else:
|
| 110 |
+
criterion = 0
|
| 111 |
+
pass
|
| 112 |
+
weight_dir = "./run/" + str(args.exclude_feature) + '-' + args.loss + str(args.batch_size) + '-' + str(args.lr) + '-' + str(args.epochs) + args.pdb_src
|
| 113 |
+
if not os.path.exists(weight_dir):
|
| 114 |
+
os.makedirs(weight_dir)
|
| 115 |
+
|
| 116 |
+
logging.basicConfig(handlers=[
|
| 117 |
+
logging.FileHandler(filename=os.path.join(weight_dir, "training.log"), encoding='utf-8', mode='w+'),
|
| 118 |
+
logging.StreamHandler()],
|
| 119 |
+
format="%(asctime)s: %(message)s", datefmt="%F %T", level=logging.INFO)
|
| 120 |
+
|
| 121 |
+
logging.info(f'saving_dir: {weight_dir}')
|
| 122 |
+
|
| 123 |
+
with open(os.path.join(weight_dir, "config.json"), "w") as f:
|
| 124 |
+
f.write(json.dumps(vars(args)))
|
| 125 |
+
|
| 126 |
+
device = torch.device("cpu" if args.gpu == -1 or not torch.cuda.is_available() else f"cuda:{args.gpu}")
|
| 127 |
+
|
| 128 |
+
logging.info('Loading Training Dataset')
|
| 129 |
+
if args.task_type == 'mlc':
|
| 130 |
+
set_all = MDataset(threshold=args.threshold, mode='train', max_length=args.max_length, pdb_src=args.pdb_src, data_ver=args.data_ver, exclude_feature=args.exclude_feature)
|
| 131 |
+
# else:
|
| 132 |
+
# set_all = SDataset(threshold=args.threshold, mode='train', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src, data_ver=args.data_ver)
|
| 133 |
+
logging.info('Loading Test Dataset')
|
| 134 |
+
if args.task_type == 'mlc':
|
| 135 |
+
qlx_set = MDataset(threshold=args.threshold, mode='qlx', max_length=args.max_length, pdb_src=args.pdb_src, exclude_feature=args.exclude_feature)
|
| 136 |
+
saap_set = MDataset(threshold=args.threshold, mode='saap', max_length=args.max_length, pdb_src=args.pdb_src, exclude_feature=args.exclude_feature)
|
| 137 |
+
# else:
|
| 138 |
+
# qlx_set = SDataset(threshold=args.threshold, mode='qlx', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src)
|
| 139 |
+
# saap_set = SDataset(threshold=args.threshold, mode='saap', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src)
|
| 140 |
+
|
| 141 |
+
best_perform_list = [[] for i in range(5)]
|
| 142 |
+
qlx_perform_list = [[] for i in range(5)]
|
| 143 |
+
saap_perform_list = [[] for i in range(5)]
|
| 144 |
+
|
| 145 |
+
kf = KFold(n_splits=5, shuffle=True, random_state=42)
|
| 146 |
+
|
| 147 |
+
for fold, (train_idx, val_idx) in enumerate(kf.split(set_all)):
|
| 148 |
+
train_set= Subset(set_all, train_idx)
|
| 149 |
+
valid_set = Subset(set_all, val_idx)
|
| 150 |
+
# if args.loss == 'bce':
|
| 151 |
+
# weight = torch.Tensor([0] * args.classes)
|
| 152 |
+
# for i in train_set:
|
| 153 |
+
# weight += i[2]
|
| 154 |
+
# weight = (len(train_set) - weight) / weight
|
| 155 |
+
# criterion.register_buffer('pos_weight', weight.to(device))
|
| 156 |
+
|
| 157 |
+
train_loader = DataLoader(train_set, batch_size=args.batch_size, follow_batch=['x_s'], shuffle=True, drop_last=True)
|
| 158 |
+
valid_loader = DataLoader(valid_set, batch_size=args.batch_size, follow_batch=['x_s'], shuffle=False)
|
| 159 |
+
qlx_loader = DataLoader(qlx_set, batch_size=args.batch_size, follow_batch=['x_s'], shuffle=False)
|
| 160 |
+
saap_loader = DataLoader(saap_set, batch_size=args.batch_size, follow_batch=['x_s'], shuffle=False)
|
| 161 |
+
|
| 162 |
+
if args.model == 'mm':
|
| 163 |
+
model = GraphGNN(num_layer=args.num_layer, input_dim=set_all.num_features, emb_dim=args.emb_dim, out_dim=set_all.num_classes, JK="last",
|
| 164 |
+
drop_ratio=args.dropout_ratio, graph_pooling=args.graph_pooling, gnn_type=args.gnn_type)
|
| 165 |
+
|
| 166 |
+
if len(args.pretrain) != 0:
|
| 167 |
+
logging.info('loading pretrain model')
|
| 168 |
+
# model = load_pretrain_model(model, torch.load(args.pretrain))
|
| 169 |
+
model_state = model.state_dict()
|
| 170 |
+
pretrained_state = torch.load(args.pretrain)
|
| 171 |
+
pretrained_state = {k: v for k, v in pretrained_state.items() if
|
| 172 |
+
k in model_state and v.size() == model_state[k].size()}
|
| 173 |
+
model_state.update(pretrained_state)
|
| 174 |
+
model.load_state_dict(model_state)
|
| 175 |
+
# model.load_state_dict(torch.load(args.pretrain), strict=False)
|
| 176 |
+
model.to(device)
|
| 177 |
+
# optimizer = torch.optim.Adam(model.parameters())
|
| 178 |
+
optimizer = torch.optim.AdamW(model.parameters(), lr=args.lr)
|
| 179 |
+
# optimizer = torch.optim.SGD(model.parameters(), lr=args.lr, momentum=True, weight_decay=5e-5)
|
| 180 |
+
weights_path = f"{weight_dir}/model_{fold + 1}.pth"
|
| 181 |
+
# early_stopping = EarlyStopping(patience=args.patience, path=weights_path)
|
| 182 |
+
logging.info(f'Running Cross Validation {fold + 1}')
|
| 183 |
+
logging.info(f'Fold {fold + 1} Train set:{len(train_set)}, Valid set:{len(valid_set)}, Test set:qlx {len(qlx_set)} saap {len(saap_set)}')
|
| 184 |
+
best_metric = 0
|
| 185 |
+
best_qlx = 0
|
| 186 |
+
best_saap = 0
|
| 187 |
+
start_time = time.time()
|
| 188 |
+
|
| 189 |
+
for epoch in range(1, args.epochs + 1):
|
| 190 |
+
if args.task_type in ('mlc', 'slc') :
|
| 191 |
+
train_loss, ap, f1, acc, auc = train(args, epoch, model, train_loader, valid_loader, device, criterion, optimizer)
|
| 192 |
+
logging.info(f'Epoch: {epoch:03d}, Train Loss: {train_loss:.3f}, ap: {ap:.3f}, f1: {f1:.3f}, acc: {acc:.3f}, auc: {auc:.3f}')
|
| 193 |
+
avg_metric = ap + f1 + acc + auc
|
| 194 |
+
if avg_metric > best_metric:
|
| 195 |
+
logging.info(f'Epoch: {epoch:03d} New Best validation metrics, running test session')
|
| 196 |
+
torch.save(model.state_dict(), weights_path)
|
| 197 |
+
best_metric = avg_metric
|
| 198 |
+
best_perform_list[fold] = np.asarray([ap, f1, acc, auc])
|
| 199 |
+
|
| 200 |
+
_, qlx_ap, qlx_f1, qlx_acc, qlx_auc = train(args, epoch, model, None, qlx_loader, device, None, None)
|
| 201 |
+
logging.info(f'Epoch: {epoch:03d} QLX results, ap: {qlx_ap:.3f}, f1: {qlx_f1:.3f}, acc: {qlx_acc:.3f}, auc: {qlx_auc:.3f}')
|
| 202 |
+
qlx_metric = qlx_ap + qlx_f1 + qlx_acc + qlx_auc
|
| 203 |
+
if qlx_metric > best_qlx:
|
| 204 |
+
best_qlx = qlx_metric
|
| 205 |
+
qlx_perform_list[fold] = np.asarray([qlx_ap, qlx_f1, qlx_acc, qlx_auc])
|
| 206 |
+
|
| 207 |
+
_, saap_ap, saap_f1, saap_acc, saap_auc = train(args, epoch, model, None, saap_loader, device, None, None)
|
| 208 |
+
logging.info(f'Epoch: {epoch:03d} SAAP results, ap: {saap_ap:.3f}, f1: {saap_f1:.3f}, acc: {saap_acc:.3f}, auc: {saap_auc:.3f}')
|
| 209 |
+
saap_metric = saap_ap + saap_f1 + saap_acc + saap_auc
|
| 210 |
+
if saap_metric > best_saap:
|
| 211 |
+
best_saap = saap_metric
|
| 212 |
+
saap_perform_list[fold] = np.asarray([saap_ap, saap_f1, saap_acc, saap_auc])
|
| 213 |
+
|
| 214 |
+
else:
|
| 215 |
+
raise NotImplementedError
|
| 216 |
+
|
| 217 |
+
logging.info(f'used time {(time.time()-start_time)/3600:.2f}h')
|
| 218 |
+
|
| 219 |
+
logging.info(f'Cross Validation Finished!')
|
| 220 |
+
best_perform_list = np.asarray(best_perform_list)
|
| 221 |
+
qlx_perform_list = np.asarray(qlx_perform_list)
|
| 222 |
+
saap_perform_list = np.asarray(saap_perform_list)
|
| 223 |
+
logging.info('Best validation perform list\n%s', best_perform_list)
|
| 224 |
+
logging.info('mean: %s', np.round(np.mean(best_perform_list, 0), 3))
|
| 225 |
+
logging.info('std: %s', np.round(np.std(best_perform_list, 0), 3))
|
| 226 |
+
logging.info('Best qlx perform list\n%s', qlx_perform_list)
|
| 227 |
+
logging.info('mean: %s', np.round(np.mean(qlx_perform_list, 0), 3))
|
| 228 |
+
logging.info('std: %s', np.round(np.std(qlx_perform_list, 0), 3))
|
| 229 |
+
logging.info('Best saap perform list\n%s', saap_perform_list)
|
| 230 |
+
logging.info('mean: %s', np.round(np.mean(saap_perform_list, 0), 3))
|
| 231 |
+
logging.info('std: %s', np.round(np.std(saap_perform_list, 0), 3))
|
| 232 |
+
perform = open(weight_dir+'/result.txt', 'w')
|
| 233 |
+
perform.write('Valid\n')
|
| 234 |
+
perform.write(','.join([str(i) for i in np.mean(best_perform_list, 0)])+'\n')
|
| 235 |
+
perform.write(','.join([str(i) for i in np.std(best_perform_list, 0)])+'\n')
|
| 236 |
+
perform.write('qlx\n')
|
| 237 |
+
perform.write(','.join([str(i) for i in np.mean(qlx_perform_list, 0)])+'\n')
|
| 238 |
+
perform.write(','.join([str(i) for i in np.std(qlx_perform_list, 0)])+'\n')
|
| 239 |
+
perform.write('saap\n')
|
| 240 |
+
perform.write(','.join([str(i) for i in np.mean(saap_perform_list, 0)])+'\n')
|
| 241 |
+
perform.write(','.join([str(i) for i in np.std(saap_perform_list, 0)])+'\n')
|
| 242 |
+
|
| 243 |
+
|
| 244 |
+
if __name__ == "__main__":
|
| 245 |
+
main()
|
graphabl/network.py
ADDED
|
@@ -0,0 +1,367 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
import torch.nn.functional as F
|
| 4 |
+
import numpy as np
|
| 5 |
+
from torch.nn import Sequential, Linear, ReLU
|
| 6 |
+
from torch_geometric.nn import GraphConv, GINConv, GATConv, SAGEConv, GPSConv, GINEConv, GATv2Conv
|
| 7 |
+
from torch_geometric.nn import global_mean_pool, global_add_pool, global_max_pool, GlobalAttention, Set2Set, MulAggregation
|
| 8 |
+
from torch_geometric.nn import aggr
|
| 9 |
+
from torch_geometric.nn import GraphNorm
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
import torch
|
| 13 |
+
import torch.nn as nn
|
| 14 |
+
import torch.nn.functional as F
|
| 15 |
+
|
| 16 |
+
class SimpleSelfAttention(nn.Module):
|
| 17 |
+
def __init__(self, embedding_dim, num_heads=1):
|
| 18 |
+
super(SimpleSelfAttention, self).__init__()
|
| 19 |
+
self.embedding_dim = embedding_dim
|
| 20 |
+
self.num_heads = num_heads
|
| 21 |
+
|
| 22 |
+
# Assuming key_channels = value_channels = embedding_dim
|
| 23 |
+
self.key_channels = self.embedding_dim
|
| 24 |
+
self.value_channels = self.embedding_dim
|
| 25 |
+
|
| 26 |
+
# Linear layers for queries, keys, and values
|
| 27 |
+
self.query = nn.Linear(embedding_dim, self.key_channels * num_heads)
|
| 28 |
+
self.key = nn.Linear(embedding_dim, self.key_channels * num_heads)
|
| 29 |
+
self.value = nn.Linear(embedding_dim, self.value_channels * num_heads)
|
| 30 |
+
|
| 31 |
+
# Output projection layer
|
| 32 |
+
self.proj = nn.Linear(self.value_channels * num_heads, embedding_dim)
|
| 33 |
+
|
| 34 |
+
# Scaling for dot-product attention
|
| 35 |
+
self.scale = nn.Parameter(torch.sqrt(torch.FloatTensor([self.key_channels // num_heads])))
|
| 36 |
+
|
| 37 |
+
def forward(self, x1, x2, x3):
|
| 38 |
+
# x1, x2, x3 shapes: [Batch_size, Embedding_dim]
|
| 39 |
+
# Stack the inputs along a new dimension (sequence dimension)
|
| 40 |
+
batch_size = x1.shape[0]
|
| 41 |
+
x = torch.stack((x1, x2, x3), dim=1) # [Batch_size, 3, Embedding_dim]
|
| 42 |
+
|
| 43 |
+
# Compute queries, keys, values for all three inputs
|
| 44 |
+
Q = self.query(x) # [Batch_size, 3, num_heads * embedding_dim]
|
| 45 |
+
K = self.key(x) # [Batch_size, 3, num_heads * embedding_dim]
|
| 46 |
+
V = self.value(x) # [Batch_size, 3, num_heads * embedding_dim]
|
| 47 |
+
|
| 48 |
+
# Reshape for multi-head attention
|
| 49 |
+
Q = Q.view(batch_size, -1, self.num_heads, self.embedding_dim).transpose(1, 2) # [Batch_size, num_heads, 3, embedding_dim]
|
| 50 |
+
K = K.view(batch_size, -1, self.num_heads, self.embedding_dim).transpose(1, 2) # [Batch_size, num_heads, 3, embedding_dim]
|
| 51 |
+
V = V.view(batch_size, -1, self.num_heads, self.embedding_dim).transpose(1, 2) # [Batch_size, num_heads, 3, embedding_dim]
|
| 52 |
+
|
| 53 |
+
# Calculate dot product attention
|
| 54 |
+
attention_scores = torch.matmul(Q, K.transpose(-2, -1)) / self.scale
|
| 55 |
+
attention = F.softmax(attention_scores, dim=-1)
|
| 56 |
+
|
| 57 |
+
# Apply attention to V
|
| 58 |
+
x = torch.matmul(attention, V) # [Batch_size, num_heads, 3, embedding_dim]
|
| 59 |
+
|
| 60 |
+
# Concatenate heads and put through final linear layer
|
| 61 |
+
x = x.transpose(1, 2).reshape(batch_size, -1, self.num_heads * self.embedding_dim)
|
| 62 |
+
x = self.proj(x) # [Batch_size, 3, embedding_dim]
|
| 63 |
+
|
| 64 |
+
# Sum the outputs from the three inputs
|
| 65 |
+
out = x.sum(dim=1) # [Batch_size, embedding_dim]
|
| 66 |
+
return out
|
| 67 |
+
|
| 68 |
+
def cosine_similarity(x,y):
|
| 69 |
+
num = x.dot(y.T)
|
| 70 |
+
denom = np.linalg.norm(x) * np.linalg.norm(y)
|
| 71 |
+
return num / denom
|
| 72 |
+
|
| 73 |
+
class LogCoshLoss(nn.Module):
|
| 74 |
+
def __init__(self):
|
| 75 |
+
super().__init__()
|
| 76 |
+
|
| 77 |
+
def forward(self, y_t, y_prime_t):
|
| 78 |
+
ey_t = y_t - y_prime_t
|
| 79 |
+
return torch.mean(torch.log(torch.cosh(ey_t + 1e-12)))
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
class WeightedMSELoss(nn.Module):
|
| 83 |
+
def __init__(self):
|
| 84 |
+
super().__init__()
|
| 85 |
+
|
| 86 |
+
def forward(self, y, y_t, weights=None):
|
| 87 |
+
loss = (y - y_t) ** 2
|
| 88 |
+
if weights is not None:
|
| 89 |
+
loss *= weights.expand_as(loss)
|
| 90 |
+
return torch.mean(loss)
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
class GNN(nn.Module):
|
| 94 |
+
def __init__(self, num_layer, input_dim, emb_dim, JK="last", drop_ratio=0, gnn_type="gin"):
|
| 95 |
+
super(GNN, self).__init__()
|
| 96 |
+
self.num_layer = num_layer
|
| 97 |
+
self.drop_ratio = drop_ratio
|
| 98 |
+
self.JK = JK
|
| 99 |
+
# self.fc2 = nn.Linear(200, 200)
|
| 100 |
+
self.gnns = torch.nn.ModuleList()
|
| 101 |
+
for layer in range(num_layer):
|
| 102 |
+
in_dim = input_dim if layer == 0 else emb_dim
|
| 103 |
+
if gnn_type == "gin":
|
| 104 |
+
# self.gnns.append(GINConv(nn.Sequential(nn.Linear(in_dim, emb_dim), nn.BatchNorm1d(emb_dim), nn.ReLU(),
|
| 105 |
+
# nn.Linear(emb_dim, emb_dim))))
|
| 106 |
+
self.gnns.append(GINConv(nn.Sequential(nn.Linear(in_dim, emb_dim), GraphNorm(emb_dim), nn.ReLU(),
|
| 107 |
+
nn.Linear(emb_dim, emb_dim), nn.ReLU())))
|
| 108 |
+
elif gnn_type == "gps":
|
| 109 |
+
nn_ = Sequential(
|
| 110 |
+
Linear(in_dim, emb_dim),
|
| 111 |
+
ReLU(),
|
| 112 |
+
Linear(emb_dim, emb_dim),
|
| 113 |
+
)
|
| 114 |
+
conv = GPSConv(emb_dim, GINEConv(nn_), heads=4)
|
| 115 |
+
self.gnns.append(conv)
|
| 116 |
+
elif gnn_type == "gcn":
|
| 117 |
+
self.gnns.append(GraphConv(in_dim, emb_dim))
|
| 118 |
+
elif gnn_type == "gat":
|
| 119 |
+
self.gnns.append(GATConv(in_dim, emb_dim))
|
| 120 |
+
elif gnn_type == "gatv2":
|
| 121 |
+
self.gnns.append(GATv2Conv(in_dim, emb_dim))
|
| 122 |
+
elif gnn_type == "graphsage":
|
| 123 |
+
self.gnns.append(SAGEConv(in_dim, emb_dim))
|
| 124 |
+
else:
|
| 125 |
+
raise ValueError("Invalid GNN type.")
|
| 126 |
+
|
| 127 |
+
def forward(self, x, edge_index, edge_attr=None):
|
| 128 |
+
h_list = [x]
|
| 129 |
+
mut_site = []
|
| 130 |
+
for layer in range(self.num_layer):
|
| 131 |
+
|
| 132 |
+
h = self.gnns[layer](h_list[layer], edge_index, edge_attr)
|
| 133 |
+
# if layer == self.num_layer - 1:
|
| 134 |
+
# # remove relu from the last layer
|
| 135 |
+
# h = F.dropout(h, self.drop_ratio, training=self.training)
|
| 136 |
+
# else:
|
| 137 |
+
# h = F.dropout(F.relu(h), self.drop_ratio, training=self.training) # F.relu()
|
| 138 |
+
h_list.append(h)
|
| 139 |
+
# if len(h_list) == 2:
|
| 140 |
+
# previous_mut_site_feature = h_list[-2][mut_res_idx]
|
| 141 |
+
# current_mut_site_feature = h_list[-1][mut_res_idx]
|
| 142 |
+
# # print(previous_mut_site_feature.shape, current_mut_site_feature.shape)
|
| 143 |
+
# h_feature = self.global_encoder(previous_mut_site_feature)
|
| 144 |
+
# h_list[-1][mut_res_idx] = h_feature + current_mut_site_feature
|
| 145 |
+
# if len(h_list) == 3:
|
| 146 |
+
# previous_mut_site_feature = h_list[-2][mut_res_idx].squeeze(0)
|
| 147 |
+
# current_mut_site_feature = h_list[-1][mut_res_idx].squeeze(0)
|
| 148 |
+
|
| 149 |
+
# h_feature = self.fc2(previous_mut_site_feature) + current_mut_site_feature
|
| 150 |
+
# h_list[-1][mut_res_idx] = h_feature.unsqueeze(0)
|
| 151 |
+
# mut_site.append()
|
| 152 |
+
# print(len(h_list))
|
| 153 |
+
if self.JK == "last":
|
| 154 |
+
node_representation = h_list[-1]
|
| 155 |
+
elif self.JK == "sum":
|
| 156 |
+
h_list = [h.unsqueeze_(0) for h in h_list]
|
| 157 |
+
node_representation = torch.sum(torch.cat(h_list[1:], dim=0), dim=0)
|
| 158 |
+
# print('node_rep', node_representation.shape)
|
| 159 |
+
return h_list[-1]
|
| 160 |
+
|
| 161 |
+
|
| 162 |
+
# orthogonal initialization
|
| 163 |
+
def init_gru_orth(model, gain=1):
|
| 164 |
+
model.reset_parameters()
|
| 165 |
+
# orthogonal initialization of gru weights
|
| 166 |
+
for _, hh, _, _ in model.all_weights:
|
| 167 |
+
for i in range(0, hh.size(0), model.hidden_size):
|
| 168 |
+
torch.nn.init.orthogonal_(hh[i:i + model.hidden_size], gain=gain)
|
| 169 |
+
|
| 170 |
+
|
| 171 |
+
def init_lstm_orth(model, gain=1):
|
| 172 |
+
init_gru_orth(model, gain)
|
| 173 |
+
|
| 174 |
+
# positive forget gate bias (Jozefowicz es at. 2015)
|
| 175 |
+
for _, _, ih_b, hh_b in model.all_weights:
|
| 176 |
+
l = len(ih_b)
|
| 177 |
+
ih_b[l // 4: l // 2].data.fill_(1.0)
|
| 178 |
+
hh_b[l // 4: l // 2].data.fill_(1.0)
|
| 179 |
+
|
| 180 |
+
|
| 181 |
+
class GraphGNN(nn.Module):
|
| 182 |
+
def __init__(self, num_layer, input_dim, emb_dim, out_dim, JK="last", drop_ratio=0.5, graph_pooling="attention",
|
| 183 |
+
gnn_type="gat", concat_type=None, fds=False, feature_level='both', contrast_curri=False) -> object:
|
| 184 |
+
super(GraphGNN, self).__init__()
|
| 185 |
+
self.num_layer = num_layer
|
| 186 |
+
self.drop_ratio = drop_ratio
|
| 187 |
+
self.JK = JK
|
| 188 |
+
self.input_dim = input_dim
|
| 189 |
+
self.emb_dim = emb_dim
|
| 190 |
+
self.out_dim = out_dim
|
| 191 |
+
self.concat_type = concat_type
|
| 192 |
+
self.feature_level = feature_level
|
| 193 |
+
self.contrast_curri = contrast_curri
|
| 194 |
+
|
| 195 |
+
self.fc = nn.Sequential(
|
| 196 |
+
nn.Linear(self.emb_dim, self.emb_dim), nn.LeakyReLU(0.1), nn.Dropout(p=self.drop_ratio), nn.Linear(self.emb_dim, self.out_dim))
|
| 197 |
+
|
| 198 |
+
self.gnn = GNN(num_layer, input_dim, emb_dim, JK, drop_ratio, gnn_type=gnn_type)
|
| 199 |
+
|
| 200 |
+
if graph_pooling == "sum":
|
| 201 |
+
self.pool = global_add_pool
|
| 202 |
+
elif graph_pooling == "mean":
|
| 203 |
+
self.pool = global_mean_pool
|
| 204 |
+
elif graph_pooling == "max":
|
| 205 |
+
self.pool = global_max_pool
|
| 206 |
+
elif graph_pooling == "mul":
|
| 207 |
+
self.pool = MulAggregation()
|
| 208 |
+
elif graph_pooling == "attention":
|
| 209 |
+
self.pool = GlobalAttention(gate_nn=torch.nn.Linear(emb_dim, 1))
|
| 210 |
+
elif graph_pooling == "set2set":
|
| 211 |
+
self.pool = Set2Set(emb_dim, processing_steps=2)
|
| 212 |
+
elif graph_pooling == "lstm":
|
| 213 |
+
self.pool = aggr.LSTMAggregation(emb_dim, emb_dim)
|
| 214 |
+
else:
|
| 215 |
+
raise ValueError("Invalid graph pooling type.")
|
| 216 |
+
|
| 217 |
+
def forward_once(self, x, edge_index, batch):
|
| 218 |
+
node_representation = self.gnn(x, edge_index)
|
| 219 |
+
graph_rep = self.pool(node_representation, batch)
|
| 220 |
+
return graph_rep
|
| 221 |
+
|
| 222 |
+
def forward(self, data):
|
| 223 |
+
graph_rep_be = self.forward_once(data.x_s, data.edge_index_s, data.x_s_batch)
|
| 224 |
+
x = self.fc(graph_rep_be)
|
| 225 |
+
return x
|
| 226 |
+
|
| 227 |
+
|
| 228 |
+
|
| 229 |
+
class MMGraph(nn.Module):
|
| 230 |
+
def __init__(self, num_layer, input_dim, emb_dim, out_dim, JK="last", drop_ratio=0.5, graph_pooling="attention",
|
| 231 |
+
gnn_type="gat", concat_type=None, fds=False, feature_level='both', contrast_curri=False, max_length=50) -> object:
|
| 232 |
+
super(MMGraph, self).__init__()
|
| 233 |
+
self.num_layer = num_layer
|
| 234 |
+
self.drop_ratio = drop_ratio
|
| 235 |
+
self.JK = JK
|
| 236 |
+
self.input_dim = input_dim
|
| 237 |
+
self.emb_dim = emb_dim
|
| 238 |
+
self.out_dim = out_dim
|
| 239 |
+
self.concat_type = concat_type
|
| 240 |
+
self.feature_level = feature_level
|
| 241 |
+
self.contrast_curri = contrast_curri
|
| 242 |
+
|
| 243 |
+
self.graph_pool = nn.Linear(self.emb_dim, 1)
|
| 244 |
+
|
| 245 |
+
self.fc = nn.Sequential(
|
| 246 |
+
nn.Linear(self.emb_dim, self.emb_dim), nn.LeakyReLU(0.1), nn.Dropout(p=self.drop_ratio),
|
| 247 |
+
nn.Linear(self.emb_dim, self.out_dim))
|
| 248 |
+
|
| 249 |
+
if fds:
|
| 250 |
+
self.dir = True
|
| 251 |
+
else:
|
| 252 |
+
self.dir = False
|
| 253 |
+
self.global_encoder = nn.Sequential(nn.Linear(10, self.emb_dim), nn.LeakyReLU(0.1), nn.Dropout(p=self.drop_ratio),)
|
| 254 |
+
self.seq_encoder = nn.Sequential(
|
| 255 |
+
nn.Linear(max_length, self.emb_dim), nn.LeakyReLU(0.1), nn.Dropout(p=self.drop_ratio),
|
| 256 |
+
)
|
| 257 |
+
self.gnn = GNN(num_layer, input_dim, emb_dim, JK, drop_ratio, gnn_type=gnn_type)
|
| 258 |
+
|
| 259 |
+
if graph_pooling == "sum":
|
| 260 |
+
self.pool = global_add_pool
|
| 261 |
+
elif graph_pooling == "mean":
|
| 262 |
+
self.pool = global_mean_pool
|
| 263 |
+
elif graph_pooling == "max":
|
| 264 |
+
self.pool = global_max_pool
|
| 265 |
+
elif graph_pooling == "mul":
|
| 266 |
+
self.pool = MulAggregation()
|
| 267 |
+
elif graph_pooling == "attention":
|
| 268 |
+
self.pool = GlobalAttention(gate_nn=torch.nn.Linear(emb_dim, 1))
|
| 269 |
+
elif graph_pooling == "set2set":
|
| 270 |
+
self.pool = Set2Set(emb_dim, processing_steps=2)
|
| 271 |
+
elif graph_pooling == "lstm":
|
| 272 |
+
self.pool = aggr.LSTMAggregation(emb_dim, emb_dim)
|
| 273 |
+
else:
|
| 274 |
+
raise ValueError("Invalid graph pooling type.")
|
| 275 |
+
self.att = SimpleSelfAttention(emb_dim, num_heads=4)
|
| 276 |
+
|
| 277 |
+
def forward_once(self, x, edge_index, batch):
|
| 278 |
+
node_representation = self.gnn(x, edge_index)
|
| 279 |
+
graph_rep = self.pool(node_representation, batch)
|
| 280 |
+
return graph_rep
|
| 281 |
+
|
| 282 |
+
def forward(self, data):
|
| 283 |
+
seq1, global_1 = data.seq, data.global_f
|
| 284 |
+
device = self.graph_pool.bias.device
|
| 285 |
+
seq1 = torch.tensor(seq1, dtype=torch.float, device=device)
|
| 286 |
+
global_1 = torch.tensor(global_1, dtype=torch.float, device=device)
|
| 287 |
+
|
| 288 |
+
graph_rep_be = self.forward_once(data.x_s, data.edge_index_s, data.x_s_batch)
|
| 289 |
+
seq1_rep_be = self.seq_encoder(seq1)
|
| 290 |
+
global1 = self.global_encoder(global_1)
|
| 291 |
+
|
| 292 |
+
a1 = self.att(graph_rep_be, seq1_rep_be, global1)
|
| 293 |
+
return self.fc(a1)
|
| 294 |
+
|
| 295 |
+
|
| 296 |
+
|
| 297 |
+
class PMMGraph(nn.Module):
|
| 298 |
+
def __init__(self, num_layer, input_dim, emb_dim, out_dim, JK="last", drop_ratio=0.5, graph_pooling="attention",
|
| 299 |
+
gnn_type="gat", concat_type=None, fds=False, feature_level='both', contrast_curri=False):
|
| 300 |
+
super(PMMGraph, self).__init__()
|
| 301 |
+
self.num_layer = num_layer
|
| 302 |
+
self.drop_ratio = drop_ratio
|
| 303 |
+
self.JK = JK
|
| 304 |
+
self.input_dim = input_dim
|
| 305 |
+
self.emb_dim = emb_dim
|
| 306 |
+
self.out_dim = out_dim
|
| 307 |
+
self.concat_type = concat_type
|
| 308 |
+
self.feature_level = feature_level
|
| 309 |
+
self.contrast_curri = contrast_curri
|
| 310 |
+
|
| 311 |
+
# Define the learnable prompt token
|
| 312 |
+
self.prompt_token = nn.Parameter(torch.randn(1, 10))
|
| 313 |
+
|
| 314 |
+
self.graph_pool = nn.Linear(self.emb_dim, 1)
|
| 315 |
+
|
| 316 |
+
self.fc = nn.Sequential(
|
| 317 |
+
nn.Linear(self.emb_dim + 10, self.emb_dim), # Adjust input size to include the prompt token
|
| 318 |
+
nn.LeakyReLU(0.1), nn.Dropout(p=self.drop_ratio),
|
| 319 |
+
nn.Linear(self.emb_dim, self.out_dim))
|
| 320 |
+
|
| 321 |
+
self.dir = fds
|
| 322 |
+
self.global_encoder = nn.Sequential(nn.Linear(10, self.emb_dim), nn.LeakyReLU(0.1), nn.Dropout(p=self.drop_ratio))
|
| 323 |
+
self.seq_encoder = nn.Sequential(
|
| 324 |
+
nn.Linear(30, self.emb_dim), nn.LeakyReLU(0.1), nn.Dropout(p=self.drop_ratio),
|
| 325 |
+
)
|
| 326 |
+
self.gnn = GNN(num_layer, input_dim, emb_dim, JK, drop_ratio, gnn_type=gnn_type)
|
| 327 |
+
|
| 328 |
+
# Initialize pooling based on the specified type
|
| 329 |
+
if graph_pooling in ["sum", "mean", "max", "mul", "attention", "set2set", "lstm"]:
|
| 330 |
+
pooling_classes = {
|
| 331 |
+
"sum": global_add_pool,
|
| 332 |
+
"mean": global_mean_pool,
|
| 333 |
+
"max": global_max_pool,
|
| 334 |
+
"mul": aggr.MulAggregation(),
|
| 335 |
+
"attention": GlobalAttention(gate_nn=torch.nn.Linear(emb_dim, 1)),
|
| 336 |
+
"set2set": Set2Set(emb_dim, processing_steps=2),
|
| 337 |
+
"lstm": aggr.LSTMAggregation(emb_dim, emb_dim)
|
| 338 |
+
}
|
| 339 |
+
self.pool = pooling_classes[graph_pooling]
|
| 340 |
+
else:
|
| 341 |
+
raise ValueError("Invalid graph pooling type.")
|
| 342 |
+
|
| 343 |
+
self.att = SimpleSelfAttention(emb_dim + 10, num_heads=4) # Adjust for prompt dimension
|
| 344 |
+
|
| 345 |
+
def forward_once(self, x, edge_index, batch):
|
| 346 |
+
node_representation = self.gnn(x, edge_index)
|
| 347 |
+
graph_rep = self.pool(node_representation, batch)
|
| 348 |
+
# Concatenate the prompt token
|
| 349 |
+
graph_rep = torch.cat([graph_rep, self.prompt_token.expand(graph_rep.size(0), -1)], dim=1)
|
| 350 |
+
return graph_rep
|
| 351 |
+
|
| 352 |
+
def forward(self, data):
|
| 353 |
+
seq1 = torch.tensor(np.array(data.seq, dtype=np.float32)).to(device='cuda')
|
| 354 |
+
global_1 = torch.tensor(np.array(data.global_f, dtype=np.float32)).to(device='cuda')
|
| 355 |
+
|
| 356 |
+
graph_rep_be = self.forward_once(data.x_s, data.edge_index_s, data.x_s_batch)
|
| 357 |
+
seq1_rep_be = self.seq_encoder(seq1)
|
| 358 |
+
global1_rep = self.global_encoder(global_1)
|
| 359 |
+
# Concatenate the prompt token to other representations as well
|
| 360 |
+
seq1_rep_be = torch.cat([seq1_rep_be, self.prompt_token.expand(seq1_rep_be.size(0), -1)], dim=1)
|
| 361 |
+
global1_rep = torch.cat([global1_rep, self.prompt_token.expand(global1_rep.size(0), -1)], dim=1)
|
| 362 |
+
|
| 363 |
+
# Process combined representations
|
| 364 |
+
a1 = self.att(graph_rep_be, seq1_rep_be, global1_rep)
|
| 365 |
+
return self.fc(a1)
|
| 366 |
+
|
| 367 |
+
|
graphabl/run/0-bce256-0.001-50af/config.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
{"model": "mm", "fusion": "1", "num_layer": 2, "emb_dim": 128, "dropout_ratio": 0.3, "graph_pooling": "attention", "gnn_type": "gat", "pdb_src": "af", "task_type": "mlc", "data_ver": "0920", "task": "all", "classes": 6, "max_length": 30, "split": 5, "seed": 1, "threshold": 128, "exclude_feature": 0, "gpu": 0, "batch_size": 256, "epochs": 50, "lr": 0.001, "decay": 0.0005, "warm_steps": 0, "patience": 10, "pretrain": "", "metric_avg": "macro", "loss": "bce", "bias_curri": false, "anti_curri": false, "std_coff": 1}
|
graphabl/run/0-bce256-0.001-50af/model_1.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:dad7f2bb88a59092b90fca5321988f9238c36346ea1be00f08c636e87f5fa6ce
|
| 3 |
+
size 154796
|
graphabl/run/0-bce256-0.001-50af/model_2.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:740b0e562b26bd5f080c6f0f02f123c75b34a8ce745daba51fbb9a99c3023a4f
|
| 3 |
+
size 154796
|
graphabl/run/0-bce256-0.001-50af/model_3.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:848cc853139d38d15b4d84d171d75619ff1dada05448445d22e4de77e6de6d7b
|
| 3 |
+
size 154796
|
graphabl/run/0-bce256-0.001-50af/model_4.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:985f68a84bb7262181f3c13c9fe2bccf093dc2b09433b102bb7535c228e0a98e
|
| 3 |
+
size 154796
|
graphabl/run/0-bce256-0.001-50af/model_5.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:f36416aeba32de0bf3ac631298a20089fb8e1cce4828ba7efa7d47337aaea425
|
| 3 |
+
size 154796
|
graphabl/run/0-bce256-0.001-50af/result.txt
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Valid
|
| 2 |
+
0.8311869859695434,0.7992442131042481,0.7261005401611328,0.7632609367370605
|
| 3 |
+
0.01089897049504037,0.006706284424244486,0.008409664915215989,0.009405898938088679
|
| 4 |
+
qlx
|
| 5 |
+
0.6402872443199158,0.708274245262146,0.5708609342575073,0.6535321354866028
|
| 6 |
+
0.012465302588486486,0.003057944969321764,0.006488706331848968,0.008808348470467832
|
| 7 |
+
saap
|
| 8 |
+
0.599161159992218,0.5113322138786316,0.40466665625572207,0.6856127262115479
|
| 9 |
+
0.027291274786404338,0.0011573248233569527,0.00985450937591801,0.027237991254309084
|
graphabl/run/1-bce256-0.001-50af/config.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
{"model": "mm", "fusion": "1", "num_layer": 2, "emb_dim": 128, "dropout_ratio": 0.3, "graph_pooling": "attention", "gnn_type": "gat", "pdb_src": "af", "task_type": "mlc", "data_ver": "0920", "task": "all", "classes": 6, "max_length": 30, "split": 5, "seed": 1, "threshold": 128, "exclude_feature": 1, "gpu": 1, "batch_size": 256, "epochs": 50, "lr": 0.001, "decay": 0.0005, "warm_steps": 0, "patience": 10, "pretrain": "", "metric_avg": "macro", "loss": "bce", "bias_curri": false, "anti_curri": false, "std_coff": 1}
|
graphabl/run/1-bce256-0.001-50af/model_1.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:983cb35e257c0cd54583a802d4be899b066d3aac69916879d8521fab756f7011
|
| 3 |
+
size 154796
|
graphabl/run/1-bce256-0.001-50af/model_2.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:c4e0885e5114ab118f98dc452f691753a1f1570c1bf11eff43b7605391d77ed5
|
| 3 |
+
size 154796
|
graphabl/run/1-bce256-0.001-50af/model_3.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:c6314642ce4a6f7b63adeeb1b4c7e985add18565cb7648afc172c7044d57a8bc
|
| 3 |
+
size 154796
|
graphabl/run/1-bce256-0.001-50af/model_4.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:a26e91e9415a4ca7dadb1372331f7f79258b64bdcd4d7a1d62ef94fc21c50f04
|
| 3 |
+
size 154796
|
graphabl/run/1-bce256-0.001-50af/model_5.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:7331bbf91e11dd08e8409ef0cf5064be381bbcfb7a611529afb65a04c08c7a6e
|
| 3 |
+
size 154796
|
graphabl/run/1-bce256-0.001-50af/result.txt
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Valid
|
| 2 |
+
0.8527601003646851,0.8259660601615906,0.7641097426414489,0.7995261788368225
|
| 3 |
+
0.0059770813086121736,0.005005334075867127,0.0075221584213969106,0.00905588109790848
|
| 4 |
+
qlx
|
| 5 |
+
0.8474146246910095,0.7568915486335754,0.6677704215049743,0.882286548614502
|
| 6 |
+
0.006088750075017117,0.005074444981365584,0.009391623656924189,0.00546507473652238
|
| 7 |
+
saap
|
| 8 |
+
0.5873079895973206,0.5063873946666717,0.46200000047683715,0.6355118036270142
|
| 9 |
+
0.01626200352905642,0.014330853382268484,0.03196179334254133,0.01651862039748566
|
graphabl/run/2-bce256-0.001-50af/config.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
{"model": "mm", "fusion": "1", "num_layer": 2, "emb_dim": 128, "dropout_ratio": 0.3, "graph_pooling": "attention", "gnn_type": "gat", "pdb_src": "af", "task_type": "mlc", "data_ver": "0920", "task": "all", "classes": 6, "max_length": 30, "split": 5, "seed": 1, "threshold": 128, "exclude_feature": 2, "gpu": 1, "batch_size": 256, "epochs": 50, "lr": 0.001, "decay": 0.0005, "warm_steps": 0, "patience": 10, "pretrain": "", "metric_avg": "macro", "loss": "bce", "bias_curri": false, "anti_curri": false, "std_coff": 1}
|
graphabl/run/2-bce256-0.001-50af/model_1.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:ae523c84d47ceb99f0d6a262f66d165b2611348e079a93cbc0fea6561fba589b
|
| 3 |
+
size 163500
|
graphabl/run/2-bce256-0.001-50af/model_2.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:365f99c2d2553b2913ee8919da1bd8143619680a074ab9bfac16c2af9aabdeeb
|
| 3 |
+
size 163500
|
graphabl/run/2-bce256-0.001-50af/model_3.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:de1fea2b6cba7c3ec405f94e0a01b62da2aa55677e81b88e6d6c94804c0d8fc3
|
| 3 |
+
size 163500
|
graphabl/run/2-bce256-0.001-50af/model_4.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:617a27e7d4a36795a9d53e0d55f57d21ff41e94fc2d7c4453471b6e0e2fcd201
|
| 3 |
+
size 163500
|
graphabl/run/2-bce256-0.001-50af/model_5.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:ba996cfae42f644da78337dcea5c716f549244f3370c6f85257899c8eade4bad
|
| 3 |
+
size 163500
|
graphabl/run/2-bce256-0.001-50af/result.txt
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Valid
|
| 2 |
+
0.8713407397270203,0.8358925104141235,0.7774726510047912,0.8197148442268372
|
| 3 |
+
0.009872133162932156,0.008417747308414294,0.010920918060023394,0.012654893910484355
|
| 4 |
+
qlx
|
| 5 |
+
0.808427631855011,0.7580655455589295,0.6690949201583862,0.8328594923019409
|
| 6 |
+
0.01449267758309804,0.03268245468668244,0.061416284002013204,0.019205095683731136
|
| 7 |
+
saap
|
| 8 |
+
0.5958592414855957,0.5154798030853271,0.43899999260902406,0.6548543810844422
|
| 9 |
+
0.026156405618067657,0.007699140763235296,0.040573662765624754,0.02752765483558861
|
graphabl/summary.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import csv
|
| 3 |
+
import argparse
|
| 4 |
+
|
| 5 |
+
parser = argparse.ArgumentParser()
|
| 6 |
+
parser.add_argument('--dir', type=str, default='./run')
|
| 7 |
+
parser.add_argument('--output', type=str, default='summary.csv')
|
| 8 |
+
args = parser.parse_args()
|
| 9 |
+
|
| 10 |
+
# 设置目录路径和输出CSV文件名
|
| 11 |
+
directory_path = args.dir
|
| 12 |
+
output_csv = args.output
|
| 13 |
+
|
| 14 |
+
# 创建CSV文件并写入数据
|
| 15 |
+
with open(output_csv, mode='w', newline='') as file:
|
| 16 |
+
writer = csv.writer(file)
|
| 17 |
+
|
| 18 |
+
# 写入第一个子表的标题和表头
|
| 19 |
+
writer.writerow(['QLX'])
|
| 20 |
+
writer.writerow(['Run', 'AP', 'F1', 'ACC', 'AUC'])
|
| 21 |
+
|
| 22 |
+
# 遍历指定目录中的子目录
|
| 23 |
+
for subdirectory in os.listdir(directory_path):
|
| 24 |
+
subdirectory_path = os.path.join(directory_path, subdirectory)
|
| 25 |
+
if os.path.isdir(subdirectory_path):
|
| 26 |
+
result_file_path = os.path.join(subdirectory_path, 'result.txt')
|
| 27 |
+
if os.path.isfile(result_file_path):
|
| 28 |
+
with open(result_file_path, 'r') as result_file:
|
| 29 |
+
lines = result_file.readlines()
|
| 30 |
+
if len(lines) >= 6:
|
| 31 |
+
means_5_6 = lines[4].strip().split(',')
|
| 32 |
+
std_devs_5_6 = lines[5].strip().split(',')
|
| 33 |
+
if len(means_5_6) == 4 and len(std_devs_5_6) == 4:
|
| 34 |
+
values_5_6 = [f'{float(mean.strip()) * 100:.2f}$_{{\\pm{{{float(std_dev.strip()) * 100:.2f}}}}}$'
|
| 35 |
+
for mean, std_dev in zip(means_5_6, std_devs_5_6)]
|
| 36 |
+
writer.writerow([subdirectory] + values_5_6)
|
| 37 |
+
|
| 38 |
+
# 加入空行以分隔两个子表
|
| 39 |
+
writer.writerow([])
|
| 40 |
+
|
| 41 |
+
# 写入第二个子表的标题和表头
|
| 42 |
+
writer.writerow(['SAAP'])
|
| 43 |
+
writer.writerow(['Run', 'AP', 'F1', 'ACC', 'AUC'])
|
| 44 |
+
|
| 45 |
+
# 再次遍历指定目录中的子目录
|
| 46 |
+
for subdirectory in os.listdir(directory_path):
|
| 47 |
+
subdirectory_path = os.path.join(directory_path, subdirectory)
|
| 48 |
+
if os.path.isdir(subdirectory_path):
|
| 49 |
+
result_file_path = os.path.join(subdirectory_path, 'result.txt')
|
| 50 |
+
if os.path.isfile(result_file_path):
|
| 51 |
+
with open(result_file_path, 'r') as result_file:
|
| 52 |
+
lines = result_file.readlines()
|
| 53 |
+
if len(lines) >= 9:
|
| 54 |
+
means_8_9 = lines[7].strip().split(',')
|
| 55 |
+
std_devs_8_9 = lines[8].strip().split(',')
|
| 56 |
+
if len(means_8_9) == 4 and len(std_devs_8_9) == 4:
|
| 57 |
+
values_8_9 = [f'{float(mean.strip()) * 100:.2f}$_{{\\pm{{{float(std_dev.strip()) * 100:.2f}}}}}$'
|
| 58 |
+
for mean, std_dev in zip(means_8_9, std_devs_8_9)]
|
| 59 |
+
writer.writerow([subdirectory] + values_8_9)
|
graphabl/train.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from torchmetrics import F1Score, Accuracy, AveragePrecision, AUROC
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
def train(args, epoch, model, train_loader, valid_loader, device, criterion, optimizer):
|
| 6 |
+
train_loss = 0
|
| 7 |
+
num_labels = model.out_dim
|
| 8 |
+
avg = args.metric_avg
|
| 9 |
+
if num_labels == 1:
|
| 10 |
+
task = 'binary'
|
| 11 |
+
else:
|
| 12 |
+
task = 'multilabel'
|
| 13 |
+
metric_macro_acc = Accuracy(average=avg, task=task, num_labels=num_labels, threshold=0.5).to(device)
|
| 14 |
+
metric_macro_f1 = F1Score(average=avg, task=task, num_labels=num_labels, threshold=0.5).to(device)
|
| 15 |
+
metric_macro_ap = AveragePrecision(average=avg, task=task, num_labels=num_labels).to(device)
|
| 16 |
+
metric_auc = AUROC(average=avg, task=task, num_labels=num_labels).to(device)
|
| 17 |
+
|
| 18 |
+
if train_loader is not None:
|
| 19 |
+
model.train()
|
| 20 |
+
for data in train_loader:
|
| 21 |
+
data = data.to(device)
|
| 22 |
+
# print(seq_lengths)
|
| 23 |
+
out = model(data)
|
| 24 |
+
# print(out[0])
|
| 25 |
+
# print(gt[0])
|
| 26 |
+
loss = criterion(out, torch.tensor(data.gt, dtype=torch.float, device=device))
|
| 27 |
+
# loss_0 = criterion(out[0], gt.to(device).float())
|
| 28 |
+
# loss_1 = criterion(out[1], gt.to(device).float())
|
| 29 |
+
# loss_2 = criterion(out[2], gt.to(device).float())
|
| 30 |
+
# loss = loss_0 + loss_1 + loss_2
|
| 31 |
+
loss.backward()
|
| 32 |
+
optimizer.step()
|
| 33 |
+
optimizer.zero_grad()
|
| 34 |
+
train_loss += loss.item()
|
| 35 |
+
|
| 36 |
+
model.eval()
|
| 37 |
+
preds = []
|
| 38 |
+
gt_list_valid = []
|
| 39 |
+
with torch.no_grad():
|
| 40 |
+
for data in valid_loader:
|
| 41 |
+
data = data.to(device)
|
| 42 |
+
gt_list_valid.append(torch.tensor(data.gt, device=device))
|
| 43 |
+
out = model(data)
|
| 44 |
+
preds.append(out)
|
| 45 |
+
|
| 46 |
+
# calculate metrics
|
| 47 |
+
preds = torch.nn.functional.sigmoid(torch.cat(preds, dim=0))
|
| 48 |
+
gt_list_valid = torch.cat(gt_list_valid, dim=0).int()
|
| 49 |
+
|
| 50 |
+
macro_ap = metric_macro_ap(preds, gt_list_valid).item()
|
| 51 |
+
# class_ap = [round(i.item(), 5) for i in metric_class_ap(preds, gt_list_valid)]
|
| 52 |
+
macro_auc = metric_auc(preds, gt_list_valid).item()
|
| 53 |
+
macro_f1 = metric_macro_f1(preds, gt_list_valid).item()
|
| 54 |
+
macro_acc = metric_macro_acc(preds, gt_list_valid).item()
|
| 55 |
+
return train_loss, macro_ap, macro_f1, macro_acc, macro_auc
|
| 56 |
+
|
graphabl/utils.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import random
|
| 2 |
+
import torch
|
| 3 |
+
import numpy as np
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def set_seed(seed):
|
| 7 |
+
random.seed(seed)
|
| 8 |
+
np.random.seed(seed)
|
| 9 |
+
torch.manual_seed(seed)
|
| 10 |
+
torch.cuda.manual_seed(seed)
|
| 11 |
+
torch.cuda.manual_seed_all(seed)
|
| 12 |
+
torch.backends.cudnn.deterministic = True
|
| 13 |
+
torch.backends.cudnn.benchmark = False
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def load_pretrain_model(net, weights):
|
| 17 |
+
net_keys = list(net.state_dict().keys())
|
| 18 |
+
weights_keys = list(weights.keys())
|
| 19 |
+
# assert(len(net_keys) <= len(weights_keys))
|
| 20 |
+
i = 0
|
| 21 |
+
j = 0
|
| 22 |
+
while i < len(net_keys) and j < len(weights_keys):
|
| 23 |
+
name_i = net_keys[i]
|
| 24 |
+
name_j = weights_keys[j]
|
| 25 |
+
if net.state_dict()[name_i].shape == weights[name_j].shape:
|
| 26 |
+
net.state_dict()[name_i].copy_(weights[name_j].cpu())
|
| 27 |
+
i += 1
|
| 28 |
+
j += 1
|
| 29 |
+
else:
|
| 30 |
+
i += 1
|
| 31 |
+
# print i, len(net_keys), j, len(weights_keys)
|
| 32 |
+
return net
|
pics/benchmark.png
ADDED
|
Git LFS Details
|
t3.1/dataset.py
ADDED
|
@@ -0,0 +1,260 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import torch
|
| 3 |
+
from Bio.PDB import PDBParser, is_aa
|
| 4 |
+
from Bio.PDB.Polypeptide import three_to_one
|
| 5 |
+
from torch.utils.data import Dataset
|
| 6 |
+
import pandas as pd
|
| 7 |
+
import numpy as np
|
| 8 |
+
from tqdm import tqdm
|
| 9 |
+
from Bio.Align import PairwiseAligner
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
aligner = PairwiseAligner()
|
| 13 |
+
# Define the scoring system
|
| 14 |
+
aligner.mode = 'global' # Global alignment
|
| 15 |
+
aligner.match_score = 1
|
| 16 |
+
aligner.mismatch_score = -1
|
| 17 |
+
aligner.open_gap_score = -0.5
|
| 18 |
+
aligner.extend_gap_score = -0.1
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def calculate_similarity(seq_gen, seq_tem):
|
| 22 |
+
|
| 23 |
+
# Perform the alignment
|
| 24 |
+
score = aligner.score(seq_gen, seq_tem)
|
| 25 |
+
return score / len(seq_gen)
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def parse_ll37_testset(path='metadata/LL37_v0.csv'):
|
| 29 |
+
files = open(path, 'r').readlines()
|
| 30 |
+
seqs = []
|
| 31 |
+
for file in files:
|
| 32 |
+
seqs.append(str(file.strip().split(',')[-1].strip().upper()))
|
| 33 |
+
return seqs
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def gmean(labels):
|
| 37 |
+
# Assuming 'labels' is a NumPy array of shape (n, m)
|
| 38 |
+
# Calculate the geometric mean across axis 1
|
| 39 |
+
|
| 40 |
+
# Take the product of elements along axis=1
|
| 41 |
+
product = np.prod(labels, axis=1)
|
| 42 |
+
|
| 43 |
+
# nth root of product, where n is the number of elements along axis=1
|
| 44 |
+
geometric_means = product ** (1.0 / labels.shape[1])
|
| 45 |
+
return geometric_means
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def clamp(n, smallest, largest):
|
| 49 |
+
return sorted([smallest, n, largest])[1]
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
ATOMS = {'H': 1, 'C': 12, 'N': 14, 'O': 16, 'S': 30}
|
| 53 |
+
ATOMS_R = {'H': 1, 'C': 1.5, 'N': 1.5, 'O': 1.5, 'S': 2}
|
| 54 |
+
AMINO_ACID_WATER = {'A': 255, 'V': 255, 'P': 255, 'F': 255, 'W': 255, 'I': 255, 'L': 255, 'G': 155, 'M': 155,
|
| 55 |
+
'Y': 55, 'S': 55, 'T': 55, 'C': 55, 'N': 55, 'Q': 55, 'D': 55, 'E': 55, 'K': 55, 'R': 55, 'H': 55}
|
| 56 |
+
AMINO_ACID_CHARGE = {'D': 55, 'E': 55, 'A': 155, 'V': 155, 'P': 155, 'F': 155, 'W': 155, 'I': 155, 'L': 155, 'G': 155,
|
| 57 |
+
'M': 155, 'Y': 155, 'S': 155, 'T': 155, 'C': 155, 'N': 155, 'Q': 155, 'K': 255, 'R': 255, 'H': 255}
|
| 58 |
+
# AMAs = {'G': 20, 'A': 1, 'V': 2, 'L': 3, 'I': 4, 'P': 5, 'F': 6, 'Y': 7, 'W': 8, 'S': 9, 'T': 10, 'C': 11,
|
| 59 |
+
# 'M': 12, 'N': 13, 'Q': 14, 'D': 15, 'E': 16, 'K': 17, 'R': 18, 'H': 19}
|
| 60 |
+
AMAs = {'G': 20, 'A': 1, 'V': 2, 'L': 3, 'I': 4, 'P': 5, 'F': 6, 'Y': 7, 'W': 8, 'S': 9, 'T': 10, 'C': 11,
|
| 61 |
+
'M': 12, 'N': 13, 'Q': 14, 'D': 15, 'E': 16, 'K': 17, 'R': 18, 'H': 19, 'X': 21}
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
def pdb_parser(structure):
|
| 65 |
+
"""
|
| 66 |
+
|
| 67 |
+
"""
|
| 68 |
+
voxel = np.zeros((4, 64, 64, 64), dtype=np.int8)
|
| 69 |
+
id = ''
|
| 70 |
+
seq_str = ''
|
| 71 |
+
for i in structure[0]:
|
| 72 |
+
id = i.id
|
| 73 |
+
chain = structure[0][id]
|
| 74 |
+
for res in chain:
|
| 75 |
+
if is_aa(res.get_resname(), standard=True):
|
| 76 |
+
resname = res.get_resname()
|
| 77 |
+
amino = three_to_one(resname)
|
| 78 |
+
seq_str += str(amino)
|
| 79 |
+
ATOM_WATER = AMINO_ACID_WATER[amino]
|
| 80 |
+
ATOM_CHARGE = AMINO_ACID_CHARGE[amino]
|
| 81 |
+
ATOM_CATEGORY = AMAs[amino] * 20
|
| 82 |
+
|
| 83 |
+
for i in res:
|
| 84 |
+
if i.id not in ATOMS.keys():
|
| 85 |
+
continue
|
| 86 |
+
x, y, z = i.get_coord()
|
| 87 |
+
if abs(x) > 32:
|
| 88 |
+
x = clamp(x, -31, 31)
|
| 89 |
+
if abs(y) > 32:
|
| 90 |
+
y = clamp(x, -31, 31)
|
| 91 |
+
if abs(z) > 32:
|
| 92 |
+
z = clamp(x, -31, 31)
|
| 93 |
+
x_i, y_i, z_i = int(x) + 32, int(y) + 32, int(z) + 32
|
| 94 |
+
ATOM_WEIGHT = ATOMS[i.id]
|
| 95 |
+
ATOM_R = ATOMS_R[i.id]
|
| 96 |
+
|
| 97 |
+
if ATOM_R <= 1.5:
|
| 98 |
+
voxel[0, x_i - 1:x_i + 1, y_i - 1:y_i + 1, z_i - 1:z_i + 1] = ATOM_WEIGHT
|
| 99 |
+
voxel[1, x_i - 1:x_i + 1, y_i - 1:y_i + 1, z_i - 1:z_i + 1] = ATOM_WATER
|
| 100 |
+
voxel[2, x_i - 1:x_i + 1, y_i - 1:y_i + 1, z_i - 1:z_i + 1] = ATOM_CHARGE
|
| 101 |
+
voxel[3, x_i - 1:x_i + 1, y_i - 1:y_i + 1, z_i - 1:z_i + 1] = ATOM_CATEGORY
|
| 102 |
+
else:
|
| 103 |
+
voxel[0, x_i - ATOM_R: x_i + ATOM_R, x_i - ATOM_R: x_i + ATOM_R,
|
| 104 |
+
x_i - ATOM_R: x_i + ATOM_R] = ATOM_WEIGHT
|
| 105 |
+
voxel[1, x_i - ATOM_R: x_i + ATOM_R, x_i - ATOM_R: x_i + ATOM_R,
|
| 106 |
+
x_i - ATOM_R: x_i + ATOM_R] = ATOM_WATER
|
| 107 |
+
voxel[2, x_i - ATOM_R: x_i + ATOM_R, x_i - ATOM_R: x_i + ATOM_R,
|
| 108 |
+
x_i - ATOM_R: x_i + ATOM_R] = ATOM_CHARGE
|
| 109 |
+
voxel[3, x_i - ATOM_R: x_i + ATOM_R, x_i - ATOM_R: x_i + ATOM_R,
|
| 110 |
+
x_i - ATOM_R: x_i + ATOM_R] = ATOM_CATEGORY
|
| 111 |
+
return voxel
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
class MDataset(Dataset):
|
| 115 |
+
def __init__(self, threshold=32, mode='train', max_length=50, pdb_src='af', data_ver='0922'):
|
| 116 |
+
self.num_classes = 6
|
| 117 |
+
exclude_list = pd.read_csv('../metadata/data_simi.csv', encoding="unicode_escape")['Seq'].str.upper().str.strip().tolist()
|
| 118 |
+
exclude_filter = False
|
| 119 |
+
|
| 120 |
+
p = PDBParser(QUIET=True)
|
| 121 |
+
|
| 122 |
+
if mode == 'train':
|
| 123 |
+
all_data = pd.read_csv(f'../metadata/data_{data_ver}_i.csv', encoding="unicode_escape").values
|
| 124 |
+
exclude_filter = True
|
| 125 |
+
elif mode == 'qlx':
|
| 126 |
+
all_data = pd.read_csv('../metadata/data_qlx.csv', encoding="unicode_escape").values
|
| 127 |
+
elif mode == 'saap':
|
| 128 |
+
all_data = pd.read_csv('../metadata/data_saap.csv', encoding="unicode_escape").values
|
| 129 |
+
else:
|
| 130 |
+
raise NotImplementedError
|
| 131 |
+
idx_list, seq_list, labels = all_data[:, 0], all_data[:, 1], np.concatenate((all_data[:, 4:9], all_data[:, 10:11]), axis=1)
|
| 132 |
+
labels = (labels < threshold).astype(int)
|
| 133 |
+
|
| 134 |
+
filter_idx_list = []
|
| 135 |
+
seq_new_list = []
|
| 136 |
+
label_list = []
|
| 137 |
+
for idx in range(len(idx_list)):
|
| 138 |
+
seq = seq_list[idx].upper().strip()
|
| 139 |
+
if 'X' in seq or 'B' in seq or 'J' in seq or 'Z' in seq or 'U' in seq or 'O' in seq or len(seq) > max_length or len(seq) < 6:
|
| 140 |
+
continue
|
| 141 |
+
if exclude_filter:
|
| 142 |
+
if seq in exclude_list:
|
| 143 |
+
continue
|
| 144 |
+
|
| 145 |
+
filter_idx_list.append(idx)
|
| 146 |
+
seq_new_list.append(seq)
|
| 147 |
+
label_list.append(labels[idx])
|
| 148 |
+
|
| 149 |
+
# if pdb_src == 'af':
|
| 150 |
+
# pdb_root = './pdb/pdb_af/'
|
| 151 |
+
# elif pdb_src == 'hf':
|
| 152 |
+
# pdb_root = './pdb/pdb_dbassp/'
|
| 153 |
+
# else:
|
| 154 |
+
# raise NotImplementedError
|
| 155 |
+
self.data_list = []
|
| 156 |
+
for i in tqdm(range(len(filter_idx_list))):
|
| 157 |
+
idx = filter_idx_list[i]
|
| 158 |
+
seq = seq_new_list[i]
|
| 159 |
+
label = label_list[i]
|
| 160 |
+
|
| 161 |
+
# if os.path.exists("./pdb/pdb_dbassp/" + seq + ".pdb"):
|
| 162 |
+
# pdb_path = "./pdb/pdb_dbassp/" + seq + ".pdb"
|
| 163 |
+
# elif os.path.exists("./pdb/pdb_gen/" + seq + ".pdb"):
|
| 164 |
+
# pdb_path = "./pdb/pdb_gen/" + seq + ".pdb"
|
| 165 |
+
# if os.path.exists(pdb_root + seq + ".pdb"):
|
| 166 |
+
# pdb_path = pdb_root + seq + ".pdb"
|
| 167 |
+
# else:
|
| 168 |
+
# print(f'lacking pdb file {seq}')
|
| 169 |
+
# continue
|
| 170 |
+
# # raise FileNotFoundError
|
| 171 |
+
|
| 172 |
+
# structure = p.get_structure(idx, pdb_path)
|
| 173 |
+
# voxel = pdb_parser(structure)
|
| 174 |
+
seq_emb = [AMAs[char] for char in seq] + [0] * (max_length - len(seq))
|
| 175 |
+
self.data_list.append((0, seq_emb, label))
|
| 176 |
+
|
| 177 |
+
def __getitem__(self, idx):
|
| 178 |
+
voxel, seq_emb, gt = self.data_list[idx]
|
| 179 |
+
|
| 180 |
+
return torch.Tensor(voxel).float(), torch.Tensor(seq_emb), torch.Tensor(gt)
|
| 181 |
+
|
| 182 |
+
def __len__(self):
|
| 183 |
+
return len(self.data_list)
|
| 184 |
+
|
| 185 |
+
|
| 186 |
+
# class SDataset(Dataset):
|
| 187 |
+
# '''
|
| 188 |
+
# x: Features.
|
| 189 |
+
# y: Targets, if none, do prediction.
|
| 190 |
+
# '''
|
| 191 |
+
|
| 192 |
+
# def __init__(self, threshold=32, mode='train', task='0', max_length=50, pdb_src='af'):
|
| 193 |
+
# self.num_classes = 1
|
| 194 |
+
# self.task = int(task)
|
| 195 |
+
# exclude_list = pd.read_csv('metadata/data_simi.csv', encoding="unicode_escape")['Seq'].str.upper().str.strip().tolist()
|
| 196 |
+
# exclude_filter = False
|
| 197 |
+
|
| 198 |
+
# p = PDBParser(QUIET=True)
|
| 199 |
+
|
| 200 |
+
# if mode == 'train':
|
| 201 |
+
# all_data = pd.read_csv('metadata/data_0922_i.csv', encoding="unicode_escape").values
|
| 202 |
+
# exclude_filter = True
|
| 203 |
+
# elif mode == 'qlx':
|
| 204 |
+
# all_data = pd.read_csv('metadata/data_qlx.csv', encoding="unicode_escape").values
|
| 205 |
+
# elif mode == 'saap':
|
| 206 |
+
# all_data = pd.read_csv('metadata/data_saap.csv', encoding="unicode_escape").values
|
| 207 |
+
# else:
|
| 208 |
+
# raise NotImplementedError
|
| 209 |
+
# idx_list, seq_list, labels = all_data[:, 0], all_data[:, 1], np.concatenate((all_data[:, 4:9], all_data[:, 10:11]), axis=1)
|
| 210 |
+
# labels = (labels[:, self.task:self.task+1] <= threshold).astype(int)
|
| 211 |
+
|
| 212 |
+
# filter_idx_list = []
|
| 213 |
+
# seq_new_list = []
|
| 214 |
+
# label_list = []
|
| 215 |
+
# for idx in range(len(idx_list)):
|
| 216 |
+
# seq = seq_list[idx].upper().strip()
|
| 217 |
+
# if 'X' in seq or 'B' in seq or 'J' in seq or 'Z' in seq or 'U' in seq or 'O' in seq or len(seq) > max_length or len(seq) < 6:
|
| 218 |
+
# continue
|
| 219 |
+
# if exclude_filter:
|
| 220 |
+
# if seq in exclude_list:
|
| 221 |
+
# continue
|
| 222 |
+
|
| 223 |
+
# filter_idx_list.append(idx)
|
| 224 |
+
# seq_new_list.append(seq)
|
| 225 |
+
# label_list.append(labels[idx])
|
| 226 |
+
|
| 227 |
+
# if pdb_src == 'af':
|
| 228 |
+
# pdb_root = './pdb/pdb_af/'
|
| 229 |
+
# elif pdb_src == 'hf':
|
| 230 |
+
# pdb_root = './pdb/pdb_dbassp/'
|
| 231 |
+
# else:
|
| 232 |
+
# raise NotImplementedError
|
| 233 |
+
# self.data_list = []
|
| 234 |
+
# for i in tqdm(range(len(filter_idx_list))):
|
| 235 |
+
# idx = filter_idx_list[i]
|
| 236 |
+
# seq = seq_new_list[i]
|
| 237 |
+
# label = label_list[i]
|
| 238 |
+
|
| 239 |
+
# # if os.path.exists("./pdb/pdb_dbassp/" + seq + ".pdb"):
|
| 240 |
+
# # pdb_path = "./pdb/pdb_dbassp/" + seq + ".pdb"
|
| 241 |
+
# # elif os.path.exists("./pdb/pdb_gen/" + seq + ".pdb"):
|
| 242 |
+
# # pdb_path = "./pdb/pdb_gen/" + seq + ".pdb"
|
| 243 |
+
# if os.path.exists(pdb_root + seq + ".pdb"):
|
| 244 |
+
# pdb_path = pdb_root + seq + ".pdb"
|
| 245 |
+
# else:
|
| 246 |
+
# continue
|
| 247 |
+
# # raise FileNotFoundError
|
| 248 |
+
|
| 249 |
+
# structure = p.get_structure(idx, pdb_path)
|
| 250 |
+
# voxel = pdb_parser(structure)
|
| 251 |
+
# seq_emb = [AMAs[char] for char in seq] + [0] * (max_length - len(seq))
|
| 252 |
+
# self.data_list.append((voxel, seq_emb, label))
|
| 253 |
+
|
| 254 |
+
# def __getitem__(self, idx):
|
| 255 |
+
# voxel, seq_emb, gt = self.data_list[idx]
|
| 256 |
+
|
| 257 |
+
# return torch.Tensor(voxel).float(), torch.Tensor(seq_emb), torch.Tensor(gt)
|
| 258 |
+
|
| 259 |
+
# def __len__(self):
|
| 260 |
+
# return len(self.data_list)
|
t3.1/eval.py
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import argparse
|
| 2 |
+
import json
|
| 3 |
+
import logging
|
| 4 |
+
import os
|
| 5 |
+
import time
|
| 6 |
+
|
| 7 |
+
from dataset import SDataset, MDataset
|
| 8 |
+
from network import MMPeptide, SEQPeptide, VoxPeptide, MMFPeptide, SMPeptide
|
| 9 |
+
from sklearn.model_selection import KFold
|
| 10 |
+
from train import train
|
| 11 |
+
import torch
|
| 12 |
+
import torch.nn as nn
|
| 13 |
+
from torch.utils.data import Subset, DataLoader
|
| 14 |
+
import numpy as np
|
| 15 |
+
from loss import MLCE, SuperLoss, LogCoshLoss
|
| 16 |
+
from utils import set_seed
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
parser = argparse.ArgumentParser(description='resnet26')
|
| 20 |
+
# model setting
|
| 21 |
+
parser.add_argument('--model', type=str, default='mm',
|
| 22 |
+
help='model resnet26, bi-gru')
|
| 23 |
+
parser.add_argument('--fusion', type=str, default='1',
|
| 24 |
+
help="Seed for splitting dataset (default 1)")
|
| 25 |
+
|
| 26 |
+
# task & dataset setting
|
| 27 |
+
parser.add_argument('--pdb-src', type=str, dest='pdb_src', default='af',
|
| 28 |
+
help='af or hf')
|
| 29 |
+
parser.add_argument('--task-type', type=str, dest='task_type', default='mlc',
|
| 30 |
+
help='mlc or slc')
|
| 31 |
+
parser.add_argument('--task', type=str, default='all',
|
| 32 |
+
help='task: anti toxin anti-all mechanism anti-binary anti-regression mic')
|
| 33 |
+
parser.add_argument('--classes', type=int, default=6,
|
| 34 |
+
help='model')
|
| 35 |
+
parser.add_argument('--max-length', dest='max_length', type=int, default=30,
|
| 36 |
+
help='Max length for sequence filtering')
|
| 37 |
+
parser.add_argument('--split', type=int, default=5,
|
| 38 |
+
help="Split k fold in cross validation (default: 5)")
|
| 39 |
+
parser.add_argument('--seed', type=int, default=1,
|
| 40 |
+
help="Seed for splitting dataset (default: 1)")
|
| 41 |
+
parser.add_argument('--threshold', type=float, default=64,
|
| 42 |
+
help="MIC threshold for determine labels (default: 64)")
|
| 43 |
+
|
| 44 |
+
# training setting
|
| 45 |
+
parser.add_argument('--gpu', type=int, default=0,
|
| 46 |
+
help='GPU index to use, -1 for CPU (default: 0)')
|
| 47 |
+
parser.add_argument('--batch-size', type=int, dest='batch_size', default=128,
|
| 48 |
+
help='input batch size for training (default: 128)')
|
| 49 |
+
parser.add_argument('--epochs', type=int, default=50,
|
| 50 |
+
help='number of epochs to train (default: 50)')
|
| 51 |
+
parser.add_argument('--lr', type=float, default=0.02,
|
| 52 |
+
help='learning rate (default: 0.002)')
|
| 53 |
+
parser.add_argument('--decay', type=float, default=0.0005,
|
| 54 |
+
help='weight decay (default: 0.0005)')
|
| 55 |
+
parser.add_argument('--warm-steps', type=int, dest='warm_steps', default=0,
|
| 56 |
+
help='number of warm start steps for learning rate (default: 10)')
|
| 57 |
+
parser.add_argument('--patience', type=int, default=10,
|
| 58 |
+
help='patience for early stopping (default: 10)')
|
| 59 |
+
parser.add_argument('--pretrain', type=str, dest='pretrain', default='',
|
| 60 |
+
help='path of the pretrain model') # /home/duadua/Desktop/fetal/3dpretrain/runs/e50.pth
|
| 61 |
+
parser.add_argument('--metric-avg', type=str, dest='metric_avg', default='micro',
|
| 62 |
+
help='metric average type')
|
| 63 |
+
|
| 64 |
+
# args for losses
|
| 65 |
+
parser.add_argument('--loss', type=str, default='mlce',
|
| 66 |
+
help='loss function (mlce, sl, mix)')
|
| 67 |
+
|
| 68 |
+
parser.add_argument('--bias-curri', dest='bias_curri', action='store_true', default=False,
|
| 69 |
+
help='directly use loss as the training data (biased) or not (unbiased)')
|
| 70 |
+
parser.add_argument('--anti-curri', dest='anti_curri', action='store_true', default=False,
|
| 71 |
+
help='easy to hard (curri), hard to easy (anti)')
|
| 72 |
+
parser.add_argument('--std-coff', dest='std_coff', type=float, default=1,
|
| 73 |
+
help='the hyper-parameter of std')
|
| 74 |
+
|
| 75 |
+
args = parser.parse_args()
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
def main():
|
| 79 |
+
set_seed(args.seed)
|
| 80 |
+
|
| 81 |
+
if args.task_type == 'slc':
|
| 82 |
+
if args.task == 'all':
|
| 83 |
+
raise ValueError('Choose one task number to run single label classification')
|
| 84 |
+
args.classes = 1
|
| 85 |
+
elif args.task_type == 'mlc':
|
| 86 |
+
pass
|
| 87 |
+
else:
|
| 88 |
+
raise NotImplementedError
|
| 89 |
+
|
| 90 |
+
weight_dir = "./run/" + args.task_type + '-' + args.task + "-m-" + args.model + '-' + args.loss + str(args.batch_size) + str(args.lr) + str(args.epochs) + args.pdb_src
|
| 91 |
+
if not os.path.exists(weight_dir):
|
| 92 |
+
raise ValueError
|
| 93 |
+
|
| 94 |
+
logging.basicConfig(handlers=[
|
| 95 |
+
# logging.FileHandler(filename=os.path.join(weight_dir, "eval.log"), encoding='utf-8', mode='w+'),
|
| 96 |
+
logging.StreamHandler()],
|
| 97 |
+
format="%(asctime)s: %(message)s", datefmt="%F %T", level=logging.INFO)
|
| 98 |
+
|
| 99 |
+
logging.info(f'saving_dir: {weight_dir}')
|
| 100 |
+
|
| 101 |
+
# with open(os.path.join(weight_dir, "config.json"), "w") as f:
|
| 102 |
+
# f.write(json.dumps(vars(args)))
|
| 103 |
+
|
| 104 |
+
device = torch.device("cpu" if args.gpu == -1 or not torch.cuda.is_available() else f"cuda:{args.gpu}")
|
| 105 |
+
|
| 106 |
+
logging.info('Loading Training Dataset')
|
| 107 |
+
if args.task_type == 'mlc':
|
| 108 |
+
set_all = MDataset(threshold=args.threshold, mode='train', max_length=args.max_length, pdb_src=args.pdb_src)
|
| 109 |
+
else:
|
| 110 |
+
set_all = SDataset(threshold=args.threshold, mode='train', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src)
|
| 111 |
+
logging.info('Loading Test Dataset')
|
| 112 |
+
if args.task_type == 'mlc':
|
| 113 |
+
qlx_set = MDataset(threshold=args.threshold, mode='qlx', max_length=args.max_length, pdb_src=args.pdb_src)
|
| 114 |
+
saap_set = MDataset(threshold=args.threshold, mode='saap', max_length=args.max_length, pdb_src=args.pdb_src)
|
| 115 |
+
else:
|
| 116 |
+
qlx_set = SDataset(threshold=args.threshold, mode='qlx', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src)
|
| 117 |
+
saap_set = SDataset(threshold=args.threshold, mode='saap', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src)
|
| 118 |
+
|
| 119 |
+
best_perform_list = [[] for i in range(5)]
|
| 120 |
+
qlx_perform_list = [[] for i in range(5)]
|
| 121 |
+
saap_perform_list = [[] for i in range(5)]
|
| 122 |
+
|
| 123 |
+
kf = KFold(n_splits=5, shuffle=True, random_state=42)
|
| 124 |
+
|
| 125 |
+
for fold, (train_idx, val_idx) in enumerate(kf.split(set_all)):
|
| 126 |
+
# train_set= Subset(set_all, train_idx)
|
| 127 |
+
valid_set = Subset(set_all, val_idx)
|
| 128 |
+
|
| 129 |
+
# train_loader = DataLoader(train_set, batch_size=args.batch_size, shuffle=True, drop_last=True)
|
| 130 |
+
valid_loader = DataLoader(valid_set, batch_size=args.batch_size, shuffle=False)
|
| 131 |
+
qlx_loader = DataLoader(qlx_set, batch_size=1, shuffle=False)
|
| 132 |
+
saap_loader = DataLoader(saap_set, batch_size=1, shuffle=False)
|
| 133 |
+
if args.model == 'seq':
|
| 134 |
+
model = SEQPeptide(classes=set_all.num_classes, q_encoder='mlp')
|
| 135 |
+
elif args.model == 'voxel':
|
| 136 |
+
model = VoxPeptide(classes=set_all.num_classes)
|
| 137 |
+
elif args.model == 'mm':
|
| 138 |
+
# model = MMPeptide(classes=train_set.num_classes, q_encoder='tf', ) # attention='hamburger'
|
| 139 |
+
# model = SMPeptide(classes=train_set.num_classes, q_encoder='mlp', max_length=30) # attention='hamburger'
|
| 140 |
+
model = MMPeptide(classes=set_all.num_classes, q_encoder='mlp', max_length=args.max_length)# , attention='hamburger'
|
| 141 |
+
elif args.model == 'mmf':
|
| 142 |
+
model = MMFPeptide(classes=set_all.num_classes, q_encoder='mlp', max_length=args.max_length) # attention='hamburger'
|
| 143 |
+
if len(args.pretrain) != 0:
|
| 144 |
+
logging.info('loading pretrain model')
|
| 145 |
+
# model = load_pretrain_model(model, torch.load(args.pretrain))
|
| 146 |
+
model_state = model.state_dict()
|
| 147 |
+
pretrained_state = torch.load(args.pretrain)
|
| 148 |
+
pretrained_state = {k: v for k, v in pretrained_state.items() if
|
| 149 |
+
k in model_state and v.size() == model_state[k].size()}
|
| 150 |
+
model_state.update(pretrained_state)
|
| 151 |
+
model.load_state_dict(model_state)
|
| 152 |
+
# model.load_state_dict(torch.load(args.pretrain), strict=False)
|
| 153 |
+
model.to(device)
|
| 154 |
+
weights_path = f"{weight_dir}/model_{fold + 1}.pth"
|
| 155 |
+
model.load_state_dict(torch.load(weights_path))
|
| 156 |
+
# early_stopping = EarlyStopping(patience=args.patience, path=weights_path)
|
| 157 |
+
logging.info(f'Running Cross Validation {fold + 1}')
|
| 158 |
+
logging.info(f'Fold {fold + 1} Valid set:{len(valid_set)}, Test set:qlx {len(qlx_set)} saap {len(saap_set)}')
|
| 159 |
+
|
| 160 |
+
|
| 161 |
+
if args.task_type in ('mlc', 'slc') :
|
| 162 |
+
_, ap, f1, acc, auc = train(args, None, model, None, valid_loader, device, None, None)
|
| 163 |
+
logging.info(f'ap: {ap:.3f}, f1: {f1:.3f}, acc: {acc:.3f}, auc: {auc:.3f}')
|
| 164 |
+
best_perform_list[fold] = np.asarray([ap, f1, acc, auc])
|
| 165 |
+
|
| 166 |
+
_, qlx_ap, qlx_f1, qlx_acc, qlx_auc = train(args, None, model, None, qlx_loader, device, None, None)
|
| 167 |
+
logging.info(f'QLX results, ap: {qlx_ap:.3f}, f1: {qlx_f1:.3f}, acc: {qlx_acc:.3f}, auc: {qlx_auc:.3f}')
|
| 168 |
+
qlx_perform_list[fold] = np.asarray([qlx_ap, qlx_f1, qlx_acc, qlx_auc])
|
| 169 |
+
|
| 170 |
+
_, saap_ap, saap_f1, saap_acc, saap_auc = train(args, None, model, None, saap_loader, device, None, None)
|
| 171 |
+
logging.info(f'SAAP results, ap: {saap_ap:.3f}, f1: {saap_f1:.3f}, acc: {saap_acc:.3f}, auc: {saap_auc:.3f}')
|
| 172 |
+
saap_perform_list[fold] = np.asarray([saap_ap, saap_f1, saap_acc, saap_auc])
|
| 173 |
+
|
| 174 |
+
else:
|
| 175 |
+
raise NotImplementedError
|
| 176 |
+
|
| 177 |
+
logging.info(f'Cross Validation Finished!')
|
| 178 |
+
best_perform_list = np.asarray(best_perform_list)
|
| 179 |
+
qlx_perform_list = np.asarray(qlx_perform_list)
|
| 180 |
+
saap_perform_list = np.asarray(saap_perform_list)
|
| 181 |
+
logging.info('Best validation perform list\n%s', best_perform_list)
|
| 182 |
+
logging.info('mean: %s', np.round(np.mean(best_perform_list, 0), 3))
|
| 183 |
+
logging.info('std: %s', np.round(np.std(best_perform_list, 0), 3))
|
| 184 |
+
logging.info('Best qlx perform list\n%s', qlx_perform_list)
|
| 185 |
+
logging.info('mean: %s', np.round(np.mean(qlx_perform_list, 0), 3))
|
| 186 |
+
logging.info('std: %s', np.round(np.std(qlx_perform_list, 0), 3))
|
| 187 |
+
logging.info('Best saap perform list\n%s', saap_perform_list)
|
| 188 |
+
logging.info('mean: %s', np.round(np.mean(saap_perform_list, 0), 3))
|
| 189 |
+
logging.info('std: %s', np.round(np.std(saap_perform_list, 0), 3))
|
| 190 |
+
perform = open(weight_dir+'/eval.txt', 'w')
|
| 191 |
+
perform.write('Valid\n')
|
| 192 |
+
perform.write(','.join([str(i) for i in np.mean(best_perform_list, 0)])+'\n')
|
| 193 |
+
perform.write(','.join([str(i) for i in np.std(best_perform_list, 0)])+'\n')
|
| 194 |
+
perform.write('qlx\n')
|
| 195 |
+
perform.write(','.join([str(i) for i in np.mean(qlx_perform_list, 0)])+'\n')
|
| 196 |
+
perform.write(','.join([str(i) for i in np.std(qlx_perform_list, 0)])+'\n')
|
| 197 |
+
perform.write('saap\n')
|
| 198 |
+
perform.write(','.join([str(i) for i in np.mean(saap_perform_list, 0)])+'\n')
|
| 199 |
+
perform.write(','.join([str(i) for i in np.std(saap_perform_list, 0)])+'\n')
|
| 200 |
+
|
| 201 |
+
|
| 202 |
+
if __name__ == "__main__":
|
| 203 |
+
main()
|
t3.1/gen_simi.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
from dataset import calculate_similarity
|
| 3 |
+
|
| 4 |
+
# 读取模板 CSV 文件和目标 CSV 文件
|
| 5 |
+
template_file = './metadata/data_test.csv' # 模板CSV文件路径
|
| 6 |
+
target_file = './metadata/data_0920_i.csv' # 目标CSV文件路径
|
| 7 |
+
|
| 8 |
+
template_df = pd.read_csv(template_file)
|
| 9 |
+
target_df = pd.read_csv(target_file)
|
| 10 |
+
|
| 11 |
+
# 假设我们要使用 'template_column' 列作为模板中的列,'target_column' 列作为目标表格中的列
|
| 12 |
+
template_column = 'Seq' # 替换为你的模板表格列名
|
| 13 |
+
target_column = 'Seq' # 替换为你的目标表格列名
|
| 14 |
+
|
| 15 |
+
# 创建一个空的 DataFrame,用于存储符合条件的行,并复制目标表格的表头
|
| 16 |
+
extracted_rows = pd.DataFrame(columns=target_df.columns)
|
| 17 |
+
|
| 18 |
+
# 遍历目标表格的每一行
|
| 19 |
+
for index, target_row in target_df.iterrows():
|
| 20 |
+
target_value = target_row[target_column]
|
| 21 |
+
|
| 22 |
+
# 遍历模板表格的每个值
|
| 23 |
+
for template_value in template_df[template_column]:
|
| 24 |
+
|
| 25 |
+
# 计算指标
|
| 26 |
+
metric = calculate_similarity(target_value.strip().upper(), template_value.strip().upper())
|
| 27 |
+
|
| 28 |
+
# 如果指标大于0.3,则提取目标表格的该行
|
| 29 |
+
if metric > 0.3:
|
| 30 |
+
extracted_rows = pd.concat([extracted_rows, pd.DataFrame([target_row])], ignore_index=True)
|
| 31 |
+
# 只要找到一个匹配值,就可以继续检查下一行
|
| 32 |
+
break
|
| 33 |
+
|
| 34 |
+
# 将提取的行保存到一个新的CSV文件中,确保表头与目标表格一致
|
| 35 |
+
output_file = './metadata/data_simi.csv'
|
| 36 |
+
extracted_rows.to_csv(output_file, index=False)
|
| 37 |
+
|
| 38 |
+
print(f"符合条件的行已保存到 {output_file}")
|
t3.1/infer.py
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import argparse
|
| 2 |
+
import json
|
| 3 |
+
import logging
|
| 4 |
+
import os
|
| 5 |
+
import time
|
| 6 |
+
import pandas as pd
|
| 7 |
+
|
| 8 |
+
from dataset import MDataset
|
| 9 |
+
from network import SEQPeptide
|
| 10 |
+
from sklearn.model_selection import KFold
|
| 11 |
+
from train import train
|
| 12 |
+
import torch
|
| 13 |
+
import torch.nn as nn
|
| 14 |
+
from torch.utils.data import Subset, DataLoader
|
| 15 |
+
import numpy as np
|
| 16 |
+
from loss import MLCE, SuperLoss, LogCoshLoss
|
| 17 |
+
from utils import set_seed
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
parser = argparse.ArgumentParser(description='resnet26')
|
| 21 |
+
# model setting
|
| 22 |
+
parser.add_argument('--model', type=str, default='mlp',
|
| 23 |
+
help='mlp gru lstm rnn tf mamba')
|
| 24 |
+
parser.add_argument('--fusion', type=str, default='1',
|
| 25 |
+
help="Seed for splitting dataset (default 1)")
|
| 26 |
+
|
| 27 |
+
# task & dataset setting
|
| 28 |
+
parser.add_argument('--pdb-src', type=str, dest='pdb_src', default='af',
|
| 29 |
+
help='af or hf')
|
| 30 |
+
parser.add_argument('--task-type', type=str, dest='task_type', default='mlc',
|
| 31 |
+
help='mlc or slc')
|
| 32 |
+
parser.add_argument('--data-ver', type=str, dest='data_ver', default='0920',
|
| 33 |
+
help='data version')
|
| 34 |
+
parser.add_argument('--task', type=str, default='all',
|
| 35 |
+
help='task: anti toxin anti-all mechanism anti-binary anti-regression mic')
|
| 36 |
+
parser.add_argument('--classes', type=int, default=6,
|
| 37 |
+
help='model')
|
| 38 |
+
parser.add_argument('--max-length', dest='max_length', type=int, default=30,
|
| 39 |
+
help='Max length for sequence filtering')
|
| 40 |
+
parser.add_argument('--split', type=int, default=5,
|
| 41 |
+
help="Split k fold in cross validation (default: 5)")
|
| 42 |
+
parser.add_argument('--seed', type=int, default=1,
|
| 43 |
+
help="Seed for splitting dataset (default: 1)")
|
| 44 |
+
parser.add_argument('--threshold', type=float, default=128,
|
| 45 |
+
help="MIC threshold for determine labels (default: 128)")
|
| 46 |
+
|
| 47 |
+
# training setting
|
| 48 |
+
parser.add_argument('--gpu', type=int, default=0,
|
| 49 |
+
help='GPU index to use, -1 for CPU (default: 0)')
|
| 50 |
+
parser.add_argument('--batch-size', type=int, dest='batch_size', default=256,
|
| 51 |
+
help='input batch size for training (default: 128)')
|
| 52 |
+
parser.add_argument('--epochs', type=int, default=50,
|
| 53 |
+
help='number of epochs to train (default: 50)')
|
| 54 |
+
parser.add_argument('--lr', type=float, default=0.001,
|
| 55 |
+
help='learning rate (default: 0.001)')
|
| 56 |
+
parser.add_argument('--decay', type=float, default=0.0005,
|
| 57 |
+
help='weight decay (default: 0.0005)')
|
| 58 |
+
parser.add_argument('--warm-steps', type=int, dest='warm_steps', default=0,
|
| 59 |
+
help='number of warm start steps for learning rate (default: 10)')
|
| 60 |
+
parser.add_argument('--patience', type=int, default=10,
|
| 61 |
+
help='patience for early stopping (default: 10)')
|
| 62 |
+
parser.add_argument('--mlp', type=str, default='./run/mlp-bce256-0.001-50',
|
| 63 |
+
help='path of the pretrain model')
|
| 64 |
+
parser.add_argument('--gru', type=str, default='./run/gru-bce256-0.001-50',
|
| 65 |
+
help='path of the pretrain model')
|
| 66 |
+
parser.add_argument('--lstm', type=str, default='./run/lstm-bce256-0.001-50',
|
| 67 |
+
help='path of the pretrain model')
|
| 68 |
+
parser.add_argument('--rnn', type=str, default='./run/rnn-bce256-0.001-50',
|
| 69 |
+
help='path of the pretrain model')
|
| 70 |
+
parser.add_argument('--tf', type=str, default='./run/tf-bce256-0.001-50',
|
| 71 |
+
help='path of the pretrain model')
|
| 72 |
+
parser.add_argument('--mamba', type=str, default='./run/mamba-bce256-0.001-50',
|
| 73 |
+
help='path of the pretrain model')
|
| 74 |
+
parser.add_argument('--metric-avg', type=str, dest='metric_avg', default='macro',
|
| 75 |
+
help='metric average type')
|
| 76 |
+
|
| 77 |
+
# args for losses
|
| 78 |
+
parser.add_argument('--loss', type=str, default='bce',
|
| 79 |
+
help='loss function (mlce, sl, mix)')
|
| 80 |
+
|
| 81 |
+
parser.add_argument('--bias-curri', dest='bias_curri', action='store_true', default=False,
|
| 82 |
+
help='directly use loss as the training data (biased) or not (unbiased)')
|
| 83 |
+
parser.add_argument('--anti-curri', dest='anti_curri', action='store_true', default=False,
|
| 84 |
+
help='easy to hard (curri), hard to easy (anti)')
|
| 85 |
+
parser.add_argument('--std-coff', dest='std_coff', type=float, default=1,
|
| 86 |
+
help='the hyper-parameter of std')
|
| 87 |
+
|
| 88 |
+
args = parser.parse_args()
|
| 89 |
+
|
| 90 |
+
model_path = {'mlp': args.mlp, 'gru': args.gru, 'lstm': args.lstm, 'rnn': args.rnn, 'tf': args.tf, 'mamba': args.mamba}
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
def main():
|
| 94 |
+
set_seed(args.seed)
|
| 95 |
+
device = torch.device("cpu" if args.gpu == -1 or not torch.cuda.is_available() else f"cuda:{args.gpu}")
|
| 96 |
+
|
| 97 |
+
results = pd.DataFrame()
|
| 98 |
+
|
| 99 |
+
logging.info('Loading Test Dataset')
|
| 100 |
+
qlx_set = MDataset(threshold=args.threshold, mode='qlx', max_length=args.max_length)
|
| 101 |
+
qlx_loader = DataLoader(qlx_set, batch_size=1, shuffle=False)
|
| 102 |
+
|
| 103 |
+
models = {'mlp': SEQPeptide(classes=args.classes, q_encoder='mlp'),
|
| 104 |
+
'gru': SEQPeptide(classes=args.classes, q_encoder='gru'),
|
| 105 |
+
'lstm': SEQPeptide(classes=args.classes, q_encoder='lstm'),
|
| 106 |
+
'rnn': SEQPeptide(classes=args.classes, q_encoder='rnn'),
|
| 107 |
+
'tf': SEQPeptide(classes=args.classes, q_encoder='tf'),
|
| 108 |
+
'mamba': SEQPeptide(classes=args.classes, q_encoder='mamba')}
|
| 109 |
+
|
| 110 |
+
for model_name in model_path.keys():
|
| 111 |
+
model = models[model_name]
|
| 112 |
+
pred_all = []
|
| 113 |
+
gt_all = []
|
| 114 |
+
for i in range(1,6):
|
| 115 |
+
model.load_state_dict(torch.load(os.path.join(model_path[model_name], f'model_{i}.pth')))
|
| 116 |
+
model.to(device).eval()
|
| 117 |
+
with torch.no_grad():
|
| 118 |
+
for data in qlx_loader:
|
| 119 |
+
voxel, seq, gt = data
|
| 120 |
+
gt_all.append(gt.to(device))
|
| 121 |
+
out = model((voxel.to(device), seq.to(device)))
|
| 122 |
+
pred_all.append(out)
|
| 123 |
+
pred_all = torch.nn.functional.sigmoid(torch.cat(pred_all, dim=0)).squeeze().cpu().numpy()
|
| 124 |
+
gt_all = torch.cat(gt_all, dim=0).int().squeeze().cpu().numpy()
|
| 125 |
+
results[model_name] = pred_all.ravel(order='F')
|
| 126 |
+
|
| 127 |
+
results['gt'] = gt_all.ravel(order='F')
|
| 128 |
+
|
| 129 |
+
results.to_csv("preds.csv", index=False)
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
if __name__ == "__main__":
|
| 133 |
+
main()
|
t3.1/loss.py
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from torch import nn
|
| 3 |
+
import torch.nn.functional as F
|
| 4 |
+
from math import cos, pi, sin
|
| 5 |
+
import math
|
| 6 |
+
import numpy as np
|
| 7 |
+
from scipy.special import lambertw
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def mixup_criterion(criterion, pred, y_a, y_b, lam, pow=2):
|
| 11 |
+
y = lam ** pow * y_a + (1 - lam) ** pow * y_b
|
| 12 |
+
return criterion(pred, y)
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def mixup_data(v, q, a):
|
| 16 |
+
'''Returns mixed inputs, pairs of targets, and lambda without organ constraint'''
|
| 17 |
+
lam = np.random.beta(1, 1)
|
| 18 |
+
|
| 19 |
+
batch_size = v.shape[0]
|
| 20 |
+
index = torch.randperm(batch_size)
|
| 21 |
+
|
| 22 |
+
mixed_v = lam * v + (1 - lam) * v[index, :]
|
| 23 |
+
mixed_q = lam * q + (1 - lam) * q[index, :]
|
| 24 |
+
|
| 25 |
+
a_1, a_2 = a, a[index]
|
| 26 |
+
return mixed_v, mixed_q, a_1, a_2, lam
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def linear(epoch, nepoch):
|
| 30 |
+
return 1 - epoch / nepoch
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def convex(epoch, nepoch):
|
| 34 |
+
return epoch / (2 - nepoch)
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def concave(epoch, nepoch):
|
| 38 |
+
return 1 - sin((epoch / nepoch) * (pi / 2))
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def composite(epoch, nepoch):
|
| 42 |
+
return 0.5 * cos((epoch / nepoch) * pi) + 0.5
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
class LogCoshLoss(nn.Module):
|
| 46 |
+
def __init__(self):
|
| 47 |
+
super().__init__()
|
| 48 |
+
|
| 49 |
+
def forward(self, y_t, y_prime_t):
|
| 50 |
+
ey_t = y_t - y_prime_t
|
| 51 |
+
return torch.mean(torch.log(torch.cosh(ey_t + 1e-12)))+F.mse_loss(y_t, y_prime_t)
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
class MLCE(nn.Module):
|
| 55 |
+
def __init__(self):
|
| 56 |
+
super(MLCE, self).__init__()
|
| 57 |
+
|
| 58 |
+
def _mlcce(self, y_pred, y_true):
|
| 59 |
+
y_pred = (1 - 2 * y_true) * y_pred
|
| 60 |
+
y_pred_neg = y_pred - y_true * 1e12
|
| 61 |
+
y_pred_pos = y_pred - (1 - y_true) * 1e12
|
| 62 |
+
zeros = torch.zeros_like(y_pred[..., :1])
|
| 63 |
+
y_pred_neg = torch.cat([y_pred_neg, zeros], dim=-1)
|
| 64 |
+
y_pred_pos = torch.cat([y_pred_pos, zeros], dim=-1)
|
| 65 |
+
neg_loss = torch.logsumexp(y_pred_neg, dim=-1)
|
| 66 |
+
pos_loss = torch.logsumexp(y_pred_pos, dim=-1)
|
| 67 |
+
loss = torch.mean(neg_loss + pos_loss)
|
| 68 |
+
return loss
|
| 69 |
+
|
| 70 |
+
def __call__(self, y_pred, y_true):
|
| 71 |
+
return self._mlcce(y_pred, y_true)
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
class SuperLoss(nn.Module):
|
| 75 |
+
def __init__(self, C=10, lam=1, batch_size=128):
|
| 76 |
+
super(SuperLoss, self).__init__()
|
| 77 |
+
self.tau = math.log(C)
|
| 78 |
+
self.lam = lam # set to 1 for CIFAR10 and 0.25 for CIFAR100
|
| 79 |
+
self.batch_size = batch_size
|
| 80 |
+
|
| 81 |
+
def forward(self, logits, targets):
|
| 82 |
+
l_i = F.mse_loss(logits, targets, reduction='none').detach()
|
| 83 |
+
sigma = self.sigma(l_i)
|
| 84 |
+
loss = (F.mse_loss(logits, targets, reduction='none') - self.tau) * sigma + self.lam * (
|
| 85 |
+
torch.log(sigma) ** 2)
|
| 86 |
+
loss = loss.sum() / self.batch_size
|
| 87 |
+
return loss
|
| 88 |
+
|
| 89 |
+
def sigma(self, l_i):
|
| 90 |
+
x = torch.ones(l_i.size()) * (-2 / math.exp(1.))
|
| 91 |
+
x = x.cuda()
|
| 92 |
+
y = 0.5 * torch.max(x, (l_i - self.tau) / self.lam)
|
| 93 |
+
y = y.cpu().numpy()
|
| 94 |
+
sigma = np.exp(-lambertw(y))
|
| 95 |
+
sigma = sigma.real.astype(np.float32)
|
| 96 |
+
sigma = torch.from_numpy(sigma).cuda()
|
| 97 |
+
return sigma
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
def unbiased_curriculum_loss(out, data, args, epoch, epochs, scheduler='linear'):
|
| 101 |
+
losses = []
|
| 102 |
+
scheduler = linear if scheduler == 'linear' else concave
|
| 103 |
+
|
| 104 |
+
# calculate difficulty measurement function
|
| 105 |
+
adjusted_losses = []
|
| 106 |
+
for idx in range(out.shape[0]):
|
| 107 |
+
ground_truth = max(1, abs(data[idx].item()))
|
| 108 |
+
loss = F.mse_loss(out[idx], data[idx])
|
| 109 |
+
losses.append(loss)
|
| 110 |
+
adjusted_losses.append(loss.item() / ground_truth)
|
| 111 |
+
|
| 112 |
+
mean_loss, std_loss = np.mean(adjusted_losses), np.std(adjusted_losses)
|
| 113 |
+
|
| 114 |
+
# re-weight losses
|
| 115 |
+
total_loss = 0
|
| 116 |
+
for i, loss in enumerate(losses):
|
| 117 |
+
if adjusted_losses[i] > mean_loss + 1 * std_loss:
|
| 118 |
+
schedule_factor = scheduler(epoch, args.epochs)
|
| 119 |
+
total_loss += schedule_factor * loss
|
| 120 |
+
else:
|
| 121 |
+
total_loss += loss
|
| 122 |
+
|
| 123 |
+
return total_loss
|
t3.1/main.py
ADDED
|
@@ -0,0 +1,229 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import argparse
|
| 2 |
+
import json
|
| 3 |
+
import logging
|
| 4 |
+
import os
|
| 5 |
+
import time
|
| 6 |
+
|
| 7 |
+
from dataset import MDataset
|
| 8 |
+
from network import SEQPeptide
|
| 9 |
+
from sklearn.model_selection import KFold
|
| 10 |
+
from train import train
|
| 11 |
+
import torch
|
| 12 |
+
import torch.nn as nn
|
| 13 |
+
from torch.utils.data import Subset, DataLoader
|
| 14 |
+
import numpy as np
|
| 15 |
+
from loss import MLCE, SuperLoss, LogCoshLoss
|
| 16 |
+
from utils import set_seed
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
parser = argparse.ArgumentParser(description='resnet26')
|
| 20 |
+
# model setting
|
| 21 |
+
parser.add_argument('--model', type=str, default='mlp',
|
| 22 |
+
help='mlp gru lstm rnn tf mamba')
|
| 23 |
+
parser.add_argument('--fusion', type=str, default='1',
|
| 24 |
+
help="Seed for splitting dataset (default 1)")
|
| 25 |
+
|
| 26 |
+
# task & dataset setting
|
| 27 |
+
parser.add_argument('--pdb-src', type=str, dest='pdb_src', default='af',
|
| 28 |
+
help='af or hf')
|
| 29 |
+
parser.add_argument('--task-type', type=str, dest='task_type', default='mlc',
|
| 30 |
+
help='mlc or slc')
|
| 31 |
+
parser.add_argument('--data-ver', type=str, dest='data_ver', default='0920',
|
| 32 |
+
help='data version')
|
| 33 |
+
parser.add_argument('--task', type=str, default='all',
|
| 34 |
+
help='task: anti toxin anti-all mechanism anti-binary anti-regression mic')
|
| 35 |
+
parser.add_argument('--classes', type=int, default=6,
|
| 36 |
+
help='model')
|
| 37 |
+
parser.add_argument('--max-length', dest='max_length', type=int, default=30,
|
| 38 |
+
help='Max length for sequence filtering')
|
| 39 |
+
parser.add_argument('--split', type=int, default=5,
|
| 40 |
+
help="Split k fold in cross validation (default: 5)")
|
| 41 |
+
parser.add_argument('--seed', type=int, default=1,
|
| 42 |
+
help="Seed for splitting dataset (default: 1)")
|
| 43 |
+
parser.add_argument('--threshold', type=float, default=128,
|
| 44 |
+
help="MIC threshold for determine labels (default: 128)")
|
| 45 |
+
|
| 46 |
+
# training setting
|
| 47 |
+
parser.add_argument('--gpu', type=int, default=0,
|
| 48 |
+
help='GPU index to use, -1 for CPU (default: 0)')
|
| 49 |
+
parser.add_argument('--batch-size', type=int, dest='batch_size', default=256,
|
| 50 |
+
help='input batch size for training (default: 128)')
|
| 51 |
+
parser.add_argument('--epochs', type=int, default=50,
|
| 52 |
+
help='number of epochs to train (default: 50)')
|
| 53 |
+
parser.add_argument('--lr', type=float, default=0.001,
|
| 54 |
+
help='learning rate (default: 0.001)')
|
| 55 |
+
parser.add_argument('--decay', type=float, default=0.0005,
|
| 56 |
+
help='weight decay (default: 0.0005)')
|
| 57 |
+
parser.add_argument('--warm-steps', type=int, dest='warm_steps', default=0,
|
| 58 |
+
help='number of warm start steps for learning rate (default: 10)')
|
| 59 |
+
parser.add_argument('--patience', type=int, default=10,
|
| 60 |
+
help='patience for early stopping (default: 10)')
|
| 61 |
+
parser.add_argument('--pretrain', type=str, dest='pretrain', default='',
|
| 62 |
+
help='path of the pretrain model') # /home/duadua/Desktop/fetal/3dpretrain/runs/e50.pth
|
| 63 |
+
parser.add_argument('--metric-avg', type=str, dest='metric_avg', default='macro',
|
| 64 |
+
help='metric average type')
|
| 65 |
+
|
| 66 |
+
# args for losses
|
| 67 |
+
parser.add_argument('--loss', type=str, default='bce',
|
| 68 |
+
help='loss function (mlce, sl, mix)')
|
| 69 |
+
|
| 70 |
+
parser.add_argument('--bias-curri', dest='bias_curri', action='store_true', default=False,
|
| 71 |
+
help='directly use loss as the training data (biased) or not (unbiased)')
|
| 72 |
+
parser.add_argument('--anti-curri', dest='anti_curri', action='store_true', default=False,
|
| 73 |
+
help='easy to hard (curri), hard to easy (anti)')
|
| 74 |
+
parser.add_argument('--std-coff', dest='std_coff', type=float, default=1,
|
| 75 |
+
help='the hyper-parameter of std')
|
| 76 |
+
|
| 77 |
+
args = parser.parse_args()
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
def main():
|
| 81 |
+
set_seed(args.seed)
|
| 82 |
+
|
| 83 |
+
if args.task_type == 'slc':
|
| 84 |
+
if args.task == 'all':
|
| 85 |
+
raise ValueError('Choose one task number to run single label classification')
|
| 86 |
+
args.classes = 1
|
| 87 |
+
elif args.task_type == 'mlc':
|
| 88 |
+
pass
|
| 89 |
+
else:
|
| 90 |
+
raise NotImplementedError
|
| 91 |
+
|
| 92 |
+
if args.loss == 'mlce' and args.task_type != 'slc':
|
| 93 |
+
criterion = MLCE()
|
| 94 |
+
elif args.loss == "bce" or args.task_type == 'slc':
|
| 95 |
+
args.loss = "bce"
|
| 96 |
+
criterion = nn.BCEWithLogitsLoss()
|
| 97 |
+
else:
|
| 98 |
+
criterion = 0
|
| 99 |
+
pass
|
| 100 |
+
weight_dir = "./run/" + args.model + '-' + args.loss + str(args.batch_size) + '-' + str(args.lr) + '-' + str(args.epochs)
|
| 101 |
+
if not os.path.exists(weight_dir):
|
| 102 |
+
os.makedirs(weight_dir)
|
| 103 |
+
|
| 104 |
+
logging.basicConfig(handlers=[
|
| 105 |
+
logging.FileHandler(filename=os.path.join(weight_dir, "training.log"), encoding='utf-8', mode='w+'),
|
| 106 |
+
logging.StreamHandler()],
|
| 107 |
+
format="%(asctime)s: %(message)s", datefmt="%F %T", level=logging.INFO)
|
| 108 |
+
|
| 109 |
+
logging.info(f'saving_dir: {weight_dir}')
|
| 110 |
+
|
| 111 |
+
with open(os.path.join(weight_dir, "config.json"), "w") as f:
|
| 112 |
+
f.write(json.dumps(vars(args)))
|
| 113 |
+
|
| 114 |
+
device = torch.device("cpu" if args.gpu == -1 or not torch.cuda.is_available() else f"cuda:{args.gpu}")
|
| 115 |
+
|
| 116 |
+
logging.info('Loading Training Dataset')
|
| 117 |
+
if args.task_type == 'mlc':
|
| 118 |
+
set_all = MDataset(threshold=args.threshold, mode='train', max_length=args.max_length, pdb_src=args.pdb_src, data_ver=args.data_ver)
|
| 119 |
+
# else:
|
| 120 |
+
# set_all = SDataset(threshold=args.threshold, mode='train', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src)
|
| 121 |
+
logging.info('Loading Test Dataset')
|
| 122 |
+
if args.task_type == 'mlc':
|
| 123 |
+
qlx_set = MDataset(threshold=args.threshold, mode='qlx', max_length=args.max_length, pdb_src=args.pdb_src)
|
| 124 |
+
saap_set = MDataset(threshold=args.threshold, mode='saap', max_length=args.max_length, pdb_src=args.pdb_src)
|
| 125 |
+
# else:
|
| 126 |
+
# qlx_set = SDataset(threshold=args.threshold, mode='qlx', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src)
|
| 127 |
+
# saap_set = SDataset(threshold=args.threshold, mode='saap', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src)
|
| 128 |
+
|
| 129 |
+
best_perform_list = [[] for i in range(5)]
|
| 130 |
+
qlx_perform_list = [[] for i in range(5)]
|
| 131 |
+
saap_perform_list = [[] for i in range(5)]
|
| 132 |
+
|
| 133 |
+
kf = KFold(n_splits=5, shuffle=True, random_state=42)
|
| 134 |
+
|
| 135 |
+
for fold, (train_idx, val_idx) in enumerate(kf.split(set_all)):
|
| 136 |
+
train_set= Subset(set_all, train_idx)
|
| 137 |
+
valid_set = Subset(set_all, val_idx)
|
| 138 |
+
# if args.loss == 'bce':
|
| 139 |
+
# weight = torch.Tensor([0] * args.classes)
|
| 140 |
+
# for i in train_set:
|
| 141 |
+
# weight += i[2]
|
| 142 |
+
# weight = (len(train_set) - weight) / weight
|
| 143 |
+
# criterion.register_buffer('pos_weight', weight.to(device))
|
| 144 |
+
|
| 145 |
+
train_loader = DataLoader(train_set, batch_size=args.batch_size, shuffle=True, drop_last=True)
|
| 146 |
+
valid_loader = DataLoader(valid_set, batch_size=args.batch_size, shuffle=False)
|
| 147 |
+
qlx_loader = DataLoader(qlx_set, batch_size=1, shuffle=False)
|
| 148 |
+
saap_loader = DataLoader(saap_set, batch_size=1, shuffle=False)
|
| 149 |
+
model = SEQPeptide(classes=set_all.num_classes, q_encoder=args.model)
|
| 150 |
+
if len(args.pretrain) != 0:
|
| 151 |
+
logging.info('loading pretrain model')
|
| 152 |
+
# model = load_pretrain_model(model, torch.load(args.pretrain))
|
| 153 |
+
model_state = model.state_dict()
|
| 154 |
+
pretrained_state = torch.load(args.pretrain)
|
| 155 |
+
pretrained_state = {k: v for k, v in pretrained_state.items() if
|
| 156 |
+
k in model_state and v.size() == model_state[k].size()}
|
| 157 |
+
model_state.update(pretrained_state)
|
| 158 |
+
model.load_state_dict(model_state)
|
| 159 |
+
# model.load_state_dict(torch.load(args.pretrain), strict=False)
|
| 160 |
+
model.to(device)
|
| 161 |
+
# optimizer = torch.optim.Adam(model.parameters())
|
| 162 |
+
optimizer = torch.optim.AdamW(model.parameters(), lr=args.lr)
|
| 163 |
+
# optimizer = torch.optim.SGD(model.parameters(), lr=args.lr, momentum=True, weight_decay=5e-5)
|
| 164 |
+
weights_path = f"{weight_dir}/model_{fold + 1}.pth"
|
| 165 |
+
# early_stopping = EarlyStopping(patience=args.patience, path=weights_path)
|
| 166 |
+
logging.info(f'Running Cross Validation {fold + 1}')
|
| 167 |
+
logging.info(f'Fold {fold + 1} Train set:{len(train_set)}, Valid set:{len(valid_set)}, Test set:qlx {len(qlx_set)} saap {len(saap_set)}')
|
| 168 |
+
best_metric = 0
|
| 169 |
+
best_qlx = 0
|
| 170 |
+
best_saap = 0
|
| 171 |
+
start_time = time.time()
|
| 172 |
+
|
| 173 |
+
for epoch in range(1, args.epochs + 1):
|
| 174 |
+
if args.task_type in ('mlc', 'slc') :
|
| 175 |
+
train_loss, ap, f1, acc, auc = train(args, epoch, model, train_loader, valid_loader, device, criterion, optimizer)
|
| 176 |
+
logging.info(f'Epoch: {epoch:03d}, Train Loss: {train_loss:.3f}, ap: {ap:.3f}, f1: {f1:.3f}, acc: {acc:.3f}, auc: {auc:.3f}')
|
| 177 |
+
avg_metric = ap + f1 + acc + auc
|
| 178 |
+
if avg_metric > best_metric:
|
| 179 |
+
logging.info(f'Epoch: {epoch:03d} New Best validation metrics, running test session')
|
| 180 |
+
torch.save(model.state_dict(), weights_path)
|
| 181 |
+
best_metric = avg_metric
|
| 182 |
+
best_perform_list[fold] = np.asarray([ap, f1, acc, auc])
|
| 183 |
+
|
| 184 |
+
_, qlx_ap, qlx_f1, qlx_acc, qlx_auc = train(args, epoch, model, None, qlx_loader, device, None, None)
|
| 185 |
+
logging.info(f'Epoch: {epoch:03d} QLX results, ap: {qlx_ap:.3f}, f1: {qlx_f1:.3f}, acc: {qlx_acc:.3f}, auc: {qlx_auc:.3f}')
|
| 186 |
+
qlx_metric = qlx_ap + qlx_f1 + qlx_acc + qlx_auc
|
| 187 |
+
if qlx_metric > best_qlx:
|
| 188 |
+
best_qlx = qlx_metric
|
| 189 |
+
qlx_perform_list[fold] = np.asarray([qlx_ap, qlx_f1, qlx_acc, qlx_auc])
|
| 190 |
+
|
| 191 |
+
_, saap_ap, saap_f1, saap_acc, saap_auc = train(args, epoch, model, None, saap_loader, device, None, None)
|
| 192 |
+
logging.info(f'Epoch: {epoch:03d} SAAP results, ap: {saap_ap:.3f}, f1: {saap_f1:.3f}, acc: {saap_acc:.3f}, auc: {saap_auc:.3f}')
|
| 193 |
+
saap_metric = saap_ap + saap_f1 + saap_acc + saap_auc
|
| 194 |
+
if saap_metric > best_saap:
|
| 195 |
+
best_saap = saap_metric
|
| 196 |
+
saap_perform_list[fold] = np.asarray([saap_ap, saap_f1, saap_acc, saap_auc])
|
| 197 |
+
|
| 198 |
+
else:
|
| 199 |
+
raise NotImplementedError
|
| 200 |
+
|
| 201 |
+
logging.info(f'used time {(time.time()-start_time)/3600:.2f}h')
|
| 202 |
+
|
| 203 |
+
logging.info(f'Cross Validation Finished!')
|
| 204 |
+
best_perform_list = np.asarray(best_perform_list)
|
| 205 |
+
qlx_perform_list = np.asarray(qlx_perform_list)
|
| 206 |
+
saap_perform_list = np.asarray(saap_perform_list)
|
| 207 |
+
logging.info('Best validation perform list\n%s', best_perform_list)
|
| 208 |
+
logging.info('mean: %s', np.round(np.mean(best_perform_list, 0), 3))
|
| 209 |
+
logging.info('std: %s', np.round(np.std(best_perform_list, 0), 3))
|
| 210 |
+
logging.info('Best qlx perform list\n%s', qlx_perform_list)
|
| 211 |
+
logging.info('mean: %s', np.round(np.mean(qlx_perform_list, 0), 3))
|
| 212 |
+
logging.info('std: %s', np.round(np.std(qlx_perform_list, 0), 3))
|
| 213 |
+
logging.info('Best saap perform list\n%s', saap_perform_list)
|
| 214 |
+
logging.info('mean: %s', np.round(np.mean(saap_perform_list, 0), 3))
|
| 215 |
+
logging.info('std: %s', np.round(np.std(saap_perform_list, 0), 3))
|
| 216 |
+
perform = open(weight_dir+'/result.txt', 'w')
|
| 217 |
+
perform.write('Valid\n')
|
| 218 |
+
perform.write(','.join([str(i) for i in np.mean(best_perform_list, 0)])+'\n')
|
| 219 |
+
perform.write(','.join([str(i) for i in np.std(best_perform_list, 0)])+'\n')
|
| 220 |
+
perform.write('qlx\n')
|
| 221 |
+
perform.write(','.join([str(i) for i in np.mean(qlx_perform_list, 0)])+'\n')
|
| 222 |
+
perform.write(','.join([str(i) for i in np.std(qlx_perform_list, 0)])+'\n')
|
| 223 |
+
perform.write('saap\n')
|
| 224 |
+
perform.write(','.join([str(i) for i in np.mean(saap_perform_list, 0)])+'\n')
|
| 225 |
+
perform.write(','.join([str(i) for i in np.std(saap_perform_list, 0)])+'\n')
|
| 226 |
+
|
| 227 |
+
|
| 228 |
+
if __name__ == "__main__":
|
| 229 |
+
main()
|
t3.1/network.py
ADDED
|
@@ -0,0 +1,703 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
import torch.nn.functional as F
|
| 4 |
+
from mamba_ssm import Mamba
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
class Hamburger(nn.Module):
|
| 8 |
+
def __init__(self, inp, oup, reduction=32):
|
| 9 |
+
super(Hamburger, self).__init__()
|
| 10 |
+
self.pool_h = nn.AdaptiveAvgPool3d((1, None, None))
|
| 11 |
+
self.pool_w = nn.AdaptiveAvgPool3d((None, 1, None))
|
| 12 |
+
self.pool_d = nn.AdaptiveAvgPool3d((None, None, 1))
|
| 13 |
+
|
| 14 |
+
mip = max(8, inp // reduction)
|
| 15 |
+
self.conv1 = nn.Conv3d(inp, mip, kernel_size=1, stride=1, padding=0)
|
| 16 |
+
self.conv2 = nn.Conv3d(inp, mip, kernel_size=1, stride=1, padding=0)
|
| 17 |
+
self.conv3 = nn.Conv3d(inp, mip, kernel_size=1, stride=1, padding=0)
|
| 18 |
+
self.gn1 = nn.GroupNorm(8, mip)
|
| 19 |
+
self.gn2 = nn.GroupNorm(8, mip)
|
| 20 |
+
self.gn3 = nn.GroupNorm(8, mip)
|
| 21 |
+
|
| 22 |
+
self.act = nn.LeakyReLU(0.2)
|
| 23 |
+
self.conv_h = nn.Conv3d(mip, oup, kernel_size=1, stride=1, padding=0)
|
| 24 |
+
self.conv_w = nn.Conv3d(mip, oup, kernel_size=1, stride=1, padding=0)
|
| 25 |
+
self.conv_d = nn.Conv3d(mip, oup, kernel_size=1, stride=1, padding=0)
|
| 26 |
+
|
| 27 |
+
def forward(self, x):
|
| 28 |
+
n, c, h, w, d = x.size()
|
| 29 |
+
x_h = self.pool_h(x)
|
| 30 |
+
# print(x_h.shape)
|
| 31 |
+
x_w = self.pool_w(x).permute(0, 1, 3, 2, 4)
|
| 32 |
+
# print(x_w.shape)
|
| 33 |
+
x_d = self.pool_d(x).permute(0, 1, 4, 2, 3)
|
| 34 |
+
# print(x_d.shape)
|
| 35 |
+
y_hwd = torch.cat([x_h, x_w, x_d], dim=2)
|
| 36 |
+
# y_hd = torch.cat([x_h, x_d], dim=2)
|
| 37 |
+
# y_dw = torch.cat([x_d, x_w], dim=2)
|
| 38 |
+
y_hwd = self.conv1(y_hwd)
|
| 39 |
+
# y_hd = self.conv2(y_hd)
|
| 40 |
+
# y_dw = self.conv3(y_dw)
|
| 41 |
+
y_hwd = self.gn1(y_hwd)
|
| 42 |
+
# y_hd = self.gn2(y_hd)
|
| 43 |
+
# y_dw = self.gn3(y_dw)
|
| 44 |
+
y_hwd = self.act(y_hwd)
|
| 45 |
+
# y_hd = self.act(y_hd)
|
| 46 |
+
# y_dw = self.act(y_dw)
|
| 47 |
+
# print(y_hwd.shape)
|
| 48 |
+
x_h, x_w, x_d = torch.split(y_hwd, [1, 1, 1], dim=2)
|
| 49 |
+
x_w = x_w
|
| 50 |
+
x_h = x_h.permute(0, 1, 3, 2, 4)
|
| 51 |
+
x_d = x_d.permute(0, 1, 3, 4, 2)
|
| 52 |
+
a_h = self.conv_h(x_h).sigmoid()
|
| 53 |
+
a_w = self.conv_w(x_w).sigmoid()
|
| 54 |
+
a_d = self.conv_d(x_d).sigmoid()
|
| 55 |
+
a_hw = a_w * a_h
|
| 56 |
+
out = a_hw * a_d
|
| 57 |
+
return out + x
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
class BasicBlock(nn.Module):
|
| 61 |
+
expansion = 1
|
| 62 |
+
|
| 63 |
+
def __init__(self, in_channels, channels, stride=1):
|
| 64 |
+
super().__init__()
|
| 65 |
+
|
| 66 |
+
self.conv1 = nn.Conv3d(in_channels, channels, kernel_size=3, stride=stride, padding=1, bias=False)
|
| 67 |
+
self.bn1 = nn.BatchNorm3d(channels)
|
| 68 |
+
self.relu = nn.ReLU()
|
| 69 |
+
|
| 70 |
+
self.conv2 = nn.Conv3d(channels, channels, kernel_size=3, stride=1, padding=1, bias=False)
|
| 71 |
+
self.bn2 = nn.BatchNorm3d(channels)
|
| 72 |
+
|
| 73 |
+
self.downsample = nn.Sequential()
|
| 74 |
+
if stride != 1 or in_channels != channels * self.expansion:
|
| 75 |
+
self.downsample = nn.Sequential(
|
| 76 |
+
nn.Conv3d(in_channels, channels * self.expansion, kernel_size=1, stride=stride, bias=False),
|
| 77 |
+
nn.BatchNorm3d(channels * self.expansion)
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
self.stride = stride
|
| 81 |
+
|
| 82 |
+
def forward(self, x):
|
| 83 |
+
shortcut = self.downsample(x)
|
| 84 |
+
|
| 85 |
+
out = self.conv1(x)
|
| 86 |
+
out = self.bn1(out)
|
| 87 |
+
out = self.relu(out)
|
| 88 |
+
|
| 89 |
+
out = self.conv2(out)
|
| 90 |
+
out = self.bn2(out)
|
| 91 |
+
|
| 92 |
+
out += shortcut
|
| 93 |
+
out = self.relu(out)
|
| 94 |
+
|
| 95 |
+
return out
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
class Bottleneck(nn.Module):
|
| 99 |
+
expansion = 4
|
| 100 |
+
|
| 101 |
+
def __init__(self, in_channels, channels, stride=1):
|
| 102 |
+
super().__init__()
|
| 103 |
+
|
| 104 |
+
self.conv1 = nn.Conv3d(in_channels, channels, kernel_size=1, bias=False)
|
| 105 |
+
self.bn1 = nn.BatchNorm3d(channels)
|
| 106 |
+
self.relu = nn.ReLU()
|
| 107 |
+
|
| 108 |
+
self.conv2 = nn.Conv3d(channels, channels, kernel_size=3, stride=stride, padding=1, bias=False)
|
| 109 |
+
self.bn2 = nn.BatchNorm3d(channels)
|
| 110 |
+
|
| 111 |
+
self.conv3 = nn.Conv3d(channels, channels * self.expansion, kernel_size=1, bias=False)
|
| 112 |
+
self.bn3 = nn.BatchNorm3d(channels * self.expansion)
|
| 113 |
+
|
| 114 |
+
self.downsample = nn.Sequential()
|
| 115 |
+
if stride != 1 or in_channels != channels * self.expansion:
|
| 116 |
+
self.downsample = nn.Sequential(
|
| 117 |
+
nn.Conv3d(in_channels, channels * self.expansion, kernel_size=1, stride=stride, bias=False),
|
| 118 |
+
nn.BatchNorm3d(channels * self.expansion)
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
def forward(self, x):
|
| 122 |
+
shortcut = self.downsample(x)
|
| 123 |
+
|
| 124 |
+
out = self.conv1(x)
|
| 125 |
+
out = self.bn1(out)
|
| 126 |
+
out = self.relu(out)
|
| 127 |
+
|
| 128 |
+
out = self.conv2(out)
|
| 129 |
+
out = self.bn2(out)
|
| 130 |
+
out = self.relu(out)
|
| 131 |
+
|
| 132 |
+
out = self.conv3(out)
|
| 133 |
+
out = self.bn3(out)
|
| 134 |
+
|
| 135 |
+
out += shortcut
|
| 136 |
+
out = self.relu(out)
|
| 137 |
+
|
| 138 |
+
return out
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
class ResNet3D(nn.Module):
|
| 142 |
+
def __init__(self, block, layers, attention_module):
|
| 143 |
+
super().__init__()
|
| 144 |
+
if attention_module is not None:
|
| 145 |
+
self.attention_flag = True
|
| 146 |
+
self.attention_module = attention_module
|
| 147 |
+
else:
|
| 148 |
+
self.attention_flag = False
|
| 149 |
+
|
| 150 |
+
self.in_channels = 64
|
| 151 |
+
|
| 152 |
+
self.conv1 = nn.Conv3d(4, 64, kernel_size=(7, 7, 7), stride=(2, 2, 2), padding=(3, 3, 3), bias=False)
|
| 153 |
+
self.bn1 = nn.BatchNorm3d(64)
|
| 154 |
+
self.relu = nn.ReLU()
|
| 155 |
+
self.max_pool = nn.MaxPool3d(kernel_size=(3, 3, 3), stride=(2, 2, 2), padding=(1, 1, 1))
|
| 156 |
+
|
| 157 |
+
self.layer1 = self._make_layer(block, 64, layers[0], stride=1)
|
| 158 |
+
self.layer2 = self._make_layer(block, 128, layers[1], stride=2)
|
| 159 |
+
self.layer3 = self._make_layer(block, 256, layers[2], stride=2)
|
| 160 |
+
self.layer4 = self._make_layer(block, 512, layers[3], stride=2)
|
| 161 |
+
self.avg_pool = nn.AdaptiveAvgPool3d(1)
|
| 162 |
+
|
| 163 |
+
# initialize
|
| 164 |
+
for m in self.modules():
|
| 165 |
+
if isinstance(m, nn.Conv3d):
|
| 166 |
+
nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
|
| 167 |
+
elif isinstance(m, nn.BatchNorm3d):
|
| 168 |
+
m.weight.data.fill_(1)
|
| 169 |
+
m.bias.data.zero_()
|
| 170 |
+
|
| 171 |
+
def _make_layer(self, block, channels, n_blocks, stride=1):
|
| 172 |
+
assert n_blocks > 0, "number of blocks should be greater than zero"
|
| 173 |
+
layers = [block(self.in_channels, channels, stride)]
|
| 174 |
+
self.in_channels = channels * block.expansion
|
| 175 |
+
for i in range(1, n_blocks):
|
| 176 |
+
layers.append(block(self.in_channels, channels))
|
| 177 |
+
return nn.Sequential(*layers)
|
| 178 |
+
|
| 179 |
+
def forward(self, x, debug=False):
|
| 180 |
+
out = self.conv1(x)
|
| 181 |
+
out = self.bn1(out)
|
| 182 |
+
out = self.relu(out)
|
| 183 |
+
if debug:
|
| 184 |
+
print("shape1:", out.shape)
|
| 185 |
+
out = self.max_pool(out)
|
| 186 |
+
if debug:
|
| 187 |
+
print("shape2:", out.shape)
|
| 188 |
+
out = self.layer1(out)
|
| 189 |
+
if debug:
|
| 190 |
+
print("shape3:", out.shape)
|
| 191 |
+
out = self.layer2(out)
|
| 192 |
+
if debug:
|
| 193 |
+
print("shape4:", out.shape)
|
| 194 |
+
out = self.layer3(out)
|
| 195 |
+
if debug:
|
| 196 |
+
print("shape5:", out.shape)
|
| 197 |
+
out = self.layer4(out)
|
| 198 |
+
if self.attention_flag:
|
| 199 |
+
out = self.attention_module(out)
|
| 200 |
+
if debug:
|
| 201 |
+
print("shape6:", out.shape)
|
| 202 |
+
out = self.avg_pool(out)
|
| 203 |
+
if debug:
|
| 204 |
+
print("shape7:", out.shape)
|
| 205 |
+
|
| 206 |
+
out = out.view(out.size(0), -1)
|
| 207 |
+
return out
|
| 208 |
+
|
| 209 |
+
|
| 210 |
+
class FusionModule(nn.Module):
|
| 211 |
+
def __init__(self):
|
| 212 |
+
super().__init__()
|
| 213 |
+
self.f_value = nn.Sequential(
|
| 214 |
+
nn.Conv1d(32, 1, kernel_size=1, stride=1),
|
| 215 |
+
)
|
| 216 |
+
|
| 217 |
+
def forward(self, x, x_seq):
|
| 218 |
+
x = self.f_value(x)
|
| 219 |
+
print(x.shape)
|
| 220 |
+
|
| 221 |
+
|
| 222 |
+
class ResNet3DFusion(nn.Module):
|
| 223 |
+
def __init__(self, block, layers, attention_module):
|
| 224 |
+
super().__init__()
|
| 225 |
+
if attention_module is not None:
|
| 226 |
+
self.attention_flag = True
|
| 227 |
+
self.attention_module = attention_module
|
| 228 |
+
else:
|
| 229 |
+
self.attention_flag = False
|
| 230 |
+
|
| 231 |
+
self.in_channels = 64
|
| 232 |
+
|
| 233 |
+
self.conv1 = nn.Conv3d(3, 64, kernel_size=(7, 7, 7), stride=(2, 2, 2), padding=(3, 3, 3), bias=False)
|
| 234 |
+
self.bn1 = nn.BatchNorm3d(64)
|
| 235 |
+
self.relu = nn.ReLU()
|
| 236 |
+
self.max_pool = nn.MaxPool3d(kernel_size=(3, 3, 3), stride=(2, 2, 2), padding=(1, 1, 1))
|
| 237 |
+
|
| 238 |
+
self.layer1 = self._make_layer(block, 64, layers[0], stride=1)
|
| 239 |
+
self.layer2 = self._make_layer(block, 128, layers[1], stride=2)
|
| 240 |
+
self.layer3 = self._make_layer(block, 256, layers[2], stride=2)
|
| 241 |
+
self.layer4 = self._make_layer(block, 512, layers[3], stride=2)
|
| 242 |
+
self.avg_pool = nn.AdaptiveAvgPool3d(1)
|
| 243 |
+
self.fc1 = nn.Linear(256, 1)
|
| 244 |
+
self.fc2 = nn.Linear(256, 1)
|
| 245 |
+
self.fc3 = nn.Linear(256, 1)
|
| 246 |
+
self.fc4 = nn.Linear(256, 1)
|
| 247 |
+
self.fc5 = nn.Linear(256, 1)
|
| 248 |
+
self.fc6 = nn.Linear(256, 1)
|
| 249 |
+
|
| 250 |
+
# initialize
|
| 251 |
+
for m in self.modules():
|
| 252 |
+
if isinstance(m, nn.Conv3d):
|
| 253 |
+
nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
|
| 254 |
+
elif isinstance(m, nn.BatchNorm3d):
|
| 255 |
+
m.weight.data.fill_(1)
|
| 256 |
+
m.bias.data.zero_()
|
| 257 |
+
|
| 258 |
+
def _make_layer(self, block, channels, n_blocks, stride=1):
|
| 259 |
+
assert n_blocks > 0, "number of blocks should be greater than zero"
|
| 260 |
+
layers = [block(self.in_channels, channels, stride)]
|
| 261 |
+
self.in_channels = channels * block.expansion
|
| 262 |
+
for i in range(1, n_blocks):
|
| 263 |
+
layers.append(block(self.in_channels, channels))
|
| 264 |
+
return nn.Sequential(*layers)
|
| 265 |
+
|
| 266 |
+
def forward(self, x, x_seq, debug=False):
|
| 267 |
+
seq1 = self.fc1(x_seq)
|
| 268 |
+
seq2 = self.fc2(x_seq)
|
| 269 |
+
seq3 = self.fc3(x_seq)
|
| 270 |
+
seq4 = self.fc4(x_seq)
|
| 271 |
+
seq5 = self.fc5(x_seq)
|
| 272 |
+
out = self.conv1(x)
|
| 273 |
+
|
| 274 |
+
out = out * (seq1.unsqueeze(-1).unsqueeze(-1).unsqueeze(-1) + 1)
|
| 275 |
+
|
| 276 |
+
out = self.bn1(out)
|
| 277 |
+
out = self.relu(out)
|
| 278 |
+
if debug:
|
| 279 |
+
print("shape1:", out.shape)
|
| 280 |
+
out = self.max_pool(out)
|
| 281 |
+
if debug:
|
| 282 |
+
print("shape2:", out.shape)
|
| 283 |
+
out = self.layer1(out)
|
| 284 |
+
out = out * (seq2.unsqueeze(-1).unsqueeze(-1).unsqueeze(-1) + 1)
|
| 285 |
+
|
| 286 |
+
if debug:
|
| 287 |
+
print("shape3:", out.shape)
|
| 288 |
+
out = self.layer2(out)
|
| 289 |
+
out = out * (seq3.unsqueeze(-1).unsqueeze(-1).unsqueeze(-1) + 1)
|
| 290 |
+
|
| 291 |
+
if debug:
|
| 292 |
+
print("shape4:", out.shape)
|
| 293 |
+
out = self.layer3(out)
|
| 294 |
+
out = out * (seq4.unsqueeze(-1).unsqueeze(-1).unsqueeze(-1) + 1)
|
| 295 |
+
|
| 296 |
+
if debug:
|
| 297 |
+
print("shape5:", out.shape)
|
| 298 |
+
out = self.layer4(out)
|
| 299 |
+
out = out * (seq5.unsqueeze(-1).unsqueeze(-1).unsqueeze(-1) + 1)
|
| 300 |
+
|
| 301 |
+
if self.attention_flag:
|
| 302 |
+
out = self.attention_module(out)
|
| 303 |
+
if debug:
|
| 304 |
+
print("shape6:", out.shape)
|
| 305 |
+
out = self.avg_pool(out)
|
| 306 |
+
if debug:
|
| 307 |
+
print("shape7:", out.shape)
|
| 308 |
+
|
| 309 |
+
out = out.view(out.size(0), -1)
|
| 310 |
+
return out
|
| 311 |
+
|
| 312 |
+
|
| 313 |
+
class MLP(nn.Module):
|
| 314 |
+
def __init__(self, input_dim, hidden_dim, output_dim, num_layers, dropout_rate):
|
| 315 |
+
super(MLP, self).__init__()
|
| 316 |
+
|
| 317 |
+
layers = []
|
| 318 |
+
layers.append(nn.Linear(input_dim, hidden_dim))
|
| 319 |
+
layers.append(nn.ReLU())
|
| 320 |
+
layers.append(nn.Dropout(dropout_rate))
|
| 321 |
+
|
| 322 |
+
for _ in range(num_layers - 1):
|
| 323 |
+
layers.append(nn.Linear(hidden_dim, hidden_dim))
|
| 324 |
+
layers.append(nn.ReLU())
|
| 325 |
+
layers.append(nn.Dropout(dropout_rate))
|
| 326 |
+
|
| 327 |
+
layers.append(nn.Linear(hidden_dim, output_dim))
|
| 328 |
+
|
| 329 |
+
self.network = nn.Sequential(*layers)
|
| 330 |
+
|
| 331 |
+
def forward(self, x):
|
| 332 |
+
return self.network(x)
|
| 333 |
+
|
| 334 |
+
|
| 335 |
+
class PositionalEncoding(nn.Module):
|
| 336 |
+
def __init__(self, d_model, max_len=50):
|
| 337 |
+
super(PositionalEncoding, self).__init__()
|
| 338 |
+
|
| 339 |
+
pe = torch.zeros(max_len, d_model) # (max_len, d_model)
|
| 340 |
+
position = torch.arange(0, max_len, dtype=torch.float).unsqueeze(1) # (max_len, 1)
|
| 341 |
+
div_term = torch.exp(torch.arange(0, d_model, 2).float() *
|
| 342 |
+
(-torch.log(torch.FloatTensor([10000.0])) / d_model)) # (d_model/2,)
|
| 343 |
+
pe[:, 0::2] = torch.sin(position * div_term) # 偶数维
|
| 344 |
+
pe[:, 1::2] = torch.cos(position * div_term) # 奇数维
|
| 345 |
+
pe = pe.unsqueeze(0) # (1, max_len, d_model)
|
| 346 |
+
self.register_buffer('pe', pe)
|
| 347 |
+
|
| 348 |
+
def forward(self, x):
|
| 349 |
+
"""
|
| 350 |
+
x: (B, N, d_model)
|
| 351 |
+
"""
|
| 352 |
+
x = x + self.pe[:, :x.size(1), :]
|
| 353 |
+
return x
|
| 354 |
+
|
| 355 |
+
|
| 356 |
+
class TransformerModel(nn.Module):
|
| 357 |
+
def __init__(self, nheads, d_model, num_layers, out_dim, max_length=50):
|
| 358 |
+
super(TransformerModel, self).__init__()
|
| 359 |
+
|
| 360 |
+
# 嵌入层,将输入从 (B, N) 转换到 (B, N, embed_dim)
|
| 361 |
+
self.embedding = nn.Linear(1, d_model)
|
| 362 |
+
|
| 363 |
+
# 位置编码
|
| 364 |
+
self.pos_encoder = PositionalEncoding(d_model, max_length)
|
| 365 |
+
|
| 366 |
+
# Transformer 编码器层
|
| 367 |
+
encoder_layers = nn.TransformerEncoderLayer(d_model=d_model,
|
| 368 |
+
nhead=nheads,
|
| 369 |
+
activation='relu')
|
| 370 |
+
self.transformer_encoder = nn.TransformerEncoder(encoder_layers, num_layers=num_layers)
|
| 371 |
+
|
| 372 |
+
# 全局池化(可以根据任务选择不同的聚合方式)
|
| 373 |
+
self.global_pool = nn.AdaptiveAvgPool1d(1)
|
| 374 |
+
|
| 375 |
+
# 输出层
|
| 376 |
+
self.fc = nn.Linear(d_model, out_dim)
|
| 377 |
+
|
| 378 |
+
def forward(self, src):
|
| 379 |
+
"""
|
| 380 |
+
src: (B, N)
|
| 381 |
+
"""
|
| 382 |
+
# 嵌入
|
| 383 |
+
embedded = self.embedding(src.unsqueeze(-1)) # (B, N, embed_dim)
|
| 384 |
+
embedded = self.pos_encoder(embedded) # 添加位置编码
|
| 385 |
+
|
| 386 |
+
# 转置以适应 Transformer (N, B, embed_dim)
|
| 387 |
+
embedded = embedded.permute(1, 0, 2)
|
| 388 |
+
|
| 389 |
+
# Transformer 编码
|
| 390 |
+
transformer_out = self.transformer_encoder(embedded) # (N, B, embed_dim)
|
| 391 |
+
|
| 392 |
+
# 转置回 (B, N, embed_dim)
|
| 393 |
+
transformer_out = transformer_out.permute(1, 0, 2)
|
| 394 |
+
|
| 395 |
+
# 全局池化,将 (B, N, embed_dim) 转换为 (B, embed_dim)
|
| 396 |
+
pooled = self.global_pool(transformer_out.permute(0, 2, 1)).squeeze(-1)
|
| 397 |
+
|
| 398 |
+
# 输出层
|
| 399 |
+
output = self.fc(pooled) # (B, output_dim)
|
| 400 |
+
|
| 401 |
+
return output
|
| 402 |
+
|
| 403 |
+
|
| 404 |
+
class MambaModel(nn.Module):
|
| 405 |
+
def __init__(self, d_model, out_dim, max_length=30):
|
| 406 |
+
super(MambaModel, self).__init__()
|
| 407 |
+
self.linear = nn.Linear(in_features=1, out_features=d_model)
|
| 408 |
+
self.pos_encoder = PositionalEncoding(d_model, max_length)
|
| 409 |
+
self.mamba = Mamba(d_model=d_model)
|
| 410 |
+
self.global_pool = nn.AdaptiveAvgPool1d(1)
|
| 411 |
+
self.fc = nn.Linear(d_model * 2, out_dim)
|
| 412 |
+
|
| 413 |
+
def forward(self, x: torch.Tensor):
|
| 414 |
+
x = self.pos_encoder(self.linear(x.unsqueeze(-1)))
|
| 415 |
+
y = self.mamba(x)
|
| 416 |
+
y_flip = self.mamba(x.flip([-2])).flip([-2])
|
| 417 |
+
y = torch.cat((y, y_flip), dim=-1)
|
| 418 |
+
y = self.fc(self.global_pool(y.permute(0, 2, 1)).squeeze(-1))
|
| 419 |
+
return y
|
| 420 |
+
|
| 421 |
+
|
| 422 |
+
class SEQ(nn.Module):
|
| 423 |
+
def __init__(self, seq_type='mlp', input_dim=21, hidden_dim=128, out_dim=128, num_layers=2, max_length=30):
|
| 424 |
+
super(SEQ, self).__init__()
|
| 425 |
+
self.seq_type = seq_type
|
| 426 |
+
if seq_type == 'rnn':
|
| 427 |
+
self.rnn = nn.RNN(
|
| 428 |
+
input_size=input_dim,
|
| 429 |
+
hidden_size=hidden_dim,
|
| 430 |
+
num_layers=num_layers,
|
| 431 |
+
batch_first=True, # input & output will take batch size as 1 dim (batch, time_step, input_size)
|
| 432 |
+
bidirectional=True
|
| 433 |
+
)
|
| 434 |
+
elif seq_type == 'gru':
|
| 435 |
+
self.rnn = nn.GRU(
|
| 436 |
+
input_size=input_dim,
|
| 437 |
+
hidden_size=hidden_dim,
|
| 438 |
+
num_layers=num_layers,
|
| 439 |
+
batch_first=True, # input & output will take batch size as 1 dim (batch, time_step, input_size)
|
| 440 |
+
bidirectional=True
|
| 441 |
+
)
|
| 442 |
+
elif seq_type == 'lstm':
|
| 443 |
+
self.rnn = nn.LSTM(
|
| 444 |
+
input_size=input_dim,
|
| 445 |
+
hidden_size=hidden_dim,
|
| 446 |
+
num_layers=num_layers,
|
| 447 |
+
batch_first=True, # input & output will take batch size as 1 dim (batch, time_step, input_size)
|
| 448 |
+
bidirectional=True
|
| 449 |
+
)
|
| 450 |
+
elif seq_type == 'tf':
|
| 451 |
+
self.transformer = TransformerModel(nheads=4, d_model=hidden_dim, num_layers=2, out_dim=out_dim, max_length=max_length)
|
| 452 |
+
elif seq_type == 'mamba':
|
| 453 |
+
self.mamba = MambaModel(d_model=hidden_dim, out_dim=out_dim, max_length=max_length)
|
| 454 |
+
elif self.seq_type == 'mlp':
|
| 455 |
+
self.rnn = MLP(30, hidden_dim, out_dim, 3, 0.3)
|
| 456 |
+
else:
|
| 457 |
+
raise NotImplementedError(f'\'{seq_type}\' not implemented')
|
| 458 |
+
|
| 459 |
+
self.rnn_fc = nn.Sequential(
|
| 460 |
+
nn.Linear(hidden_dim * 2, hidden_dim),
|
| 461 |
+
nn.ReLU(),
|
| 462 |
+
nn.Linear(hidden_dim, out_dim)
|
| 463 |
+
)
|
| 464 |
+
|
| 465 |
+
def forward(self, seq):
|
| 466 |
+
if self.seq_type == 'mlp':
|
| 467 |
+
return self.rnn(seq)
|
| 468 |
+
elif self.seq_type == 'tf':
|
| 469 |
+
return self.transformer(seq)
|
| 470 |
+
elif self.seq_type == 'mamba':
|
| 471 |
+
return self.mamba(seq)
|
| 472 |
+
else:
|
| 473 |
+
one_hot_seq = F.one_hot(seq.to(torch.int64), num_classes=21).float()
|
| 474 |
+
r_out = self.rnn(one_hot_seq, None)[0] # None represents zero initial hidden state
|
| 475 |
+
out = self.rnn_fc(r_out[:, -1, :])
|
| 476 |
+
return out
|
| 477 |
+
# def forward(self, x, seq_lengths):
|
| 478 |
+
#
|
| 479 |
+
|
| 480 |
+
|
| 481 |
+
class VoxPeptide(nn.Module):
|
| 482 |
+
def __init__(self, v_encoder='resnet26', q_encoder='mlp', fusion='mlp', classes=6, attention=None):
|
| 483 |
+
super().__init__()
|
| 484 |
+
|
| 485 |
+
if attention == 'hamburger':
|
| 486 |
+
self.attention = Hamburger(2048, 2048)
|
| 487 |
+
else:
|
| 488 |
+
self.attention = None
|
| 489 |
+
# v_encoder could be resnet26 or resnet50
|
| 490 |
+
if v_encoder == 'resnet26':
|
| 491 |
+
self.v_encoder = ResNet3D(Bottleneck, [1, 2, 4, 1], self.attention)
|
| 492 |
+
|
| 493 |
+
if fusion == 'mlp':
|
| 494 |
+
self.fusion = nn.Linear(512 * 4 + 256, classes)
|
| 495 |
+
elif fusion == 'att':
|
| 496 |
+
self.fusion = nn.Linear(512 * 4 + 256, classes)
|
| 497 |
+
else:
|
| 498 |
+
raise NotImplementedError
|
| 499 |
+
self.vox_fc = nn.Linear(2048, classes)
|
| 500 |
+
|
| 501 |
+
def forward(self, x, seq_lengths=None):
|
| 502 |
+
vox, seq = x
|
| 503 |
+
seq_emb = self.v_encoder(vox)
|
| 504 |
+
pred = self.vox_fc(seq_emb)
|
| 505 |
+
return pred
|
| 506 |
+
|
| 507 |
+
|
| 508 |
+
class SEQPeptide(nn.Module):
|
| 509 |
+
def __init__(self, v_encoder='resnet26', q_encoder='mlp', fusion='mlp', classes=6, attention=None, max_length=30):
|
| 510 |
+
super().__init__()
|
| 511 |
+
self.classes = classes
|
| 512 |
+
# q_encoder could be mlp, gru, rnn, lstm, transformer
|
| 513 |
+
self.q_encoder = SEQ(seq_type=q_encoder, max_length=max_length)
|
| 514 |
+
|
| 515 |
+
self.seq_fc = nn.Linear(128, classes)
|
| 516 |
+
|
| 517 |
+
def forward(self, x, seq_lengths=None):
|
| 518 |
+
vox, seq = x
|
| 519 |
+
seq_emb = self.q_encoder(seq)
|
| 520 |
+
pred = self.seq_fc(seq_emb)
|
| 521 |
+
return pred
|
| 522 |
+
|
| 523 |
+
|
| 524 |
+
import torch
|
| 525 |
+
import torch.nn as nn
|
| 526 |
+
import torch.nn.functional as F
|
| 527 |
+
|
| 528 |
+
|
| 529 |
+
class ConvNet(nn.Module):
|
| 530 |
+
def __init__(self, num_classes: int = 2):
|
| 531 |
+
super(ConvNet, self).__init__()
|
| 532 |
+
self.conv1 = nn.Conv1d(3, 16, 3, padding=1)
|
| 533 |
+
self.conv2 = nn.Conv1d(16, 32, 3, padding=1)
|
| 534 |
+
self.fc1 = nn.Linear(32 * 7, 128)
|
| 535 |
+
# self.fc2 = nn.Linear(128, num_classes)
|
| 536 |
+
|
| 537 |
+
def forward(self, x):
|
| 538 |
+
# print(x.shape)
|
| 539 |
+
x = x.permute(0, 2, 1)
|
| 540 |
+
x = F.relu(self.conv1(x))
|
| 541 |
+
x = F.max_pool1d(x, 2)
|
| 542 |
+
x = F.relu(self.conv2(x))
|
| 543 |
+
x = F.max_pool1d(x, 2)
|
| 544 |
+
x = x.view(x.shape[0], -1)
|
| 545 |
+
return self.fc1(x)
|
| 546 |
+
# x = F.relu(self.fc1(x))
|
| 547 |
+
# x = self.fc2(x)
|
| 548 |
+
# return x
|
| 549 |
+
|
| 550 |
+
|
| 551 |
+
class ConvNet2D(nn.Module):
|
| 552 |
+
def __init__(self, num_classes: int = 2):
|
| 553 |
+
super(ConvNet2D, self).__init__()
|
| 554 |
+
self.conv1 = nn.Conv2d(1, 8, 3, padding=1)
|
| 555 |
+
self.conv2 = nn.Conv2d(8, 16, 3, padding=2, stride=2)
|
| 556 |
+
self.conv3 = nn.Conv2d(16, 32, 3, padding=2, stride=2)
|
| 557 |
+
# self.pool = nn.AdaptiveAvgPool2d(32)
|
| 558 |
+
self.fc1 = nn.Linear(32 * 3 * 9, 128)
|
| 559 |
+
# self.fc2 = nn.Linear(128, num_classes)
|
| 560 |
+
|
| 561 |
+
def forward(self, x):
|
| 562 |
+
# print(x.shape)
|
| 563 |
+
x = x.unsqueeze(1)
|
| 564 |
+
x = F.relu(self.conv1(x))
|
| 565 |
+
# print(x.shape)
|
| 566 |
+
x = F.relu(self.conv2(x))
|
| 567 |
+
# print(x.shape)
|
| 568 |
+
x = F.relu(self.conv3(x))
|
| 569 |
+
# x = self.pool(x)
|
| 570 |
+
# print(x.shape)
|
| 571 |
+
x = x.view(x.shape[0], -1)
|
| 572 |
+
return self.fc1(x)
|
| 573 |
+
|
| 574 |
+
|
| 575 |
+
# convnet = ConvNet()
|
| 576 |
+
# print(convnet)
|
| 577 |
+
class MMPeptide(nn.Module):
|
| 578 |
+
def __init__(self, v_encoder='resnet26', q_encoder='mlp', fusion='mlp', classes=6, attention=None, max_length=30):
|
| 579 |
+
super().__init__()
|
| 580 |
+
if attention == 'hamburger':
|
| 581 |
+
self.attention = Hamburger(2048, 2048)
|
| 582 |
+
else:
|
| 583 |
+
self.attention = None
|
| 584 |
+
# v_encoder could be resnet26 or resnet50
|
| 585 |
+
if v_encoder == 'resnet26':
|
| 586 |
+
self.v_encoder = ResNet3D(Bottleneck, [1, 2, 4, 1], self.attention)
|
| 587 |
+
# self.v_encoder = SwinUNETR(img_size=(64, 64, 64), in_channels=3, out_channels=1)
|
| 588 |
+
elif v_encoder == 'resnet50':
|
| 589 |
+
self.v_encoder = ResNet3D(Bottleneck, [3, 4, 6, 3], self.attention)
|
| 590 |
+
else:
|
| 591 |
+
raise NotImplementedError
|
| 592 |
+
|
| 593 |
+
# q_encoder could be mlp, gru, rnn, lstm, transformer
|
| 594 |
+
self.q_encoder = SEQ(seq_type=q_encoder, max_length=max_length)
|
| 595 |
+
# self.ss_encoder = SEQ(seq_type=q_encoder)
|
| 596 |
+
if fusion == 'mlp':
|
| 597 |
+
self.fusion = nn.Linear(512 * 4 + 256, 256)
|
| 598 |
+
# self.fusion = nn.Linear(192 + 256, classes)
|
| 599 |
+
elif fusion == 'att':
|
| 600 |
+
self.fusion = nn.Linear(512 * 4 + 256, 256)
|
| 601 |
+
else:
|
| 602 |
+
raise NotImplementedError
|
| 603 |
+
|
| 604 |
+
# self.vox_fc = nn.Linear(2048, classes)
|
| 605 |
+
# self.seq_fc = nn.Linear(256, classes)
|
| 606 |
+
self.out = nn.Sequential(nn.ReLU(inplace=True), nn.Linear(256, classes))
|
| 607 |
+
self.classes = classes
|
| 608 |
+
|
| 609 |
+
def forward(self, x, seq_lengths=None):
|
| 610 |
+
vox, seq = x
|
| 611 |
+
# print(vox.shape)
|
| 612 |
+
# print(seq.shape)
|
| 613 |
+
vox_emb = self.v_encoder(vox)
|
| 614 |
+
# print(vox_emb.shape)
|
| 615 |
+
seq_emb = self.q_encoder(seq, seq_lengths)
|
| 616 |
+
# print(seq_emb.shape)
|
| 617 |
+
# ss_emb = self.ss_encoder(second_s)
|
| 618 |
+
fused_feature = torch.cat((seq_emb, vox_emb), dim=1)
|
| 619 |
+
pred = self.fusion(fused_feature)
|
| 620 |
+
pred = self.out(pred)
|
| 621 |
+
# pred1 = self.vox_fc(vox_emb)
|
| 622 |
+
# pred2 = self.seq_fc(seq_emb)
|
| 623 |
+
# return pred, fused_feature
|
| 624 |
+
return pred
|
| 625 |
+
|
| 626 |
+
|
| 627 |
+
class SMPeptide(nn.Module):
|
| 628 |
+
def __init__(self, v_encoder='resnet26', q_encoder='mlp', fusion='mlp', classes=6, attention=None, hidden_dim=256, max_length=30):
|
| 629 |
+
super().__init__()
|
| 630 |
+
self.siamese_encoder1 = MMPeptide(v_encoder, q_encoder, fusion, classes, attention, max_length)
|
| 631 |
+
# self.siamese_encoder2 = MMPeptide(v_encoder, q_encoder, fusion, classes, attention)
|
| 632 |
+
self.fc = nn.Sequential(
|
| 633 |
+
nn.Linear(hidden_dim * 2, hidden_dim),
|
| 634 |
+
nn.ReLU(),
|
| 635 |
+
nn.Linear(hidden_dim, 1)
|
| 636 |
+
)
|
| 637 |
+
|
| 638 |
+
def forward(self, x, seq_lengths=None):
|
| 639 |
+
f_mutated = self.siamese_encoder1(x[0])
|
| 640 |
+
f_wide_type = self.siamese_encoder1(x[1])
|
| 641 |
+
return self.fc(torch.cat((f_mutated, f_wide_type), dim=1))
|
| 642 |
+
|
| 643 |
+
|
| 644 |
+
class MMFPeptide(nn.Module):
|
| 645 |
+
def __init__(self, v_encoder='resnet26', q_encoder='mlp', fusion='mlp', classes=6, attention=None, max_length=30):
|
| 646 |
+
super().__init__()
|
| 647 |
+
if attention == 'hamburger':
|
| 648 |
+
self.attention = Hamburger(2048, 2048)
|
| 649 |
+
else:
|
| 650 |
+
self.attention = None
|
| 651 |
+
# v_encoder could be resnet26 or resnet50
|
| 652 |
+
if v_encoder == 'resnet26':
|
| 653 |
+
self.v_encoder = ResNet3D(Bottleneck, [1, 2, 4, 1], self.attention)
|
| 654 |
+
# self.v_encoder = ResNet3DFusion(Bottleneck, [1, 2, 4, 1], self.attention)
|
| 655 |
+
elif v_encoder == 'resnet50':
|
| 656 |
+
self.v_encoder = ResNet3D(Bottleneck, [3, 4, 6, 3], self.attention)
|
| 657 |
+
else:
|
| 658 |
+
raise NotImplementedError
|
| 659 |
+
|
| 660 |
+
# q_encoder could be mlp, gru, rnn, lstm, transformer
|
| 661 |
+
self.q_encoder = SEQ(seq_type=q_encoder, max_length=max_length)
|
| 662 |
+
|
| 663 |
+
if fusion == 'mlp':
|
| 664 |
+
self.fusion = nn.Linear(512 * 4 + 256, classes)
|
| 665 |
+
elif fusion == 'att':
|
| 666 |
+
self.fusion = nn.Linear(512 * 4 + 256, classes)
|
| 667 |
+
else:
|
| 668 |
+
raise NotImplementedError
|
| 669 |
+
|
| 670 |
+
self.vox_fc = nn.Linear(2048, classes)
|
| 671 |
+
self.seq_fc = nn.Linear(256, classes)
|
| 672 |
+
|
| 673 |
+
def forward(self, x, seq_lengths=None):
|
| 674 |
+
vox, seq = x
|
| 675 |
+
# print(vox.shape)
|
| 676 |
+
# print(seq.shape)
|
| 677 |
+
seq_emb = self.q_encoder(seq, seq_lengths)
|
| 678 |
+
|
| 679 |
+
vox_emb = self.v_encoder(vox, seq_emb)
|
| 680 |
+
# print(vox_emb.shape)
|
| 681 |
+
# print(seq_emb.shape)
|
| 682 |
+
fused_feature = torch.cat((seq_emb, vox_emb), dim=1)
|
| 683 |
+
pred = self.fusion(fused_feature)
|
| 684 |
+
# pred1 = self.vox_fc(vox_emb)
|
| 685 |
+
# pred2 = self.seq_fc(seq_emb)
|
| 686 |
+
return pred
|
| 687 |
+
|
| 688 |
+
|
| 689 |
+
if __name__ == "__main__":
|
| 690 |
+
# model = MMFPeptide()
|
| 691 |
+
# voxel = torch.zeros((4, 3, 64, 64, 64))
|
| 692 |
+
# # # h_in = torch.zeros((2, 2048, 2, 2, 2))
|
| 693 |
+
# # # h = Hamburger(2048, 2048)
|
| 694 |
+
# # # h(h_in)
|
| 695 |
+
# seq = torch.ones((4, 50))
|
| 696 |
+
# res = model.forward((voxel, seq))
|
| 697 |
+
# out = model((voxel, seq))
|
| 698 |
+
# print(out.shape)
|
| 699 |
+
# model = ConvNet2D()
|
| 700 |
+
input_seq = torch.ones((4, 1, 30))
|
| 701 |
+
# model(input_seq)
|
| 702 |
+
transformer = TransformerModel(nhead=4, d_model=32, num_layers=2)
|
| 703 |
+
print(transformer(input_seq).shape)
|
t3.1/run/gru-bce256-0.001-50/config.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
{"model": "gru", "fusion": "1", "pdb_src": "af", "task_type": "mlc", "data_ver": "0920", "task": "all", "classes": 6, "max_length": 30, "split": 5, "seed": 1, "threshold": 128, "gpu": 0, "batch_size": 256, "epochs": 50, "lr": 0.001, "decay": 0.0005, "warm_steps": 0, "patience": 10, "pretrain": "", "metric_avg": "macro", "loss": "bce", "bias_curri": false, "anti_curri": false, "std_coff": 1}
|
t3.1/run/gru-bce256-0.001-50/model_1.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:44a0a6f59469bd945e6ab7336a04da049f63e8fe61edea1e8206747da4fba871
|
| 3 |
+
size 1854596
|
t3.1/run/gru-bce256-0.001-50/model_2.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:792b01df7874d7680ab772d874e789376080f24814ef1774dcfac3520342c39f
|
| 3 |
+
size 1854596
|
t3.1/run/gru-bce256-0.001-50/model_3.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:c7fbec6fe9eb8b38fdfeb78fc848e1a8169e8f5df510d2b5b3b9d3c69315df10
|
| 3 |
+
size 1854596
|
t3.1/run/gru-bce256-0.001-50/model_4.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:ca22553d99c46dd857f653f3b055aadd1c53045c4a6c118c01d422f65917c178
|
| 3 |
+
size 1854596
|
t3.1/run/gru-bce256-0.001-50/model_5.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:69b590f53b23c804cd9dc4efc21b7bdfa66f42a811aca6e82600f465d7f7d45e
|
| 3 |
+
size 1854596
|
t3.1/run/gru-bce256-0.001-50/result.txt
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Valid
|
| 2 |
+
0.8988269329071045,0.8484792709350586,0.8013780236244201,0.8573168516159058
|
| 3 |
+
0.007939289205324655,0.0059540444194092395,0.0067192045478746375,0.005264435899967903
|
| 4 |
+
qlx
|
| 5 |
+
0.8493218064308167,0.7297939658164978,0.6150110244750977,0.874067509174347
|
| 6 |
+
0.0029503253825771893,0.012699909074253446,0.02536615358364534,0.006737497012955599
|
| 7 |
+
saap
|
| 8 |
+
0.5456169724464417,0.5129449367523193,0.38499999046325684,0.6531985998153687
|
| 9 |
+
0.016126342152109306,0.0,0.0,0.013445912126869347
|
t3.1/run/lstm-bce256-0.001-50/config.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
{"model": "lstm", "fusion": "1", "pdb_src": "af", "task_type": "mlc", "data_ver": "0920", "task": "all", "classes": 6, "max_length": 30, "split": 5, "seed": 1, "threshold": 128, "gpu": 0, "batch_size": 256, "epochs": 50, "lr": 0.001, "decay": 0.0005, "warm_steps": 0, "patience": 10, "pretrain": "", "metric_avg": "macro", "loss": "bce", "bias_curri": false, "anti_curri": false, "std_coff": 1}
|
t3.1/run/lstm-bce256-0.001-50/model_1.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:b1e44b7386d60d1b1903626529e7c892d650229ddfc752f93155d36a6f515eda
|
| 3 |
+
size 2404484
|