Skip to content

Commit

Permalink
Change query to only query ids within the dataset
Browse files Browse the repository at this point in the history
  • Loading branch information
SergiAguilo committed Jan 31, 2025
1 parent 74a8e24 commit 62548e6
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions beacon/connections/omopcdm/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
from beacon.request.parameters import AlphanumericFilter, OntologyFilter
from beacon.connections.omopcdm.utils import peek
from beacon.connections.omopcdm.__init__ import client
from beacon.connections.omopcdm.utils import get_datasetIds
from beacon.logs.logs import log_with_args, LOG
from beacon.conf.conf import level
from beacon.connections.mongo.__init__ import client as mongoclient

import aiosql
from pathlib import Path

Expand All @@ -14,7 +17,7 @@
filter_queries = aiosql.from_path(queries_file, "psycopg2")

@log_with_args(level)
def apply_filters(self, filtersGet: dict, filtersPost: List[dict], scope: str, granularity : str, limit : int = 10, skip : int = 0) -> dict:
def apply_filters(self, filtersGet: dict, filtersPost: List[dict], scope: str, granularity : str, dataset: str, limit : int = 10, skip : int = 0) -> dict:
# Get and Post Filters as dict
filters = []
if filtersGet:
Expand All @@ -30,6 +33,28 @@ def apply_filters(self, filtersGet: dict, filtersPost: List[dict], scope: str, g

# Create query
query = ''

# Create final SQL queries per scope
if scope == "biosamples":
column = "specimen_id"
table = "specimen"
collectionId = "biosampleId"

else: # Individuals
column = "person_id"
table = "person"
collectionId = "individualId"


targetIds, _ = get_datasetIds(self,
mongoclient.beacon.datasetsToId,
dataset,
collectionId,
skip,
limit
)
# Filter by Ids of the dataset
query += f"and {column} in {tuple(targetIds)}"
for filter in filters:
# Alphanumeric filter
if "value" in filter:
Expand All @@ -47,15 +72,6 @@ def apply_filters(self, filtersGet: dict, filtersPost: List[dict], scope: str, g
return query
query += partial_query

# Create final SQL queries per scope
if scope == "biosamples":
column = "specimen_id"
table = "specimen"

else: # Individuals
column = "person_id"
table = "person"

# Query different on granularity
if granularity == "boolean":
finalQuery = f"""
Expand All @@ -82,6 +98,7 @@ def apply_filters(self, filtersGet: dict, filtersPost: List[dict], scope: str, g
limit {limit}
offset {skip};
"""
LOG.debug(finalQuery)
return finalQuery

def search_descendants(concept_id: str) -> list:
Expand Down

0 comments on commit 62548e6

Please sign in to comment.