Skip to content

Commit

Permalink
Merge pull request #17 from OpenFreeEnergy/pdb_loading
Browse files Browse the repository at this point in the history
Makes PDBFile core object of Protein component
  • Loading branch information
IAlibay authored Apr 4, 2022
2 parents 32076f9 + 4fc31a7 commit 84ac0f7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 12 deletions.
40 changes: 29 additions & 11 deletions gufe/proteincomponent.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# This code is part of OpenFE and is licensed under the MIT license.
# For details, see https://github.com/OpenFreeEnergy/gufe
from rdkit import Chem
try:
import openmm.app
except ImportError:
HAS_OPENMM = False
else:
HAS_OPENMM = True

from gufe import Component
from gufe.custom_typing import RDKitMol, OEMol
Expand All @@ -12,8 +18,20 @@ class ProteinComponent(Component):
This representation is immutable. If you want to make any modifications,
do this in an appropriate toolkit then remake this class.
"""
def __init__(self, rdkit: RDKitMol, name=""):
self._rdkit = rdkit
def __init__(self, openmm_top, openmm_pos, name=""):
"""
Parameters
----------
openmm_top : openmm.app.Topology
the Topology object
openmm_pos : openmm.unit.Quantity
the positions for this Topology
name : str, optional
identifier for this Protein, used as the hash
"""
# yes this is fragile and silly, but it'll do for now
self._openmm_top = openmm_top
self._openmm_pos = openmm_pos
self._name = name

@property
Expand All @@ -22,22 +40,22 @@ def name(self):

@classmethod
def from_pdbfile(cls, pdbfile: str, name=""):
m = Chem.MolFromPDBFile(pdbfile, removeHs=False)
if m is None:
raise ValueError(f"RDKit failed to produce a molecule from "
"{pdbfile}")
return cls(rdkit=m, name=name)
if not HAS_OPENMM:
raise ImportError("OpenMM is currently required")
f = openmm.app.PDBFile(pdbfile)

return cls(f.topology, f.positions, name)

@classmethod
def from_pdbxfile(cls, pdbxfile: str, name=""):
raise NotImplementedError()

@classmethod
def from_rdkit(cls, rdkit: RDKitMol, name=""):
return cls(rdkit=Chem.Mol(rdkit), name=name)
raise NotImplementedError()

def to_rdkit(self) -> RDKitMol:
return Chem.Mol(self._rdkit)
raise NotImplementedError()

@classmethod
def from_openff(cls, offmol, name=""):
Expand All @@ -61,11 +79,11 @@ def from_dict(cls, d: dict):
raise NotImplementedError()

def __hash__(self):
return hash((self.name, Chem.MolToSequence(self._rdkit)))
return hash(self.name)

def __eq__(self, other):
return hash(self) == hash(other)

@property
def total_charge(self):
return Chem.GetFormalCharge(self._rdkit)
return None
1 change: 1 addition & 0 deletions gufe/tests/test_chemicalsystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ def test_chemical_system_neq_5(solvated_complex, prot_comp, solv_comp,
assert hash(solvated_complex) != hash(complex2)


@pytest.mark.xfail
def test_complex_system_charge(solvated_complex):
# protein = 22, ligand = 0, solvent = 0
assert solvated_complex.total_charge == 22
Expand Down
7 changes: 6 additions & 1 deletion gufe/tests/test_protein_molecule.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@ def test_from_pdbfile(PDB_181L_path):

assert isinstance(p, ProteinComponent)
assert p.name == 'Steve'
assert p.to_rdkit().GetNumAtoms() == 2639
assert p._openmm_top.getNumAtoms() == 2639


def test_from_pdbfile_ValueError(PDBx_181L_path):
with pytest.raises(ValueError):
_ = ProteinComponent.from_pdbfile(PDBx_181L_path)


@pytest.mark.xfail
def test_from_rdkit(PDB_181L_path):
m = Chem.MolFromPDBFile(PDB_181L_path, removeHs=False)
p = ProteinComponent.from_rdkit(m, 'Steve')
Expand All @@ -41,6 +42,7 @@ def test_from_rdkit(PDB_181L_path):
assert p.to_rdkit().GetNumAtoms() == 2639


@pytest.mark.xfail
def test_to_rdkit(PDB_181L_path):
pm = ProteinComponent.from_pdbfile(PDB_181L_path)
rdkitmol = pm.to_rdkit()
Expand All @@ -63,6 +65,7 @@ def test_hash_eq(PDB_181L_path):
assert hash(m1) == hash(m2)


@pytest.mark.xfail
def test_neq(PDB_181L_path, PDB_181L_mutant):
m1 = ProteinComponent.from_pdbfile(PDB_181L_path)

Expand All @@ -76,6 +79,7 @@ def test_neq_name(PDB_181L_path):
assert m1 != m2


@pytest.mark.xfail
def test_hash_neq(PDB_181L_path, PDB_181L_mutant):
m1 = ProteinComponent.from_pdbfile(PDB_181L_path)

Expand All @@ -89,6 +93,7 @@ def test_hash_neq_name(PDB_181L_path):
assert hash(m1) != hash(m2)


@pytest.mark.xfail
def test_protein_total_charge(PDB_181L_path):
m1 = ProteinComponent.from_pdbfile(PDB_181L_path)

Expand Down

0 comments on commit 84ac0f7

Please sign in to comment.