Skip to content

Commit 77ef78c

Browse files
committed
scale gene viewer data according min/max in all time points. see #36.
1 parent 4d7cc91 commit 77ef78c

File tree

3 files changed

+66
-40
lines changed

3 files changed

+66
-40
lines changed

app/pods/components/pv-wrapper/component.js

+24-13
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export default Ember.Component.extend({
1313
// bound
1414
structure: null,
1515
data: [],
16+
range: [0, 1],
1617

1718
// created
1819
viewer: null,
@@ -62,7 +63,7 @@ export default Ember.Component.extend({
6263

6364
updateColors: function() {
6465
Ember.run.once(this, '_updateColors');
65-
}.observes('data.[]', 'structure', 'geometry'),
66+
}.observes('data.[]', 'range.[]', 'structure', 'geometry'),
6667

6768
_updateColors() {
6869
var geometry = this.get('geometry');
@@ -76,24 +77,34 @@ export default Ember.Component.extend({
7677
var gradient = pv.color.gradient(['white', 'darkred']);
7778
if (data) {
7879
// remap to [0, 1], since pv's stops seems broken
80+
var range = this.get('range');
7981
minval = d3.min(data);
8082
maxval = d3.max(data);
81-
if (-minval === maxval) {
83+
if (range[0] > minval) {
84+
throw {name: 'RangeError', message: 'range[0] too large'};
85+
}
86+
if (range[1] < maxval) {
87+
throw {name: 'RangeError', message: 'range[1] too small'};
88+
}
89+
if (range[0] < 0) {
8290
// TODO: get gradient stops working, so this is unnecessary
91+
// map [range[0], 0] to [0, 0.5]
92+
// map [0, range[1]] to [0, 0.5]
8393
gradient = pv.color.gradient(['darkblue', 'white', 'darkred']);
8494
}
85-
var range = maxval - minval;
86-
data = _.map(data, d => (d - minval) / range);
95+
data = _.map(data, d => (d - range[0]) / (range[1] - range[0]));
96+
97+
structure.eachResidue(function(res) {
98+
var ref_coord = res.num();
99+
var val = data[ref_coord];
100+
val = (val === undefined ? 0 : val);
101+
res.customData = function() {return val;};
102+
});
103+
var newrange = [0, 1];
104+
var colorOp = pv.color.byResidueProp('customData', gradient, newrange);
105+
geometry.colorBy(colorOp);
106+
this.get('viewer').requestRedraw();
87107
}
88-
structure.eachResidue(function(res) {
89-
var ref_coord = res.num();
90-
var val = data[ref_coord];
91-
val = (val === undefined ? 0 : val);
92-
res.customData = function() {return val;};
93-
});
94-
var colorOp = pv.color.byResidueProp('customData', gradient);
95-
geometry.colorBy(colorOp);
96-
this.get('viewer').requestRedraw();
97108
},
98109

99110
labelResidues: function() {

app/pods/session/gene/controller.js

+41-26
Original file line numberDiff line numberDiff line change
@@ -92,43 +92,58 @@ export default Ember.Controller.extend({
9292
}.property('selectedMetric', 'divergence', 'entropy', 'meanDN'),
9393

9494
structureData: function() {
95-
var idx = this.get('selectedTimepointIdx');
96-
if (idx >= this.get('names.length')) {
97-
// a hack; presumable selectedIdx will be updated later
98-
idx = this.get('names.length') - 1;
99-
}
10095
var metric = this.get('selectedMetric');
10196
if (metric === "Entropy") {
102-
return this.get('entropy')[idx];
97+
return this.get('entropy');
10398
}
10499
if (metric === "JS Divergence") {
105-
return this.get('divergence')[idx];
100+
return this.get('divergence');
106101
}
107-
var dn = this.get('meanDN');
108-
var ds = this.get('meanDS');
109-
var zipped = _.zip(dn[idx], ds[idx]);
110-
// TODO: do not hardcode these values
111-
// FIXME: fix issue when number of timepoints changes; selector should remain on current one if possible
112-
var upper = Math.log(5);
113-
var lower = Math.log(1/5);
114-
var ratios = zipped.map(function(pair) {
115-
var result = Math.log(pair[0] / pair[1]);
116-
// cap extreme values
117-
if (result > upper) {
118-
result = upper;
119-
} else if (result < lower) {
120-
result = lower;
102+
if (metric === 'dNdS') {
103+
var dn = this.get('meanDN');
104+
var ds = this.get('meanDS');
105+
var result = [];
106+
for (let idx=0; idx<dn.length; idx++) {
107+
var zipped = _.zip(dn[idx], ds[idx]);
108+
var logratios = zipped.map(function(pair) {
109+
var logratio = Math.log(pair[0] / pair[1]);
110+
return logratio;
111+
});
112+
result.push(logratios);
121113
}
122114
return result;
123-
});
124-
// take only reference coordinates
125-
var coordMap = this.get('model.coordinates.refToFirstAlnCoords');
126-
var result = _.map(coordMap, alnCoord => ratios[alnCoord] || 0);
127-
return result;
115+
}
116+
throw {name: 'UnknownMetricError', message: metric};
128117
}.property('model.coordinates.refToFirstAlnCoords',
129118
'meanDN', 'meanDS', 'entropy', 'divergence', 'selectedTimepointIdx',
130119
'selectedMetric'),
131120

121+
selectedStructureData: function() {
122+
var idx = this.get('selectedTimepointIdx');
123+
if (idx >= this.get('names.length')) {
124+
// a hack; presumable selectedIdx will be updated later
125+
idx = this.get('names.length') - 1;
126+
}
127+
var data = this.get('structureData')[idx];
128+
// take only reference coordinates
129+
var coordMap = this.get('model.coordinates.refToFirstAlnCoords');
130+
var result = _.map(coordMap, alnCoord => data[alnCoord] || 0);
131+
return result;
132+
}.property('structureData', 'selectedTimepointIdx'),
133+
134+
structureDataRange: function() {
135+
var data = this.get('structureData');
136+
var minval = d3.min(data, d => d3.min(d));
137+
var maxval = d3.max(data, d => d3.max(d));
138+
if (minval < 0 && maxval > 0) {
139+
var r = Math.max(Math.abs(minval), maxval);
140+
minval = -r;
141+
maxval = r;
142+
}
143+
console.log([minval, maxval]);
144+
return [minval, maxval];
145+
}.property('structureData'),
146+
132147
timepoints: function() {
133148
if (this.get('selectedMetric') === "JS Divergence") {
134149
var divergence = this.get('model.divergence.sortedDivergence');

app/pods/session/gene/template.hbs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<div class="pv-viewer">
2-
{{pv-wrapper structure=model.structure data=structureData}}
2+
{{pv-wrapper structure=model.structure data=selectedStructureData range=structureDataRange}}
33
</div>
44

55
<div class="form-horizontal control-group">

0 commit comments

Comments
 (0)