Skip to content

Commit

Permalink
Support queries for "all" in straw. See #236
Browse files Browse the repository at this point in the history
  • Loading branch information
jrobinso committed Oct 21, 2018
1 parent 8342315 commit afe0500
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 14 deletions.
24 changes: 15 additions & 9 deletions js/hicStraw.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,20 @@ var hic = (function (hic) {
var self = this,
chr1 = region1.chr,
chr2 = region2.chr,
x1 = region1.start / binsize,
x2 = region1.end / binsize,
y1 = region2.start / binsize,
y2 = region2.end / binsize;
x1 = (region1.start === undefined) ? undefined : region1.start / binsize,
x2 = (region1.end === undefined) ? undefined : region1.end / binsize,
y1 = (region2.start === undefined) ? undefined : region2.start / binsize,
y2 = (region2.end === undefined) ? undefined : region2.end / binsize;

return getDataset.call(self)

.then(function (dataset) {

self.dataset = dataset;

if("ALL" === chr1.toUpperCase()) chr1 = chr1.toUpperCase();
if("ALL" === chr2.toUpperCase()) chr2 = chr2.toUpperCase();

var chr1idx = dataset.getChrIndexFromName(chr1),
chr2idx = dataset.getChrIndexFromName(chr2);

Expand All @@ -59,8 +63,9 @@ var hic = (function (hic) {
return dataset.getMatrix(chr1idx, chr2idx)
})
.then(function (matrix) {

// Find the requested resolution
var z = self.dataset.getZoomIndexForBinSize(binsize, units);
var z = undefined === binsize ? 0 : self.dataset.getZoomIndexForBinSize(binsize, units);
if (z === -1) {
throw new Error("Invalid bin size");
}
Expand All @@ -69,8 +74,8 @@ var hic = (function (hic) {
blockBinCount = zd.blockBinCount, // Dimension in bins of a block (width = height = blockBinCount)
col1 = x1 === undefined ? 0 : Math.floor(x1 / blockBinCount),
col2 = x1 === undefined ? zd.blockColumnCount : Math.floor(x2 / blockBinCount),
row1 = Math.floor(y1 / blockBinCount),
row2 = Math.floor(y2 / blockBinCount),
row1 = y1 === undefined ? 0 : Math.floor(y1 / blockBinCount),
row2 = y2 === undefined ? zd.blockColumnCount : Math.floor(y2 / blockBinCount),
row, column, sameChr, blockNumber,
promises = [];

Expand All @@ -91,15 +96,16 @@ var hic = (function (hic) {
return Promise.all(promises);
})
.then(function (blocks) {

var contactRecords = [];

blocks.forEach(function (block) {
if (block === null) { // This is most likely caused by a base pair range outside the chromosome
if (!block) { // This is most likely caused by a base pair range outside the chromosome
return;
}
block.records.forEach(function(rec) {
// TODO -- transpose?
if(rec.bin1 >= x1 && rec.bin1 <= x2 && rec.bin2 >= y1 && rec.bin2 <= y2) {
if(x1 === undefined || (rec.bin1 >= x1 && rec.bin1 <= x2 && rec.bin2 >= y1 && rec.bin2 <= y2)) {
contactRecords.push(rec);
}
});
Expand Down
13 changes: 9 additions & 4 deletions test/runTests.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@
<script src="../js/hicTrack2D.js"></script>
<script src="../js/hicStraw.js"></script>

<script src="../vendor/colors.js"></script>
<script src="../vendor/jquery-ui.js"></script>
<script src="../vendor/throbber.js"></script>
<script src="../vendor/zlib_and_gzip.js"></script>

<script src="testReader.js"></script>
<script src="testColorScale.js"></script>
<script src="testTrack2D.js"></script>
Expand All @@ -53,11 +58,11 @@
<div id="qunit-fixture"></div>
<script type="text/javascript">
document.addEventListener('DOMContentLoaded', function () {
// runTrack2DTests();
// runColorTests();
runTrack2DTests();
runColorTests();
runHicReaderTests();
// runMathTests();
// runStrawTests();
runMathTests();
runStrawTests();
});
</script>
</body>
Expand Down
27 changes: 26 additions & 1 deletion test/testStraw.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ function runStrawTests() {

}


asyncTest("Version 7 file", function () {

var url = "https://data.broadinstitute.org/igvdata/test/data/hic/inter.hic",
Expand All @@ -51,6 +50,32 @@ function runStrawTests() {
start();
})
.catch(function (error) {
ok(false);
console.error(error);
start();
})
});

asyncTest("All chromosome", function () {

var url = "https://data.broadinstitute.org/igvdata/test/data/hic/inter.hic",
normalization = "NONE",
units = "BP",
region1 = {
chr: "all"
},
binSize = undefined,
straw;

straw = new hic.Straw({url: url});

straw.getContactRecords(normalization,region1, region1, units, binSize)
.then( function(records) {
ok(records.length > 0);
start();
})
.catch(function (error) {
ok(false);
console.error(error);
start();
})
Expand Down

0 comments on commit afe0500

Please sign in to comment.