-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #185 from monarch-initiative/variant_formatter
Created a Formatter
- Loading branch information
Showing
8 changed files
with
117 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import abc | ||
import typing | ||
|
||
from genophenocorr.model import Variant | ||
|
||
T = typing.TypeVar('T') | ||
|
||
class Formatter(typing.Generic[T], metaclass=abc.ABCMeta): | ||
|
||
@abc.abstractmethod | ||
def format_as_string(self, item: T) -> str: | ||
""" Inputs an item and outputs a human readable string that can be used | ||
to more easily read the item in tables or other visualizers. | ||
Args: | ||
item (T): an element to be formatted | ||
Returns: | ||
str: a human readable string | ||
""" | ||
pass | ||
|
||
class VariantFormatter(Formatter[Variant]): | ||
""" | ||
A class that can be used to format a `Variant` to a human readable string | ||
""" | ||
def __init__(self, tx_id: typing.Optional[str] = None) -> None: | ||
self._tx_id = tx_id | ||
|
||
def format_as_string(self, item: Variant) -> str: | ||
""" | ||
Args: | ||
item (Variant): An object of class `Variant` representing a variant. | ||
Returns: | ||
str: A human readable string for the variant. | ||
""" | ||
if self._tx_id is not None: | ||
transcript = item.get_tx_anno_by_tx_id(self._tx_id) | ||
else: | ||
transcript = item.get_preferred_tx_annotation() | ||
if transcript is not None and transcript.hgvs_cdna is not None: | ||
if len(transcript.hgvs_cdna) > 50: | ||
return "Long HGVS" | ||
return transcript.hgvs_cdna | ||
elif item.variant_coordinates.variant_key is not None: | ||
return item.variant_coordinates.variant_key | ||
else: | ||
# To be reevaluated | ||
return f"Variant {item} has no string format." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import pytest | ||
|
||
import typing | ||
|
||
from genophenocorr.model import Cohort | ||
from genophenocorr.view import VariantFormatter | ||
|
||
class TestFormatter: | ||
|
||
@pytest.mark.parametrize( | ||
"variant, expected", | ||
[ | ||
('12_56004525_56004525_A_G', "NM_001032386.2:c.1136A>G"), | ||
] | ||
) | ||
def test_variant_formatter( | ||
self, | ||
variant: str, | ||
expected: str, | ||
suox_cohort: Cohort | ||
): | ||
var = suox_cohort.get_variant_by_key(variant) | ||
formatter = VariantFormatter() | ||
assert formatter.format_as_string(var) == expected |