def aggregated_statistics(self, request, pk): """ Retrieve the aggregation percentage for associations meeting filters criteria for this gene. Check the FAQ for details on the filters. """ gene = elastic.load_gene_by_id(pk) zoom = int(request.query_params.get('zoom', 0)) selected = bool(request.query_params.get('gene', '') == "1") # last_el = [request.query_params.get('lastel', '')] filters = _get_filter_from_params(request.query_params) filters['chr'] = [gene['chr']] filters['start'] = gene['positions']['gte'] - zoom filters['end'] = gene['positions']['lte'] + zoom if selected: filters['start'] = gene['positions']['gte'] filters['end'] = gene['positions']['lte'] filters['gene_id'] = gene['name'] chr, maf, mac, type, annotations = elastic.get_aggregated_filtered_statistics( filters) chr_dict = _get_percentages_from_buckets(chr) maf_dict = _get_percentages_from_buckets(maf) mac_dict = _get_percentages_from_buckets(mac) type_dict = _get_percentages_from_buckets(type) annotations_dict = _get_percentages_from_buckets(annotations) return Response({ 'chromosomes': chr_dict, 'maf': maf_dict, 'mac': mac_dict, 'types': type_dict, 'annotations': annotations_dict })
def associations(self, request, pk): """ Return associations for the selected gene. Can add zoom and other filters. Check the FAQ for details on the filters. """ gene = elastic.load_gene_by_id(pk) zoom = int(request.query_params.get('zoom', 0)) selected = bool(request.query_params.get('gene', '') == "1") # last_el = [request.query_params.get('lastel', '')] filters = _get_filter_from_params(request.query_params) filters['chr'] = [gene['chr']] filters['start'] = gene['positions']['gte'] - zoom filters['end'] = gene['positions']['lte'] + zoom if selected: filters['start'] = gene['positions']['gte'] filters['end'] = gene['positions']['lte'] filters['gene_id'] = gene['name'] limit = self.paginator.get_limit(request) offset = self.paginator.get_offset(request) associations, count = elastic.load_filtered_top_associations(filters, offset, limit) queryset = EsQuerySet(associations, count) paginated_asso = self.paginate_queryset(queryset) return self.get_paginated_response(paginated_asso)
def retrieve(self, request, pk): """ Retrieve information about a specific gene """ gene = elastic.load_gene_by_id(pk) return Response(gene)
def download_csv(self, request): """ Prepare a csv file from the elasticsearch database and return it as a downloadable file through a celery task. Check the FAQ for details on the filters. """ # Load studies from regular db import datetime filters = _get_filter_from_params(request.query_params) gene_id = request.query_params.getlist( 'gene_id' ) # We need to do this because we cannot solely rely on the annotations of the SNPs for gene-name import os export_folder = '%s/export' % settings.HDF5_FILE_PATH if not os.path.isdir(export_folder): os.mkdir(export_folder) # Other download basenames: download_name = "aragwas_associations" if filters['study_id'] != []: if isinstance(filters['study_id'], list): filters['study_id'] = filters['study_id'][0] study_name = Study.objects.get(pk=filters['study_id']).name download_name = "{}_associations".format(study_name) elif filters['phenotype_id'] != []: phenotype_name = Phenotype.objects.get( pk=filters['phenotype_id']).name download_name = "{}_associations".format(phenotype_name) if gene_id != []: download_name = "{}_associations".format( gene_id[0]) # Override previous ones zoom = int(request.query_params.getlist('zoom')[0]) print(zoom) gene = elastic.load_gene_by_id(gene_id[0]) filters['chr'] = [gene['chr']] filters['start'] = gene['positions']['gte'] - zoom filters['end'] = gene['positions']['lte'] + zoom if _is_filter_whole_dataset(filters): # download_name = "all_associations" file_name = "%s/all_associations.csv" % (settings.HDF5_FILE_PATH) chunk_size = 8192 response = StreamingHttpResponse(FileWrapper( open(file_name, "rb"), chunk_size), content_type="text/csv") response['Content-Length'] = os.path.getsize(file_name) else: file_name = '%s/' % export_folder + str( datetime.datetime.now()) + '.csv' # give it a unique name opts = dict(doc_type='associations', output_file=file_name) fn = download_es2csv(opts, filters) # wait for file to be done # Add filters to name: if 'chr' in filters and len(filters['chr']) > 0 and len( filters['chr']) < 5: download_name += "_chr" for c in filters['chr']: download_name += "_{}".format(c) if 'maf' in filters and len(filters['maf']) > 0 and len( filters['maf']) < 4: download_name += "_maf_filtered" if 'annotation' in filters and len( filters['annotation']) > 0 and len( filters['annotation']) < 4: download_name += "_annotation_filtered" if 'type' in filters and len(filters['type']) == 1: download_name += "_{}".format(filters['type'][0]) if 'significant' in filters: if filters['significant'] == ['p']: download_name += "_significant_permutation" elif filters['significant'] == ['b']: download_name += "_significant_bonferroni" chunk_size = 8192 response = StreamingHttpResponse(FileWrapper( open(file_name, "rb"), chunk_size), content_type="text/csv") response['Content-Length'] = os.path.getsize(file_name) response['Content-Disposition'] = "attachment; filename={}.csv".format( download_name) return response
def retrieve(self, request, pk): """ Retrieve information about a specific gene """ gene = elastic.load_gene_by_id(pk) gene['ko_associations'] = elastic.load_gene_ko_associations(pk, return_only_significant=True) return Response(gene)