Skip to content

Commit

Permalink
addressed range and sequence queries
Browse files Browse the repository at this point in the history
  • Loading branch information
costero-e committed Nov 15, 2024
1 parent 200ad47 commit d4ef536
Showing 1 changed file with 188 additions and 24 deletions.
212 changes: 188 additions & 24 deletions beacon/db/g_variants.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,15 @@ def include_resultset_responses(query: Dict[str, List[dict]], qparams: RequestPa
#LOG.debug("Include Resultset Responses = {}".format(qparams.query.include_resultset_responses))
return query


def generate_position_filter_start(key: str, value: List[int]) -> List[AlphanumericFilter]:
#LOG.debug("len value = {}".format(len(value)))
filters = []
if len(value) == 1:
filters.append(AlphanumericFilter(
id=VARIANTS_PROPERTY_MAP[key],
value=value[0],
operator=Operator.GREATER_EQUAL
))
elif len(value) == 2:
elif len(value) == 2:# pragma: no cover
filters.append(AlphanumericFilter(
id=VARIANTS_PROPERTY_MAP[key],
value=value[0],
Expand All @@ -56,17 +54,99 @@ def generate_position_filter_start(key: str, value: List[int]) -> List[Alphanume
))
return filters

def generate_position_filter_start_2(key: str, value: List[int]) -> List[AlphanumericFilter]:
filters = []
if len(value) == 1:
filters.append(AlphanumericFilter(
id=VARIANTS_PROPERTY_MAP["end"],
value=value[0],
operator=Operator.GREATER_EQUAL
))
elif len(value) == 2:# pragma: no cover
filters.append(AlphanumericFilter(
id=VARIANTS_PROPERTY_MAP[key],
value=value[0],
operator=Operator.GREATER_EQUAL
))
filters.append(AlphanumericFilter(
id=VARIANTS_PROPERTY_MAP[key],
value=value[1],
operator=Operator.LESS_EQUAL
))
return filters

def generate_position_filter_start_3(key: str, value: List[int]) -> List[AlphanumericFilter]:
filters = []
if len(value) == 1:
filters.append(AlphanumericFilter(
id=VARIANTS_PROPERTY_MAP[key],
value=value[0],
operator=Operator.LESS_EQUAL
))
elif len(value) == 2:# pragma: no cover
filters.append(AlphanumericFilter(
id=VARIANTS_PROPERTY_MAP[key],
value=value[0],
operator=Operator.GREATER_EQUAL
))
filters.append(AlphanumericFilter(
id=VARIANTS_PROPERTY_MAP[key],
value=value[1],
operator=Operator.LESS_EQUAL
))
return filters

def generate_position_filter_end(key: str, value: List[int]) -> List[AlphanumericFilter]:
#LOG.debug("len value = {}".format(len(value)))
filters = []
if len(value) == 1:
filters.append(AlphanumericFilter(
id=VARIANTS_PROPERTY_MAP[key],
value=value[0],
operator=Operator.LESS
))
elif len(value) == 2:# pragma: no cover
filters.append(AlphanumericFilter(
id=VARIANTS_PROPERTY_MAP[key],
value=value[0],
operator=Operator.GREATER_EQUAL
))
filters.append(AlphanumericFilter(
id=VARIANTS_PROPERTY_MAP[key],
value=value[1],
operator=Operator.LESS_EQUAL
))
return filters

def generate_position_filter_end_2(key: str, value: List[int]) -> List[AlphanumericFilter]:
filters = []
if len(value) == 1:
filters.append(AlphanumericFilter(
id=VARIANTS_PROPERTY_MAP["start"],
value=value[0],
operator=Operator.LESS
))
elif len(value) == 2:# pragma: no cover
filters.append(AlphanumericFilter(
id=VARIANTS_PROPERTY_MAP[key],
value=value[0],
operator=Operator.GREATER_EQUAL
))
filters.append(AlphanumericFilter(
id=VARIANTS_PROPERTY_MAP[key],
value=value[1],
operator=Operator.LESS_EQUAL
))
elif len(value) == 2:
return filters

def generate_position_filter_end_3(key: str, value: List[int]) -> List[AlphanumericFilter]:
filters = []
if len(value) == 1:
filters.append(AlphanumericFilter(
id=VARIANTS_PROPERTY_MAP[key],
value=value[0],
operator=Operator.GREATER_EQUAL
))
elif len(value) == 2:# pragma: no cover
filters.append(AlphanumericFilter(
id=VARIANTS_PROPERTY_MAP[key],
value=value[0],
Expand All @@ -79,33 +159,70 @@ def generate_position_filter_end(key: str, value: List[int]) -> List[Alphanumeri
))
return filters

def generate_position_filter_start_sequence_query(key: str, value: List[int]) -> List[AlphanumericFilter]:
filters = []
if len(value) == 1:
filters.append(AlphanumericFilter(
id=VARIANTS_PROPERTY_MAP[key],
value=value[0],
operator=Operator.EQUAL
))
return filters

def apply_request_parameters(query: Dict[str, List[dict]], qparams: RequestParams):
collection = 'g_variants'
#LOG.debug("Request parameters len = {}".format(len(qparams.query.request_parameters)))
if len(qparams.query.request_parameters) > 0 and "$and" not in query:
query["$and"] = []
if isinstance(qparams.query.request_parameters, list):
if isinstance(qparams.query.request_parameters, list):# pragma: no cover
query={}
query["$or"]=[]
for reqparam in qparams.query.request_parameters:
subquery={}
subquery["$and"] = []
startquery={}
startquery["$and"] = []
endquery={}
endquery["$and"] = []
startendquery={}
startendquery["$and"] = []
subqueryor={}
subqueryor["$or"] = []
equal=True
for k, v in reqparam.items():
if k == 'end':
equal=False
for k, v in reqparam.items():
if k == "start":
if isinstance(v, str):
v = v.split(',')
filters = generate_position_filter_start(k, v)
if equal == True:
filters = generate_position_filter_start_sequence_query(k, v)
else:
filters = generate_position_filter_start(k, v)
for filter in filters:
subquery["$and"].append(apply_alphanumeric_filter({}, filter, collection))
if filter.id == "start":
filter[id]=VARIANTS_PROPERTY_MAP["start"]
startquery["$and"].append(apply_alphanumeric_filter({}, filter, collection))
elif filter.id == "start2":
filter[id]=VARIANTS_PROPERTY_MAP["start"]
startquery["$and"].append(apply_alphanumeric_filter({}, filter, collection))
elif filter.id == "start3":
filter[id]=VARIANTS_PROPERTY_MAP["start"]
startendquery["$and"].append(apply_alphanumeric_filter({}, filter, collection))
elif k == "end":
if isinstance(v, str):
v = v.split(',')
filters = generate_position_filter_end(k, v)
for filter in filters:
subquery["$and"].append(apply_alphanumeric_filter({}, filter, collection))
if filter.id == "end":
filter[id]=VARIANTS_PROPERTY_MAP["end"]
endquery["$and"].append(apply_alphanumeric_filter({}, filter, collection))
elif filter.id == "end2":
filter[id]=VARIANTS_PROPERTY_MAP["end"]
endquery["$and"].append(apply_alphanumeric_filter({}, filter, collection))
elif filter.id == "end3":
filter[id]=VARIANTS_PROPERTY_MAP["end"]
startendquery["$and"].append(apply_alphanumeric_filter({}, filter, collection))
elif k == "datasets":
pass
elif k == "variantMinLength":
Expand Down Expand Up @@ -145,7 +262,6 @@ def apply_request_parameters(query: Dict[str, List[dict]], qparams: RequestParam
v_list=[]
if ',' in v:
v_list =v.split(',')
LOG.debug(v_list)
else:
v_list.append(v)
for id in v_list:
Expand All @@ -154,9 +270,14 @@ def apply_request_parameters(query: Dict[str, List[dict]], qparams: RequestParam
qparams.query.filters.append(v_dict)
return query, True
try:
LOG.debug(subqueryor)
if subqueryor["$or"] != []:
subquery["$and"].append(subqueryor)
if startquery["$and"] != []:
subquery["$or"].append(startquery)
if endquery["$and"] != []:
subquery["$or"].append(endquery)
if startendquery["$and"] != []:
subquery["$or"].append(startendquery)
except Exception:
pass
query["$or"].append(subquery)
Expand All @@ -165,19 +286,46 @@ def apply_request_parameters(query: Dict[str, List[dict]], qparams: RequestParam
subquery["$and"] = []
subqueryor={}
subqueryor["$or"] = []
startquery={}
startquery["$and"] = []
endquery={}
endquery["$and"] = []
startendquery={}
startendquery["$and"] = []
equal=False
for k, v in qparams.query.request_parameters.items():
if k == 'end':
equal=True
for k, v in qparams.query.request_parameters.items():
if k == "start":
if isinstance(v, str):
v = v.split(',')
filters = generate_position_filter_start(k, v)
for filter in filters:
query["$and"].append(apply_alphanumeric_filter({}, filter, collection))
if equal == False:
filters = generate_position_filter_start_sequence_query(k, v)
for filter in filters:
query["$and"].append(apply_alphanumeric_filter({}, filter, collection))
else:
filters = generate_position_filter_start(k, v)
filters2=generate_position_filter_start_2(k, v)
filters3=generate_position_filter_start_3(k, v)
for filter in filters:
startquery["$and"].append(apply_alphanumeric_filter({}, filter, collection))
for filter in filters2:
endquery["$and"].append(apply_alphanumeric_filter({}, filter, collection))
for filter in filters3:
startendquery["$and"].append(apply_alphanumeric_filter({}, filter, collection))
elif k == "end":
if isinstance(v, str):
v = v.split(',')
filters = generate_position_filter_end(k, v)
filters2 = generate_position_filter_end_2(k, v)
filters3 = generate_position_filter_end_3(k, v)
for filter in filters:
query["$and"].append(apply_alphanumeric_filter({}, filter, collection))
endquery["$and"].append(apply_alphanumeric_filter({}, filter, collection))
for filter in filters2:
startquery["$and"].append(apply_alphanumeric_filter({}, filter, collection))
for filter in filters3:
startendquery["$and"].append(apply_alphanumeric_filter({}, filter, collection))
elif k == "datasets":
pass
elif k == "variantMinLength":
Expand All @@ -186,38 +334,37 @@ def apply_request_parameters(query: Dict[str, List[dict]], qparams: RequestParam
id=VARIANTS_PROPERTY_MAP[k],
value='min'+v
), collection))
except KeyError:
except KeyError:# pragma: no cover
raise web.HTTPNotFound
elif k == "variantMaxLength":
try:
query["$and"].append(apply_alphanumeric_filter({}, AlphanumericFilter(
id=VARIANTS_PROPERTY_MAP[k],
value='max'+v
), collection))
except KeyError:
except KeyError:# pragma: no cover
raise web.HTTPNotFound
elif k == "mateName" or k == 'referenceName':
try:
subqueryor["$or"].append(apply_alphanumeric_filter({}, AlphanumericFilter(
id=VARIANTS_PROPERTY_MAP[k],
value=v
), collection))
except KeyError:
except KeyError:# pragma: no cover
raise web.HTTPNotFound
elif k != 'filters':
try:
query["$and"].append(apply_alphanumeric_filter({}, AlphanumericFilter(
id=VARIANTS_PROPERTY_MAP[k],
value=v
), collection))
except KeyError:
except KeyError:# pragma: no cover
raise web.HTTPNotFound

elif k == 'filters':
v_list=[]
if ',' in v:
v_list =v.split(',')
LOG.debug(v_list)
v_list =v.split(',')# pragma: no cover
else:
v_list.append(v)
for id in v_list:
Expand All @@ -226,13 +373,30 @@ def apply_request_parameters(query: Dict[str, List[dict]], qparams: RequestParam
qparams.query.filters.append(v_dict)
return query, True
try:
LOG.debug(subqueryor)
if subqueryor["$or"] != []:
subquery["$and"].append(subqueryor)
except Exception:
except Exception:# pragma: no cover
pass
if subquery["$and"] != []:
query["$and"].append(subquery)
if startquery["$and"] != []:
try:
query["$or"].append(startquery)
except Exception:
query["$or"]=[]
query["$or"].append(startquery)
if endquery["$and"] != []:
try:
query["$or"].append(endquery)
except Exception:
query["$or"]=[]
query["$or"].append(endquery)
if startendquery["$and"] != []:
try:
query["$or"].append(startendquery)
except Exception:
query["$or"]=[]
query["$or"].append(startendquery)


return query, False
Expand Down

0 comments on commit d4ef536

Please sign in to comment.