Пример #1
0
 def test404sReturnJson(self):
     paths = [
         '/doesNotExist',
         '/reads/sea',
         '/variantsets/id/doesNotExist',
     ]
     for path in paths:
         response = self.app.get(path)
         protocol.fromJson(response.get_data(), protocol.GAException)
         self.assertEqual(404, response.status_code)
Пример #2
0
 def test404sReturnJson(self):
     paths = [
         '/doesNotExist',
         '/reads/sea',
         '/variantsets/id/doesNotExist',
     ]
     for path in paths:
         response = self.app.get(path)
         protocol.fromJson(
             response.get_data(), protocol.GAException)
         self.assertEqual(404, response.status_code)
Пример #3
0
    def test_search_datasets(self):
        response = self.client.post('/datasets/search', data='{}')
        response_proto = protocol.fromJson(response.get_data(),
                                           protocol.SearchDatasetsResponse)
        self.assertIsNotNone(response_proto, "Something should be returned")
        self.assertEqual(response.status_code, 200, "A known good endpoint "
                         "should return success")
        self.assertGreater(len(response_proto.datasets), 0,
                           "Some datasets should be returned.")

        response = self.client.post('/datasets/search', data='{"pageSize": 2}')
        response_proto = protocol.fromJson(response.get_data(),
                                           protocol.SearchDatasetsResponse)
        self.assertEqual(len(response_proto.datasets), 2)
Пример #4
0
 def populateFromRow(self, readGroupSetRecord):
     """
     Populates the instance variables of this ReadGroupSet from the
     specified database row.
     """
     self._dataUrl = readGroupSetRecord.dataurl
     self._indexFile = readGroupSetRecord.indexfile
     self._programs = []
     for jsonDict in json.loads(readGroupSetRecord.programs):
         program = protocol.fromJson(json.dumps(jsonDict), protocol.Program)
         self._programs.append(program)
     stats = protocol.fromJson(readGroupSetRecord.stats, protocol.ReadStats)
     self._numAlignedReads = stats.aligned_read_count
     self._numUnalignedReads = stats.unaligned_read_count
Пример #5
0
 def populateFromRow(self, readGroupSetRecord):
     """
     Populates the instance variables of this ReadGroupSet from the
     specified database row.
     """
     self._dataUrl = readGroupSetRecord.dataurl
     self._indexFile = readGroupSetRecord.indexfile
     self._programs = []
     for jsonDict in json.loads(readGroupSetRecord.programs):
         program = protocol.fromJson(json.dumps(jsonDict),
                                     protocol.Program)
         self._programs.append(program)
     stats = protocol.fromJson(readGroupSetRecord.stats, protocol.ReadStats)
     self._numAlignedReads = stats.aligned_read_count
     self._numUnalignedReads = stats.unaligned_read_count
Пример #6
0
    def test_search_read_group_sets(self):
        response = self.client.post('/readgroupsets/search', data='{}')
        response_proto = protocol.fromJson(
            response.get_data(), protocol.SearchReadGroupSetsResponse)
        self.assertIsNotNone(response_proto, "Something should be returned")
        self.assertEqual(response.status_code, 200, "A known good endpoint "
                         "should return success")
        self.assertGreater(len(response_proto.read_group_sets), 0,
                           "Some read group sets should be returned.")

        response = self.client.post('/readgroupsets/search',
                                    data='{"read_group_sets": 356464}')
        response_proto = protocol.fromJson(
            response.get_data(), protocol.SearchReadGroupSetsResponse)
        self.assertEqual(len(response_proto.read_group_sets), 356464)
Пример #7
0
 def sendCallSetsSearch(self):
     response = self.sendVariantSetsSearch()
     variantSets = protocol.fromJson(
         response.data, protocol.SearchVariantSetsResponse).variant_sets
     request = protocol.SearchCallSetsRequest()
     request.variant_set_id = variantSets[0].id
     return self.sendPostRequest('/callsets/search', request)
Пример #8
0
 def test_search_reads(self):
     run_accession = 'SRR2856889'  # paired
     # acc = 'SRR1482462' # unpaired
     reference_name = 'chr1'
     start = 9000000
     end = 9100000
     request = protocol.SearchReadsRequest()
     request.read_group_ids.extend([run_accession])
     request.reference_id = reference_name
     request.start = start
     request.end = end
     reads_list = ncbi.search_reads(request)
     response = self.client.post('/reads/search',
                                 data=protocol.toJson(request))
     response_proto = protocol.fromJson(response.get_data(),
                                        protocol.SearchReadsResponse)
     self.assertIsNotNone(response_proto, "Something should be returned")
     self.assertEqual(response.status_code, 200, "A known good endpoint "
                      "should return success")
     i = 0
     for ga_alignment in response_proto.alignments:
         self.assertEqual(reads_list[i], ga_alignment)
         i += 1
     self.assertGreater(len(response_proto.alignments), 0,
                        "Some alignments should be returned.")
Пример #9
0
def search_reads_route():
    search_request = protocol.SearchReadsRequest
    try:
        deserialized_request = protocol.fromJson(request.get_data(),
                                                 search_request)
    except Exception, e:
        print str(e)
Пример #10
0
 def runSearchRequest(
         self, requestStr, requestClass, responseClass, objectGenerator):
     """
     Runs the specified request. The request is a string containing
     a JSON representation of an instance of the specified requestClass.
     We return a string representation of an instance of the specified
     responseClass in JSON format. Objects are filled into the page list
     using the specified object generator, which must return
     (object, nextPageToken) pairs, and be able to resume iteration from
     any point using the nextPageToken attribute of the request object.
     """
     self.startProfile()
     try:
         request = protocol.fromJson(requestStr, requestClass)
     except protocol.json_format.ParseError:
         raise exceptions.InvalidJsonException(requestStr)
     # TODO How do we detect when the page size is not set?
     if not request.page_size:
         request.page_size = self._defaultPageSize
     if request.page_size < 0:
         raise exceptions.BadPageSizeException(request.page_size)
     responseBuilder = response_builder.SearchResponseBuilder(
         responseClass, request.page_size, self._maxResponseLength)
     nextPageToken = None
     for obj, nextPageToken in objectGenerator(request):
         responseBuilder.addValue(obj)
         if responseBuilder.isFull():
             break
     responseBuilder.setNextPageToken(nextPageToken)
     responseString = responseBuilder.getSerializedResponse()
     self.endProfile()
     return responseString
Пример #11
0
 def runSearchRequest(
         self, requestStr, requestClass, responseClass, objectGenerator):
     """
     Runs the specified request. The request is a string containing
     a JSON representation of an instance of the specified requestClass.
     We return a string representation of an instance of the specified
     responseClass in JSON format. Objects are filled into the page list
     using the specified object generator, which must return
     (object, nextPageToken) pairs, and be able to resume iteration from
     any point using the nextPageToken attribute of the request object.
     """
     self.startProfile()
     try:
         request = protocol.fromJson(requestStr, requestClass)
     except protocol.json_format.ParseError:
         raise exceptions.InvalidJsonException(requestStr)
     # TODO How do we detect when the page size is not set?
     if not request.page_size:
         request.page_size = self._defaultPageSize
     if request.page_size < 0:
         raise exceptions.BadPageSizeException(request.page_size)
     responseBuilder = response_builder.SearchResponseBuilder(
         responseClass, request.page_size, self._maxResponseLength)
     nextPageToken = None
     for obj, nextPageToken in objectGenerator(request):
         responseBuilder.addValue(obj)
         if responseBuilder.isFull():
             break
     responseBuilder.setNextPageToken(nextPageToken)
     responseString = responseBuilder.getSerializedResponse()
     self.endProfile()
     return responseString
Пример #12
0
 def populateFromJson(self, jsonString):
     try:
         parsed = protocol.fromJson(jsonString, protocol.Experiment)
     except:
         raise exceptions.InvalidJsonException(jsonString)
     if parsed.message_create_time != "":
         self._created = parsed.message_create_time
     if parsed.message_update_time != "":
         self._updated = parsed.message_update_time
     self._run_time = parsed.run_time
     self._description = parsed.description
     self._molecule = parsed.molecule
     self._strategy = parsed.strategy
     self._selection = parsed.selection
     self._library = parsed.library
     self._library_layout = parsed.library_layout
     self._instrument_model = parsed.instrument_model
     self._instrument_data_file = parsed.instrument_data_file
     self._sequencing_center = parsed.sequencing_center
     self._platform_unit = parsed.platform_unit
     attributes = {}
     for key in parsed.attributes.attr:
         attributes[key] = {
             "values": protocol.toJsonDict(parsed.attributes.attr[key])
         }
     self.setAttributes(attributes)
     return self
Пример #13
0
 def _deserialize_response(self, json_response_string,
                           protocol_response_class):
     self._protocol_bytes_received += len(json_response_string)
     self._logger.debug("response:{}".format(json_response_string))
     if not json_response_string:
         raise exceptions.EmptyResponseException()
     return protocol.fromJson(json_response_string, protocol_response_class)
Пример #14
0
 def testNextPageToken(self):
     responseClass = protocol.SearchVariantsResponse
     builder = response_builder.SearchResponseBuilder(
         responseClass, 100, 2 ** 32)
     # If not set, pageToken should be empty string
     self.assertIsNone(builder.getNextPageToken())
     instance = protocol.fromJson(builder.getSerializedResponse(),
                                  responseClass)
     self.assertEqual(instance.next_page_token, "")
     # page tokens can be any string.
     for nextPageToken in ["", "string"]:
         builder.setNextPageToken(nextPageToken)
         self.assertEqual(nextPageToken, builder.getNextPageToken())
         instance = protocol.fromJson(builder.getSerializedResponse(),
                                      responseClass)
         self.assertEqual(nextPageToken, instance.next_page_token)
Пример #15
0
 def sendCallSetsSearch(self):
     response = self.sendVariantSetsSearch()
     variantSets = protocol.fromJson(
         response.data, protocol.SearchVariantSetsResponse).variant_sets
     request = protocol.SearchCallSetsRequest()
     request.variant_set_id = variantSets[0].id
     return self.sendPostRequest('/callsets/search', request)
Пример #16
0
 def testToJsonAndFromJson(self):
     classes = protocol.getProtocolClasses()
     for clazz in classes:
         obj = clazz()
         jsonStr = protocol.toJson(obj)
         obj2 = protocol.fromJson(jsonStr, clazz)
         self.assertTrue(obj, obj2)
Пример #17
0
    def testCompoundFeatureSearch(self):
        datasetName, featureSet = self.getCGDDataSetFeatureSet()
        featureId = \
            "http://cancer.sanger.ac.uk/cosmic/mutation/overview?id=736"
        obfuscated = self.getObfuscatedFeatureCompoundId(
            datasetName, featureSet.name, featureId)
        request = protocol.GetFeatureRequest
        request.feature_id = obfuscated
        response = self.sendGetRequest('/features/{}'.format(obfuscated))

        feature = protocol.fromJson(response.data, protocol.Feature)

        self.assertIsNotNone(feature)
        featureId = feature.id

        request = protocol.SearchGenotypePhenotypeRequest()
        request.phenotype_association_set_id = \
            self.getPhenotypeAssociationSetId()
        request.feature_ids.append(featureId)
        response = self.sendSearchRequest(
            '/featurephenotypeassociations/search',
            request,
            protocol.SearchGenotypePhenotypeResponse)
        self.assertEqual(1, len(response.associations))
        self.assertEqual(1, len(response.associations[0].feature_ids))
Пример #18
0
 def testToJsonAndFromJson(self):
     classes = protocol.getProtocolClasses()
     for clazz in classes:
         obj = clazz()
         jsonStr = protocol.toJson(obj)
         obj2 = protocol.fromJson(jsonStr, clazz)
         self.assertTrue(obj, obj2)
Пример #19
0
 def testNextPageToken(self):
     responseClass = protocol.SearchVariantsResponse
     builder = response_builder.SearchResponseBuilder(
         responseClass, 100, 2**32)
     # If not set, pageToken should be empty string
     self.assertIsNone(builder.getNextPageToken())
     instance = protocol.fromJson(builder.getSerializedResponse(),
                                  responseClass)
     self.assertEqual(instance.next_page_token, "")
     # page tokens can be any string.
     for nextPageToken in ["", "string"]:
         builder.setNextPageToken(nextPageToken)
         self.assertEqual(nextPageToken, builder.getNextPageToken())
         instance = protocol.fromJson(builder.getSerializedResponse(),
                                      responseClass)
         self.assertEqual(nextPageToken, instance.next_page_token)
Пример #20
0
 def testReadsSearch(self):
     response = self.sendReadsSearch(readGroupIds=[self.readGroupId],
                                     referenceId=self.referenceId)
     self.assertEqual(200, response.status_code)
     responseData = protocol.fromJson(response.data,
                                      protocol.SearchReadsResponse)
     self.assertEqual(len(responseData.alignments), 2)
     self.assertEqual(responseData.alignments[0].id, self.readAlignmentId)
Пример #21
0
 def searchObjectTest(
         self, responseMethod, responseClass, attributeName, objectId):
     response = responseMethod()
     self.assertEqual(200, response.status_code)
     responseData = protocol.fromJson(response.data, responseClass)
     responseList = getattr(responseData, attributeName)
     objectList = list(responseList)
     self.assertEqual(objectId, objectList[0].id)
Пример #22
0
 def searchObjectTest(self, responseMethod, responseClass, attributeName,
                      objectId):
     response = responseMethod()
     self.assertEqual(200, response.status_code)
     responseData = protocol.fromJson(response.data, responseClass)
     responseList = getattr(responseData, attributeName)
     objectList = list(responseList)
     self.assertEqual(objectId, objectList[0].id)
Пример #23
0
 def test_schemas_deserialization(self):
     # Mock search datasets request
     request = '{"pageSize": 3, "pageToken": "text"}'
     # We pass the class, not an instance of the class to `fromJson`
     request_schema = protocol.SearchDatasetsRequest
     deserialized_request = protocol.fromJson(request, request_schema)
     self.assertEqual(
         3, deserialized_request.page_size,
         "The deserialized version should also have 3 for page_size.")
Пример #24
0
 def testSimplePost(self):
     path = "/datasets/search"
     response = protocol.fromJson(self.app.post(
         path, headers={}).get_data(), protocol.SearchDatasetsResponse)
     self.assertIsNotNone(
         response.datasets,
         "When an empty JSON document "
         "without a mimetype is sent we can still"
         "get datasets.")
Пример #25
0
 def populateFromRow(self, readGroupRecord):
     """
     Populate the instance variables using the specified DB row.
     """
     self._sampleName = readGroupRecord.samplename
     self._biosampleId = readGroupRecord.biosampleid
     self._description = readGroupRecord.description
     self._predictedInsertSize = readGroupRecord.predictedinsertsize
     stats = protocol.fromJson(readGroupRecord.stats, protocol.ReadStats)
     self._numAlignedReads = stats.aligned_read_count
     self._numUnalignedReads = stats.unaligned_read_count
     experiment = protocol.fromJson(readGroupRecord.experiment,
                                    protocol.Experiment)
     self._instrumentModel = experiment.instrument_model
     self._sequencingCenter = experiment.sequencing_center
     self._experimentDescription = experiment.description
     self._library = experiment.library
     self._platformUnit = experiment.platform_unit
     self._runTime = experiment.run_time
Пример #26
0
 def testPhenotypeAssociationSetsSearch(self):
     response = self.sendPhenotypeAssociationSetsSearch()
     responseData = protocol.fromJson(
         response.data, protocol.SearchPhenotypeAssociationSetsResponse)
     pasets = list(responseData.phenotype_association_sets)
     foundPASet = False
     for paset in pasets:
         if self.phenotypeAssociationSetId == paset.id:
             foundPASet = True
     self.assertTrue(foundPASet)
Пример #27
0
 def testPhenotypeAssociationSetsSearch(self):
     response = self.sendPhenotypeAssociationSetsSearch()
     responseData = protocol.fromJson(
         response.data, protocol.SearchPhenotypeAssociationSetsResponse)
     pasets = list(responseData.phenotype_association_sets)
     foundPASet = False
     for paset in pasets:
         if self.phenotypeAssociationSetId == paset.id:
             foundPASet = True
     self.assertTrue(foundPASet)
Пример #28
0
 def testReadsSearch(self):
     response = self.sendReadsSearch(readGroupIds=[self.readGroupId],
                                     referenceId=self.referenceId)
     self.assertEqual(200, response.status_code)
     responseData = protocol.fromJson(
         response.data, protocol.SearchReadsResponse)
     self.assertEqual(len(responseData.alignments), 2)
     self.assertEqual(
         responseData.alignments[0].id,
         self.readAlignmentId)
Пример #29
0
 def toProtocolElement(self):
     species = None
     sex = None
     if self.getSpecies():
         species = protocol.fromJson(json.dumps(self.getSpecies()),
                                     protocol.OntologyTerm)
     if self.getSex():
         sex = protocol.fromJson(json.dumps(self.getSex()),
                                 protocol.OntologyTerm)
     gaIndividual = protocol.Individual(dataset_id=self._datasetId,
                                        created=self.getCreated(),
                                        updated=self.getUpdated(),
                                        description=self.getDescription(),
                                        id=self.getId(),
                                        name=self.getName(),
                                        species=species,
                                        sex=sex)
     self.serializeAttributes(gaIndividual)
     return gaIndividual
Пример #30
0
 def sendVariantsSearch(self):
     response = self.sendVariantSetsSearch()
     variantSets = protocol.fromJson(
         response.data, protocol.SearchVariantSetsResponse).variant_sets
     request = protocol.SearchVariantsRequest()
     request.variant_set_id = variantSets[0].id
     request.reference_name = "1"
     request.start = 0
     request.end = 1
     return self.sendPostRequest('/variants/search', request)
Пример #31
0
 def sendVariantsSearch(self):
     response = self.sendVariantSetsSearch()
     variantSets = protocol.fromJson(
         response.data, protocol.SearchVariantSetsResponse).variant_sets
     request = protocol.SearchVariantsRequest()
     request.variant_set_id = variantSets[0].id
     request.reference_name = "1"
     request.start = 0
     request.end = 1
     return self.sendPostRequest('/variants/search', request)
Пример #32
0
 def populateFromRow(self, readGroupRecord):
     """
     Populate the instance variables using the specified DB row.
     """
     self._sampleName = readGroupRecord.samplename
     self._biosampleId = readGroupRecord.biosampleid
     self._description = readGroupRecord.description
     self._predictedInsertSize = readGroupRecord.predictedinsertsize
     stats = protocol.fromJson(readGroupRecord.stats, protocol.ReadStats)
     self._numAlignedReads = stats.aligned_read_count
     self._numUnalignedReads = stats.unaligned_read_count
     experiment = protocol.fromJson(
         readGroupRecord.experiment, protocol.Experiment)
     self._instrumentModel = experiment.instrument_model
     self._sequencingCenter = experiment.sequencing_center
     self._experimentDescription = experiment.description
     self._library = experiment.library
     self._platformUnit = experiment.platform_unit
     self._runTime = experiment.run_time
Пример #33
0
 def sendSearchRequest(self, path, request, responseClass):
     """
     Sends the specified protocol request instance as JSON, and
     parses the result into an instance of the specified response.
     """
     response = self.sendJsonPostRequest(path, protocol.toJson(request))
     self.assertEqual(200, response.status_code)
     responseData = protocol.fromJson(response.data, responseClass)
     self.assertTrue(
         protocol.validate(protocol.toJson(responseData), responseClass))
     return responseData
Пример #34
0
 def toProtocolElement(self):
     species = None
     sex = None
     if self.getSpecies():
         species = protocol.fromJson(
             json.dumps(self.getSpecies()), protocol.OntologyTerm)
     if self.getSex():
         sex = protocol.fromJson(
             json.dumps(self.getSex()), protocol.OntologyTerm)
     gaIndividual = protocol.Individual(
         dataset_id=self._datasetId,
         created=self.getCreated(),
         updated=self.getUpdated(),
         description=self.getDescription(),
         id=self.getId(),
         name=self.getName(),
         species=species,
         sex=sex)
     self.serializeAttributes(gaIndividual)
     return gaIndividual
Пример #35
0
 def sendSearchRequest(self, path, request, responseClass):
     """
     Sends the specified protocol request instance as JSON, and
     parses the result into an instance of the specified response.
     """
     response = self.sendJsonPostRequest(path, protocol.toJson(request))
     self.assertEqual(200, response.status_code)
     responseData = protocol.fromJson(response.data, responseClass)
     self.assertTrue(
         protocol.validate(protocol.toJson(responseData), responseClass))
     return responseData
Пример #36
0
    def verifySearchRouting(self, path, getDefined=False):
        """
        Verifies that the specified path has the correct routing for a
        search command. If getDefined is False we check to see if it
        returns the correct status code.
        """
        if not getDefined:
            getResponse = self.app.get(path)
            protocol.fromJson(
                getResponse.get_data(), protocol.GAException)
            self.assertEqual(405, getResponse.status_code)

        # Malformed requests should return 400
        for badJson in ["JSON", "<xml/>", "{]"]:
            badResponse = self.app.post(
                path, data=badJson,
                headers={'Content-type': 'application/json'})
            self.assertEqual(400, badResponse.status_code)

        # OPTIONS should return success
        self.assertEqual(200, self.app.options(path).status_code)
Пример #37
0
 def testGetVariantSet(self):
     response = self.sendVariantSetsSearch()
     responseData = protocol.fromJson(
         response.data, protocol.SearchVariantSetsResponse)
     variantSetId = responseData.variant_sets[0].id
     response = self.sendGetVariantSet(variantSetId)
     self.assertEqual(200, response.status_code)
     invalidId = datamodel.VariantSetCompoundId.getInvalidIdString()
     obfuscated = datamodel.CompoundId.obfuscate(invalidId)
     compoundId = datamodel.VariantSetCompoundId.parse(obfuscated)
     response = self.sendGetVariantSet(str(compoundId))
     self.assertEqual(404, response.status_code)
Пример #38
0
 def toProtocolElement(self):
     disease = None
     if self.getDisease():
         disease = protocol.fromJson(
             json.dumps(self.getDisease()), protocol.OntologyTerm)
     individualAgeAtCollection = None
     if self.getIndividualAgeAtCollection():
         individualAgeAtCollection = protocol.fromJson(
             json.dumps(self.getIndividualAgeAtCollection()), protocol.Age)
     biosample = protocol.Biosample(
         dataset_id=self._datasetId,
         created=self.getCreated(),
         updated=self.getUpdated(),
         description=self.getDescription(),
         id=self.getId(),
         individual_id=self.getIndividualId(),
         name=self.getName(),
         disease=disease,
         individual_age_at_collection=individualAgeAtCollection)
     self.serializeAttributes(biosample)
     return biosample
Пример #39
0
    def setSpeciesFromJson(self, speciesJson):
        """
        Sets the species, an OntologyTerm, to the specified value, given as
        a JSON string.

        See the documentation for details of this field.
        """
        try:
            parsed = protocol.fromJson(speciesJson, protocol.OntologyTerm)
        except:
            raise exceptions.InvalidJsonException(speciesJson)
        self._species = protocol.toJsonDict(parsed)
Пример #40
0
    def setSpeciesFromJson(self, speciesJson):
        """
        Sets the species, an OntologyTerm, to the specified value, given as
        a JSON string.

        See the documentation for details of this field.
        """
        try:
            parsed = protocol.fromJson(speciesJson, protocol.OntologyTerm)
        except:
            raise exceptions.InvalidJsonException(speciesJson)
        self._species = protocol.toJsonDict(parsed)
Пример #41
0
 def toProtocolElement(self):
     disease = None
     if self.getDisease():
         disease = protocol.fromJson(json.dumps(self.getDisease()),
                                     protocol.OntologyTerm)
     individualAgeAtCollection = None
     if self.getIndividualAgeAtCollection():
         individualAgeAtCollection = protocol.fromJson(
             json.dumps(self.getIndividualAgeAtCollection()), protocol.Age)
     biosample = protocol.Biosample(
         dataset_id=self._datasetId,
         created=self.getCreated(),
         updated=self.getUpdated(),
         description=self.getDescription(),
         id=self.getId(),
         individual_id=self.getIndividualId(),
         name=self.getName(),
         disease=disease,
         individual_age_at_collection=individualAgeAtCollection)
     self.serializeAttributes(biosample)
     return biosample
Пример #42
0
 def testGetVariantSet(self):
     response = self.sendVariantSetsSearch()
     responseData = protocol.fromJson(response.data,
                                      protocol.SearchVariantSetsResponse)
     variantSetId = responseData.variant_sets[0].id
     response = self.sendGetVariantSet(variantSetId)
     self.assertEqual(200, response.status_code)
     invalidId = datamodel.VariantSetCompoundId.getInvalidIdString()
     obfuscated = datamodel.CompoundId.obfuscate(invalidId)
     compoundId = datamodel.VariantSetCompoundId.parse(obfuscated)
     response = self.sendGetVariantSet(str(compoundId))
     self.assertEqual(404, response.status_code)
Пример #43
0
    def runAddAnnouncement(self, flaskrequest):
        """
        Takes a flask request from the frontend and attempts to parse
        into an AnnouncePeerRequest. If successful, it will log the
        announcement to the `announcement` table with some other metadata
        gathered from the request.
        """
        announcement = {}
        # We want to parse the request ourselves to collect a little more
        # data about it.
        try:
            requestData = protocol.fromJson(
                flaskrequest.get_data(), protocol.AnnouncePeerRequest)
            announcement['hostname'] = flaskrequest.host_url
            announcement['remote_addr'] = flaskrequest.remote_addr
            announcement['user_agent'] = flaskrequest.headers.get('User-Agent')
        except AttributeError:
            # Sometimes in testing we will send protocol requests instead
            # of flask requests and so the hostname and user agent won't
            # be present.
            try:
                requestData = protocol.fromJson(
                    flaskrequest, protocol.AnnouncePeerRequest)
            except Exception as e:
                raise exceptions.InvalidJsonException(e)
        except Exception as e:
            raise exceptions.InvalidJsonException(e)

        # Validate the url before accepting the announcement
        peer = datamodel.peers.Peer(requestData.peer.url)
        peer.setAttributesJson(protocol.toJson(
                requestData.peer.attributes))
        announcement['url'] = peer.getUrl()
        announcement['attributes'] = peer.getAttributes()
        try:
            self.getDataRepository().insertAnnouncement(announcement)
        except:
            raise exceptions.BadRequestException(announcement['url'])
        return protocol.toJson(
            protocol.AnnouncePeerResponse(success=True))
Пример #44
0
    def runAddAnnouncement(self, flaskrequest):
        """
        Takes a flask request from the frontend and attempts to parse
        into an AnnouncePeerRequest. If successful, it will log the
        announcement to the `announcement` table with some other metadata
        gathered from the request.
        """
        announcement = {}
        # We want to parse the request ourselves to collect a little more
        # data about it.
        try:
            requestData = protocol.fromJson(
                flaskrequest.get_data(), protocol.AnnouncePeerRequest)
            announcement['hostname'] = flaskrequest.host_url
            announcement['remote_addr'] = flaskrequest.remote_addr
            announcement['user_agent'] = flaskrequest.headers.get('User-Agent')
        except AttributeError:
            # Sometimes in testing we will send protocol requests instead
            # of flask requests and so the hostname and user agent won't
            # be present.
            try:
                requestData = protocol.fromJson(
                    flaskrequest, protocol.AnnouncePeerRequest)
            except Exception as e:
                raise exceptions.InvalidJsonException(e)
        except Exception as e:
            raise exceptions.InvalidJsonException(e)

        # Validate the url before accepting the announcement
        peer = datamodel.peers.Peer(requestData.peer.url)
        peer.setAttributesJson(protocol.toJson(
                requestData.peer.attributes))
        announcement['url'] = peer.getUrl()
        announcement['attributes'] = peer.getAttributes()
        try:
            self.getDataRepository().insertAnnouncement(announcement)
        except:
            raise exceptions.BadRequestException(announcement['url'])
        return protocol.toJson(
            protocol.AnnouncePeerResponse(success=True))
Пример #45
0
 def testPageSizeExactFill(self):
     responseClass = protocol.SearchVariantsResponse
     valueClass = protocol.Variant
     for pageSize in range(1, 10):
         builder = response_builder.SearchResponseBuilder(
             responseClass, pageSize, 2**32)
         self.assertEqual(builder.getPageSize(), pageSize)
         while not builder.isFull():
             builder.addValue(valueClass())
         instance = protocol.fromJson(builder.getSerializedResponse(),
                                      responseClass)
         valueList = getattr(instance, getValueListName(responseClass))
         self.assertEqual(len(valueList), pageSize)
 def assertRawRequestRaises(self, exceptionClass, url, requestString):
     """
     Verifies that the specified request string returns a protocol
     exception corresponding to the specified class when applied to
     all POST endpoints.
     """
     response = self.app.post(url,
                              headers={'Content-type': 'application/json'},
                              data=requestString)
     self.assertEqual(response.status_code, exceptionClass.httpStatus)
     error = protocol.fromJson(response.data, protocol.GAException)
     self.assertEqual(error.error_code, exceptionClass.getErrorCode())
     self.assertGreater(len(error.message), 0)
Пример #47
0
 def testPageSizeExactFill(self):
     responseClass = protocol.SearchVariantsResponse
     valueClass = protocol.Variant
     for pageSize in range(1, 10):
         builder = response_builder.SearchResponseBuilder(
             responseClass, pageSize, 2 ** 32)
         self.assertEqual(builder.getPageSize(), pageSize)
         while not builder.isFull():
             builder.addValue(valueClass())
         instance = protocol.fromJson(builder.getSerializedResponse(),
                                      responseClass)
         valueList = getattr(instance, getValueListName(responseClass))
         self.assertEqual(len(valueList), pageSize)
Пример #48
0
 def assertRawRequestRaises(self, exceptionClass, url, requestString):
     """
     Verifies that the specified request string returns a protocol
     exception corresponding to the specified class when applied to
     all POST endpoints.
     """
     response = self.app.post(
         url, headers={'Content-type': 'application/json'},
         data=requestString)
     self.assertEqual(response.status_code, exceptionClass.httpStatus)
     error = protocol.fromJson(response.data, protocol.GAException)
     self.assertEqual(
         error.error_code, exceptionClass.getErrorCode())
     self.assertGreater(len(error.message), 0)
Пример #49
0
    def testGetDataset(self):
        # Test OK: ID found
        response = self.sendDatasetsSearch()
        responseData = protocol.fromJson(
            response.data, protocol.SearchDatasetsResponse)
        datasetId = responseData.datasets[0].id
        response = self.sendGetDataset(datasetId)
        self.assertEqual(200, response.status_code)

        # Test Error: 404, ID not found
        invalidId = datamodel.DatasetCompoundId.getInvalidIdString()
        obfuscated = datamodel.CompoundId.obfuscate(invalidId)
        compoundId = datamodel.DatasetCompoundId.parse(obfuscated)
        response = self.sendGetDataset(str(compoundId))
        self.assertEqual(404, response.status_code)
Пример #50
0
 def getPhenotypeAssociationSetId(self):
     """
     Gets the dataset phenotype association set ID
     """
     request = protocol.SearchDatasetsRequest()
     response = self.sendSearchRequest("datasets/search", request,
                                       protocol.SearchDatasetsResponse)
     datasetId = response.datasets[0].id
     request = protocol.SearchPhenotypeAssociationSetsRequest()
     request.dataset_id = datasetId
     response = self.sendPostRequest("phenotypeassociationsets/search",
                                     request)
     response = protocol.fromJson(
         response.data, protocol.SearchPhenotypeAssociationSetsResponse)
     return response.phenotype_association_sets[0].id
Пример #51
0
    def testGetDataset(self):
        # Test OK: ID found
        response = self.sendDatasetsSearch()
        responseData = protocol.fromJson(response.data,
                                         protocol.SearchDatasetsResponse)
        datasetId = responseData.datasets[0].id
        response = self.sendGetDataset(datasetId)
        self.assertEqual(200, response.status_code)

        # Test Error: 404, ID not found
        invalidId = datamodel.DatasetCompoundId.getInvalidIdString()
        obfuscated = datamodel.CompoundId.obfuscate(invalidId)
        compoundId = datamodel.DatasetCompoundId.parse(obfuscated)
        response = self.sendGetDataset(str(compoundId))
        self.assertEqual(404, response.status_code)
Пример #52
0
    def verifySearchRouting(self, path, getDefined=False):
        """
        Verifies that the specified path has the correct routing for a
        search command. If getDefined is False we check to see if it
        returns the correct status code.
        """
        response = self.app.post(path)
        protocol.fromJson(response.get_data(), protocol.GAException)
        self.assertEqual(415, response.status_code)
        if not getDefined:
            getResponse = self.app.get(path)
            protocol.fromJson(getResponse.get_data(), protocol.GAException)
            self.assertEqual(405, getResponse.status_code)

        # Malformed requests should return 400
        for badJson in ["", None, "JSON", "<xml/>", "{]"]:
            badResponse = self.app.post(
                path,
                data=badJson,
                headers={'Content-type': 'application/json'})
            self.assertEqual(400, badResponse.status_code)

        # OPTIONS should return success
        self.assertEqual(200, self.app.options(path).status_code)
Пример #53
0
 def testIntegrity(self):
     # Verifies that the values we put in are exactly what we get
     # back across all subclasses of SearchResponse
     for class_ in [responseClass for _, _, responseClass in
                    protocol.postMethods]:
         instance = class_()
         valueList = getattr(instance, getValueListName(class_))
         valueList.add()
         builder = response_builder.SearchResponseBuilder(
             class_, len(valueList), 2 ** 32)
         for value in valueList:
             builder.addValue(value)
         builder.setNextPageToken(instance.next_page_token)
         otherInstance = protocol.fromJson(
             builder.getSerializedResponse(), class_)
         self.assertEqual(instance, otherInstance)
Пример #54
0
 def testRoundTripDataset(self):
     id_ = "id"
     name = "name"
     description = "description"
     dataset = protocol.Dataset()
     dataset.id = id_
     dataset.name = name
     dataset.description = description
     jsonStr = protocol.toJson(dataset)
     newDataset = protocol.fromJson(jsonStr, Dataset)
     self.assertEquals(dataset.id, id_)
     self.assertEquals(dataset.name, name)
     self.assertEquals(dataset.description, description)
     datasetDict = protocol.toJsonDict(newDataset)
     self.assertEquals(datasetDict['id'], id_)
     self.assertEquals(datasetDict['name'], name)
     self.assertEquals(datasetDict['description'], description)
Пример #55
0
 def populateFromJson(self, jsonString):
     # TODO validate
     try:
         parsed = protocol.fromJson(jsonString, protocol.Individual)
     except:
         raise exceptions.InvalidJsonException(jsonString)
     self._created = parsed.created
     self._updated = parsed.updated
     self._description = parsed.description
     self._species = protocol.toJsonDict(parsed.species)
     self._sex = protocol.toJsonDict(parsed.sex)
     attributes = {}
     for key in parsed.attributes.attr:
         attributes[key] = {
             "values": protocol.toJsonDict(parsed.attributes.attr[key])}
     self.setAttributes(attributes)
     return self
Пример #56
0
 def getPhenotypeAssociationSetId(self):
     """
     Gets the dataset phenotype association set ID
     """
     request = protocol.SearchDatasetsRequest()
     response = self.sendSearchRequest(
         "datasets/search",
         request,
         protocol.SearchDatasetsResponse)
     datasetId = response.datasets[0].id
     request = protocol.SearchPhenotypeAssociationSetsRequest()
     request.dataset_id = datasetId
     response = self.sendPostRequest(
         "phenotypeassociationsets/search", request)
     response = protocol.fromJson(
         response.data, protocol.SearchPhenotypeAssociationSetsResponse)
     return response.phenotype_association_sets[0].id
Пример #57
0
 def testIntegrity(self):
     # Verifies that the values we put in are exactly what we get
     # back across all subclasses of SearchResponse
     for class_ in [
             responseClass for _, _, responseClass in protocol.postMethods
     ]:
         instance = class_()
         valueList = getattr(instance, getValueListName(class_))
         valueList.add()
         builder = response_builder.SearchResponseBuilder(
             class_, len(valueList), 2**32)
         for value in valueList:
             builder.addValue(value)
         builder.setNextPageToken(instance.next_page_token)
         otherInstance = protocol.fromJson(builder.getSerializedResponse(),
                                           class_)
         self.assertEqual(instance, otherInstance)
Пример #58
0
 def populateFromJson(self, jsonString):
     try:
         parsed = protocol.fromJson(jsonString, protocol.Biosample)
     except:
         raise exceptions.InvalidJsonException(jsonString)
     self._created = parsed.created
     self._updated = parsed.updated
     self._description = parsed.description
     self._disease = protocol.toJsonDict(parsed.disease)
     self._individualId = parsed.individual_id
     self._individualAgeAtCollection = protocol.toJsonDict(
                                        parsed.individual_age_at_collection)
     attributes = {}
     for key in parsed.attributes.attr:
         attributes[key] = {
             "values": protocol.toJsonDict(parsed.attributes.attr[key])}
     self.setAttributes(attributes)
     return self
Пример #59
0
    def testFeaturesSearchById(self):
        datasetName, featureSet = self.getCGDDataSetFeatureSet()
        featureId = \
            "http://cancer.sanger.ac.uk/cosmic/mutation/overview?id=965"
        obfuscated = self.getObfuscatedFeatureCompoundId(
            datasetName, featureSet.name, featureId)
        request = protocol.GetFeatureRequest
        request.feature_id = obfuscated
        response = self.sendGetRequest(
            '/features/{}'.format(obfuscated))

        feature = protocol.fromJson(response.data, protocol.Feature)
        self.assertIsNotNone(feature)
        self.assertEqual(request.feature_id, feature.id)
        self.assertIsNotNone(feature.feature_type)
        self.assertIsNotNone(feature.feature_type.term_id)
        self.assertEqual(feature.reference_name,  "chr10")
        self.assertEqual(feature.start,  43617416)
        self.assertEqual(feature.end,  43617416)
Пример #60
0
    def runListReferenceBases(self, requestJson):
        """
        Runs a listReferenceBases request for the specified ID and
        request arguments.
        """
        # In the case when an empty post request is made to the endpoint
        # we instantiate an empty ListReferenceBasesRequest.
        if not requestJson:
            request = protocol.ListReferenceBasesRequest()
        else:
            try:
                request = protocol.fromJson(
                    requestJson,
                    protocol.ListReferenceBasesRequest)
            except protocol.json_format.ParseError:
                raise exceptions.InvalidJsonException(requestJson)
        compoundId = datamodel.ReferenceCompoundId.parse(request.reference_id)
        referenceSet = self.getDataRepository().getReferenceSet(
            compoundId.reference_set_id)
        reference = referenceSet.getReference(request.reference_id)
        start = request.start
        end = request.end
        if end == 0:  # assume meant "get all"
            end = reference.getLength()
        if request.page_token:
            pageTokenStr = request.page_token
            start = paging._parsePageToken(pageTokenStr, 1)[0]

        chunkSize = self._maxResponseLength
        nextPageToken = None
        if start + chunkSize < end:
            end = start + chunkSize
            nextPageToken = str(start + chunkSize)
        sequence = reference.getBases(start, end)

        # build response
        response = protocol.ListReferenceBasesResponse()
        response.offset = start
        response.sequence = sequence
        if nextPageToken:
            response.next_page_token = nextPageToken
        return protocol.toJson(response)