Skip to content

Commit 0a0d971

Browse files
committed
refactor classes into separate files
1 parent dbf3155 commit 0a0d971

File tree

7 files changed

+389
-375
lines changed

7 files changed

+389
-375
lines changed

pgscatalog.core/src/pgscatalog/core/__init__.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
ScoringFile,
1111
NormalisedScoringFile,
1212
ScoreVariant,
13-
EffectType,
1413
GenomeBuild,
1514
TargetVariants,
1615
TargetVariant,
@@ -37,6 +36,8 @@
3736
relabel_write,
3837
effect_type_keyfunc,
3938
chrom_keyfunc,
39+
EffectType,
40+
Allele,
4041
)
4142

4243
log_fmt = "%(name)s: %(asctime)s %(levelname)-8s %(message)s"
@@ -73,12 +74,13 @@
7374
"TargetVariants",
7475
"TargetType",
7576
"NormalisedScoringFile",
76-
"EffectType",
7777
"RelabelArgs",
7878
"relabel",
7979
"relabel_write",
8080
"effect_type_keyfunc",
8181
"chrom_keyfunc",
82+
"EffectType",
83+
"Allele",
8284
]
8385

8486
__version__ = importlib.metadata.version("pgscatalog.core")

pgscatalog.core/src/pgscatalog/core/lib/__init__.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
from ._config import Config
2+
from .allele import Allele
3+
from .effecttype import EffectType
4+
from .genomebuild import GenomeBuild
25
from .catalogapi import ScoreQueryResult, CatalogQuery, CatalogCategory
36
from .scorefiles import ScoringFiles, ScoringFile, NormalisedScoringFile
4-
from .scorevariant import ScoreVariant, EffectType
5-
from .genomebuild import GenomeBuild
7+
from .catalogscorevariant import CatalogScoreVariant
8+
from .scorevariant import ScoreVariant
69
from .targetvariants import TargetVariants, TargetVariant, TargetType
710
from ._relabel import RelabelArgs, relabel, relabel_write
811
from ._sortpaths import effect_type_keyfunc, chrom_keyfunc
@@ -56,10 +59,12 @@
5659
"TargetVariants",
5760
"TargetType",
5861
"NormalisedScoringFile",
59-
"EffectType",
6062
"RelabelArgs",
6163
"relabel",
6264
"relabel_write",
6365
"effect_type_keyfunc",
6466
"chrom_keyfunc",
67+
"CatalogScoreVariant",
68+
"EffectType",
69+
"Allele",
6570
]

pgscatalog.core/src/pgscatalog/core/lib/_normalise.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import pyliftover
1010

1111
from .genomebuild import GenomeBuild
12-
from .scorevariant import Allele
12+
from .allele import Allele
1313
from .pgsexceptions import LiftoverError
1414

1515
logger = logging.getLogger(__name__)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
from functools import cached_property
2+
from typing import ClassVar
3+
4+
from pydantic import BaseModel, computed_field, model_serializer
5+
6+
7+
class Allele(BaseModel):
8+
"""A class that represents an allele found in PGS Catalog scoring files
9+
>>> simple_ea = Allele(**{"allele": "A"})
10+
>>> simple_ea
11+
Allele(allele='A', is_snp=True)
12+
>>> str(simple_ea)
13+
'A'
14+
>>> Allele(**{"allele": "AG"})
15+
Allele(allele='AG', is_snp=True)
16+
>>> hla_example = Allele(**{"allele": "+"})
17+
>>> hla_example
18+
Allele(allele='+', is_snp=False)
19+
20+
>>> Allele(allele="A")
21+
Allele(allele='A', is_snp=True)
22+
23+
>>> Allele(allele="A/T").has_multiple_alleles
24+
True
25+
"""
26+
27+
allele: str
28+
_valid_snp_bases: ClassVar[frozenset[str]] = frozenset({"A", "C", "T", "G"})
29+
30+
@computed_field
31+
@cached_property
32+
def is_snp(self) -> bool:
33+
"""SNPs are the most common type of effect allele in PGS Catalog scoring
34+
files. More complex effect alleles, like HLAs or APOE genes, often require
35+
extra work to represent in genomes. Users should be warned about complex
36+
effect alleles.
37+
"""
38+
return not frozenset(self.allele) - self._valid_snp_bases
39+
40+
@cached_property
41+
def has_multiple_alleles(self) -> bool:
42+
return "/" in self.allele
43+
44+
@model_serializer(mode="plain", return_type=str)
45+
def serialize(self):
46+
"""When dumping the model, flatten it to just return the allele as a string"""
47+
return self.allele
48+
49+
def __str__(self):
50+
return self.allele
51+
52+
def __eq__(self, other):
53+
if isinstance(other, Allele):
54+
return self.allele == other.allele
55+
return False
56+
57+
def __hash__(self):
58+
return hash(self.allele)

0 commit comments

Comments
 (0)