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

Use authorized counts where available for patient counts / data vis #174

Merged
merged 3 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions src/views/clinicalGenomic/search/SearchHandler.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useState, useRef } from 'react';
import { useEffect, useRef } from 'react';
import PropTypes from 'prop-types';

import { trackPromise } from 'react-promise-tracker';
Expand Down Expand Up @@ -85,7 +85,7 @@ function SearchHandler({ setLoading }) {
}

const discoveryCounts = {
diagnosis_age_count: CollateSummary(data, 'age_at_diagnosis'),
age_at_diagnosis: CollateSummary(data, 'age_at_diagnosis'),
treatment_type_count: CollateSummary(data, 'treatment_type_count'),
primary_site_count: CollateSummary(data, 'primary_site_count'),
patients_per_cohort: {}
Expand Down
43 changes: 35 additions & 8 deletions src/views/clinicalGenomic/widgets/dataVisualization.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,35 +23,62 @@ import { HAS_CENSORED_DATA_MARKER } from 'utils/utils';

function DataVisualization() {
// Hooks
const resultsContext = useSearchResultsReaderContext().counts;
const resultsContext = useSearchResultsReaderContext();
const counts = resultsContext.counts;
const clinical = resultsContext.clinical;
// Plan for context below see current dataVis for expected shape
// const dataVis = resultsContext || {};
const handleCensoring = (dataObj) => {
// const dataVis = counts || {};
const isCensored = (datum) => typeof datum === 'string' && datum.startsWith('<');
const handleCensoring = (dataKey, transformer = (site, input) => input, isObject = false) => {
const dataObj = counts?.[dataKey];
if (dataObj === null || typeof dataObj === 'undefined') {
return {};
}

let hasCensoredData = false;
const newDataObj = {};
// Copy over the data into a new object, substituting 0 instead of any censored data
Object.keys(dataObj).forEach((key) => {
if (typeof dataObj[key] === 'string' && dataObj[key].startsWith('<')) {
newDataObj[key] = 0;
if (isCensored(dataObj[key])) {
newDataObj[key] = 0;
hasCensoredData = true;
} else {
newDataObj[key] = dataObj[key];
}
});

// If clinical data hasn't returned yet, exit here
if (!clinical) {
return newDataObj;
}

// Check the clinical results to see if we can fill in any censored data with real ones
Object.entries(clinical).forEach(([siteName, site]) => {
Object.keys(site.summary?.[dataKey]).forEach((key) => {
if (isObject) {
Object.keys(site.summary[dataKey]).forEach((innerKey) => {
if (isCensored(dataObj[transformer(siteName, key)][innerKey])) {
newDataObj[transformer(siteName, key)][innerKey] = site.summary[dataKey][innerKey];
}
});
} else if (isCensored(dataObj[transformer(siteName, key)])) {
newDataObj[transformer(site, key)] += site.summary[dataKey][key];
}
});
});

if (hasCensoredData) {
newDataObj[HAS_CENSORED_DATA_MARKER] = true;
}
return newDataObj;
};

const dataVis = {
patients_per_cohort: handleCensoring(resultsContext?.patients_per_cohort) || {},
diagnosis_age_count: handleCensoring(resultsContext?.diagnosis_age_count) || {},
treatment_type_count: handleCensoring(resultsContext?.treatment_type_count) || {},
primary_site_count: handleCensoring(resultsContext?.primary_site_count) || {}
patients_per_cohort: handleCensoring('patients_per_cohort', (site, _) => site, true) || {},
diagnosis_age_count: handleCensoring('age_at_diagnosis', (_, age) => age.replace(/ Years$/, '')) || {},
treatment_type_count: handleCensoring('treatment_type_count') || {},
primary_site_count: handleCensoring('primary_site_count') || {}
};
const theme = useTheme();

Expand Down
15 changes: 13 additions & 2 deletions src/views/clinicalGenomic/widgets/patientCounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,33 @@ function PatientCounts() {
const context = useSearchResultsReaderContext();
const sites = context?.federation;
const programs = context?.programs;
const discoveryCounts = context?.counts?.patients_per_cohort;
const clinicalCounts = context?.clinical;

// Generate the map of site->cohort->numbers
let siteData = [];
if (Array.isArray(sites)) {
siteData = sites.map((entry) => {
const counts = context?.counts?.patients_per_cohort?.[entry.location.name] || {};
const counts = discoveryCounts?.[entry.location.name] || {};
const realCounts = clinicalCounts?.[entry.location.name]?.summary?.patients_per_cohort || {};
let unlockedPrograms = [];
// Fill up the programs using the summary counts
if (Array.isArray(programs)) {
unlockedPrograms = programs
.filter((search) => entry.location.name === search.location.name)?.[0]
?.results?.items?.map((program) => program.program_id);
}

const finalCounts = {};
Object.keys(counts).forEach((program) => {
finalCounts[program] = program in realCounts ? realCounts[program] : counts[program];
});

// Where possible, also use the real counts

return {
location: entry.location.name,
counts,
counts: finalCounts,
totals: entry?.results || {},
unlockedPrograms
};
Expand Down
Loading