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 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 sendListRequest(self, path, request): headers = { 'Origin': self.exampleUrl, } data = protocol.toJsonDict(request) response = self.app.post(path, data=data, headers=headers) return response
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 testToProtocolElement(self): dataset = datasets.Dataset('dataset1') term = protocol.OntologyTerm() term.term = "male genotypic sex" term.term_id = "PATO:0020001" # Write out a valid input print(protocol.toJsonDict(term)) validIndividual = protocol.Individual( name="test", created="2016-05-19T21:00:19Z", updated="2016-05-19T21:00:19Z", sex=term) validIndividual.attributes.attr['test']. \ values.add().string_value = 'test-info' # pass through protocol creation individual = bioMetadata.Individual( dataset, "test") individual.populateFromJson(protocol.toJson(validIndividual)) gaIndividual = individual.toProtocolElement() # Verify elements exist self.assertEqual(gaIndividual.created, validIndividual.created) self.assertEqual(gaIndividual.updated, validIndividual.updated) # Invalid input invalidIndividual = '{"bad:", "json"}' individual = bioMetadata.Individual(dataset, "test") # Should fail self.assertRaises( exceptions.InvalidJsonException, individual.populateFromJson, invalidIndividual)
def verifyParsedOutputsEqualReadGroup(self, clientIterator, cliCommand, cliArguments=""): cliOutput = self.captureJsonOutput(cliCommand, cliArguments) clientOutput = [protocol.toJsonDict(gObj) for gObj in clientIterator] self.assertEqual(clientOutput, cliOutput) return len(clientOutput)
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 _formatExternalIdentifiers(self, element, element_type): """ Formats several external identifiers for query """ elementClause = None elements = [] if not issubclass(element.__class__, dict): element = protocol.toJsonDict(element) if element['externalIdentifiers']: for _id in element['externalIdentifiers']: elements.append(self._formatExternalIdentifier( _id, element_type)) elementClause = "({})".format(" || ".join(elements)) return elementClause
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 verifyParsedOutputsEqual(self, clientIterator, cliCommand, cliArguments="", scrubFunc=None): """ Verify that the parsed JSON of all the objects in the specified client iterator are equal to the parsed JSON from the specified CLI command. """ cliOutput = self.captureJsonOutput(cliCommand, cliArguments) clientOutput = [protocol.toJsonDict(gObj) for gObj in clientIterator] if scrubFunc is not None: # this is a hack to deal with the issue of the timestamps on # objects being different scrubFunc(cliOutput, clientOutput) self.assertEqual(clientOutput, cliOutput) return len(clientOutput)
def testInstantiation(self): for class_ in self._getExceptionClasses(): # some exceptions are becoming too complicated to instantiate # like the rest of the exceptions; just do them manually if class_ == exceptions.RequestValidationFailureException: objClass = protocol.SearchReadsRequest obj = objClass() obj.start = -1 jsonDict = protocol.toJsonDict(obj) args = (jsonDict, objClass) else: numInitArgs = len(inspect.getargspec(class_.__init__).args) - 1 args = ['arg' for _ in range(numInitArgs)] instance = class_(*args) self.assertIsInstance(instance, exceptions.BaseServerException) message = instance.getMessage() self.assertIsInstance(message, str) self.assertGreater(len(message), 0) self.assertEqual(instance.getErrorCode(), class_.getErrorCode())
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 assertAlignmentsEqual(self, gaAlignment, pysamAlignment, readGroupInfo): if pysamAlignment.query_qualities is None: self.assertEqual(gaAlignment.aligned_quality, []) else: self.assertEqual(gaAlignment.aligned_quality, list(pysamAlignment.query_qualities)) self.assertEqual(gaAlignment.aligned_sequence, pysamAlignment.query_sequence) if reads.SamFlags.isFlagSet(pysamAlignment.flag, reads.SamFlags.READ_UNMAPPED): self.assertEqual(0, gaAlignment.alignment.ByteSize()) else: self.assertEqual(gaAlignment.alignment.mapping_quality, pysamAlignment.mapping_quality) self.assertEqual( gaAlignment.alignment.position.reference_name, readGroupInfo.samFile.getrname(pysamAlignment.reference_id)) self.assertEqual(gaAlignment.alignment.position.position, pysamAlignment.reference_start) # TODO test reverseStrand on position and on # nextMatePosition once it has been implemented. self.assertCigarEqual(gaAlignment.alignment.cigar, pysamAlignment.cigar) self.assertFlag(gaAlignment.duplicate_fragment, pysamAlignment, reads.SamFlags.DUPLICATE_READ) self.assertFlag(gaAlignment.failed_vendor_quality_checks, pysamAlignment, reads.SamFlags.FAILED_QUALITY_CHECK) self.assertEqual(gaAlignment.fragment_length, pysamAlignment.template_length) self.assertEqual(gaAlignment.fragment_name, pysamAlignment.query_name) compoundId = datamodel.ReadAlignmentCompoundId( self._gaObject.getCompoundId(), pysamAlignment.query_name) self.assertEqual(gaAlignment.id, str(compoundId)) ret = protocol.ReadAlignment() for key, value in pysamAlignment.tags: protocol.setAttribute(ret.attributes.attr[key].values, value) self.assertEqual(protocol.toJsonDict(gaAlignment.attributes), protocol.toJsonDict(ret.attributes)) if reads.SamFlags.isFlagSet(pysamAlignment.flag, reads.SamFlags.MATE_UNMAPPED): self.assertEqual(0, gaAlignment.next_mate_position.ByteSize()) else: self.assertEqual(gaAlignment.next_mate_position.position, pysamAlignment.next_reference_start) if pysamAlignment.next_reference_id != -1: self.assertEqual( gaAlignment.next_mate_position.reference_name, readGroupInfo.samFile.getrname( pysamAlignment.next_reference_id)) else: self.assertEqual(gaAlignment.next_mate_position.reference_name, "") if gaAlignment.number_reads == 1: self.assertFlag(False, pysamAlignment, reads.SamFlags.READ_PAIRED) elif gaAlignment.number_reads == 2: self.assertFlag(True, pysamAlignment, reads.SamFlags.READ_PAIRED) else: # we shouldn't be setting numberReads to anything else self.assertTrue(False) if gaAlignment.read_number is -1: self.assertFlag(False, pysamAlignment, reads.SamFlags.FIRST_IN_PAIR) self.assertFlag(False, pysamAlignment, reads.SamFlags.SECOND_IN_PAIR) elif gaAlignment.read_number == 0: self.assertFlag(True, pysamAlignment, reads.SamFlags.FIRST_IN_PAIR) self.assertFlag(False, pysamAlignment, reads.SamFlags.SECOND_IN_PAIR) elif gaAlignment.read_number == 1: self.assertFlag(False, pysamAlignment, reads.SamFlags.FIRST_IN_PAIR) self.assertFlag(True, pysamAlignment, reads.SamFlags.SECOND_IN_PAIR) elif gaAlignment.read_number == 2: self.assertFlag(True, pysamAlignment, reads.SamFlags.FIRST_IN_PAIR) self.assertFlag(True, pysamAlignment, reads.SamFlags.SECOND_IN_PAIR) else: # we shouldn't be setting readNumber to anything else self.assertTrue(False) self.assertFlag(not gaAlignment.improper_placement, pysamAlignment, reads.SamFlags.READ_PROPER_PAIR) self.assertEqual(gaAlignment.read_group_id, readGroupInfo.id) self.assertFlag(gaAlignment.secondary_alignment, pysamAlignment, reads.SamFlags.SECONDARY_ALIGNMENT) self.assertFlag(gaAlignment.supplementary_alignment, pysamAlignment, reads.SamFlags.SUPPLEMENTARY_ALIGNMENT)
def testToGA4GH(self): sample_associations = { 'environment_label': 'sunitinib', 'feature_label': 'RET M918T missense mutation', 'evidence_type': 'http://purl.obolibrary.org/obo/ECO_0000033', 'feature': { 'http://purl.obolibrary.org/obo/GENO_0000408': 'http://www.ncbi.nlm.nih.gov/gene/5979', 'http://purl.obolibrary.org/obo/GENO_reference_amino_acid': 'M', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type': 'http://purl.obolibrary.org/obo/SO_0001059', 'http://biohackathon.org/resource/faldo#location': 'http://www.monarchinitiative.org/_918918UniProtKB:' 'P07949#P07949-1Region', 'http://purl.obolibrary.org/obo/GENO_reference_nucleotide': 'T', 'http://purl.obolibrary.org/obo/' 'GENO_results_in_amino_acid_change': 'T', 'http://purl.obolibrary.org/obo/RO_0002200': 'http://ohsu.edu/cgd/3774b1d2', 'http://purl.obolibrary.org/obo/RO_0002205': 'http://www.ncbi.nlm.nih.gov/CCDS/CcdsBrowse.cgi?' 'REQUEST=CCDS&DATA=7200.1', 'http://purl.obolibrary.org/obo/GENO_altered_nucleotide': 'C', 'http://www.w3.org/2000/01/rdf-schema#label': 'RET M918T missense mutation', 'id': 'http://cancer.sanger.ac.uk/cosmic/mutation/' 'overview?id=965', 'http://www.w3.org/2002/07/owl#sameAs': 'http://www.ncbi.nlm.nih.gov/SNP/74799832', }, 'evidence': 'http://ohsu.edu/cgd/sensitivity', 'environment': { 'http://purl.obolibrary.org/obo/RO_0002606': 'http://ohsu.edu/cgd/71fe9f0f', 'http://www.w3.org/2000/01/rdf-schema#subClassOf': 'http://purl.obolibrary.org/obo/CHEBI_23888', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type': 'http://www.w3.org/2002/07/owl#Class', 'http://www.w3.org/2000/01/rdf-schema#label': 'sunitinib', 'id': 'http://www.drugbank.ca/drugs/DB01268', }, 'sources': 'http://www.ncbi.nlm.nih.gov/pubmed/21470995|' 'http://www.ncbi.nlm.nih.gov/pubmed/21470995', 'phenotype': { 'http://purl.obolibrary.org/obo/BFO_0000159': 'http://ohsu.edu/cgd/sensitivity', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type': 'http://purl.obolibrary.org/obo/DOID_3969', 'http://www.w3.org/2000/01/rdf-schema#label': 'Papillary thyroid carcinoma with sensitivity to therapy', 'id': 'http://ohsu.edu/cgd/30ebfd1a', }, 'phenotype_label': 'Papillary thyroid carcinoma with sensitivity to therapy', 'id': 'http://ohsu.edu/cgd/fe484b5c', 'association': 'http://ohsu.edu/cgd/fe484b5c', } result = self.phenotypeAssocationSet._toGA4GH(sample_associations) self.assertEqual(result.__class__.__name__, 'FeaturePhenotypeAssociation') fpa_dict = protocol.toJsonDict(result) description = 'Association: genotype:[RET M918T missense mutation]' \ ' phenotype:[Papillary thyroid carcinoma with ' \ 'sensitivity to therapy] environment:[sunitinib]' \ ' evidence:[sensitivity] publications:' \ '[http://www.ncbi.nlm.nih.gov/pubmed/21470995|' \ 'http://www.ncbi.nlm.nih.gov/pubmed/21470995]' self.assertEqual(fpa_dict['description'], description) self.assertIn('featureIds', list(fpa_dict.keys())) self.assertIn('evidence', list(fpa_dict.keys())) self.assertIn('environmentalContexts', list(fpa_dict.keys())) self.assertEqual(len(fpa_dict['featureIds']), 1) self.assertEqual(len(fpa_dict['evidence']), 1) self.assertEqual(len(fpa_dict['environmentalContexts']), 1)
def testToJsonDict(self): classes = protocol.getProtocolClasses() for clazz in classes: obj = clazz() jsonDict = protocol.toJsonDict(obj) self.assertIsInstance(jsonDict, dict)