Skip to content

Commit

Permalink
More refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
cmdcolin committed Nov 11, 2024
1 parent 071d8cb commit c699a89
Show file tree
Hide file tree
Showing 19 changed files with 124 additions and 105 deletions.
7 changes: 5 additions & 2 deletions plugins/alignments/src/BamAdapter/BamSlightlyLazyFeature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ export default class BamSlightlyLazyFeature implements Feature {
}

get(field: string): any {
return field === 'mismatches' ? this.mismatches : this.fields[field]
return field === 'mismatches'
? this.mismatches
: field === 'qual'
? this.record.qual
: this.fields[field]
}

parent() {
Expand All @@ -51,7 +55,6 @@ export default class BamSlightlyLazyFeature implements Feature {
name: r.name,
end: r.end,
score: r.score,
qual: r.qual,
strand: r.strand,
template_length: r.template_length,
flags: r.flags,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ exports[`adapter can fetch features from volvox.bam 1`] = `
"CIGAR": "100M",
"end": 5540,
"flags": 16,
"id": "test-10158054",
"name": "ctgA_5060_5540_1:0:0_3:1:0_15a3",
"next_pos": undefined,
"next_ref": undefined,
Expand Down Expand Up @@ -36,7 +35,6 @@ exports[`adapter can fetch features from volvox.bam 1`] = `
"CIGAR": "100M",
"end": 5541,
"flags": 16,
"id": "test-10158322",
"name": "ctgA_4973_5541_1:0:0_1:0:0_1569",
"next_pos": undefined,
"next_ref": undefined,
Expand Down Expand Up @@ -66,7 +64,6 @@ exports[`adapter can fetch features from volvox.bam 1`] = `
"CIGAR": "100M",
"end": 5544,
"flags": 0,
"id": "test-10158581",
"name": "ctgA_5445_5947_3:1:0_2:0:0_2b9",
"next_pos": undefined,
"next_ref": undefined,
Expand Down Expand Up @@ -96,7 +93,6 @@ exports[`adapter can fetch features from volvox.bam 1`] = `
"CIGAR": "100M",
"end": 5553,
"flags": 0,
"id": "test-10158847",
"name": "ctgA_5454_5906_0:1:0_2:0:0_d8a",
"next_pos": undefined,
"next_ref": undefined,
Expand Down Expand Up @@ -126,7 +122,6 @@ exports[`adapter can fetch features from volvox.bam 1`] = `
"CIGAR": "100M",
"end": 5554,
"flags": 0,
"id": "test-12607066",
"name": "ctgA_5455_5964_2:0:0_2:0:0_23e2",
"next_pos": undefined,
"next_ref": undefined,
Expand Down Expand Up @@ -156,7 +151,6 @@ exports[`adapter can fetch features from volvox.bam 1`] = `
"CIGAR": "100M",
"end": 5559,
"flags": 16,
"id": "test-12607328",
"name": "ctgA_5153_5559_3:0:0_1:1:0_11ac",
"next_pos": undefined,
"next_ref": undefined,
Expand Down Expand Up @@ -186,7 +180,6 @@ exports[`adapter can fetch features from volvox.bam 1`] = `
"CIGAR": "100M",
"end": 5560,
"flags": 0,
"id": "test-12607590",
"name": "ctgA_5461_5986_1:1:0_3:0:0_200e",
"next_pos": undefined,
"next_ref": undefined,
Expand Down Expand Up @@ -216,7 +209,6 @@ exports[`adapter can fetch features from volvox.bam 1`] = `
"CIGAR": "100M",
"end": 5568,
"flags": 0,
"id": "test-12607851",
"name": "ctgA_5469_5932_0:0:0_2:0:0_6f8",
"next_pos": undefined,
"next_ref": undefined,
Expand Down Expand Up @@ -246,7 +238,6 @@ exports[`adapter can fetch features from volvox.bam 1`] = `
"CIGAR": "100M",
"end": 5594,
"flags": 16,
"id": "test-12608107",
"name": "ctgA_5079_5594_1:0:0_2:0:0_d64",
"next_pos": undefined,
"next_ref": undefined,
Expand Down Expand Up @@ -276,7 +267,6 @@ exports[`adapter can fetch features from volvox.bam 1`] = `
"CIGAR": "100M",
"end": 5596,
"flags": 0,
"id": "test-12608367",
"name": "ctgA_5497_6082_1:0:0_2:0:0_b2",
"next_pos": undefined,
"next_ref": undefined,
Expand Down
41 changes: 28 additions & 13 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 @@ -110,7 +104,13 @@ export default class CramSlightlyLazyFeature implements Feature {
}

get(field: string): any {
return field === 'mismatches' ? this.mismatches : this.fields[field]
return field === 'mismatches'
? this.mismatches
: field === 'qual'
? this.qual
: field === 'CIGAR'
? this.CIGAR
: this.fields[field]
}

parent() {
Expand All @@ -122,11 +122,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 All @@ -135,13 +146,11 @@ export default class CramSlightlyLazyFeature implements Feature {
name: this.name,
end: this.end,
score: this.score,
qual: this.qual,
strand: this.strand,
template_length: this.template_length,
flags: this.flags,
tags: this.tags,
refName: this.refName,
CIGAR: this.CIGAR,
seq: this.seq,
type: 'match',
pair_orientation: this.pair_orientation,
Expand All @@ -153,7 +162,13 @@ export default class CramSlightlyLazyFeature implements Feature {
}

toJSON(): SimpleFeatureSerialized {
return this.fields
return {
...this.fields,
// lazy
CIGAR: this.CIGAR,
// lazy
qual: this.qual,
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ exports[`adapter can fetch features from volvox-sorted.cram 1`] = `
[
{
"CIGAR": "100M",
"clipPos": 0,
"end": 5540,
"flags": 16,
"is_paired": false,
"name": "ctgA_5060_5540_1:0:0_3:1:0_15a3",
"next_pos": undefined,
"next_ref": undefined,
"next_segment_position": undefined,
"pair_orientation": undefined,
"qual": "17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17",
"refName": "ctgA",
"score": 37,
Expand All @@ -29,11 +31,13 @@ exports[`adapter can fetch features from volvox-sorted.cram 1`] = `
},
{
"CIGAR": "100M",
"clipPos": 0,
"end": 5541,
"flags": 16,
"is_paired": false,
"name": "ctgA_4973_5541_1:0:0_1:0:0_1569",
"next_pos": undefined,
"next_ref": undefined,
"next_segment_position": undefined,
"pair_orientation": undefined,
"qual": "17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17",
"refName": "ctgA",
"score": 37,
Expand All @@ -54,11 +58,13 @@ exports[`adapter can fetch features from volvox-sorted.cram 1`] = `
},
{
"CIGAR": "100M",
"clipPos": 0,
"end": 5544,
"flags": 0,
"is_paired": false,
"name": "ctgA_5445_5947_3:1:0_2:0:0_2b9",
"next_pos": undefined,
"next_ref": undefined,
"next_segment_position": undefined,
"pair_orientation": undefined,
"qual": "17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17",
"refName": "ctgA",
"score": 37,
Expand All @@ -79,11 +85,13 @@ exports[`adapter can fetch features from volvox-sorted.cram 1`] = `
},
{
"CIGAR": "100M",
"clipPos": 0,
"end": 5553,
"flags": 0,
"is_paired": false,
"name": "ctgA_5454_5906_0:1:0_2:0:0_d8a",
"next_pos": undefined,
"next_ref": undefined,
"next_segment_position": undefined,
"pair_orientation": undefined,
"qual": "17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17",
"refName": "ctgA",
"score": 37,
Expand All @@ -104,11 +112,13 @@ exports[`adapter can fetch features from volvox-sorted.cram 1`] = `
},
{
"CIGAR": "100M",
"clipPos": 0,
"end": 5554,
"flags": 0,
"is_paired": false,
"name": "ctgA_5455_5964_2:0:0_2:0:0_23e2",
"next_pos": undefined,
"next_ref": undefined,
"next_segment_position": undefined,
"pair_orientation": undefined,
"qual": "17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17",
"refName": "ctgA",
"score": 37,
Expand All @@ -129,11 +139,13 @@ exports[`adapter can fetch features from volvox-sorted.cram 1`] = `
},
{
"CIGAR": "100M",
"clipPos": 0,
"end": 5559,
"flags": 16,
"is_paired": false,
"name": "ctgA_5153_5559_3:0:0_1:1:0_11ac",
"next_pos": undefined,
"next_ref": undefined,
"next_segment_position": undefined,
"pair_orientation": undefined,
"qual": "17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17",
"refName": "ctgA",
"score": 37,
Expand All @@ -154,11 +166,13 @@ exports[`adapter can fetch features from volvox-sorted.cram 1`] = `
},
{
"CIGAR": "100M",
"clipPos": 0,
"end": 5560,
"flags": 0,
"is_paired": false,
"name": "ctgA_5461_5986_1:1:0_3:0:0_200e",
"next_pos": undefined,
"next_ref": undefined,
"next_segment_position": undefined,
"pair_orientation": undefined,
"qual": "17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17",
"refName": "ctgA",
"score": 37,
Expand All @@ -179,11 +193,13 @@ exports[`adapter can fetch features from volvox-sorted.cram 1`] = `
},
{
"CIGAR": "100M",
"clipPos": 0,
"end": 5568,
"flags": 0,
"is_paired": false,
"name": "ctgA_5469_5932_0:0:0_2:0:0_6f8",
"next_pos": undefined,
"next_ref": undefined,
"next_segment_position": undefined,
"pair_orientation": undefined,
"qual": "17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17",
"refName": "ctgA",
"score": 37,
Expand All @@ -204,11 +220,13 @@ exports[`adapter can fetch features from volvox-sorted.cram 1`] = `
},
{
"CIGAR": "100M",
"clipPos": 0,
"end": 5594,
"flags": 16,
"is_paired": false,
"name": "ctgA_5079_5594_1:0:0_2:0:0_d64",
"next_pos": undefined,
"next_ref": undefined,
"next_segment_position": undefined,
"pair_orientation": undefined,
"qual": "17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17",
"refName": "ctgA",
"score": 37,
Expand All @@ -229,11 +247,13 @@ exports[`adapter can fetch features from volvox-sorted.cram 1`] = `
},
{
"CIGAR": "100M",
"clipPos": 0,
"end": 5596,
"flags": 0,
"is_paired": false,
"name": "ctgA_5497_6082_1:0:0_2:0:0_b2",
"next_pos": undefined,
"next_ref": undefined,
"next_segment_position": undefined,
"pair_orientation": undefined,
"qual": "17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17",
"refName": "ctgA",
"score": 37,
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
Loading

0 comments on commit c699a89

Please sign in to comment.