Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions web/handlers/v2/genomics/lineage_mutations.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
import pandas as pd

from web.handlers.genomics.base import BaseHandler
from web.handlers.genomics.util import create_nested_mutation_query, get_total_hits
from web.handlers.genomics.util import (
create_date_range_filter,
create_nested_mutation_query,
get_total_hits,
parse_time_window_to_query,
)


class LineageMutationsHandler(BaseHandler):
Expand All @@ -28,6 +33,8 @@ class LineageMutationsHandler(BaseHandler):
"pangolin_lineage": {"type": str, "required": True},
"frequency": {"type": float, "default": 0.8, "min": 0, "max": 1},
"gene": {"type": str, "default": None},
"min_date": {"type": str, "default": None, "date_format": "%Y-%m-%d"},
"max_date": {"type": str, "default": None, "date_format": "%Y-%m-%d"},
}

async def _get(self):
Expand All @@ -38,6 +45,11 @@ async def _get(self):
genes = gene.lower().split(",")
else:
genes = []

date_range_filter = create_date_range_filter(
"date_collected", self.args.min_date, self.args.max_date
)

dict_response = {}
# Query structure: Lineage 1 OR Lineage 2 OR Lineage 3 AND Mutation 1 AND Mutation 2, Lineage 4 AND Mutation 2, Lineage 5 ....
for query_lineage in pangolin_lineage.split(","):
Expand Down Expand Up @@ -66,7 +78,7 @@ async def _get(self):
query["query"] = create_nested_mutation_query(
lineages=query_pangolin_lineage, mutations=query_mutations
)
# print(query)
parse_time_window_to_query(date_range_filter, query["query"])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current behavior looks like the date range filter isn't operating. I think the problem might be that we need to change this line to update the query with the new time window being calculated -- something like query["query"] = parse_time_window... maybe?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking into the range filter created here:

When making the request to the endpoint below for test purposes:

  • /genomics/lineage-mutations?pangolin_lineage=BA.2&frequency=0&gene=ORF1a,ORF1b,S,ORF7b&min_date=2022-01-12&max_date=2022-10-22

The query created is below. The date range was added date_collected.

{
    "size": 0,
    "query": {
        "bool": {
            "should": [
                {
                    "bool": {
                        "must": [
                            {
                                "term": {
                                    "pangolin_lineage": "BA.2"
                                }
                            },
                            {
                                "range": {
                                    "date_collected": {
                                        "lte": "2022-10-22",
                                        "gte": "2022-01-12"
                                    }
                                }
                            }
                        ]
                    }
                }
            ]
        }
    },
    "aggs": {
        "mutations": {
            "nested": {
                "path": "mutations"
            },
            "aggs": {
                "mutations": {
                    "terms": {
                        "field": "mutations.mutation",
                        "size": 10000
                    },
                    "aggs": {
                        "genomes": {
                            "reverse_nested": {}
                        }
                    }
                }
            }
        }
    }
}

The query dict is mutable and passed by reference when calling the function below:

parse_time_window_to_query(date_range_filter, query["query"])

Does it make sense?

resp = await self.asynchronous_fetch(query)
path_to_results = ["aggregations", "mutations", "mutations", "buckets"]
buckets = resp
Expand Down