Created
November 12, 2014 22:00
-
-
Save ptgolden/199d988ee1607c9963ab to your computer and use it in GitHub Desktop.
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
var NUM_BINS = 2; | |
var data = [ | |
[5, 15, 25, 35], | |
[25, 35, 5, 15], | |
[25, 25, 15, 5], | |
[0, 10, 20, 40] | |
] | |
var all = Array.prototype.concat.apply([], data) | |
var bins = d3.scale.linear().domain([d3.min(all), d3.max(all)]).ticks(NUM_BINS); | |
var numCells = data[0].length; | |
var genesByBin = bins.reduce(function (acc, bin, binIdx) {acc[binIdx] = []; return acc}, {}); | |
var binsByGene = data.reduce(function (acc, gene, geneIdx) {acc[geneIdx] = []; return acc}, {}); | |
for (var cellIdx = 0; cellIdx < numCells; cellIdx++) { | |
var cell = data | |
.map(function (gene, geneIdx) { return {geneIdx: geneIdx, rpkm: gene[cellIdx]} } ) | |
.sort(function (a, b) { return a.rpkm - b.rpkm }) | |
var binIdx = 0; | |
var binLength = bins.length; | |
cell.forEach(function (gene) { | |
while (true) { | |
if (gene.rpkm <= bins[binIdx + 1]) { | |
genesByBin[binIdx].push(gene.geneIdx); | |
binsByGene[gene.geneIdx].push(binIdx); | |
break; | |
} | |
binIdx += 1; | |
if (binIdx >= binLength) throw new Error("Couldn't place all elements in bins."); | |
} | |
}); | |
} | |
for (var cellIdx = 0; cellIdx < numCells; cellIdx++) { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment