Skip to content

Commit

Permalink
modularized sample coloring
Browse files Browse the repository at this point in the history
  • Loading branch information
kwcantrell committed Sep 30, 2019
1 parent 8abc73e commit 712585d
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 123 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
trees/metadata used for development
# trees/metadata used for development
data/

# NodeJS file used for chrome headless QUnit tests
Expand Down
4 changes: 2 additions & 2 deletions empress/empress-biom.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
from tree import Tree

# create/parse tree
# tree_file = 'data/97_sites.nwk'
tree_file = 'data/97_otus_no_none.tree'
tree_file = 'data/97_sites.nwk'
# tree_file = 'data/97_otus_no_none.tree'
with open(tree_file) as file:
nwk = file.readline();
t = parse_newick(nwk)
Expand Down
45 changes: 0 additions & 45 deletions empress/empress-tree.py

This file was deleted.

138 changes: 75 additions & 63 deletions empress/support_files/js/biom-table.js
Original file line number Diff line number Diff line change
@@ -1,83 +1,95 @@
define([], function() {
/**
* biom-table
*/
function BiomTable(obs, samp) {
/**
* The observation table format:
* {sampleID1: [observationIDs],
sampleID2: [observationIDs],
...}
* @private
* biom-table
*/
this._obs = obs;
function BiomTable(obs, samp) {
/**
* The observation table format:
* {sampleID1: [observationIDs],
sampleID2: [observationIDs],
...}
* @private
*/
this._obs = obs;

/**
* @type {Dictionary}
* Sample metedata format:
* {sampleID1: {cat1: val, cat2: val, ...},
sampleID2: {cat1: val, cat2: val, ...},
...}
* @private
*/
this._samp = samp;
};

/**
* @type {Dictionary}
* Sample metedata format:
* {sampleID1: {cat1: val, cat2: val, ...},
sampleID2: {cat1: val, cat2: val, ...},
...}
* @private
* Returns the list of observations in the sample
*
* @param {String} The sample Id
*
* @return {Array}
*/
this._samp = samp;
};
BiomTable.prototype.getSampleObs = function(sId) {
var result = new Set();
return this._obs[sId];
};

/**
* Returns a dictionary of observation ids whose keys are the values of a sample
* category.
*
* @param {String} The category to return observation
*
* @return {Dictionary}
*/
BiomTable.prototype.getObsBy = function(cat) {
var result = {};
var cVal;
for (var sample in this._samp) {
cVal = this._samp[sample][cat];
if (!(cVal in result)) {
result[cVal] = new Set();
}
for (var i = 0; i < this._obs[sample].length; i++) {
result[cVal].add(this._obs[sample][i]);
/**
* Returns a dictionary of observation ids whose keys are the values of a sample
* category.
*
* @param {String} The category to return observation
*
* @return {Dictionary}
*/
BiomTable.prototype.getObsBy = function(cat) {
var result = {};
var cVal;
for (var sample in this._samp) {
cVal = this._samp[sample][cat];
if (!(cVal in result)) {
result[cVal] = new Set();
}
for (var i = 0; i < this._obs[sample].length; i++) {
result[cVal].add(this._obs[sample][i]);
}
}
}

for (var key in result) {
result[key] = Array.from(result[key]);
}
for (var key in result) {
result[key] = Array.from(result[key]);
}

return result;
};
return result;
};

/**
* Returns number of unique observations in samples.
*
* @return {Number}
*/
BiomTable.prototype.getUniqueObs = function() {
var obs = new Set();
/**
* Returns number of unique observations in samples.
*
* @return {Number}
*/
BiomTable.prototype.getUniqueObs = function() {
var obs = new Set();

for (var sample in this._samp) {
for (var i = 0; i < this._obs[sample].length; i++) {
obs.add(this._obs[sample][i]);
for (var sample in this._samp) {
for (var i = 0; i < this._obs[sample].length; i++) {
obs.add(this._obs[sample][i]);
}
}
}

return obs.size;
};
return obs.size;
};



/**
* Returns a list of sample categories
*
* @return{Array}
*/
BiomTable.prototype.getSampleCats = function() {
return Object.keys((Object.values(this._samp)[0])).sort();
};
/**
* Returns a list of sample categories
*
* @return{Array}
*/
BiomTable.prototype.getSampleCats = function() {
return Object.keys((Object.values(this._samp)[0])).sort();
};

return BiomTable;
});
28 changes: 17 additions & 11 deletions empress/support_files/js/empress.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ define(['Camera', 'Drawer', 'Colorer', 'VectorOps'],
const BLUE = 2;

/**
*
* @class EmpressTree
*
*/
function Empress(tree, treeData, biom, pToName, canvas ) {
/**
Expand Down Expand Up @@ -223,6 +221,20 @@ define(['Camera', 'Drawer', 'Colorer', 'VectorOps'],
this._drawer.loadSampleThickBuf(coords);
}

/**
* Color the tree by sample IDs
*
* @param {Array} sID - The sample IDs
* @param {Array} rgb - The rgb array which defines the color
*/
Empress.prototype.colorSampleIDs = function(sIds, rgb) {
var tree = this._tree;
var obs = this._biom.getSampleObs(sIds);
for (var i = 0; i < obs.length; i++) {
this._treeData[obs].color = rgb;
}
};

/**
* Color the tree using sample data
*
Expand All @@ -231,7 +243,7 @@ define(['Camera', 'Drawer', 'Colorer', 'VectorOps'],
*
* @return {dictionary} Maps keys to colors
*/
Empress.prototype.colorBySample = function(cat, color) {
Empress.prototype.colorBySampleCat = function(cat, color) {
var tree = this._tree;
var obs = this._biom.getObsBy(cat);
var primes = [2, 3, 5, 7];
Expand Down Expand Up @@ -316,22 +328,16 @@ define(['Camera', 'Drawer', 'Colorer', 'VectorOps'],
return keyInfo;
};

// Empress.prototype.getSampleKeyInfo = function(cat) {

// };

/**
* Sets the color of the tree back to default
*/
Empress.prototype.resetTree = function() {
var keys = Object.keys(this._treeData);
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
this._treeData[key].color = this.DEFAULT_COLOR;
// this._treeData[key].sampVal = this.DEFAULT_BRANCH_VAL;
this._treeData[key].visible = true;
this._treeData[key].color = this.DEFAULT_COLOR; this._treeData[key].visible = true;
}
// this._drawer.loadSampleThickBuf([]);
this._drawer.loadSampleThickBuf([]);
};

/**
Expand Down
2 changes: 1 addition & 1 deletion empress/support_files/js/side-panel-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ define(['Colorer'], function(Colorer) {
var colBy = this.sSel.value;
var col = this.sColor.value;
var hide = this.sHideChk.checked;
var keyInfo = this.empress.colorBySample(colBy, col);
var keyInfo = this.empress.colorBySampleCat(colBy, col);
this.empress.hideUnColoredTips(hide);
this.legend.addColorKey(colBy, keyInfo, 'node', false);
};
Expand Down

0 comments on commit 712585d

Please sign in to comment.