Skip to content

Commit

Permalink
fix(browser): include all ancestries for dataset in variant table
Browse files Browse the repository at this point in the history
  • Loading branch information
rileyhgrant committed May 17, 2024
1 parent e9eda1c commit 3d69c5c
Show file tree
Hide file tree
Showing 3 changed files with 4,436 additions and 10 deletions.
58 changes: 48 additions & 10 deletions browser/src/VariantList/mergeExomeAndGenomeData.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,61 @@
import { DatasetId } from '@gnomad/dataset-metadata/metadata'
import { Filter } from '../QCFilter'
import { Population, Variant } from '../VariantPage/VariantPage'
import { PopulationId, getPopulationsInDataset } from '@gnomad/dataset-metadata/gnomadPopulations'

// safe math on possibly null values
const add = (n1: number | null | undefined, n2: number | null | undefined) => (n1 || 0) + (n2 || 0)

// include placeholders for any ancestries missing from the dataset
const addMissingAncestries = (
currentAncestries: Population[],
versionAncestries: PopulationId[]
) => {
const fullAncestries = [...currentAncestries]

versionAncestries.forEach((ancestry) => {
if (
fullAncestries.filter((ancestryObject: Population) => ancestryObject.id === ancestry)
.length === 0
) {
fullAncestries.push({ id: ancestry, ac: 0, an: 0, ac_hemi: 0, ac_hom: 0 })
fullAncestries.push({ id: `${ancestry}_XX`, ac: 0, an: 0, ac_hemi: 0, ac_hom: 0 })
fullAncestries.push({ id: `${ancestry}_XY`, ac: 0, an: 0, ac_hemi: 0, ac_hom: 0 })
}
})

return fullAncestries
}

export const mergeExomeGenomeAndJointPopulationData = ({
datasetId,
exomePopulations = [],
genomePopulations = [],
jointPopulations = null,
}: {
datasetId?: DatasetId
exomePopulations: Population[]
genomePopulations: Population[]
jointPopulations?: Population[] | null
}) => {
const datasetPopulations = datasetId ? getPopulationsInDataset(datasetId) : []

if (jointPopulations) {
return (
jointPopulations
// filter to remove duplicate XX an XY keys from joint populations array
.filter((item, index, self) => index === self.findIndex((t) => t.id === item.id))
.map((jointPopulation) => ({
...jointPopulation,
ac_hemi: jointPopulation.hemizygote_count!,
ac_hom: jointPopulation.homozygote_count!,
}))
const reshapedJointPopulations = jointPopulations
// filter to remove duplicate XX an XY keys from joint populations array
.filter((item, index, self) => index === self.findIndex((t) => t.id === item.id))
.map((jointPopulation) => ({
...jointPopulation,
ac_hemi: jointPopulation.hemizygote_count!,
ac_hom: jointPopulation.homozygote_count!,
}))

const reshapedJointPopulaitonsWithAllAncestries = addMissingAncestries(
reshapedJointPopulations,
datasetPopulations
)

return reshapedJointPopulaitonsWithAllAncestries
}

const populations: { [key: string]: Population } = {}
Expand Down Expand Up @@ -59,7 +91,13 @@ export const mergeExomeGenomeAndJointPopulationData = ({
}
})

return Object.values(populations)
const reshapedMergedPopulations = Object.values(populations)
const reshapedMergedPopulationsWithAllAncestries = addMissingAncestries(
reshapedMergedPopulations,
datasetPopulations
)

return reshapedMergedPopulationsWithAllAncestries
}

type MergedVariant = Variant & {
Expand Down
1 change: 1 addition & 0 deletions browser/src/VariantPage/GnomadPopulationsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ export class GnomadPopulationsTable extends Component<
const { includeExomes, includeGenomes } = this.state

const mergedPopulations = mergeExomeGenomeAndJointPopulationData({
datasetId,
exomePopulations: includeExomes ? exomePopulations : [],
genomePopulations: includeGenomes ? genomePopulations : [],
jointPopulations:
Expand Down
Loading

0 comments on commit 3d69c5c

Please sign in to comment.