Skip to content

Commit

Permalink
cleanup, more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
danvk committed Mar 13, 2015
1 parent 6fa636a commit 07544ff
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 14 deletions.
7 changes: 0 additions & 7 deletions src/BigBed.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,15 +163,13 @@ function parseHeader(dataView: DataView) {
// NB: dalliance doesn't support big endian formats.
var jb = new jBinary(dataView.buffer, BigBedTypeSet);
var header = jb.readAll();
console.log(header);

return header;
}

function parseCirTree(dataView: DataView) {
var jb = new jBinary(dataView.buffer, CirTreeTypeSet);
var cirTree = jb.readAll();
console.log(cirTree);

return cirTree;
}
Expand Down Expand Up @@ -215,7 +213,6 @@ function findOverlappingBlocks(twoBitHeader, cirTree, contigRange) {
}

function extractFeaturesInRange(dataView, dataRange, blocks, contigRange) {
console.log('Fetched ', dataRange);
var buffer = dataView.buffer;

return _.flatten(blocks.map(block => {
Expand All @@ -230,12 +227,9 @@ function extractFeaturesInRange(dataView, dataRange, blocks, contigRange) {
// TODO: parse only one record at a time, as many as is necessary.
var beds = jb.readAll();

console.log(beds);

beds = beds.filter(function(bed) {
var bedInterval = new ContigInterval(bed.chrId, bed.start, bed.end);
var r = contigRange.intersects(bedInterval);
console.log('intersect?', contigRange.toString(), bedInterval.toString(), r);
return r;
});

Expand Down Expand Up @@ -285,7 +279,6 @@ class BigBed {
if (blocks.length == 0) {
return [];
}
console.log(blocks);

var range = Interval.boundingInterval(
blocks.map(n => new Interval(+n.offset, n.offset+n.size)));
Expand Down
9 changes: 9 additions & 0 deletions src/ContigInterval.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

var Interval = require('./Interval');

/**
* Class representing a closed interval on the genome: contig:start-stop.
*
* The contig may be either a string ("chr22") or a number (in case the contigs
* are indexed, for example).
*/
class ContigInterval {
contig: string|number;
interval: Interval;
Expand All @@ -18,6 +24,9 @@ class ContigInterval {
stop() {
return this.interval.stop;
}
length() {
return this.interval.length();
}

intersects(other) {
return (this.contig == other.contig &&
Expand Down
12 changes: 8 additions & 4 deletions src/Interval.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
/* @flow */
/**
* Class representing a closed numeric interval, [start, stop].
*
* @flow
*/
class Interval {
start: number;
stop: number;
Expand All @@ -12,7 +16,7 @@ class Interval {

// TODO: make this a getter method & switch to Babel.
length() {
return this.stop - this.start + 1;
return Math.max(0, this.stop - this.start + 1);
}

intersect(otherInterval) {
Expand All @@ -34,7 +38,7 @@ class Interval {

static intersectAll(intervals: Array<Interval>): Interval {
if (!intervals.length) {
throw 'Tried to intersect zero intervals';
throw new Error('Tried to intersect zero intervals');
}
var result = intervals[0].clone();
intervals.slice(1).forEach(function({start, stop}) {
Expand All @@ -46,7 +50,7 @@ class Interval {

static boundingInterval(intervals: Array<Interval>): Interval {
if (!intervals.length) {
throw 'Tried to intersect zero intervals';
throw new Error('Tried to bound zero intervals');
}
var result = intervals[0].clone();
intervals.slice(1).forEach(function({start, stop}) {
Expand Down
9 changes: 9 additions & 0 deletions test/ContigInterval-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ var expect = chai.expect;
var ContigInterval = require('../src/ContigInterval');

describe('ContigInterval', function() {
it('should have basic accessors', function() {
var tp53 = new ContigInterval(10, 7512444, 7531643);
expect(tp53.toString()).to.equal('10:7512444-7531643');
expect(tp53.contig).to.equal(10);
expect(tp53.start()).to.equal(7512444);
expect(tp53.stop()).to.equal(7531643);
expect(tp53.length()).to.equal(19200);
});

it('should determine intersections', function() {
var tp53 = new ContigInterval(10, 7512444, 7531643);
var other = new ContigInterval(10, 7512444, 7531642);
Expand Down
66 changes: 66 additions & 0 deletions test/Interval-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,76 @@ var expect = chai.expect;
var Interval = require('../src/Interval');

describe('Interval', function() {
it('should have start/stop/length', function() {
var x = new Interval(10, 20);
expect(x.start).to.equal(10);
expect(x.stop).to.equal(20);
expect(x.length()).to.equal(11);
expect(x.toString()).to.equal('[10, 20]');
});

it('should determine containment', function() {
var x = new Interval(-10, 10);
expect(x.contains(0)).to.be.true;
expect(x.contains(-10)).to.be.true;
expect(x.contains(+10)).to.be.true;
expect(x.contains(+11)).to.be.false;
expect(x.contains(-11)).to.be.false;
});

it('should work with empty intervals', function() {
var empty = new Interval(5, 0),
other = new Interval(-10, 10);
expect(empty.contains(0)).to.be.false;
expect(empty.length()).to.equal(0);
expect(empty.intersect(other).length()).to.equal(0);
});

it('should determine intersections', function() {
var tp53 = new Interval(7512444, 7531643);
var other = new Interval(7512444, 7531642);

expect(tp53.intersects(other)).to.be.true;
});

it('should clone', function() {
var x = new Interval(0, 5),
y = x.clone();

y.start = 1;
expect(x.start).to.equal(0);
expect(y.start).to.equal(1);
});

it('should intersect many intervals', function() {
var ivs = [
new Interval(0, 10),
new Interval(5, 15),
new Interval(-5, 5)
];

var intAll = Interval.intersectAll;
expect(intAll( ivs ).toString()).to.equal('[5, 5]');
expect(intAll([ivs[0], ivs[1]]).toString()).to.equal('[5, 10]');
expect(intAll([ivs[0], ivs[2]]).toString()).to.equal('[0, 5]');
expect(intAll([ivs[0] ]).toString()).to.equal('[0, 10]');

expect(() => intAll([])).to.throw(/intersect zero intervals/);
});

it('should construct bounding intervals', function() {
var ivs = [
new Interval(0, 10),
new Interval(5, 15),
new Interval(-5, 5)
];

var bound = Interval.boundingInterval;
expect(bound( ivs ).toString()).to.equal('[-5, 15]');
expect(bound([ivs[0], ivs[1]]).toString()).to.equal('[0, 15]');
expect(bound([ivs[0], ivs[2]]).toString()).to.equal('[-5, 10]');
expect(bound([ivs[0] ]).toString()).to.equal('[0, 10]');

expect(() => bound([])).to.throw(/bound zero intervals/);
});
});
7 changes: 4 additions & 3 deletions test/jbinary-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ describe('jBinary', function() {

var jb = new jBinary(u8array.buffer, twoBitTypeSet);
var header = jb.readAll();
console.log(header);

expect(header.magic).to.equal(0x1A412743); // two bit magic
expect(header.version).to.equal(0);
Expand All @@ -51,9 +50,11 @@ describe('jBinary', function() {
var buffer = u8array.buffer;

var jb = new jBinary(buffer, uint8TypeSet);
var num = 0;
while (jb.tell() < buffer.byteLength) {
var x = jb.read({value: 'uint8'});
console.log(jb.tell(), x);
var x = jb.read('File');
expect(x).to.deep.equal({value: num * num});
num++;
}
});
});

0 comments on commit 07544ff

Please sign in to comment.