def test_import_cellosaurus(self, mock_api_call): """The cellosaurus ontology is not part of the OLS so we need to handle it separately. We still include it because metaSRA uses its terms. NOTE: the actual cellosaurus ontology is massive, so this VCR was created using a trimmed-down cellosaurus ontology where I went in and deleted a bunch of the publications and cell lines from the respective lists. Besides this, no alterations were made to the original.""" # We shouldn't be hitting the API at all here, because we should have # the ontology already imported mock_api_call.return_value = "The wrong answer" created_terms = OntologyTerm.import_entire_ontology("cvcl") self.assertEqual( OntologyTerm.objects.all().count(), 34, # This is the number I counted in the file ) self.assertEqual(OntologyTerm.objects.all().count(), created_terms) self.assertEqual( "#W7079", OntologyTerm.get_or_create_from_api( "CVCL:E549").human_readable_name, ) mock_api_call.assert_not_called()
def test_import_entire_ontology(self, mock_api_call): # We shouldn't be hitting the API at all here, because we should have # the ontology already imported mock_api_call.return_value = "The wrong answer" # I used the UO ontology here because it is much smaller than other important # ontologies like EFO, which could take upwards of a minute to download and parse OntologyTerm.import_entire_ontology("uo") self.assertGreater(OntologyTerm.objects.all().count(), 0) self.assertEqual( "month", OntologyTerm.get_or_create_from_api( "UO:0000035").human_readable_name) mock_api_call.assert_not_called()
def test_get_or_create_from_api_returns_cached(self, mock_api_call): mock_api_call.return_value = "medulloblastoma" self.assertEqual( "medulloblastoma", OntologyTerm.get_or_create_from_api( "EFO:0002939").human_readable_name, ) mock_api_call.assert_called_once_with("EFO:0002939") mock_api_call.reset_mock() self.assertEqual( "medulloblastoma", OntologyTerm.get_or_create_from_api( "EFO:0002939").human_readable_name, ) mock_api_call.assert_not_called()
def set_value(self, value): """This method sets the attribute value and assigns the correct value_type. NOTE: we assume that all provided strings are ontology terms.""" if type(value) == str: self.value_type = ONTOLOGY_TERM # make sure that we know how to deal with this ontology term, and error out if we don't OntologyTerm.get_or_create_from_api(value) elif type(value) == bool: self.value_type = BOOL elif type(value) == int: self.value_type = INT elif type(value) == float: self.value_type = FLOAT else: raise ValueError("Invalid metadata value type '{}'".format(type(value))) self.value = str(value)
def test_import_cl(self, mock_api_call): """Try importing the CL ontology, which was updated at some point and broke the original parsing code""" # We shouldn't be hitting the API at all here, because we should have # the ontology already imported mock_api_call.return_value = "The wrong answer" created_terms = OntologyTerm.import_entire_ontology("cl") self.assertEqual( OntologyTerm.objects.all().count(), 2493, # Since we are using a VCR, this number should not change until we refresh it ) self.assertEqual(OntologyTerm.objects.all().count(), created_terms) self.assertEqual( "hematopoietic cell", OntologyTerm.get_or_create_from_api( "CL:0000988").human_readable_name, ) mock_api_call.assert_not_called()
def get_value(self): """This method returns the value of this attribute using `value_type` to convert to the same type""" if self.value_type == ONTOLOGY_TERM: return OntologyTerm.get_or_create_from_api(self.value) elif self.value_type == BOOL: return bool(self.value) elif self.value_type == INT: return int(self.value) elif self.value_type == FLOAT: return float(self.value) else: raise ValueError("Invalid value_type '{}'".format(self.value_type))
def test_get_or_create_from_api_fetches(self): self.assertEqual( "month", OntologyTerm.get_or_create_from_api( "UO:0000035").human_readable_name)
def test_get_or_create_from_cellosaurus_api(self): self.assertEqual( "LNCaP", OntologyTerm.get_or_create_from_api( "CVCL:0395").human_readable_name)