예제 #1
0
 def testGetChemSearchRequests(self):
     """ChemSearch repetition GET protocol test (using requests module)"""
     # dev instances east
     # baseUrl = ["http://128.6.159.86"]
     #
     # Production west instances
     # baseUrlList = ["http://132.249.213.210", "http://132.249.213.110", "https://chemsearch-west.rcsb.org"]
     # baseUrlList = ["http://128.6.158.85", "http://128.6.158.158", "https://chemsearch-east.rcsb.org"]
     baseUrlList = [
         "https://chemsearch-west.rcsb.org",
         "https://chemsearch-east.rcsb.org"
     ]
     #
     endPoint = "chem-match-v1/InChI"
     resultLen = 13
     descr = "InChI=1S/C9H15N5O3/c1-3(15)6(16)4-2-11-7-5(12-4)8(17)14-9(10)13-7/h3-4,6,12,15-16H,2H2,1H3,(H4,10,11,13,14,17)/t3-,4-,6-/m1/s1"
     try:
         for baseUrl in baseUrlList:
             pD = {"query": descr, "matchType": "fingerprint-similarity"}
             for ii in range(100):
                 ureq = UrlRequestUtil()
                 ret, retCode = ureq.getUnWrapped(baseUrl,
                                                  endPoint,
                                                  pD,
                                                  headers={},
                                                  sslCert="enable",
                                                  returnContentType="JSON")
                 if len(ret["matchedIdList"]) != resultLen:
                     logger.info(">>> %3d (%r) (%r) result length %r",
                                 ii, baseUrl, retCode,
                                 len(ret["matchedIdList"]))
     except Exception as e:
         logger.exception("Failing with %s", str(e))
         self.fail()
예제 #2
0
    def testUnpBatchFetchGetRequests(self):
        """UniProt batch fetch (uploadlists) get test (requests)"""

        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 unwrapped (requests) version
            ret, retCode = ureq.getUnWrapped(baseUrl,
                                             endPoint,
                                             pD,
                                             headers=hD,
                                             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()
예제 #3
0
 def __doSequenceRequestSecondary(self, unpIdList):
     """ """
     sD = {}
     baseUrl = self.__urlSecondary
     hD = {"Accept": "text/x-fasta"}
     pD = {}
     ok = True
     for unpId in unpIdList:
         endPoint = "proteins/api/proteins/" + unpId
         ureq = UrlRequestUtil()
         ret, retCode = ureq.getUnWrapped(baseUrl, endPoint, pD, headers=hD)
         if retCode in [200] and ret and len(ret) > 0:
             rOk, seqId, rD = self.__parseFastaResponse(ret)
             if rOk:
                 sD[seqId] = rD
             else:
                 logger.error("Parsing error in sequence data for %r",
                              unpId)
         else:
             ok = False
     return ok, sD
예제 #4
0
 def __doSequenceRequestPrimary(self, unpIdList):
     """ """
     sD = {}
     fmt = "fasta"
     baseUrl = self.__urlPrimary
     hD = {"Accept": "text/x-fasta"}
     pD = {}
     ureq = UrlRequestUtil()
     ok = True
     for unpId in unpIdList:
         endPoint = "uniprot/" + unpId + "." + fmt
         ret, retCode = ureq.getUnWrapped(baseUrl, endPoint, pD, headers=hD)
         logger.debug("unpId %r url %s endpoint %r ret %r retCode %r",
                      unpId, baseUrl, endPoint, ret, retCode)
         if retCode in [200] and ret and len(ret) > 0:
             rOk, seqId, rD = self.__parseFastaResponse(ret)
             if rOk:
                 sD[seqId] = rD
             else:
                 logger.error("Parsing error in sequence data for %r",
                              unpId)
         else:
             ok = False
     return ok, sD
예제 #5
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()
예제 #6
0
    def testPubChemFetch(self):
        """PubChem fetch test"""
        idTupList = [("JTOKYIBTLUQVQV-FGHQGBLESA-N", 404, None),
                     ("CXHHBNMLPJOKQD-UHFFFAOYSA-N", 200, 78579)]
        nameSpace = "inchikey"
        domain = "compound"
        searchType = "lookup"
        returnType = "record"
        requestType = "GET"
        outputType = "JSON"
        baseUrl = "https://pubchem.ncbi.nlm.nih.gov"
        httpCodesCatch = [404]

        try:
            for (identifier, testRetCode, testPcId) in idTupList:
                for requestType in ["GET", "POST"]:
                    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.getUnWrapped(
                            baseUrl,
                            endPoint,
                            pD,
                            headers=hL,
                            httpCodesCatch=httpCodesCatch,
                            returnContentType="JSON",
                            sslCert="enable")
                    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.postUnWrapped(
                            baseUrl,
                            endPoint,
                            pD,
                            headers=hL,
                            httpCodesCatch=httpCodesCatch,
                            returnContentType="JSON",
                            sslCert="enable")
                    #
                    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 = {nameSpace: identifier}
                        ret, retCode = ureq.getUnWrapped(
                            baseUrl,
                            endPoint,
                            pD,
                            headers=hL,
                            httpCodesCatch=httpCodesCatch,
                            returnContentType="JSON",
                            sslCert="enable")
                    #
                    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=hL,
                            httpCodesCatch=httpCodesCatch,
                            returnContentType="JSON",
                            sslCert="enable")
                    #
                    #
                    logger.debug("Result status code %r", retCode)
                    self.assertEqual(retCode, testRetCode)
                    if retCode == 200:
                        pcId = ret["PC_Compounds"][0]["id"]["id"]["cid"]
                        self.assertEqual(pcId, testPcId)

            #
        except Exception as e:
            logger.exception("Failing with %s", str(e))
            self.fail()