Skip to content

Commit

Permalink
More refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
cmdcolin committed Nov 10, 2024
1 parent 071d8cb commit b1d7877
Show file tree
Hide file tree
Showing 15 changed files with 56 additions and 60 deletions.
23 changes: 14 additions & 9 deletions plugins/alignments/src/CramAdapter/CramSlightlyLazyFeature.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import {
Feature,
SimpleFeatureSerialized,
} from '@jbrowse/core/util/simpleFeature'
import { Feature, SimpleFeatureSerialized } from '@jbrowse/core/util'
import { CramRecord } from '@gmod/cram'

// locals
import CramAdapter from './CramAdapter'
import { readFeaturesToCIGAR, readFeaturesToMismatches } from './util'
import { mdToMismatches, parseCigar } from '../MismatchParser'

export default class CramSlightlyLazyFeature implements Feature {
// uses parameter properties to automatically create fields on the class
Expand All @@ -28,10 +26,6 @@ export default class CramSlightlyLazyFeature implements Feature {
return this.start + (this.record.lengthOnRef ?? 1)
}

get type() {
return 'match'
}

get score() {
return this.record.mappingQuality
}
Expand Down Expand Up @@ -122,11 +116,22 @@ export default class CramSlightlyLazyFeature implements Feature {
}

get mismatches() {
return readFeaturesToMismatches(
const mismatches = readFeaturesToMismatches(
this.record.readFeatures,
this.start,
this.qualRaw,
)
return this.tags.MD && this.seq
? mismatches.concat(
mdToMismatches(
this.tags.MD,
parseCigar(this.CIGAR),
mismatches,
this.seq,
this.qualRaw,
),
)
: mismatches
}

get fields(): SimpleFeatureSerialized {
Expand Down
14 changes: 2 additions & 12 deletions plugins/alignments/src/CramAdapter/util.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
import { CramRecord } from '@gmod/cram'
import { Mismatch } from '../shared/types'

type ReadFeatures = CramRecord['readFeatures']

export interface Mismatch {
qual?: number
start: number
length: number
type: string
base: string | undefined
altbase?: string
seq?: string
cliplen?: number
}

export function readFeaturesToMismatches(
readFeatures: ReadFeatures,
start: number,
Expand Down Expand Up @@ -48,7 +38,7 @@ export function readFeaturesToMismatches(
mismatches[j++] = {
start: refPos,
length: 1,
base: sub,
base: sub!,
qual: qual?.[pos - 1],
altbase: ref?.toUpperCase(),
type: 'mismatch',
Expand Down
13 changes: 2 additions & 11 deletions plugins/alignments/src/MismatchParser/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
import { Feature } from '@jbrowse/core/util'
import type { Buffer } from 'buffer'
import { Mismatch } from '../shared/types'

export interface Mismatch {
qual?: number
start: number
length: number
type: string
base: string
altbase?: string
seq?: string
cliplen?: number
}
const mdRegex = new RegExp(/(\d+|\^[a-z]+|[a-z])/gi)
const cigarRegex = new RegExp(/([MIDNSHPX=])/)
const startClip = new RegExp(/(\d+)[SH]$/)
Expand Down Expand Up @@ -125,7 +116,7 @@ export function mdToMismatches(
ops: string[],
cigarMismatches: Mismatch[],
seq: string,
qual?: Buffer,
qual?: Buffer | number[] | null,
) {
let curr: Mismatch = { start: 0, base: '', length: 0, type: 'mismatch' }
let lastCigar = 0
Expand Down
2 changes: 1 addition & 1 deletion plugins/alignments/src/PileupRenderer/layoutFeature.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { bpSpanPx, Feature, Region } from '@jbrowse/core/util'
import { BaseLayout } from '@jbrowse/core/util/layouts'
import { Mismatch } from '../shared/types'
// locals
import { Mismatch } from '../MismatchParser'

export interface LayoutRecord {
feature: Feature
Expand Down
2 changes: 1 addition & 1 deletion plugins/alignments/src/PileupRenderer/renderMismatches.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { bpSpanPx, measureText } from '@jbrowse/core/util'
import { Mismatch } from '../MismatchParser'
import { fillRect, LayoutFeature } from './util'
import { RenderArgsWithColor } from './makeImageData'
import { colord } from '@jbrowse/core/util/colord'
import { Mismatch } from '../shared/types'

export function renderMismatches({
ctx,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { bpSpanPx } from '@jbrowse/core/util'
import { Theme } from '@mui/material'

// locals
import { Mismatch } from '../MismatchParser'
import { RenderArgsDeserializedWithFeaturesAndLayout } from './PileupRenderer'
import { fillRect, getCharWidthHeight, LayoutFeature } from './util'
import { Mismatch } from '../shared/types'

export function renderSoftClipping({
ctx,
Expand Down
32 changes: 14 additions & 18 deletions plugins/alignments/src/PileupRenderer/sortUtil.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { doesIntersect2, Feature } from '@jbrowse/core/util'
import { Mismatch } from '../MismatchParser'
import { SortedBy } from '../shared/types'
import { Mismatch, SortedBy } from '../shared/types'

export const sortFeature = (
features: Map<string, Feature>,
Expand Down Expand Up @@ -53,33 +52,30 @@ export const sortFeature = (
// first sort all mismatches, then all reference bases at the end
case 'Base pair': {
const baseSortArray: [string, Mismatch][] = []
featuresInCenterLine.forEach(feature => {
for (const feature of featuresInCenterLine) {
const mismatches: Mismatch[] = feature.get('mismatches')
mismatches.forEach(mismatch => {
for (const m of mismatches) {
const start = feature.get('start')
const offset = start + mismatch.start + 1
const consuming =
mismatch.type === 'insertion' || mismatch.type === 'softclip'
const len = consuming ? 0 : mismatch.length
const offset = start + m.start + 1
const consuming = m.type === 'insertion' || m.type === 'softclip'
const len = consuming ? 0 : m.length
if (pos >= offset && pos < offset + len) {
baseSortArray.push([feature.id(), mismatch])
baseSortArray.push([feature.id(), m])
}
})
})
}
}

const baseMap = new Map(baseSortArray)
featuresInCenterLine.sort((a, b) => {
const aMismatch = baseMap.get(a.id())
const bMismatch = baseMap.get(b.id())
const acode = bMismatch?.base.toUpperCase()
const bcode = aMismatch?.base.toUpperCase()
if (acode === bcode && acode === '*') {
// @ts-expect-error
return aMismatch.length - bMismatch.length
}
return (
(acode ? acode.charCodeAt(0) : 0) - (bcode ? bcode.charCodeAt(0) : 0)
)
return acode === bcode && acode === '*'
? // @ts-expect-error
aMismatch.length - bMismatch.length
: (acode ? acode.charCodeAt(0) : 0) -
(bcode ? bcode.charCodeAt(0) : 0)
})

break
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import { AugmentedRegion as Region } from '@jbrowse/core/util/types'
import { doesIntersect2, Feature, max, sum } from '@jbrowse/core/util'

// locals
import { parseCigar, Mismatch } from '../MismatchParser'
import { parseCigar } from '../MismatchParser'
import { getMethBins } from '../ModificationParser'
import {
ColorBy,
Mismatch,
PreBaseCoverageBin,
PreBaseCoverageBinSubtypes,
SkipMap,
Expand Down
11 changes: 11 additions & 0 deletions plugins/alignments/src/shared/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,14 @@ export interface SortedBy {
assemblyName: string
tag?: string
}

export interface Mismatch {
qual?: number
start: number
length: number
type: string
base: string
altbase?: string
seq?: string
cliplen?: number
}
9 changes: 5 additions & 4 deletions test_data/volvox/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2217,19 +2217,20 @@
},
"assemblyNames": ["volvox"]
},

{
"type": "AlignmentsTrack",
"trackId": "out_noref",
"name": "out_noref",
"trackId": "volvox-sorted.noref.cram",
"name": "volvox-sorted.cram (noref)",
"category": ["Integration test"],
"adapter": {
"type": "CramAdapter",
"cramLocation": {
"uri": "out_noref.cram",
"uri": "volvox-sorted.noref.cram",
"locationType": "UriLocation"
},
"craiLocation": {
"uri": "out_noref.cram.crai",
"uri": "volvox-sorted.noref.cram.crai",
"locationType": "UriLocation"
},
"sequenceAdapter": {
Expand Down
Binary file removed test_data/volvox/out_noref.cram
Binary file not shown.
Binary file removed test_data/volvox/out_noref.cram.crai
Binary file not shown.
Binary file added test_data/volvox/volvox-sorted.noref.cram
Binary file not shown.
Binary file added test_data/volvox/volvox-sorted.noref.cram.crai
Binary file not shown.
5 changes: 3 additions & 2 deletions website/docs/user_guides/alignments_track.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,9 @@ If you have data that marks DNA/RNA modifications using the MM tag in BAM/CRAM
format, then the alignments track can use these tags to color modifications. It
uses two modes:

1. Modifications mode - draws the modifications as they are
2. Methylation mode - draws both unmodified and modified CpGs (unmodified
1. All modifications - draws the modifications as they are
1. modifications - draws the modifications as they are
1. Methylation mode - draws both unmodified and modified CpGs (unmodified
positions are not indicated by the MM tag and this mode considers the
sequence context)

Expand Down

0 comments on commit b1d7877

Please sign in to comment.