Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BIOMAGE-2001] Replace gene selection #759

Merged
merged 26 commits into from
Jul 22, 2022
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
52203c5
WIP - replace gene selection, add autoscroll
jszpila314 Jul 5, 2022
781d964
remove and adjust tests
jszpila314 Jul 6, 2022
3cc8488
adjust tests and comments
jszpila314 Jul 6, 2022
5d074dd
Merge branch 'master' into replace-gene-selection
jszpila314 Jul 6, 2022
7bb9824
Remove test from MarkerGeneSelection
jszpila314 Jul 6, 2022
bf58496
add functions dependent on plot
jszpila314 Jul 13, 2022
c942c35
merge master, resolve merge conflict by changing imports in marker-he…
jszpila314 Jul 13, 2022
ca35596
update dot-plot snapshot
jszpila314 Jul 15, 2022
d183f18
Merge branch 'master' into replace-gene-selection
aerlaut Jul 15, 2022
5d1b143
resolve comments
jszpila314 Jul 16, 2022
605f0e5
add search and re-order tests to dot plot
jszpila314 Jul 20, 2022
ae57b16
Merge branch 'master' into replace-gene-selection
jszpila314 Jul 20, 2022
f2a1b36
Merge branch 'master' into replace-gene-selection
jszpila314 Jul 20, 2022
6aa28d6
Merge branch 'replace-gene-selection' of https://github.com/hms-dbmi-…
jszpila314 Jul 20, 2022
f7f9fce
fix reset button in dot plot
jszpila314 Jul 20, 2022
f64769f
add comments for config change cases
jszpila314 Jul 20, 2022
9d644ad
fix scroll bugs, add scroll instructions
jszpila314 Jul 21, 2022
5c374fa
Merge branch 'master' into replace-gene-selection
jszpila314 Jul 21, 2022
599b2dc
Merge branch 'master' into replace-gene-selection
jszpila314 Jul 21, 2022
7d9edc0
fix dot plot reset and switching to marker genes
jszpila314 Jul 22, 2022
5ebe010
Merge branch 'master' into replace-gene-selection
jszpila314 Jul 22, 2022
b0d8c5d
fix ant tree colours
jszpila314 Jul 22, 2022
85e2da8
hide the plot when all genes are removed
jszpila314 Jul 22, 2022
848553e
correct comment
jszpila314 Jul 22, 2022
3e9167d
update previous config when deleting last gene
jszpila314 Jul 22, 2022
42a5ac9
fix plot name in gene selection instructions
jszpila314 Jul 22, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 5 additions & 30 deletions src/__test__/components/plots/styling/MarkerGeneSelection.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,49 +12,24 @@ import { plotTypes } from 'utils/constants';

const mockOnUpdate = jest.fn();
const mockOnReset = jest.fn();
const mockOnGeneEnter = jest.fn();

const plotType = plotTypes.DOT_PLOT;

const defaultProps = {
onUpdate: mockOnUpdate,
onReset: mockOnReset,
onGeneEnter: mockOnGeneEnter,
plotUuid: 'dotPlotMain',
experimentId: 'experimentId',
searchBarUuid: 'searchBarUuid',
};

const plotType = plotTypes.DOT_PLOT;

const markerGeneSelectionFactory = createTestComponentFactory(MarkerGeneSelection, defaultProps);

describe('MarkerGeneSelection', () => {
beforeEach(() => {
jest.clearAllMocks();
});

it('Should show the custom gene input by default', async () => {
const mockConfig = initialPlotConfigStates[plotType];

await act(async () => {
render(
markerGeneSelectionFactory({ config: mockConfig }),
);
});

// Expect screen to show the custom gene selection input by default
expect(screen.getByText(/Type in a gene name/i)).toBeInTheDocument();

// Typing genes and then pressing enter
const geneInput = screen.getByRole('combobox');

// This is not wrapped in act() because changes to the value causes a re-render
// which causes mockOnGeneEnter to lose its memory of having been called
userEvent.type(geneInput, 'ABC{enter}');

// Expect geneEnter to be called
expect(mockOnGeneEnter).toHaveBeenCalledTimes(1);
const inputValue = mockOnGeneEnter.mock.calls[0][0];

expect(inputValue).toEqual(['ABC']);
});

it('Should show the number of marker genes input', async () => {
const mockConfig = { ...initialPlotConfigStates[plotType], useMarkerGenes: true };

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,6 @@ Array [
},
Object {},
],
Array [
Object {
"data": Array [],
"filename": "Test_Experiment-dot_plot-louvain-All",
},
Object {},
],
Array [
Object {
"data": Array [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { render, screen } from '@testing-library/react';
import { mount } from 'enzyme';
import { within, fireEvent } from '@testing-library/dom';
import { within } from '@testing-library/dom';
import userEvent from '@testing-library/user-event';
import fetchMock, { enableFetchMocks } from 'jest-fetch-mock';
import _ from 'lodash';
Expand Down Expand Up @@ -85,12 +85,6 @@ const defaultProps = { experimentId };

const heatmapPageFactory = createTestComponentFactory(MarkerHeatmap, defaultProps);

// Helper function to get displayed genes from the gene input
const getDisplayedGenes = (container) => {
const genesNodeList = container.querySelectorAll('span[class*=selection-item-content]');
return Array.from(genesNodeList).map((gene) => gene.textContent);
};

// Helper function to get genes held within the tree
const getTreeGenes = (container) => {
const treeNodeList = container.querySelectorAll('span[class*=ant-tree-title]');
Expand All @@ -106,7 +100,7 @@ const getCurrentGeneOrder = (component) => {
});
newOrder.splice(0, 1);
return newOrder;
}
};

const renderHeatmapPage = async (store) => {
await act(async () => (
Expand Down Expand Up @@ -167,7 +161,7 @@ describe('Marker heatmap plot', () => {
expect(screen.getByText(/Colours/i)).toBeInTheDocument();
expect(screen.getByText(/Legend/i)).toBeInTheDocument();
});

it('Loads the plot', async () => {
await renderHeatmapPage(storeState);

Expand Down Expand Up @@ -242,19 +236,19 @@ describe('Marker heatmap plot', () => {
await renderHeatmapPage(storeState);

// Add in a new gene
// This is done because we can not insert text into the genes list input
const genesToLoad = [...markerGenesData5.order, 'FAKEGENE'];

await act(async () => {
await storeState.dispatch(loadGeneExpression(experimentId, genesToLoad, plotUuid));
});

expect(screen.getByText('FAKEGENE')).toBeInTheDocument();
// Get genes displayed in the tree
const geneTree = screen.getByRole('tree');

// The returned value is a HTML NodeList
const genesContainer = screen.getByText('FAKEGENE').closest('div[class*=selector]');
const displayedGenesList = getTreeGenes(geneTree);

const displayedGenesList = getDisplayedGenes(genesContainer);
// check the added gene is in the tree
expect(within(geneTree).getByText('FAKEGENE')).toBeInTheDocument();

// Check that the genes is ordered correctly.
// This means that FAKEGENE should not be the last in the genes list
Expand Down Expand Up @@ -287,64 +281,8 @@ describe('Marker heatmap plot', () => {
});

it('removing a gene keeps the sorted order without re-sorting', async () => {
seekFromS3
.mockReset()
// load genes list
.mockImplementationOnce(() => null)
.mockImplementationOnce((Etag) => mockWorkerResponses[Etag])
// 1st load
.mockImplementationOnce(() => null)
.mockImplementationOnce((ETag) => mockWorkerResponses[ETag])
// 2nd load
.mockImplementationOnce(() => null)
.mockImplementationOnce((ETag) => mockWorkerResponses[ETag]);

await renderHeatmapPage(storeState);

// Setting up so that there is an inserted gene in the list
const genesToLoad = [...markerGenesData5.order, 'FAKEGENE'];

await act(async () => {
// This is done because we can not insert text into the genes list input
await storeState.dispatch(loadGeneExpression(experimentId, genesToLoad, plotUuid));
});

expect(screen.getByText('FAKEGENE')).toBeInTheDocument();

// The returned value is a HTML NodeList
const genesContainer = screen.getByText('FAKEGENE').closest('div[class*=selector]');
const genesListBeforeRemoval = getDisplayedGenes(genesContainer);

// Removing the 5th gene from the list
// genesListBeforeRemoval is modified - splice removes the item from the list
const geneToRemove = genesListBeforeRemoval.splice(5, 1);
const geneRemoveButton = screen.getByText(geneToRemove).nextSibling;

userEvent.click(geneRemoveButton);

// Get newly displayed genes after the removal
const genesListAfterRemoval = getDisplayedGenes(genesContainer);

// The list of displayed genes should be in the same order as the displayed genes
expect(_.isEqual(genesListAfterRemoval, genesListBeforeRemoval)).toEqual(true);
});

it('loads the tabs under gene selection', async () => {
await renderHeatmapPage(storeState);

expect(screen.getByText(/Add\/Remove genes/i)).toBeInTheDocument();
expect(screen.getByText(/Search for and re-order genes/i)).toBeInTheDocument();
});

it('switches tabs and removes genes within the tree', async () => {
await renderHeatmapPage(storeState);

await act(async () => {
userEvent.click(screen.getByText('Search for and re-order genes'));
});
// note: clicking another tab doesn't remove previous tab from screen
// screen.getByText will find multiples of the same gene -> use within(geneTree)

const geneTree = screen.getByRole('tree');

// The genes in Data 5 should be in the tree
Expand Down Expand Up @@ -373,10 +311,6 @@ describe('Marker heatmap plot', () => {
it('searches for genes and adds a valid gene', async () => {
await renderHeatmapPage(storeState);

await act(async () => {
userEvent.click(screen.getByText('Search for and re-order genes'));
});

// check placeholder text is loaded
expect(screen.getByText('Search for genes...')).toBeInTheDocument();

Expand Down Expand Up @@ -413,17 +347,13 @@ describe('Marker heatmap plot', () => {
it('adds an already loaded gene and clears the input', async () => {
await renderHeatmapPage(storeState);

await act(async () => {
userEvent.click(screen.getByText('Search for and re-order genes'));
});

const searchBox = screen.getByRole('combobox');

userEvent.type(searchBox, 'tmem');

// this finds option for selection box in 1st tab and search box, use second element
const option = screen.getAllByTitle('Tmem176a')[1];
const option = screen.getByTitle('Tmem176a');

// expecting option to be disabled throws error, click the option instead and check reaction
await act(async () => {
userEvent.click(option, undefined, { skipPointerEventsCheck: true });
});
Expand All @@ -440,7 +370,7 @@ describe('Marker heatmap plot', () => {
});
});

// drag and drop is impossible in jest, use enzyme
// drag and drop is impossible in RTL, use enzyme
describe('Drag and drop enzyme tests', () => {
let component;
let tree;
Expand Down Expand Up @@ -471,12 +401,6 @@ describe('Drag and drop enzyme tests', () => {

await waitForComponentToPaint(component);

// activate re-order genes tab
await act(async () => {
const reorderTab = component.find('div.ant-tabs-tab-btn');
reorderTab.at(1).simulate('click');
});

component.update();

// antd renders 5 elements, use the first one
Expand Down
21 changes: 8 additions & 13 deletions src/components/plots/GeneReorderTool.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,24 @@ import React, { useEffect, useState } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import PropTypes from 'prop-types';

import { arrayMoveImmutable } from 'utils/array-move';
import { updatePlotConfig } from 'redux/actions/componentConfig';
import { loadGeneExpression } from 'redux/actions/genes';

import { arrayMoveImmutable } from 'utils/array-move';
import HierarchicalTreeGenes from 'components/plots/hierarchical-tree-genes/HierarchicalTreeGenes';

import { Space, Button } from 'antd';
import { CloseOutlined } from '@ant-design/icons';

const GeneReorderTool = (props) => {
const { plotUuid } = (props);
const { plotUuid, onDelete } = props;

const dispatch = useDispatch();

const config = useSelector((state) => state.componentConfig[plotUuid]?.config);

const experimentId = useSelector((state) => state.componentConfig[plotUuid]?.experimentId);

const loadedMarkerGenes = useSelector(
(state) => state.genes.expression.views[plotUuid]?.data,
);

// Tree from antd requires format [{key: , title: }], made from gene names from loadedMarkerGenes and config
const composeGeneTree = (treeGenes) => {
if (!treeGenes) {
if (!treeGenes.length) {
return [];
}

Expand All @@ -36,7 +30,7 @@ const GeneReorderTool = (props) => {
return data;
};

const [geneTreeData, setGeneTreeData] = useState(composeGeneTree(loadedMarkerGenes));
const [geneTreeData, setGeneTreeData] = useState([]);

useEffect(() => {
setGeneTreeData(composeGeneTree(config?.selectedGenes));
Expand All @@ -56,7 +50,7 @@ const GeneReorderTool = (props) => {
const genes = geneTreeData.map((treeNode) => treeNode.title);
genes.splice(geneKey, 1);

dispatch(loadGeneExpression(experimentId, genes, plotUuid));
onDelete(genes);
};

const renderTitles = (data) => {
Expand Down Expand Up @@ -101,6 +95,7 @@ GeneReorderTool.defaultProps = {};

GeneReorderTool.propTypes = {
plotUuid: PropTypes.string.isRequired,
onDelete: PropTypes.func.isRequired,
};

export default GeneReorderTool;
18 changes: 9 additions & 9 deletions src/components/plots/GeneSearchBar.jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import React, { useState } from 'react';
import _ from 'lodash';
import { AutoComplete } from 'antd';
import { useSelector, useDispatch } from 'react-redux';
import { loadGeneExpression } from 'redux/actions/genes';
import { useSelector } from 'react-redux';
import PropTypes from 'prop-types';

const filterGenes = (searchText, geneList, loadedGenes) => {
Expand All @@ -17,9 +16,9 @@ const filterGenes = (searchText, geneList, loadedGenes) => {
};

const GeneSearchBar = (props) => {
const { plotUuid, experimentId, searchBarUuid } = props;

const dispatch = useDispatch();
const {
plotUuid, searchBarUuid, onSelect,
} = props;

const geneList = useSelector((state) => state.genes.properties.views[searchBarUuid]?.data);

Expand All @@ -30,14 +29,15 @@ const GeneSearchBar = (props) => {
// pass reactive component as value (search text) to allow auto clear on select
const [value, setValue] = useState('');

const onSelect = (newGene) => {
const onOptionSelect = (newGene) => {
if (!geneList.includes(newGene) || config?.selectedGenes.includes(newGene)) {
return;
}

const genes = _.clone(config?.selectedGenes);
genes.push(newGene);
dispatch(loadGeneExpression(experimentId, genes, plotUuid));

onSelect(genes);
setValue('');
};

Expand All @@ -52,7 +52,7 @@ const GeneSearchBar = (props) => {
value={value}
options={options}
style={{ width: '100%' }}
onSelect={onSelect}
onSelect={onOptionSelect}
onSearch={onSearch}
placeholder='Search for genes...'
/>
Expand All @@ -61,8 +61,8 @@ const GeneSearchBar = (props) => {

GeneSearchBar.propTypes = {
plotUuid: PropTypes.string.isRequired,
experimentId: PropTypes.string.isRequired,
searchBarUuid: PropTypes.string.isRequired,
onSelect: PropTypes.func.isRequired,
};

export default GeneSearchBar;
Loading