import DISNET.services.ServiceDisnet as ServiceDisnet import DISNET.Constants as Constants import DISNET.objects.DrugObject as DrugObject import DISNET.objects.DiseaseObject as DiseaseObject import DISNET.objects.TargetObject as TargetObject import DISNET.objects.PhenotypeObject as PhenotypeObject import DISNET.objects.DisnetObject as DisnetObject import DISNET.Exceptions as E C = Constants.Constants() class ServicePharma(ServiceDisnet.ServiceDisnet): """Class to instance objects to access methods related to the Pharmacology Layer. It inherits from the ServiceDisnet class """ def __init__(self): ServiceDisnet.ServiceDisnet.__init__(self) def getAllDiseases(self)-> list[DiseaseObject.DiseaseObject]: """Method to get all the Disease objects from the API. :raises NoResultsFoundException: when no results are found """ url = C.MAIN_PATH + C.PATH_PHARMA + C.PATH_DISEASES responseJson = self.connection(url) return self.__getAllDiseases(responseJson) def getAllPhenotypes(self)->list[PhenotypeObject.PhenotypeObject]: """Method to get all the Phenotype objects from the API. :raises NoResultsFoundException: when no results are found """ url = C.MAIN_PATH + C.PATH_PHARMA + C.PATH_PHENOTYPES responseJson = self.connection(url) return self.__getAllPhenotypes(responseJson) def getAllDrugs(self)->list[DrugObject.DrugObject]: """Method to get all the Drug objects from the API. :raises NoResultsFoundException: when no results are found """ url = C.MAIN_PATH + C.PATH_PHARMA + C.PATH_DRUGS responseJson = self.connection(url) return self.__getAllDrugs(responseJson) def getAllTargets(self) -> list[TargetObject.TargetObject]: """Method to get all the Target objects from the API. :raises NoResultsFoundException: when no results are found """ url = C.MAIN_PATH + C.PATH_PHARMA + C.PATH_TARGETS responseJson = self.connection(url) return self.__getAllTargets(responseJson) """ def getAll(self, next: str) -> list[DisnetObject.DisnetObject]: Method to get all the specified Disnet objects from the API. :param next: should be one of the following: DISEASES, PHENOTYPES, DRUGS, TARGETS :raises WrongOptionException: when the options are wrong :raises NoResultsFoundException: when no results are found if next.upper() == "DISEASES": url = C.MAIN_PATH + C.PATH_PHARMA + C.PATH_DISEASES responseJson = self.connection(url) returnList = self.__getAllDiseases(responseJson) elif next.upper() == "PHENOTYPES": url = C.MAIN_PATH + C.PATH_PHARMA + C.PATH_PHENOTYPES responseJson = self.connection(url) returnList = self.__getAllPhenotypes(responseJson) elif next.upper() == "DRUGS": url = C.MAIN_PATH + C.PATH_PHARMA + C.PATH_DRUGS responseJson = self.connection(url) returnList = self.__getAllDrugs(responseJson) elif next.upper() == "TARGETS": url = C.MAIN_PATH + C.PATH_PHARMA + C.PATH_TARGETS responseJson = self.connection(url) returnList = self.__getAllTargets(responseJson) else: raise E.WrongOptionChosen( "Possible inputs: DISEASES, PHENOTYPES, DRUGS, TARGETS. You entered " + str(next.upper())) return returnList """ """ def getDrugsBy(self, next: str, id: (str, int)) -> list[DrugObject.DrugObject]: Method to get all the drugs from the API specified by different options. :param next: should be one of the following: DISEASEID, PHENOTYPEID, TARGETID :param id: the id to filter by :raises WrongOptionException: when the options are wrong :raises NoResultsFoundException: when no results are found if next.upper() == "DISEASEID": url = C.MAIN_PATH + C.PATH_PHARMA + C.PATH_DISEASES + \ C.SLASH + str(id) + C.PATH_DRUGS elif next.upper() == "PHENOTYPEID": url = C.MAIN_PATH + C.PATH_PHARMA + C.PATH_PHENOTYPES + \ C.SLASH + str(id) + C.PATH_DRUGS elif next.upper() == "TARGETID": url = C.MAIN_PATH + C.PATH_PHARMA + C.PATH_TARGETS + \ C.SLASH + str(id) + C.PATH_DRUGS else: raise E.WrongOptionChosen( "Possible inputs: DISEASEID, PHENOTYPEID, TARGETID. You entered " + str(next.upper())) responseJson = self.connection(url) returnList = self.__getAllDrugs(responseJson) return returnList """ def getDrugsByDiseaseId(self, id: (str, int)) -> list[DrugObject.DrugObject]: """Method to get all the drugs from the API specified by the Disease id :param id: the id to filter by :raises NoResultsFoundException: when no results are found """ url = C.MAIN_PATH + C.PATH_PHARMA + C.PATH_DISEASES + C.SLASH + str(id) + C.PATH_DRUGS responseJson = self.connection(url) returnList = self.__getAllDrugs(responseJson) return returnList def getDrugsByPhenotypeId(self, id: (str, int)) -> list[DrugObject.DrugObject]: """Method to get all the drugs from the API specified by the Phenotype id :param id: the id to filter by :raises NoResultsFoundException: when no results are found """ url = C.MAIN_PATH + C.PATH_PHARMA + C.PATH_PHENOTYPES + C.SLASH + str(id) + C.PATH_DRUGS responseJson = self.connection(url) returnList = self.__getAllDrugs(responseJson) return returnList def getDrugsByTargetId(self, id: (str, int)) -> list[DrugObject.DrugObject]: """Method to get all the drugs from the API specified by the Target id :param id: the id to filter by :raises NoResultsFoundException: when no results are found """ url = C.MAIN_PATH + C.PATH_PHARMA + C.PATH_TARGETS + C.SLASH + str(id) + C.PATH_DRUGS responseJson = self.connection(url) returnList = self.__getAllDrugs(responseJson) return returnList def getDrugByDrugID(self, id: (str, id)) -> DrugObject.DrugObject: """Method to get the drug from the API specified by its Id. :param id: the id of the drug to look for :raises NoResultsFoundException: when no results are found """ url = C.MAIN_PATH + C.PATH_PHARMA + C.PATH_DRUGS + C.QUESTION_MARK_ID_EQUALS + str(id) responseJson = self.connection(url) drugJson = responseJson["data"] drugId = drugJson["drugId"] drugName = drugJson["drugName"] molecularType = drugJson["molecularType"] chemicalStructure = drugJson["chemicalStructure"] inchiKey = drugJson["inchiKey"] drug = DrugObject.DrugObject(drugId, drugName, molecularType, chemicalStructure, inchiKey) return drug def getDrugsByDrugName(self, name: str, exactMatch: bool) -> list[DrugObject.DrugObject]: """Method to get the drug from the API specified by its name or all that match. :param name: the name of the drug to look for :param exactMatch: true for only exact matches false for any match :raises NoResultsFoundException: when no results are found """ url = C.MAIN_PATH + C.PATH_PHARMA + C.PATH_DRUGS + C.QUESTION_MARK_NAME_EQUALS + \ name.replace(" ", "%20") + C.AMPERSAND_EXACT_MATCH_EQUALS + str(exactMatch) responseJson = self.connection(url) returnList = self.__getAllDrugs(responseJson) return returnList def getDiseaseByDiseaseID(self, id: (str, int)) -> DiseaseObject.DiseaseObject: """Method to get the disease from the API specified by its Id. :param id: the id of the disease to look for :raises NoResultsFoundException: when no results are found """ url = C.MAIN_PATH + C.PATH_PHARMA + C.PATH_DISEASES + C.QUESTION_MARK_ID_EQUALS + str(id) responseJson = self.connection(url) diseaseJson = responseJson["data"] diseaseId = diseaseJson["diseaseId"] diseaseName = diseaseJson["diseaseName"] disease = DiseaseObject.DiseaseObject(diseaseId, diseaseName) return disease def getDiseaseByDiseaseName(self, name, exactMatch): """Method to get the disease from the API specified by its name or all that match. :param name: the name of the disease to look for :param exactMatch: true for only exact matches false for any match :raises NoResultsFoundException: when no results are found """ url = C.MAIN_PATH + C.PATH_PHARMA + C.PATH_DISEASES + C.QUESTION_MARK_NAME_EQUALS + \ name.replace(" ", "%20") + C.AMPERSAND_EXACT_MATCH_EQUALS + str(exactMatch) responseJson = self.connection(url) returnList = self.__getAllDiseases(responseJson) return returnList def getTargetByTargetID(self, id: (str, int)) -> TargetObject.TargetObject: """Method to get the target from the API specified by its Id. :param id: the id of the target to look for :raises NoResultsFoundException: when no results are found """ url = C.MAIN_PATH + C.PATH_PHARMA + C.PATH_TARGETS + C.QUESTION_MARK_ID_EQUALS + str(id) responseJson = self.connection(url) targetJson = responseJson["data"] targetId = targetJson["targetId"] targetName = targetJson["targetName"] targeType = targetJson["targetType"] organismName = targetJson["organismName"] target = TargetObject.TargetObject(targetId, targetName, targeType, organismName) return target def getTargetByTargetName(self, name: str, exactMatch: bool) -> TargetObject.TargetObject: """Method to get the target from the API specified by its name or all that match. :param name: the name of the target to look for :param exactMatch: true for only exact matches false for any match :raises NoResultsFoundException: when no results are found """ url = C.MAIN_PATH + C.PATH_PHARMA + C.PATH_TARGETS + C.QUESTION_MARK_NAME_EQUALS + \ name.replace(" ", "%20") + C.AMPERSAND_EXACT_MATCH_EQUALS + str(exactMatch) responseJson = self.connection(url) returnList = self.__getAllTargets(responseJson) return returnList def getPhenotypeByPhenotypeID(self, id: (str, int)) -> PhenotypeObject.PhenotypeObject: """Method to get the phenotype from the API specified by its Id. :param id: the id of the phenotype to look for :raises NoResultsFoundException: when no results are found """ url = C.MAIN_PATH + C.PATH_PHARMA + C.PATH_PHENOTYPES + C.QUESTION_MARK_ID_EQUALS + str(id) responseJson = self.connection(url) phenoJson = responseJson["data"] phenotypeId = phenoJson["phenotypeId"] pehnotypeName = phenoJson["phenotypeName"] pheno = PhenotypeObject.PhenotypeObject(phenotypeId, pehnotypeName) return pheno def getPhenotypeByPhenotypeName(self, name: str, exactMatch: bool) -> PhenotypeObject.PhenotypeObject: """Method to get the phenotype from the API specified by its name or all that match. :param name: the name of the phenotype to look for :param exactMatch: true for only exact matches false for any match :raises NoResultsFoundException: when no results are found """ url = C.MAIN_PATH + C.PATH_PHARMA + C.PATH_PHENOTYPES + C.QUESTION_MARK_NAME_EQUALS + \ name.replace(" ", "%20") + C.AMPERSAND_EXACT_MATCH_EQUALS + str(exactMatch) responseJson = self.connection(url) returnList = self.__getAllPhenotypes(responseJson) return returnList def getDiseasesByDrugID(self, id: (str, int)) -> list[DiseaseObject.DiseaseObject]: """Method to get the diseases associated to the drug identified by its id. :param id: the id of the drug to filter by :raises NoResultsFoundException: when no results are found """ url = C.MAIN_PATH + C.PATH_PHARMA + C.PATH_DRUGS + C.SLASH + str(id) + C.PATH_DISEASES responseJson = self.connection(url) returnList = self.__getAllDiseases(responseJson) return returnList def getPhenotypesByDrugsID(self, id: (str, int)) -> list[PhenotypeObject.PhenotypeObject]: """Method to get the phenotypes associated to the drug identified by its id. :param id: the id of the drug to filter by :raises NoResultsFoundException: when no results are found """ url = C.MAIN_PATH + C.PATH_PHARMA + C.PATH_DRUGS + C.SLASH + str(id) + C.PATH_PHENOTYPES responseJson = self.connection(url) returnList = self.__getAllPhenotypes(responseJson) return returnList def getTargetsByDrugID(self, id: (str, int)) -> list[TargetObject.TargetObject]: """Method to get the targets associated to the drug identified by its id. :param id: the id of the drug to filter by :raises NoResultsFoundException: when no results are found """ url = C.MAIN_PATH + C.PATH_PHARMA + C.PATH_DRUGS + C.SLASH + str(id) + C.PATH_TARGETS responseJson = self.connection(url) returnList = self.__getAllTargets(responseJson) return returnList def getInteractingDrugsByDrugsID(self, id: (str, int)) -> list[DrugObject.DrugObject]: """Method to get the Interacting Drugs associated to a different drug identified by its id. :param id: the id of the drug to filter by :raises NoResultsFoundException: when no results are found """ url = C.MAIN_PATH + C.PATH_PHARMA + C.PATH_DRUGS + C.SLASH + id + C.SLASH_INTERACTIONS responseJson = self.connection(url) returnList = self.__getAllDrugs(responseJson) return returnList def getPhenotypesByDDI(self, id1: (str, int), id2: (str, int)) -> list[PhenotypeObject.PhenotypeObject]: """Method to get the Phenotypes associated to the drug interaction identified by its id. :param id: the id of the drug interaction to filter by :raises NoResultsFoundException: when no results are found """ url = C.MAIN_PATH + C.PATH_PHARMA + C.PATH_DRUGS + C.SLASH + str(id1) + \ C.SLASH_INTERACTIONS + C.SLASH + str(id2) + C.PATH_PHENOTYPES responseJson = self.connection(url) returnList = self.__getAllPhenotypes(responseJson) return returnList def __getAllDiseases(self, responseJson: dict) -> list[DiseaseObject.DiseaseObject]: """Method to get all diseases from the response received in other methods :param responseJson: json containing the response of the get petition """ listDiseases = responseJson["data"] returnList = [] for diseaseJson in listDiseases: diseaseId = diseaseJson["diseaseId"] diseaseName = diseaseJson["diseaseName"] disease = DiseaseObject.DiseaseObject(diseaseId, diseaseName) returnList.append(disease) return returnList def __getAllPhenotypes(self, responseJson: dict) -> list[PhenotypeObject.PhenotypeObject]: """Method to get all Phenotypes from the response received in other methods :param responseJson: json containing the response of the get petition """ listPhenotypes = responseJson["data"] returnList = [] for phenoJson in listPhenotypes: phenotypeId = phenoJson["phenotypeId"] phenotypeName = phenoJson["phenotypeName"] pheno = PhenotypeObject.PhenotypeObject(phenotypeId, phenotypeName) returnList.append(pheno) return returnList def __getAllDrugs(self, responseJson: dict) -> list[DrugObject.DrugObject]: """Method to get all drugs from the response received in other methods :param responseJson: json containing the response of the get petition """ listDrugs = responseJson["data"] returnList = [] for drugJson in listDrugs: drugId = drugJson["drugId"] drugName = drugJson["drugName"] drug = DrugObject.DrugObject(drugId, drugName) returnList.append(drug) return returnList def __getAllTargets(self, responseJson): """Method to get all targets from the response received in other methods :param responseJson: json containing the response of the get petition """ listTargets = responseJson["data"] returnList = [] for targetJson in listTargets: targetId = targetJson["targetId"] targetName = targetJson["targetName"] target = TargetObject.TargetObject(targetId, targetName) returnList.append(target) return returnList