Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
gregorjerse committed May 21, 2024
1 parent 0add540 commit 8b3c32d
Show file tree
Hide file tree
Showing 6 changed files with 235 additions and 110 deletions.
22 changes: 6 additions & 16 deletions resolwe_bio/variants/listener_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
VariantAnnotationTranscript,
VariantCall,
VariantExperiment,
VariantPrimaryKey,
)

if TYPE_CHECKING:
Expand Down Expand Up @@ -116,14 +117,7 @@ def add_variants(
# 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": variant_data["species"],
"genome_assembly": variant_data["genome_assembly"],
"chromosome": variant_data["chromosome"],
"position": variant_data["position"],
"reference": variant_data["reference"],
"alternative": variant_data["alternative"],
}
key = {key: variant_data[key] for key in VariantPrimaryKey}
# To reduce the hits to the database use cache for variants.
key_tuple = tuple(key.values())
if key_tuple not in variant_cache:
Expand All @@ -136,8 +130,11 @@ def add_variants(
data=data,
sample=sample,
quality=variant_data.get("quality"),
depth_norm_quality=variant_data.get("depth_norm_quality"),
unfiltered_allele_depth=variant_data.get("unfiltered_allele_depth"),
depth=variant_data.get("depth"),
genotype=variant_data.get("genotype"),
genotype_quality=variant_data.get("genotype_quality"),
filter=variant_data.get("filter"),
experiment=experiment,
)
Expand All @@ -161,14 +158,7 @@ def add_variants_annotations(

for annotation_data in typed_data:

key = {
"species": annotation_data["species"],
"genome_assembly": annotation_data["genome_assembly"],
"chromosome": annotation_data["chromosome"],
"position": annotation_data["position"],
"reference": annotation_data["reference"],
"alternative": annotation_data["alternative"],
}
key = {key: annotation_data[key] for key in VariantPrimaryKey}
# To reduce the hits to the database use cache for variants.
key_tuple = tuple(key.values())
if key_tuple not in variant_cache:
Expand Down
10 changes: 5 additions & 5 deletions resolwe_bio/variants/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 4.2.13 on 2024-05-16 11:48
# Generated by Django 4.2.11 on 2024-05-21 11:58

from django.conf import settings
import django.contrib.postgres.fields
Expand All @@ -11,8 +11,8 @@ class Migration(migrations.Migration):
initial = True

dependencies = [
("flow", "0026_create_unaccent_full_text_search_config"),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
("flow", "0024_update_sql_entity_methods"),
]

operations = [
Expand Down Expand Up @@ -134,7 +134,7 @@ class Migration(migrations.Migration):
verbose_name="ID",
),
),
("quality", models.FloatField()),
("quality", models.FloatField(blank=True, null=True)),
("depth_norm_quality", models.FloatField(blank=True, null=True)),
(
"unfiltered_allele_depth",
Expand All @@ -146,7 +146,7 @@ class Migration(migrations.Migration):
"genotype_quality",
models.PositiveIntegerField(blank=True, null=True),
),
("filter", models.CharField(max_length=20)),
("filter", models.CharField(blank=True, max_length=20, null=True)),
(
"data",
models.ForeignKey(
Expand Down Expand Up @@ -218,7 +218,7 @@ class Migration(migrations.Migration):
(
"variant_annotation",
models.ForeignKey(
on_delete=django.db.models.deletion.PROTECT,
on_delete=django.db.models.deletion.CASCADE,
related_name="transcripts",
to="resolwe_bio_variants.variantannotation",
),
Expand Down
23 changes: 13 additions & 10 deletions resolwe_bio/variants/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from resolwe.auditlog.models import AuditModel
from resolwe.flow.models import Data
from resolwe.flow.models import Entity as Sample
from resolwe.permissions.models import PermissionGroup, PermissionInterface
from resolwe.permissions.models import PermissionInterface

# TODO: sync with the kb, at least the first entry.
SPECIES_MAX_LENGTH = 50
Expand All @@ -38,6 +38,15 @@
FEATURE_ID_MAX_LENGTH = 200
FILTER_MAX_LENGTH = 20

VariantPrimaryKey = (
"species",
"genome_assembly",
"chromosome",
"position",
"reference",
"alternative",
)


class Variant(AuditModel):
"""Describe a variant in the database."""
Expand Down Expand Up @@ -70,14 +79,7 @@ class Meta:

constraints = [
models.UniqueConstraint(
fields=[
"species",
"genome_assembly",
"chromosome",
"position",
"reference",
"alternative",
],
fields=VariantPrimaryKey,
name="uniq_composite_key_variants",
),
]
Expand Down Expand Up @@ -177,7 +179,8 @@ class VariantExperiment(AuditModel):
class VariantCall(AuditModel, PermissionInterface):
"""VariantCall object."""

def permission_proxy(self) -> str:
@classmethod
def permission_proxy(cls) -> str:
"""Return the permission proxy name."""
return "sample"

Expand Down
39 changes: 31 additions & 8 deletions resolwe_bio/variants/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@

from rest_framework import serializers

from resolwe.flow.serializers.fields import DictRelatedField
from resolwe.rest.serializers import SelectiveFieldMixin

from resolwe_bio.variants.models import (
Variant,
VariantAnnotation,
VariantAnnotationTranscript,
VariantCall,
VariantExperiment,
)
Expand All @@ -37,9 +39,36 @@ class Meta:
]


class VariantTranscriptSerializer(SelectiveFieldMixin, serializers.ModelSerializer):
"""Serializer for VariantAnnotationTranscript objects."""

class Meta:
"""Serializer configuration."""

model = VariantAnnotationTranscript
fields = [
"id",
"variant_annotation_id",
"annotation",
"annotation_impact",
"gene",
"protein_impact",
"transcript_ids",
"canonical",
]


class VariantAnnotationSerializer(SelectiveFieldMixin, serializers.ModelSerializer):
"""Serializer for VariantAnnotation objects."""

transcripts = DictRelatedField(
queryset=VariantAnnotationTranscript.objects.all(),
serializer=VariantTranscriptSerializer,
allow_null=True,
required=False,
many=True,
)

class Meta:
"""Serializer configuration."""

Expand All @@ -48,17 +77,11 @@ class Meta:
"id",
"variant_id",
"type",
"transcripts__annotation",
"transcripts__annotation_impact",
"transcripts__gene",
"transcripts__protein_impact",
"transcripts__transcript_ids",
"transcripts__canonical",
"clinical_diagnosis",
"clinical_significance",
"dbsnp_id",
"clinvar_id",
"data_id",
"transcripts",
]


Expand Down Expand Up @@ -89,4 +112,4 @@ class Meta:
"""Serializer configuration."""

model = VariantExperiment
fields = ["id", "date", "contributor", "variant_data_source"]
fields = ["id", "timestamp", "contributor", "variant_data_source"]
Loading

0 comments on commit 8b3c32d

Please sign in to comment.