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

✨ Add Time Range Selection to KM Plots on Study View #4645

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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
82 changes: 64 additions & 18 deletions src/pages/resultsView/survival/SurvivalChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
VictoryLabel,
VictoryScatter,
VictoryZoomContainer,
VictorySelectionContainer,
} from 'victory';
import {
getSurvivalSummaries,
Expand All @@ -29,12 +30,13 @@ import {
SurvivalPlotFilters,
SurvivalSummary,
SURVIVAL_COMPACT_MODE_THRESHOLD,
ScatterData,
} from './SurvivalUtil';
import { toConditionalPrecision } from 'shared/lib/NumberUtils';
import { getPatientViewUrl } from '../../../shared/api/urls';
import { DefaultTooltip, DownloadControls } from 'cbioportal-frontend-commons';
import autobind from 'autobind-decorator';
import { AnalysisGroup } from '../../studyView/StudyViewUtils';
import { AnalysisGroup, DataBin } from '../../studyView/StudyViewUtils';
import { AbstractChart } from '../../studyView/charts/ChartContainer';
import { toSvgDomNodeWithLegend } from '../../studyView/StudyViewUtils';
import classnames from 'classnames';
Expand Down Expand Up @@ -91,6 +93,8 @@ export interface ISurvivalChartProps {
xAxisTickCount?: number;
// Compact mode will hide censoring dots in the chart and do binning based on configuration
compactMode?: boolean;
attributeId?: string;
onUserSelection?: (dataBins: DataBin[]) => void;
}

const MIN_GROUP_SIZE_FOR_LOGRANK = 10;
Expand Down Expand Up @@ -594,6 +598,51 @@ export default class SurvivalChart
this.sliderValue = Number.parseFloat(text);
}

@autobind
private onSelection(data: any[]) {
if (data.length < 2) {
console.error(
'Survival Plot onSelection Failure: Not enough elements to select'
);
return;
}

const scatterPoints: Array<ScatterData> = data[1].data;
haynescd marked this conversation as resolved.
Show resolved Hide resolved
if (scatterPoints.length > 2) {
const dataBin = generateFilterDataBin(
scatterPoints,
() => this.props.attributeId!
);
if (this.props.onUserSelection) {
this.props.onUserSelection([dataBin]);
}
}
}

@computed
get victoryChartContainer() {
return this.props.onUserSelection ? (
<VictorySelectionContainer
selectionDimension="x"
onSelection={this.onSelection}
/>
) : (
<VictoryZoomContainer
responsive={false}
disable={true}
zoomDomain={
this.props.showSlider
? { x: [0, this.sliderValue] }
: undefined
}
onZoomDomainChange={_.debounce((domain: any) => {
this.scatterFilter = domain as SurvivalPlotFilters;
}, 1000)}
containerRef={(ref: any) => (this.svgContainer = ref)}
/>
);
}

@computed
get chart() {
return (
Expand Down Expand Up @@ -676,23 +725,7 @@ export default class SurvivalChart
</div>

<VictoryChart
containerComponent={
<VictoryZoomContainer
responsive={false}
disable={true}
zoomDomain={
this.props.showSlider
? { x: [0, this.sliderValue] }
: undefined
}
onZoomDomainChange={_.debounce((domain: any) => {
this.scatterFilter = domain as SurvivalPlotFilters;
}, 1000)}
containerRef={(ref: any) =>
(this.svgContainer = ref)
}
/>
}
containerComponent={this.victoryChartContainer}
height={this.styleOpts.height}
width={this.styleOpts.width}
padding={this.styleOpts.padding}
Expand Down Expand Up @@ -944,3 +977,16 @@ export default class SurvivalChart
}
}
}

export function generateFilterDataBin(
scatterPoints: Array<ScatterData>,
getAttributeId: () => string
): DataBin {
const minX = scatterPoints[0].x;
const maxX = scatterPoints[scatterPoints.length - 1].x;
return {
id: getAttributeId(),
start: minX,
end: maxX,
} as DataBin;
}
16 changes: 12 additions & 4 deletions src/pages/studyView/StudyViewPageStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,11 @@ import {
StudyViewFilterQueryExtractor,
StudyViewQueryExtractor,
} from './StudyViewQueryExtractor';
import {
getAllowedSurvivalClinicalDataFilterId,
isSurvivalAttributeId,
isSurvivalChart,
} from './charts/survival/StudyViewSurvivalUtils';

export const STUDY_VIEW_FILTER_AUTOSUBMIT = 'study_view_filter_autosubmit';

Expand Down Expand Up @@ -2772,10 +2777,12 @@ export class StudyViewPageStore
if (this.chartMetaSet[chartUniqueKey]) {
let chartMeta = this.chartMetaSet[chartUniqueKey];
trackStudyViewFilterEvent('clinicalDataFilters', this);
this.updateClinicalAttributeFilterByValues(
chartMeta.clinicalAttribute!.clinicalAttributeId,
values
);

const attributeId: string = isSurvivalChart(chartMeta.uniqueKey)
? getAllowedSurvivalClinicalDataFilterId(chartMeta.uniqueKey)
: chartMeta.clinicalAttribute!.clinicalAttributeId;

this.updateClinicalAttributeFilterByValues(attributeId, values);
}
}
@action.bound
Expand Down Expand Up @@ -6026,6 +6033,7 @@ export class StudyViewPageStore
dataType: getChartMetaDataType(survivalPlot.id),
patientAttribute: true,
displayName: survivalPlot.title,
clinicalAttribute: survivalPlot.survivalStatusAttribute,
// use survival status attribute's priority as KM plot's priority for non-reserved plots
priority:
STUDY_VIEW_CONFIG.priority[survivalPlot.id] ||
Expand Down
11 changes: 9 additions & 2 deletions src/pages/studyView/UserSelections.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ import {
import { StudyViewPageStore } from 'pages/studyView/StudyViewPageStore';
import classNames from 'classnames';
import { StudyViewPageTabKeyEnum } from 'pages/studyView/StudyViewPageTabs';
import {
getSurvivalChartMetaId,
isSurvivalAttributeId,
} from './charts/survival/StudyViewSurvivalUtils';

export interface IUserSelectionsProps {
store: StudyViewPageStore;
Expand Down Expand Up @@ -547,9 +551,12 @@ export default class UserSelections extends React.Component<
return _.reduce(
filters || [],
(acc, clinicalDataFilter) => {
const chartMeta = this.props.attributesMetaSet[
const attributeId = isSurvivalAttributeId(
clinicalDataFilter.attributeId
];
)
? getSurvivalChartMetaId(clinicalDataFilter.attributeId)
: clinicalDataFilter.attributeId;
const chartMeta = this.props.attributesMetaSet[attributeId];
if (chartMeta) {
const dataType = this.props.clinicalAttributeIdToDataType[
clinicalDataFilter.attributeId
Expand Down
5 changes: 4 additions & 1 deletion src/pages/studyView/charts/ChartContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,8 @@ export class ChartContainer extends React.Component<IChartContainerProps, {}> {
this.props.chartMeta.uniqueKey
]?.survivalData,
this.props.analysisGroupsSettings.groups,
this.props.patientToAnalysisGroup!.result!
this.props.patientToAnalysisGroup!.result!,
this.props.chartMeta.uniqueKey
);
} else {
return undefined;
Expand Down Expand Up @@ -981,6 +982,8 @@ export class ChartContainer extends React.Component<IChartContainerProps, {}> {
yAxisTickCount={2}
xAxisTickCount={4}
compactMode={this.showCompactSurvivalChart}
attributeId={data.attributeId}
onUserSelection={this.handlers.onDataBinSelection}
/>
);
} else {
Expand Down
34 changes: 32 additions & 2 deletions src/pages/studyView/charts/survival/StudyViewSurvivalUtils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { PatientSurvival } from '../../../../shared/model/PatientSurvival';
import { IChartContainerProps } from '../ChartContainer';
import { AnalysisGroup } from '../../StudyViewUtils';
import _ from 'lodash';
import { logRankTest } from 'pages/resultsView/survival/logRankTest';
Expand All @@ -8,7 +7,10 @@ import { sortPatientSurvivals } from 'pages/resultsView/survival/SurvivalUtil';
export function makeSurvivalChartData(
patientSurvivals: ReadonlyArray<PatientSurvival>,
analysisGroups: ReadonlyArray<AnalysisGroup>,
patientToAnalysisGroup: { [uniquePatientKey: string]: string }
patientToAnalysisGroup: {
[uniquePatientKey: string]: string;
},
attributeId: string
) {
let patientToAnalysisGroups = _.mapValues(patientToAnalysisGroup, group => [
group,
Expand Down Expand Up @@ -48,7 +50,35 @@ export function makeSurvivalChartData(
analysisGroups,
sortedGroupedSurvivals,
pValue,
attributeId,
};
}

export function makeScatterPlotData() {}

export function isSurvivalAttributeId(attributeId: string) {
return /_MONTHS|_STATUS$/i.test(attributeId);
}

export function isSurvivalChart(chartUniqueKey: string) {
return /_SURVIVAL$/i.test(chartUniqueKey);
}

export function getAllowedSurvivalClinicalDataFilterId(chartUniqueKey: string) {
const prefix = chartUniqueKey.substring(
0,
chartUniqueKey.indexOf('_SURVIVAL')
);
return `${prefix}_MONTHS`;
}

export function getSurvivalChartMetaId(attributeId: string) {
const survivalClinicalDataType = /_MONTHS$/i.test(attributeId)
? '_MONTHS'
: '_STATUS';
const prefix = attributeId.substring(
0,
attributeId.indexOf(survivalClinicalDataType)
);
return `${prefix}_SURVIVAL`;
}
1 change: 1 addition & 0 deletions src/pages/studyView/tabs/SummaryTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,7 @@ export class StudySummaryTab extends React.Component<
'OS_SURVIVAL'
]?.survivalDataWithoutLeftTruncation;
}
props.onDataBinSelection = this.handlers.onDataBinSelection;
/* end of left truncation adjustment related settings */
break;
}
Expand Down