Skip to content

Commit

Permalink
Update to cache modifications
Browse files Browse the repository at this point in the history
  • Loading branch information
cmdcolin committed Nov 14, 2024
1 parent 8efc12f commit 9233a8f
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 12 deletions.
6 changes: 6 additions & 0 deletions plugins/alignments/src/BamAdapter/BamAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ export default class BamAdapter extends BaseFeatureDataAdapter {
readName,
} = filterBy || {}

let hits = 0
let nohits = 0
for (const record of records) {
let ref: string | undefined
if (!record.tags.MD) {
Expand Down Expand Up @@ -224,14 +226,18 @@ export default class BamAdapter extends BaseFeatureDataAdapter {
// features in the cache have pre-computed mismatches objects that
// can be re-used across blocks
const ret = this.featureCache.get(`${record.id}`)

if (!ret) {
nohits++
const elt = new BamSlightlyLazyFeature(record, this, ref)
this.featureCache.set(`${record.id}`, elt)
observer.next(elt)
} else {
hits++
observer.next(ret)
}
}
console.log({ hits, nohits })
observer.complete()
})
}, signal)
Expand Down
14 changes: 12 additions & 2 deletions plugins/alignments/src/BamAdapter/BamSlightlyLazyFeature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import {
import { BamRecord } from '@gmod/bam'

// locals
import { getMismatches } from '../MismatchParser'
import { getMismatches, parseCigar } from '../MismatchParser'
import BamAdapter from './BamAdapter'
import { cacheGetter } from '../shared/util'
import { getTagAlt } from '../util'
import { getModPositions, getModProbabilities } from '../ModificationParser'
import { getMaxProbModAtEachPosition } from '../shared/getMaximumModificationAtEachPosition'

export default class BamSlightlyLazyFeature implements Feature {
// uses parameter properties to automatically create fields on the class
Expand All @@ -31,6 +34,10 @@ export default class BamSlightlyLazyFeature implements Feature {
)
}

get modifications() {
return getMaxProbModAtEachPosition(this)
}

get qual() {
return this.record.qual?.join(' ')
}
Expand All @@ -40,7 +47,9 @@ export default class BamSlightlyLazyFeature implements Feature {
? this.mismatches
: field === 'qual'
? this.qual
: this.fields[field]
: field === 'modifications'
? this.modifications
: this.fields[field]
}

parent() {
Expand Down Expand Up @@ -88,3 +97,4 @@ export default class BamSlightlyLazyFeature implements Feature {

cacheGetter(BamSlightlyLazyFeature, 'fields')
cacheGetter(BamSlightlyLazyFeature, 'mismatches')
cacheGetter(BamSlightlyLazyFeature, 'modifications')
6 changes: 5 additions & 1 deletion plugins/alignments/src/CramAdapter/CramAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,8 @@ export default class CramAdapter extends BaseFeatureDataAdapter {
tagFilter,
readName,
} = filterBy || {}

let hits = 0
let nohits = 0
for (const record of records) {
const flags = record.flags
if ((flags & flagInclude) !== flagInclude && !(flags & flagExclude)) {
Expand Down Expand Up @@ -275,13 +276,16 @@ export default class CramAdapter extends BaseFeatureDataAdapter {
// can be re-used across blocks
const ret = this.featureCache.get(`${record.uniqueId}`)
if (!ret) {
nohits++
const elt = this.cramRecordToFeature(record)
this.featureCache.set(`${record.uniqueId}`, elt)
observer.next(elt)
} else {
hits++
observer.next(ret)
}
}
console.log({ nohits, hits })

observer.complete()
})
Expand Down
11 changes: 10 additions & 1 deletion plugins/alignments/src/CramAdapter/CramSlightlyLazyFeature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { readFeaturesToCIGAR, readFeaturesToMismatches } from './util'
import { parseCigar } from '../MismatchParser'
import { mdToMismatches } from '../MismatchParser/mdToMismatches'
import { cacheGetter } from '../shared/util'
import { getMaxProbModAtEachPosition } from '../shared/getMaximumModificationAtEachPosition'

export default class CramSlightlyLazyFeature implements Feature {
// uses parameter properties to automatically create fields on the class
Expand Down Expand Up @@ -112,7 +113,9 @@ export default class CramSlightlyLazyFeature implements Feature {
? this.qual
: field === 'CIGAR'
? this.CIGAR
: this.fields[field]
: field === 'modifications'
? this.modifications
: this.fields[field]
}

parent() {
Expand Down Expand Up @@ -142,6 +145,11 @@ export default class CramSlightlyLazyFeature implements Feature {
: mismatches
}

get modifications() {
console.log('here')
return getMaxProbModAtEachPosition(this)
}

get fields(): SimpleFeatureSerialized {
return {
start: this.start,
Expand Down Expand Up @@ -177,3 +185,4 @@ export default class CramSlightlyLazyFeature implements Feature {
cacheGetter(CramSlightlyLazyFeature, 'fields')
cacheGetter(CramSlightlyLazyFeature, 'CIGAR')
cacheGetter(CramSlightlyLazyFeature, 'mismatches')
cacheGetter(CramSlightlyLazyFeature, 'modifications')
1 change: 0 additions & 1 deletion plugins/alignments/src/PileupRenderer/renderAlignment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ export function renderAlignment({
bpPerPx,
renderArgs,
canvasWidth,
cigarOps,
})
break
}
Expand Down
6 changes: 2 additions & 4 deletions plugins/alignments/src/PileupRenderer/renderModifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { bpSpanPx, max, sum } from '@jbrowse/core/util'
import { fillRect, LayoutFeature } from './util'
import { RenderArgsWithColor } from './makeImageData'
import { alphaColor } from '../shared/util'
import { getMaxProbModAtEachPosition } from '../shared/getMaximumModificationAtEachPosition'
import { MaximumProbabilityMod } from '../shared/getMaximumModificationAtEachPosition'

// render modifications stored in MM tag in BAM
export function renderModifications({
Expand All @@ -15,15 +15,13 @@ export function renderModifications({
bpPerPx,
renderArgs,
canvasWidth,
cigarOps,
}: {
ctx: CanvasRenderingContext2D
feat: LayoutFeature
region: Region
bpPerPx: number
renderArgs: RenderArgsWithColor
canvasWidth: number
cigarOps: string[]
}) {
const { feature, topPx, heightPx } = feat
const { colorBy, visibleModifications = {} } = renderArgs
Expand All @@ -37,7 +35,7 @@ export function renderModifications({
const isolatedModification = colorBy?.modifications?.isolatedModification
const twoColor = colorBy?.modifications?.twoColor

getMaxProbModAtEachPosition(feature, cigarOps)?.forEach(
;(feature.get('modifications') as MaximumProbabilityMod[])?.forEach(
({ allProbs, prob, type }, pos) => {
const r = start + pos
const [leftPx, rightPx] = bpSpanPx(r, r + 1, region, bpPerPx)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
PreBaseCoverageBinSubtypes,
SkipMap,
} from '../shared/types'
import { getMaxProbModAtEachPosition } from '../shared/getMaximumModificationAtEachPosition'
import { MaximumProbabilityMod } from '../shared/getMaxProbModAtEachPosition'

function mismatchLen(mismatch: Mismatch) {
return !isInterbase(mismatch.type) ? mismatch.length : 1
Expand Down Expand Up @@ -282,7 +282,8 @@ function processModification({
const fend = feature.get('end')
const twoColor = colorBy?.modifications?.twoColor
const isolatedModification = colorBy?.modifications?.isolatedModification
getMaxProbModAtEachPosition(feature)?.forEach(

;(feature.get('modifications') as MaximumProbabilityMod[])?.forEach(
({ type, prob, allProbs }, pos) => {
if (isolatedModification && type !== isolatedModification) {
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { getModPositions, getModProbabilities } from '../ModificationParser'
import { getNextRefPos, parseCigar } from '../MismatchParser'
import { getTagAlt } from '../util'

interface MaximumProbabilityMod {
export interface MaximumProbabilityMod {
type: string
prob: number
allProbs: number[]
Expand Down

0 comments on commit 9233a8f

Please sign in to comment.