-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rm
- Loading branch information
Showing
20 changed files
with
678 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
199 changes: 199 additions & 0 deletions
199
packages/core/pluggableElementTypes/models/components/SaveTrackData.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,199 @@ | ||
import React, { useEffect, useState } from 'react' | ||
import { | ||
Button, | ||
DialogActions, | ||
DialogContent, | ||
FormControl, | ||
FormControlLabel, | ||
FormLabel, | ||
Radio, | ||
RadioGroup, | ||
TextField, | ||
Typography, | ||
} from '@mui/material' | ||
import { IAnyStateTreeNode } from 'mobx-state-tree' | ||
import { makeStyles } from 'tss-react/mui' | ||
import { saveAs } from 'file-saver' | ||
import { observer } from 'mobx-react' | ||
import { Dialog, ErrorMessage } from '@jbrowse/core/ui' | ||
import { | ||
getSession, | ||
getContainingView, | ||
Feature, | ||
Region, | ||
AbstractTrackModel, | ||
AbstractSessionModel, | ||
} from '@jbrowse/core/util' | ||
import { getConf } from '@jbrowse/core/configuration' | ||
|
||
// icons | ||
import GetAppIcon from '@mui/icons-material/GetApp' | ||
|
||
const useStyles = makeStyles()({ | ||
root: { | ||
width: '80em', | ||
}, | ||
textAreaFont: { | ||
fontFamily: 'Courier New', | ||
}, | ||
}) | ||
|
||
async function fetchFeatures( | ||
track: IAnyStateTreeNode, | ||
regions: Region[], | ||
signal?: AbortSignal, | ||
) { | ||
const { rpcManager } = getSession(track) | ||
const adapterConfig = getConf(track, ['adapter']) | ||
const sessionId = 'getFeatures' | ||
return rpcManager.call(sessionId, 'CoreGetFeatures', { | ||
adapterConfig, | ||
regions, | ||
sessionId, | ||
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 & { | ||
saveTrackFileFormatOptions: () => Record<string, FileTypeExporter> | ||
} | ||
handleClose: () => void | ||
}) { | ||
const options = model.saveTrackFileFormatOptions() | ||
const { classes } = useStyles() | ||
const [error, setError] = useState<unknown>() | ||
const [features, setFeatures] = useState<Feature[]>() | ||
const [type, setType] = useState(Object.keys(options)[0]) | ||
const [str, setStr] = useState('') | ||
|
||
useEffect(() => { | ||
// eslint-disable-next-line @typescript-eslint/no-floating-promises | ||
;(async () => { | ||
try { | ||
const view = getContainingView(model) as { visibleRegions?: Region[] } | ||
setError(undefined) | ||
setFeatures(await fetchFeatures(model, view.visibleRegions || [])) | ||
} catch (e) { | ||
console.error(e) | ||
setError(e) | ||
} | ||
})() | ||
}, [model]) | ||
|
||
useEffect(() => { | ||
// eslint-disable-next-line @typescript-eslint/no-floating-promises | ||
;(async () => { | ||
try { | ||
const { visibleRegions } = getContainingView(model) as { | ||
visibleRegions?: Region[] | ||
} | ||
const session = getSession(model) | ||
if (!features || !visibleRegions?.length || !type) { | ||
return | ||
} | ||
const generator = options[type] || { | ||
callback: () => 'Unknown', | ||
} | ||
setStr( | ||
await generator.callback({ | ||
features, | ||
session, | ||
assemblyName: visibleRegions[0]!.assemblyName, | ||
}), | ||
) | ||
} catch (e) { | ||
setError(e) | ||
} | ||
})() | ||
}, [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 && !features.length ? ( | ||
<Typography>No features found</Typography> | ||
) : null} | ||
|
||
<FormControl> | ||
<FormLabel>File type</FormLabel> | ||
<RadioGroup | ||
value={type} | ||
onChange={e => { | ||
setType(e.target.value) | ||
}} | ||
> | ||
{Object.entries(options).map(([key, val]) => ( | ||
<FormControlLabel | ||
key={key} | ||
value={key} | ||
control={<Radio />} | ||
label={val.name} | ||
/> | ||
))} | ||
</RadioGroup> | ||
</FormControl> | ||
<TextField | ||
variant="outlined" | ||
multiline | ||
minRows={5} | ||
maxRows={15} | ||
fullWidth | ||
value={ | ||
loading | ||
? 'Loading...' | ||
: str.length > 100_000 | ||
? 'Too large to view here, click "Download" to results to file' | ||
: str | ||
} | ||
InputProps={{ | ||
readOnly: true, | ||
classes: { | ||
input: classes.textAreaFont, | ||
}, | ||
}} | ||
/> | ||
</DialogContent> | ||
<DialogActions> | ||
<Button | ||
onClick={() => { | ||
if (!type) { | ||
return | ||
} | ||
const ext = options[type]?.extension || 'unknown' | ||
const blob = new Blob([str], { type: 'text/plain;charset=utf-8' }) | ||
saveAs(blob, `jbrowse_track_data.${ext}`) | ||
}} | ||
startIcon={<GetAppIcon />} | ||
> | ||
Download | ||
</Button> | ||
|
||
<Button | ||
variant="contained" | ||
type="submit" | ||
onClick={() => { | ||
handleClose() | ||
}} | ||
> | ||
Close | ||
</Button> | ||
</DialogActions> | ||
</Dialog> | ||
) | ||
}) | ||
|
||
export default SaveTrackDataDialog |
13 changes: 13 additions & 0 deletions
13
packages/core/pluggableElementTypes/models/saveTrackFileTypes/bed.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Feature } from '@jbrowse/core/util' | ||
|
||
export function stringifyBED({ features }: { features: Feature[] }) { | ||
const fields = ['refName', 'start', 'end', 'name', 'score', 'strand'] | ||
return features | ||
.map(feature => | ||
fields | ||
.map(field => feature.get(field)) | ||
.join('\t') | ||
.trim(), | ||
) | ||
.join('\n') | ||
} |
Oops, something went wrong.