本文整理汇总了Python中elastic.query.Query.term方法的典型用法代码示例。如果您正苦于以下问题:Python Query.term方法的具体用法?Python Query.term怎么用?Python Query.term使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类elastic.query.Query
的用法示例。
在下文中一共展示了Query.term方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_interaction_doc
# 需要导入模块: from elastic.query import Query [as 别名]
# 或者: from elastic.query.Query import term [as 别名]
def get_interaction_doc(self, interaction_source='intact', parent_id=None):
idx_key = 'GENE'
idx_type_key = 'INTERACTIONS'
parent_idx_key = 'GENE'
idx = ElasticSettings.idx(idx_key, idx_type_key)
(idx, idx_type) = idx.split('/')
if parent_id:
qbool_intact = BoolQuery().must([Query.term("interaction_source", interaction_source),
Query.term("_parent", parent_id)])
else:
qbool_intact = BoolQuery().should([Query.term("interaction_source", interaction_source)])
# Get random doc or specific if id is passed in query
docs_by_geneid = DataIntegrityUtils.get_rdm_docs(idx, idx_type, qbool=qbool_intact, sources=[], size=1)
doc = docs_by_geneid[0]
# Get parent doc
parent_id = doc.parent()
parent_docs = DataIntegrityUtils.fetch_from_elastic(idx_key, parent_idx_key, [parent_id])
if parent_docs:
self.assertTrue(len(parent_docs) >= 1, "Found 1 parent")
parent_doc = parent_docs[0]
return doc, parent_doc
else:
return self.get_interaction_doc("intact", parent_id)
示例2: fetch_overlapping_features
# 需要导入模块: from elastic.query import Query [as 别名]
# 或者: from elastic.query.Query import term [as 别名]
def fetch_overlapping_features(cls, build, seqid, start, end, idx=None, idx_type=None, disease_id=None):
''' function to create fetch overlapping features for a given stretch of region
the build info is stored as nested document..so nested query is build
@type build: string
@param build: build info eg: 'GRCh38'
@type seqid: string
@param seqid: chromosome number
@type start: string
@param start: region start
@type end: string
@param end: region end
@type idx: string
@param idx: name of the index
@type idx_type: string
@param idx_type: name of the idx type, each criteria is an index type
@type disease_id: string
@param disease_id: disease code
'''
nbuild = build
start_range = start
end_range = end
bool_range = BoolQuery()
bool_range.must(RangeQuery("build_info.start", lte=start_range)) \
.must(RangeQuery("build_info.end", gte=end_range))
or_filter = OrFilter(RangeQuery("build_info.start", gte=start_range, lte=end_range))
or_filter.extend(RangeQuery("build_info.end", gte=start_range, lte=end_range)) \
.extend(bool_range)
bool_query = BoolQuery()
if disease_id:
qnested_buildinfo = Query.nested('build_info', bool_query)
bool_query = BoolQuery()
bool_query.must(Query.term("disease", disease_id.lower())).must(qnested_buildinfo)
qnested = ElasticQuery(bool_query, sources=['build_info.*',
'disease_locus',
'disease',
'chr_band',
'species'])
else:
bool_query.must(Query.term("build_info.build", nbuild)) \
.must(Query.term("build_info.seqid", seqid)) \
.filter(or_filter)
qnested = ElasticQuery(Query.nested('build_info', bool_query), sources=['build_info.*',
'disease_locus',
'disease',
'chr_band',
'species'])
elastic = Search(qnested, idx=idx, idx_type=idx_type)
res = elastic.search()
return res.docs
示例3: get_elastic_query
# 需要导入模块: from elastic.query import Query [as 别名]
# 或者: from elastic.query.Query import term [as 别名]
def get_elastic_query(cls, section=None, config=None):
''' function to build the elastic query object
@type section: string
@keyword section: The section in the criteria.ini file
@type config: string
@keyword config: The config object initialized from criteria.ini.
@return: L{Query}
'''
section_config = config[section]
source_fields = []
if 'source_fields' in section_config:
source_fields_str = section_config['source_fields']
source_fields = source_fields_str.split(',')
if 'mhc' in section:
seqid = '6'
start_range = 25000000
end_range = 35000000
seqid_param = section_config['seqid_param']
start_param = section_config['start_param']
end_param = section_config['end_param']
if section == 'is_gene_in_mhc':
# for region you should make a different query
# Defined MHC region as chr6:25,000,000..35,000,000
query = ElasticUtils.range_overlap_query(seqid, start_range, end_range,
source_fields,
seqid_param,
start_param,
end_param)
elif section == 'is_marker_in_mhc':
query_bool = BoolQuery()
query_bool.must(RangeQuery("start", lte=end_range)) \
.must(RangeQuery("start", gte=start_range)) \
.must(Query.term("seqid", seqid))
query = ElasticQuery.filtered_bool(Query.match_all(), query_bool, sources=["id", "seqid", "start"])
elif section == 'is_region_in_mhc':
query = ElasticQuery(Query.term("region_name", "MHC"))
elif section == 'marker_is_gwas_significant_in_ic':
# build a range query
gw_sig_p = 0.00000005
query = ElasticQuery(RangeQuery("p_value", lte=gw_sig_p))
else:
if len(source_fields) > 0:
query = ElasticQuery(Query.match_all(), sources=source_fields)
else:
# query = ElasticQuery(Query.match_all())
return None
return query
示例4: show_disease
# 需要导入模块: from elastic.query import Query [as 别名]
# 或者: from elastic.query.Query import term [as 别名]
def show_disease(disease, scores, text=True, selected=None, href="/disease/"):
''' Template inclusion tag to render disease bar. '''
if isinstance(disease, str):
if disease == 'OD':
disease = Document({"_source": {"code": "OD", "colour": "grey", "name": "Other Diseases"}})
else:
query = ElasticQuery(BoolQuery(should_arr=[Query.term('code', disease.lower()),
Query.term('name', disease.lower())]))
disease = Search(query, idx=ElasticSettings.idx('DISEASE'), size=1).search().docs[0]
score = ''
if scores != '':
score = scores[0]
return {'disease': disease, 'score': score, 'text': text, 'selected': selected, 'href': href}
示例5: filter_queryset
# 需要导入模块: from elastic.query import Query [as 别名]
# 或者: from elastic.query.Query import term [as 别名]
def filter_queryset(self, request, queryset, view):
''' Override this method to request just the documents required from Rserve. '''
try:
filterable = getattr(view, 'filter_fields', [])
filters = dict([(k, v) for k, v in request.GET.items() if k in filterable])
mid1 = filters.get('marker', 'rs2476601')
dataset = filters.get('dataset', 'EUR').replace('-', '')
query = ElasticQuery(BoolQuery(must_arr=[Query.term("id", mid1)]), sources=['seqid', 'start'])
elastic = Search(search_query=query, idx=ElasticSettings.idx('MARKER', 'MARKER'), size=1)
doc = elastic.search().docs[0]
seqid = getattr(doc, 'seqid')
rserve = getattr(settings, 'RSERVE')
conn = pyRserve.connect(host=rserve.get('HOST'), port=rserve.get('PORT'))
pop_str = conn.r.get_pop(dataset, seqid, mid1)
pops = json.loads(str(pop_str))
populations = []
for pop in pops:
pops[pop]['population'] = pop
populations.append(pops[pop])
conn.close()
return [ElasticObject(initial={'populations': populations, 'marker': mid1})]
except (TypeError, ValueError, IndexError, ConnectionError):
return [ElasticObject(initial={'populations': None, 'marker': mid1})]
示例6: get_disease_tags
# 需要导入模块: from elastic.query import Query [as 别名]
# 或者: from elastic.query.Query import term [as 别名]
def get_disease_tags(cls, feature_id, idx=None, idx_type=None):
''' function to get the aggregated list of disease_tags for a given feature id, aggregated
from all criteria_types for a feature type
@type feature_id: string
@keyword feature_id: Id of the feature (gene => gene_id, region=>region_id)
@type idx: string
@param idx: name of the index
@type idx_type: string
@param idx_type: name of the idx type, each criteria is an index type
'''
query = ElasticQuery(Query.term("qid", feature_id))
agg = Agg("criteria_disease_tags", "terms", {"field": "disease_tags", "size": 0})
aggs = Aggs(agg)
if idx_type:
search = Search(query, aggs=aggs, idx=idx, idx_type=idx_type)
else:
search = Search(query, aggs=aggs, idx=idx)
disease_tags = []
try:
r_aggs = search.search().aggs
buckets = r_aggs['criteria_disease_tags'].get_buckets()
disease_tags = [dis_dict['key'].lower() for dis_dict in buckets]
except:
return []
# get disease docs
if (len(disease_tags) > 0):
(core, other) = Disease.get_site_diseases(dis_list=disease_tags)
diseases = list(core)
diseases.extend(other)
return diseases
else:
return None
示例7: _get_random_marker
# 需要导入模块: from elastic.query import Query [as 别名]
# 或者: from elastic.query.Query import term [as 别名]
def _get_random_marker(self):
''' Get a random marker from the dbSNP elastic index. '''
(idx, idx_type) = ElasticSettings.idx('MARKER', 'MARKER').split('/')
seqid = random.randint(1, 10)
qbool = BoolQuery(must_arr=[Query.term("seqid", seqid), RangeQuery("tags.weight", gte=80)])
doc = ElasticUtils.get_rdm_docs(idx, idx_type, qbool=qbool, sources=['id', 'start'], size=1)[0]
return getattr(doc, 'id')
示例8: get_hits_by_study_id
# 需要导入模块: from elastic.query import Query [as 别名]
# 或者: from elastic.query.Query import term [as 别名]
def get_hits_by_study_id(cls, study_id, sources=[]):
''' Get visible/authenticated hits. '''
hits_query = ElasticQuery(BoolQuery(must_arr=Query.term('dil_study_id', study_id),
b_filter=Filter(Query.missing_terms("field", "group_name"))),
sources=sources)
docs = Search(hits_query, idx=ElasticSettings.idx('REGION', 'STUDY_HITS'), size=1000).search().docs
ens_ids = [gene for doc in docs if getattr(doc, 'genes') for gene in getattr(doc, 'genes')]
gene_docs = utils.get_gene_docs_by_ensembl_id(ens_ids, ['symbol'])
for doc in docs:
if getattr(doc, 'genes'):
genes = {}
for ens_id in getattr(doc, 'genes'):
try:
genes[ens_id] = getattr(gene_docs[ens_id], 'symbol')
except KeyError:
genes = {ens_id: ens_id}
setattr(doc, 'genes', genes)
build_info = getattr(doc, 'build_info')
for bi in build_info:
if bi['build'] == settings.DEFAULT_BUILD:
setattr(doc, "loc", "chr" + bi['seqid'] + ":" +
str(locale.format("%d", bi['start'], grouping=True)) + "-" +
str(locale.format("%d", bi['end'], grouping=True)))
setattr(doc, "encoded_loc", "chr" + bi['seqid'] + "%3A" +
str(bi['start']) + ".." + str(bi['end']))
return docs
示例9: test_pubs_disease_tags
# 需要导入模块: from elastic.query import Query [as 别名]
# 或者: from elastic.query.Query import term [as 别名]
def test_pubs_disease_tags(self):
''' Check the number of disease publications against the number of tags.disease and
report differences`. '''
count = True
msg = ''
for disease in DiseasePublicationTest.DISEASES:
pmids = self._get_pmids(disease)
disease_code = disease.lower()
elastic = Search(search_query=ElasticQuery(BoolQuery(
b_filter=Filter(Query.term('tags.disease', disease_code))), sources=['pmid']),
idx=ElasticSettings.idx('PUBLICATION'), size=len(pmids)*2)
res = elastic.get_count()
msg += disease_code+'\tINDEX: '+str(res['count'])+'\tNCBI: '+str(len(pmids))
if res['count'] != len(pmids):
count = False
docs = elastic.search().docs
pmids_in_idx = [getattr(doc, 'pmid') for doc in docs]
pmids_diff1 = [pmid for pmid in pmids_in_idx if pmid not in pmids]
pmids_diff2 = [pmid for pmid in pmids if pmid not in pmids_in_idx]
if len(pmids_diff1) > 0:
msg += '\textra PMIDs: '+str(pmids_diff1)
if len(pmids_diff2) > 0:
msg += '\tmissing PMIDs: '+str(pmids_diff2)
msg += '\n'
print(msg)
self.assertTrue(count, 'Count for disease tags')
示例10: get_overlapping_hits
# 需要导入模块: from elastic.query import Query [as 别名]
# 或者: from elastic.query.Query import term [as 别名]
def get_overlapping_hits(self, build, seqid, start, end):
query_bool = BoolQuery(must_arr=[RangeQuery("build_info.start", lte=start),
RangeQuery("build_info.end", gte=end)])
or_filter = OrFilter(RangeQuery("build_info.start", gte=start, lte=end))
or_filter.extend(RangeQuery("build_info.end", gte=start, lte=end)) \
.extend(query_bool)
range_query = FilteredQuery(BoolQuery(must_arr=[Query.term("build_info.seqid", seqid),
Query.term("build_info.build", build)]),
or_filter)
query = ElasticQuery.filtered_bool(
Query.nested("build_info", range_query),
BoolQuery(must_arr=[RangeQuery("tier", lte=2)]),
# sources=["disease", "marker", "chr_band", "tier", "build_info", "disease_locus"]
)
elastic = Search(search_query=query, idx=ElasticSettings.idx('REGION', 'STUDY_HITS'))
return elastic.search().docs
示例11: check_hits
# 需要导入模块: from elastic.query import Query [as 别名]
# 或者: from elastic.query.Query import term [as 别名]
def check_hits(resp_json):
rsids = {}
docs = [Document(hit) for hit in resp_json['hits']['hits']]
for doc in docs:
rsid = getattr(doc, "id")
if rsid is not None:
rsids[rsid] = doc
rsids_keys = list(rsids.keys())
terms_filter = TermsFilter.get_terms_filter("id", rsids_keys)
query = ElasticQuery.filtered(Query.match_all(), terms_filter)
elastic = Search(query, idx=ElasticSettings.idx('MARKER', 'MARKER'), size=len(rsids_keys))
docs_by_rsid = elastic.search().docs
for doc in docs_by_rsid:
info = getattr(doc, "info")
if 'VC=SNV' not in info:
continue
rsid = getattr(doc, "id")
ic_doc = rsids[rsid]
pos1 = getattr(doc, "start")
pos2 = self._get_highest_build(ic_doc)['position']
if abs(int(pos1) - int(pos2)) > 1:
is_par = getattr(ic_doc, 'is_par')
allele_a = getattr(ic_doc, 'allele_a')
if is_par is None and not (allele_a == 'D' or allele_a == 'I'):
msg = ("CHECK IC/DBSNP POSITIONS:: "+getattr(ic_doc, 'name') +
' '+str(pos2)+" "+rsid+' '+str(pos1))
# ' ('+ic_doc.doc_id()+' '+json.dumps(getattr(ic_doc, 'build_info'))+')'
query = ElasticQuery.filtered(Query.term("seqid", getattr(doc, 'seqid')),
Filter(Query.term("start", pos2)))
elastic = Search(query, idx=ElasticSettings.idx('MARKER', 'MARKER'))
docs_by_pos = elastic.search().docs
if len(docs_by_pos) > 0:
for d in docs_by_pos:
msg += " ("+getattr(d, "id")+":"+str(getattr(d, "start"))+")"
query = ElasticQuery.filtered(Query.match_all(), Filter(Query.term("rslow", rsid)))
elastic = Search(query, idx=ElasticSettings.idx('MARKER', 'HISTORY'))
docs_by_pos = elastic.search().docs
if len(docs_by_pos) > 0:
for d in docs_by_pos:
msg += " (rshigh:"+str(getattr(d, "rshigh")) + \
" build_id:"+str(getattr(d, "build_id"))+")"
logger.error(msg)
示例12: get_studies
# 需要导入模块: from elastic.query import Query [as 别名]
# 或者: from elastic.query.Query import term [as 别名]
def get_studies(cls, study_ids=None, disease_code=None, sources=[], split_name=True):
studies_query = ElasticQuery(Query.match_all(), sources=sources)
if disease_code is not None:
studies_query = ElasticQuery(BoolQuery(must_arr=Query.term("diseases", disease_code)), sources=sources)
elif study_ids:
studies_query = ElasticQuery(Query.ids(study_ids), sources=sources)
studies = Search(studies_query, idx=ElasticSettings.idx('STUDY', 'STUDY'), size=200).search().docs
for doc in studies:
if split_name and getattr(doc, 'study_name') is not None:
setattr(doc, 'study_name', getattr(doc, 'study_name').split(':', 1)[0])
return Document.sorted_alphanum(studies, "study_id")
示例13: _build_exon_query
# 需要导入模块: from elastic.query import Query [as 别名]
# 或者: from elastic.query.Query import term [as 别名]
def _build_exon_query(chrom, segmin, segmax, genes):
# get exonic structure for genes in this section
geneExons = dict()
query_bool = BoolQuery()
query_bool.must([Query.term("seqid", chrom)])
if len(genes) > 0:
for g in genes:
query = ElasticQuery.filtered_bool(Query.query_string(g["gene_id"], fields=["name"]),
query_bool, sources=utils.snpFields)
elastic = Search(query, idx=getattr(chicp_settings, 'CP_GENE_IDX')+'/exons/', search_from=0, size=2000)
result = elastic.get_result()
exons = result['data']
exons = utils.makeRelative(int(segmin), int(segmax), ['start', 'end'], exons)
geneExons[g["gene_id"]] = sorted(exons, key=operator.itemgetter("start"))
return geneExons
示例14: filter_queryset
# 需要导入模块: from elastic.query import Query [as 别名]
# 或者: from elastic.query.Query import term [as 别名]
def filter_queryset(self, request, queryset, view):
""" Override this method to request just the documents required from Rserve. """
try:
filterable = getattr(view, "filter_fields", [])
filters = dict([(k, v) for k, v in request.GET.items() if k in filterable])
mid1 = filters.get("m1")
if mid1 is None or mid1 == "":
return [ElasticObject(initial={"error": "No marker ID provided."})]
dataset = filters.get("dataset", "EUR").replace("-", "")
mid2 = filters.get("m2")
window_size = int(filters.get("window_size", 1000000))
dprime = filters.get("dprime", 0.0)
rsq = filters.get("rsq", 0.8)
maf = filters.get("maf", False)
if maf:
maf = True
build_version = filters.get("build", "GRCh38").lower()
pos = filters.get("pos", False)
if pos:
pos = True
query = ElasticQuery(BoolQuery(must_arr=[Query.term("id", mid1)]), sources=["seqid", "start"])
elastic = Search(search_query=query, idx=ElasticSettings.idx("MARKER", "MARKER"), size=1)
doc = elastic.search().docs[0]
seqid = getattr(doc, "seqid")
rserve = getattr(settings, "RSERVE")
conn = pyRserve.connect(host=rserve.get("HOST"), port=rserve.get("PORT"))
ld_str = conn.r.ld_run(
dataset,
seqid,
mid1,
marker2=mid2,
window_size=window_size,
dprime=dprime,
rsq=rsq,
maf=maf,
position=pos,
build_version=build_version,
)
ld_str = ld_str.replace("D.prime", "dprime").replace("R.squared", "rsquared")
conn.close()
return [ElasticObject(initial=json.loads(str(ld_str)))]
except (TypeError, ValueError, IndexError, ConnectionError):
raise Http404
示例15: get_criteria_details
# 需要导入模块: from elastic.query import Query [as 别名]
# 或者: from elastic.query.Query import term [as 别名]
def get_criteria_details(cls, feature_id, idx, idx_type, criteria_id=None):
'''Function to get criteria details for a given feature_id. If criteria_id is given,
the result is restricted to that criteria
@type feature_id: string
@keyword feature_id: Id of the feature (gene => gene_id, region=>region_id)
@type criteria_id: string
@keyword criteria_id: criteria_id eg: cand_gene_in_study, gene_in_region
@type idx: string
@param idx: name of the index
@type idx_type: string
@param idx_type: name of the idx type, each criteria is an index type
'''
query = ElasticQuery(Query.term("qid", feature_id))
search = Search(query, idx=idx, idx_type=idx_type)
# elastic_docs = search.search().docs
criteria_hits = search.get_json_response()['hits']
return(criteria_hits)