-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmane_transcript.py
More file actions
1430 lines (1265 loc) · 53.4 KB
/
Copy pathmane_transcript.py
File metadata and controls
1430 lines (1265 loc) · 53.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Retrieve MANE transcript from a location on p./c./g. coordinates.
Steps:
#. Map annotation layer to genome
#. Liftover to preferred genome (GRCh38). GRCh36 and earlier assemblies are not supported
for fetching MANE transcripts.
#. Select preferred compatible annotation (see :ref:`transcript compatibility <transcript_compatibility>`)
#. Map back to correct annotation layer
In addition to a mapper utility class, this module also defines several vocabulary
constraints and data models for coordinate representation.
"""
import logging
import math
from enum import Enum
from typing import Literal
import polars as pl
from pydantic import BaseModel
from cool_seq_tool.handlers.seqrepo_access import SeqRepoAccess
from cool_seq_tool.mappers.liftover import LiftOver
from cool_seq_tool.schemas import (
AnnotationLayer,
Assembly,
CoordinateType,
GenomicTxMetadata,
ManeGeneData,
Strand,
TranscriptPriority,
)
from cool_seq_tool.sources import (
ManeTranscriptMappings,
TranscriptMappings,
UtaDatabase,
)
from cool_seq_tool.utils import get_inter_residue_pos
_logger = logging.getLogger(__name__)
class EndAnnotationLayer(str, Enum):
"""Define constraints for end annotation layer. This is used for determining the
end annotation layer when getting the longest compatible remaining representation
"""
PROTEIN = AnnotationLayer.PROTEIN
CDNA = AnnotationLayer.CDNA
PROTEIN_AND_CDNA = "p_and_c"
class DataRepresentation(BaseModel):
"""Define object model for final output representation"""
gene: str | None = None
refseq: str | None = None
ensembl: str | None = None
pos: tuple[int, int]
strand: Strand
status: TranscriptPriority
class CdnaRepresentation(DataRepresentation):
"""Define object model for coding DNA representation"""
coding_start_site: int
coding_end_site: int
alt_ac: str | None = None
class GenomicRepresentation(BaseModel):
"""Define object model for genomic representation"""
pos: tuple[int, int]
mane_genes: list[ManeGeneData] = []
status: Literal["grch38"] = TranscriptPriority.GRCH38.value
ac: str
class ProteinAndCdnaRepresentation(BaseModel):
"""Define object model for protein and cDNA representation"""
protein: DataRepresentation
cdna: CdnaRepresentation
class ManeTranscript:
"""Class for retrieving MANE transcripts."""
def __init__(
self,
seqrepo_access: SeqRepoAccess,
transcript_mappings: TranscriptMappings,
mane_transcript_mappings: ManeTranscriptMappings,
uta_db: UtaDatabase,
liftover: LiftOver,
) -> None:
"""Initialize the ManeTranscript class.
A handful of resources are required for initialization, so when defaults are
enough, it's easiest to let the core CoolSeqTool class handle it for you:
>>> from cool_seq_tool import CoolSeqTool
>>> mane_mapper = CoolSeqTool().mane_transcript
Note that most methods are defined as Python coroutines, so they must be called
with ``await`` or run from an ``async`` event loop:
>>> import asyncio
>>> result = asyncio.run(mane_mapper.g_to_grch38("NC_000001.11", 100, 200))
>>> result.ac
'NC_000001.11'
See the :ref:`Usage section <async_note>` for more information.
:param seqrepo_access: Access to seqrepo queries
:param transcript_mappings: Access to transcript accession mappings and
conversions
:param mane_transcript_mappings: Access to MANE Transcript accession mapping
data
:param uta_db: UtaDatabase instance to give access to query UTA database
:param liftover: Instance to provide mapping between human genome assemblies
"""
self.seqrepo_access = seqrepo_access
self.transcript_mappings = transcript_mappings
self.mane_transcript_mappings = mane_transcript_mappings
self.uta_db = uta_db
self.liftover = liftover
@staticmethod
def get_reading_frame(pos: int) -> int:
"""Return reading frame number. Only used on c. coordinate.
:param pos: cDNA position
:return: Reading frame
"""
pos_mod_3 = pos % 3
if pos_mod_3 == 0:
pos_mod_3 = 3
return pos_mod_3
@staticmethod
def _p_to_c_pos(start: int, end: int) -> tuple[int, int]:
"""Return cDNA position given a protein position.
:param start: Start protein position. Inter-residue coordinates
:param end: End protein position. Inter-residue coordinates
:return: cDNA position start, cDNA position end
"""
start_pos = start * 3
end_pos = end * 3 if end != start else start_pos
return start_pos, end_pos - 1
async def _p_to_c(
self, ac: str, start_pos: int, end_pos: int
) -> tuple[str, tuple[int, int]] | None:
"""Convert protein (p.) annotation to cDNA (c.) annotation.
:param ac: Protein accession
:param start_pos: Protein start position. Inter-residue coordinates
:param end_pos: Protein end position. Inter-residue coordinates
:return: [cDNA transcript accession, [cDNA pos start, cDNA pos end]]
"""
# TODO: Check version mappings 1 to 1 relationship
temp_ac = await self.uta_db.p_to_c_ac(ac)
if temp_ac:
ac = temp_ac[-1]
else:
try:
if ac.startswith("NP_"):
ac = self.transcript_mappings.np_to_nm[ac]
elif ac.startswith("ENSP"):
ac = self.transcript_mappings.ensp_to_enst[ac]
else:
_logger.warning("Unable to find accession: %s", ac)
return None
except KeyError:
_logger.warning("%s not found in transcript_mappings", ac)
return None
pos = self._p_to_c_pos(start_pos, end_pos)
return ac, pos
async def _c_to_g(self, ac: str, pos: tuple[int, int]) -> GenomicTxMetadata | None:
"""Get g. annotation from c. annotation.
:param ac: cDNA accession
:param pos: [cDNA pos start, cDNA pos end]
:return: Metadata for genomic and transcript accessions
"""
# UTA does not store ENST versions
# So we want to make sure version is valid
if ac.startswith("ENST"):
if (
not self.transcript_mappings.ensembl_transcript_version_to_gene_symbol.get(
ac
)
and not self.seqrepo_access.get_reference_sequence(ac, start=1, end=1)[
0
]
):
_logger.warning("Ensembl transcript not found: %s", ac)
return None
temp_ac = ac.split(".")[0]
else:
temp_ac = ac
# c. coordinate does not contain cds start, so we need to add it
cds_start_end = await self.uta_db.get_cds_start_end(temp_ac)
if not cds_start_end:
_logger.warning("Accession %s not found in UTA", temp_ac)
return None
coding_start_site = cds_start_end[0]
pos = pos[0] + coding_start_site, pos[1] + coding_start_site
return await self._get_and_validate_genomic_tx_data(
ac, pos, AnnotationLayer.CDNA, coding_start_site=coding_start_site
)
async def _liftover_to_38(self, genomic_tx_data: GenomicTxMetadata) -> None:
"""Liftover genomic_tx_data to hg38 assembly.
:param genomic_tx_data: Metadata for genomic and transcript accessions. This
will be mutated in-place if not GRCh38 assembly.
"""
descr = await self.uta_db.get_chr_assembly(genomic_tx_data.alt_ac)
if descr is None:
# already grch38
return
chromosome, _ = descr
query = f"""
SELECT DISTINCT alt_ac
FROM {self.uta_db.schema}.tx_exon_aln_mv
WHERE tx_ac = '{genomic_tx_data.tx_ac}';
""" # noqa: S608
nc_acs = await self.uta_db.execute_query(query)
nc_acs = [nc_ac[0] for nc_ac in nc_acs]
if nc_acs == [genomic_tx_data.alt_ac]:
_logger.warning(
"UTA does not have GRCh38 assembly for %s",
genomic_tx_data.alt_ac.split(".")[0],
)
return
# Get most recent assembly version position
# Liftover range
self._set_liftover(
genomic_tx_data, "alt_pos_range", chromosome, Assembly.GRCH38
)
# Liftover changes range
self._set_liftover(
genomic_tx_data, "alt_pos_change_range", chromosome, Assembly.GRCH38
)
# Change alt_ac to most recent
if genomic_tx_data.alt_ac.startswith("EN"):
order_by_cond = "ORDER BY alt_ac DESC;"
else:
order_by_cond = """
ORDER BY CAST(SUBSTR(alt_ac, position('.' in alt_ac) + 1,
LENGTH(alt_ac)) AS INT) DESC;
"""
query = f"""
SELECT alt_ac
FROM {self.uta_db.schema}.genomic
WHERE alt_ac LIKE '{genomic_tx_data.alt_ac.split(".")[0]}%'
{order_by_cond}
""" # noqa: S608
nc_acs = await self.uta_db.execute_query(query)
genomic_tx_data.alt_ac = nc_acs[0][0]
def _set_liftover(
self,
genomic_tx_data: GenomicTxMetadata,
key: str,
chromosome: str,
liftover_to_assembly: Assembly,
) -> None:
"""Update genomic_tx_data to have coordinates for given assembly.
:param genomic_tx_data: Metadata for genomic and transcript accessions
:param key: Key to access coordinate positions
:param chromosome: Chromosome, must be prefixed with ``chr``
:param liftover_to_assembly: Assembly to liftover to
"""
coords = getattr(genomic_tx_data, key)
liftover_start_i = self.liftover.get_liftover(
chromosome, coords[0], liftover_to_assembly
)
if liftover_start_i is None:
_logger.warning(
"Unable to liftover position %s on %s",
coords[0],
chromosome,
)
return
liftover_end_i = self.liftover.get_liftover(
chromosome, coords[1], liftover_to_assembly
)
if liftover_end_i is None:
_logger.warning(
"Unable to liftover position %s on %s",
coords[1],
chromosome,
)
return
setattr(genomic_tx_data, key, (liftover_start_i[1], liftover_end_i[1]))
async def _get_and_validate_genomic_tx_data(
self,
tx_ac: str,
pos: tuple[int, int],
annotation_layer: Literal[AnnotationLayer.CDNA]
| Literal[AnnotationLayer.GENOMIC] = AnnotationLayer.CDNA,
coding_start_site: int | None = None,
alt_ac: str | None = None,
) -> GenomicTxMetadata | None:
"""Get and validate genomic_tx_data
:param tx_ac: Accession on c. coordinate
:param pos: (start pos, end pos). These are inter-residue coordinates
:param annotation_layer: Annotation layer for ``ac`` and ``pos``
:param coding_start_site: Coding start site
:param alt_ac: Accession on g. coordinate
:return: Metadata for genomic and transcript accessions if found and validated,
else None
"""
genomic_tx_data = await self.uta_db.get_genomic_tx_data(
tx_ac, pos, annotation_layer, alt_ac=alt_ac
)
if not genomic_tx_data:
_logger.warning(
"Unable to find genomic_tx_data for %s at position %s on annotation layer %s",
alt_ac,
pos,
annotation_layer,
)
return None
genomic_tx_data.coding_start_site = coding_start_site
if not alt_ac:
# Only want to liftover if alt_ac not provided. If alt_ac is provided,
# it means user wants to stick with the queried assembly
og_alt_exon_id = genomic_tx_data.alt_exon_id
await self._liftover_to_38(genomic_tx_data)
liftover_alt_exon_id = genomic_tx_data.alt_exon_id
# Validation check: Exon structure
if og_alt_exon_id != liftover_alt_exon_id:
_logger.warning(
"Original alt_exon_id %s does not match liftover alt_exon_id %s",
og_alt_exon_id,
liftover_alt_exon_id,
)
return None
return genomic_tx_data
@staticmethod
def _get_c_data(
cds_start_end: tuple[int, int],
c_pos_change: tuple[int, int],
strand: Strand,
status: TranscriptPriority,
refseq_c_ac: str,
gene: str | None = None,
ensembl_c_ac: str | None = None,
alt_ac: str | None = None,
) -> CdnaRepresentation:
"""Return transcript data on c. coordinate.
:param gene: Gene symbol
:param cds_start_end: Coding start and end site for transcript
:param c_pos_change: Start and end positions for change on c. coordinate
:param strand: Strand
:param status: Status of transcript
:param refseq_c_ac: Refseq transcript
:param ensembl_c_ac: Ensembl transcript
:param alt_ac: Genomic accession
:return: Transcript data on c. coord
"""
cds_start = cds_start_end[0]
cds_end = cds_start_end[1]
lt_cds_start = c_pos_change[0] < cds_start and c_pos_change[1] < cds_start
gt_cds_end = c_pos_change[1] > cds_end and c_pos_change[1] > cds_end
if lt_cds_start or gt_cds_end:
_logger.info(
"%s with position %s is not within CDS start/end",
refseq_c_ac,
c_pos_change,
)
return CdnaRepresentation(
gene=gene,
refseq=refseq_c_ac,
ensembl=ensembl_c_ac,
coding_start_site=cds_start,
coding_end_site=cds_end,
pos=c_pos_change,
strand=strand,
status=status,
alt_ac=alt_ac,
)
def _c_to_p_pos(self, c_pos: tuple[int, int]) -> tuple[int, int]:
"""Get protein position from cdna position
:param c_pos: cdna position. inter-residue coordinates
:return: protein position. inter-residue coordinates
"""
end = math.ceil(c_pos[1] / 3)
if c_pos[1] - c_pos[0] == 1:
start = end - 1
else:
start = math.ceil((c_pos[0] + 1) / 3) - 1
return start, end
def _get_mane_p(
self, mane_data: dict, mane_c_pos_range: tuple[int, int]
) -> DataRepresentation:
"""Translate MANE Transcript c. annotation to p. annotation
:param mane_data: MANE Transcript data
:param mane_c_pos_range: Position change range on MANE Transcript c. coordinate
using inter-residue coordinates
:return: Protein representation
"""
return DataRepresentation(
gene=mane_data["symbol"],
refseq=mane_data["RefSeq_prot"],
ensembl=mane_data["Ensembl_prot"],
pos=self._c_to_p_pos(mane_c_pos_range),
strand=Strand.NEGATIVE
if mane_data["chr_strand"] == "-"
else Strand.POSITIVE,
status=TranscriptPriority(
"_".join(mane_data["MANE_status"].split()).lower()
),
)
async def _g_to_c(
self,
g: GenomicTxMetadata,
refseq_c_ac: str,
status: TranscriptPriority,
ensembl_c_ac: str | None = None,
alt_ac: str | None = None,
found_result: bool = False,
) -> CdnaRepresentation | None:
"""Get transcript c. annotation data from g. annotation.
:param g: Genomic data
:param refseq_c_ac: Refseq transcript accession
:param status: Status of transcript
:param ensembl_c_ac: Ensembl transcript accession
:param alt_ac: Genomic accession
:param found_result: ``True`` if found result, so do not need to query
tx_exon_aln_mv table. This is because the user did not need to liftover.
``False`` if need to get result from tx_exon_aln_mv table.
:return: Transcript data
"""
if found_result:
tx_g_pos = g.alt_pos_range
tx_pos_range = g.tx_pos_range
else:
result = await self.uta_db.get_tx_exon_aln_data(
refseq_c_ac,
g.alt_pos_change_range[0],
g.alt_pos_change_range[1],
alt_ac=alt_ac if alt_ac else g.alt_ac,
use_tx_pos=False,
)
if not result:
_logger.warning(
"Unable to find transcript, %s, position change", refseq_c_ac
)
return None
result = result[-1]
tx_g_pos = result.alt_start_i, result.alt_end_i
tx_pos_range = result.tx_start_i, result.tx_end_i
cds_start_end = await self.uta_db.get_cds_start_end(refseq_c_ac)
if not cds_start_end:
return None
coding_start_site = cds_start_end[0]
g_pos = g.alt_pos_change_range # start/end genomic change
g_pos_change = g_pos[0] - tx_g_pos[0], tx_g_pos[1] - g_pos[1]
if g.strand == Strand.NEGATIVE:
g_pos_change = (tx_g_pos[1] - g_pos[0], g_pos[1] - tx_g_pos[0])
c_pos_change = (
tx_pos_range[0] + g_pos_change[0] - coding_start_site,
tx_pos_range[1] - g_pos_change[1] - coding_start_site,
)
if c_pos_change[0] > c_pos_change[1]:
c_pos_change = c_pos_change[1], c_pos_change[0]
return self._get_c_data(
gene=g.gene,
cds_start_end=cds_start_end,
c_pos_change=c_pos_change,
strand=g.strand,
alt_ac=alt_ac,
status=status,
refseq_c_ac=refseq_c_ac,
ensembl_c_ac=ensembl_c_ac,
)
def _validate_reading_frames(
self, ac: str, start_pos: int, end_pos: int, transcript_data: CdnaRepresentation
) -> bool:
"""Return whether reading frames are the same after translation.
:param ac: Query accession
:param start_pos: Original start cDNA position change
:param end_pos: Original end cDNA position change
:param transcript_data: Ensembl and RefSeq transcripts with corresponding
position change
:return: ``True`` if reading frames are the same after translation.
``False`` otherwise
"""
for pos, pos_index in [(start_pos, 0), (end_pos, 1)]:
if pos is not None:
og_rf = self.get_reading_frame(pos)
new_rf = self.get_reading_frame(transcript_data.pos[pos_index])
if og_rf != new_rf:
_logger.warning(
"%s original reading frame (%s) does not match new %s, %s reading frame (%s)",
ac,
og_rf,
transcript_data.ensembl,
transcript_data.refseq,
new_rf,
)
return False
elif pos_index == 0:
_logger.warning("%s must having start position", ac)
return False
return True
def _validate_references(
self,
ac: str,
coding_start_site: int,
start_pos: int,
end_pos: int,
mane_transcript: DataRepresentation
| CdnaRepresentation
| GenomicRepresentation,
expected_ref: str,
anno: AnnotationLayer,
coordinate_type: CoordinateType,
) -> bool:
"""Return whether or not reference changes are the same.
: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 coordinate_type: Coordinate type for ``start_pos`` and ``end_pos``
:return: ``True`` if reference check passes. ``False`` otherwise.
"""
if anno == AnnotationLayer.CDNA:
start_pos += coding_start_site
end_pos += coding_start_site
ref, _ = self.seqrepo_access.get_reference_sequence(
ac, start=start_pos, end=end_pos, coordinate_type=coordinate_type
)
if ref is None:
return False
if mane_transcript:
mane_start_pos = mane_transcript.pos[0]
mane_end_pos = mane_transcript.pos[1]
if anno == AnnotationLayer.CDNA and isinstance(
mane_transcript, CdnaRepresentation
):
mane_cds = mane_transcript.coding_start_site
mane_start_pos += mane_cds
mane_end_pos += mane_cds
if mane_transcript.refseq:
mane_ref, _ = self.seqrepo_access.get_reference_sequence(
mane_transcript.refseq,
start=mane_start_pos,
end=mane_end_pos if mane_start_pos != mane_end_pos else None,
coordinate_type=coordinate_type,
)
else:
mane_ref = None
if not mane_ref:
_logger.info("Unable to validate reference for MANE Transcript")
if expected_ref != mane_ref:
_logger.info(
"Expected ref, %s, but got %s on MANE accession, %s",
expected_ref,
mane_ref,
mane_transcript.refseq,
)
if expected_ref != ref:
_logger.warning(
"Expected ref, %s, but got %s on accession, %s", expected_ref, ref, ac
)
return False
return True
def validate_index(
self, ac: str, pos: tuple[int, int], coding_start_site: int
) -> bool:
"""Validate that positions actually exist on accession
:param ac: Accession
:param pos: Start position change, End position change
:param coding_start_site: coding start site for accession
:return: ``True`` if positions exist on accession. ``False`` otherwise
"""
start_pos = pos[0] + coding_start_site
end_pos = pos[1] + coding_start_site
return bool(
self.seqrepo_access.get_reference_sequence(
ac,
start=start_pos,
end=end_pos,
coordinate_type=CoordinateType.INTER_RESIDUE,
)[0]
)
def _get_prioritized_transcripts_from_gene(self, df: pl.DataFrame) -> list:
"""Sort and filter transcripts from gene to get priority list
:param df: Data frame containing transcripts from gene
data
:return: List of prioritized transcripts for a given gene. Sort by latest
assembly, longest length of transcript, with first-published transcripts
breaking ties. If there are multiple transcripts for a given accession, the
most recent version of a transcript associated with an assembly will be kept
"""
copy_df = df.clone()
copy_df = copy_df.drop("alt_ac").unique()
copy_df = copy_df.with_columns(
[
pl.col("tx_ac")
.str.split(".")
.list.get(0)
.str.split("NM_")
.list.get(1)
.cast(pl.Int64)
.alias("ac_no_version_as_int"),
pl.col("tx_ac")
.str.split(".")
.list.get(1)
.cast(pl.Int16)
.alias("ac_version"),
]
)
copy_df = copy_df.sort(
by=["ac_no_version_as_int", "ac_version"], descending=[True, True]
)
copy_df = copy_df.unique(["ac_no_version_as_int"], keep="first")
copy_df = copy_df.with_columns(
copy_df.map_rows(
lambda x: len(self.seqrepo_access.get_reference_sequence(x[1])[0])
)
.to_series()
.alias("len_of_tx")
)
copy_df = copy_df.sort(
by=["len_of_tx", "ac_no_version_as_int"], descending=[True, False]
)
return copy_df.select("tx_ac").to_series().to_list()
async def get_longest_compatible_transcript(
self,
start_pos: int,
end_pos: int,
start_annotation_layer: AnnotationLayer,
gene: str | None = None,
ref: str | None = None,
coordinate_type: CoordinateType = CoordinateType.RESIDUE,
mane_transcripts: set | None = None,
alt_ac: str | None = None,
end_annotation_layer: EndAnnotationLayer | None = None,
) -> DataRepresentation | CdnaRepresentation | ProteinAndCdnaRepresentation | None:
"""Get longest compatible transcript from a gene. See the documentation for
the :ref:`transcript compatibility policy <transcript_compatibility>` for more
information.
>>> import asyncio
>>> from cool_seq_tool import CoolSeqTool
>>> from cool_seq_tool.schemas import AnnotationLayer, CoordinateType
>>> mane_mapper = CoolSeqTool().mane_transcript
>>> mane_transcripts = {
... "ENST00000646891.2",
... "NM_001374258.1",
... "NM_004333.6",
... "ENST00000644969.2",
... }
>>> result = asyncio.run(
... mane_mapper.get_longest_compatible_transcript(
... 599,
... 599,
... gene="BRAF",
... start_annotation_layer=AnnotationLayer.PROTEIN,
... coordinate_type=CoordinateType.INTER_RESIDUE,
... mane_transcripts=mane_transcripts,
... )
... )
>>> result.refseq
'NP_001365396.1'
If unable to find a match on GRCh38, this method will then attempt to drop down
to GRCh37.
# TODO example for inputs that demonstrate this?
:param start_pos: Start position change
:param end_pos: End position change
:param start_annotation_layer: Starting annotation layer
:param gene: HGNC gene symbol
:param ref: Reference at position given during input
:param coordinate_type: Coordinate type for ``start_pos`` and ``end_pos``
:param mane_transcripts: Attempted mane transcripts that were not compatible
:param alt_ac: Genomic accession
:param end_annotation_layer: The end annotation layer. If not provided, will be
set to ``EndAnnotationLayer.PROTEIN`` if
``start_annotation_layer == AnnotationLayer.PROTEIN``,
``EndAnnotationLayer.CDNA`` otherwise
:return: Data for longest compatible transcript
"""
def _get_protein_rep(
gene: str | None,
pro_ac: str,
lcr_c_data_pos: tuple[int, int],
strand: Strand,
status: TranscriptPriority,
) -> DataRepresentation:
"""Get longest compatible remaining protein representation
:param gene: HGNC gene symbol
:param pro_ac: Protein accession
:param lcr_c_data_pos: Longest compatible remaining position
:param strand: Strand
:param status: Status for `pro_ac`
:return: Protein representation for longest compatible remaining result
"""
return DataRepresentation(
gene=gene,
refseq=pro_ac if pro_ac.startswith("N") else None,
ensembl=pro_ac if pro_ac.startswith("E") else None,
pos=self._c_to_p_pos(lcr_c_data_pos),
strand=strand,
status=status,
)
lcr_result = None
start_pos, end_pos = get_inter_residue_pos(start_pos, end_pos, coordinate_type)
coordinate_type = CoordinateType.INTER_RESIDUE
is_p_or_c_start_anno = True
if start_annotation_layer == AnnotationLayer.PROTEIN:
c_start_pos, c_end_pos = self._p_to_c_pos(start_pos, end_pos)
elif start_annotation_layer == AnnotationLayer.CDNA:
c_start_pos, c_end_pos = start_pos, end_pos
else:
is_p_or_c_start_anno = False
# Data Frame that contains transcripts associated to a gene
if is_p_or_c_start_anno:
df = await self.uta_db.get_transcripts(
c_start_pos, c_end_pos, gene=gene, use_tx_pos=True, alt_ac=alt_ac
)
else:
df = await self.uta_db.get_transcripts(
start_pos, end_pos, gene=gene, use_tx_pos=False, alt_ac=alt_ac
)
if df.is_empty():
_logger.warning("Unable to get transcripts from gene %s", gene)
return lcr_result
prioritized_tx_acs = self._get_prioritized_transcripts_from_gene(df)
if mane_transcripts:
# Dont check MANE transcripts since we know that are not compatible
prioritized_tx_acs = [
el for el in prioritized_tx_acs if el not in mane_transcripts
]
for tx_ac in prioritized_tx_acs:
# Only need to check the one row since we do liftover in _c_to_g
tmp_df = df.filter(pl.col("tx_ac") == tx_ac).sort(
by="alt_ac", descending=True
)
row = tmp_df[0].to_dicts()[0]
if alt_ac is None:
alt_ac = row["alt_ac"]
found_tx_exon_aln_result = False
if is_p_or_c_start_anno:
# Go from c -> g annotation (liftover as well)
g = await self._c_to_g(tx_ac, (c_start_pos, c_end_pos))
else:
# g -> GRCh38 (if alt_ac not provided. if it is, will use that assembly)
g = await self._get_and_validate_genomic_tx_data(
tx_ac,
(start_pos, end_pos),
annotation_layer=AnnotationLayer.GENOMIC,
alt_ac=alt_ac,
)
found_tx_exon_aln_result = True
if not g:
continue
# Get prioritized transcript data for gene
# grch38 -> c
lcr_c_data: CdnaRepresentation | None = await self._g_to_c(
g=g,
refseq_c_ac=tx_ac,
status=TranscriptPriority.LONGEST_COMPATIBLE_REMAINING,
found_result=found_tx_exon_aln_result,
)
if not lcr_c_data:
continue
# Validation checks
if is_p_or_c_start_anno:
validate_reading_frame = self._validate_reading_frames(
tx_ac, c_start_pos, c_end_pos, lcr_c_data
)
if not validate_reading_frame:
continue
if ref:
if start_annotation_layer == AnnotationLayer.PROTEIN:
valid_references = self._validate_references(
row["pro_ac"],
row["cds_start_i"],
start_pos,
end_pos,
{},
ref,
AnnotationLayer.PROTEIN,
coordinate_type,
)
elif start_annotation_layer == AnnotationLayer.CDNA:
valid_references = self._validate_references(
row["tx_ac"],
row["cds_start_i"],
c_start_pos,
c_end_pos,
{},
ref,
AnnotationLayer.CDNA,
coordinate_type,
)
else:
valid_references = self._validate_references(
alt_ac,
0,
start_pos,
end_pos,
{},
ref,
AnnotationLayer.GENOMIC,
coordinate_type,
)
if not valid_references:
continue
if not end_annotation_layer:
if start_annotation_layer == AnnotationLayer.PROTEIN:
end_annotation_layer = EndAnnotationLayer.PROTEIN
else:
end_annotation_layer = EndAnnotationLayer.CDNA
if end_annotation_layer in {
EndAnnotationLayer.CDNA,
EndAnnotationLayer.PROTEIN,
}:
if end_annotation_layer == EndAnnotationLayer.CDNA:
lcr_result = lcr_c_data
coding_start_site = lcr_result.coding_start_site
else:
lcr_result = _get_protein_rep(
gene,
row["pro_ac"],
lcr_c_data.pos,
g.strand,
lcr_c_data.status,
)
coding_start_site = 0
ac = lcr_result.refseq or lcr_result.ensembl
pos = lcr_result.pos
if not self.validate_index(ac, pos, coding_start_site):
_logger.warning(
"%s are not valid positions on %s with coding start site %s",
pos,
ac,
coding_start_site,
)
continue
return lcr_result
lcr_result = ProteinAndCdnaRepresentation(
protein=_get_protein_rep(
gene,
row["pro_ac"],
lcr_c_data.pos,
g.strand,
lcr_c_data.status,
),
cdna=lcr_c_data,
)
lcr_result_dict = lcr_result.model_dump()
valid = True
for k in lcr_result_dict:
cds = lcr_result_dict[k].get("coding_start_site", 0)
ac = lcr_result_dict[k]["refseq"] or lcr_result_dict[k]["ensembl"]
pos = lcr_result_dict[k]["pos"]
if not self.validate_index(ac, pos, cds):
valid = False
_logger.warning(
"%s are not valid positions on %s with coding start site %s",
pos,
ac,
cds,
)
break
if valid:
return lcr_result
return lcr_result
async def get_mane_transcript(
self,
ac: str,
start_pos: int,
end_pos: int,
start_annotation_layer: AnnotationLayer,
gene: str | None = None,
ref: str | None = None,
try_longest_compatible: bool = False,
coordinate_type: Literal[CoordinateType.RESIDUE]
| Literal[CoordinateType.INTER_RESIDUE] = CoordinateType.RESIDUE,
) -> DataRepresentation | CdnaRepresentation | None:
"""Return MANE representation
If ``start_annotation_layer`` is ``AnnotationLayer.PROTEIN``, will return
``AnnotationLayer.PROTEIN`` representation.
If ``start_annotation_layer`` is ``AnnotationLayer.CDNA``, will return
``AnnotationLayer.CDNA`` representation.
If ``start_annotation_layer`` is ``AnnotationLayer.GENOMIC`` will return
``AnnotationLayer.CDNA`` representation if ``gene`` is provided and
``AnnotationLayer.GENOMIC`` GRCh38 representation if ``gene`` is NOT
provided.
>>> from cool_seq_tool import CoolSeqTool
>>> from cool_seq_tool.schemas import AnnotationLayer, CoordinateType
>>> import asyncio
>>> mane_mapper = CoolSeqTool().mane_transcript
>>> result = asyncio.run(
... mane_mapper.get_mane_transcript(
... "NP_004324.2",
... 599,
... AnnotationLayer.PROTEIN,
... coordinate_type=CoordinateType.INTER_RESIDUE,
... )
... )
>>> result.gene, result.refseq, result.status
('BRAF', 'NP_004324.2', <TranscriptPriority.MANE_SELECT: 'mane_select'>)