def doGeneLookup(self, geneName, taxId, reviewed=False): """ """ rL = [] try: baseUrl = self.__urlPrimary endPoint = "uniprot" # hL = [("Accept", "application/xml")] hL = [] if reviewed: pD = { "query": 'gene:"%s" and taxonomy:%s and reviewed:yes' % (geneName, taxId), "format": "list" } else: pD = { "query": 'gene:"%s" and taxonomy:%s' % (geneName, taxId), "format": "list" } ureq = UrlRequestUtil() rspTxt, retCode = ureq.get(baseUrl, endPoint, pD, headers=hL) tValL = rspTxt.split("\n") if rspTxt else [] idList = [tVal for tVal in tValL if tVal] return idList, retCode except Exception as e: logger.exception("Failing with %s", str(e)) return rL, None
def testUnpBatchFetchGetUrllib(self): """UniProt batch fetch (uploadlists) get test (urllib)""" baseUrl = "https://www.uniprot.org" # baseUrl = "https://pir3.uniprot.org" endPoint = "uploadlists" idList = self.__unpIdList1[:10] try: # hD = {"Accept": "application/xml"} hL = [("Accept", "application/xml")] pD = { "from": "ACC+ID", "to": "ACC", "format": "xml", "query": " ".join(idList) } ureq = UrlRequestUtil() # using wrapped version ret, retCode = ureq.get(baseUrl, endPoint, pD, headers=hL, sslCert="enable") logger.debug("XML result %r", ret) nm = ret.count("<entry ") logger.info("Result count %d status code %r", nm, retCode) self.assertGreaterEqual(nm, len(idList)) except Exception as e: logger.exception("Failing with %s", str(e)) self.fail()
def __doRequestPrimary(self, idList): """ http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=pubmed&retmode=xml&id=ID1,ID2,... """ baseUrl = "http://eutils.ncbi.nlm.nih.gov" endPoint = "entrez/eutils/efetch.fcgi" hL = [("Accept", "application/xml")] pD = {"db": "pubmed", "retmode": "xml", "id": ",".join(idList)} ureq = UrlRequestUtil() return ureq.get(baseUrl, endPoint, pD, headers=hL)
def __doRequestSecondary(self, idList): baseUrl = self.__urlSecondary endPoint = "proteins/api/proteins" # hL = [("Accept", "application/xml")] pD = {} pD["size"] = "-1" pD["accession"] = ",".join(idList) ureq = UrlRequestUtil() return ureq.get(baseUrl, endPoint, pD, headers=hL)
def __doRequestPrimary(self, idList): """ """ baseUrl = self.__urlPrimary endPoint = "uploadlists" hL = [("Accept", "application/xml")] pD = { "from": "ACC+ID", "to": "ACC", "format": "xml", "query": " ".join(idList) } ureq = UrlRequestUtil() return ureq.get(baseUrl, endPoint, pD, headers=hL)
def getStatusDetails(self): try: version = releaseDateString = None baseUrl = "https://www.ebi.ac.uk" endPoint = "chembl/api/data/status.json" hL = [] pD = {} ureq = UrlRequestUtil() ret, retCode = ureq.get(baseUrl, endPoint, pD, headers=hL, returnContentType="JSON") logger.info("retCode %r ret %r", retCode, ret) if ret: tS = ret["chembl_db_version"] if ret and "chembl_db_version" in ret else None version = tS.split("_")[1] if tS and tS.split("_")[1] else None releaseDateString = ret["chembl_release_date"] if "chembl_release_date" in ret else None except Exception as e: logger.exception("Failing with %s", str(e)) return version, releaseDateString
def testUnpBatchFetchFail(self): """UniProt batch fetch (proteins) get test (expected failure)""" baseUrl = "https://www0.ebi.ac.uk" endPoint = "proteins/api/proteins" idList = self.__unpIdList1[:10] try: hL = [("Accept", "application/xml")] pD = {} pD["size"] = "-1" pD["accession"] = ",".join(idList) ureq = UrlRequestUtil() ret, retCode = ureq.get(baseUrl, endPoint, pD, headers=hL) logger.debug("XML result %r", ret) logger.debug("Result status code %r", retCode) self.assertEqual(ret, None) self.assertEqual(retCode, None) except Exception as e: logger.exception("Failing with %s", str(e)) self.fail()
def testUnpBatchFetchGetEbi(self): """UniProt batch fetch (proteins) get test (EBI endpoint)""" baseUrl = "https://www.ebi.ac.uk" endPoint = "proteins/api/proteins" idList = self.__unpIdList1[:10] try: hL = [("Accept", "application/xml")] pD = {} pD["size"] = "-1" pD["accession"] = ",".join(idList) ureq = UrlRequestUtil() ret, retCode = ureq.get(baseUrl, endPoint, pD, headers=hL) logger.debug("XML result %r", ret) nm = ret.count("<entry ") logger.info("Result count %d status code %r", nm, retCode) self.assertGreaterEqual(nm, len(idList) - 1) except Exception as e: logger.exception("Failing with %s", str(e)) self.fail()
def testNcbiFetchEntryPost(self): """NCBI batch fetch (efetch) get test""" idList = ["AP012306.1", "U53879.1"] database = "Nucleotide" baseUrl = "https://eutils.ncbi.nlm.nih.gov" endPoint = "entrez/eutils/efetch.fcgi" try: hL = [("Accept", "application/xml")] pD = {} pD["db"] = database pD["id"] = ",".join(idList) pD["retmode"] = "xml" ureq = UrlRequestUtil() ret, retCode = ureq.get(baseUrl, endPoint, pD, headers=hL) nm = ret.count("<GBSeq_length>") logger.debug("XML result %r", ret) logger.info("Result count %d status code %r", nm, retCode) self.assertGreaterEqual(nm, len(idList)) except Exception as e: logger.exception("Failing with %s", str(e)) self.fail()
def doLookup(self, itemList, itemKey="GENENAME"): """ """ rL = [] try: baseUrl = self.__urlPrimary endPoint = "uploadlists" # hL = [("Accept", "application/xml")] hL = [] pD = { "from": itemKey, "to": "ACC", "format": "list", "query": " ".join(itemList) } ureq = UrlRequestUtil() rspTxt, retCode = ureq.get(baseUrl, endPoint, pD, headers=hL) tValL = rspTxt.split("\n") if rspTxt else [] idList = [tVal for tVal in tValL if tVal] return idList, retCode except Exception as e: logger.exception("Failing with %s", str(e)) return rL, None
def testPubChemFetchClassification(self): """PubChem fetch classification test - can timeout""" idTupList = [("2244", 200, "2244", "record"), ("123631", 200, "123631", "record"), ("2244", 200, "2244", "classification"), ("123631", 200, "123631", "classification")] nameSpace = "cid" domain = "compound" searchType = "lookup" # returnType = "record" requestType = "GET" outputType = "JSON" baseUrl = "https://pubchem.ncbi.nlm.nih.gov" httpCodesCatch = [404] try: for (identifier, testRetCode, testPcId, returnType) in idTupList: for requestType in ["GET", "POST"]: logger.info( "namespace %r identifier %r returnType %r requestType %r", nameSpace, identifier, returnType, requestType) ret, retCode = None, None pD = {} hL = [] ureq = UrlRequestUtil() if nameSpace in [ "cid", "name", "inchikey" ] and returnType in ["record"] and searchType in [ "lookup" ] and requestType == "GET": uId = quote(identifier.encode("utf8")) endPoint = "/".join([ "rest", "pug", domain, nameSpace, uId, outputType ]) ret, retCode = ureq.get(baseUrl, endPoint, pD, headers=hL, httpCodesCatch=httpCodesCatch, returnContentType="JSON") elif nameSpace in [ "cid", "name", "inchikey" ] and returnType in ["record"] and searchType in [ "lookup" ] and requestType == "POST": endPoint = "/".join( ["rest", "pug", domain, nameSpace, outputType]) pD = {nameSpace: identifier} ret, retCode = ureq.post(baseUrl, endPoint, pD, headers=hL, httpCodesCatch=httpCodesCatch, returnContentType="JSON") # elif nameSpace in ["cid"] and returnType in [ "classification" ] and searchType in ["lookup"] and requestType == "GET": # Needs to be specifically targeted on a particular compound ... uId = quote(identifier.encode("utf8")) endPoint = "/".join([ "rest", "pug", domain, nameSpace, uId, returnType, outputType ]) # pD = {"classification_type": "simple"} pD = {} # pD = {nameSpace: identifier} ret, retCode = ureq.getUnWrapped( baseUrl, endPoint, pD, headers={}, httpCodesCatch=httpCodesCatch, returnContentType="JSON") # elif nameSpace in ["cid"] and returnType in [ "classification" ] and searchType in ["lookup"] and requestType == "POST": # Needs to be specifically targeted on a particular compound ... endPoint = "/".join([ "rest", "pug", domain, nameSpace, returnType, outputType ]) # This is a long request return server codes may be observed 500 # pD = {nameSpace: identifier, "classification_type": "simple"} pD = {nameSpace: identifier} ret, retCode = ureq.postUnWrapped( baseUrl, endPoint, pD, headers={}, httpCodesCatch=httpCodesCatch, returnContentType="JSON") # # logger.debug("Result status code %r", retCode) self.assertEqual(retCode, testRetCode) if retCode == 200 and returnType == "record": pcId = str(ret["PC_Compounds"][0]["id"]["id"]["cid"]) self.assertEqual(pcId, testPcId) # except Exception as e: logger.exception("Failing with %s", str(e)) self.fail()