import * as React from 'react';
import _ from 'lodash';
import { DefaultTooltip } from 'cbioportal-frontend-commons';
import 'rc-tooltip/assets/bootstrap_white.css';
import { Mutation } from 'cbioportal-ts-api-client';
import SampleManager from '../../SampleManager';
import { isUncalled } from 'shared/lib/MutationUtils';
import { getFormattedFrequencyValue } from 'shared/components/mutationTable/column/TumorAlleleFreqColumnFormatter';
export default class AlleleFreqColumnFormatter {
static barWidth = 6;
static barSpacing = 3;
static maxBarHeight = 12;
static indexToBarLeft = (n: number) =>
n *
(AlleleFreqColumnFormatter.barWidth +
AlleleFreqColumnFormatter.barSpacing);
public static getComponentForSampleArgs<
T extends { tumorAltCount: number; molecularProfileId: string }
>(mutation: T) {
const altReads = mutation.tumorAltCount;
let opacity: number = 1;
let extraTooltipText: string = '';
if (isUncalled(mutation.molecularProfileId)) {
if (altReads > 0) {
opacity = 0.1;
extraTooltipText =
"Mutation has supporting reads, but wasn't called";
} else {
opacity = 0;
extraTooltipText =
"Mutation has 0 supporting reads and wasn't called";
}
}
return {
opacity,
extraTooltipText,
};
}
public static convertMutationToSampleElement<
T extends {
sampleId: string;
tumorRefCount: number;
tumorAltCount: number;
molecularProfileId: string;
}
>(mutation: T, color: string, barX: number, sampleComponent: any) {
const altReads = mutation.tumorAltCount;
const refReads = mutation.tumorRefCount;
if (altReads < 0 || refReads < 0) {
return null;
}
const freq = altReads / (altReads + refReads);
const barHeight =
(isNaN(freq) ? 0 : freq) * AlleleFreqColumnFormatter.maxBarHeight;
const barY = AlleleFreqColumnFormatter.maxBarHeight - barHeight;
const bar = (
));
const freqs = sampleOrder.map(
(sampleId: string) =>
(sampleToElements[sampleId] &&
sampleToElements[sampleId].freq) ||
undefined
);
const bars = elementsInSampleOrder.map((elements: any) => elements.bar);
let content: JSX.Element = ;
// single sample: just show the number
if (sampleManager.sampleIdsInHeader.length === 1) {
// The frequency is populated by the sampleOrder.
// In the sample view of multi-sample patient, it still populates frequency for all samples.
const visualizedSampleIndex = sampleOrder.indexOf(
sampleManager.sampleIdsInHeader[0]
);
content = (
{!isNaN(freqs[visualizedSampleIndex])
? getFormattedFrequencyValue(
freqs[visualizedSampleIndex]
)
: ''}
);
}
// multiple samples: show a graphical component
// (if no tooltip info available do not update content)
else if (tooltipLines.length > 0) {
content = (
);
}
// as long as we have tooltip lines, show tooltip in either cases (single or multiple)
if (
tooltipLines.length > 0 &&
freqs.filter(freq => freq !== undefined).length > 0
) {
const overlay = () => {tooltipLines};
content = (