Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add variant related models #352

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions docs/CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Changed
Added
-----
- Add ``restart`` method to the ``Data`` resource
- Add variants related models

Fixed
-----
Expand Down
4 changes: 3 additions & 1 deletion src/resdk/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,9 @@ def _add_filter(self, filter_):
"""Add filtering parameters."""
for key, value in filter_.items():
# 'sample' is called 'entity' in the backend.
key = key.replace("sample", "entity")
if not key.startswith("variant_calls__"):
key = key.replace("sample", "entity")
print("Adding filter", key, value)
value = self._dehydrate_resources(value)
if self._non_string_iterable(value):
value = ",".join(map(str, value))
Expand Down
9 changes: 9 additions & 0 deletions src/resdk/resolwe.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@
Relation,
Sample,
User,
Variant,
VariantAnnotation,
VariantCall,
VariantExperiment,
)
from .resources.base import BaseResource
from .resources.kb import Feature, Mapping
Expand Down Expand Up @@ -114,6 +118,10 @@ class Resolwe:
resource_query_mapping = {
AnnotationField: "annotation_field",
AnnotationValue: "annotation_value",
Variant: "variant",
VariantAnnotation: "variant_annotation",
VariantExperiment: "variant_experiment",
VariantCall: "variant_calls",
Data: "data",
Collection: "collection",
Sample: "sample",
Expand All @@ -126,6 +134,7 @@ class Resolwe:
Mapping: "mapping",
Geneset: "geneset",
Metadata: "metadata",
Variant: "variant",
}
# Map ResolweQuery name to it's slug_field
slug_field_mapping = {
Expand Down
9 changes: 9 additions & 0 deletions src/resdk/resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@
:members:
:inherited-members:

.. autoclass:: resdk.resources.Variants
:members:
:inherited-members:

.. autoclass:: resdk.resources.User
:members:
:inherited-members:
Expand Down Expand Up @@ -102,6 +106,7 @@
from .relation import Relation
from .sample import Sample
from .user import Group, User
from .variants import Variant, VariantAnnotation, VariantCall, VariantExperiment

__all__ = (
"AnnotationField",
Expand All @@ -117,4 +122,8 @@
"Process",
"Relation",
"User",
"Variant",
"VariantAnnotation",
"VariantCall",
"VariantExperiment",
)
18 changes: 17 additions & 1 deletion src/resdk/resources/sample.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
"""Sample resource."""

import logging
from typing import TYPE_CHECKING, Any, Dict, Optional
from typing import TYPE_CHECKING, Any, Dict, List, Optional

from resdk.exceptions import ResolweServerError
from resdk.shortcuts.sample import SampleUtilsMixin

from ..utils.decorators import assert_object_exists
from .background_task import BackgroundTask
from .collection import BaseCollection, Collection
from .variants import Variant

if TYPE_CHECKING:
from .annotations import AnnotationValue
Expand Down Expand Up @@ -39,6 +40,8 @@ def __init__(self, resolwe, **model_data):
self._background = None
#: is this sample background to any other sample?
self._is_background = None
#: list of ``Variant`` objects attached to the sample
self._variants = None

super().__init__(resolwe, **model_data)

Expand All @@ -60,6 +63,19 @@ def data(self):

return self._data

@property
def variants(self):
"""Get variants."""
if self._variants is None:
self._variants = self.resolwe.variant.filter(variant_calls__sample=self.id)
return self._variants

def variants_by_experiment(self, experiment):
"""Get variants for sample detected by the given experiment."""
return self.resolwe.variant.filter(
variant_calls__sample=self.id, variant_calls__experiment=experiment.id
)

@property
def collection(self):
"""Get collection."""
Expand Down
154 changes: 154 additions & 0 deletions src/resdk/resources/variants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
"""Variant resources."""

from typing import Any

from .base import BaseResource


class Variant(BaseResource):
"""ResolweBio Variant resource."""

endpoint = "variant"

READ_ONLY_FIELDS = BaseResource.READ_ONLY_FIELDS + (
"species",
"genome_assembly",
"chromosome",
"position",
"reference",
"alternative",
)

def __init__(self, resolwe, **model_data):
"""Initialize object."""
super().__init__(resolwe, **model_data)
self._annotations = None
self._samples = None
self._calls = None

@property
def annotations(self):
"""Get the annotations for this variant."""
if self._annotations is None:
self._annotations = self.resolwe.variant_annotation.filter(variant=self.id)
return self._annotations

@property
def samples(self):
"""Get samples."""
if self._samples is None:
self._samples = self.resolwe.sample.filter(variant_calls__variant=self.id)
return self._samples

@property
def calls(self):
"""Get variant calls associated with this variant."""
if self._calls is None:
self._calls = self.resolwe.variant_calls.filter(variant=self.id)
return self._calls

def __repr__(self) -> str:
"""Return string representation."""
return (
f"Variant <chr: {self.chromosome}, pos: {self.position}, "
f"ref: {self.reference}, alt: {self.alternative}>"
)


class VariantAnnotation(BaseResource):
"""VariantAnnotation resource."""

endpoint = "variant_annotations"

READ_ONLY_FIELDS = BaseResource.READ_ONLY_FIELDS + (
"variant_id",
"type",
"clinical_diagnosis",
"clinical_significance",
"dbsnp_id",
"clinvar_id",
"data",
"transcripts",
)

def __repr__(self) -> str:
"""Return string representation."""
return f"VariantAnnotation <variant: {self.variant_id}>"


class VariantExperiment(BaseResource):
"""Variant experiment resource."""

endpoint = "variant_experiment"

READ_ONLY_FIELDS = BaseResource.READ_ONLY_FIELDS + (
"variant_data_source",
"timestamp",
"contributor",
)

def __repr__(self) -> str:
"""Return string representation."""
return f"VariantExperiment <pk: {self.id}>"


class VariantCall(BaseResource):
"""VariantCall resource."""

endpoint = "variant_calls"

READ_ONLY_FIELDS = BaseResource.READ_ONLY_FIELDS + (
"sample_id",
"variant_id",
"quality",
"depth_norm_quality",
"alternative_allele_depth",
"depth",
"genotype",
"genotype_quality",
"filter",
"data_id",
"experiment_id",
)

def __init__(self, resolwe, **model_data: Any):
"""Initialize object."""
super().__init__(resolwe, **model_data)
self._data = None
self._sample = None
self._experiment = None
self._variant = None

@property
def data(self):
"""Get the data object for this variant call."""
if self._data is None:
self._data = self.resolwe.data.get(self.data_id)
return self._data

@property
def sample(self):
"""Get the sample object for this variant call."""
if self._sample is None:
self._sample = self.resolwe.sample.get(self.sample_id)
return self._sample

@property
def experiment(self):
"""Get the experiment object for this variant call."""
if self._experiment is None:
self._experiment = self.resolwe.variant_experiment.get(
id=self.experiment_id
)
return self._experiment

@property
def variant(self):
"""Get the variant object for this variant call."""
if self._variant is None:
self._variant = self.resolwe.variant.get(id=self.variant_id)
return self._variant

def __repr__(self) -> str:
"""Return string representation."""
return f"VariantCall <pk: {self.id}>"
Loading