Skip to content

Commit

Permalink
Modularize helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
cmdcolin committed Nov 8, 2023
1 parent 7f5eba2 commit 63a30d7
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 70 deletions.
73 changes: 3 additions & 70 deletions src/MsaView/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@ import { Instance, addDisposer, cast, types } from 'mobx-state-tree'
import { autorun } from 'mobx'
import { Region } from '@jbrowse/core/util/types/mst'
import { LinearGenomeViewModel } from '@jbrowse/plugin-linear-genome-view'
import {
Feature,
SimpleFeature,
doesIntersect2,
getSession,
} from '@jbrowse/core/util'
import { SimpleFeature, doesIntersect2, getSession } from '@jbrowse/core/util'
import { checkHovered, generateMap } from './util'

type LGV = LinearGenomeViewModel
type MaybeLGV = LGV | undefined
Expand All @@ -20,18 +16,6 @@ interface IRegion {
end: number
}

function checkHovered(hovered: unknown): hovered is {
hoverFeature: Feature
hoverPosition: { coord: number; refName: string }
} {
return (
!!hovered &&
typeof hovered == 'object' &&
'hoverFeature' in hovered &&
'hoverPosition' in hovered
)
}

export default function stateModelFactory() {
return types
.compose(
Expand Down Expand Up @@ -77,58 +61,7 @@ export default function stateModelFactory() {
* #getter
*/
get transcriptToMsaMap() {
const f = new SimpleFeature(self.connectedFeature)
let iter = 0

const strand = f.get('strand')
const subs = f.children() ?? []
return strand === -1
? subs
.filter(f => f.get('type') === 'CDS')
.sort((a, b) => b.get('start') - a.get('start'))
.map(f => {
const refName = f.get('refName').replace('chr', '')
const featureStart = f.get('start')
const featureEnd = f.get('end')
const phase = f.get('phase')
const len = featureEnd - featureStart
const op = len / 3
const proteinStart = iter
const proteinEnd = iter + op
iter += op
return {
refName,
featureStart,
featureEnd,
proteinStart,
proteinEnd,
phase,
strand,
} as const
})
: subs
.filter(f => f.get('type') === 'CDS')
.sort((a, b) => a.get('start') - b.get('start'))
.map(f => {
const refName = f.get('refName').replace('chr', '')
const featureStart = f.get('start')
const featureEnd = f.get('end')
const phase = f.get('phase')
const len = featureEnd - featureStart
const op = len / 3
const proteinStart = iter
const proteinEnd = iter + op
iter += op
return {
refName,
featureStart,
featureEnd,
proteinStart,
proteinEnd,
phase,
strand,
} as const
})
return generateMap(new SimpleFeature(self.connectedFeature))
},

/**
Expand Down
68 changes: 68 additions & 0 deletions src/MsaView/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { Feature } from '@jbrowse/core/util'

// see similar function in protein3d plugin
export function generateMap(f: Feature) {
let iter = 0

const strand = f.get('strand')
const subs = f.children() ?? []
return strand === -1
? subs
.filter(f => f.get('type') === 'CDS')
.sort((a, b) => b.get('start') - a.get('start'))
.map(f => {
const refName = f.get('refName').replace('chr', '')
const featureStart = f.get('start')
const featureEnd = f.get('end')
const phase = f.get('phase')
const len = featureEnd - featureStart
const op = len / 3
const proteinStart = iter
const proteinEnd = iter + op
iter += op
return {
refName,
featureStart,
featureEnd,
proteinStart,
proteinEnd,
phase,
strand,
} as const
})
: subs
.filter(f => f.get('type') === 'CDS')
.sort((a, b) => a.get('start') - b.get('start'))
.map(f => {
const refName = f.get('refName').replace('chr', '')
const featureStart = f.get('start')
const featureEnd = f.get('end')
const phase = f.get('phase')
const len = featureEnd - featureStart
const op = len / 3
const proteinStart = iter
const proteinEnd = iter + op
iter += op
return {
refName,
featureStart,
featureEnd,
proteinStart,
proteinEnd,
phase,
strand,
} as const
})
}

export function checkHovered(hovered: unknown): hovered is {
hoverFeature: Feature
hoverPosition: { coord: number; refName: string }
} {
return (
!!hovered &&
typeof hovered == 'object' &&
'hoverFeature' in hovered &&
'hoverPosition' in hovered
)
}

0 comments on commit 63a30d7

Please sign in to comment.