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

Feature/esckan-43 - connect Summary Filters #27

Merged
merged 5 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
74 changes: 6 additions & 68 deletions src/components/Connections.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { useEffect, useMemo, useState } from "react";
import { Box, Chip, TextField, Typography } from "@mui/material";
import { ArrowRightIcon } from "./icons";
import { vars } from "../theme/variables";
import { HierarchicalItem, ISubConnections, Option, SummaryType, ksMapType } from "./common/Types";
import { HierarchicalItem, ISubConnections, PhenotypeDetail, SummaryType, ksMapType } from "./common/Types";
import { useDataContext } from "../context/DataContext.ts";
import {
calculateSecondaryConnections,
Expand All @@ -12,13 +12,13 @@ import {
getYAxisNode
} from "../services/summaryHeatmapService.ts";
import { getYAxis, getKnowledgeStatementAndCount } from "../services/heatmapService.ts";
import CustomFilterDropdown from "./common/CustomFilterDropdown";
import SummaryHeader from "./connections/SummaryHeader";
import Details from "./connections/Details.tsx";
import SummaryInstructions from "./connections/SummaryInstructions.tsx";
import PhenotypeLegend from "./connections/PhenotypeLegend.tsx";
import HeatmapGrid from "./common/Heatmap.tsx";
import { Organ } from "../models/explorer.ts";
import SummaryFiltersDropdown from "./SummaryFiltersDropdown.tsx";

const { gray700, gray600A, gray100 } = vars;

Expand All @@ -38,28 +38,6 @@ const styles = {
}
}

type PhenotypeDetail = {
label: string;
color: string;
};
const phenotype: PhenotypeDetail[] = [
{
label: 'Sympathetic',
color: '#9B18D8'
},
{
label: 'Parasympathetic',
color: '#2C2CCE'
},
{
label: 'Sensory',
color: '#DC6803'
},
{
label: 'Motor',
color: '#EAAA08'
}
]


function Connections() {
Expand All @@ -81,44 +59,23 @@ function Connections() {
const viasConnection = getAllViasFromConnections(selectedConnectionSummary.connections);
const viasStatement = convertViaToString(Object.values(viasConnection))
const totalConnectionCount = Object.keys(selectedConnectionSummary.connections).length;
const phenotypes = getAllPhenotypes(selectedConnectionSummary.connections);
const [phenotypeFilters, setPhenotypeFilters] = useState<PhenotypeDetail[]>(phenotype);
const [phenotypeFilters, setPhenotypeFilters] = useState<PhenotypeDetail[]>([]);

useEffect(() => {
if (!checkIfConnectionSummaryIsEmpty(selectedConnectionSummary)) {
const phenotypes = getAllPhenotypes(selectedConnectionSummary.connections);
const phenotypeColors: string[] = generatePhenotypeColors(phenotypes.length)
setPhenotypeFilters(phenotypes.map((phenotype, index) => ({
label: phenotype,
color: phenotypeColors[index]
color: phenotypeColors[index],
ksId: ''
})))
}

}, [selectedConnectionSummary])

const nerves = getNerveFilters(viasConnection, majorNerves);

const searchPhenotypeFilter = (searchValue: string): Option[] => {
console.log(searchValue)
const searchedPhenotype = phenotypes
return searchedPhenotype.map((phenotype) => ({
id: phenotype,
label: phenotype,
group: 'Phenotype',
content: []
}));
}

const searchNerveFilter = (searchValue: string): Option[] => {
console.log(searchValue)
const searchedNerve = Object.keys(nerves)
return searchedNerve.map((nerve) => ({
id: nerve,
label: nerves[nerve],
group: 'Nerve',
content: []
}));
}

useEffect(() => {
if (!checkIfConnectionSummaryIsEmpty(selectedConnectionSummary) && phenotypeFilters) {
Expand Down Expand Up @@ -230,26 +187,7 @@ function Connections() {
Summary map shows the connections of the selected connection origin and end organ with phenotypes. Select individual squares to view the details of each connections.
</Typography>
</Box>
<Box display="flex" gap={1} flexWrap='wrap'>
<CustomFilterDropdown
key={"Phenotype"}
id={"Phenotype"}
placeholder="Phenotype"
searchPlaceholder="Search Phenotype"
selectedOptions={[]}
onSearch={(searchValue: string) => searchPhenotypeFilter(searchValue)}
onSelect={() => {}}
/>
<CustomFilterDropdown
key={"Nerve"}
id={"Nerve"}
placeholder="Nerve"
searchPlaceholder="Search Nerve"
selectedOptions={[]}
onSearch={(searchValue: string) => searchNerveFilter(searchValue)}
onSelect={() => {}}
/>
</Box>
<SummaryFiltersDropdown nerves={nerves} phenotypes={phenotypeFilters} />
<HeatmapGrid
yAxis={yAxis}
setYAxis={setYAxis}
Expand Down
3 changes: 1 addition & 2 deletions src/components/FiltersDropdowns.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {Box} from "@mui/material";
import CustomFilterDropdown from "./common/CustomFilterDropdown.tsx";
import React, {useMemo} from "react";
import {Filters, useDataContext} from "../context/DataContext.ts";
import {Option} from "./common/Types.ts";
import { Option } from "./common/Types.ts";
import {
getUniqueApinatomies, getUniqueOrgans,
getUniqueOrigins,
Expand All @@ -17,7 +17,6 @@ import {
searchSpecies, searchVias
} from "../services/searchService.ts";


interface FilterConfig {
id: keyof Filters;
placeholder: string;
Expand Down
85 changes: 85 additions & 0 deletions src/components/SummaryFiltersDropdown.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { useMemo } from "react";
import { SummaryFilters, useDataContext } from "../context/DataContext";
import CustomFilterDropdown from "./common/CustomFilterDropdown";
import { Option, PhenotypeDetail } from "./common/Types";
import { Box } from "@mui/material";
import { searchNerveFilter, searchPhenotypeFilter } from "../services/searchService";
import { OTHER_LABEL } from "../constants";

interface FilterConfig {
id: keyof SummaryFilters;
placeholder: string;
searchPlaceholder: string;
}

const filterConfig: FilterConfig[] = [
{
id: "Phenotype",
placeholder: "Phenotype",
searchPlaceholder: "Search phenotype",
},
{
id: "Nerve",
placeholder: "Nerve",
searchPlaceholder: "Search Nerve",
}
]

const SummaryFiltersDropdown = ({ nerves, phenotypes }: {
nerves: { [key: string]: string },
phenotypes: PhenotypeDetail[]
}) => {
const { summaryFilters, setSummaryFilters } = useDataContext();

const convertNervesToOptions = (nerves: { [key: string]: string }): Option[] => {
return Object.keys(nerves).map(nerve => ({
id: nerve,
label: nerves[nerve],
group: 'Nerve',
content: []
}));
}
const convertPhenotypesToOptions = (phenotypes: PhenotypeDetail[]): Option[] => {
// filter the phenotype where label is other
return phenotypes.map(phenotype => ({
id: phenotype.label,
label: phenotype.label,
group: 'Phenotype',
content: []
})).filter(phenotype => phenotype.label !== OTHER_LABEL);
}

const phenotypeOptions = useMemo(() => convertPhenotypesToOptions(phenotypes), [phenotypes]);
const nerveOptions = useMemo(() => convertNervesToOptions(nerves), [nerves]);

const handleSelect = (filterKey: keyof typeof summaryFilters, selectedOptions: Option[]) => {
setSummaryFilters(prevFilters => ({
...prevFilters,
[filterKey]: selectedOptions
}));
};

const searchFunctions = {
Phenotype: (value: string) => searchPhenotypeFilter(value, phenotypeOptions),
Nerve: (value: string) => searchNerveFilter(value, nerveOptions)
};

return (
<Box display="flex" gap={1} flexWrap="wrap">
{filterConfig.map(filter => (
<CustomFilterDropdown
key={filter.id}
id={filter.id}
placeholder={filter.placeholder}
searchPlaceholder={filter.searchPlaceholder}
selectedOptions={summaryFilters[filter.id]}
onSearch={(searchValue: string) => searchFunctions[filter.id](searchValue)}
onSelect={(options: Option[]) => handleSelect(filter.id as keyof SummaryFilters, options)}
/>
))}
</Box>
)

}

export default SummaryFiltersDropdown;
3 changes: 2 additions & 1 deletion src/components/common/Types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ export interface HierarchicalItem {
export type PhenotypeDetail = {
label: string;
color: string;
};
ksId: string;
};
1 change: 1 addition & 0 deletions src/constants.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const OTHER_LABEL = 'other'
11 changes: 9 additions & 2 deletions src/services/searchService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Search origins
import {Option} from "../components/common/Types.ts";
import {SYNONYMS_TITLE} from "../settings.ts";
import { Option } from "../components/common/Types.ts";
import { SYNONYMS_TITLE } from "../settings.ts";

export const searchOrigins = (searchValue: string, options: Option[]): Option[] => {
return searchAnatomicalEntities(searchValue, options);
Expand All @@ -26,6 +26,13 @@ export const searchVias = (searchValue: string, options: Option[]): Option[] =>
return searchAnatomicalEntities(searchValue, options);
};

export const searchNerveFilter = (value: string, nerveOptions: Option[]): Option[] => {
return searchByLabel(value, nerveOptions);
}

export const searchPhenotypeFilter = (value: string, phenotypeOptions: Option[]): Option[] => {
return searchByLabel(value, phenotypeOptions);
}

const searchByLabel = (searchValue: string, options: Option[]): Option[] => {
const lowerSearchValue = searchValue.toLowerCase();
Expand Down
16 changes: 12 additions & 4 deletions src/services/summaryHeatmapService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { HierarchicalItem, ISubConnections, ksMapType } from "../components/comm
import { ConnectionSummary, SummaryFilters } from "../context/DataContext.ts";
import { HierarchicalNode, KnowledgeStatement, Organ } from "../models/explorer.ts";
import { PhenotypeDetail } from "../components/common/Types.ts";
import { OTHER_LABEL } from "../constants.tsx";


export const checkIfConnectionSummaryIsEmpty = (connectionSummary: ConnectionSummary): boolean => {
Expand Down Expand Up @@ -58,7 +59,7 @@ export function getAllPhenotypes(connections: ksMapType): string[] {
if (connection.ks?.phenotype) {
phenotypeNames.add(connection.ks.phenotype);
} else {
phenotypeNames.add('other');
phenotypeNames.add(OTHER_LABEL);
}
});
return Array.from(phenotypeNames)
Expand Down Expand Up @@ -129,8 +130,14 @@ export function getSecondaryHeatmapData(yAxis: HierarchicalItem[], connections:
export function summaryFilterKnowledgeStatements(knowledgeStatements: Record<string, KnowledgeStatement>, summaryFilters: SummaryFilters): Record<string, KnowledgeStatement> {
const phenotypeIds = summaryFilters.Phenotype.map(option => option.id);
const nerveIds = summaryFilters.Nerve.map(option => option.id);
console.log(phenotypeIds, nerveIds);
return knowledgeStatements;
return Object.entries(knowledgeStatements).reduce((filtered, [id, ks]) => {
const phenotypeMatch = !phenotypeIds.length || phenotypeIds.includes(ks.phenotype);
const nerveMatch = !nerveIds.length || ks.via?.some(via => via.anatomical_entities.map(entity => entity.id).some(id => nerveIds.includes(id)));
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NOTE: even though the connections/KS may have vias it may not be filtered through them. Since filters shown in the dropdown are only MAJOR NERVES. This can be seen in the video above in PR description.

cc @afonsobspinto @ddelpiano

if (phenotypeMatch && nerveMatch) {
filtered[id] = ks;
}
return filtered;
}, {} as Record<string, KnowledgeStatement>);
}

export function calculateSecondaryConnections(
Expand Down Expand Up @@ -187,7 +194,7 @@ export function calculateSecondaryConnections(
const ksPhenotypes = knowledgeStatementIds.map(ksId => knowledgeStatements[ksId].phenotype).filter(phenotype => phenotype !== '');
const phenotypeColorsSet = new Set<string>();

const unknownFilter = phenotypes.find(p => p.label === 'other');
const unknownFilter = phenotypes.find(p => p.label === OTHER_LABEL);
ksPhenotypes.length === 0 ? phenotypeColorsSet.add(unknownFilter?.color || '') :
ksPhenotypes.map(phenotype => {
const phn = phenotypes.find(p => p.label === phenotype);
Expand Down Expand Up @@ -220,5 +227,6 @@ export const getNormalizedValueForMinMax = (value: number, min: number, max: num
// keep the min 0 always...
// Ex. for situations where min is 4... the value 4 will not be shown...
min = 0;
if (max === 0) return 0;
return max !== min ? (value - min) / (max - min) : 1;
}