From 33489152dc832027953b33cd42a2ab96a303c3f0 Mon Sep 17 00:00:00 2001 From: Sam Ireland Date: Fri, 12 Jan 2018 16:28:58 +0000 Subject: [PATCH] Add full residue names (resolves #12) --- atomium/structures/molecules.py | 16 ++++++++++++++-- tests/integration/test_pdb.py | 4 ++++ tests/unit/structure_tests/test_molecules.py | 7 +++++++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/atomium/structures/molecules.py b/atomium/structures/molecules.py index 89e48285..d8560540 100644 --- a/atomium/structures/molecules.py +++ b/atomium/structures/molecules.py @@ -297,15 +297,16 @@ def molecule_id(self, molecule_id=None): return self._id - def name(self, name=None): + def name(self, name=None, full=False): """Returns the molecule's name. If a value is given, the name will be updated, provided it is a string. :param int name: If given, the name will be set to this. + :param bool full: If ``True`` the full English name will be returned. :raises TypeError: if the name given is not str.""" if name is None: - return self._name + return RESIDUES.get(self._name, self._name) if full else self._name else: if not isinstance(name, str): raise TypeError("Molecule name '{}' is not str".format(name)) @@ -489,3 +490,14 @@ def chain(self): for atom in self.atoms(): return atom.chain() + + + +RESIDUES = { + "GLY": "glycine", "ALA": "alanine", "VAL": "valine", "LEU": "leucine", + "ILE": "isoleucine", "MET": "methionine", "PHE": "phenylalanine", + "TRP": "tryptophan", "PRO": "proline", "SER": "serine", "THR": "threonine", + "CYS": "cysteine", "TYR": "tyrosine", "ASN": "asparagine", "GLN": "glutamine", + "ASP": "aspartic acid", "GLU": "glutamic acid", "LYS": "lysine", + "ARG": "arginine", "HIS": "histidine", "HOH": "water" +} diff --git a/tests/integration/test_pdb.py b/tests/integration/test_pdb.py index b31220b2..8aaf11e1 100644 --- a/tests/integration/test_pdb.py +++ b/tests/integration/test_pdb.py @@ -40,8 +40,11 @@ def test_can_read_pdb(self): # Residues are correct self.assertEqual(chaina[0].name(), "VAL") + self.assertEqual(chaina[0].name(full=True), "valine") self.assertEqual(chaina[0].next().name(), "MET") + self.assertEqual(chaina[0].next().name(full=True), "methionine") self.assertEqual(chaina[-1].name(), "ILE") + self.assertEqual(chaina[-1].name(full=True), "isoleucine") self.assertEqual(chaina[-1].previous().name(), "SER") self.assertEqual(len(chaina.residues(name="ASN")), 6) for residue in chaina: @@ -77,6 +80,7 @@ def test_can_read_pdb(self): mol = model.molecule(molecule_id="A2001") self.assertIs(mol.model(), model) self.assertEqual(mol.name(), "XMP") + self.assertEqual(mol.name(full=True), "XMP") self.assertEqual(len(mol.atoms()), 24) # Bindsites are correct diff --git a/tests/unit/structure_tests/test_molecules.py b/tests/unit/structure_tests/test_molecules.py index 55c98caf..bdf66880 100644 --- a/tests/unit/structure_tests/test_molecules.py +++ b/tests/unit/structure_tests/test_molecules.py @@ -94,6 +94,13 @@ def test_molecule_name_property(self): self.assertIs(mol._name, mol.name()) + def test_molecule_name_property_full(self): + mol = Molecule(self.atom1, self.atom2, self.atom3, name="VAL") + self.assertEqual(mol.name(full=True), "valine") + mol = Molecule(self.atom1, self.atom2, self.atom3, name="VL") + self.assertEqual(mol.name(full=True), "VL") + + def test_can_update_molecule_name(self): mol = Molecule(self.atom1, self.atom2, self.atom3, name="VAL") mol.name("HIS")