Skip to content

Commit

Permalink
Convert displayedRegions from MST array of Region[] into a types.froz…
Browse files Browse the repository at this point in the history
…en<IRegion[]> to speed up large displayedRegions sets (#4423)
  • Loading branch information
cmdcolin committed Jun 14, 2024
1 parent c9cc868 commit 9cffada
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 27 deletions.
12 changes: 4 additions & 8 deletions packages/core/util/Base1DUtils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { getSnapshot } from 'mobx-state-tree'
import { Region, ViewSnap } from './index'

export interface BpOffset {
Expand Down Expand Up @@ -100,7 +99,7 @@ export function pxToBp(
offset: number
start: number
end: number
reversed: boolean
reversed?: boolean
} {
let bpSoFar = 0
const {
Expand All @@ -114,8 +113,7 @@ export function pxToBp(
const bp = (offsetPx + px) * bpPerPx
if (bp < 0) {
const r = displayedRegions[0]
const snap = getSnapshot(r)
// @ts-expect-error
const snap = r
return {
// xref https://github.com/mobxjs/mobx-state-tree/issues/1524 for Omit
...(snap as Omit<typeof snap, symbol>),
Expand All @@ -133,8 +131,7 @@ export function pxToBp(
const len = r.end - r.start
const offset = bp - bpSoFar
if (len + bpSoFar > bp && bpSoFar <= bp) {
const snap = getSnapshot(r)
// @ts-expect-error
const snap = r
return {
// xref https://github.com/mobxjs/mobx-state-tree/issues/1524 for Omit
...(snap as Omit<typeof snap, symbol>),
Expand All @@ -160,8 +157,7 @@ export function pxToBp(
const len = r.end - r.start
const offset = bp - bpSoFar + len

const snap = getSnapshot(r)
// @ts-expect-error
const snap = r
return {
// xref https://github.com/mobxjs/mobx-state-tree/issues/1524 for Omit
...(snap as Omit<typeof snap, symbol>),
Expand Down
22 changes: 8 additions & 14 deletions packages/core/util/Base1DViewModel.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { types, cast, Instance } from 'mobx-state-tree'
import { clamp } from './index'
import { clamp, sum } from './index'
import { Feature } from './simpleFeature'
import { Region, ElementId } from './types/mst'
import { ElementId } from './types/mst'
import { Region as IRegion } from './types'
import calculateDynamicBlocks from './calculateDynamicBlocks'
import calculateStaticBlocks from './calculateStaticBlocks'
Expand All @@ -23,7 +23,7 @@ const Base1DView = types
/**
* #property
*/
displayedRegions: types.array(Region),
displayedRegions: types.optional(types.frozen<IRegion[]>(), []),
/**
* #property
*/
Expand Down Expand Up @@ -76,9 +76,7 @@ const Base1DView = types
* #getter
*/
get assemblyNames() {
return [
...new Set(self.displayedRegions.map(region => region.assemblyName)),
]
return [...new Set(self.displayedRegions.map(r => r.assemblyName))]
},
/**
* #getter
Expand Down Expand Up @@ -109,9 +107,7 @@ const Base1DView = types
* #getter
*/
get totalBp() {
return self.displayedRegions
.map(a => a.end - a.start)
.reduce((a, b) => a + b, 0)
return sum(self.displayedRegions.map(a => a.end - a.start))
},
}))
.views(self => ({
Expand All @@ -133,9 +129,7 @@ const Base1DView = types
* #getter
*/
get currBp() {
return this.dynamicBlocks
.map(a => a.end - a.start)
.reduce((a, b) => a + b, 0)
return sum(this.dynamicBlocks.map(a => a.end - a.start))
},
}))
.views(self => ({
Expand Down Expand Up @@ -265,8 +259,8 @@ const Base1DView = types
.actions(self => ({
/**
* #action
* offset is the base-pair-offset in the displayed region, index is the index of the
* displayed region in the linear genome view
* offset is the base-pair-offset in the displayed region, index is the
* index of the displayed region in the linear genome view
*
* @param start - object as `{start, end, offset, index}`
* @param end - object as `{start, end, offset, index}`
Expand Down
2 changes: 1 addition & 1 deletion packages/core/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1126,7 +1126,7 @@ export interface ViewSnap {
start: number
end: number
refName: string
reversed: boolean
reversed?: boolean
assemblyName: string
})[]
}
Expand Down
5 changes: 4 additions & 1 deletion packages/core/util/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,10 @@ export function isAbstractMenuManager(
export interface NoAssemblyRegion
extends SnapshotIn<typeof MUNoAssemblyRegion> {}

/** a description of a specific genomic region. assemblyName, refName, start, end, and reversed */
/**
* a description of a specific genomic region. assemblyName, refName, start,
* end, and reversed
*/
export interface Region extends SnapshotIn<typeof MUIRegion> {}

export interface AugmentedRegion extends Region {
Expand Down
7 changes: 4 additions & 3 deletions plugins/linear-genome-view/src/LinearGenomeView/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import React, { lazy } from 'react'
import { getConf, AnyConfigurationModel } from '@jbrowse/core/configuration'
import { BaseViewModel } from '@jbrowse/core/pluggableElementTypes/models'
import { Region } from '@jbrowse/core/util/types'
import { ElementId, Region as MUIRegion } from '@jbrowse/core/util/types/mst'
import { ElementId } from '@jbrowse/core/util/types/mst'
import { Region as IRegion } from '@jbrowse/core/util/types'
import { MenuItem } from '@jbrowse/core/ui'
import {
assembleLocString,
Expand Down Expand Up @@ -175,7 +176,7 @@ export function stateModelFactory(pluginManager: PluginManager) {
* advised to use the entire set of chromosomes if your assembly is very
* fragmented
*/
displayedRegions: types.array(MUIRegion),
displayedRegions: types.optional(types.frozen<IRegion[]>(), []),

/**
* #property
Expand Down Expand Up @@ -421,7 +422,7 @@ export function stateModelFactory(pluginManager: PluginManager) {
* #getter
*/
get totalBp() {
return self.displayedRegions.reduce((a, b) => a + b.end - b.start, 0)
return sum(self.displayedRegions.map(r => r.end - r.start))
},

/**
Expand Down

0 comments on commit 9cffada

Please sign in to comment.