Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions js/feature/render/renderFeature.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ function renderFeature(feature, bpStart, xScale, pixelHeight, ctx, options) {
const leftExon = i > 0 && feature.exons[i - 1].readingFrame !== undefined ? feature.exons[i - 1] : undefined
const riteExon = i < feature.exons.length - 1 && feature.exons[i + 1].readingFrame !== undefined ? feature.exons[i + 1] : undefined

renderAminoAcidSequence.call(this, ctx, feature.strand, leftExon, exon, riteExon, bpStart, options.bpPerPixel, py, h, options.sequenceInterval)
renderAminoAcidSequence.call(this, ctx, feature.strand, leftExon, exon, riteExon, bpStart, options.bpPerPixel, py, h, options.sequenceInterval, options.axis)
}
}

Expand Down Expand Up @@ -184,7 +184,7 @@ function renderFeature(feature, bpStart, xScale, pixelHeight, ctx, options) {
}
}

function renderAminoAcidSequence(ctx, strand, leftExon, exon, riteExon, bpStart, bpPerPixel, y, height, sequenceInterval) {
function renderAminoAcidSequence(ctx, strand, leftExon, exon, riteExon, bpStart, bpPerPixel, y, height, sequenceInterval, axis) {

const aaColors =
[
Expand All @@ -201,6 +201,11 @@ function renderAminoAcidSequence(ctx, strand, leftExon, exon, riteExon, bpStart,
aminoAcidLetter = '*'
}

// Pre-flip about the letter's center for juicebox's y-axis transpose, as for feature labels
if ('y' === axis) {
IGVGraphics.labelTransformWithContext(ctx, xs + width / 2)
}

const aminoAcidLetterWidth = ctx.measureText(aminoAcidLetter).width
IGVGraphics.fillText(ctx, aminoAcidLetter, xs + (width - aminoAcidLetterWidth) / 2, y - 4, {fillStyle: '#ffffff'})
}
Expand Down Expand Up @@ -389,6 +394,12 @@ function renderFeatureLabel(ctx, feature, featureX, featureX1, featureY, referen
labelY - textMetrics.actualBoundingBoxAscent - 1,
textMetrics.width + 2,
textMetrics.actualBoundingBoxAscent + textMetrics.actualBoundingBoxDescent + 2)

// juicebox transposes y-axis tiles, which mirrors text. Pre-flip the label about its center
// so it reads correctly, and stays on its feature, once transposed.
if ('y' === options.axis) {
IGVGraphics.labelTransformWithContext(ctx, centerX)
}
IGVGraphics.fillText(ctx, name, centerX, labelY, geneFontStyle, transform)

}
Expand Down
152 changes: 152 additions & 0 deletions test/testRenderFeature.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
import "./utils/mockObjects.js"
import {assert} from 'chai'
import {renderFeature} from "../js/feature/render/renderFeature.js"
import SequenceInterval from "../js/genome/sequenceInterval.js"

suite("testRenderFeature", function () {

// A feature spanning pixels 100-300, so its label is centered at x = 200
const feature = {chr: "chr1", start: 100, end: 300, strand: "+", name: "GENE1", row: 0}

const track = {
color: "rgb(0,0,150)",
displayMode: "EXPANDED",
featureHeight: 14,
expandedRowHeight: 30,
margin: 10,
arrowSpacing: 30,
config: {},
browser: {qtlSelections: {hasPhenotype: () => false}},
getColorForFeature: () => "rgb(0,0,150)"
}

function drawOptions(axis) {
const options = {
pixelWidth: 1000,
viewportWidth: 1000,
pixelXOffset: 0,
bpPerPixel: 1,
drawLabel: true,
rowLastLabelX: {}
}
if (axis) options.axis = axis
return options
}

function labelDrawnWith(axis) {
const ctx = new RecordingContext()
renderFeature.call(track, feature, 0, 1, 100, ctx, drawOptions(axis))
const label = ctx.texts.find(t => t.text === "GENE1")
assert.isDefined(label, "label was not drawn")
return label
}

test("label is drawn upright, centered on its feature, with no axis", function () {
const label = labelDrawnWith(undefined)
assert.equal(label.scaleX, 1)
assert.equal(label.x, 200)
assert.equal(label.textAlign, "center")
})

test("label is mirrored about its center on a y-axis track", function () {
const label = labelDrawnWith("y")
assert.equal(label.scaleX, -1)
assert.equal(label.x, 200)
assert.equal(label.textAlign, "center")
})

// ATG TAA at 10 pixels per bp: "M" spans pixels 0-30 and "*" (stop) spans 30-60. Each 6-pixel-wide letter is
// centered in its codon, so on an x track it is anchored at its left edge, 12 and 42, and on a y track it is
// flipped about its center, 15 and 45, which puts the anchor at its right edge, 18 and 48.
const codingFeature = {
chr: "chr1", start: 0, end: 6, strand: "+", name: "CDS1", row: 0,
exons: [{start: 0, end: 6, readingFrame: 0}]
}

function aminoAcidsDrawnWith(axis) {
const ctx = new RecordingContext()
const bpPerPixel = 0.1
const options = Object.assign(drawOptions(axis), {
bpPerPixel,
drawLabel: false,
sequenceInterval: new SequenceInterval("chr1", 0, 6, "ATGTAA")
})
renderFeature.call(track, codingFeature, 0, bpPerPixel, 100, ctx, options)
return ctx.texts
}

test("amino acids are drawn upright with no axis", function () {
const [m, stop] = aminoAcidsDrawnWith(undefined)
assert.deepEqual([m.text, m.scaleX, m.x], ["M", 1, 12])
assert.deepEqual([stop.text, stop.scaleX, stop.x], ["*", 1, 42])
})

test("amino acids are mirrored about their centers on a y-axis track", function () {
const [m, stop] = aminoAcidsDrawnWith("y")
assert.deepEqual([m.text, m.scaleX, m.x], ["M", -1, 18])
assert.deepEqual([stop.text, stop.scaleX, stop.x], ["*", -1, 48])
})
})

/**
* A minimal 2D context that tracks the current transform and records each text drawn,
* with its anchor in canvas coordinates and the horizontal scale in effect.
*/
class RecordingContext {

constructor() {
this.matrix = [1, 0, 0, 1, 0, 0]
this.stack = []
this.texts = []
this.textAlign = "start"
this.fillStyle = undefined
this.strokeStyle = undefined
}

save() {
this.stack.push({matrix: [...this.matrix], textAlign: this.textAlign, fillStyle: this.fillStyle, strokeStyle: this.strokeStyle})
}

restore() {
const s = this.stack.pop()
if (s) Object.assign(this, s)
}

transform(a, b, c, d, e, f) {
const [m0, m1, m2, m3, m4, m5] = this.matrix
this.matrix = [
m0 * a + m2 * b, m1 * a + m3 * b,
m0 * c + m2 * d, m1 * c + m3 * d,
m0 * e + m2 * f + m4, m1 * e + m3 * f + m5
]
}

translate(x, y) { this.transform(1, 0, 0, 1, x, y) }

scale(x, y) { this.transform(x, 0, 0, y, 0, 0) }

rotate(angle) {
const c = Math.cos(angle), s = Math.sin(angle)
this.transform(c, s, -s, c, 0, 0)
}

fillText(text, x, y) {
const [a, b, c, d, e, f] = this.matrix
this.texts.push({text, x: a * x + c * y + e, y: b * x + d * y + f, scaleX: a, textAlign: this.textAlign})
}

strokeText(text, x, y) { this.fillText(text, x, y) }

measureText(text) {
return {width: 6 * text.length, actualBoundingBoxAscent: 8, actualBoundingBoxDescent: 2}
}

fillRect() {}
clearRect() {}
beginPath() {}
moveTo() {}
lineTo() {}
stroke() {}
fill() {}
closePath() {}
}
Loading