| 1 |  |  | from dataclasses import dataclass | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | from typing import Sequence, TypeVar, Set | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | from mandos.model.pubchem_api import PubchemApi | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | from mandos.model.pubchem_support.pubchem_models import DrugbankTargetType, DrugbankInteraction | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | from mandos.search.pubchem import PubchemHit, PubchemSearch | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | @dataclass(frozen=True, order=True, repr=True) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | class _DrugbankInteractionHit(PubchemHit): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     """""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     gene_symbol: str | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     protein_id: str | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     target_type: str | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     target_name: str | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     general_function: str | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | @dataclass(frozen=True, order=True, repr=True) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | class DrugbankTargetHit(_DrugbankInteractionHit): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     """""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | @dataclass(frozen=True, order=True, repr=True) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | class DrugbankGeneralFunctionHit(_DrugbankInteractionHit): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     """""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | T = TypeVar("T", bound=_DrugbankInteractionHit, covariant=True) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | class _DrugbankInteractionSearch(PubchemSearch[T]): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     def __init__(self, key: str, api: PubchemApi, target_types: Set[DrugbankTargetType]): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         super().__init__(key, api) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         self.target_types = target_types | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     """""" | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     @classmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     def _get_obj(cls, dd: DrugbankInteraction) -> str: | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         raise NotImplementedError() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     @classmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     def _get_predicate(cls, dd: DrugbankInteraction) -> str: | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         raise NotImplementedError() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     def find(self, inchikey: str) -> Sequence[T]: | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         data = self.api.fetch_data(inchikey) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         # noinspection PyPep8Naming | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         H = self.__class__.get_h() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             H( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |                 record_id=dd.record_id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                 origin_inchikey=inchikey, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |                 matched_inchikey=data.names_and_identifiers.inchikey, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |                 compound_id=str(data.cid), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |                 compound_name=data.name, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |                 predicate=self._get_predicate(dd), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                 object_id=self._get_obj(dd), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                 object_name=self._get_obj(dd), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                 search_key=self.key, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                 search_class=self.search_class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |                 data_source=self.data_source, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |                 gene_symbol=dd.gene_symbol, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                 protein_id=dd.protein_id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                 target_type=dd.target_type.name, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                 target_name=dd.target_name, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                 general_function=dd.general_function, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             for dd in data.biomolecular_interactions_and_pathways.drugbank_interactions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             if dd.target_type in self.target_types | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | class DrugbankTargetSearch(_DrugbankInteractionSearch[_DrugbankInteractionHit]): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     """""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     @property | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     def data_source(self) -> str: | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         return "DrugBank :: target interactions" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     @classmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     def _get_obj(cls, dd: DrugbankInteraction) -> str: | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         return dd.target_name | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |     @classmethod | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |     def _get_predicate(cls, dd: DrugbankInteraction) -> str: | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |         return dd.target_type.name + ("" if dd.action is None else (" :: " + dd.action) + " on") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | class DrugbankGeneralFunctionSearch(_DrugbankInteractionSearch[_DrugbankInteractionHit]): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     """""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     @property | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     def data_source(self) -> str: | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         return "DrugBank :: non-target interactions" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     @classmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     def _get_obj(cls, dd: DrugbankInteraction) -> str: | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         return dd.target_name if dd.general_function is None else dd.general_function | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     @classmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     def _get_predicate(cls, dd: DrugbankInteraction) -> str: | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         return ("action" if dd.action is None else dd.action) + " on" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | __all__ = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     "DrugbankTargetHit", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     "DrugbankGeneralFunctionHit", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     "DrugbankTargetSearch", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     "DrugbankGeneralFunctionSearch", | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 113 |  |  | ] | 
            
                                                        
            
                                    
            
            
                | 114 |  |  |  |