| 1 |  |  | from __future__ import annotations | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | import logging | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | from dataclasses import dataclass | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | from typing import Sequence | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | from pocketutils.core.dot_dict import NestedDotDict | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | from mandos import logger | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | from mandos.model.chembl_api import ChemblApi | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | from mandos.model.chembl_support import ChemblCompound | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | from mandos.model.chembl_support.chembl_utils import ChemblUtils | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | from mandos.search.chembl import ChemblHit, ChemblSearch | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | @dataclass(frozen=True, order=True, repr=True) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | class IndicationHit(ChemblHit): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     An indication with a MESH term. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     max_phase: int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     first_approval_year: str | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | class IndicationSearch(ChemblSearch[IndicationHit]): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     """""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     def __init__(self, key: str, api: ChemblApi, min_phase: int): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         super().__init__(key, api) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         self.min_phase = min_phase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     @property | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     def data_source(self) -> str: | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         return "ChEMBL :: indications" | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |     def find(self, lookup: str) -> Sequence[IndicationHit]: | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |         """ | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |         Args: | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |             lookup: | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |         Returns: | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |         """ | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         # 'atc_classifications': ['S01HA01', 'N01BC01', 'R02AD03', 'S02DA02'] | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |         # 'indication_class': 'Anesthetic (topical)' | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |         ch = ChemblUtils(self.api).get_compound_dot_dict(lookup) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |         compound = ChemblUtils(self.api).compound_dot_dict_to_obj(ch) | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         inds = self.api.drug_indication.filter(parent_molecule_chembl_id=compound.chid) | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |         hits = [] | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |         for ind in inds: | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |             if ind.req_as("max_phase_for_ind", int) >= self.min_phase: | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |                 hits.append(self.process(lookup, compound, ind)) | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |         return hits | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     def process( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         self, lookup: str, compound: ChemblCompound, indication: NestedDotDict | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     ) -> IndicationHit: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         phase = indication.req_as("max_phase_for_ind", int) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         return IndicationHit( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             record_id=indication.req_as("drugind_id", str), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             origin_inchikey=lookup, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             matched_inchikey=compound.inchikey, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             compound_id=compound.chid, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             compound_name=compound.name, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             predicate=f"used in a phase {phase} trial for", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             object_id=indication.req_as("mesh_id", str), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             object_name=indication.req_as("mesh_heading", str).strip("\n"), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             search_key=self.key, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             search_class=self.search_class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             data_source=self.data_source + " :: " + indication.req_as("references", str), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             max_phase=phase, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             first_approval_year=indication.req_as("first_approval", str), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 78 |  |  | __all__ = ["IndicationHit", "IndicationSearch"] | 
            
                                                        
            
                                    
            
            
                | 79 |  |  |  |