Skip to content

Commit

Permalink
fixes to get atom selector and pdb reader tests working
Browse files Browse the repository at this point in the history
  • Loading branch information
ChiCheng45 committed Jan 15, 2024
1 parent cd86819 commit 83e99c2
Show file tree
Hide file tree
Showing 7 changed files with 41,143 additions and 30 deletions.
8 changes: 8 additions & 0 deletions MDANSE/Src/MDANSE/Chemistry/ChemicalEntity.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,8 @@ def __init__(

self._parent = None

self.element = ATOMS_DATABASE[self._symbol]["element"]

for k, v in kwargs.items():
try:
setattr(self, k, v)
Expand Down Expand Up @@ -523,6 +525,8 @@ def restore_bonds(self, atom_dict: dict[str, Atom]) -> List[Tuple[int, int]]:
return [(self.index, other.index) for other in self._bonds]

def __eq__(self, other):
if not isinstance(other, _ChemicalEntity):
return False
if not self._index == other._index:
return False
if not self._symbol == other._symbol:
Expand Down Expand Up @@ -1823,6 +1827,10 @@ def serialize(self, h5_contents: dict[str, list[list[str]]]) -> tuple[str, int]:

return "nucleotide_chains", len(h5_contents["nucleotide_chains"]) - 1

@property
def nucleotides(self):
return self._nucleotides

def set_nucleotides(self, nucleotides: list[Nucleotide]) -> None:
"""
Sets the provided Nucleotide objects as the nucleotides making up this chain, and creates bonds between them.
Expand Down
6 changes: 6 additions & 0 deletions MDANSE/Src/MDANSE/Framework/Selectors/Macromolecule.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,11 @@ def select(self, macromolecules):
m = Macromolecule.lookup.get(ce.__class__, None)
if m in macromolecules:
sel.update([at for at in ce.atom_list])
if (isinstance(ce, Protein)
and "peptide_chain" in macromolecules):
for ce2 in ce.peptide_chains:
m = Macromolecule.lookup.get(ce2.__class__, None)
if m in macromolecules:
sel.update([at for at in ce.atom_list])

return sel
760 changes: 760 additions & 0 deletions MDANSE/Tests/UnitTests/Data/1gip.pdb

Large diffs are not rendered by default.

40,304 changes: 40,304 additions & 0 deletions MDANSE/Tests/UnitTests/Data/2vb1.pdb

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions MDANSE/Tests/UnitTests/select_atoms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
selection = [
at for at in chemicalSystem.atom_list if at.element.strip().lower() == "phosphorus"
]
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,54 @@
@author: Eric C. Pellegrini
"""

import os
import unittest

from MDANSE.Framework.Selectors import *
from MDANSE.Framework.Selectors import All
from MDANSE.Framework.Selectors import Amine
from MDANSE.Framework.Selectors import Ammonium
from MDANSE.Framework.Selectors import AtomElement
from MDANSE.Framework.Selectors import AtomFullName
from MDANSE.Framework.Selectors import AtomIndex
from MDANSE.Framework.Selectors import AtomName
from MDANSE.Framework.Selectors import AtomPicked
from MDANSE.Framework.Selectors import AtomSymbol
from MDANSE.Framework.Selectors import Backbone
from MDANSE.Framework.Selectors import CarboHydrogen
from MDANSE.Framework.Selectors import CarbonAlpha
from MDANSE.Framework.Selectors import ChainName
from MDANSE.Framework.Selectors import HeteroHydrogen
from MDANSE.Framework.Selectors import Hydroxyl
from MDANSE.Framework.Selectors import Macromolecule
from MDANSE.Framework.Selectors import Methyl
from MDANSE.Framework.Selectors import MoleculeIndex
from MDANSE.Framework.Selectors import MoleculeName
from MDANSE.Framework.Selectors import NitroHydrogen
from MDANSE.Framework.Selectors import NucleotideBase
from MDANSE.Framework.Selectors import NucleotideSugar
from MDANSE.Framework.Selectors import NucleotideType
from MDANSE.Framework.Selectors import OxyHydrogen
from MDANSE.Framework.Selectors import Peptide
from MDANSE.Framework.Selectors import Phosphate
from MDANSE.Framework.Selectors import PythonScript
from MDANSE.Framework.Selectors import ResidueClass
from MDANSE.Framework.Selectors import ResidueName
from MDANSE.Framework.Selectors import ResidueType
from MDANSE.Framework.Selectors import SideChain
from MDANSE.Framework.Selectors import Sulphate
from MDANSE.Framework.Selectors import SulphurHydrogen
from MDANSE.Framework.Selectors import Thiol
from MDANSE.Framework.Selectors import WithinShell
from MDANSE.IO.PDBReader import PDBReader


pbd_2vb1 = os.path.join(
os.path.dirname(os.path.realpath(__file__)), "Data", "2vb1.pdb")
pbd_1gip = os.path.join(
os.path.dirname(os.path.realpath(__file__)), "Data", "1gip.pdb")
select_atoms_script = os.path.join(
os.path.dirname(os.path.realpath(__file__)), "select_atoms.py")


class TestAtomsSelector(unittest.TestCase):
""" """

Expand All @@ -61,10 +102,10 @@ def setUpClass(cls):
"""
super(TestAtomsSelector, cls).setUpClass()

reader = PDBReader("../../Data/PDB/2vb1.pdb")
reader = PDBReader(pbd_2vb1)
cls._proteinChemicalSystem = reader.build_chemical_system()

reader = PDBReader("../../Data/PDB/1gip.pdb")
reader = PDBReader(pbd_1gip)
cls._nucleicAcidChemicalSystem = reader.build_chemical_system()

def test_all(self):
Expand Down Expand Up @@ -93,7 +134,7 @@ def test_atom_element(self):

def test_atom_full_name(self):
selector = AtomFullName.AtomFullName(self._proteinChemicalSystem)
selection = selector.select([".LYS1.N", ".VAL2.O"])
selection = selector.select(["...LYS1.N", "...VAL2.O"])
self.assertEqual(len(selection), 2)

selection = list(selector.select(["*"]))
Expand Down Expand Up @@ -146,7 +187,7 @@ def test_backbone(self):

def test_carbo_hydrogens(self):
selector = CarboHydrogen.CarboHydrogen(self._proteinChemicalSystem)
selection = selector.select([".LYS1.HB2", ".LYS1.HB3"])
selection = selector.select(["...LYS1.HB2", "...LYS1.HB3"])
self.assertEqual(len(selection), 2)

selection = list(selector.select(["*"]))
Expand All @@ -170,7 +211,7 @@ def test_chain_name(self):
def test_hetero_hydrogen(self):
selector = HeteroHydrogen.HeteroHydrogen(self._proteinChemicalSystem)

selection = selector.select([".SER24.HG"])
selection = selector.select(["...SER24.HG"])
self.assertEqual(len(selection), 1)

selection = selector.select(["*"])
Expand All @@ -179,7 +220,7 @@ def test_hetero_hydrogen(self):
def test_hydroxyl(self):
selector = Hydroxyl.Hydroxyl(self._proteinChemicalSystem)

selection = selector.select([".SER24.HG"])
selection = selector.select(["...SER24.HG"])
self.assertEqual(len(selection), 1)

selection = selector.select(["*"])
Expand All @@ -197,7 +238,7 @@ def test_macromolecule(self):
def test_methyl(self):
selector = Methyl.Methyl(self._proteinChemicalSystem)

selection = selector.select([".VAL29.1HG1", ".VAL29.1HG2"])
selection = selector.select(["...VAL29.1HG1", "...VAL29.1HG2"])
self.assertEqual(len(selection), 2)

selection = selector.select(["*"])
Expand All @@ -221,15 +262,15 @@ def test_molecule_name(self):
selection = selector.select([""])
self.assertEqual(len(selection), 1960)

selection = selector.select(["WAT9709"])
selection = selector.select(["WAT_9709"])
self.assertEqual(len(selection), 3)

selection = selector.select(["*"])
self.assertEqual(len(selection), 30714)

def test_nitro_hydrogens(self):
selector = NitroHydrogen.NitroHydrogen(self._proteinChemicalSystem)
selection = selector.select([".LYS1.HT1", ".LYS1.HT2", ".LYS1.HT3"])
selection = selector.select(["...LYS1.HT1", "...LYS1.HT2", "...LYS1.HT3"])
self.assertEqual(len(selection), 3)

selection = list(selector.select(["*"]))
Expand Down Expand Up @@ -275,7 +316,7 @@ def test_nucleotide_type(self):
def test_oxy_hydrogen(self):
selector = OxyHydrogen.OxyHydrogen(self._proteinChemicalSystem)

selection = selector.select([".THR118.HG1"])
selection = selector.select(["...THR118.HG1"])
self.assertEqual(len(selection), 1)

selection = selector.select(["*"])
Expand All @@ -297,7 +338,7 @@ def test_phosphate(self):

def test_python_script(self):
selector = PythonScript.PythonScript(self._nucleicAcidChemicalSystem)
selection = selector.select(["./Tests/UnitTests/select_atoms.py"])
selection = selector.select([select_atoms_script])
self.assertEqual(len(selection), 22)

def test_residue_class(self):
Expand All @@ -312,7 +353,7 @@ def test_residue_class(self):
def test_residue_name(self):
selector = ResidueName.ResidueName(self._proteinChemicalSystem)

selection = selector.select([".LYS1"])
selection = selector.select(["...LYS1"])
self.assertEqual(len(selection), 24)

selection = selector.select(["*"])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,16 @@
@author: Eric C. Pellegrini
"""

import os
import unittest

import numpy

from MDANSE.IO.PDBReader import PDBReader


pbd_2vb1 = os.path.join(
os.path.dirname(os.path.realpath(__file__)), "data", "2vb1.pdb")


class TestPDBReader(unittest.TestCase):
"""
Unittest for the geometry-related functions
Expand All @@ -61,7 +63,7 @@ def test_reader(self):
with self.assertRaises((IOError, AttributeError)):
reader = PDBReader("xxxxx.pdb")

reader = PDBReader("../../Data/Trajectories/CHARMM/2vb1.pdb")
reader = PDBReader(pbd_2vb1)

chemicalSystem = reader.build_chemical_system()

Expand All @@ -81,14 +83,3 @@ def test_reader(self):
self.assertAlmostEqual(conf.variables["coordinates"][-1, 0], 2.4937)
self.assertAlmostEqual(conf.variables["coordinates"][-1, 1], 3.9669)
self.assertAlmostEqual(conf.variables["coordinates"][-1, 2], -0.5209)


def suite():
loader = unittest.TestLoader()
s = unittest.TestSuite()
s.addTest(loader.loadTestsFromTestCase(TestPDBReader))
return s


if __name__ == "__main__":
unittest.main(verbosity=2)

0 comments on commit 83e99c2

Please sign in to comment.