protein-quest 0.5.0__py3-none-any.whl → 0.6.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of protein-quest might be problematic. Click here for more details.
- protein_quest/__version__.py +1 -1
- protein_quest/alphafold/confidence.py +2 -2
- protein_quest/alphafold/fetch.py +28 -19
- protein_quest/cli.py +133 -68
- protein_quest/filters.py +2 -5
- protein_quest/io.py +350 -0
- protein_quest/mcp_server.py +8 -5
- protein_quest/ss.py +3 -7
- protein_quest/{pdbe/io.py → structure.py} +53 -126
- protein_quest/uniprot.py +7 -3
- protein_quest/utils.py +26 -2
- {protein_quest-0.5.0.dist-info → protein_quest-0.6.0.dist-info}/METADATA +12 -1
- protein_quest-0.6.0.dist-info/RECORD +27 -0
- protein_quest-0.5.0.dist-info/RECORD +0 -26
- {protein_quest-0.5.0.dist-info → protein_quest-0.6.0.dist-info}/WHEEL +0 -0
- {protein_quest-0.5.0.dist-info → protein_quest-0.6.0.dist-info}/entry_points.txt +0 -0
- {protein_quest-0.5.0.dist-info → protein_quest-0.6.0.dist-info}/licenses/LICENSE +0 -0
protein_quest/io.py
ADDED
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
"""Module for structure file input/output."""
|
|
2
|
+
|
|
3
|
+
import gzip
|
|
4
|
+
import logging
|
|
5
|
+
import shutil
|
|
6
|
+
import tempfile
|
|
7
|
+
from collections.abc import Generator, Iterable
|
|
8
|
+
from io import StringIO
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
from typing import Literal, get_args
|
|
11
|
+
from urllib.request import urlopen
|
|
12
|
+
|
|
13
|
+
import gemmi
|
|
14
|
+
from mmcif.api.DictionaryApi import DictionaryApi
|
|
15
|
+
from mmcif.io.BinaryCifReader import BinaryCifReader
|
|
16
|
+
from mmcif.io.BinaryCifWriter import BinaryCifWriter
|
|
17
|
+
from mmcif.io.PdbxReader import PdbxReader
|
|
18
|
+
from mmcif.io.PdbxWriter import PdbxWriter
|
|
19
|
+
|
|
20
|
+
from protein_quest.utils import CopyMethod, copyfile, user_cache_root_dir
|
|
21
|
+
|
|
22
|
+
logger = logging.getLogger(__name__)
|
|
23
|
+
|
|
24
|
+
# TODO remove once v0.7.4 of gemmi is released,
|
|
25
|
+
# as uv pip install git+https://github.com/project-gemmi/gemmi.git installs 0.7.4.dev0 which does not print leaks
|
|
26
|
+
# Swallow gemmi leaked function warnings
|
|
27
|
+
gemmi.set_leak_warnings(False)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
StructureFileExtensions = Literal[".pdb", ".pdb.gz", ".ent", ".ent.gz", ".cif", ".cif.gz", ".bcif", ".bcif.gz"]
|
|
31
|
+
"""Type of supported structure file extensions."""
|
|
32
|
+
valid_structure_file_extensions: set[str] = set(get_args(StructureFileExtensions))
|
|
33
|
+
"""Set of valid structure file extensions."""
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def write_structure(structure: gemmi.Structure, path: Path):
|
|
37
|
+
"""Write a gemmi structure to a file.
|
|
38
|
+
|
|
39
|
+
Args:
|
|
40
|
+
structure: The gemmi structure to write.
|
|
41
|
+
path: The file path to write the structure to.
|
|
42
|
+
The format depends on the file extension.
|
|
43
|
+
See [StructureFileExtensions][protein_quest.io.StructureFileExtensions]
|
|
44
|
+
for supported extensions.
|
|
45
|
+
|
|
46
|
+
Raises:
|
|
47
|
+
ValueError: If the file extension is not supported.
|
|
48
|
+
"""
|
|
49
|
+
if path.name.endswith(".pdb") or path.name.endswith(".ent"):
|
|
50
|
+
body: str = structure.make_pdb_string()
|
|
51
|
+
path.write_text(body)
|
|
52
|
+
elif path.name.endswith(".pdb.gz") or path.name.endswith(".ent.gz"):
|
|
53
|
+
body: str = structure.make_pdb_string()
|
|
54
|
+
with gzip.open(path, "wt") as f:
|
|
55
|
+
f.write(body)
|
|
56
|
+
elif path.name.endswith(".cif"):
|
|
57
|
+
# do not write chem_comp so it is viewable by molstar
|
|
58
|
+
# see https://github.com/project-gemmi/gemmi/discussions/362
|
|
59
|
+
doc = structure.make_mmcif_document(gemmi.MmcifOutputGroups(True, chem_comp=False))
|
|
60
|
+
doc.write_file(str(path))
|
|
61
|
+
elif path.name.endswith(".cif.gz"):
|
|
62
|
+
doc = structure.make_mmcif_document(gemmi.MmcifOutputGroups(True, chem_comp=False))
|
|
63
|
+
cif_str = doc.as_string()
|
|
64
|
+
with gzip.open(path, "wt") as f:
|
|
65
|
+
f.write(cif_str)
|
|
66
|
+
elif path.name.endswith(".bcif"):
|
|
67
|
+
structure2bcif(structure, path)
|
|
68
|
+
elif path.name.endswith(".bcif.gz"):
|
|
69
|
+
structure2bcifgz(structure, path)
|
|
70
|
+
else:
|
|
71
|
+
msg = f"Unsupported file extension in {path.name}. Supported extensions are: {valid_structure_file_extensions}"
|
|
72
|
+
raise ValueError(msg)
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def read_structure(file: Path) -> gemmi.Structure:
|
|
76
|
+
"""Read a structure from a file.
|
|
77
|
+
|
|
78
|
+
Args:
|
|
79
|
+
file: Path to the input structure file.
|
|
80
|
+
See [StructureFileExtensions][protein_quest.io.StructureFileExtensions]
|
|
81
|
+
for supported extensions.
|
|
82
|
+
|
|
83
|
+
Returns:
|
|
84
|
+
A gemmi Structure object representing the structure in the file.
|
|
85
|
+
"""
|
|
86
|
+
if file.name.endswith(".bcif"):
|
|
87
|
+
return bcif2structure(file)
|
|
88
|
+
if file.name.endswith(".bcif.gz"):
|
|
89
|
+
return bcifgz2structure(file)
|
|
90
|
+
return gemmi.read_structure(str(file))
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def bcif2cif(bcif_file: Path) -> str:
|
|
94
|
+
"""Convert a binary CIF (bcif) file to a CIF string.
|
|
95
|
+
|
|
96
|
+
Args:
|
|
97
|
+
bcif_file: Path to the binary CIF file.
|
|
98
|
+
|
|
99
|
+
Returns:
|
|
100
|
+
A string containing the CIF representation of the structure.
|
|
101
|
+
"""
|
|
102
|
+
reader = BinaryCifReader()
|
|
103
|
+
container = reader.deserialize(str(bcif_file))
|
|
104
|
+
capture = StringIO()
|
|
105
|
+
writer = PdbxWriter(capture)
|
|
106
|
+
writer.write(container)
|
|
107
|
+
return capture.getvalue()
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def bcifgz2structure(bcif_gz_file: Path) -> gemmi.Structure:
|
|
111
|
+
"""Read a binary CIF (bcif) gzipped file and return a gemmi Structure object.
|
|
112
|
+
|
|
113
|
+
This is slower than other formats because gemmi does not support reading bcif files directly.
|
|
114
|
+
So we first gunzip the file to a temporary location, convert it to a cif string using mmcif package,
|
|
115
|
+
and then read the cif string using gemmi.
|
|
116
|
+
|
|
117
|
+
Args:
|
|
118
|
+
bcif_gz_file: Path to the binary CIF gzipped file.
|
|
119
|
+
|
|
120
|
+
Returns:
|
|
121
|
+
A gemmi Structure object representing the structure in the bcif.gz file.
|
|
122
|
+
"""
|
|
123
|
+
with tempfile.NamedTemporaryFile(suffix=".bcif", delete=True) as tmp_bcif:
|
|
124
|
+
tmp_path = Path(tmp_bcif.name)
|
|
125
|
+
gunzip_file(bcif_gz_file, output_file=tmp_path, keep_original=True)
|
|
126
|
+
return bcif2structure(tmp_path)
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
def bcif2structure(bcif_file: Path) -> gemmi.Structure:
|
|
130
|
+
"""Read a binary CIF (bcif) file and return a gemmi Structure object.
|
|
131
|
+
|
|
132
|
+
This is slower than other formats because gemmi does not support reading bcif files directly.
|
|
133
|
+
So we convert it to a cif string first using mmcif package and then read the cif string using gemmi.
|
|
134
|
+
|
|
135
|
+
Args:
|
|
136
|
+
bcif_file: Path to the binary CIF file.
|
|
137
|
+
|
|
138
|
+
Returns:
|
|
139
|
+
A gemmi Structure object representing the structure in the bcif file.
|
|
140
|
+
"""
|
|
141
|
+
cif_content = bcif2cif(bcif_file)
|
|
142
|
+
doc = gemmi.cif.read_string(cif_content)
|
|
143
|
+
block = doc.sole_block()
|
|
144
|
+
return gemmi.make_structure_from_block(block)
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
def _initialize_dictionary_api(containers) -> DictionaryApi:
|
|
148
|
+
dict_local = user_cache_root_dir() / "mmcif_pdbx_v5_next.dic"
|
|
149
|
+
if not dict_local.exists():
|
|
150
|
+
dict_url = "https://raw.githubusercontent.com/wwpdb-dictionaries/mmcif_pdbx/master/dist/mmcif_pdbx_v5_next.dic"
|
|
151
|
+
logger.info("Downloading mmcif dictionary from %s to %s", dict_url, dict_local)
|
|
152
|
+
dict_local.parent.mkdir(parents=True, exist_ok=True)
|
|
153
|
+
with dict_local.open("wb") as f, urlopen(dict_url) as response: # noqa: S310 url is hardcoded and https
|
|
154
|
+
f.write(response.read())
|
|
155
|
+
return DictionaryApi(containerList=containers, consolidate=True)
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
def structure2bcif(structure: gemmi.Structure, bcif_file: Path):
|
|
159
|
+
"""Write a gemmi Structure object to a binary CIF (bcif) file.
|
|
160
|
+
|
|
161
|
+
This is slower than other formats because gemmi does not support writing bcif files directly.
|
|
162
|
+
So we convert it to a cif string first using gemmi and then convert cif to bcif using mmcif package.
|
|
163
|
+
|
|
164
|
+
Args:
|
|
165
|
+
structure: The gemmi Structure object to write.
|
|
166
|
+
bcif_file: Path to the output binary CIF file.
|
|
167
|
+
"""
|
|
168
|
+
doc = structure.make_mmcif_document(gemmi.MmcifOutputGroups(True, chem_comp=False))
|
|
169
|
+
containers = []
|
|
170
|
+
with StringIO(doc.as_string()) as sio:
|
|
171
|
+
reader = PdbxReader(sio)
|
|
172
|
+
reader.read(containers)
|
|
173
|
+
dict_api = _initialize_dictionary_api(containers)
|
|
174
|
+
writer = BinaryCifWriter(dictionaryApi=dict_api)
|
|
175
|
+
writer.serialize(str(bcif_file), containers)
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
def gunzip_file(gz_file: Path, output_file: Path | None = None, keep_original: bool = True) -> Path:
|
|
179
|
+
"""Unzip a .gz file.
|
|
180
|
+
|
|
181
|
+
Args:
|
|
182
|
+
gz_file: Path to the .gz file.
|
|
183
|
+
output_file: Optional path to the output unzipped file. If None, the .gz suffix is removed from gz_file.
|
|
184
|
+
keep_original: Whether to keep the original .gz file. Default is True.
|
|
185
|
+
|
|
186
|
+
Returns:
|
|
187
|
+
Path to the unzipped file.
|
|
188
|
+
|
|
189
|
+
Raises:
|
|
190
|
+
ValueError: If output_file is None and gz_file does not end with .gz.
|
|
191
|
+
"""
|
|
192
|
+
if output_file is None and not gz_file.name.endswith(".gz"):
|
|
193
|
+
msg = f"If output_file is not provided, {gz_file} must end with .gz"
|
|
194
|
+
raise ValueError(msg)
|
|
195
|
+
out_file = output_file or gz_file.with_suffix("")
|
|
196
|
+
with gzip.open(gz_file, "rb") as f_in, out_file.open("wb") as f_out:
|
|
197
|
+
shutil.copyfileobj(f_in, f_out)
|
|
198
|
+
if not keep_original:
|
|
199
|
+
gz_file.unlink()
|
|
200
|
+
return out_file
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
def structure2bcifgz(structure: gemmi.Structure, bcif_gz_file: Path):
|
|
204
|
+
"""Write a gemmi Structure object to a binary CIF gzipped (bcif.gz) file.
|
|
205
|
+
|
|
206
|
+
This is slower than other formats because gemmi does not support writing bcif files directly.
|
|
207
|
+
So we convert it to a cif string first using gemmi and then convert cif to bcif using mmcif package.
|
|
208
|
+
Finally, we gzip the bcif file.
|
|
209
|
+
|
|
210
|
+
Args:
|
|
211
|
+
structure: The gemmi Structure object to write.
|
|
212
|
+
bcif_gz_file: Path to the output binary CIF gzipped file.
|
|
213
|
+
"""
|
|
214
|
+
with tempfile.NamedTemporaryFile(suffix=".bcif", delete=True) as tmp_bcif:
|
|
215
|
+
tmp_path = Path(tmp_bcif.name)
|
|
216
|
+
structure2bcif(structure, tmp_path)
|
|
217
|
+
with tmp_path.open("rb") as f_in, gzip.open(bcif_gz_file, "wb") as f_out:
|
|
218
|
+
shutil.copyfileobj(f_in, f_out)
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
def convert_to_cif_files(
|
|
222
|
+
input_files: Iterable[Path], output_dir: Path, copy_method: CopyMethod
|
|
223
|
+
) -> Generator[tuple[Path, Path]]:
|
|
224
|
+
"""Convert structure files to .cif format.
|
|
225
|
+
|
|
226
|
+
Args:
|
|
227
|
+
input_files: Iterable of structure files to convert.
|
|
228
|
+
output_dir: Directory to save the converted .cif files.
|
|
229
|
+
copy_method: How to copy when no changes are needed to output file.
|
|
230
|
+
|
|
231
|
+
Yields:
|
|
232
|
+
A tuple of the input file and the output file.
|
|
233
|
+
"""
|
|
234
|
+
for input_file in input_files:
|
|
235
|
+
output_file = convert_to_cif_file(input_file, output_dir, copy_method)
|
|
236
|
+
yield input_file, output_file
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
def convert_to_cif_file(input_file: Path, output_dir: Path, copy_method: CopyMethod) -> Path:
|
|
240
|
+
"""Convert a single structure file to .cif format.
|
|
241
|
+
|
|
242
|
+
Args:
|
|
243
|
+
input_file: The structure file to convert.
|
|
244
|
+
See [StructureFileExtensions][protein_quest.io.StructureFileExtensions]
|
|
245
|
+
for supported extensions.
|
|
246
|
+
output_dir: Directory to save the converted .cif file.
|
|
247
|
+
copy_method: How to copy when no changes are needed to output file.
|
|
248
|
+
|
|
249
|
+
Returns:
|
|
250
|
+
Path to the converted .cif file.
|
|
251
|
+
"""
|
|
252
|
+
name, extension = split_name_and_extension(input_file.name)
|
|
253
|
+
output_file = output_dir / f"{name}.cif"
|
|
254
|
+
if output_file.exists():
|
|
255
|
+
logger.info("Output file %s already exists for input file %s. Skipping.", output_file, input_file)
|
|
256
|
+
elif extension in {".pdb", ".pdb.gz", ".ent", ".ent.gz"}:
|
|
257
|
+
structure = read_structure(input_file)
|
|
258
|
+
write_structure(structure, output_file)
|
|
259
|
+
elif extension == ".cif":
|
|
260
|
+
logger.info("File %s is already in .cif format, copying to %s", input_file, output_dir)
|
|
261
|
+
copyfile(input_file, output_file, copy_method)
|
|
262
|
+
elif extension == ".cif.gz":
|
|
263
|
+
gunzip_file(input_file, output_file=output_file, keep_original=True)
|
|
264
|
+
elif extension == ".bcif":
|
|
265
|
+
with output_file.open("w") as f:
|
|
266
|
+
f.write(bcif2cif(input_file))
|
|
267
|
+
else:
|
|
268
|
+
msg = (
|
|
269
|
+
f"Unsupported file extension {extension} in {input_file}. "
|
|
270
|
+
f"Supported extensions are {valid_structure_file_extensions}."
|
|
271
|
+
)
|
|
272
|
+
raise ValueError(msg)
|
|
273
|
+
return output_file
|
|
274
|
+
|
|
275
|
+
|
|
276
|
+
def split_name_and_extension(name: str) -> tuple[str, str]:
|
|
277
|
+
"""Split a filename into its name and extension.
|
|
278
|
+
|
|
279
|
+
`.gz` is considered part of the extension if present.
|
|
280
|
+
|
|
281
|
+
Examples:
|
|
282
|
+
Some example usages.
|
|
283
|
+
|
|
284
|
+
>>> from protein_quest.pdbe.io import split_name_and_extension
|
|
285
|
+
>>> split_name_and_extension("1234.pdb")
|
|
286
|
+
('1234', '.pdb')
|
|
287
|
+
>>> split_name_and_extension("1234.pdb.gz")
|
|
288
|
+
('1234', '.pdb.gz')
|
|
289
|
+
|
|
290
|
+
Args:
|
|
291
|
+
name: The filename to split.
|
|
292
|
+
|
|
293
|
+
Returns:
|
|
294
|
+
A tuple containing the name and the extension.
|
|
295
|
+
"""
|
|
296
|
+
ext = ""
|
|
297
|
+
if name.endswith(".gz"):
|
|
298
|
+
ext = ".gz"
|
|
299
|
+
name = name.removesuffix(".gz")
|
|
300
|
+
i = name.rfind(".")
|
|
301
|
+
if 0 < i < len(name) - 1:
|
|
302
|
+
ext = name[i:] + ext
|
|
303
|
+
name = name[:i]
|
|
304
|
+
return name, ext
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
def locate_structure_file(root: Path, pdb_id: str) -> Path:
|
|
308
|
+
"""Locate a structure file for a given PDB ID in the specified directory.
|
|
309
|
+
|
|
310
|
+
Uses [StructureFileExtensions][protein_quest.io.StructureFileExtensions] as potential extensions.
|
|
311
|
+
Also tries different casing of the PDB ID.
|
|
312
|
+
|
|
313
|
+
Args:
|
|
314
|
+
root: The root directory to search in.
|
|
315
|
+
pdb_id: The PDB ID to locate.
|
|
316
|
+
|
|
317
|
+
Returns:
|
|
318
|
+
The path to the located structure file.
|
|
319
|
+
|
|
320
|
+
Raises:
|
|
321
|
+
FileNotFoundError: If no structure file is found for the given PDB ID.
|
|
322
|
+
"""
|
|
323
|
+
for ext in valid_structure_file_extensions:
|
|
324
|
+
candidates = (
|
|
325
|
+
root / f"{pdb_id}{ext}",
|
|
326
|
+
root / f"{pdb_id.lower()}{ext}",
|
|
327
|
+
root / f"{pdb_id.upper()}{ext}",
|
|
328
|
+
root / f"pdb{pdb_id.lower()}{ext}",
|
|
329
|
+
)
|
|
330
|
+
for candidate in candidates:
|
|
331
|
+
if candidate.exists():
|
|
332
|
+
return candidate
|
|
333
|
+
msg = f"No structure file found for {pdb_id} in {root}"
|
|
334
|
+
raise FileNotFoundError(msg)
|
|
335
|
+
|
|
336
|
+
|
|
337
|
+
def glob_structure_files(input_dir: Path) -> Generator[Path]:
|
|
338
|
+
"""Glob for structure files in a directory.
|
|
339
|
+
|
|
340
|
+
Uses [StructureFileExtensions][protein_quest.io.StructureFileExtensions] as valid extensions.
|
|
341
|
+
Does not search recursively.
|
|
342
|
+
|
|
343
|
+
Args:
|
|
344
|
+
input_dir: The input directory to search for structure files.
|
|
345
|
+
|
|
346
|
+
Yields:
|
|
347
|
+
Paths to the found structure files.
|
|
348
|
+
"""
|
|
349
|
+
for ext in valid_structure_file_extensions:
|
|
350
|
+
yield from input_dir.glob(f"*{ext}")
|
protein_quest/mcp_server.py
CHANGED
|
@@ -45,9 +45,10 @@ from protein_quest.alphafold.fetch import AlphaFoldEntry, DownloadableFormat
|
|
|
45
45
|
from protein_quest.alphafold.fetch import fetch_many as alphafold_fetch
|
|
46
46
|
from protein_quest.emdb import fetch as emdb_fetch
|
|
47
47
|
from protein_quest.go import search_gene_ontology_term
|
|
48
|
+
from protein_quest.io import convert_to_cif_file, glob_structure_files
|
|
48
49
|
from protein_quest.pdbe.fetch import fetch as pdbe_fetch
|
|
49
|
-
from protein_quest.pdbe.io import glob_structure_files, nr_residues_in_chain, write_single_chain_pdb_file
|
|
50
50
|
from protein_quest.ss import filter_file_on_secondary_structure
|
|
51
|
+
from protein_quest.structure import nr_residues_in_chain, write_single_chain_structure_file
|
|
51
52
|
from protein_quest.taxonomy import search_taxon
|
|
52
53
|
from protein_quest.uniprot import (
|
|
53
54
|
PdbResult,
|
|
@@ -112,18 +113,18 @@ def extract_single_chain_from_structure(
|
|
|
112
113
|
out_chain: str = "A",
|
|
113
114
|
) -> Path:
|
|
114
115
|
"""
|
|
115
|
-
Extract a single chain from a mmCIF
|
|
116
|
+
Extract a single chain from a structure (mmCIF or pdb) file and write to a new file.
|
|
116
117
|
|
|
117
118
|
Args:
|
|
118
|
-
input_file: Path to the input mmCIF
|
|
119
|
+
input_file: Path to the input structure (mmCIF or pdb) file.
|
|
119
120
|
chain2keep: The chain to keep.
|
|
120
121
|
output_dir: Directory to save the output file.
|
|
121
122
|
out_chain: The chain identifier for the output file.
|
|
122
123
|
|
|
123
124
|
Returns:
|
|
124
|
-
Path to the output mmCIF
|
|
125
|
+
Path to the output structure (mmCIF or pdb) file
|
|
125
126
|
"""
|
|
126
|
-
return
|
|
127
|
+
return write_single_chain_structure_file(input_file, chain2keep, output_dir, out_chain)
|
|
127
128
|
|
|
128
129
|
|
|
129
130
|
@mcp.tool
|
|
@@ -199,6 +200,8 @@ def alphafold_confidence_filter(file: Path, query: ConfidenceFilterQuery, filter
|
|
|
199
200
|
|
|
200
201
|
mcp.tool(filter_file_on_secondary_structure)
|
|
201
202
|
|
|
203
|
+
mcp.tool(convert_to_cif_file)
|
|
204
|
+
|
|
202
205
|
|
|
203
206
|
@mcp.prompt
|
|
204
207
|
def candidate_structures(
|
protein_quest/ss.py
CHANGED
|
@@ -5,17 +5,13 @@ from collections.abc import Generator, Iterable
|
|
|
5
5
|
from dataclasses import dataclass
|
|
6
6
|
from pathlib import Path
|
|
7
7
|
|
|
8
|
-
from gemmi import Structure
|
|
8
|
+
from gemmi import Structure
|
|
9
9
|
|
|
10
10
|
from protein_quest.converter import PositiveInt, Ratio, converter
|
|
11
|
+
from protein_quest.io import read_structure
|
|
11
12
|
|
|
12
13
|
logger = logging.getLogger(__name__)
|
|
13
14
|
|
|
14
|
-
# TODO remove once v0.7.4 of gemmi is released,
|
|
15
|
-
# as uv pip install git+https://github.com/project-gemmi/gemmi.git installs 0.7.4.dev0 which does not print leaks
|
|
16
|
-
# Swallow gemmi leaked function warnings
|
|
17
|
-
set_leak_warnings(False)
|
|
18
|
-
|
|
19
15
|
# TODO if a structure has no secondary structure information, calculate it with `gemmi ss`.
|
|
20
16
|
# https://github.com/MonomerLibrary/monomers/wiki/Installation as --monomers dir
|
|
21
17
|
# gemmi executable is in https://pypi.org/project/gemmi-program/
|
|
@@ -261,7 +257,7 @@ def filter_file_on_secondary_structure(
|
|
|
261
257
|
Returns:
|
|
262
258
|
Filtering statistics and whether file passed.
|
|
263
259
|
"""
|
|
264
|
-
structure = read_structure(
|
|
260
|
+
structure = read_structure(file_path)
|
|
265
261
|
return filter_on_secondary_structure(structure, query)
|
|
266
262
|
|
|
267
263
|
|
|
@@ -1,51 +1,29 @@
|
|
|
1
|
-
"""Module for
|
|
1
|
+
"""Module for querying and modifying [gemmi structures][gemmi.Structure]."""
|
|
2
2
|
|
|
3
|
-
import gzip
|
|
4
3
|
import logging
|
|
5
|
-
from collections.abc import
|
|
4
|
+
from collections.abc import Iterable
|
|
6
5
|
from datetime import UTC, datetime
|
|
7
6
|
from pathlib import Path
|
|
8
7
|
|
|
9
8
|
import gemmi
|
|
10
9
|
|
|
11
10
|
from protein_quest.__version__ import __version__
|
|
11
|
+
from protein_quest.io import read_structure, split_name_and_extension, write_structure
|
|
12
12
|
from protein_quest.utils import CopyMethod, copyfile
|
|
13
13
|
|
|
14
14
|
logger = logging.getLogger(__name__)
|
|
15
15
|
|
|
16
|
-
# TODO remove once v0.7.4 of gemmi is released,
|
|
17
|
-
# as uv pip install git+https://github.com/project-gemmi/gemmi.git installs 0.7.4.dev0 which does not print leaks
|
|
18
|
-
# Swallow gemmi leaked function warnings
|
|
19
|
-
gemmi.set_leak_warnings(False)
|
|
20
16
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
"""Returns the number of residues in a specific chain from a mmCIF/pdb file.
|
|
17
|
+
def find_chain_in_model(model: gemmi.Model, wanted_chain: str) -> gemmi.Chain | None:
|
|
18
|
+
"""Find a chain in a model.
|
|
24
19
|
|
|
25
20
|
Args:
|
|
26
|
-
|
|
27
|
-
|
|
21
|
+
model: The gemmi model to search in.
|
|
22
|
+
wanted_chain: The chain identifier to search for.
|
|
28
23
|
|
|
29
24
|
Returns:
|
|
30
|
-
The
|
|
25
|
+
The found chain or None if not found.
|
|
31
26
|
"""
|
|
32
|
-
structure = gemmi.read_structure(str(file))
|
|
33
|
-
gchain = find_chain_in_structure(structure, chain)
|
|
34
|
-
if gchain is None:
|
|
35
|
-
logger.warning("Chain %s not found in %s. Returning 0.", chain, file)
|
|
36
|
-
return 0
|
|
37
|
-
return len(gchain)
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
def find_chain_in_structure(structure: gemmi.Structure, wanted_chain: str) -> gemmi.Chain | None:
|
|
41
|
-
for model in structure:
|
|
42
|
-
chain = find_chain_in_model(model, wanted_chain)
|
|
43
|
-
if chain is not None:
|
|
44
|
-
return chain
|
|
45
|
-
return None
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
def find_chain_in_model(model: gemmi.Model, wanted_chain: str) -> gemmi.Chain | None:
|
|
49
27
|
chain = model.find_chain(wanted_chain)
|
|
50
28
|
if chain is None:
|
|
51
29
|
# For chain A in 4v92 the find_chain method returns None,
|
|
@@ -57,106 +35,39 @@ def find_chain_in_model(model: gemmi.Model, wanted_chain: str) -> gemmi.Chain |
|
|
|
57
35
|
return chain
|
|
58
36
|
|
|
59
37
|
|
|
60
|
-
def
|
|
61
|
-
"""
|
|
62
|
-
|
|
63
|
-
Args:
|
|
64
|
-
structure: The gemmi structure to write.
|
|
65
|
-
path: The file path to write the structure to.
|
|
66
|
-
The format depends on the file extension.
|
|
67
|
-
Supported extensions are .pdb, .pdb.gz, .cif, .cif.gz.
|
|
68
|
-
|
|
69
|
-
Raises:
|
|
70
|
-
ValueError: If the file extension is not supported.
|
|
71
|
-
"""
|
|
72
|
-
if path.name.endswith(".pdb"):
|
|
73
|
-
body: str = structure.make_pdb_string()
|
|
74
|
-
path.write_text(body)
|
|
75
|
-
elif path.name.endswith(".pdb.gz"):
|
|
76
|
-
body: str = structure.make_pdb_string()
|
|
77
|
-
with gzip.open(path, "wt") as f:
|
|
78
|
-
f.write(body)
|
|
79
|
-
elif path.name.endswith(".cif"):
|
|
80
|
-
# do not write chem_comp so it is viewable by molstar
|
|
81
|
-
# see https://github.com/project-gemmi/gemmi/discussions/362
|
|
82
|
-
doc = structure.make_mmcif_document(gemmi.MmcifOutputGroups(True, chem_comp=False))
|
|
83
|
-
doc.write_file(str(path))
|
|
84
|
-
elif path.name.endswith(".cif.gz"):
|
|
85
|
-
doc = structure.make_mmcif_document(gemmi.MmcifOutputGroups(True, chem_comp=False))
|
|
86
|
-
cif_str = doc.as_string()
|
|
87
|
-
with gzip.open(path, "wt") as f:
|
|
88
|
-
f.write(cif_str)
|
|
89
|
-
else:
|
|
90
|
-
msg = f"Unsupported file extension in {path.name}. Supported extensions are .pdb, .pdb.gz, .cif, .cif.gz"
|
|
91
|
-
raise ValueError(msg)
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
def _split_name_and_extension(name: str) -> tuple[str, str]:
|
|
95
|
-
# 1234.pdb -> (1234, .pdb)
|
|
96
|
-
# 1234.pdb.gz -> (1234, .pdb.gz)
|
|
97
|
-
# 1234.cif -> (1234, .cif)
|
|
98
|
-
# 1234.cif.gz -> (1234, .cif.gz)
|
|
99
|
-
if name.endswith(".pdb.gz"):
|
|
100
|
-
return name.replace(".pdb.gz", ""), ".pdb.gz"
|
|
101
|
-
if name.endswith(".cif.gz"):
|
|
102
|
-
return name.replace(".cif.gz", ""), ".cif.gz"
|
|
103
|
-
if name.endswith(".pdb"):
|
|
104
|
-
return name.replace(".pdb", ""), ".pdb"
|
|
105
|
-
if name.endswith(".cif"):
|
|
106
|
-
return name.replace(".cif", ""), ".cif"
|
|
107
|
-
|
|
108
|
-
msg = f"Unknown file extension in {name}. Supported extensions are .pdb, .pdb.gz, .cif, .cif.gz"
|
|
109
|
-
raise ValueError(msg)
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
def locate_structure_file(root: Path, pdb_id: str) -> Path:
|
|
113
|
-
"""Locate a structure file for a given PDB ID in the specified directory.
|
|
38
|
+
def find_chain_in_structure(structure: gemmi.Structure, wanted_chain: str) -> gemmi.Chain | None:
|
|
39
|
+
"""Find a chain in a structure.
|
|
114
40
|
|
|
115
41
|
Args:
|
|
116
|
-
|
|
117
|
-
|
|
42
|
+
structure: The gemmi structure to search in.
|
|
43
|
+
wanted_chain: The chain identifier to search for.
|
|
118
44
|
|
|
119
45
|
Returns:
|
|
120
|
-
The
|
|
121
|
-
|
|
122
|
-
Raises:
|
|
123
|
-
FileNotFoundError: If no structure file is found for the given PDB ID.
|
|
46
|
+
The found chain or None if not found.
|
|
124
47
|
"""
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
root / f"{pdb_id.upper()}{ext}",
|
|
131
|
-
root / f"pdb{pdb_id.lower()}{ext}",
|
|
132
|
-
)
|
|
133
|
-
for candidate in candidates:
|
|
134
|
-
if candidate.exists():
|
|
135
|
-
return candidate
|
|
136
|
-
msg = f"No structure file found for {pdb_id} in {root}"
|
|
137
|
-
raise FileNotFoundError(msg)
|
|
48
|
+
for model in structure:
|
|
49
|
+
chain = find_chain_in_model(model, wanted_chain)
|
|
50
|
+
if chain is not None:
|
|
51
|
+
return chain
|
|
52
|
+
return None
|
|
138
53
|
|
|
139
54
|
|
|
140
|
-
def
|
|
141
|
-
"""
|
|
55
|
+
def nr_residues_in_chain(file: Path, chain: str = "A") -> int:
|
|
56
|
+
"""Returns the number of residues in a specific chain from a structure file.
|
|
142
57
|
|
|
143
58
|
Args:
|
|
144
|
-
|
|
59
|
+
file: Path to the input structure file.
|
|
60
|
+
chain: Chain to count residues of.
|
|
145
61
|
|
|
146
|
-
|
|
147
|
-
|
|
62
|
+
Returns:
|
|
63
|
+
The number of residues in the specified chain.
|
|
148
64
|
"""
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
def __init__(self, chain: str, file: Path | str, available_chains: Iterable[str]):
|
|
157
|
-
super().__init__(f"Chain {chain} not found in {file}. Available chains are: {available_chains}")
|
|
158
|
-
self.chain_id = chain
|
|
159
|
-
self.file = file
|
|
65
|
+
structure = read_structure(file)
|
|
66
|
+
gchain = find_chain_in_structure(structure, chain)
|
|
67
|
+
if gchain is None:
|
|
68
|
+
logger.warning("Chain %s not found in %s. Returning 0.", chain, file)
|
|
69
|
+
return 0
|
|
70
|
+
return len(gchain)
|
|
160
71
|
|
|
161
72
|
|
|
162
73
|
def _dedup_helices(structure: gemmi.Structure):
|
|
@@ -198,18 +109,34 @@ def _add_provenance_info(structure: gemmi.Structure, chain2keep: str, out_chain:
|
|
|
198
109
|
|
|
199
110
|
|
|
200
111
|
def chains_in_structure(structure: gemmi.Structure) -> set[gemmi.Chain]:
|
|
201
|
-
"""Get a list of chains in a structure.
|
|
112
|
+
"""Get a list of chains in a structure.
|
|
113
|
+
|
|
114
|
+
Args:
|
|
115
|
+
structure: The gemmi structure to get chains from.
|
|
116
|
+
|
|
117
|
+
Returns:
|
|
118
|
+
A set of chains in the structure.
|
|
119
|
+
"""
|
|
202
120
|
return {c for model in structure for c in model}
|
|
203
121
|
|
|
204
122
|
|
|
205
|
-
|
|
123
|
+
class ChainNotFoundError(IndexError):
|
|
124
|
+
"""Exception raised when a chain is not found in a structure."""
|
|
125
|
+
|
|
126
|
+
def __init__(self, chain: str, file: Path | str, available_chains: Iterable[str]):
|
|
127
|
+
super().__init__(f"Chain {chain} not found in {file}. Available chains are: {available_chains}")
|
|
128
|
+
self.chain_id = chain
|
|
129
|
+
self.file = file
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def write_single_chain_structure_file(
|
|
206
133
|
input_file: Path,
|
|
207
134
|
chain2keep: str,
|
|
208
135
|
output_dir: Path,
|
|
209
136
|
out_chain: str = "A",
|
|
210
137
|
copy_method: CopyMethod = "copy",
|
|
211
138
|
) -> Path:
|
|
212
|
-
"""Write a single chain from a
|
|
139
|
+
"""Write a single chain from a structure file to a new structure file.
|
|
213
140
|
|
|
214
141
|
Also
|
|
215
142
|
|
|
@@ -226,14 +153,14 @@ def write_single_chain_pdb_file(
|
|
|
226
153
|
```
|
|
227
154
|
|
|
228
155
|
Args:
|
|
229
|
-
input_file: Path to the input
|
|
156
|
+
input_file: Path to the input structure file.
|
|
230
157
|
chain2keep: The chain to keep.
|
|
231
158
|
output_dir: Directory to save the output file.
|
|
232
159
|
out_chain: The chain identifier for the output file.
|
|
233
160
|
copy_method: How to copy when no changes are needed to output file.
|
|
234
161
|
|
|
235
162
|
Returns:
|
|
236
|
-
Path to the output
|
|
163
|
+
Path to the output structure file
|
|
237
164
|
|
|
238
165
|
Raises:
|
|
239
166
|
FileNotFoundError: If the input file does not exist.
|
|
@@ -241,7 +168,7 @@ def write_single_chain_pdb_file(
|
|
|
241
168
|
"""
|
|
242
169
|
|
|
243
170
|
logger.debug(f"chain2keep: {chain2keep}, out_chain: {out_chain}")
|
|
244
|
-
structure =
|
|
171
|
+
structure = read_structure(input_file)
|
|
245
172
|
structure.setup_entities()
|
|
246
173
|
|
|
247
174
|
chain = find_chain_in_structure(structure, chain2keep)
|
|
@@ -249,7 +176,7 @@ def write_single_chain_pdb_file(
|
|
|
249
176
|
if chain is None:
|
|
250
177
|
raise ChainNotFoundError(chain2keep, input_file, chainnames_in_structure)
|
|
251
178
|
chain_name = chain.name
|
|
252
|
-
name, extension =
|
|
179
|
+
name, extension = split_name_and_extension(input_file.name)
|
|
253
180
|
output_file = output_dir / f"{name}_{chain_name}2{out_chain}{extension}"
|
|
254
181
|
|
|
255
182
|
if output_file.exists():
|