Skip to content

Commit

Permalink
Move save file types to base track model
Browse files Browse the repository at this point in the history
  • Loading branch information
cmdcolin committed Nov 17, 2023
1 parent c1c051a commit 55caa7d
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 19 deletions.
31 changes: 30 additions & 1 deletion packages/core/pluggableElementTypes/models/BaseTrackModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ import { getContainingView, getEnv, getSession } from '../../util'
import { isSessionModelWithConfigEditing } from '../../util/types'
import { ElementId } from '../../util/types/mst'

// locals
import { stringifyGFF3 } from './saveTrackFileTypes/gff3'
import { stringifyGBK } from './saveTrackFileTypes/genbank'
import { stringifyBED } from './saveTrackFileTypes/bed'

// lazies
const SaveTrackDataDlg = lazy(() => import('./components/SaveTrackData'))

Expand Down Expand Up @@ -187,6 +192,27 @@ export function createBaseTrackModel(
})
},
}))
.views(() => ({
saveTrackFileFormatOptions() {
return {
gff3: {
name: 'GFF3',
extension: 'gff3',
callback: stringifyGFF3,
},
genbank: {
name: 'GenBank',
extension: 'gbk',
callback: stringifyGBK,
},
bed: {
name: 'BED',
extension: 'bed',
callback: stringifyBED,
},
}
},
}))
.views(self => ({
/**
* #method
Expand All @@ -206,7 +232,10 @@ export function createBaseTrackModel(
onClick: () => {
getSession(self).queueDialog(handleClose => [
SaveTrackDataDlg,
{ model: self, handleClose },
{
model: self,
handleClose,
},
])
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,13 @@ import {
Feature,
Region,
AbstractTrackModel,
AbstractSessionModel,
} from '@jbrowse/core/util'
import { getConf } from '@jbrowse/core/configuration'

// icons
import GetAppIcon from '@mui/icons-material/GetApp'

// locals
import { stringifyGFF3 } from './gff3'
import { stringifyGBK } from './genbank'
import { stringifyBED } from './bed'

const useStyles = makeStyles()({
root: {
width: '80em',
Expand All @@ -57,24 +53,29 @@ async function fetchFeatures(
signal,
}) as Promise<Feature[]>
}

interface FileTypeExporter {
name: string
extension: string
callback: (arg: {
features: Feature[]
session: AbstractSessionModel
assemblyName: string
}) => Promise<string> | string
}
const SaveTrackDataDialog = observer(function ({
model,
handleClose,
}: {
model: AbstractTrackModel
model: AbstractTrackModel & {
saveTrackFileFormatOptions: () => Record<string, FileTypeExporter>
}
handleClose: () => void
}) {
const { classes } = useStyles()
const [error, setError] = useState<unknown>()
const [features, setFeatures] = useState<Feature[]>()
const [type, setType] = useState('gff3')
const [str, setStr] = useState('')
const options = {
gff3: { name: 'GFF3', extension: 'gff3', callback: stringifyGFF3 },
genbank: { name: 'GenBank', extension: 'gbk', callback: stringifyGBK },
bed: { name: 'BED', extension: 'bed', callback: stringifyBED },
}

useEffect(() => {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
Expand All @@ -90,6 +91,8 @@ const SaveTrackDataDialog = observer(function ({
})()
}, [model])

const options = model.saveTrackFileFormatOptions()

useEffect(() => {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
;(async () => {
Expand All @@ -99,7 +102,7 @@ const SaveTrackDataDialog = observer(function ({
if (!features) {
return
}
const generator = options[type as keyof typeof options] || {
const generator = options[type] || {
callback: () => 'Unknown',
}
setStr(
Expand All @@ -115,13 +118,12 @@ const SaveTrackDataDialog = observer(function ({
})()
}, [type, features, options, model])

const loading = !features
return (
<Dialog maxWidth="xl" open onClose={handleClose} title="Save track data">
<DialogContent className={classes.root}>
{error ? <ErrorMessage error={error} /> : null}
{!features ? (
<LoadingEllipses />
) : !features.length ? (
{features && !features.length ? (
<Typography>No features found</Typography>
) : null}

Expand All @@ -145,7 +147,9 @@ const SaveTrackDataDialog = observer(function ({
maxRows={15}
fullWidth
value={
str.length > 100_000
loading
? 'Loading...'
: str.length > 100_000
? 'Too large to view here, click "Download" to results to file'
: str
}
Expand All @@ -160,7 +164,7 @@ const SaveTrackDataDialog = observer(function ({
<DialogActions>
<Button
onClick={() => {
const ext = options[type as keyof typeof options].extension
const ext = options[type].extension
const blob = new Blob([str], { type: 'text/plain;charset=utf-8' })
saveAs(blob, `jbrowse_track_data.${ext}`)
}}
Expand Down

0 comments on commit 55caa7d

Please sign in to comment.