def toProtocolElement(self, tier=0): 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, estimated_tumor_content=self.getEstimatedTumorContent(), normal_sample_source=self.getNormalSampleSource(), biopsy_data=self.getBiopsyData(), tumor_biopsy_anatomical_site=self.getTumorBiopsyAnatomicalSite(), biopsy_type=self.getBiopsyType(), sample_shipment_date=self.getSampleShipmentDate(), ) self.serializeAttributes(biosample) return biosample
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)
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) self._biosample_id = parsed.biosample_id self._dna_library_construction_method = parsed.dna_library_construction_method self._wgs_sequencing_completion_date = parsed.wgs_sequencing_completion_date self._rna_library_construction_method = parsed.rna_library_construction_method self._rna_sequencing_completion_date = parsed.rna_sequencing_completion_date self._panel_completion_date = parsed.panel_completion_date return self
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)
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) self._patient_id = parsed.patient_id self._regional_profiling_centre = parsed.regional_profiling_centre self._diagnosis = json.dumps(protocol.toJsonDict(parsed.diagnosis)) self._pathology_type = parsed.pathology_type self._enrollment_approval_date = parsed.enrollment_approval_date self._enrollment_approval_initials = parsed.enrollment_approval_initials self._date_of_upload_to_sFTP = parsed.date_of_upload_to_sFTP self._tumor_board_presentation_date_and_analyses = parsed.tumor_board_presentation_date_and_analyses self._comments = parsed.comments return self
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) self._estimated_tumor_content = parsed.estimated_tumor_content self._normal_sample_source = parsed.normal_sample_source self._biopsy_data = parsed.biopsy_data self._tumor_biopsy_anatomical_site = parsed.tumor_biopsy_anatomical_site self._biopsy_type = parsed.biopsy_type self._sample_shipment_date = parsed.sample_shipment_date return self
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 self._patientId = readGroupSetRecord.patientId self._sampleId = readGroupSetRecord.sampleId
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
def toProtocolElement(self, tier=0): dataset = protocol.Dataset() dataset.id = self.getId() dataset.name = pb.string(self.getLocalId()) dataset.description = pb.string(self.getDescription()) # Populate DUO info by extending the list dataset.terms_of_use.extend([ protocol.fromJson(json.dumps(p), protocol.OntologyTerm) for p in self.getInfo() ]) self.serializeAttributes(dataset) return dataset
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)
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)
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 toProtocolElement(self, tier=0): 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) if self.getDiagnosis(): diagnosis = protocol.fromJson(json.dumps(self.getDiagnosis()), protocol.OntologyTerm) else: diagnosis = None 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, patient_id=self.getPatientId(), regional_profiling_centre=self.getRegionalProfilingCentre(), diagnosis=diagnosis, pathology_type=self.getPathologyType(), enrollment_approval_date=self.getEnrollmentApprovalDate(), enrollment_approval_initials=self.getEnrollmentApprovalInitials(), date_of_upload_to_sFTP=self.getDateOfUploadToSftp(), tumor_board_presentation_date_and_analyses=self. getTumorBoardPresentationDateAndAnalyses(), comments=self.getComments(), ) self.serializeAttributes(gaIndividual) return gaIndividual
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)
def testRoundTripDatasetJson(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.assertEqual(dataset.id, id_) self.assertEqual(dataset.name, name) self.assertEqual(dataset.description, description) datasetDict = protocol.toJsonDict(newDataset) self.assertEqual(datasetDict['id'], id_) self.assertEqual(datasetDict['name'], name) self.assertEqual(datasetDict['description'], description)
def testMaxBufferSizeOverridesPageSize(self): responseClass = protocol.SearchVariantsResponse typicalValue = protocol.Variant() # We have to put some values in here or it will have zero length. typicalValue.start = 1 typicalValue.end = 2 typicalValue.reference_bases = "AAAAAAAA" typicalValueLength = typicalValue.ByteSize() for numValues in range(1, 10): maxBufferSize = numValues * typicalValueLength builder = response_builder.SearchResponseBuilder( responseClass, 1000, maxBufferSize) self.assertEqual( maxBufferSize, builder.getMaxBufferSize()) while not builder.isFull(): builder.addValue(typicalValue) instance = protocol.fromJson(builder.getSerializedResponse(), responseClass) valueList = getattr(instance, getValueListName(responseClass)) self.assertEqual(len(valueList), numValues)
def toProtocolElement(self, tier=0): """ Returns the GA4GH protocol representation of this Reference. """ reference = protocol.Reference() reference.id = self.getId() reference.is_derived = self.getIsDerived() reference.length = self.getLength() reference.md5checksum = self.getMd5Checksum() reference.name = self.getName() if self.getSpecies(): term = protocol.fromJson( json.dumps(self.getSpecies()), protocol.OntologyTerm) reference.species.term_id = term.term_id reference.species.term = term.term reference.source_accessions.extend(self.getSourceAccessions()) reference.source_divergence = pb.int(self.getSourceDivergence()) reference.source_uri = self.getSourceUri() self.serializeAttributes(reference) return reference
def toProtocolElement(self, tier=0): """ Returns the GA4GH protocol representation of this ReferenceSet. """ ret = protocol.ReferenceSet() ret.assembly_id = pb.string(self.getAssemblyId()) ret.description = pb.string(self.getDescription()) ret.id = self.getId() ret.is_derived = self.getIsDerived() ret.md5checksum = self.getMd5Checksum() if self.getSpecies(): term = protocol.fromJson( json.dumps(self.getSpecies()), protocol.OntologyTerm) ret.species.term_id = term.term_id ret.species.term = term.term ret.source_accessions.extend(self.getSourceAccessions()) ret.source_uri = pb.string(self.getSourceUri()) ret.name = self.getLocalId() self.serializeAttributes(ret) return ret
def testPageSizeOverflow(self): # Verifies that the page size behaviour is correct when we keep # filling after full is True. 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) self.assertFalse(builder.isFull()) for listLength in range(1, 2 * pageSize): builder.addValue(valueClass()) instance = protocol.fromJson( builder.getSerializedResponse(), responseClass) valueList = getattr(instance, getValueListName(responseClass)) self.assertEqual(len(valueList), listLength) if listLength < pageSize: self.assertFalse(builder.isFull()) else: self.assertTrue(builder.isFull())
def populateFromJson(self, jsonString): try: parsed = protocol.fromJson(jsonString, protocol.Analysis) except: raise exceptions.InvalidJsonException(jsonString) if parsed.created: self._created = parsed.created if parsed.updated: self._updated = parsed.updated self._description = parsed.description # if parsed.name: # self._name = parsed.name self._type = parsed.type self._software = parsed.software attributes = {} for key in parsed.attributes.attr: attributes[key] = { "values": protocol.toJsonDict(parsed.attributes.attr[key]) } self.setAttributes(attributes) self._experiment_id = parsed.experiment_id self._other_analysis_descriptor = parsed.other_analysis_descriptor self._other_analysis_completition_date = parsed.other_analysis_completition_date return self
def getGa4ghException(self, data): return protocol.fromJson(data, protocol.GAException)
def getInvalidFields(self, jsonDict): # FIXME: get the proper list of fields protocol.fromJson(json.dumps(jsonDict), self.class_) return []