Skip to content

Commit 614a3aa

Browse files
README: add usage examples
1 parent 3e070c8 commit 614a3aa

File tree

2 files changed

+67
-9
lines changed

2 files changed

+67
-9
lines changed

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ uv sync
3535
# Analyze a structure file
3636
uv run grodecoder path/to/structure.gro
3737

38+
# Analyze a pair topology file + coordinates
39+
uv run grodecoder path/to/topology.psf /path/to/coordinates.coor
40+
3841
# Output to stdout with compact format
3942
uv run grodecoder structure.pdb --compact --stdout
4043

@@ -101,6 +104,40 @@ GROdecoder produces detailed JSON inventories with the following structure:
101104

102105
## 🔧 Advanced Features
103106

107+
### Read back a Grodecoder inventory file
108+
109+
Reading a Grodecoder inventory file is essential to be able to access the different parts of a system
110+
without having to identify them again:
111+
112+
```python
113+
from grodecoder import read_grodecoder_output
114+
115+
gro_results = read_grodecoder_output("1BRS_grodecoder.json")
116+
117+
# Print the sequence of protein segment only.
118+
for segment in gro_results.decoded.inventory.segments:
119+
if segment.is_protein():
120+
print(segment.sequence)
121+
```
122+
123+
In conjunction with the structure file, we can use the grodecoder output file to access the different
124+
parts of the system, as identified by grodecoder:
125+
126+
```python
127+
import MDAnalysis
128+
from grodecoder import read_grodecoder_output
129+
130+
131+
universe = MDAnalysis.Universe("tests/data/1BRS.pdb")
132+
gro_results = read_grodecoder_output("1BRS_grodecoder.json")
133+
134+
# Prints the center of mass of each protein segment.
135+
for segment in gro_results.decoded.inventory.segments:
136+
if segment.is_protein():
137+
seg: MDAnalysis.AtomGroup = universe.atoms[segment.atoms]
138+
print(seg.center_of_mass())
139+
```
140+
104141
### Chain Detection
105142
GROdecoder uses sophisticated distance-based algorithms to detect protein and nucleic acid chains:
106143

src/grodecoder/models.py

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,36 @@ def serialize(self, handler: SerializerFunctionWrapHandler, info: SerializationI
114114
return self_data
115115

116116

117-
class SmallMolecule(FrozenWithAtoms):
117+
class MolecularTypeMixin:
118+
molecular_type: MolecularType
119+
120+
def is_ion(self) -> bool:
121+
return self.molecular_type == MolecularType.ION
122+
123+
def is_lipid(self) -> bool:
124+
return self.molecular_type == MolecularType.LIPID
125+
126+
def is_solvent(self) -> bool:
127+
return self.molecular_type == MolecularType.SOLVENT
128+
129+
def is_unknown(self) -> bool:
130+
return self.molecular_type == MolecularType.UNKNOWN
131+
132+
def is_other(self) -> bool:
133+
"""Alias for MolecularTypeMixin.is_unknown()"""
134+
return self.is_unknown()
135+
136+
def is_protein(self) -> bool:
137+
return self.molecular_type == MolecularType.PROTEIN
138+
139+
def is_nucleic(self) -> bool:
140+
return self.molecular_type == MolecularType.NUCLEIC
141+
142+
143+
class SmallMolecule(MolecularTypeMixin, FrozenWithAtoms):
118144
"""Small molecules are defined as residues with a single residue name."""
119145

120146
description: str
121-
molecular_type: MolecularType
122147

123148
@computed_field
124149
@property
@@ -127,11 +152,9 @@ def name(self) -> str:
127152
return self.atoms.residues[0].resname
128153

129154

130-
class Segment(FrozenWithAtoms):
155+
class Segment(MolecularTypeMixin, FrozenWithAtoms):
131156
"""A segment is a group of atoms that are connected."""
132157

133-
molecular_type: MolecularType # likely to be protein or nucleic acid
134-
135158
@computed_field
136159
@property
137160
def sequence(self) -> str:
@@ -191,15 +214,13 @@ def check_number_of_atoms_is_valid(self):
191214
return self
192215

193216

194-
class SmallMoleculeRead(BaseModelWithAtomsRead):
217+
class SmallMoleculeRead(MolecularTypeMixin, BaseModelWithAtomsRead):
195218
name: str
196219
description: str
197-
molecular_type: MolecularType
198220

199221

200-
class SegmentRead(BaseModelWithAtomsRead):
222+
class SegmentRead(MolecularTypeMixin, BaseModelWithAtomsRead):
201223
sequence: str
202-
molecular_type: MolecularType
203224

204225

205226
class InventoryRead(FrozenModel):

0 commit comments

Comments
 (0)