예제 #1
0
 def getAccessionMapping(self, wurcsTupL):
     """Fetch GlyTouCan accessions for the input WURCS desriptor list"""
     accessionMapD = {}
     logger.info("Fetching (%d) WURCS descriptors", len(wurcsTupL))
     baseUrl = "https://api.glycosmos.org"
     endPoint = "glytoucan/sparql/wurcs2gtcids"
     numDescriptors = len(wurcsTupL)
     for ii, (entityId, wurcs) in enumerate(wurcsTupL, 1):
         try:
             pD = {}
             pD["wurcs"] = wurcs
             uR = UrlRequestUtil()
             rDL, retCode = uR.post(baseUrl, endPoint, pD, returnContentType="JSON")
             logger.debug(" %r wurcs fetch result (%r) %r", entityId, retCode, rDL)
             if rDL:
                 for rD in rDL:
                     if "id" in rD:
                         accessionMapD.setdefault(wurcs, []).append(rD["id"])
                     else:
                         logger.info("%r fetch fails (%r) (%r) %r", entityId, retCode, wurcs, rDL)
             if ii % 5 == 0:
                 logger.info("Fetched %d/%d", ii, numDescriptors)
         except Exception as e:
             logger.exception("Failing for (%r) wurcs (%r) with %s", entityId, wurcs, str(e))
     return accessionMapD
예제 #2
0
 def testUnpBatchFetchPost(self):
     """UniProt batch fetch (ebi dbfetch) post test"""
     baseUrl = "https://www.ebi.ac.uk"
     endPoint = "Tools/dbfetch/dbfetch"
     idList = self.__unpIdList1[:10]
     try:
         pD = {}
         pD["db"] = "uniprotkb"
         pD["id"] = ",".join(idList)
         pD["format"] = "uniprotxml"
         pD["style"] = "raw"
         #
         ureq = UrlRequestUtil()
         ret, retCode = ureq.post(baseUrl, endPoint, pD)
         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()
예제 #3
0
    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()