-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathTranscriptWidgetEditLocation.tsx
More file actions
1212 lines (1143 loc) · 37.9 KB
/
TranscriptWidgetEditLocation.tsx
File metadata and controls
1212 lines (1143 loc) · 37.9 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
/* eslint-disable unicorn/no-nested-ternary */
/* eslint-disable unicorn/prefer-at */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable @typescript-eslint/no-unsafe-return */
import {
type AnnotationFeature,
type TranscriptPart,
} from '@apollo-annotation/mst'
import {
LocationEndChange,
LocationStartChange,
} from '@apollo-annotation/shared'
import styled from '@emotion/styled'
import {
type AbstractSessionModel,
defaultCodonTable,
revcom,
} from '@jbrowse/core/util'
import AddIcon from '@mui/icons-material/Add'
import ContentCopyIcon from '@mui/icons-material/ContentCopy'
import ContentCutIcon from '@mui/icons-material/ContentCut'
import ExpandMoreIcon from '@mui/icons-material/ExpandMore'
import RemoveIcon from '@mui/icons-material/Remove'
import {
Accordion,
AccordionDetails,
Grid,
Tooltip,
Typography,
} from '@mui/material'
import { observer } from 'mobx-react'
import React, { useRef } from 'react'
import { type OntologyRecord } from '../OntologyManager'
import { type ApolloSessionModel } from '../session'
import { copyToClipboard } from '../util/copyToClipboard'
import { StyledAccordionSummary } from './ApolloTranscriptDetailsWidget'
import { NumberTextField } from './NumberTextField'
const StyledTextField = styled(NumberTextField)(() => ({
'&.MuiFormControl-root': {
marginTop: 0,
marginBottom: 0,
width: '100%',
},
'& .MuiInputBase-input': {
fontSize: 12,
height: 20,
padding: 1,
paddingLeft: 10,
},
}))
const SequenceContainer = styled('div')({
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
textAlign: 'left',
width: '100%',
overflowWrap: 'break-word',
wordWrap: 'break-word',
wordBreak: 'break-all',
'& span': {
fontSize: 12,
},
})
const Strand = (props: { strand: 1 | -1 | undefined }) => {
const { strand } = props
return (
<div>
{strand === 1 ? (
<AddIcon />
) : strand === -1 ? (
<RemoveIcon />
) : (
<Typography component={'span'}>N/A</Typography>
)}
</div>
)
}
const minMaxExonTranscriptLocation = (
transcript: AnnotationFeature,
featureTypeOntology: OntologyRecord,
) => {
const { transcriptExonParts } = transcript
const exonParts = transcriptExonParts
.filter((part) => featureTypeOntology.isTypeOf(part.type, 'exon'))
.sort(({ min: a }, { min: b }) => a - b)
const exonMin: number = exonParts[0]?.min
const exonMax: number = exonParts[exonParts.length - 1]?.max
return [exonMin, exonMax]
}
export const TranscriptWidgetEditLocation = observer(
function TranscriptWidgetEditLocation({
assembly,
feature,
refName,
session,
}: {
feature: AnnotationFeature
refName: string
session: ApolloSessionModel
assembly: string
}) {
const { notify } = session as unknown as AbstractSessionModel
const currentAssembly = session.apolloDataStore.assemblies.get(assembly)
const refData = currentAssembly?.getByRefName(refName)
const { changeManager } = session.apolloDataStore
const seqRef = useRef<HTMLDivElement>(null)
const { changeInProgress } = session
if (!refData) {
return null
}
const { apolloDataStore } = session
const { featureTypeOntology } =
apolloDataStore.ontologyManager as unknown as {
featureTypeOntology: OntologyRecord
}
if (
!featureTypeOntology.isTypeOf(feature.type, 'transcript') &&
!featureTypeOntology.isTypeOf(feature.type, 'pseudogenic_transcript')
) {
throw new Error('Feature is not a transcript or equivalent')
}
const { cdsLocations, transcriptExonParts, strand } = feature
const [firstCDSLocation] = cdsLocations
const [exonMin, exonMax] = minMaxExonTranscriptLocation(
feature,
featureTypeOntology,
)
let cdsMin = exonMin
let cdsMax = exonMax
const cdsPresent = firstCDSLocation.length > 0
if (cdsPresent) {
const sortedCDSLocations = firstCDSLocation.toSorted(
({ min: a }, { min: b }) => a - b,
)
cdsMin = sortedCDSLocations[0].min
cdsMax = sortedCDSLocations[sortedCDSLocations.length - 1].max
}
const updateCDSLocation = (
oldLocation: number,
newLocation: number,
feature: AnnotationFeature,
isMin: boolean,
onComplete?: () => void,
): boolean => {
if (!feature.children) {
throw new Error('Transcript should have child features')
}
if (oldLocation === newLocation) {
return true
}
const cdsFeature = getMatchingCDSFeature(
feature,
featureTypeOntology,
oldLocation,
isMin,
)
if (!cdsFeature) {
notify('No matching CDS feature found', 'error')
return false
}
if (isMin && newLocation >= cdsFeature.max) {
notify('Start location should be less than CDS end location', 'error')
return false
}
if (!isMin && newLocation <= cdsFeature.min) {
notify(
'End location should be greater than CDS start location',
'error',
)
return false
}
// overlapping exon of new CDS location
const overlappingExon = getOverlappingExonForCDS(
feature,
featureTypeOntology,
newLocation,
isMin,
)
if (!overlappingExon) {
notify(
'There should be an overlapping exon for the new CDS location',
'error',
)
return false
}
const change = isMin
? new LocationStartChange({
typeName: 'LocationStartChange',
changedIds: [cdsFeature._id],
featureId: cdsFeature._id,
oldStart: cdsFeature.min,
newStart: newLocation,
assembly,
})
: new LocationEndChange({
typeName: 'LocationEndChange',
changedIds: [cdsFeature._id],
featureId: cdsFeature._id,
oldEnd: cdsFeature.max,
newEnd: newLocation,
assembly,
})
void changeManager
.submit(change)
.then(() => {
if (onComplete) {
onComplete()
}
})
.catch(() => {
notify('Error updating feature CDS position', 'error')
})
return true
}
function handleExonLocationChange(
oldLocation: number,
newLocation: number,
feature: AnnotationFeature,
isMin: boolean,
): boolean {
if (!feature.children) {
throw new Error('Transcript should have child features')
}
const { matchingExon, prevExon, nextExon } = getNeighboringExonParts(
feature,
featureTypeOntology,
oldLocation,
isMin,
)
if (!matchingExon) {
notify('No matching exon found', 'error')
return false
}
// Start location should be less than end location
if (isMin && newLocation >= matchingExon.max) {
notify(`Start location should be less than end location`, 'error')
return false
}
// End location should be greater than start location
if (!isMin && newLocation <= matchingExon.min) {
notify(`End location should be greater than start location`, 'error')
return false
}
// Changed location should be greater than end location of previous exon - give 2bp buffer
if (prevExon && prevExon.max + 2 > newLocation) {
notify(`Error while changing start location`, 'error')
return false
}
// Changed location should be less than start location of next exon - give 2bp buffer
if (nextExon && nextExon.min - 2 < newLocation) {
notify(`Error while changing end location`, 'error')
return false
}
const exonFeature = getExonFeature(
feature,
matchingExon.min,
matchingExon.max,
featureTypeOntology,
)
if (!exonFeature) {
notify('No matching exon feature found', 'error')
return false
}
const cdsFeature = getFirstCDSFeature(feature, featureTypeOntology)
// START LOCATION CHANGE
if (isMin && newLocation !== matchingExon.min) {
const startChange = new LocationStartChange({
typeName: 'LocationStartChange',
changedIds: [],
changes: [],
assembly,
})
if (prevExon) {
// update exon start location
appendStartLocationChange(exonFeature, startChange, newLocation)
} else {
const transcriptStart = feature.min
const gene = feature.parent
if (newLocation < transcriptStart) {
if (gene && newLocation < gene.min) {
// update gene start location
appendStartLocationChange(gene, startChange, newLocation)
}
// update transcript start location
appendStartLocationChange(feature, startChange, newLocation)
// update exon start location
appendStartLocationChange(exonFeature, startChange, newLocation)
} else if (newLocation > transcriptStart) {
// update exon start location
appendStartLocationChange(exonFeature, startChange, newLocation)
// update transcript start location
appendStartLocationChange(feature, startChange, newLocation)
if (gene) {
const [geneMinWithNewLoc] = geneMinMaxWithNewLocation(
gene,
feature,
newLocation,
featureTypeOntology,
isMin,
)
if (gene.min != geneMinWithNewLoc) {
// update gene start location
appendStartLocationChange(gene, startChange, geneMinWithNewLoc)
}
}
}
}
// When we change the start location of the exon overlapping with start location of the CDS
// and the new start location is greater than the CDS start location then we need to update the CDS start location
if (
cdsFeature &&
cdsFeature.min >= matchingExon.min &&
cdsFeature.min <= matchingExon.max &&
newLocation > cdsFeature.min
) {
// update CDS start location
appendStartLocationChange(cdsFeature, startChange, newLocation)
}
void changeManager.submit(startChange).catch(() => {
notify('Error updating feature exon start position', 'error')
})
}
// END LOCATION CHANGE
if (!isMin && newLocation !== matchingExon.max) {
const endChange = new LocationEndChange({
typeName: 'LocationEndChange',
changedIds: [],
changes: [],
assembly,
})
if (nextExon) {
// update exon end location
appendEndLocationChange(exonFeature, endChange, newLocation)
} else {
const transcriptEnd = feature.max
const gene = feature.parent
if (newLocation > transcriptEnd) {
if (gene && newLocation > gene.max) {
// update gene end location
appendEndLocationChange(gene, endChange, newLocation)
}
// update transcript end location
appendEndLocationChange(feature, endChange, newLocation)
// update exon end location
appendEndLocationChange(exonFeature, endChange, newLocation)
} else if (newLocation < transcriptEnd) {
// update exon end location
appendEndLocationChange(exonFeature, endChange, newLocation)
// update transcript end location
appendEndLocationChange(feature, endChange, newLocation)
if (gene) {
const [, geneMaxWithNewLoc] = geneMinMaxWithNewLocation(
gene,
feature,
newLocation,
featureTypeOntology,
isMin,
)
if (gene.max != geneMaxWithNewLoc) {
// update gene end location
appendEndLocationChange(gene, endChange, geneMaxWithNewLoc)
}
}
}
}
// When we change the end location of the exon overlapping with end location of the CDS
// and the new end location is less than the CDS end location then we need to update the CDS end location
if (
cdsFeature &&
cdsFeature.max >= matchingExon.min &&
cdsFeature.max <= matchingExon.max &&
newLocation < cdsFeature.max
) {
// update CDS end location
appendEndLocationChange(cdsFeature, endChange, newLocation)
}
void changeManager.submit(endChange).catch(() => {
notify('Error updating feature exon end position', 'error')
})
}
return true
}
const appendEndLocationChange = (
feature: AnnotationFeature,
change: LocationEndChange,
newLocation: number,
) => {
change.changedIds.push(feature._id)
change.changes.push({
featureId: feature._id,
oldEnd: feature.max,
newEnd: newLocation,
})
}
const appendStartLocationChange = (
feature: AnnotationFeature,
change: LocationStartChange,
newLocation: number,
) => {
change.changedIds.push(feature._id)
change.changes.push({
featureId: feature._id,
oldStart: feature.min,
newStart: newLocation,
})
}
const getMatchingCDSFeature = (
feature: AnnotationFeature,
featureTypeOntology: OntologyRecord,
oldCDSLocation: number,
isMin: boolean,
) => {
let cdsFeature
for (const [, child] of feature.children ?? []) {
if (!featureTypeOntology.isTypeOf(child.type, 'CDS')) {
continue
}
if (isMin && oldCDSLocation === child.min) {
cdsFeature = child
break
}
if (!isMin && oldCDSLocation === child.max) {
cdsFeature = child
break
}
}
return cdsFeature
}
const getFirstCDSFeature = (
feature: AnnotationFeature,
featureTypeOntology: OntologyRecord,
) => {
let cdsFeature
for (const [, child] of feature.children ?? []) {
if (!featureTypeOntology.isTypeOf(child.type, 'CDS')) {
continue
}
cdsFeature = child
break
}
return cdsFeature
}
const getExonFeature = (
feature: AnnotationFeature,
exonMin: number,
exonMax: number,
featureTypeOntology: OntologyRecord,
) => {
let exonFeature
for (const [, child] of feature.children ?? []) {
if (!featureTypeOntology.isTypeOf(child.type, 'exon')) {
continue
}
if (exonMin === child.min && exonMax === child.max) {
exonFeature = child
break
}
}
return exonFeature
}
const geneMinMaxWithNewLocation = (
gene: AnnotationFeature,
transcript: AnnotationFeature,
newLocation: number,
featureTypeOntology: OntologyRecord,
isMin: boolean,
) => {
const mins = []
const maxs = []
for (const [, t] of gene.children?.entries() ?? []) {
if (!featureTypeOntology.isTypeOf(t.type, 'transcript')) {
continue
}
if (t._id === transcript._id) {
if (isMin) {
mins.push(newLocation)
maxs.push(t.max)
} else {
maxs.push(newLocation)
mins.push(t.min)
}
} else {
mins.push(t.min)
maxs.push(t.max)
}
}
const newMin = Math.min(...mins)
const newMax = Math.max(...maxs)
return [newMin, newMax]
}
const getOverlappingExonForCDS = (
transcript: AnnotationFeature,
featureTypeOntology: OntologyRecord,
oldCDSLocation: number,
isMin: boolean,
) => {
const { transcriptExonParts } = transcript
let overlappingExonPart
for (const [, exonPart] of transcriptExonParts.entries()) {
if (!featureTypeOntology.isTypeOf(exonPart.type, 'exon')) {
continue
}
if (
!isMin &&
oldCDSLocation >= exonPart.min &&
oldCDSLocation <= exonPart.max
) {
overlappingExonPart = exonPart
break
}
if (
isMin &&
oldCDSLocation >= exonPart.min &&
oldCDSLocation <= exonPart.max
) {
overlappingExonPart = exonPart
break
}
}
return overlappingExonPart
}
const getNeighboringExonParts = (
transcript: AnnotationFeature,
featureTypeOntology: OntologyRecord,
oldExonLoc: number,
isMin: boolean,
) => {
const { transcriptExonParts, strand } = transcript
let matchingExon, matchingExonIdx, prevExon, nextExon
for (const [i, exonPart] of transcriptExonParts.entries()) {
if (!featureTypeOntology.isTypeOf(exonPart.type, 'exon')) {
continue
}
if (isMin && exonPart.min === oldExonLoc) {
matchingExon = exonPart
matchingExonIdx = i
break
}
if (!isMin && exonPart.max === oldExonLoc) {
matchingExon = exonPart
matchingExonIdx = i
break
}
}
if (matchingExon && matchingExonIdx !== undefined) {
if (strand === 1 && matchingExonIdx > 0) {
for (let i = matchingExonIdx - 1; i >= 0; i--) {
const prevLoc = transcriptExonParts[i]
if (featureTypeOntology.isTypeOf(prevLoc.type, 'exon')) {
prevExon = prevLoc
break
}
}
}
if (strand === -1 && matchingExonIdx < transcriptExonParts.length - 1) {
for (
let i = matchingExonIdx + 1;
i < transcriptExonParts.length;
i++
) {
const prevLoc = transcriptExonParts[i]
if (featureTypeOntology.isTypeOf(prevLoc.type, 'exon')) {
prevExon = prevLoc
break
}
}
}
if (strand === 1 && matchingExonIdx < transcriptExonParts.length - 1) {
for (
let i = matchingExonIdx + 1;
i < transcriptExonParts.length;
i++
) {
const nextLoc = transcriptExonParts[i]
if (featureTypeOntology.isTypeOf(nextLoc.type, 'exon')) {
nextExon = nextLoc
break
}
}
}
if (strand === -1 && matchingExonIdx > 0) {
for (let i = matchingExonIdx - 1; i >= 0; i--) {
const nextLoc = transcriptExonParts[i]
if (featureTypeOntology.isTypeOf(nextLoc.type, 'exon')) {
nextExon = nextLoc
break
}
}
}
}
return { matchingExon, prevExon, nextExon }
}
const getFivePrimeSpliceSite = (
loc: TranscriptPart,
prevLocIdx: number,
) => {
let spliceSite = ''
if (prevLocIdx > 0) {
const prevLoc = transcriptExonParts[prevLocIdx - 1]
if (strand === 1) {
if (prevLoc.type === 'intron') {
spliceSite = refData.getSequence(loc.min - 2, loc.min)
}
} else {
if (prevLoc.type === 'intron') {
spliceSite = revcom(refData.getSequence(loc.max, loc.max + 2))
}
}
}
spliceSite = spliceSite.toUpperCase()
return [
{
spliceSite,
color: spliceSite === 'AG' ? 'green' : 'red',
},
]
}
const getThreePrimeSpliceSite = (
loc: TranscriptPart,
nextLocIdx: number,
) => {
let spliceSite = ''
if (nextLocIdx < transcriptExonParts.length - 1) {
const nextLoc = transcriptExonParts[nextLocIdx + 1]
if (strand === 1) {
if (nextLoc.type === 'intron') {
spliceSite = refData.getSequence(loc.max, loc.max + 2)
}
} else {
if (nextLoc.type === 'intron') {
spliceSite = revcom(refData.getSequence(loc.min - 2, loc.min))
}
}
}
spliceSite = spliceSite.toUpperCase()
return [
{
spliceSite,
color: spliceSite === 'GT' ? 'green' : 'red',
},
]
}
const getTranslationSequence = () => {
let wholeSequence = ''
const [firstLocation] = cdsLocations
const sortedCDSLocations = firstLocation.toSorted(
({ min: a }, { min: b }) => a - b,
)
for (const loc of sortedCDSLocations) {
wholeSequence += refData.getSequence(loc.min, loc.max)
}
if (strand === -1) {
// Original: ACGCAT
// Complement: TGCGTA
// Reverse complement: ATGCGT
wholeSequence = revcom(wholeSequence)
}
const elements = []
for (
let codonGenomicPos = 0;
codonGenomicPos < wholeSequence.length;
codonGenomicPos += 3
) {
const codonSeq = wholeSequence
.slice(codonGenomicPos, codonGenomicPos + 3)
.toUpperCase()
const protein =
defaultCodonTable[codonSeq as keyof typeof defaultCodonTable] || '&'
// highlight start codon and stop codons
if (codonSeq === 'ATG') {
elements.push(
<Typography
component={'span'}
style={{
backgroundColor: changeInProgress ? 'lightgray' : 'yellow',
cursor: 'pointer',
border: '1px solid black',
}}
key={codonGenomicPos}
onClick={() => {
if (changeInProgress) {
return
}
// NOTE: codonGenomicPos is important here for calculating the genomic location
// of the start codon. We are using the codonGenomicPos as the key in the typography
// elements to maintain the genomic postion of the codon start
const startCodonGenomicLocation =
getCodonGenomicLocation(codonGenomicPos)
if (startCodonGenomicLocation !== cdsMin && strand === 1) {
updateCDSLocation(
cdsMin,
startCodonGenomicLocation,
feature,
true,
)
}
if (startCodonGenomicLocation !== cdsMax && strand === -1) {
updateCDSLocation(
cdsMax,
startCodonGenomicLocation,
feature,
false,
)
}
}}
>
{protein}
</Typography>,
)
} else if (['TAA', 'TAG', 'TGA'].includes(codonSeq)) {
elements.push(
<Typography
style={{ backgroundColor: 'red', color: 'white' }}
component={'span'}
// Pass the codonGenomicPos as the key to maintain the genomic position of the codon
key={codonGenomicPos}
>
{protein}
</Typography>,
)
} else {
elements.push(
// Pass the codonGenomicPos as the key to maintain the genomic position of the codon
<Typography component={'span'} key={codonGenomicPos}>
{protein}
</Typography>,
)
}
}
return elements
}
// Codon position is the index of the start codon in the CDS genomic sequence
// Calculate the genomic location of the start codon based on the codon position in the CDS
const getCodonGenomicLocation = (codonGenomicPosition: number) => {
const [firstLocation] = cdsLocations
let cdsLen = 0
const sortedCDSLocations = firstLocation.toSorted(
({ min: a }, { min: b }) => a - b,
)
// Suppose CDS locations are [{min: 0, max: 10}, {min: 20, max: 30}, {min: 40, max: 50}]
// and codonGenomicPosition is 25
// ((10 - 0) + (30 - 20) + (50 - 40)) > 25
// So, start codon is in (40, 50)
// 40 + (25-20) = 45 is the genomic location of the start codon
if (strand === 1) {
for (const loc of sortedCDSLocations) {
const locLength = loc.max - loc.min
if (cdsLen + locLength > codonGenomicPosition) {
return loc.min + (codonGenomicPosition - cdsLen)
}
cdsLen += locLength
}
} else if (strand === -1) {
for (let i = sortedCDSLocations.length - 1; i >= 0; i--) {
const loc = sortedCDSLocations[i]
const locLength = loc.max - loc.min
if (cdsLen + locLength > codonGenomicPosition) {
return loc.max - (codonGenomicPosition - cdsLen)
}
cdsLen += locLength
}
}
if (strand === 1) {
return cdsMin
}
return cdsMax
}
const trimTranslationSequence = () => {
const sequenceElements = getTranslationSequence()
const translationSequence = sequenceElements
.map((el) => el.props.children)
.join('')
if (
translationSequence.startsWith('M') &&
translationSequence.endsWith('*')
) {
return
}
// NOTE: We are maintaining the genomic location of the codon start as the "key"
// in typography elements. See getTranslationSequence function
const translSeqCodonStartGenomicPosArr = []
for (const el of sequenceElements) {
translSeqCodonStartGenomicPosArr.push({
codonGenomicPos: el.key,
sequenceLetter: el.props.children,
})
}
if (translSeqCodonStartGenomicPosArr.length === 0) {
return
}
// Trim any sequence before first start codon and after stop codon
const startCodonIndex = translationSequence.indexOf('M')
const stopCodonIndex = translationSequence.indexOf('*')
const startCodonPos =
translSeqCodonStartGenomicPosArr[startCodonIndex].codonGenomicPos
const stopCodonPos =
translSeqCodonStartGenomicPosArr[stopCodonIndex].codonGenomicPos
if (!startCodonPos || !stopCodonPos) {
return
}
const startCodonGenomicLoc = getCodonGenomicLocation(
startCodonPos as unknown as number,
)
let stopCodonGenomicLoc = getCodonGenomicLocation(
stopCodonPos as unknown as number,
)
if (strand === 1) {
if (startCodonGenomicLoc > stopCodonGenomicLoc) {
notify(
'Start codon genomic location should be less than stop codon genomic location',
'error',
)
return
}
let promise
stopCodonGenomicLoc += 3 // move to end of stop codon
if (startCodonGenomicLoc !== cdsMin) {
promise = new Promise((resolve) => {
updateCDSLocation(
cdsMin,
startCodonGenomicLoc,
feature,
true,
() => {
resolve(true)
},
)
})
}
if (stopCodonGenomicLoc !== cdsMax) {
if (promise) {
void promise.then(() => {
updateCDSLocation(cdsMax, stopCodonGenomicLoc, feature, false)
})
} else {
updateCDSLocation(cdsMax, stopCodonGenomicLoc, feature, false)
}
}
}
if (strand === -1) {
// reverse strand
if (startCodonGenomicLoc < stopCodonGenomicLoc) {
notify(
'Start codon genomic location should be less than stop codon genomic location',
'error',
)
return
}
let promise
stopCodonGenomicLoc -= 3 // move to end of stop codon
if (startCodonGenomicLoc !== cdsMax) {
promise = new Promise((resolve) => {
updateCDSLocation(
cdsMax,
startCodonGenomicLoc,
feature,
false,
() => {
resolve(true)
},
)
})
}
if (stopCodonGenomicLoc !== cdsMin) {
if (promise) {
void promise.then(() => {
updateCDSLocation(cdsMin, stopCodonGenomicLoc, feature, true)
})
} else {
updateCDSLocation(cdsMin, stopCodonGenomicLoc, feature, true)
}
}
}
notify('Translation sequence trimmed to start and stop codons', 'success')
}
const onCopyClick = () => {
const seqDiv = seqRef.current
if (!seqDiv) {
return
}
void copyToClipboard(seqDiv)
}
return (
<div>
{cdsPresent && (
<div>
<Accordion>
<StyledAccordionSummary
expandIcon={<ExpandMoreIcon style={{ color: 'white' }} />}
aria-controls="panel1-content"
id="panel1-header"
>
<Typography component="span" fontWeight={'bold'}>
Translation
</Typography>
</StyledAccordionSummary>
<AccordionDetails>
<SequenceContainer>
<Typography
component={'span'}
ref={seqRef}
style={{ maxHeight: 120, overflowY: 'scroll' }}
>
{getTranslationSequence()}
</Typography>
</SequenceContainer>
<div
style={{
marginTop: 10,
display: 'flex',
flexDirection: 'row',
alignItems: 'center',
gap: 10,
}}
>
<Tooltip title="Copy">
<button
onClick={onCopyClick}
style={{ border: 'none', background: 'none', padding: 0 }}
disabled={changeInProgress}
>
<ContentCopyIcon style={{ fontSize: 15 }} />
</button>
</Tooltip>
<Tooltip title="Trim">
<button
onClick={trimTranslationSequence}
style={{ border: 'none', background: 'none', padding: 0 }}
disabled={changeInProgress}
>