Skip to content

Commit

Permalink
added co-occurrence patern, p value, q value, enriched in columns and…
Browse files Browse the repository at this point in the history
… dropdown, shift click message. specified fisher test use in text
  • Loading branch information
Bryan Lai committed Apr 10, 2023
1 parent 5abb62d commit 600a1de
Show file tree
Hide file tree
Showing 20 changed files with 896 additions and 442 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ export class PercentToggle extends React.Component<IPercentToggleProps, {}> {
<div
style={{
marginLeft: 10,
marginRight: 10,
}}
>
<div style={{ textAlign: 'center' }}>Y-Axis:</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import styles from './filterResetPanel.module.scss';
type FilterResetPanelProps = {
resetFilters: () => void;
filterInfo?: JSX.Element | string;
shiftClickMessage?: JSX.Element | string;
className?: string;
buttonText?: string;
buttonClass?: string;
Expand Down Expand Up @@ -42,6 +43,7 @@ export class FilterResetPanel extends React.Component<
>
{this.props.buttonText}
</button>
{this.props.shiftClickMessage}
</span>
</div>
);
Expand Down
134 changes: 47 additions & 87 deletions src/pages/groupComparison/FisherExactTwoSidedTestLabel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,111 +2,71 @@ import { action, makeObservable, observable } from 'mobx';
import { observer } from 'mobx-react';
import * as React from 'react';
import MutationMapperDataStore from 'shared/components/mutationMapper/MutationMapperDataStore';
import { ComparisonGroup, getSampleIdentifiers } from './GroupComparisonUtils';
import {
ComparisonGroup,
getPatientIdentifiers,
getSampleIdentifiers,
} from './GroupComparisonUtils';
import { toConditionalPrecisionWithMinimum } from 'shared/lib/FormatUtils';
import _ from 'lodash';
import { getTwoTailedPValue } from 'shared/lib/FisherExactTestCalculator';
import ComplexKeyMap from 'shared/lib/complexKeyDataStructures/ComplexKeyMap';
import { Sample } from 'cbioportal-ts-api-client';
import { countUniqueMutations } from 'shared/lib/MutationUtils';

interface IFisherExactTwoSidedTestLabelProps {
dataStore: MutationMapperDataStore;
groups: ComparisonGroup[];
maxSize: number;
sampleSet: ComplexKeyMap<Sample>;
}

export const FisherExactTwoSidedTestLabel: React.FC<IFisherExactTwoSidedTestLabelProps> = observer(
({ dataStore, groups, maxSize }: IFisherExactTwoSidedTestLabelProps) => {
// initialize array of factorials for speed
let f = new Array(maxSize + 1);
f[0] = 0.0;
for (let i = 1; i <= maxSize; i++) {
f[i] = f[i - 1] + Math.log(i);
}

const mutationCountForActiveGeneGroupA = _.intersection(
_.flatten(dataStore.tableData),
_.flatten(dataStore.sortedFilteredGroupedData[0].data)
).length;
const mutationCountForActiveGeneGroupB = _.intersection(
_.flatten(dataStore.tableData),
_.flatten(dataStore.sortedFilteredGroupedData[1].data)
).length;
const sampleCountForGroupA = getSampleIdentifiers([groups[0]]).length;
const sampleCountForGroupB = getSampleIdentifiers([groups[1]]).length;

const getP = (a: number, b: number, c: number, d: number): number => {
let n = a + b + c + d;
if (n > maxSize) {
return NaN;
}
let p;
p =
f[a + b] +
f[c + d] +
f[a + c] +
f[b + d] -
(f[a] + f[b] + f[c] + f[d] + f[n]);
return Math.exp(p);
};

const getTwoTailedP = (
a: number,
b: number,
c: number,
d: number
): number => {
let min, i;
let n = a + b + c + d;
if (n > maxSize) {
return NaN;
}
let p = 0;

let baseP = getP(a, b, c, d);
// in order for a table under consideration to have its p-value included
// in the final result, it must have a p-value less than the baseP, i.e.
// Fisher's exact test computes the probability, given the observed marginal
// frequencies, of obtaining exactly the frequencies observed and any configuration more extreme.
// By "more extreme," we mean any configuration (given observed marginals) with a smaller probability of
// occurrence in the same direction (one-tailed) or in both directions (two-tailed).

let initialA = a,
initialB = b,
initialC = c,
initialD = d;
p += baseP;

min = c < b ? c : b;
for (i = 0; i < min; i++) {
let tempP = getP(++a, --b, --c, ++d);
if (tempP <= baseP) {
p += tempP;
}
}

// reset the values to their original so we can repeat this process for the other side
a = initialA;
b = initialB;
c = initialC;
d = initialD;
({ dataStore, groups, sampleSet }: IFisherExactTwoSidedTestLabelProps) => {
const mutationCountForActiveGeneGroupA = countUniqueMutations(
_.intersection(
_.flatten(dataStore.tableData),
_.flatten(dataStore.sortedFilteredGroupedData[0].data)
)
);
const mutationCountForActiveGeneGroupB = countUniqueMutations(
_.intersection(
_.flatten(dataStore.tableData),
_.flatten(dataStore.sortedFilteredGroupedData[1].data)
)
);
const sampleIdentifiersForGroupA = getSampleIdentifiers([groups[0]]);
const patientIdentifiersforGroupA = getPatientIdentifiers(
sampleIdentifiersForGroupA,
sampleSet
);
const sampleIdentifiersForGroupB = getSampleIdentifiers([groups[1]]);
const patientIdentifiersforGroupB = getPatientIdentifiers(
sampleIdentifiersForGroupB,
sampleSet
);

min = a < d ? a : d;
for (i = 0; i < min; i++) {
let pTemp = getP(--a, ++b, ++c, --d);
if (pTemp <= baseP) {
p += pTemp;
}
const getFisherTestLabel = () => {
if (dataStore.sortedFilteredSelectedData.length > 0) {
return 'Fisher Exact Two-Sided Test p-value for selected mutations: ';
} else if (
dataStore.sortedFilteredData.length < dataStore.allData.length
) {
return 'Fisher Exact Two-Sided Test p-value for filtered mutations: ';
}
return p;
return 'Fisher Exact Two-Sided Test p-value for all mutations: ';
};

return (
<div style={{ fontWeight: 'bold' }}>
Fisher Exact Two-Sided Test p-value:{' '}
{getFisherTestLabel()}
{toConditionalPrecisionWithMinimum(
getTwoTailedP(
getTwoTailedPValue(
mutationCountForActiveGeneGroupA,
sampleCountForGroupA - mutationCountForActiveGeneGroupA,
patientIdentifiersforGroupA.length -
mutationCountForActiveGeneGroupA,
mutationCountForActiveGeneGroupB,
sampleCountForGroupB - mutationCountForActiveGeneGroupB
patientIdentifiersforGroupB.length -
mutationCountForActiveGeneGroupB
),
3,
0.01,
Expand Down
Loading

0 comments on commit 600a1de

Please sign in to comment.