def search_variants(request): """Handles requests to the /variants/search method""" conditional = validate_search_variants_request(request) if conditional: return conditional else: try: protocol_variable = json_format.Parse( request.body, variant_service.SearchVariantsRequest()) except Exception as e: return HttpResponseBadRequest(json.dumps( {"message": e.message.replace("\"", "'")}), content_type='application/json') variant_set_id = protocol_variable.variant_set_id reference_name = protocol_variable.reference_name start = protocol_variable.start end = protocol_variable.end page_size = protocol_variable.page_size page_token = protocol_variable.page_token if not page_size or page_size == 0: page_size = DEFAULT_PAGE_SIZE if not page_token: page_token = '0' response = variant_service.SearchVariantsResponse() variants = CurrentVariant.objects dataset_id, reference_genome = variant_set_id.split('-') if dataset_id != DATASET_ID: return HttpResponseBadRequest(json.dumps( ErrorMessages['variantSetId']), content_type='application/json') variants = range_filter(reference_genome, variants, reference_name, start, end) variants = ga4gh_brca_page(variants, int(page_size), int(page_token)) ga_variants = [] for i in variants.values(): try: ga_variants.append(brca_to_ga4gh(i, reference_genome)) except ValueError as e: print e if len(ga_variants) > page_size: ga_variants.pop() page_token = str(1 + int(page_token)) response.next_page_token = page_token response.variants.extend(ga_variants) resp = json_format.MessageToDict(response, True) return JsonResponse(resp)
def test_search_variants_request_validation(self): """Ensures the search variants endpoint responds with expected failure modes""" request = variant_service.SearchVariantsRequest() req = self.factory.post("/data/ga4gh/variants/search", json.dumps(json_format.MessageToDict(request, False)), content_type="application/json") response = views.search_variants(req) self.assertEqual(response.status_code, 400, "No variant set ID should 400") self.assertJSONEqual(response.content, views.ErrorMessages['variantSetId'], "No variant set ID in the request should provide a useful error") request.variant_set_id = "Something not null" req = self.factory.post("/data/ga4gh/variants/search", json.dumps(json_format.MessageToDict(request, False)), content_type="application/json") response = views.search_variants(req) self.assertEquals(response.status_code, 400) self.assertJSONEqual(response.content, views.ErrorMessages['referenceName'], "A useful error is thrown when the reference name is not present") request.reference_name = "chr17" req= self.factory.post("/data/ga4gh/variants/search", json.dumps(json_format.MessageToDict(request, False)), content_type="application/json") response = views.search_variants(req) self.assertJSONEqual(response.content, views.ErrorMessages['start'], "A useful error is thrown when no start is present") request.start = 14589 req = self.factory.post("/data/ga4gh/variants/search", json.dumps(json_format.MessageToDict(request, False)), content_type="application/json") response = views.search_variants(req) self.assertJSONEqual(response.content, views.ErrorMessages['end'], "A useful error is provided when no end is present") request.end = 143295 req = self.factory.post("/data/ga4gh/variants/search", json.dumps(json_format.MessageToDict(request, False)), content_type="application/json") response = views.search_variants(req) self.assertEquals(response.status_code, 404, "A bad variant set ID should 404") # Test for an end value less than the end value test_request = self.factory.post("/data/ga4gh/variants/search", json.dumps({"referenceName": "chr17", "variantSetId": "brca-hg37", "start": 10000, "end": 1000}), content_type="application/json") response_x = views.search_variants(test_request) self.assertEqual(response_x.status_code, 400) self.assertJSONEqual(response_x.content, views.ErrorMessages['invalidPositions'])