class HasCathIDMixin(object): """ Mixin for classes that contain a :class:`cathpy.core.models.CathID` Usage: .. code:: python class MyClass(HasCathIDMixin, object): def __init__(self, *, cath_id, **kwargs): super(MyClass, self).super(cath_id=cath_id) Provides: .. code:: python self.cath_id self.cath_id_depth self.sfam_id self.cath_id_to_depth(n) """ def __init__(self, *, cath_id, **kwargs): super().__init__(**kwargs) self._cath_id = CathID(cath_id) @property def cath_id(self): """ Returns the full CATH ID """ return self._cath_id.cath_id @property def cath_id_depth(self): """ Returns the depth of the CATH ID (eg '1.10.8' = 3) """ return self._cath_id.depth @property def sfam_id(self): """ Returns the Superfamily ID of the CATH ID """ return self._cath_id.cath_id_to_depth(4) def cath_id_to_depth(self, depth): """ Returns the CATH ID to a given depth """ return self._cath_id.cath_id_to_depth(depth) def __lt__(self, other): if isinstance(other, str): other = CathID(other) return self._cath_id < other._cath_id
def filter_cath_id(self, cath_id): """ Returns a new container after filtering to only include entries within a given CATH ID """ if not isinstance(cath_id, CathID): cath_id = CathID(cath_id) depth = cath_id.depth filtered_entries = [ c for c in self.entries if c.cath_id_to_depth(depth) == cath_id] return self.__class__(entries=filtered_entries)
def test_cath_id(self): self.assertEqual(str(CathID("1")), "1") self.assertEqual(str(CathID("1.10.8")), "1.10.8") self.assertEqual(str(CathID("1.10.8.10.1.1.1.2.3")), "1.10.8.10.1.1.1.2.3") self.assertEqual(CathID("1.10.8.10").sfam_id, "1.10.8.10") self.assertEqual(CathID("1.10.8.10.1").sfam_id, "1.10.8.10") with self.assertRaises(OutOfBoundsError) as err: cath_id = CathID("1.10.8").sfam_id self.assertRegex(err.exception, r'require depth', 'sfam_id fails when depth < 4')
def __lt__(self, other): if isinstance(other, str): other = CathID(other) return self._cath_id < other._cath_id
def __init__(self, *, cath_id, **kwargs): super().__init__(**kwargs) self._cath_id = CathID(cath_id)