Skip to content

Commit

Permalink
Refactored annotation_task to use annotation_units. Updated tests and…
Browse files Browse the repository at this point in the history
… linted. Should be passing.
  • Loading branch information
fatimarabab committed Jul 1, 2024
1 parent 7d9bf4c commit db29916
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 18 deletions.
2 changes: 1 addition & 1 deletion backend/src/core/annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def process_tasks(annotation_queue, genomic_unit_collection): # pylint: disable

continue

task = AnnotationTaskFactory.create(annotation_unit.genomic_unit, annotation_unit.dataset)
task = AnnotationTaskFactory.create(annotation_unit)
logger.info('%s Creating Task To Annotate...', format_annotation_logging(annotation_unit))

annotation_task_futures[executor.submit(task.annotate)] = (annotation_unit.genomic_unit, task)
Expand Down
32 changes: 17 additions & 15 deletions backend/src/core/annotation_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import jq
import requests

from ..core.annotation_unit import AnnotationUnit

# create logger
logger = logging.getLogger(__name__)

Expand All @@ -26,9 +28,9 @@ def empty_gen():
class AnnotationTaskInterface:
"""Abstract class to define the interface for the the types of Annotation Task"""

def __init__(self, genomic_unit_json: dict):
def __init__(self, genomic_unit: dict):
self.dataset = {}
self.genomic_unit = genomic_unit_json
self.genomic_unit = genomic_unit

def set(self, dataset: dict):
"""Adds the dataset configuration for the annotation"""
Expand Down Expand Up @@ -110,9 +112,9 @@ class ForgeAnnotationTask(AnnotationTaskInterface):
annotation depedencies and its genomic unit
"""

def __init__(self, genomic_unit_json):
def __init__(self, genomic_unit):
"""Instantiates the force annotation task with the genomic unit's json"""
AnnotationTaskInterface.__init__(self, genomic_unit_json)
AnnotationTaskInterface.__init__(self, genomic_unit)

def annotate(self):
"""
Expand All @@ -126,9 +128,9 @@ def annotate(self):
class NoneAnnotationTask(AnnotationTaskInterface):
"""An empty annotation task to be a place holder for datasets that do not have an annotation type yet"""

def __init__(self, genomic_unit_json):
def __init__(self, genomic_unit):
"""Instantiates the annotation task for the fake annotation with a genomic unit"""
AnnotationTaskInterface.__init__(self, genomic_unit_json)
AnnotationTaskInterface.__init__(self, genomic_unit)

def annotate(self):
"""Creates a fake 'annotation' using a randomly generated pause time to a query io operation"""
Expand All @@ -144,9 +146,9 @@ def annotate(self):
class CsvAnnotationTask(AnnotationTaskInterface):
"""Example placeholder for a future type of annotation task"""

def __init__(self, genomic_unit_json):
def __init__(self, genomic_unit):
"""Insantiates the annotation task associated with the genomic unit"""
AnnotationTaskInterface.__init__(self, genomic_unit_json)
AnnotationTaskInterface.__init__(self, genomic_unit)

def annotate(self):
"""placeholder for annotating a genomic unit"""
Expand All @@ -156,9 +158,9 @@ def annotate(self):
class HttpAnnotationTask(AnnotationTaskInterface):
"""Initializes the annotation that uses an HTTP request to fetch the annotation"""

def __init__(self, genomic_unit_json):
def __init__(self, genomic_unit):
"""initializes the task with the genomic_unit"""
AnnotationTaskInterface.__init__(self, genomic_unit_json)
AnnotationTaskInterface.__init__(self, genomic_unit)

def annotate(self):
"""builds the complete url and fetches the annotation with an http request"""
Expand Down Expand Up @@ -191,9 +193,9 @@ def build_url(self):
class VersionAnnotationTask(AnnotationTaskInterface):
"""An annotation task that gets the version of the annotation"""

def __init__(self, genomic_unit_json):
def __init__(self, genomic_unit):
"""initializes the task with the genomic_unit"""
AnnotationTaskInterface.__init__(self, genomic_unit_json)
AnnotationTaskInterface.__init__(self, genomic_unit)

def annotate(self):
"""placeholder for annotating a genomic unit with version"""
Expand Down Expand Up @@ -250,13 +252,13 @@ def register(cls, key: str, annotation_task_interface: AnnotationTaskInterface):
cls.tasks[key] = annotation_task_interface

@classmethod
def create(cls, genomic_unit_json: dict, dataset: dict):
def create(cls, annotation_unit: AnnotationUnit):
"""
Creates an annotation task with a genomic_units and dataset json. Instantiates the class according to
a datasets 'annotation_source_type' from the datasets configurtion.
"""
# In the future, this could be modified to use a static function instead
# and those would be set to the dict, or an additional dictionary
new_task = cls.tasks[dataset["annotation_source_type"]](genomic_unit_json)
new_task.set(dataset)
new_task = cls.tasks[annotation_unit.dataset["annotation_source_type"]](annotation_unit.genomic_unit)
new_task.set(annotation_unit.dataset)
return new_task
14 changes: 12 additions & 2 deletions backend/tests/unit/core/test_annotation_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from src.core.annotation_task import AnnotationTaskFactory, ForgeAnnotationTask, HttpAnnotationTask
from src.enums import GenomicUnitType
from src.core.annotation_unit import AnnotationUnit


def test_http_annotation_base_url(http_annotation_transcript_id):
Expand All @@ -24,9 +25,9 @@ def test_http_annotation_task_build_url_with_dependency(http_annotation_task_gen
assert actual == "https://hpo.jax.org/api/hpo/gene/45614"


def test_annotation_task_create_http_task(hgvs_variant_genomic_unit, transcript_id_dataset):
def test_annotation_task_create_http_task(hgvs_variant_annotation_unit):
"""Verifies that the annotation task factory creates the correct annotation task according to the dataset type"""
actual_task = AnnotationTaskFactory.create(hgvs_variant_genomic_unit, transcript_id_dataset)
actual_task = AnnotationTaskFactory.create(hgvs_variant_annotation_unit)
assert isinstance(actual_task, HttpAnnotationTask)


Expand Down Expand Up @@ -189,6 +190,15 @@ def fixture_transcript_id_dataset():
}


@pytest.fixture(name="hgvs_variant_annotation_unit")
def fixture_hgvs_variant_annotation_unit(hgvs_variant_genomic_unit, transcript_id_dataset):
"""
Returns the annotation unit with hgvs_variant genomic unit and transcript_id dataset
"""
annotation_unit = AnnotationUnit(hgvs_variant_genomic_unit, transcript_id_dataset)
return annotation_unit


@pytest.fixture(name="http_annotation_transcript_id")
def fixture_http_annotation_transcript_id(hgvs_variant_genomic_unit, transcript_id_dataset):
"""An HTTP annotation task with a single dataset"""
Expand Down

0 comments on commit db29916

Please sign in to comment.