Skip to content

Commit

Permalink
Merge ef62950 into 9d8fa3c
Browse files Browse the repository at this point in the history
  • Loading branch information
holtgrewe committed Mar 28, 2022
2 parents 9d8fa3c + ef62950 commit c79ac4b
Show file tree
Hide file tree
Showing 10 changed files with 318 additions and 120 deletions.
1 change: 1 addition & 0 deletions HISTORY.rst
Expand Up @@ -23,6 +23,7 @@ Full Change List
- Strip trailing slashes in beconsite entrypoints (#388)
- Documenting PAP setup (#393)
- Adding more indices (#395)
- Fixing discrepancy with REST API query shortcuts (#402)

------
v1.1.0
Expand Down
4 changes: 2 additions & 2 deletions docs_manual/developer_development.rst
Expand Up @@ -63,7 +63,7 @@ To do so, go to the `VarFish repository <https://github.com/bihealth/varfish-ser
Update Main
===========

`Pull with rebase on gitready <https://gitready.com/advanced/2009/02/11/pull-with-rebase.html>`_
`Pull with rebase on gitready <https://gitready.com/advanced/2009/02/11/pull-with-rebase.html>`__

.. code-block:: bash
Expand Down Expand Up @@ -103,7 +103,7 @@ We suggest to first squash your commits and then do a rebase to the main branch.
Squash Multiple Commits (Or Use Amend)
--------------------------------------

`Pull with rebase on gitready <https://gitready.com/advanced/2009/02/10/squashing-commits-with-rebase.html>`_
`Pull with rebase on gitready <https://gitready.com/advanced/2009/02/10/squashing-commits-with-rebase.html>`__

We prefer to have only one commit per feature (most of the time there is only one feature per branch).
When your branch is rebased on the main branch, do:
Expand Down
22 changes: 21 additions & 1 deletion variants/queries.py
Expand Up @@ -1012,14 +1012,28 @@ def _build_gene_sub_query(self, hgnc_field, gene_list):
)


def normalize_chrom(chrom, release):
"""Normalize chromosome for the given ``release``."""
while chrom.startswith("chr"):
chrom = chrom[len("chr") :]
if chrom in ("M", "MT"):
if release == "GRCh37":
return "MT"
else:
return "chrM"
else:
return f"chr{chrom}" if release != "GRCh37" else chrom


class ExtendQueryPartsGenomicRegionFilter(ExtendQueryPartsBase):
def extend_conditions(self, _query_parts):
case = self._case_for_release()
if self.kwargs["genomic_region"]:
return [
or_(
*[
and_(
SmallVariant.sa.chromosome == chrom,
SmallVariant.sa.chromosome == normalize_chrom(chrom, case.release),
(SmallVariant.sa.start >= start) if start else True,
(SmallVariant.sa.start <= end) if end else True,
)
Expand All @@ -1029,6 +1043,12 @@ def extend_conditions(self, _query_parts):
]
return []

def _case_for_release(self):
if hasattr(self, "cases"):
return self.cases[0]
else:
return self.case


class ExtendQueryPartsLoadPrefetchedBase(ExtendQueryPartsBase):
def __init__(self, *args, **kwargs):
Expand Down
24 changes: 12 additions & 12 deletions variants/query_presets.py
Expand Up @@ -576,15 +576,17 @@ class _ImpactPresets:
"transcripts_coding": True,
"transcripts_noncoding": False,
"effects": (
"3_prime_UTR_exon_variant",
"3_prime_UTR_intron_variant",
"5_prime_UTR_exon_variant",
"5_prime_UTR_intron_variant",
"coding_transcript_intron_variant",
"complex_substitution",
"direct_tandem_duplication",
"disruptive_inframe_deletion",
"disruptive_inframe_insertion",
"exon_loss_variant",
"feature_truncation",
"five_prime_UTR_exon_variant",
"five_prime_UTR_intron_variant",
"frameshift_elongation",
"frameshift_truncation",
"frameshift_variant",
Expand All @@ -602,8 +604,6 @@ class _ImpactPresets:
"stop_retained_variant",
"structural_variant",
"synonymous_variant",
"three_prime_UTR_exon_variant",
"three_prime_UTR_intron_variant",
"transcript_ablation",
),
}
Expand All @@ -615,6 +615,10 @@ class _ImpactPresets:
"transcripts_coding": True,
"transcripts_noncoding": True,
"effects": (
"3_prime_UTR_exon_variant",
"3_prime_UTR_intron_variant",
"5_prime_UTR_exon_variant",
"5_prime_UTR_intron_variant",
"coding_transcript_intron_variant",
"complex_substitution",
"direct_tandem_duplication",
Expand All @@ -623,8 +627,6 @@ class _ImpactPresets:
"downstream_gene_variant",
"exon_loss_variant",
"feature_truncation",
"five_prime_UTR_exon_variant",
"five_prime_UTR_intron_variant",
"frameshift_elongation",
"frameshift_truncation",
"frameshift_variant",
Expand All @@ -645,8 +647,6 @@ class _ImpactPresets:
"stop_retained_variant",
"structural_variant",
"synonymous_variant",
"three_prime_UTR_exon_variant",
"three_prime_UTR_intron_variant",
"transcript_ablation",
"upstream_gene_variant",
),
Expand Down Expand Up @@ -773,25 +773,25 @@ class _ChromosomePresets:
}
#: Presets for the "autosomes" chromosome/region/gene settings
autosomes: typing.Dict[str, typing.Any] = {
"genomic_region": tuple(f"chr{num}" for num in range(1, 23)),
"genomic_region": tuple(f"{num}" for num in range(1, 23)),
"gene_allowlist": (),
"gene_blocklist": (),
}
#: Presets for the "X-chromosome" chromosome/region/gene settings
x_chromosome: typing.Dict[str, typing.Any] = {
"genomic_region": ("chrX",),
"genomic_region": ("X",),
"gene_allowlist": (),
"gene_blocklist": (),
}
#: Presets for the "Y-chromosomes" chromosome/region/gene settings
y_chromosome: typing.Dict[str, typing.Any] = {
"genomic_region": ("chrY",),
"genomic_region": ("Y",),
"gene_allowlist": (),
"gene_blocklist": (),
}
#: Presets for the "mitochondrial" chromosome/region/gene settings
mt_chromosome: typing.Dict[str, typing.Any] = {
"genomic_region": ("chrMT",),
"genomic_region": ("MT",),
"gene_allowlist": (),
"gene_blocklist": (),
}
Expand Down
39 changes: 38 additions & 1 deletion variants/query_schemas.py
Expand Up @@ -4,9 +4,11 @@
import copy
import os.path
import json
import re
import typing

import attr
import attrs
import cattr
from jsonschema import Draft7Validator, validators

Expand Down Expand Up @@ -136,6 +138,18 @@ class GenomicRegionV1:
chromosome: str
range: typing.Optional[RangeV1] = None

def with_chr_stripped(self):
chromosome = self.chromosome
if chromosome.startswith("chr"):
chromosome = chromosome[len("chr") :]
return GenomicRegionV1(chromosome, self.range)

def to_str(self):
if not self.range:
return self.chromosome
else:
return "%s:%d-%d" % (self.chromosome, self.range.start, self.range.end)


def convert_genomic_region_v1(region: GenomicRegionV1):
if region.range:
Expand Down Expand Up @@ -265,7 +279,7 @@ class CaseQueryV1:
helixmtdb_hom_count: typing.Optional[int] = None

mitomap_count: typing.Optional[int] = None
mitmomap_frequency: typing.Optional[float] = None
mitomap_frequency: typing.Optional[float] = None


class QueryJsonToFormConverter:
Expand Down Expand Up @@ -296,6 +310,7 @@ def convert(self, case: Case, query: CaseQueryV1) -> typing.Dict[str, typing.Any
"inhouse_enabled": query.inhouse_enabled,
"inhouse_carriers": query.inhouse_carriers,
"inhouse_heterozygous": query.inhouse_heterozygous,
"inhouse_homozygous": query.inhouse_homozygous,
"mtdb_enabled": query.mtdb_enabled,
"mtdb_count": query.mtdb_count,
"mtdb_frequency": query.mtdb_frequency,
Expand Down Expand Up @@ -420,4 +435,26 @@ def convert_query_json_to_small_variant_filter_form_v1(
tmp = copy.deepcopy(query_json)
DefaultValidatingDraft7Validator(SCHEMA_QUERY_V1).validate(tmp)
query = cattr.structure(tmp, CaseQueryV1)
if query.genomic_region:
query = attrs.evolve(
query,
genomic_region=list(map(GenomicRegionV1.with_chr_stripped, query.genomic_region)),
)
return QueryJsonToFormConverter().convert(case, query)


def _structure_genomic_region(s, _):
if not re.match("^[a-zA-Z0-9]+(:(\\d+(,\\d+)*)-(\\d+(,\\d+)*))?$", s):
raise RuntimeError("Invalid genomic region string: %s" % repr(s))
if ":" in s:
chrom, range = s.split(":")
start, end = range.split("-")
return GenomicRegionV1(chromosome=chrom, range=RangeV1(int(start), int(end)))
else:
return GenomicRegionV1(chromosome=s)


cattr.register_structure_hook(GenomicRegionV1, _structure_genomic_region)


cattr.register_unstructure_hook(GenomicRegionV1, GenomicRegionV1.to_str)
1 change: 1 addition & 0 deletions variants/tests/factories.py
Expand Up @@ -653,6 +653,7 @@ class Meta:


CHROMOSOME_MAPPING = {str(chrom): i + 1 for i, chrom in enumerate(list(range(1, 23)) + ["X", "Y"])}
CHROMOSOME_MAPPING.update({f"chr{chrom}": i for chrom, i in CHROMOSOME_MAPPING.items()})


class SmallVariantFactory(factory.django.DjangoModelFactory):
Expand Down

0 comments on commit c79ac4b

Please sign in to comment.