-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
171 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* @flow */ | ||
|
||
var Interval = require('./Interval'); | ||
|
||
class ContigInterval { | ||
contig: string|number; | ||
interval: Interval; | ||
|
||
constructor(contig: string|number, start: number, stop: number) { | ||
this.contig = contig; | ||
this.interval = new Interval(start, stop); | ||
} | ||
|
||
// TODO: make these getter methods & switch to Babel. | ||
start() { | ||
return this.interval.start; | ||
} | ||
stop() { | ||
return this.interval.stop; | ||
} | ||
|
||
intersects(other) { | ||
return (this.contig == other.contig && | ||
this.interval.intersects(other.interval)); | ||
} | ||
|
||
toString() { | ||
return `${this.contig}:${this.start()}-${this.stop()}`; | ||
} | ||
} | ||
|
||
module.exports = ContigInterval; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* @flow */ | ||
class Interval { | ||
start: number; | ||
stop: number; | ||
|
||
// Represents [start, stop] -- both ends are inclusive. | ||
// If stop < start, then this is an empty interval. | ||
constructor(start: number, stop: number) { | ||
this.start = start; | ||
this.stop = stop; | ||
} | ||
|
||
// TODO: make this a getter method & switch to Babel. | ||
length() { | ||
return this.stop - this.start + 1; | ||
} | ||
|
||
intersect(otherInterval) { | ||
return new Interval(Math.max(this.start, otherInterval.start), | ||
Math.min(this.stop, otherInterval.stop)); | ||
} | ||
|
||
intersects(otherInterval) { | ||
return this.start <= otherInterval.stop && otherInterval.start <= this.stop; | ||
} | ||
|
||
contains(value) { | ||
return value >= this.start && value <= this.stop; | ||
} | ||
|
||
clone() { | ||
return new Interval(this.start, this.stop); | ||
} | ||
|
||
static intersectAll(intervals: Array<Interval>): Interval { | ||
if (!intervals.length) { | ||
throw 'Tried to intersect zero intervals'; | ||
} | ||
var result = intervals[0].clone(); | ||
intervals.slice(1).forEach(function({start, stop}) { | ||
result.start = Math.max(start, result.start); | ||
result.stop = Math.min(stop, result.stop); | ||
}); | ||
return result; | ||
} | ||
|
||
static boundingInterval(intervals: Array<Interval>): Interval { | ||
if (!intervals.length) { | ||
throw 'Tried to intersect zero intervals'; | ||
} | ||
var result = intervals[0].clone(); | ||
intervals.slice(1).forEach(function({start, stop}) { | ||
result.start = Math.min(start, result.start); | ||
result.stop = Math.max(stop, result.stop); | ||
}); | ||
return result; | ||
} | ||
|
||
toString() { | ||
return `[${this.start}, ${this.stop}]`; | ||
} | ||
} | ||
|
||
module.exports = Interval; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Compare two tuples of equal length. Is t1 <= t2? | ||
function tupleLessOrEqual(t1, t2) { | ||
if (t1.length != t2.length) throw 'Comparing non-equal length tuples'; | ||
for (var i = 0; i < t1.length; i++) { | ||
if (t1[i] > t2[i]) { | ||
return false; | ||
} else if (t1[i] < t2[i]) { | ||
return true; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
// Do two ranges of tuples overlap? | ||
function tupleRangeOverlaps(tupleRange1, tupleRange2) { | ||
return tupleLessOrEqual(tupleRange1[0], tupleRange2[1]) && | ||
tupleLessOrEqual(tupleRange2[0], tupleRange1[1]) | ||
} | ||
|
||
module.exports = {tupleRangeOverlaps}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
var chai = require('chai'); | ||
var expect = chai.expect; | ||
|
||
var ContigInterval = require('../src/ContigInterval'); | ||
|
||
describe('ContigInterval', function() { | ||
it('should determine intersections', function() { | ||
var tp53 = new ContigInterval(10, 7512444, 7531643); | ||
var other = new ContigInterval(10, 7512444, 7531642); | ||
|
||
expect(tp53.intersects(other)).to.be.true; | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
var chai = require('chai'); | ||
var expect = chai.expect; | ||
|
||
var Interval = require('../src/Interval'); | ||
|
||
describe('Interval', function() { | ||
it('should determine intersections', function() { | ||
var tp53 = new Interval(7512444, 7531643); | ||
var other = new Interval(7512444, 7531642); | ||
|
||
expect(tp53.intersects(other)).to.be.true; | ||
}); | ||
}); |