def _find_snp_position(snp_track, name): if snp_track is None: query = ElasticQuery.query_match("id", name) elastic = Search(query, idx=ElasticSettings.idx('MARKER')) snpResult = elastic.get_json_response() if(len(snpResult['hits']['hits'])) > 0: snp = snpResult['hits']['hits'][0]['_source'] chrom = snp['seqid'].replace('chr', "") position = snp['start'] return {'chr': chrom, 'start': (position-1), 'end': position, 'name': name} else: mo = re.match(r"(.*)-(.*)", snp_track) (group, track) = mo.group(1, 2) try: snp_track_idx = ElasticSettings.idx('CP_STATS_'+group.upper(), snp_track.upper()) except SettingsError: snp_track_idx = ElasticSettings.idx('CP_STATS_'+group.upper())+"/"+track query = ElasticQuery.query_match("name", name) elastic = Search(query, idx=snp_track_idx) snpResult = elastic.get_json_response() if(len(snpResult['hits']['hits'])) > 0: snp = snpResult['hits']['hits'][0]['_source'] chrom = snp['seqid'].replace('chr', "") position = snp['start'] return {'chr': chrom, 'start': (position-1), 'end': position, 'name': name} return {'error': 'Marker '+name+' does not exist in the currently selected dataset'}
def _find_snp_position(snp_track, name): if snp_track is None: query = ElasticQuery.query_match("id", name) elastic = Search(query, idx=ElasticSettings.idx('MARKER')) snpResult = elastic.get_json_response() if(len(snpResult['hits']['hits'])) > 0: snp = snpResult['hits']['hits'][0]['_source'] chrom = snp['seqid'].replace('chr', "") position = snp['start'] return {'chr': chrom, 'start': (position-1), 'end': position, 'name': name} else: mo = re.match(r"(.*)-(.*)", snp_track) (group, track) = mo.group(1, 2) try: snp_track_idx = ElasticSettings.idx('CP_STATS_'+group.upper(), snp_track.upper()) except SettingsError: snp_track_idx = ElasticSettings.idx('CP_STATS_'+group.upper())+"/"+track query = ElasticQuery.query_match("name", name) elastic = Search(query, idx=snp_track_idx) snpResult = elastic.get_json_response() if(len(snpResult['hits']['hits'])) > 0: snp = snpResult['hits']['hits'][0]['_source'] chrom = snp['seqid'].replace('chr', "") position = snp['start'] return {'chr': chrom, 'start': (position-1), 'end': position, 'name': name} return {'error': 'Marker '+name+' does not exist in the currently selected dataset'}
def show_es_gene_section(gene_symbol=None, seqid=None, start_pos=None, end_pos=None): ''' Template inclusion tag to render a gene section given a chado gene feature. ''' if seqid is not None and isinstance(seqid, str) and seqid.startswith("chr"): seqid = seqid else: seqid = 'chr' + str(seqid) if gene_symbol is not None: ''' gene symbol query''' query = ElasticQuery.query_match("gene_symbol", gene_symbol) elif end_pos is None: ''' start and end are same, range query for snp''' query_bool = BoolQuery(must_arr=[ Query.match("seqid", seqid), RangeQuery("featureloc.start", lte=start_pos), RangeQuery("featureloc.end", gte=start_pos) ]) query = ElasticQuery.bool(query_bool) else: ''' start and end are same, range query for snp''' query_bool = BoolQuery(must_arr=[ Query.match("seqid", seqid), RangeQuery("featureloc.start", gte=start_pos), RangeQuery("featureloc.end", lte=end_pos) ]) query = ElasticQuery.bool(query_bool) elastic = Search(query, idx=ElasticSettings.idx(name='GENE')) return {'es_genes': elastic.search().docs}
def show_es_gene_section(gene_symbol=None, seqid=None, start_pos=None, end_pos=None): ''' Template inclusion tag to render a gene section given a chado gene feature. ''' seqid = str(seqid).replace('chr', '') if gene_symbol is not None: ''' gene symbol query''' query = ElasticQuery.query_match("symbol", gene_symbol) elif end_pos is None: ''' start and end are same, range query for snp''' query_bool = BoolQuery(must_arr=[ Query.match("chromosome", seqid), RangeQuery("start", lte=start_pos), RangeQuery("stop", gte=start_pos) ]) query = ElasticQuery.bool(query_bool) else: ''' start and end are same, range query for snp''' query_bool = BoolQuery(must_arr=[ Query.match("chromosome", seqid), RangeQuery("start", gte=start_pos), RangeQuery("stop", lte=end_pos) ]) query = ElasticQuery.bool(query_bool) elastic = Search(query, idx=ElasticSettings.idx(name='GENE')) return {'es_genes': elastic.search().docs}
def region_page(request, region): ''' Region elastic''' query = ElasticQuery.query_match("attr.region_id", region) elastic = Search(query, idx=ElasticSettings.idx(name='REGION')) context = elastic.get_result() context['title'] = "Region" print(context) return render(request, 'region/region.html', context, content_type='text/html')
def region_page(request, region): ''' Region elastic''' query = ElasticQuery.query_match("attr.region_id", region) elastic = Search(query, idx=ElasticSettings.idx(name='REGION')) context = elastic.get_result() context['title'] = "Region" print(context) return render(request, 'region/region.html', context, content_type='text/html')
def marker_page(request, marker): ''' Render a marker page ''' query = ElasticQuery.query_match("id", marker) elastic = Search(query) context = elastic.get_result() _add_info(context) # rs history lookup if Search.index_exists( idx=ElasticSettings.idx(name='MARKER', idx_type='HISTORY')): query = ElasticQuery.query_match("rscurrent", marker) rs_history = Search(query, idx=ElasticSettings.idx(name='MARKER', idx_type='HISTORY')) context['history'] = rs_history.get_result() # get gene(s) overlapping position position = context['data'][0]['start'] chrom = 'chr' + str(context['data'][0]['seqid']) featurelocs = ( Featureloc.objects.filter(fmin__lt=position) # @UndefinedVariable .filter(fmax__gt=position) # @UndefinedVariable .filter(srcfeature__uniquename=chrom) # @UndefinedVariable .filter(feature__type__name='gene')) # @UndefinedVariable genes = [] for loc in featurelocs: genes.append(loc.feature) context['genes'] = genes # page title if len(context["data"]) == 1 and "id" in context["data"][0]: context['title'] = "Marker - " + context["data"][0]["id"] return render(request, 'marker/marker.html', context, content_type='text/html')
def _find_snp_position(snp_track, name): mo = re.match(r"(.*)-(.*)", snp_track) (group, track) = mo.group(1, 2) snp_track_idx = getattr(chicp_settings, 'CHICP_IDX').get(group).get('INDEX') snp_track_type = '' if getattr(chicp_settings, 'CHICP_IDX').get(group).get('TRACKS').get(snp_track): snp_track_type = getattr(chicp_settings, 'CHICP_IDX').get(group).get('TRACKS') \ .get(snp_track).get('TYPE') else: snp_track_type = track query = ElasticQuery.query_match("name", name) elastic = Search(query, idx=snp_track_idx+'/'+snp_track_type) snpResult = elastic.get_result() if (len(snpResult['data']) > 0): chrom = snpResult['data'][0]['seqid'].replace('chr', "") position = snpResult['data'][0]['end'] return {'chr': chrom, 'start': (position-1), 'end': position, 'name': name} return {'error': 'Marker '+name+' does not exist in the currently selected dataset'}
def show_es_gene_section(gene_symbol=None, seqid=None, start_pos=None, end_pos=None): ''' Template inclusion tag to render a gene section given a chado gene feature. ''' seqid = str(seqid).replace('chr', '') if gene_symbol is not None: ''' gene symbol query''' query = ElasticQuery.query_match("symbol", gene_symbol) elif end_pos is None: ''' start and end are same, range query for snp''' query_bool = BoolQuery(must_arr=[Query.match("chromosome", seqid), RangeQuery("start", lte=start_pos), RangeQuery("stop", gte=start_pos)]) query = ElasticQuery.bool(query_bool) else: ''' start and end are same, range query for snp''' query_bool = BoolQuery(must_arr=[Query.match("chromosome", seqid), RangeQuery("start", gte=start_pos), RangeQuery("stop", lte=end_pos)]) query = ElasticQuery.bool(query_bool) elastic = Search(query, idx=ElasticSettings.idx(name='GENE')) return {'es_genes': elastic.search().docs}
def test_match_query(self): ''' Test building and running a match query. ''' query = ElasticQuery.query_match("id", "rs2476601") search = Search(query, idx=ElasticSettings.idx('DEFAULT')) self.assertTrue(len(search.search().docs) == 1, "Elastic string query retrieved marker (rs2476601)")