jbrowse-plugin-msaview 2.4.5 → 2.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,11 +5,10 @@ import { addDisposer, types } from '@jbrowse/mobx-state-tree';
5
5
  import { genomeToTranscriptSeqMapping } from 'g2p_mapper';
6
6
  import { autorun } from 'mobx';
7
7
  import { MSAModelF } from 'react-msaview';
8
- import { autoConnectStructures, highlightConnectedStructures, launchBlastIfNeeded, loadStoredData, observeProteinHighlights, processInit, runCleanup, storeDataToIndexedDB, } from './afterCreateAutoruns';
9
- import { genomeToMSA } from './genomeToMSA';
8
+ import { autoConnectStructures, highlightConnectedStructures, launchBlastIfNeeded, loadStoredData, observeProteinHighlights, processInit, runCleanup, storeDataToIndexedDB, syncGenomeHoverToMsaColumn, } from './afterCreateAutoruns';
10
9
  import { msaCoordToGenomeCoord } from './msaCoordToGenomeCoord';
11
10
  import { buildAlignmentMaps, runPairwiseAlignment } from './pairwiseAlignment';
12
- import { getProteinViews, ungappedToGappedPosition, } from './structureConnection';
11
+ import { getProteinViews } from './structureConnection';
13
12
  import { getCanonicalRefName } from './util';
14
13
  const ConnectStructureDialog = lazy(() => import('./components/ConnectStructureDialog'));
15
14
  /**
@@ -132,37 +131,6 @@ export default function stateModelFactory() {
132
131
  },
133
132
  }))
134
133
  .views(self => ({
135
- /**
136
- * #getter
137
- */
138
- get structureHoverCol() {
139
- for (const conn of self.connectedProteinViews) {
140
- const structure = conn.proteinView.structures[conn.structureIdx];
141
- const structurePos = structure?.hoverPosition?.structureSeqPos;
142
- if (structurePos !== undefined) {
143
- const msaUngapped = conn.structureToMsa[structurePos];
144
- if (msaUngapped !== undefined) {
145
- const seq = self.getSequenceByRowName(conn.msaRowName);
146
- if (seq) {
147
- const globalCol = ungappedToGappedPosition(seq, msaUngapped);
148
- if (globalCol !== undefined) {
149
- return self.globalColToVisibleCol(globalCol);
150
- }
151
- }
152
- }
153
- }
154
- }
155
- return undefined;
156
- },
157
- }))
158
- .views(self => ({
159
- /**
160
- * #getter
161
- */
162
- get mouseCol2() {
163
- return (self.structureHoverCol ??
164
- genomeToMSA({ model: self }));
165
- },
166
134
  /**
167
135
  * #getter
168
136
  */
@@ -296,13 +264,12 @@ export default function stateModelFactory() {
296
264
  throw new Error('Structure sequence not available');
297
265
  }
298
266
  const alignment = runPairwiseAlignment(ungappedMsaSequence, structureSequence);
299
- const { seq1ToSeq2, seq2ToSeq1 } = buildAlignmentMaps(alignment);
267
+ const { seq1ToSeq2 } = buildAlignmentMaps(alignment);
300
268
  const connection = {
301
269
  proteinViewId,
302
270
  structureIdx,
303
271
  msaRowName: rowName,
304
272
  msaToStructure: Object.fromEntries(seq1ToSeq2),
305
- structureToMsa: Object.fromEntries(seq2ToSeq1),
306
273
  };
307
274
  self.connectedStructures.push(connection);
308
275
  },
@@ -392,6 +359,7 @@ export default function stateModelFactory() {
392
359
  fn(self);
393
360
  }));
394
361
  }
362
+ addDisposer(self, autorun(syncGenomeHoverToMsaColumn(self)));
395
363
  },
396
364
  }));
397
365
  }
@@ -93,15 +93,8 @@ function buildConsensus(alignedSeq1, alignedSeq2) {
93
93
  for (let i = 0; i < alignedSeq1.length; i++) {
94
94
  const a = alignedSeq1[i];
95
95
  const b = alignedSeq2[i];
96
- if (a === '-' || b === '-') {
97
- consensus += ' ';
98
- }
99
- else if (a.toUpperCase() === b.toUpperCase()) {
100
- consensus += '|';
101
- }
102
- else {
103
- consensus += ' ';
104
- }
96
+ const match = a !== '-' && b !== '-' && a.toUpperCase() === b.toUpperCase();
97
+ consensus += match ? '|' : ' ';
105
98
  }
106
99
  return consensus;
107
100
  }
@@ -38,14 +38,8 @@ export interface StructureConnection {
38
38
  msaRowName: string;
39
39
  /** Map from MSA ungapped position to structure sequence position */
40
40
  msaToStructure: Record<number, number>;
41
- /** Map from structure sequence position to MSA ungapped position */
42
- structureToMsa: Record<number, number>;
43
41
  }
44
42
  /**
45
43
  * Helper to convert gapped MSA column to ungapped position for a specific row
46
44
  */
47
45
  export declare function gappedToUngappedPosition(sequence: string, gappedPosition: number): number | undefined;
48
- /**
49
- * Helper to convert ungapped position to gapped MSA column for a specific row
50
- */
51
- export declare function ungappedToGappedPosition(sequence: string, ungappedPosition: number): number | undefined;
@@ -27,19 +27,3 @@ export function gappedToUngappedPosition(sequence, gappedPosition) {
27
27
  }
28
28
  return ungapped;
29
29
  }
30
- /**
31
- * Helper to convert ungapped position to gapped MSA column for a specific row
32
- */
33
- export function ungappedToGappedPosition(sequence, ungappedPosition) {
34
- let ungapped = 0;
35
- for (let i = 0; i < sequence.length; i++) {
36
- const element = sequence[i];
37
- if (element !== '-') {
38
- if (ungapped === ungappedPosition) {
39
- return i;
40
- }
41
- ungapped++;
42
- }
43
- }
44
- return undefined;
45
- }
@@ -1,5 +1,5 @@
1
1
  import { describe, expect, test } from 'vitest';
2
- import { gappedToUngappedPosition, ungappedToGappedPosition, } from './structureConnection';
2
+ import { gappedToUngappedPosition } from './structureConnection';
3
3
  describe('gappedToUngappedPosition', () => {
4
4
  test('returns correct ungapped position for non-gap character', () => {
5
5
  const seq = 'M-KA-A';
@@ -51,53 +51,3 @@ describe('gappedToUngappedPosition', () => {
51
51
  expect(gappedToUngappedPosition(seq, 2)).toBeUndefined();
52
52
  });
53
53
  });
54
- describe('ungappedToGappedPosition', () => {
55
- test('returns correct gapped position', () => {
56
- const seq = 'M-KA-A';
57
- // 0 12 34 (gapped)
58
- // 0 1 23 (ungapped)
59
- expect(ungappedToGappedPosition(seq, 0)).toBe(0); // M
60
- expect(ungappedToGappedPosition(seq, 1)).toBe(2); // K
61
- expect(ungappedToGappedPosition(seq, 2)).toBe(3); // A
62
- expect(ungappedToGappedPosition(seq, 3)).toBe(5); // A
63
- });
64
- test('returns undefined for out-of-bounds ungapped position', () => {
65
- const seq = 'M-KA';
66
- expect(ungappedToGappedPosition(seq, 4)).toBeUndefined();
67
- expect(ungappedToGappedPosition(seq, 100)).toBeUndefined();
68
- });
69
- test('handles sequence with no gaps', () => {
70
- const seq = 'MKAA';
71
- expect(ungappedToGappedPosition(seq, 0)).toBe(0);
72
- expect(ungappedToGappedPosition(seq, 1)).toBe(1);
73
- expect(ungappedToGappedPosition(seq, 2)).toBe(2);
74
- expect(ungappedToGappedPosition(seq, 3)).toBe(3);
75
- });
76
- test('handles sequence with leading gaps', () => {
77
- const seq = '--MKA';
78
- expect(ungappedToGappedPosition(seq, 0)).toBe(2); // M
79
- expect(ungappedToGappedPosition(seq, 1)).toBe(3); // K
80
- expect(ungappedToGappedPosition(seq, 2)).toBe(4); // A
81
- });
82
- test('handles empty sequence', () => {
83
- expect(ungappedToGappedPosition('', 0)).toBeUndefined();
84
- });
85
- test('handles all-gap sequence', () => {
86
- const seq = '---';
87
- expect(ungappedToGappedPosition(seq, 0)).toBeUndefined();
88
- });
89
- });
90
- describe('gappedToUngappedPosition and ungappedToGappedPosition are inverses', () => {
91
- test('round-trip conversion works', () => {
92
- const seq = 'M-KA--YL-S';
93
- // For each non-gap position, converting to ungapped and back should return original
94
- for (let i = 0; i < seq.length; i++) {
95
- if (seq[i] !== '-') {
96
- const ungapped = gappedToUngappedPosition(seq, i);
97
- expect(ungapped).toBeDefined();
98
- const backToGapped = ungappedToGappedPosition(seq, ungapped);
99
- expect(backToGapped).toBe(i);
100
- }
101
- }
102
- });
103
- });
@@ -0,0 +1,92 @@
1
+ import { getSession } from '@jbrowse/core/util';
2
+ import { beforeEach, describe, expect, test, vi } from 'vitest';
3
+ import { syncGenomeHoverToMsaColumn } from './afterCreateAutoruns';
4
+ // Mock only getSession; keep the rest of the util module real so the
5
+ // afterCreateAutoruns import graph still loads.
6
+ vi.mock('@jbrowse/core/util', async (importOriginal) => ({
7
+ ...(await importOriginal()),
8
+ getSession: vi.fn(),
9
+ }));
10
+ const mockGetSession = vi.mocked(getSession);
11
+ const mafRegion = {
12
+ refName: 'chr1',
13
+ start: 1000,
14
+ end: 1010,
15
+ assemblyName: 'hg38',
16
+ };
17
+ // A model wired through the real genomeToMSA path: a connected genome view
18
+ // over a maf region, with seqPosToVisibleCol as identity so the asserted
19
+ // column equals the ungapped offset into the region.
20
+ function makeModel() {
21
+ const calls = [];
22
+ const model = {
23
+ querySeqName: 'hg38.chr1',
24
+ transcriptToMsaMap: undefined,
25
+ mafRegion,
26
+ connectedView: { initialized: true, assemblyNames: ['hg38'] },
27
+ seqPosToVisibleCol: (_name, pos) => pos,
28
+ setMousePos: (col) => {
29
+ calls.push(col);
30
+ },
31
+ };
32
+ return { model, calls };
33
+ }
34
+ function hoverGenome(coord) {
35
+ mockGetSession.mockReturnValue({
36
+ hovered: { hoverFeature: {}, hoverPosition: { coord, refName: 'chr1' } },
37
+ });
38
+ }
39
+ function clearGenomeHover() {
40
+ mockGetSession.mockReturnValue({
41
+ hovered: null,
42
+ });
43
+ }
44
+ describe('syncGenomeHoverToMsaColumn (real genomeToMSA mapping)', () => {
45
+ beforeEach(() => {
46
+ vi.clearAllMocks();
47
+ });
48
+ test('genome hover at coord 1005 highlights MSA column 5', () => {
49
+ const { model, calls } = makeModel();
50
+ const run = syncGenomeHoverToMsaColumn(model);
51
+ hoverGenome(1005); // 1005 - mafRegion.start(1000) = ungapped 5
52
+ run();
53
+ expect(calls).toEqual([5]);
54
+ });
55
+ test('moving the genome hover moves the highlighted column', () => {
56
+ const { model, calls } = makeModel();
57
+ const run = syncGenomeHoverToMsaColumn(model);
58
+ hoverGenome(1002);
59
+ run();
60
+ hoverGenome(1007);
61
+ run();
62
+ expect(calls).toEqual([2, 7]);
63
+ });
64
+ test('leaving the genome clears the column it set', () => {
65
+ const { model, calls } = makeModel();
66
+ const run = syncGenomeHoverToMsaColumn(model);
67
+ hoverGenome(1004);
68
+ run();
69
+ clearGenomeHover();
70
+ run();
71
+ expect(calls).toEqual([4, undefined]);
72
+ });
73
+ test('a hover outside the maf region clears a previously-set column once', () => {
74
+ const { model, calls } = makeModel();
75
+ const run = syncGenomeHoverToMsaColumn(model);
76
+ hoverGenome(1004);
77
+ run();
78
+ hoverGenome(5000); // outside [1000,1010) -> genomeToMSA returns undefined
79
+ run();
80
+ run();
81
+ expect(calls).toEqual([4, undefined]);
82
+ });
83
+ test('never touches mouseCol when the genome never provides a column, so a direct MSA hover survives unrelated session hovers', () => {
84
+ const { model, calls } = makeModel();
85
+ const run = syncGenomeHoverToMsaColumn(model);
86
+ clearGenomeHover();
87
+ run();
88
+ hoverGenome(9999); // unrelated/out-of-range hover elsewhere
89
+ run();
90
+ expect(calls).toEqual([]);
91
+ });
92
+ });