Skip to content

Commit

Permalink
refactor: use ResidueMode for residue_mode type
Browse files Browse the repository at this point in the history
- Inconsistent with using ResidueMode vs str
  • Loading branch information
korikuzma committed Oct 8, 2023
1 parent 2266295 commit 0bbc511
Show file tree
Hide file tree
Showing 15 changed files with 94 additions and 92 deletions.
4 changes: 2 additions & 2 deletions cool_seq_tool/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,5 @@ def __init__(
self.mane_transcript = MANETranscript(
self.seqrepo_access, self.transcript_mappings,
self.mane_transcript_mappings, self.uta_db)
self.exon_genomic_coords_mapper = ExonGenomicCoordsMapper(self.uta_db,
self.mane_transcript)
self.ex_g_coords_mapper = ExonGenomicCoordsMapper(self.uta_db,
self.mane_transcript)
13 changes: 6 additions & 7 deletions cool_seq_tool/handlers/seqrepo_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,16 @@ class SeqRepoAccess(SeqRepoDataProxy):

def get_reference_sequence(
self, ac: str, start: Optional[int] = None, end: Optional[int] = None,
residue_mode: str = ResidueMode.RESIDUE
residue_mode: ResidueMode = ResidueMode.RESIDUE
) -> Tuple[str, Optional[str]]:
"""Get reference sequence for an accession given a start and end position.
If `start` and `end` are not given, it will return the entire reference sequence
:param str ac: Accession
:param Optional[int] start: Start pos change
:param Optional[int] end: End pos change. If `None` assumes both
`start` and `end` have same values, if `start` exists.
:param str residue_mode: Residue mode for start/end positions
Must be either `inter-residue` or `residue`
:param ac: Accession
:param start: Start pos change
:param end: End pos change. If `None` assumes both `start` and `end` have same
values, if `start` exists.
:param residue_mode: Residue mode for `start` and `end`
:return: Sequence at position (if accession and positions actually
exist, else return empty string), warning if any
"""
Expand Down
5 changes: 2 additions & 3 deletions cool_seq_tool/mappers/exon_genomic_coords.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ def _get_gene_and_alt_ac(

async def _set_mane_genomic_data(
self, params: Dict, gene: str, alt_ac: str, pos: int, strand: int,
is_start: bool, residue_mode: str
is_start: bool, residue_mode: ResidueMode
) -> Optional[str]:
"""Set genomic data in `params` found from MANE.
Expand All @@ -365,8 +365,7 @@ async def _set_mane_genomic_data(
:param strand: Strand
:param is_start: `True` if `pos` is start position. `False` if `pos` is end
position.
:param residue_mode: Residue mode for start/end positions. Must be either
`inter-residue` or `residue`
:param residue_mode: Residue mode for `pos`
:return: Warnings if found
"""
mane_data = await self.mane_transcript.get_mane_transcript(
Expand Down
39 changes: 19 additions & 20 deletions cool_seq_tool/mappers/mane_transcript.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,18 +338,18 @@ def _validate_reading_frames(self, ac: str, start_pos: int, end_pos: int,
def _validate_references(self, ac: str, coding_start_site: int,
start_pos: int, end_pos: int,
mane_transcript: Dict, expected_ref: str,
anno: AnnotationLayer, residue_mode: str) -> bool:
anno: AnnotationLayer, residue_mode: ResidueMode) -> bool:
"""Return whether or not reference changes are the same.
:param str ac: Query accession
:param int coding_start_site: ac's coding start site
:param int start_pos: Original start position change
:param int end_pos: Origin end position change
:param Dict mane_transcript: Ensembl and RefSeq transcripts with
corresponding position change
:param str expected_ref: Reference at position given during input
:param AnnotationLayer anno: Annotation layer we are starting from
:param ResidueMode residue_mode: Residue mode
:param ac: Query accession
:param coding_start_site: ac's coding start site
:param start_pos: Original start position change
:param end_pos: Origin end position change
:param mane_transcript: Ensembl and RefSeq transcripts with corresponding
position change
:param expected_ref: Reference at position given during input
:param anno: Annotation layer we are starting from
:param residue_mode: Residue mode for `start_pos` and `end_pos`
:return: `True` if reference check passes. `False` otherwise.
"""
if anno == AnnotationLayer.CDNA:
Expand Down Expand Up @@ -432,23 +432,22 @@ def _get_prioritized_transcripts_from_gene(self,
async def get_longest_compatible_transcript(
self, gene: str, start_pos: int, end_pos: int,
start_annotation_layer: AnnotationLayer, ref: Optional[str] = None,
residue_mode: str = ResidueMode.RESIDUE,
residue_mode: ResidueMode = ResidueMode.RESIDUE,
mane_transcripts: Optional[Set] = None,
alt_ac: Optional[str] = None
) -> Optional[Dict]:
"""Get longest compatible transcript from a gene.
Try GRCh38 first, then GRCh37.
Transcript is compatible if it passes validation checks.
:param str gene: Gene symbol
:param int start_pos: Start position change
:param int end_pos: End position change
:param AnnotationLayer start_annotation_layer: Starting annotation layer.
:param str ref: Reference at position given during input
:param str residue_mode: Residue mode
:param Optional[Set] mane_transcripts: Attempted mane transcripts that were not
compatible
:param Optional[str] alt_ac: Genomic accession
:param gene: Gene symbol
:param start_pos: Start position change
:param end_pos: End position change
:param start_annotation_layer: Starting annotation layer.
:param ref: Reference at position given during input
:param residue_mode: Residue mode for `start_pos` and `end_pos`
:param mane_transcripts: Attempted mane transcripts that were not compatible
:param alt_ac: Genomic accession
:return: Data for longest compatible transcript
"""
inter_residue_pos, _ = get_inter_residue_pos(
Expand Down
2 changes: 1 addition & 1 deletion cool_seq_tool/routers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Module for routers"""
from enum import Enum

from cool_seq_tool import CoolSeqTool
from cool_seq_tool.app import CoolSeqTool


cool_seq_tool = CoolSeqTool()
Expand Down
11 changes: 6 additions & 5 deletions cool_seq_tool/routers/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
UNHANDLED_EXCEPTION_MSG
from cool_seq_tool.schemas import GenomicDataResponse, GenomicRequestBody, \
TranscriptRequestBody
from cool_seq_tool.utils import service_meta


logger = logging.getLogger("cool_seq_tool")
Expand All @@ -40,11 +41,11 @@ async def genomic_to_transcript_exon_coordinates(
request_body = request_body.model_dump()

response = GenomicDataResponse(
genomic_data=None, warnings=list(), service_meta=cool_seq_tool.service_meta())
genomic_data=None, warnings=list(), service_meta=service_meta())

try:
response = \
await cool_seq_tool.genomic_to_transcript_exon_coordinates(**request_body)
await cool_seq_tool.ex_g_coords_mapper.genomic_to_transcript_exon_coordinates(**request_body) # noqa: E501
except Exception as e:
logger.error(f"genomic_to_transcript_exon_coordinates unhandled exception {str(e)}") # noqa: E501
response.warnings.append(UNHANDLED_EXCEPTION_MSG)
Expand All @@ -71,10 +72,10 @@ async def transcript_to_genomic_coordinates(
request_body = request_body.model_dump()

response = GenomicDataResponse(
genomic_data=None, warnings=list(), service_meta=cool_seq_tool.service_meta())
genomic_data=None, warnings=list(), service_meta=service_meta())

try:
response = await cool_seq_tool.transcript_to_genomic_coordinates(**request_body)
response = await cool_seq_tool.ex_g_coords_mapper.transcript_to_genomic_coordinates(**request_body) # noqa: E501
except Exception as e:
logger.error(f"transcript_to_genomic_coordinates unhandled exception {str(e)}")
response.warnings.append(UNHANDLED_EXCEPTION_MSG)
Expand Down Expand Up @@ -105,7 +106,7 @@ async def get_sequence(
"""
_, path = tempfile.mkstemp(suffix=".fasta")
try:
cool_seq_tool.get_fasta_file(sequence_id, Path(path))
cool_seq_tool.seqrepo_access.get_fasta_file(sequence_id, Path(path))
except KeyError:
raise HTTPException(
status_code=404,
Expand Down
3 changes: 2 additions & 1 deletion cool_seq_tool/routers/mane.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from cool_seq_tool.routers import cool_seq_tool, SERVICE_NAME, RESP_DESCR, \
UNHANDLED_EXCEPTION_MSG, Tags
from cool_seq_tool.schemas import AnnotationLayer, ManeDataService, ResidueMode
from cool_seq_tool.utils import service_meta


logger = logging.getLogger("cool_seq_tool")
Expand Down Expand Up @@ -74,5 +75,5 @@ async def get_mane_data(
return ManeDataService(
mane_data=mane_data,
warnings=warnings,
service_meta=cool_seq_tool.service_meta()
service_meta=service_meta()
)
7 changes: 4 additions & 3 deletions cool_seq_tool/routers/mappings.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from cool_seq_tool.routers import cool_seq_tool, SERVICE_NAME, RESP_DESCR, Tags
from cool_seq_tool.schemas import Assembly, ToGenomicService, ToCdnaService, \
ResidueMode
from cool_seq_tool.utils import service_meta

logger = logging.getLogger("cool_seq_tool")

Expand Down Expand Up @@ -51,7 +52,7 @@ async def p_to_c(
return ToCdnaService(
c_data=c_data,
warnings=[w] if w else [],
service_meta=cool_seq_tool.service_meta()
service_meta=service_meta()
)


Expand Down Expand Up @@ -100,7 +101,7 @@ async def c_to_g(
return ToGenomicService(
g_data=g_data,
warnings=[w] if w else [],
service_meta=cool_seq_tool.service_meta()
service_meta=service_meta()
)


Expand Down Expand Up @@ -144,5 +145,5 @@ async def p_to_g(
return ToGenomicService(
g_data=g_data,
warnings=[w] if w else [],
service_meta=cool_seq_tool.service_meta()
service_meta=service_meta()
)
16 changes: 8 additions & 8 deletions cool_seq_tool/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,10 +419,10 @@ class ManeDataService(BaseModelForbidExtra):
class CdnaRepresentation(BaseModelForbidExtra):
"""Model response for cDNA representation"""

c_ac: str
c_start_pos: str
c_end_pos: str
cds_start: int
c_ac: StrictStr
c_start_pos: StrictInt
c_end_pos: StrictInt
cds_start: StrictInt
residue_mode: Literal[ResidueMode.INTER_RESIDUE] = ResidueMode.INTER_RESIDUE.value

model_config = ConfigDict(
Expand All @@ -432,7 +432,7 @@ class CdnaRepresentation(BaseModelForbidExtra):
"c_start_pos": 1797,
"c_end_pos": 1800,
"cds_start": 226,
"residue_mode": "inter-residue",
"residue_mode": ResidueMode.INTER_RESIDUE,
}
}
)
Expand All @@ -453,7 +453,7 @@ class ToCdnaService(BaseModelForbidExtra):
"c_start_pos": 1797,
"c_end_pos": 1800,
"cds_start": 226,
"residue_mode": "inter-residue",
"residue_mode": ResidueMode.INTER_RESIDUE,
},
"warnings": [],
"service_meta": {
Expand Down Expand Up @@ -481,7 +481,7 @@ class GenomicRepresentation(BaseModelForbidExtra):
"g_ac": "NC_000007.13",
"g_start_pos": 140453134,
"g_end_pos": 140453137,
"residue_mode": "inter-residue",
"residue_mode": ResidueMode.INTER_RESIDUE,
}
}
)
Expand All @@ -501,7 +501,7 @@ class ToGenomicService(BaseModelForbidExtra):
"g_ac": "NC_000007.13",
"g_start_pos": 140453134,
"g_end_pos": 140453137,
"residue_mode": "inter-residue",
"residue_mode": ResidueMode.INTER_RESIDUE,
},
"warnings": [],
"service_meta": {
Expand Down
11 changes: 5 additions & 6 deletions cool_seq_tool/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@


def get_inter_residue_pos(
start_pos: int, residue_mode: str, end_pos: Optional[int] = None
start_pos: int, residue_mode: ResidueMode, end_pos: Optional[int] = None
) -> Tuple[Optional[Tuple[int, int]], Optional[str]]:
"""Return inter-residue position
:param int start_pos: Start position
:param str residue_mode: `inter-residue` if start/end are 0 based coords.
`residue` if start/end are 1 based coords
:param Optional[int] end_pos: End position. If `None` assumes both
`start` and `end` have same values.
:param start_pos: Start position
:param residue_mode: Residue mode for `start_pos` and `end_pos`
:param end_pos: End position. If `None` assumes both `start` and `end` have same
values.
:return: Inter-residue coordinates, warning
"""
residue_mode = residue_mode.lower()
Expand Down
4 changes: 3 additions & 1 deletion tests/handlers/test_seqrepo_access.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""Module for testing seqrepo access class"""
import pytest

from cool_seq_tool.schemas import ResidueMode


def test_get_reference_sequence(test_seqrepo_access):
"""Test that get_reference_sequence method works correctly"""
Expand All @@ -14,7 +16,7 @@ def test_get_reference_sequence(test_seqrepo_access):
assert resp == ("V", None)

resp = test_seqrepo_access.get_reference_sequence(
"NP_004324.2", 599, 600, residue_mode="inter-residue")
"NP_004324.2", 599, 600, residue_mode=ResidueMode.INTER_RESIDUE)
assert resp == ("V", None)

resp = test_seqrepo_access.get_reference_sequence("NP_004324.2", 601, 600)
Expand Down
18 changes: 9 additions & 9 deletions tests/mappers/test_alignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def braf_v600e_c():
"c_start_pos": 1797,
"c_end_pos": 1800,
"cds_start": 226,
"residue_mode": "inter-residue"
"residue_mode": ResidueMode.INTER_RESIDUE
}


Expand All @@ -35,7 +35,7 @@ def egfr_l858r_c():
"c_start_pos": 2571,
"c_end_pos": 2574,
"cds_start": 261,
"residue_mode": "inter-residue"
"residue_mode": ResidueMode.INTER_RESIDUE
}


Expand All @@ -46,7 +46,7 @@ def braf_v600e_grch37():
"g_ac": "NC_000007.13",
"g_start_pos": 140453134,
"g_end_pos": 140453137,
"residue_mode": "inter-residue"
"residue_mode": ResidueMode.INTER_RESIDUE
}


Expand All @@ -57,7 +57,7 @@ def braf_v600e_grch38():
"g_ac": "NC_000007.14",
"g_start_pos": 140753334,
"g_end_pos": 140753337,
"residue_mode": "inter-residue"
"residue_mode": ResidueMode.INTER_RESIDUE
}


Expand All @@ -68,7 +68,7 @@ def egfr_l858r_grch37():
"g_ac": "NC_000007.13",
"g_start_pos": 55259513,
"g_end_pos": 55259516,
"residue_mode": "inter-residue"
"residue_mode": ResidueMode.INTER_RESIDUE
}


Expand All @@ -79,7 +79,7 @@ def egfr_l858r_grch38():
"g_ac": "NC_000007.14",
"g_start_pos": 55191820,
"g_end_pos": 55191823,
"residue_mode": "inter-residue"
"residue_mode": ResidueMode.INTER_RESIDUE
}


Expand All @@ -90,7 +90,7 @@ def delins_grch37():
"g_ac": "NC_000007.13",
"g_start_pos": 140453131,
"g_end_pos": 140453137,
"residue_mode": "inter-residue"
"residue_mode": ResidueMode.INTER_RESIDUE
}


Expand All @@ -104,7 +104,7 @@ def _expected(assembly):
"g_ac": g_ac,
"g_start_pos": 534316,
"g_end_pos": 534319,
"residue_mode": "inter-residue"
"residue_mode": ResidueMode.INTER_RESIDUE
}

return _expected
Expand Down Expand Up @@ -159,7 +159,7 @@ async def test_p_to_c(test_alignment_mapper, braf_v600e_c, egfr_l858r_c):
"c_start_pos": 393,
"c_end_pos": 396,
"cds_start": 223,
"residue_mode": "inter-residue"
"residue_mode": ResidueMode.INTER_RESIDUE
}


Expand Down
Loading

0 comments on commit 0bbc511

Please sign in to comment.