Skip to content

Commit

Permalink
issue #1217 - handle T2T in classifications (workarounds doesn't quit…
Browse files Browse the repository at this point in the history
…e work)
  • Loading branch information
davmlaw committed Dec 19, 2024
1 parent c4a6919 commit 116d8ea
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 14 deletions.
16 changes: 13 additions & 3 deletions classification/models/classification_variant_info_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -677,10 +677,16 @@ def _genome_builds() -> list[GenomeBuild]:
return [build for build in GenomeBuild.builds_with_annotation() if build in [GenomeBuild.grch37(), GenomeBuild.grch38()]]

def __getitem__(self, item: GenomeBuild) -> Optional[ResolvedVariantInfo]:
return getattr(self, ImportedAlleleInfo.__genome_build_to_attr(item))
try:
return getattr(self, ImportedAlleleInfo.__genome_build_to_attr(item))
except ValueError:
return None

def __setitem__(self, key: GenomeBuild, value: Optional[ResolvedVariantInfo]):
setattr(self, ImportedAlleleInfo.__genome_build_to_attr(key), value)
try:
setattr(self, ImportedAlleleInfo.__genome_build_to_attr(key), value)
except ValueError:
return None

@property
def resolved_builds(self) -> list[ResolvedVariantInfo]:
Expand Down Expand Up @@ -776,7 +782,11 @@ def get_or_create(**kwargs: dict[str, Any]) -> 'ImportedAlleleInfo':

@property
def variant_info_for_imported_genome_build(self) -> Optional[ResolvedVariantInfo]:
return self[self.imported_genome_build_patch_version.genome_build]
try:
vi = self[self.imported_genome_build_patch_version.genome_build]
except ValueError:
vi = None
return vi

@property
def variant_info_for_lifted_over_genome_build(self) -> Optional[ResolvedVariantInfo]:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{% load classification_tags %}
{% load ui_utils %}
{% if imported_allele_info %}
<a data-toggle="ajax-modal" href="{% url 'view_imported_allele_info_detail' imported_allele_info.pk %}?on_allele_page={% if on_allele_page %}true{% else %}false{% endif %}" data-title="{{ imported_allele_info }}">
{% if imported_allele_info and imported_allele_info.pk %}
<a data-toggle="ajax-modal" href="{% url 'view_imported_allele_info_detail' allele_info_id=imported_allele_info.pk %}?on_allele_page={% if on_allele_page %}true{% else %}false{% endif %}" data-title="{{ imported_allele_info }}">
<span style="font-size:0.8rem; font-weight: 500">{{ imported_allele_info.imported_genome_build_patch_version }}</span>
<br/>
{% c_hgvs imported_allele_info.imported_hgvs_obj %}{{ imported_allele_info.issue_icon|default_if_none:'' }}
Expand Down
36 changes: 27 additions & 9 deletions snpdb/models/models_genome.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,20 @@ def get_from_fuzzy_string(genome_build_str: str):
def get_name_or_alias(build_name) -> 'GenomeBuild':
""" Get by insensitive name or alias """

build_no_patch = build_name.split(".", 1)[0]
q = Q(name__iexact=build_name) | Q(alias__iexact=build_name)
q_no_patch = Q(name__iexact=build_no_patch) | Q(alias__iexact=build_no_patch)
return GenomeBuild.objects.get(q | q_no_patch, enabled=True)
q_or_list = [
Q(name__iexact=build_name),
Q(alias__iexact=build_name)
]
try:
build_no_patch = GenomeBuildPatchVersion.get_build_no_patch(build_name)
q_or_list.extend([
Q(name__iexact=build_no_patch),
Q(alias__iexact=build_no_patch),
])
except ValueError:
pass
q = reduce(operator.or_, q_or_list)
return GenomeBuild.objects.get(q, enabled=True)

@staticmethod
def detect_from_filename(filename) -> Optional['GenomeBuild']:
Expand Down Expand Up @@ -301,11 +311,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 """

# Standard eg GRCh37.p13 (build=GRCh37, patch=13) or GRCh38.p14 (build=GRCh38, patch=14
GRCH_WITH_PATCH_REGEX = re.compile(r"(?P<genome_build>GRCh[0-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),
GRCH_WITH_PATCH_REGEX,
# T2T-CHM13v2.0 is ALL BUILD, v2.0 is not a patch
re.compile(r"(?P<genome_build>T2T-CHM[0-9]+v[0-9]+\.[0-9]+)", re.IGNORECASE),
]

class Meta:
Expand All @@ -319,14 +330,21 @@ def sort_key(obj: GenomeBuildPatchVersion):
return obj.genome_build.name, obj.patch_version or 0
return sort_key(self) < sort_key(other)

@staticmethod
def get_build_no_patch(build_name: str) -> str:
for regex in GenomeBuildPatchVersion.GENOME_BUILD_VERSION_REGEXES:
if match := regex.match(build_name):
return match.group("genome_build")
raise ValueError(f"Invalid Genome Build (with optional patch) '{build_name}'")

@staticmethod
def get_or_create(name: str) -> 'GenomeBuildPatchVersion':
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'):
if patch_version_str := match.groupdict().get('patch_version'):
patch_version = int(patch_version_str)
normalized_name = f"{genome_build.name}.p{patch_version}"
else:
Expand Down

0 comments on commit 116d8ea

Please sign in to comment.