Skip to content

Commit e923d19

Browse files
committed
Add external conservation provider with cache, health check, and documentation
1 parent 34a742c commit e923d19

13 files changed

Lines changed: 993 additions & 11 deletions

File tree

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ apply plugin: "com.github.ben-manes.versions" // ./gradlew dependencyUpdates
2222

2323

2424
group = 'cz.siret'
25-
version = '2.6.0-dev.1'
25+
version = '2.6.0-dev.2'
2626

2727
description = 'Ligand binding site prediction based on machine learning.'
2828

documentation/conservation.md

Lines changed: 304 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,304 @@
1+
# Conservation Scores in P2Rank
2+
3+
## Background
4+
5+
Sequence conservation is a strong signal for predicting binding sites.
6+
Conservation-aware models have been available in [PrankWeb](https://prankweb.cz) for several years,
7+
but the underlying conservation pipeline was tightly integrated into the PrankWeb infrastructure
8+
and not separately usable by P2Rank users running predictions locally.
9+
10+
Since **P2Rank 2.6.0** (including early `-dev` builds), users can obtain conservation scores
11+
on the fly from an external conservation server and use conservation-aware prediction models
12+
directly from the command line.
13+
14+
## Conservation-Aware Models
15+
16+
P2Rank ships with pre-trained models that include conservation features:
17+
18+
| Config | Description | Usage |
19+
|--------|-------------|-------|
20+
| `conservation_hmm` | For standard experimental structures (X-ray) | `-c conservation_hmm` |
21+
| `alphafold_conservation_hmm` | For AlphaFold, cryo-EM, and NMR models (no b-factor feature) | `-c alphafold_conservation_hmm` |
22+
23+
Note: The pre-trained conservation-aware models shipped with P2Rank were trained
24+
using HMM-based conservation scores (Jensen-Shannon divergence, `.hom` format).
25+
Using conservation scores from a different method (e.g. with different score distribution or format)
26+
is not expected to produce good results and may not work at all.
27+
If you want to use a different conservation method, you would need to retrain the model.
28+
29+
## Quick Start
30+
31+
```bash
32+
# Predict with conservation (fetching scores from a server)
33+
prank predict protein.pdb \
34+
-c conservation_hmm \
35+
-conservation_type hmm \
36+
-conservation_provider hmm_server \
37+
-conservation_provider_url http://localhost:8030
38+
39+
# Preload conservation cache for a dataset, then predict
40+
prank preload-conservation dataset.ds \
41+
-conservation_type hmm \
42+
-conservation_provider hmm_server \
43+
-conservation_provider_url http://localhost:8030
44+
45+
prank predict dataset.ds \
46+
-c conservation_hmm \
47+
-conservation_type hmm \
48+
-conservation_provider hmm_server \
49+
-conservation_provider_url http://localhost:8030
50+
```
51+
52+
## Obtaining Conservation Scores
53+
54+
There are two ways to provide conservation scores to P2Rank:
55+
56+
### 1. Pre-computed Score Files
57+
58+
Place `.hom` score files next to the protein files (or in directories specified by `-conservation_dirs`).
59+
Files are matched by naming convention: `{proteinBaseName}_{chainId}.hom` (e.g., `2W83_A.hom`).
60+
61+
```bash
62+
prank predict protein.pdb -c conservation_hmm -conservation_dirs ./my_scores/
63+
```
64+
65+
Note: `-conservation_type` is only needed when using a provider or the cache mechanism.
66+
With pre-computed files and `-conservation_dirs`, files are matched by name convention alone.
67+
68+
### 2. External Conservation Server (since 2.6.0)
69+
70+
Configure P2Rank to fetch scores from an HTTP server on demand.
71+
Fetched scores are cached locally so subsequent runs reuse them.
72+
73+
```bash
74+
prank predict protein.pdb \
75+
-c conservation_hmm \
76+
-conservation_type hmm \
77+
-conservation_provider hmm_server \
78+
-conservation_provider_url http://localhost:8030
79+
```
80+
81+
The server receives a POST request to `{url}/conservation` with a JSON body:
82+
```json
83+
{"fasta_content": ">proteinName_chainId\nSEQUENCE"}
84+
```
85+
and returns the raw `.hom` TSV content.
86+
87+
## Running the Conservation Server with Docker
88+
89+
The HMM conservation pipeline is available as a Docker image
90+
(not published on Docker Hub yet, but can be built from `prankweb` repo).
91+
92+
Note: These instructions will be simplified in the near future
93+
when the conservation server becomes part of the official P2Rank docker image
94+
or is published as a separate dedicated docker image.
95+
96+
See also https://github.com/rdk/prankweb/tree/conservation-server/executor-p2rank/conservation.
97+
98+
```bash
99+
# for now, you need to build the image from the `conservation-server` branch of the prankweb repo fork
100+
git clone -b conservation-server git@github.com:rdk/prankweb.git
101+
cd prankweb
102+
103+
# to avoid running docker as root, add current user to docker group (you may need to log out and back in for this to take effect)
104+
sudo usermod -aG docker $USER
105+
106+
# prepare data/cache directory on the host
107+
mkdir -p /ssd/p2rank-conservation-docker-data/hmm-based
108+
109+
# download latest uniref database
110+
cd /ssd/p2rank-conservation-docker-data/hmm-based
111+
wget https://ftp.expasy.org/databases/uniprot/current_release/uniref/uniref50/uniref50.fasta.gz && gunzip uniref50.fasta.gz
112+
113+
# build/rebuild docker image (only necessary after changes in this repo)
114+
docker compose build conservation-server
115+
116+
# run server with mounted data directory
117+
docker compose run --rm -p 8030:8030 \
118+
--user "$(id -u):$(id -g)" \
119+
-v /ssd/p2rank-conservation-docker-data:/data/conservation \
120+
conservation-server
121+
```
122+
123+
Once the server is running, you can verify it is available:
124+
```bash
125+
curl http://localhost:8030/health
126+
```
127+
128+
### Server-side Cache
129+
130+
The conservation server maintains a sequence-indexed result cache
131+
in `/ssd/p2rank-conservation-docker-data/hmm-based-cache` (inside the mounted data directory).
132+
Repeated queries for the same sequence are served immediately from this cache.
133+
134+
There is also an option to download a cache of conservation scores pre-computed by the PrankWeb server
135+
for most PDB entries and some AlphaFold DB subsets (available upon request).
136+
However, these were calculated with older versions of the UniRef database (approximately 2023 or older),
137+
so results may differ from scores computed with a current UniRef release.
138+
139+
## Local Cache of Conservation Scores
140+
141+
By default, cache files are stored next to each protein file in `.p2rank-cache` directory.
142+
For example, for protein `{protein_dir}/{baseName}.pdb` conservation files would be cached in:
143+
```
144+
{protein_dir}/.p2rank-cache/conservation/{conservation_type}/{baseName}_{chainId}.hom
145+
```
146+
147+
To use a custom cache directory instead use the `-conservation_cache_dir` parameter:
148+
```bash
149+
prank predict protein.pdb \
150+
-c conservation_hmm \
151+
-conservation_type hmm \
152+
-conservation_provider hmm_server \
153+
-conservation_provider_url http://localhost:8030 \
154+
-conservation_cache_dir /path/to/custom/cache
155+
```
156+
157+
Cache layout: `{conservation_cache_dir}/{conservation_type}/{baseName}_{chainId}.hom`
158+
159+
### Disabling Cache
160+
161+
To disable caching entirely (always fetch from server), add `-conservation_disable_cache true`
162+
to your command.
163+
164+
### Score Resolution Order
165+
166+
When a conservation provider is configured, scores are resolved in this order:
167+
1. Pre-computed files (from `conservation_dirs` or the protein file directory)
168+
2. Local cache
169+
3. Fetch from the conservation server (result is cached)
170+
171+
## Preloading Conservation Cache (Pre-calculating Conservation Scores)
172+
173+
Note that conservation calculation is computationally intensive
174+
and typically takes several minutes per chain (depending on sequence length and hardware).
175+
176+
The `preload-conservation` command fetches and caches conservation scores for all chains in a dataset
177+
without running predictions. This is useful on large datasets for catching any errors related to conservation
178+
retrieval/calculation before starting a full prediction run.
179+
180+
```bash
181+
prank preload-conservation dataset.ds \
182+
-conservation_type hmm \
183+
-conservation_provider hmm_server \
184+
-conservation_provider_url http://localhost:8030
185+
```
186+
187+
The command reports progress and a summary of cached/fetched/failed chains.
188+
189+
Some long sequences may exceed the default timeout.
190+
If some chains fail with timeout errors, you can safely re-run the same command.
191+
Already cached chains will be skipped, and only the failed ones will be retried.
192+
Use `-conservation_provider_timeout` to increase the per-request timeout:
193+
194+
```bash
195+
prank preload-conservation dataset.ds \
196+
-conservation_type hmm \
197+
-conservation_provider hmm_server \
198+
-conservation_provider_url http://localhost:8030 \
199+
-conservation_provider_timeout 1200 # 20 minutes, default is 600 seconds (10 minutes)
200+
```
201+
202+
You can also control the number of concurrent requests to avoid overloading the server:
203+
204+
```bash
205+
prank preload-conservation dataset.ds \
206+
-conservation_type hmm \
207+
-conservation_provider hmm_server \
208+
-conservation_provider_url http://localhost:8030 \
209+
-conservation_provider_threads 4
210+
```
211+
212+
### Preloading to a Dedicated Directory for Reproducible Experiments
213+
214+
For reproducible experiments, it is better to preload conservation scores into a dedicated directory
215+
next to the dataset file and then use `-conservation_dirs` to point to it during prediction.
216+
This way the scores are stored in a known location, are easy to version or share,
217+
and you don't rely on the implicit per-protein cache mechanism.
218+
219+
```bash
220+
# 1. Preload scores into a dedicated directory next to the dataset file
221+
prank preload-conservation dataset.ds \
222+
-conservation_type hmm \
223+
-conservation_provider hmm_server \
224+
-conservation_provider_url http://localhost:8030 \
225+
-conservation_cache_dir ./conservation
226+
227+
# 2. Run prediction using -conservation_dirs, with cache disabled
228+
# (scores are loaded directly from the directory, no cache is read or written)
229+
prank predict dataset.ds \
230+
-c alphafold_conservation_hmm \
231+
-conservation_dirs ./conservation/hmm \
232+
-conservation_disable_cache true
233+
```
234+
235+
Note: `-conservation_dirs` points to `./conservation/hmm` because preloading
236+
creates the layout `{conservation_cache_dir}/{conservation_type}/`, so score files
237+
end up in `./conservation/hmm/*.hom`.
238+
239+
## Parameters Reference
240+
241+
### Core
242+
243+
| Parameter | Default | Description |
244+
|-----------|---------|-------------|
245+
| `conservation_type` | `null` | Type of conservation scores. Determines cache subdirectory. Currently: `hmm` |
246+
| `conservation_dirs` | `[]` | Directories to search for pre-computed score files. Relative to dataset dir. |
247+
248+
### Provider
249+
250+
| Parameter | Default | Description |
251+
|-----------|---------|-------------|
252+
| `conservation_provider` | `null` | External provider type. Currently: `hmm_server`. Null = use pre-computed files only. |
253+
| `conservation_provider_url` | `null` | Base URL of the conservation server. Required when provider is set. |
254+
| `conservation_provider_timeout` | `600` | Per-request timeout in seconds. |
255+
| `conservation_provider_threads` | `0` | Max concurrent requests to the server. 0 = use `-threads` value. |
256+
257+
### Cache
258+
259+
| Parameter | Default | Description |
260+
|-----------|---------|-------------|
261+
| `conservation_cache_dir` | `null` | Centralized cache directory. Null = cache next to each protein file. |
262+
| `conservation_disable_cache` | `false` | When true, cache is neither read nor written. |
263+
264+
### Other
265+
266+
| Parameter | Default | Description |
267+
|-----------|---------|-------------|
268+
| `fail_on_conserv_seq_mismatch` | `false` | Fail when sequences in the structure and score file do not match exactly. |
269+
270+
## Non-Standard Residue Mapping
271+
272+
PDB structures often contain modified (non-canonical) amino acid residues (e.g., selenomethionine MSE, phosphoserine SEP).
273+
Before sending sequences to the conservation server or matching them against score files,
274+
P2Rank maps these residues to standard amino acids according to the `-aa_mapping` parameter.
275+
276+
For example, with `-aa_mapping pdbfixer`, MSE is mapped to MET, SEP to SER, etc.
277+
This affects the FASTA sequence that P2Rank sends to the conservation provider
278+
and the sequence used for alignment between structure residues and conservation scores.
279+
280+
See [aa-mapping.md](aa-mapping.md) for details on available mapping modes and custom mapping files.
281+
282+
## Debugging Sequence and Conservation Mapping
283+
284+
P2Rank provides several `analyze` subcommands useful for inspecting how sequences are processed:
285+
286+
```bash
287+
# Export raw FASTA sequences as P2Rank sees them (after aa_mapping, before masking)
288+
prank analyze fasta-raw dataset.ds
289+
290+
# Export masked FASTA sequences (non-standard residue codes replaced with X)
291+
prank analyze fasta-masked dataset.ds
292+
293+
# Analyze conservation scores mapped to residues and optionally produce PyMOL visualizations
294+
prank analyze conservation dataset.ds -threads 1 -visualizations true
295+
```
296+
297+
`fasta-raw` shows the sequence after applying `-aa_mapping` but without any further masking.
298+
`fasta-masked` additionally replaces non-standard one-letter codes with `X`.
299+
This is the version sent to the conservation server and used for mapping scores to residues.
300+
Comparing the two outputs helps identify residues that may cause mismatches with conservation score files.
301+
302+
The `conservation` subcommand loads and prints per-residue conservation scores for each chain.
303+
With `-visualizations true`, it also generates PyMOL visualization scripts.
304+

0 commit comments

Comments
 (0)