@jbrowse/plugin-wiggle
Version:
JBrowse 2 wiggle adapters, tracks, etc.
141 lines (140 loc) • 8.85 kB
JavaScript
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import { useEffect, useState } from 'react';
import { ErrorMessage, LoadingEllipses } from '@jbrowse/core/ui';
import { getContainingView, getSession, isAbortException, useLocalStorage, } from '@jbrowse/core/util';
import { getRpcSessionId } from '@jbrowse/core/util/tracks';
import { Button, DialogActions, DialogContent, FormControlLabel, Radio, RadioGroup, TextField, Typography, } from '@mui/material';
import copy from 'copy-to-clipboard';
import { saveAs } from 'file-saver';
import { observer } from 'mobx-react';
import { isAlive } from 'mobx-state-tree';
import { makeStyles } from 'tss-react/mui';
const useStyles = makeStyles()(theme => ({
textAreaFont: {
fontFamily: 'Courier New',
},
mgap: {
display: 'flex',
flexDirection: 'column',
gap: theme.spacing(4),
},
}));
const WiggleClusterDialogManuals = observer(function ({ model, handleClose, children, }) {
const { classes } = useStyles();
const [paste, setPaste] = useState('');
const [ret, setRet] = useState();
const [error, setError] = useState();
const [loading, setLoading] = useState(false);
const [showAdvanced, setShowAdvanced] = useLocalStorage('cluster-showAdvanced', false);
const [clusterMethod, setClusterMethod] = useState('single');
const [samplesPerPixel, setSamplesPerPixel] = useState('1');
useEffect(() => {
;
(async () => {
try {
setError(undefined);
setRet(undefined);
setLoading(true);
const view = getContainingView(model);
const { dynamicBlocks, bpPerPx } = view;
const { rpcManager } = getSession(model);
const { sourcesWithoutLayout, adapterConfig } = model;
const sessionId = getRpcSessionId(model);
const ret = (await rpcManager.call(sessionId, 'MultiWiggleGetScoreMatrix', {
regions: dynamicBlocks.contentBlocks,
sources: sourcesWithoutLayout,
sessionId,
adapterConfig,
bpPerPx: bpPerPx / +samplesPerPixel,
}));
setRet(ret);
}
catch (e) {
if (!isAbortException(e) && isAlive(model)) {
console.error(e);
setError(e);
}
}
finally {
setLoading(false);
}
})();
}, [model, samplesPerPixel]);
const results = ret
? `inputMatrix<-matrix(c(${Object.values(ret)
.map(val => val.join(','))
.join(',\n')}
),nrow=${Object.values(ret).length},byrow=TRUE)
rownames(inputMatrix)<-c(${Object.keys(ret)
.map(key => `'${key}'`)
.join(',')})
resultClusters<-hclust(dist(inputMatrix), method='${clusterMethod}')
cat(resultClusters$order,sep='\\n')`
: undefined;
const resultsTsv = ret
? Object.entries(ret)
.map(([key, val]) => [key, ...val].join('\t'))
.join('\n')
: undefined;
return (_jsxs(_Fragment, { children: [_jsxs(DialogContent, { children: [children, _jsxs("div", { style: { marginTop: 50 }, children: [_jsxs("div", { children: [_jsxs("div", { style: {
display: 'flex',
gap: '8px',
flexWrap: 'wrap',
marginBottom: '16px',
}, children: [_jsx(Button, { variant: "contained", onClick: () => {
saveAs(new Blob([results || ''], {
type: 'text/plain;charset=utf-8',
}), 'cluster.R');
}, children: "Download Rscript" }), ' ', "or", ' ', _jsx(Button, { variant: "contained", onClick: () => {
copy(results || '');
}, children: "Copy Rscript to clipboard" }), ' ', "or", ' ', _jsx(Button, { variant: "contained", onClick: () => {
saveAs(new Blob([resultsTsv || ''], {
type: 'text/plain;charset=utf-8',
}), 'scores.tsv');
}, children: "Download TSV" })] }), _jsx("div", { children: _jsx(Button, { variant: "contained", onClick: () => {
setShowAdvanced(!showAdvanced);
}, children: showAdvanced
? 'Hide advanced options'
: 'Show advanced options' }) }), showAdvanced ? (_jsxs("div", { children: [_jsx(Typography, { variant: "h6", children: "Advanced options" }), _jsx(RadioGroup, { children: Object.entries({
single: 'Single',
complete: 'Complete',
}).map(([key, val]) => (_jsx(FormControlLabel, { control: _jsx(Radio, { checked: clusterMethod === key, onChange: () => {
setClusterMethod(key);
} }), label: val }, key))) }), _jsxs("div", { style: { marginTop: 20 }, children: [_jsx(Typography, { children: "This procedure samples the data at each 'pixel' across the visible by default" }), _jsx(TextField, { label: "Samples per pixel (>1 for denser sampling, between 0-1 for sparser sampling)", variant: "outlined", size: "small", value: samplesPerPixel, onChange: event => {
setSamplesPerPixel(event.target.value);
} })] })] })) : null, results ? (_jsx("div", {})) : loading ? (_jsx(LoadingEllipses, { variant: "h6", title: "Generating score matrix" })) : error ? (_jsx(ErrorMessage, { error: error })) : null] }), _jsxs("div", { children: [_jsx(Typography, { variant: "subtitle2", gutterBottom: true, style: { marginTop: '16px' }, children: "Clustering Results:" }), _jsx(TextField, { multiline: true, fullWidth: true, variant: "outlined", placeholder: "Paste results from Rscript here (sequence of numbers, one per line, specifying the new ordering)", rows: 10, value: paste, onChange: event => {
setPaste(event.target.value);
}, slotProps: {
input: {
classes: {
input: classes.textAreaFont,
},
},
} })] })] })] }), _jsxs(DialogActions, { children: [_jsx(Button, { variant: "contained", onClick: () => {
const { sourcesWithoutLayout } = model;
if (sourcesWithoutLayout) {
try {
model.setLayout(paste
.split('\n')
.map(t => t.trim())
.filter(f => !!f)
.map(r => +r)
.map(idx => {
const ret = sourcesWithoutLayout[idx - 1];
if (!ret) {
throw new Error(`out of bounds at ${idx}`);
}
return ret;
}));
}
catch (e) {
console.error(e);
getSession(model).notifyError(`${e}`, e);
}
}
handleClose();
}, children: "Apply clustering" }), _jsx(Button, { variant: "contained", color: "secondary", onClick: () => {
handleClose();
}, children: "Cancel" })] })] }));
});
export default WiggleClusterDialogManuals;