Skip to content

Commit

Permalink
Pre-select epigenomes if none are selected and metadata api payload c…
Browse files Browse the repository at this point in the history
…ontains default selection instructions
  • Loading branch information
azangru committed Dec 9, 2024
1 parent 794f070 commit ab1c6de
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import { useAppSelector } from 'src/store';
import { getActiveGenomeId } from 'src/content/app/regulatory-activity-viewer/state/general/generalSelectors';
import { getMainContentBottomView } from 'src/content/app/regulatory-activity-viewer/state/ui/uiSelectors';

import usePreselectedEpigenomes from './hooks/usePreselectedEpigenomes';

import { StandardAppLayout } from 'src/shared/components/layout';
import RegionOverview from './components/region-overview/RegionOverview';
import RegionActivitySection from './components/region-activity-section/RegionActivitySection';
Expand All @@ -32,6 +34,7 @@ import SelectedEpigenomes from './components/selected-epigenomes/SelectedEpigeno

const ActivityViewer = () => {
const activeGenomeId = useAppSelector(getActiveGenomeId);
usePreselectedEpigenomes();

return (
<StandardAppLayout
Expand All @@ -56,6 +59,7 @@ const MainContent = ({ genomeId }: { genomeId: string | null }) => {
<div>
Hello activity viewer
<RegionOverview activeGenomeId={genomeId} />
{/* The spacer divs below are temporary */}
<div style={{ margin: '0.6rem 0' }} />
<MainContentBottomViewControls genomeId={genomeId} />
<div style={{ margin: '0.6rem 0' }} />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/**
* See the NOTICE file distributed with this work for additional information
* regarding copyright ownership.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { useEffect } from 'react';

import { useAppDispatch, useAppSelector } from 'src/store';

import { getEpigenomeSelectionCriteria } from 'src/content/app/regulatory-activity-viewer/state/epigenome-selection/epigenomeSelectionSelectors';
import { getActiveGenomeId } from 'src/content/app/regulatory-activity-viewer/state/general/generalSelectors';

import { useEpigenomeMetadataDimensionsQuery } from 'src/content/app/regulatory-activity-viewer/state/api/activityViewerApiSlice';
import { addSelectionCriterion } from 'src/content/app/regulatory-activity-viewer/state/epigenome-selection/epigenomeSelectionSlice';

import type { EpigenomeMetadataDimensions } from 'src/content/app/regulatory-activity-viewer/types/epigenomeMetadataDimensions';

/**
* Upon the first load of epigenome metadata dimensions,
* check if user has selected any dimensions,
* and if not, then pre-select epigenomes using the default dimensions (if available).
*
* See the CommonMetadataDimensionFields type for epigenome metadata dimensions.
*
* NOTE: this hook should be only run once, at the top level of RegulatoryActivityViewer
*/
const usePreselectedEpigenomes = () => {
const activeGenomeId = useAppSelector(getActiveGenomeId) ?? '';
const { currentData: epigenomeMetadataDimensionsResponse } =
useEpigenomeMetadataDimensionsQuery();
const epigenomeSelectionCriteria = useAppSelector((state) =>
getEpigenomeSelectionCriteria(state, activeGenomeId)
);
const dispatch = useAppDispatch();

useEffect(() => {
if (!activeGenomeId || !epigenomeMetadataDimensionsResponse) {
// can't do anything until epigenomeMetadataDimensionsResponse is available
return;
}
if (Object.keys(epigenomeSelectionCriteria).length) {
// some epigenomes have already been selected; bail out
return;
}

const initialEpigenomeFilters = getPreselectedDimensions(
epigenomeMetadataDimensionsResponse.dimensions
);

if (initialEpigenomeFilters.length) {
for (const { dimensionName, value } of initialEpigenomeFilters) {
dispatch(
addSelectionCriterion({
genomeId: activeGenomeId,
dimensionName,
value
})
);
}
}
}, [
epigenomeMetadataDimensionsResponse,
epigenomeSelectionCriteria,
activeGenomeId
]);
};

const getPreselectedDimensions = (dimensions: EpigenomeMetadataDimensions) => {
const result: { dimensionName: string; value: string }[] = [];

for (const [dimensionName, dimensionData] of Object.entries(dimensions)) {
if (dimensionData.default_values.length) {
for (const value of dimensionData.default_values) {
result.push({ dimensionName, value });
}
}
}

return result;
};

export default usePreselectedEpigenomes;
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ type CommonMetadataDimensionFields = {
collapsible: boolean;
filterable: boolean;
zero_counts_visible: boolean;
default_values: string[];
};

type DimensionWithCategoricalData = CommonMetadataDimensionFields & {
type: 'categorical';
values: string[]; // <-- this is what makes the dimension "categorical"
default_values: string[];
};

type DimensionWithCategoricalDataAndOntology = CommonMetadataDimensionFields & {
Expand All @@ -36,7 +36,6 @@ type DimensionWithCategoricalDataAndOntology = CommonMetadataDimensionFields & {
url: string;
};
}[];
default_values: string[];
};

// the only member of this type currently is age, which is still very uncertain
Expand All @@ -47,7 +46,6 @@ type DimensionWithCategoricalDataAndDescription =
name: string;
description: string;
}[];
default_values: string[];
};

type EpigenomeMetadataDimension =
Expand Down

0 comments on commit ab1c6de

Please sign in to comment.