def testDatasetAttributes(self): """ Demonstrates the usage of the Attribute's field on a dataset message. This field is available on many other messages as well. """ key = "numbers_and_strings" string_value = "hello" int32_value = 5 double_value = 3.14159 attributes = {key: [string_value, int32_value, double_value]} dataset = metadata_pb2.Dataset() myattribute = dataset.attributes.attr[key].values myattribute.add().string_value = string_value myattribute.add().int32_value = int32_value myattribute.add().double_value = double_value self.assertEqual(dataset.attributes.attr[key].values[0].string_value, attributes[key][0]) self.assertEqual(dataset.attributes.attr[key].values[1].int32_value, attributes[key][1]) self.assertEqual(dataset.attributes.attr[key].values[2].double_value, attributes[key][2])
def search_datasets(request): """/datasets/search method request handler""" if not request.body: page_size = DEFAULT_PAGE_SIZE page_token = '0' else: try: request_dict = json_format.Parse( request.body, metadata_service.SearchDatasetsRequest()) except Exception as e: return HttpResponseBadRequest(json.dumps( {"message": e.message.replace("\"", "'")}), content_type='application/json') page_size = request_dict.page_size page_token = request_dict.page_token if not page_size or page_size == 0: page_size = DEFAULT_PAGE_SIZE if not page_token: page_token = '0' response = metadata_service.SearchDatasetsResponse() dataset = metadata.Dataset() dataset.name = SETNAME dataset.id = DATASET_ID dataset.description = 'Variants observed in brca-exchange project' # TODO uncomment when ga4gh client implements info field otherwise hardcoded values are placed #dataset.info[SETNAME].append("This set contains variants as stored and mantained by the brca-exchange project") list_of_sets = [] list_of_sets.append(dataset) sets = ga4gh_brca_page(list_of_sets, int(page_size), int(page_token)) response.next_page_token if len(sets) > page_size: page_token = str(1 + int(page_token)) response.next_page_token = page_token ############## # TODO Block gets fixed when ga4gh client implements # info field otherwise hardcoded values are placed if sets: response.datasets.extend(sets) else: response.next_page_token = ' ' response.datasets.extend([metadata.Dataset()]) ############## return JsonResponse(json_format.MessageToDict(response, False))
def get_dataset(request, dataset_id): """/datasets/<dataset id> get dataset via id method""" if not dataset_id: return HttpResponseBadRequest(json.dumps(ErrorMessages['datasetId']), content_type='application/json') response = metadata_service.GetDatasetRequest() dataset = metadata.Dataset() response.dataset_id = dataset_id dataset.id = DATASET_ID dataset.name = SETNAME dataset.description = 'Variants observed in brca-exchange project' # Needs field for info, still not available from ga4gh client return JsonResponse(json_format.MessageToDict(dataset, False))
def testTypedAttributes(self): """ Demonstrates how to use other types defined in common to create an attribute message. """ dataset = metadata_pb2.Dataset() ontologyTerm = common_pb2.OntologyTerm() ontologyTerm.term_id = "test" ontologyKey = "my_ontology_term" dataset.attributes.attr[ontologyKey] \ .values.add().ontology_term.MergeFrom(ontologyTerm) term = dataset.attributes.attr[ontologyKey] self.assertEqual(term.values[0].ontology_term.term_id, ontologyTerm.term_id) experiment = common_pb2.Experiment() experiment.id = "test" key = "my_experiment" dataset.attributes.attr[key] \ .values.add().experiment.MergeFrom(experiment) self.assertEqual(dataset.attributes.attr[key].values[0].experiment.id, experiment.id)
def testNestedAttributes(self): """ Demonstrates how nested attributes can be used to interchange arbitrary unstructured, typed data. """ dataset = metadata_pb2.Dataset() key1 = "key1" key2 = "key2" string_value = "hello" int32_value = 32 attributes = {key1: [{key2: [string_value]}, int32_value]} myAttribute = dataset.attributes.attr[key1].values nestedAttribute = myAttribute.add().attributes.attr[key2].values nestedAttribute.add().string_value = string_value myAttribute.add().int32_value = int32_value mynested = dataset.attributes.attr[key1]. \ values[0].attributes.attr[key2] self.assertEqual(mynested.values[0].string_value, attributes[key1][0][key2][0]) self.assertEqual(dataset.attributes.attr[key1].values[1].int32_value, attributes[key1][1])