Skip to content

Commit

Permalink
#1217 - T2T - Classification grid dies due to no cached column - don'…
Browse files Browse the repository at this point in the history
…t try and look for it. Also fixed genome patch lookup
  • Loading branch information
davmlaw committed Dec 19, 2024
1 parent e9e81b6 commit c4a6919
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 30 deletions.
39 changes: 28 additions & 11 deletions classification/views/classification_datatables.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import operator
from functools import cached_property, reduce
from typing import Dict, Any, List, Optional
Expand Down Expand Up @@ -58,11 +59,14 @@ def render_c_hgvs(self, row: Dict[str, Any]) -> JsonDataType:
def get_preferred_chgvs_json() -> Dict:
nonlocal row
for index, genome_build in enumerate(self.genome_build_prefs):
if c_hgvs_string := row.get(ClassificationModification.column_name_for_build(genome_build)):
c_hgvs = CHGVS(c_hgvs_string)
c_hgvs.genome_build = genome_build
c_hgvs.is_desired_build = index == 0
return c_hgvs.to_json()
try:
if c_hgvs_string := row.get(ClassificationModification.column_name_for_build(genome_build)):
c_hgvs = CHGVS(c_hgvs_string)
c_hgvs.genome_build = genome_build
c_hgvs.is_desired_build = index == 0
return c_hgvs.to_json()
except ValueError:
pass

c_hgvs = CHGVS(row.get('published_evidence__c_hgvs__value'))
c_hgvs.is_normalised = False
Expand Down Expand Up @@ -133,7 +137,18 @@ def __init__(self, request: HttpRequest):
super().__init__(request)

user_settings = UserSettings.get_for_user(self.user)
genome_build_preferred = self.genome_build_prefs[0]
# Only GRCh37 and GRCh38 are currently supported. If user has T2T we are going to fall back
c_hgvs = None
genomic_sort = None
for genome_build in self.genome_build_prefs:
try:
c_hgvs = ClassificationModification.column_name_for_build(genome_build)
genomic_sort = ClassificationModification.column_name_for_build(genome_build, 'genomic_sort')
except ValueError:
pass
if not (c_hgvs and genomic_sort):
possible_builds = ', '.join((str(gb) for gb in self.genome_build_prefs))
raise ValueError(f"Couldn't find c_hgvs and genomic_sort in builds: {possible_builds}")

self.rich_columns = [
RichColumn(
Expand Down Expand Up @@ -164,9 +179,8 @@ def __init__(self, request: HttpRequest):
orderable=True
),
RichColumn(
key=ClassificationModification.column_name_for_build(genome_build_preferred),
# sort_keys=['variant_sort', 'c_hgvs'], # annotated column
sort_keys=[ClassificationModification.column_name_for_build(genome_build_preferred, 'genomic_sort'), 'c_hgvs'],
key=c_hgvs,
sort_keys=[genomic_sort, 'c_hgvs'],
name='c_hgvs',
label=f'HGVS ({user_settings.default_genome_build.name})',
renderer=self.render_c_hgvs,
Expand Down Expand Up @@ -284,8 +298,11 @@ def get_initial_queryset(self) -> QuerySet[ClassificationModification]:
# user's normalised preference (e.g. 37), the alternative normalised (e.g. 38) the imported c.hgvs
whens = []
for genome_build in self.genome_build_prefs:
column_name = ClassificationModification.column_name_for_build(genome_build)
whens.append(When(**{f'{column_name}__isnull': False, 'then': column_name}))
try:
column_name = ClassificationModification.column_name_for_build(genome_build)
whens.append(When(**{f'{column_name}__isnull': False, 'then': column_name}))
except ValueError as ve:
logging.warning(ve)
case = Case(*whens, default=KeyTextTransform('value', KeyTransform('c_hgvs', 'published_evidence')),
output_field=TextField())
initial_qs = initial_qs.annotate(c_hgvs=case)
Expand Down
44 changes: 25 additions & 19 deletions snpdb/models/models_genome.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,12 @@ class GenomeBuildPatchVersion(models.Model):
patch_version = models.IntegerField(blank=True, null=True)
""" The version of the patch, or None if the version is unknown, note this is different to an explicit patch_version of 0 """

GENOME_BUILD_VERSION_RE = re.compile(r"(?P<genome_build>[A-Za-z0-9]+)(?:[.]p(?P<patch_version>[0-9]+))?", re.IGNORECASE)
GENOME_BUILD_VERSION_REGEXES = [
# Standard eg GRCh37.p13 or GRCh38.p14
re.compile(r"(?P<genome_build>[A-Za-z0-9]+)(?:[.]p(?P<patch_version>[0-9]+))?", re.IGNORECASE),
# T2T-CHM13v2.0
re.compile(r"(?P<genome_build>[A-Za-z0-9-]+)(v(?P<patch_version>[0-9]+\.[0-9]+))", re.IGNORECASE),
]

class Meta:
unique_together = ('genome_build', 'patch_version')
Expand All @@ -316,24 +321,25 @@ def sort_key(obj: GenomeBuildPatchVersion):

@staticmethod
def get_or_create(name: str) -> 'GenomeBuildPatchVersion':
if match := GenomeBuildPatchVersion.GENOME_BUILD_VERSION_RE.match(name):
genome_build = GenomeBuild.get_name_or_alias(match.group('genome_build'))
patch_version: Optional[int] = None
normalized_name: str
if patch_version_str := match.group('patch_version'):
patch_version = int(patch_version_str)
normalized_name = f"{genome_build.name}.p{patch_version}"
else:
normalized_name = genome_build.name

gbpv, _ = GenomeBuildPatchVersion.objects.get_or_create(
name=normalized_name,
genome_build=genome_build,
patch_version=patch_version
)
return gbpv
else:
raise ValueError(f"Invalid Genome Build Patch Version \"{name}\"")
for regex in GenomeBuildPatchVersion.GENOME_BUILD_VERSION_REGEXES:
if match := regex.match(name):
genome_build = GenomeBuild.get_name_or_alias(match.group('genome_build'))
patch_version: Optional[int] = None
normalized_name: str
if patch_version_str := match.group('patch_version'):
patch_version = int(patch_version_str)
normalized_name = f"{genome_build.name}.p{patch_version}"
else:
normalized_name = genome_build.name

gbpv, _ = GenomeBuildPatchVersion.objects.get_or_create(
name=normalized_name,
genome_build=genome_build,
patch_version=patch_version
)
return gbpv

raise ValueError(f"Invalid Genome Build Patch Version '{name}'")

@staticmethod
def get_unspecified_patch_version_for(genome_build: GenomeBuild) -> 'GenomeBuildPatchVersion':
Expand Down

0 comments on commit c4a6919

Please sign in to comment.