mirror of
https://github.com/chanzuckerberg/cellxgene.git
synced 2026-10-08 05:38:11 +08:00
fix bugs surrounding gene adding (#919)
* fix add gene button * make bulk add case insensitive * increase performance, incl memoizing * change which lists are memoized * improve hash to use ID from dataframe column * Add requested fixes, and actually make bulk add work
This commit is contained in:
@@ -22,6 +22,8 @@ import {
|
|||||||
keepAroundErrorToast
|
keepAroundErrorToast
|
||||||
} from "../framework/toasters";
|
} from "../framework/toasters";
|
||||||
|
|
||||||
|
import { memoize } from "../../util/dataframe/util";
|
||||||
|
|
||||||
const renderGene = (fuzzySortResult, { handleClick, modifiers, query }) => {
|
const renderGene = (fuzzySortResult, { handleClick, modifiers, query }) => {
|
||||||
if (!modifiers.matchesPredicate) {
|
if (!modifiers.matchesPredicate) {
|
||||||
return null;
|
return null;
|
||||||
@@ -69,10 +71,24 @@ class GeneExpression extends React.Component {
|
|||||||
super(props);
|
super(props);
|
||||||
this.state = {
|
this.state = {
|
||||||
bulkAdd: "",
|
bulkAdd: "",
|
||||||
tab: "autosuggest"
|
tab: "autosuggest",
|
||||||
|
activeItem: null
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_genesToUpper = listGenes => {
|
||||||
|
// Has to be a Map to preserve index
|
||||||
|
const upperGenes = new Map();
|
||||||
|
for (let i = 0, { length } = listGenes; i < length; i += 1) {
|
||||||
|
upperGenes.set(listGenes[i].toUpperCase(), i);
|
||||||
|
}
|
||||||
|
|
||||||
|
return upperGenes;
|
||||||
|
};
|
||||||
|
|
||||||
|
// eslint-disable-next-line react/sort-comp
|
||||||
|
_memoGenesToUpper = memoize(this._genesToUpper, arr => arr);
|
||||||
|
|
||||||
placeholderGeneNames() {
|
placeholderGeneNames() {
|
||||||
/*
|
/*
|
||||||
return a string containing gene name suggestions for use as a user hint.
|
return a string containing gene name suggestions for use as a user hint.
|
||||||
@@ -108,6 +124,7 @@ class GeneExpression extends React.Component {
|
|||||||
handleClick(g) {
|
handleClick(g) {
|
||||||
const { world, dispatch, userDefinedGenes } = this.props;
|
const { world, dispatch, userDefinedGenes } = this.props;
|
||||||
const varIndexName = world.schema.annotations.var.index;
|
const varIndexName = world.schema.annotations.var.index;
|
||||||
|
if (!g) return;
|
||||||
const gene = g.target;
|
const gene = g.target;
|
||||||
if (userDefinedGenes.indexOf(gene) !== -1) {
|
if (userDefinedGenes.indexOf(gene) !== -1) {
|
||||||
postUserErrorToast("That gene already exists");
|
postUserErrorToast("That gene already exists");
|
||||||
@@ -139,24 +156,37 @@ class GeneExpression extends React.Component {
|
|||||||
*/
|
*/
|
||||||
if (bulkAdd !== "") {
|
if (bulkAdd !== "") {
|
||||||
const genes = _.pull(_.uniq(bulkAdd.split(/[ ,]+/)), "");
|
const genes = _.pull(_.uniq(bulkAdd.split(/[ ,]+/)), "");
|
||||||
|
if (genes.length === 0) {
|
||||||
|
return keepAroundErrorToast("Must enter a gene name.");
|
||||||
|
}
|
||||||
|
const worldGenes = world.varAnnotations.col(varIndexName).asArray();
|
||||||
|
|
||||||
|
// These gene lists are unique enough where memoization is useless
|
||||||
|
const upperGenes = this._genesToUpper(genes);
|
||||||
|
const upperUserDefinedGenes = this._genesToUpper(userDefinedGenes);
|
||||||
|
|
||||||
|
const upperWorldGenes = this._memoGenesToUpper(worldGenes);
|
||||||
|
|
||||||
dispatch({ type: "bulk user defined gene start" });
|
dispatch({ type: "bulk user defined gene start" });
|
||||||
|
|
||||||
Promise.all(
|
Promise.all(
|
||||||
genes.map(gene => {
|
[...upperGenes.keys()].map(upperGene => {
|
||||||
if (gene.length === 0) {
|
if (upperUserDefinedGenes.get(upperGene) !== undefined) {
|
||||||
return keepAroundErrorToast("Must enter a gene name.");
|
|
||||||
}
|
|
||||||
if (userDefinedGenes.indexOf(gene) !== -1) {
|
|
||||||
return keepAroundErrorToast("That gene already exists");
|
return keepAroundErrorToast("That gene already exists");
|
||||||
}
|
}
|
||||||
if (
|
|
||||||
world.varAnnotations.col(varIndexName).indexOf(gene) === undefined
|
const indexOfGene = upperWorldGenes.get(upperGene);
|
||||||
) {
|
|
||||||
|
if (!indexOfGene) {
|
||||||
return keepAroundErrorToast(
|
return keepAroundErrorToast(
|
||||||
`${gene} doesn't appear to be a valid gene name.`
|
`${
|
||||||
|
genes[upperGenes.get(upperGene)]
|
||||||
|
} doesn't appear to be a valid gene name.`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
return dispatch(actions.requestUserDefinedGene(gene));
|
return dispatch(
|
||||||
|
actions.requestUserDefinedGene(worldGenes[indexOfGene])
|
||||||
|
);
|
||||||
})
|
})
|
||||||
).then(
|
).then(
|
||||||
() => dispatch({ type: "bulk user defined gene complete" }),
|
() => dispatch({ type: "bulk user defined gene complete" }),
|
||||||
@@ -175,7 +205,7 @@ class GeneExpression extends React.Component {
|
|||||||
differential
|
differential
|
||||||
} = this.props;
|
} = this.props;
|
||||||
const varIndexName = world?.schema?.annotations?.var?.index;
|
const varIndexName = world?.schema?.annotations?.var?.index;
|
||||||
const { tab, bulkAdd } = this.state;
|
const { tab, bulkAdd, activeItem } = this.state;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
@@ -234,6 +264,7 @@ class GeneExpression extends React.Component {
|
|||||||
return "";
|
return "";
|
||||||
}}
|
}}
|
||||||
itemListPredicate={filterGenes}
|
itemListPredicate={filterGenes}
|
||||||
|
onActiveItemChange={item => this.setState({ activeItem: item })}
|
||||||
itemRenderer={renderGene.bind(this)}
|
itemRenderer={renderGene.bind(this)}
|
||||||
items={
|
items={
|
||||||
world && world.varAnnotations
|
world && world.varAnnotations
|
||||||
@@ -246,6 +277,7 @@ class GeneExpression extends React.Component {
|
|||||||
className="bp3-button bp3-intent-primary"
|
className="bp3-button bp3-intent-primary"
|
||||||
data-testid={"add-gene"}
|
data-testid={"add-gene"}
|
||||||
loading={userDefinedGenesLoading}
|
loading={userDefinedGenesLoading}
|
||||||
|
onClick={() => this.handleClick(activeItem)}
|
||||||
>
|
>
|
||||||
Add gene
|
Add gene
|
||||||
</Button>
|
</Button>
|
||||||
|
|||||||
Reference in New Issue
Block a user