Skip to content

Commit

Permalink
Merge pull request #488 from akmorrow13/null_genes
Browse files Browse the repository at this point in the history
fixed null gene name
  • Loading branch information
akmorrow13 authored Aug 29, 2018
2 parents e8817ec + fd7395c commit 82bca1b
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 77 deletions.
5 changes: 3 additions & 2 deletions src/main/sources/BigBedDataSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,10 @@ function createFromBigBedFile(remoteSource: BigBed): BigBedSource {
coveredRanges = ContigInterval.coalesce(coveredRanges);
var genes = fb.rows.map(parseBedFeature);
genes.forEach(gene => addGene(gene));
//we have new data from our internal block range
o.trigger('newdata', fb.range);
});
//we have new data from our internal block range
o.trigger('newdata', interval);
o.trigger('networkdone');
});
}

Expand Down
20 changes: 19 additions & 1 deletion src/main/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,23 @@ function computePercentile(xs: number[], percentile: number): number {
}
}

/**
* Converts a string into a null, NaN, undefined, Inf or -Inf. Returns
* original string if string is not a special case.
* Returns string parsed to special case (null, NaN, undefined, Inf or -Inf)
* or original string.
*/
function stringToLiteral(value: string): any {
var maps = {
"NaN": NaN,
"null": null,
"undefined": undefined,
"Infinity": Infinity,
"-Infinity": -Infinity
};
return ((value in maps) ? maps[value] : value);
}

module.exports = {
tupleLessOrEqual,
tupleRangeOverlaps,
Expand All @@ -286,5 +303,6 @@ module.exports = {
formatInterval,
isChrMatch,
flatMap,
computePercentile
computePercentile,
stringToLiteral
};
7 changes: 6 additions & 1 deletion src/main/viz/GeneTrack.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import d3utils from './d3utils';
import scale from '../scale';
import ContigInterval from '../ContigInterval';
import canvasUtils from './canvas-utils';
import utils from '../utils';
import dataCanvas from 'data-canvas';
import style from '../style';

Expand Down Expand Up @@ -61,7 +62,8 @@ function drawGeneName(ctx: CanvasRenderingContext2D,
textIntervals: Interval[]) {
var p = gene.position,
centerX = 0.5 * (clampedScale(1 + p.start()) + clampedScale(1 + p.stop()));
var name = gene.name || gene.id;
// do not use gene name if it is null or empty
var name = !_.isEmpty(utils.stringToLiteral(gene.name)) ? gene.name : gene.id;
var textWidth = ctx.measureText(name).width;
var textInterval = new Interval(centerX - 0.5 * textWidth,
centerX + 0.5 * textWidth);
Expand All @@ -79,6 +81,9 @@ class GeneTrack extends React.Component<VizProps<BigBedSource>, State> {

constructor(props: VizProps<BigBedSource>) {
super(props);
this.state = {
networkStatus: null
};
}

render(): any {
Expand Down
93 changes: 20 additions & 73 deletions src/test/viz/FeatureTrack-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,6 @@ describe('FeatureTrack', function() {
});

it('should render features with bigBed file', function(): any {
var featureClickedData = null;
var featureClicked = function(data) {
featureClickedData = data;
};

var p = pileup.create(testDiv, {
range: {contig: 'chr17', start: 10000, stop: 16500},
Expand All @@ -140,91 +136,42 @@ describe('FeatureTrack', function() {
data: pileup.formats.bigBed({
url: '/test-data/chr17.22.10000-21000.bb',
}),
options: {onFeatureClicked: featureClicked},
name: 'Features'
}
]
});


return waitFor(ready, 2000)
.then(() => {
return waitFor(ready, 2000).then(() => {
var features = drawnObjects(testDiv, '.features');
// there can be duplicates in the case where features are
// overlapping more than one section of the canvas
features = _.uniq(features, false, function(x) {
return x.position.start();
});

expect(features).to.have.length(5);
expect(features.map(f => f.position.start())).to.deep.equal(
[10000, 10150, 10400, 16000, 16180]);

// canvas height should be height of features that are overlapping
var height = yForRow(2) * window.devicePixelRatio; // should be 2 rows

features = testDiv.querySelector('.features');
expect(features).to.not.be.null;
if (features != null) {
expect(features.style.height).to.equal(`${height}px`);
}
// check clicking on feature in row 0
var canvasList = testDiv.getElementsByTagName('canvas');
var canvas = canvasList[1];
expect(featureClickedData).to.be.null;
ReactTestUtils.Simulate.click(canvas,{nativeEvent: {offsetX: 0, offsetY: 10}});
expect(featureClickedData).to.not.be.null;

// check clicking on feature in row 1
featureClickedData = null;
ReactTestUtils.Simulate.click(canvas,{nativeEvent: {offsetX: 55, offsetY: 10}});
expect(featureClickedData).to.not.be.null;

p.destroy();
expect(features).to.have.length.at.least(2);

p.setRange({contig: 'chr22', start: 20000, stop: 21000});
}).delay(300).then(() => {
var features = drawnObjects(testDiv, '.features');
// there can be duplicates in the case where features are
// overlapping more than one section of the canvas
features = _.uniq(features, false, function(x) {
return x.position.start();
});
});

it('should not exceed parent height limits', function(): any {
var p = pileup.create(testDiv, {
range: {contig: 'chr22', start: 20000, stop: 21000},
tracks: [
{
viz: pileup.viz.genome(),
data: pileup.formats.twoBit({
url: '/test-data/test.2bit'
}),
isReference: true
},
{
viz: pileup.viz.features(),
data: pileup.formats.bigBed({
url: '/test-data/chr17.22.10000-21000.bb',
}),
name: 'Features'
}
]
});

return waitFor(ready, 2000)
.then(() => {
var features = drawnObjects(testDiv, '.features');
// there can be duplicates in the case where features are
// overlapping more than one section of the canvas
features = _.uniq(features, false, function(x) {
return x.position.start();
});

expect(features).to.have.length(10);
expect(features).to.have.length(10);

// canvas height should be maxed out
var expectedHeight = 150 * window.devicePixelRatio;
var featureCanvas = testDiv.querySelector('.features');
expect(featureCanvas).to.not.be.null;
if (featureCanvas != null) {
expect(featureCanvas.style.height).to.equal(`${expectedHeight}px`);
}
p.destroy();
});
// canvas height should be maxed out, should not exceed parent height limits
var expectedHeight = 150 * window.devicePixelRatio;
var featureCanvas = testDiv.querySelector('.features');
expect(featureCanvas).to.not.be.null;
if (featureCanvas != null) {
expect(featureCanvas.style.height).to.equal(`${expectedHeight}px`);
}
p.destroy();
});
});
});
});
33 changes: 33 additions & 0 deletions src/test/viz/GeneTrack-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,37 @@ describe('GeneTrack', function() {
});
});

it('should not print null gene name', function(): any {
var p = pileup.create(testDiv, {
range: {contig: '17', start: 1156459, stop: 1156529},
tracks: [
{
viz: pileup.viz.genome(),
data: pileup.formats.twoBit({
url: '/test-data/test.2bit'
}),
isReference: true
},
{
data: pileup.formats.bigBed({
url: '/test-data/ensembl.chr17.bb'
}),
viz: pileup.viz.genes(),
}
]
});

return waitFor(ready, 2000)
.then(() => {
var genes = drawnObjects(testDiv, '.genes');
expect(genes).to.have.length(1);
expect(genes.map(g => g.name)).to.deep.equal(
[ 'null']); // null gene name

// Do not draw null gene name. Default to gene id.
var texts = callsOf(testDiv, '.genes', 'fillText');
expect(texts.map(t => t[1])).to.deep.equal(['ENST00000386721']);
p.destroy();
});
});
});

0 comments on commit 82bca1b

Please sign in to comment.