Skip to content

Commit

Permalink
Add variants application
Browse files Browse the repository at this point in the history
  • Loading branch information
gregorjerse committed Apr 15, 2024
1 parent d599238 commit 2787170
Show file tree
Hide file tree
Showing 12 changed files with 1,652 additions and 156 deletions.
146 changes: 1 addition & 145 deletions resolwe_bio/tests/unit/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from rest_framework import status
from rest_framework.test import APIRequestFactory, force_authenticate

from resolwe.flow.models import Collection, Data, DescriptorSchema, Entity, Process
from resolwe.flow.models import Collection, Data, Entity, Process
from resolwe.flow.views import CollectionViewSet, DataViewSet, EntityViewSet
from resolwe.test import ProcessTestCase, TestCase

Expand Down Expand Up @@ -50,41 +50,6 @@ def setUpTestData(cls):
last_name="Miller",
)

cls.descriptor_schema = DescriptorSchema.objects.create(
slug="test-schema",
version="1.0.0",
contributor=cls.contributor,
schema=[
{
"name": "general",
"group": [
{"name": "species", "type": "basic:string:", "required": False},
{"name": "organ", "type": "basic:string:", "required": False},
{
"name": "biosample_treatment",
"type": "basic:string:",
"required": False,
},
{
"name": "biosample_source",
"type": "basic:string:",
"required": False,
},
],
},
{
"name": "response_and_survival_analysis",
"group": [
{
"name": "confirmed_bor",
"type": "basic:string:",
"required": False,
}
],
},
],
)

cls.collections = [
Collection.objects.create(
contributor=cls.contributor,
Expand All @@ -101,58 +66,21 @@ def setUpTestData(cls):
name="Test entity 0",
collection=cls.collections[0],
contributor=cls.contributor,
descriptor_schema=cls.descriptor_schema,
descriptor={
"general": {
"species": "Homo sapiens",
"organ": "CRC",
"biosample_source": "CRC",
"biosample_treatment": "koh",
},
"response_and_survival_analysis": {
"confirmed_bor": "pd",
},
},
),
Entity.objects.create(
name="Test entity 1",
collection=cls.collections[1],
contributor=cls.contributor,
descriptor_schema=cls.descriptor_schema,
descriptor={
"general": {"species": "Homo sapiens", "organ": "CRC"},
"response_and_survival_analysis": {
"confirmed_bor": "sd",
},
},
),
Entity.objects.create(
name="Test entity 2",
collection=cls.collections[0],
contributor=cls.contributor,
descriptor_schema=cls.descriptor_schema,
descriptor={
"general": {
"species": "Mus musculus",
"organ": "CRC",
"biosample_treatment": "dmso",
},
},
),
Entity.objects.create(
name="Test entity 3",
collection=cls.collections[2],
contributor=cls.contributor,
descriptor_schema=cls.descriptor_schema,
descriptor={
"general": {
"species": "Mus musculus",
"biosample_treatment": "dmso",
},
"response_and_survival_analysis": {
"confirmed_bor": "pd",
},
},
),
]
cls.collections[0].save()
Expand Down Expand Up @@ -205,7 +133,6 @@ def setUp(self):
version="1.0.0",
contributor=self.contributor,
entity_type="test-schema",
entity_descriptor_schema="test-schema",
input_schema=[
{"name": "input_data", "type": "data:test:", "required": False}
],
Expand All @@ -217,12 +144,6 @@ def setUp(self):
],
)

self.descriptor_schema = DescriptorSchema.objects.create(
slug="test-schema",
version="1.0.0",
contributor=self.contributor,
)

self.data = []
for index in range(10):
data = Data.objects.create(
Expand Down Expand Up @@ -288,87 +209,22 @@ def setUp(self):
}
)

clinical_schema = DescriptorSchema.objects.get(slug="general-clinical")
sample_schema = DescriptorSchema.objects.get(slug="sample")

self.entities = [
Entity.objects.create(
name="Test entity 1",
contributor=self.contributor,
descriptor_schema=clinical_schema,
descriptor={
"general": {"species": "Homo sapiens"},
"disease_information": {
"disease_type": "Colorectal cancer",
"disease_status": "Progresive",
},
"subject_information": {
"batch": 1,
"group": "Pre",
"subject_id": "P-006",
"sample_label": "CRC",
},
"immuno_oncology_treatment_type": {
"io_drug": "D-00A",
"io_treatment": "single",
},
"response_and_survival_analysis": {
"pfs_event": "no",
"confirmed_bor": "pd",
},
},
),
Entity.objects.create(
name="Test entity 2",
contributor=self.contributor,
descriptor_schema=clinical_schema,
descriptor={
"general": {"species": "Homo sapiens"},
"disease_information": {
"disease_type": "Mesothelioma",
"disease_status": "Regresive",
},
"subject_information": {
"batch": 2,
"group": "Post",
"subject_id": "P-019",
"sample_label": "Meso",
},
"immuno_oncology_treatment_type": {
"io_drug": "D-12A",
"io_treatment": "combo",
},
"response_and_survival_analysis": {
"pfs_event": "yes",
"confirmed_bor": "sd",
},
},
),
Entity.objects.create(
name="Test entity 3",
contributor=self.contributor,
descriptor_schema=sample_schema,
descriptor={
"general": {
"species": "Mus musculus",
"description": "First sample",
"biosample_source": "lung",
"biosample_treatment": "dmso",
}
},
),
Entity.objects.create(
name="Test entity 4",
contributor=self.contributor,
descriptor_schema=sample_schema,
descriptor={
"general": {
"species": "Mus musculus",
"description": "Second sample",
"biosample_source": "liver",
"biosample_treatment": "koh",
}
},
),
]

Expand Down
7 changes: 7 additions & 0 deletions resolwe_bio/variants/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
""".. Ignore pydocstyle D400.
===================================
Resolwe Bioinformatics Variants App
===================================
"""
16 changes: 16 additions & 0 deletions resolwe_bio/variants/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
""".. Ignore pydocstyle D400.
===============================
Variants Base App Configuration
===============================
"""
from django.apps import AppConfig


class VariantsConfig(AppConfig):
"""App configuration."""

name = "resolwe_bio.variants"
label = "resolwe_bio_variants"
verbose_name = "Resolwe Bioinformatics Variants Base"
75 changes: 75 additions & 0 deletions resolwe_bio/variants/listener_plugin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
"""Handle variants related commands."""

import logging
from typing import TYPE_CHECKING

from resolwe.flow.executors.socket_utils import Message, Response
from resolwe.flow.managers.listener.plugin import (
ListenerPlugin,
listener_plugin_manager,
)

from .models import Variant, VariantCall, VariantExperiment

if TYPE_CHECKING:
from resolwe.flow.managers.listener.listener import Processor

logger = logging.getLogger(__name__)


class VariantCommands(ListenerPlugin):
"""Listener handlers related to the variants application."""

plugin_manager = listener_plugin_manager

def add_variants(
self, data_id: int, message: Message[dict], manager: "Processor"
) -> Response[int]:
"""Handle connecting variants with the samples.
If the reported variant does not exist in the file it is created.
"""
data = manager.data(data_id)
sample = data.entity
metadata, variants_data = message.message_data
species, genome_assembly = metadata["species"], metadata["genome_assembly"]

variant_calls = list()
variant_cache = dict()
experiment = VariantExperiment.objects.create(
variant_data_source=metadata["variant_data_source"],
contributor=data.contributor,
)

# Bulk create variants. The consequesce of ignore_conflicts flag is that the
# database does not returt the ids of the created objects. So first create all
# the variants and then create the variant calls.
for variant_data in variants_data:
key = {
"species": species,
"genome_assembly": genome_assembly,
"chromosome": variant_data["chromosome"],
"position": variant_data["position"],
"reference": variant_data["reference"],
"alternative": variant_data["alternative"],
}
# To reduce the hits to the database use cache for variants.
key_tuple = tuple(key.values())
if key_tuple not in variant_cache:
variant_cache[key_tuple] = Variant.objects.get_or_create(**key)[0]
variant = variant_cache[key_tuple]

variant_calls.append(
VariantCall(
variant=variant,
data=data,
sample=sample,
quality=variant_data["quality"],
depth=variant_data["depth"],
genotype=variant_data["genotype"],
filter=variant_data["filter"],
experiment=experiment,
)
)

VariantCall.objects.bulk_create(variant_calls)
Loading

0 comments on commit 2787170

Please sign in to comment.