Skip to content

Commit

Permalink
Most frequent alt
Browse files Browse the repository at this point in the history
  • Loading branch information
cmdcolin committed Feb 8, 2025
1 parent 896f537 commit 69d1e4a
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export async function makeImageData({
await updateStatus('Drawing variant matrix', statusCallback, () => {
for (let i = 0; i < m; i++) {
const arr2 = [] as string[]
const { feature } = mafs[i]!
const { feature, mostFrequentAlt } = mafs[i]!
const hasPhaseSet = (feature.get('format') as string).includes('PS')
if (hasPhaseSet) {
const samp = feature.get('samples') as Record<string, SampleGenotype>
Expand All @@ -66,7 +66,7 @@ export async function makeImageData({
}
} else {
const alleles = genotype.split(/[/|]/)
drawColorAlleleCount(alleles, ctx, x, y, w, h)
drawColorAlleleCount(alleles, ctx, x, y, w, h, mostFrequentAlt)
}
}
}
Expand All @@ -92,7 +92,7 @@ export async function makeImageData({
}
} else {
const alleles = genotype.split(/[/|]/)
drawColorAlleleCount(alleles, ctx, x, y, w, h)
drawColorAlleleCount(alleles, ctx, x, y, w, h, mostFrequentAlt)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export async function makeImageData(
minorAlleleFrequencyFilter,
)
const rbush = new RBush()
for (const { feature } of mafs) {
for (const { mostFrequentAlt, feature } of mafs) {
const [leftPx, rightPx] = featureSpanPx(feature, region, bpPerPx)
const w = Math.max(Math.round(rightPx - leftPx), 2)
const samp = feature.get('genotypes') as Record<string, string>
Expand Down Expand Up @@ -59,7 +59,7 @@ export async function makeImageData(
}
} else {
const alleles = genotype.split(/[/|]/)
drawColorAlleleCount(alleles, ctx, x, y, w, h)
drawColorAlleleCount(alleles, ctx, x, y, w, h, mostFrequentAlt)
}
}
y += rowHeight
Expand Down
40 changes: 38 additions & 2 deletions plugins/variants/src/shared/drawAlleleCount.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,40 @@
import { colord } from '@jbrowse/core/util/colord'

import { f2 } from './constants'
import { getColorAlleleCount } from './multiVariantColor'

function getColorAlleleCount(alleles: string[], mostFrequentAlt: string) {
const total = alleles.length
let alt = 0
let uncalled = 0
let alt2 = 0
let ref = 0
for (const allele of alleles) {
if (allele === mostFrequentAlt) {
alt++
} else if (allele === '0') {
ref++
} else if (allele === '.') {
uncalled++
} else {
alt2++
}
}

if (ref === total) {
return `#ccc`
} else {
let a1 = colord(`hsl(200,50%,${80 - (alt / total) * 50}%)`)
if (alt2) {
// @ts-ignore
a1 = a1.mix(`hsla(0,100%,20%,${alt2 / total})`)
}
if (uncalled) {
// @ts-ignore
a1 = a1.mix(`hsla(50,50%,50%,${uncalled / total / 2})`)
}
return a1.toHex()
}
}

export function drawColorAlleleCount(
alleles: string[],
Expand All @@ -8,7 +43,8 @@ export function drawColorAlleleCount(
y: number,
w: number,
h: number,
mostFrequentAlt: string,
) {
ctx.fillStyle = getColorAlleleCount(alleles)
ctx.fillStyle = getColorAlleleCount(alleles, mostFrequentAlt)
ctx.fillRect(x - f2, y - f2, w + f2, h + f2)
}
14 changes: 13 additions & 1 deletion plugins/variants/src/shared/drawPhased.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import { set1 } from '@jbrowse/core/ui/colors'

import { f2 } from './constants'
import { getColorPhased, getColorPhasedWithPhaseSet } from './multiVariantColor'
import { colorify } from './util'

function getColorPhased(alleles: string[], HP: number) {
const c = +alleles[HP]!
return c ? set1[c - 1] || 'black' : '#ccc'
}

function getColorPhasedWithPhaseSet(alleles: string[], HP: number, PS: string) {
const c = +alleles[HP]!
return c ? colorify(+PS) || 'black' : '#ccc'
}

export function drawPhased(
alleles: string[],
Expand Down
1 change: 0 additions & 1 deletion plugins/variants/src/shared/findSecondLargestNumber.ts
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@

27 changes: 20 additions & 7 deletions plugins/variants/src/shared/minorAlleleFrequencyUtils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { sum } from '@jbrowse/core/util'

import type { Feature} from '@jbrowse/core/util'
import type { Feature } from '@jbrowse/core/util'

export function findSecondLargestNumber(arr: Iterable<number>) {
let firstMax = 0
Expand All @@ -27,8 +27,18 @@ export function calculateAlleleCounts(feat: Feature) {
alleleCounts.set(allele, (alleleCounts.get(allele) || 0) + 1)
}
}
let mostFrequentAlt
let max = 0
for (const [alt, altCount] of alleleCounts.entries()) {
if (alt !== '.' && alt !== '0') {
if (altCount > max) {
mostFrequentAlt = alt
max = altCount
}
}
}

return alleleCounts
return { alleleCounts, mostFrequentAlt }
}

export function calculateMinorAlleleFrequency(
Expand All @@ -46,16 +56,19 @@ export function getFeaturesThatPassMinorAlleleFrequencyFilter(
) {
const results = [] as {
feature: Feature
alleleCounts: Map<string, number>
mostFrequentAlt: string
}[]
for (const feat of feats) {
if (feat.get('end') - feat.get('start') <= 10) {
const alleleCounts = calculateAlleleCounts(feat)
for (const feature of feats) {
if (feature.get('end') - feature.get('start') <= 10) {
const { mostFrequentAlt, alleleCounts } = calculateAlleleCounts(feature)
if (
calculateMinorAlleleFrequency(alleleCounts) >=
minorAlleleFrequencyFilter
) {
results.push({ feature: feat, alleleCounts })
results.push({
feature,
mostFrequentAlt,
})
}
}
}
Expand Down
52 changes: 0 additions & 52 deletions plugins/variants/src/shared/multiVariantColor.ts

This file was deleted.

1 change: 0 additions & 1 deletion plugins/variants/src/util.ts
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@

0 comments on commit 69d1e4a

Please sign in to comment.