|
| 1 | +.. _custom-components: |
| 2 | + |
| 3 | +################# |
| 4 | +Custom components |
| 5 | +################# |
| 6 | + |
| 7 | +GPSEA aims to stay useful in the long run. Therefore, we took a great care |
| 8 | +to adhere to "good software development practices" and we framed GPSEA functionalities |
| 9 | +as a set of loosely coupled components. As a rule of thumb, each component corresponds |
| 10 | +to a Python abstract base class which is then extended by the builtin components |
| 11 | +and can also be extended by the future components, to serve both common or exotic use cases. |
| 12 | + |
| 13 | +The abstract base classes define the component API. |
| 14 | +Per guidelines in Python's :mod:`abc` module, the abstract classes use :class:`abc.ABCMeta` as a metaclass |
| 15 | +and the class API consists of methods annotated with the :func:`abc.abstractmethod` decorator. |
| 16 | +These decorated methods must be overridden in the subclasses. |
| 17 | + |
| 18 | +The following sections provide guidance for customizing the most commonly used GPSEA components. |
| 19 | + |
| 20 | + |
| 21 | +.. _custom-phenotype-scorer: |
| 22 | + |
| 23 | +**************** |
| 24 | +Phenotype scorer |
| 25 | +**************** |
| 26 | + |
| 27 | +:class:`~gpsea.analysis.pscore.PhenotypeScorer` computes a phenotype score for an individual. |
| 28 | +The phenotype score is a `float` with range :math:`(-\infty, \infty)` where `NaN` indicates |
| 29 | +that a score cannot be computed (e.g. the lab measurement value was not ascertained for the individual). |
| 30 | + |
| 31 | +Here we show an example of a toy phenotype scorer |
| 32 | +for using body mass index (BMI) as a phenotype score. |
| 33 | + |
| 34 | +>>> import typing |
| 35 | +>>> from gpsea.model import Patient |
| 36 | +>>> from gpsea.analysis.pscore import PhenotypeScorer |
| 37 | +>>> class BmiScorer(PhenotypeScorer): # ❶ |
| 38 | +... |
| 39 | +... def __init__( # ❷ |
| 40 | +... self, |
| 41 | +... id2bmi: typing.Mapping[str, float], |
| 42 | +... ): |
| 43 | +... self._id2bmi = id2bmi |
| 44 | +... |
| 45 | +... @property |
| 46 | +... def name(self) -> str: # ❸ |
| 47 | +... return "BMI phenotype scorer" |
| 48 | +... |
| 49 | +... @property |
| 50 | +... def description(self) -> str: # ❹ |
| 51 | +... return "Body mass index used as a phenotype score" |
| 52 | +... |
| 53 | +... @property |
| 54 | +... def variable_name(self) -> str: # ❺ |
| 55 | +... return "BMI" |
| 56 | +... |
| 57 | +... def score(self, patient: Patient) -> float: # ❻ |
| 58 | +... try: |
| 59 | +... return self._id2bmi[patient.labels.label] |
| 60 | +... except KeyError: |
| 61 | +... return float('nan') |
| 62 | + |
| 63 | +❶ The ``BmiScorer`` must extend :class:`~gpsea.analysis.pscore.PhenotypeScorer` |
| 64 | +to be used as a phenotype scorer. |
| 65 | +❷ The scorer needs a ``dict`` with `label` → `BMI` for the analyzed individuals. |
| 66 | +We assume the user will pre-compute the corresponding ``dict``. |
| 67 | + |
| 68 | +Then, the scorer must expose several properties, including ❸ ``name``, ❹ ``description``, |
| 69 | +and the ❺ ``variable_name`` it operates on. |
| 70 | +The properties provide bookkeeping metadata to use in e.g. visualizations. |
| 71 | +Try to choose short and concise names. |
| 72 | + |
| 73 | +The most important part of the scorer is the ❻ `score` method |
| 74 | +which retrieves the BMI for an individual or returns `NaN` if the value is not available |
| 75 | +and the individual should be omitted from the analysis. |
| 76 | + |
| 77 | +.. _custom-variant-predicate: |
| 78 | + |
| 79 | +***************** |
| 80 | +Variant predicate |
| 81 | +***************** |
| 82 | + |
| 83 | +The purpose of a :class:`~gpsea.analysis.predicate.VariantPredicate` is to test |
| 84 | +if a variant meets a certain criterion and GPSEA ships with an array |
| 85 | +of builtin predicates (see :mod:`gpsea.analysis.predicate` module). |
| 86 | +However, chances are a custom predicate will be needed in future, |
| 87 | +so we show how to how to extend |
| 88 | +the :class:`~gpsea.analysis.predicate.VariantPredicate` class |
| 89 | +to create one's own predicate. |
| 90 | + |
| 91 | +Specifically, we show how to create a predicate to test if the variant affects a glycine residue |
| 92 | +of the transcript of interest. |
| 93 | + |
| 94 | +>>> from gpsea.model import Variant, VariantEffect |
| 95 | +>>> from gpsea.analysis.predicate import VariantPredicate |
| 96 | +>>> class AffectsGlycinePredicate(VariantPredicate): # ❶ |
| 97 | +... def __init__( # ❷ |
| 98 | +... self, |
| 99 | +... tx_id: str, |
| 100 | +... ): |
| 101 | +... self._tx_id = tx_id |
| 102 | +... self._aa_code = "Gly" |
| 103 | +... |
| 104 | +... @property |
| 105 | +... def name(self) -> str: # ❸ |
| 106 | +... return "Affects Glycine" |
| 107 | +... |
| 108 | +... @property |
| 109 | +... def description(self) -> str: # ❹ |
| 110 | +... return "affects a glycine residue" |
| 111 | +... |
| 112 | +... @property |
| 113 | +... def variable_name(self) -> str: # ❺ |
| 114 | +... return "affected aminoacid residue" |
| 115 | +... |
| 116 | +... def test(self, variant: Variant) -> bool: # ❻ |
| 117 | +... tx_ann = variant.get_tx_anno_by_tx_id(self._tx_id) |
| 118 | +... if tx_ann is not None: |
| 119 | +... hgvsp = tx_ann.hgvsp |
| 120 | +... if hgvsp is not None: |
| 121 | +... return hgvsp.startswith(f"p.{self._aa_code}") |
| 122 | +... return False |
| 123 | +... |
| 124 | +... def __eq__(self, value: object) -> bool: # ➐ |
| 125 | +... return isinstance(value, AffectsGlycinePredicate) and self._tx_id == value._tx_id |
| 126 | +... |
| 127 | +... def __hash__(self) -> int: # ❽ |
| 128 | +... return hash((self._tx_id,)) |
| 129 | +... |
| 130 | +... def __repr__(self) -> str: # ❾ |
| 131 | +... return str(self) |
| 132 | +... |
| 133 | +... def __str__(self) -> str: # ➓ |
| 134 | +... return f"AffectsGlycinePredicate(tx_id={self._tx_id})" |
| 135 | + |
| 136 | +❶ The ``AffectsGlycinePredicate`` must extend :class:`~gpsea.analysis.predicate.VariantPredicate`. |
| 137 | +❷ We ask the user to provide the transcript accession `str` and we set the target aminoacid code to glycine ``Gly``. |
| 138 | +Like in the :ref:`custom-phenotype-scorer` above, ❸❹❺ provide metadata required for the bookkeeping. |
| 139 | +The ❻ ``test`` method includes the most interesting part - we retrieve the :class:`~gpsea.model.TranscriptAnnotation` |
| 140 | +with the functional annotation data for the transcript of interest, and we test if the HGVS protein indicates |
| 141 | +that the reference aminoacid is glycine. |
| 142 | +Last, we override ➐ ``__eq__()`` and ❽ ``__hash__()`` (required) as well as ❾ ``__repr__()`` and ➓ ``__str__()`` (recommended). |
0 commit comments