-
Notifications
You must be signed in to change notification settings - Fork 844
Implementing gemmi-based mmcif reader (with easy extension to PDB/PDBx and mmJSON)
#4712
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from 14 commits
aa2a88f
218cf43
7f78e02
6682d6e
817f3a0
3cc8c80
f1bf325
d21c220
2a1be15
77645e6
91e6942
9a0c086
9c731df
8b40ec7
bdcbd73
401a4d3
9c336bd
def88e4
cabfd37
4c9d930
184491a
3de8565
ca6ebbb
45077ad
9a1a59a
27c10d6
950cfcf
8d1a8b5
ef29338
caca17e
f0e49cc
b7ada7c
e80632c
10f3124
98353fe
35fa187
e68fcce
263e9f1
ba47d53
9ffb6f2
fcfc6c0
0de720e
236b286
b562115
816b23f
92ae164
88c64a3
59b7e29
f2c23c8
776676e
71e60f4
36b7125
b058941
ef30fa7
95572c1
a8a9436
6706bbe
8cf9da4
f13156b
dda981c
ebdf849
47043f6
9770d7b
fd7f70d
1493056
3d7fbb9
9b9286e
b8f3c04
0f38a2d
b915aab
0d61248
34d76ca
b242aa5
4fc3a78
14fa756
e3a9a1f
d492b4e
1880e4a
927d7a0
ad0f0be
e03c3e5
4d79205
32d7cf9
0df8c3a
05c6ea1
88dab79
a82fe52
e3f1714
db46016
32cd103
805089e
55c3dbb
cd201d0
81f0b5b
22d1cca
d1ba434
a03b56f
bd4c255
3d61dc5
aed9b54
53c51f4
3e0324c
205c910
f9f7912
9a90316
1c5a549
dfc10e6
aae46c8
9cf4027
522e125
0b0ec81
b3d7c1c
801d85f
bdd070e
6b2c6c6
1a7f607
157c365
1d01a3f
2020484
d362f91
959d78b
15441f0
e2e097f
bc89c0b
adcca0b
f17062c
25424a0
b2f6abc
2b64581
d1d8467
bb857a8
b026179
547eed7
95600b1
e053cd6
a7ea152
40d7747
6d4d9a4
4879d6b
39bd465
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import numpy as np | ||
| import gemmi | ||
| import logging | ||
| from . import base | ||
|
|
||
| logger = logging.getLogger("MDAnalysis.coordinates.MMCIF") | ||
|
|
||
|
|
||
| class MMCIFReader(base.SingleFrameReaderBase): | ||
| """Reads from an MMCIF file""" | ||
|
|
||
| format = "MMCIF" | ||
| units = {"time": None, "length": "Angstrom"} | ||
|
|
||
| def _read_first_frame(self): | ||
| structure = gemmi.read_structure(self.filename) | ||
| coords = np.array( | ||
| [ | ||
| [*at.pos.tolist()] | ||
| for model in structure | ||
| for chain in model | ||
| for res in chain | ||
| for at in res | ||
| ] | ||
| ) | ||
| self.n_atoms = len(coords) | ||
| self.ts = self._Timestep.from_coordinates(coords, **self._ts_kwargs) | ||
| self.ts.frame = 0 | ||
|
|
||
| def Writer(self, filename, n_atoms=None, **kwargs): | ||
| raise NotImplementedError | ||
|
|
||
| def close(self): | ||
| pass | ||
|
|
||
|
|
||
| class MMCIFWriter(base.WriterBase): | ||
| pass | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,178 @@ | ||
| """ | ||
|
yuxuanzhuang marked this conversation as resolved.
|
||
| MMCIF Topology Parser # | ||
| =================== | ||
| """ | ||
|
|
||
| import gemmi | ||
| import numpy as np | ||
| import warnings | ||
| import itertools | ||
|
|
||
| from ..core.topology import Topology | ||
| from ..core.topologyattrs import ( | ||
| AltLocs, | ||
| Atomids, | ||
| Atomnames, | ||
| Atomtypes, | ||
| ChainIDs, | ||
| Elements, | ||
| FormalCharges, | ||
| ICodes, | ||
| Masses, | ||
| Occupancies, | ||
| RecordTypes, | ||
| Resids, | ||
| Resnames, | ||
| Resnums, | ||
| Segids, | ||
| Tempfactors, | ||
| ) | ||
| from .base import TopologyReaderBase | ||
|
|
||
|
|
||
| def _into_idx(arr: list[int]) -> list[int]: | ||
|
marinegor marked this conversation as resolved.
Outdated
|
||
| return [idx for idx, (_, group) in enumerate(itertools.groupby(arr)) for _ in group] | ||
|
|
||
|
|
||
| class MMCIFParser(TopologyReaderBase): | ||
| format = "MMCIF" | ||
|
|
||
| def parse(self, **kwargs): | ||
| """Read the file and return the structure. | ||
|
orbeckst marked this conversation as resolved.
|
||
|
|
||
| Returns | ||
| ------- | ||
| MDAnalysis Topology object | ||
| """ | ||
| structure = gemmi.read_structure(self.filename) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if this is intentional, but the
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From playing around, looks like this won't take a
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bumping so this doesn't get lost, still think this should accept a path obj (unless I'm missing something) |
||
|
|
||
| if len(structure) > 1: | ||
| warnings.warn( | ||
| "MMCIF model {self.filename} contains {len(model)=} different models, " | ||
| "but only the first one will be used to assign the topology" | ||
| ) | ||
| model = structure[0] | ||
|
|
||
| # atom properties | ||
| ( | ||
| altlocs, # at.altloc | ||
| serials, # at.serial | ||
| names, # at.name | ||
| atomtypes, # at.name | ||
| # ------------------ | ||
| chainids, # chain.name | ||
| elements, # at.element.name | ||
| formalcharges, # at.charge | ||
| weights, # at.element.weight | ||
| # ------------------ | ||
| occupancies, # at.occ | ||
| record_types, # res.het_flag | ||
| tempfactors, # at.b_iso | ||
| residx, # _into_idx(res.seqid.num) | ||
| ) = map( # this is probably not pretty, but it's efficient -- one loop over the mmcif | ||
|
marinegor marked this conversation as resolved.
Outdated
|
||
| np.array, | ||
| list( | ||
| zip( | ||
| *[ | ||
| ( | ||
| at.altloc, # altlocs | ||
| at.serial, # serials | ||
| at.name, # names | ||
| at.name, # atomtypes | ||
| # ------------------ | ||
| chain.name, # chainids | ||
| at.element.name, # elements | ||
| at.charge, # formalcharges | ||
| at.element.weight, # weights | ||
| # ------------------ | ||
| at.occ, # occupancies | ||
| res.het_flag, # record_types | ||
| at.b_iso, # tempfactors | ||
| res.seqid.num, # residx, later translated into continious repr | ||
| ) | ||
| for chain in model | ||
| for res in chain | ||
| for at in res | ||
| ] | ||
| ) | ||
| ), | ||
| ) | ||
|
|
||
| ( | ||
| icodes, # res.seqid.icode | ||
| resids, # res.seqid.num | ||
| resnames, # res.name | ||
| segidx, # chain.name | ||
| resnums, | ||
| ) = map( | ||
| np.array, | ||
| list( | ||
| zip( | ||
| *[ | ||
|
BradyAJohnston marked this conversation as resolved.
Outdated
|
||
| ( | ||
| res.seqid.icode, | ||
| res.seqid.num, | ||
| res.name, | ||
| chain.name, | ||
| res.seqid.num, | ||
| ) | ||
| for chain in model | ||
| for res in chain | ||
| ] | ||
| ) | ||
| ), | ||
| ) | ||
|
|
||
| segids = [chain.name for chain in model] | ||
|
|
||
| # transform *idx into continious numpy arrays | ||
| residx = np.array(_into_idx(residx)) | ||
| segidx = np.array(_into_idx(segidx)) | ||
|
|
||
| # fill in altlocs | ||
| altlocs = ["A" if not elem else elem for elem in altlocs] | ||
| record_types = [ | ||
| "ATOM" if record == "A" else "HETATM" if record == "H" else None | ||
| for record in record_types | ||
| ] | ||
| if any((elem is None for elem in record_types)): | ||
| raise ValueError("Found an atom that is neither ATOM or HETATM") | ||
|
|
||
| attrs = [ | ||
| # AtomAttr subclasses | ||
| AltLocs(altlocs), # at.altloc | ||
| Atomids(serials), # at.serial | ||
| Atomnames(names), # at.name | ||
| Atomtypes(atomtypes), # at.name | ||
| # --------------------------------------- | ||
| ChainIDs(chainids), # chain.name | ||
| Elements(elements), # at.element.name | ||
| FormalCharges(formalcharges), # at.charge | ||
| Masses(weights), # at.element.weight | ||
| # --------------------------------------- | ||
| Occupancies(occupancies), # at.occ | ||
| RecordTypes(record_types), # res.het_flat | ||
| Resnums(resnums), # res.seqid.num | ||
| Tempfactors(tempfactors), # at.b_iso | ||
| # | ||
| # ResidueAttr subclasses | ||
| ICodes(icodes), # res.seqid.icode | ||
| Resids(resids), # res.seqid.num | ||
| Resnames(resnames), # res.name | ||
| # | ||
| # SegmentAttr subclasses | ||
| Segids(segids), # chain.name | ||
| ] | ||
|
|
||
| n_atoms = len(names) | ||
| n_residues = len(resids) | ||
| n_segments = len(segids) | ||
|
|
||
| return Topology( | ||
| n_atoms, | ||
| n_residues, | ||
| n_segments, | ||
| attrs=attrs, | ||
| atom_resindex=residx, | ||
| residue_segindex=segidx, | ||
| ) | ||
Uh oh!
There was an error while loading. Please reload this page.