Skip to content
This repository has been archived by the owner on Jun 9, 2022. It is now read-only.

Commit

Permalink
Merge pull request #150 from cBioPortal/refac
Browse files Browse the repository at this point in the history
Refactoring to make naming clearer around universal and specific rules
  • Loading branch information
adamabeshouse authored Jun 17, 2021
2 parents ce10852 + 456055e commit ac6f2ed
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 58 deletions.
43 changes: 5 additions & 38 deletions src/js/oncoprintmodel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -831,70 +831,37 @@ export default class OncoprintModel {

public getTrackUniversalShapes(
track_id:TrackId,
use_base_size:boolean,
sort_by_z:boolean
use_base_size:boolean
):ComputedShapeParams[] {
const universalRule = this.getRuleSet(track_id).getUniversalRule();
if (!universalRule) {
return [];
}
const ruleSet = this.getRuleSet(track_id);
const spacing = this.getTrackHasColumnSpacing(track_id);
const width = this.getCellWidth(use_base_size) + (!spacing ? this.getCellPadding(use_base_size, true) : 0);
const height = this.getCellHeight(track_id, use_base_size);
const shapes = universalRule.rule.apply(
{}, // a universal rule does not rely on anything specific to the data
width,
height
)

if (sort_by_z) {
shapes.sort(z_comparator);
}

return shapes;
return ruleSet.getUniversalShapes(width, height);
}

public getSpecificShapesForData(
track_id:TrackId,
use_base_size:boolean,
sort_by_z:boolean
use_base_size:boolean
):IdentifiedShapeList[] {
const active_rules = {};
const data = this.getTrackData(track_id);
const id_key = this.getTrackDataIdKey(track_id);
const spacing = this.getTrackHasColumnSpacing(track_id);
const width = this.getCellWidth(use_base_size) + (!spacing ? this.getCellPadding(use_base_size, true) : 0);
const shapes = this.getRuleSet(track_id).apply(
const shapes = this.getRuleSet(track_id).getSpecificShapesForDatum(
data, width, this.getCellHeight(track_id, use_base_size), active_rules, id_key, this.getTrackImportantIds(track_id)
);

this.setTrackActiveRules(track_id, active_rules);

return shapes.map(function(shape_list:ComputedShapeParams[], index:number) {
if (sort_by_z) {
shape_list.sort(z_comparator);
}
return {
id: data[index][id_key],
shape_list: shape_list
};
});

/*
return shapes.reduce(function(ret:IdentifiedShapeList[], shape_list:ComputedShapeParams[], index:number) {
if (shape_list.length > 0) {
// only add entry for nonempty shape list
if (sort_by_z) {
shape_list.sort(z_comparator);
}
ret.push({
id: data[index][id_key],
shape_list: shape_list
})
}
return ret;
}, []);
*/
}

public getActiveRules(rule_set_id:RuleSetId) {
Expand Down
44 changes: 30 additions & 14 deletions src/js/oncoprintruleset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import {ComputedShapeParams, Ellipse, Line, Rectangle, Shape, ShapeParams, Triangle} from "./oncoprintshape";
import heatmapColors from "./heatmapcolors";
import binarysearch from "./binarysearch";
import {Omit, cloneShallow, ifndef, objectValues, shallowExtend} from "./utils";
import {Omit, cloneShallow, ifndef, objectValues, shallowExtend, z_comparator} from "./utils";
import {ActiveRules, ColumnProp, Datum, RuleSetId} from "./oncoprintmodel";
import _ from "lodash";
import extractrgba, {hexToRGBA, rgbaToHex} from "./extractrgba";
Expand Down Expand Up @@ -413,21 +413,37 @@ export class RuleSet {
return this.universal_rule;
}

public apply(data:Datum[], cell_width:number, cell_height:number, out_active_rules?:ActiveRules|undefined, data_id_key?:string&keyof Datum, important_ids?:ColumnProp<boolean>) {
public getUniversalShapes(cell_width:number, cell_height:number) {
if (this.getUniversalRule()) {
const shapes = this.getUniversalRule().rule.apply(
{}, // a universal rule does not rely on anything specific to the data
cell_width,
cell_height
);
shapes.sort(z_comparator);
return shapes;
} else {
return [];
}
}

public getSpecificShapesForDatum(data:Datum[], cell_width:number, cell_height:number, out_active_rules?:ActiveRules|undefined, data_id_key?:string&keyof Datum, important_ids?:ColumnProp<boolean>) {
// Returns a list of lists of concrete shapes, in the same order as data
// optional parameter important_ids determines which ids count towards active rules (optional parameter data_id_key
// is used for this too)
var ret = [];
const ret = [];
for (var i = 0; i < data.length; i++) {
var datum = data[i];
var should_mark_active = !important_ids || !!important_ids[datum[data_id_key!]];
var rules = this.getSpecificRulesForDatum(datum);
const datum = data[i];
const should_mark_active = !important_ids || !!important_ids[datum[data_id_key!]];
const rules = this.getSpecificRulesForDatum(datum);
if (typeof out_active_rules !== 'undefined' && should_mark_active) {
for (let j = 0; j < rules.length; j++) {
out_active_rules[rules[j].id] = true;
}
}
ret.push(this.applyRulesToDatum(rules, data[i], cell_width, cell_height));
const shapes = this.applyRulesToDatum(rules, data[i], cell_width, cell_height);
shapes.sort(z_comparator);
ret.push(shapes);
}
// mark universal rule as active
if (this.getUniversalRule()) {
Expand Down Expand Up @@ -603,7 +619,7 @@ class CategoricalRuleSet extends LookupRuleSet {
}
}

public apply(data:Datum, cell_width:number, cell_height:number, out_active_rules:ActiveRules|undefined, data_id_key:string&keyof Datum, important_ids?:ColumnProp<boolean>) {
public getSpecificShapesForDatum(data:Datum, cell_width:number, cell_height:number, out_active_rules:ActiveRules|undefined, data_id_key:string&keyof Datum, important_ids?:ColumnProp<boolean>) {
// First ensure there is a color for all categories
for (let i = 0, data_len = data.length; i < data_len; i++) {
if (data[i][NA_STRING]) {
Expand All @@ -618,7 +634,7 @@ class CategoricalRuleSet extends LookupRuleSet {
}
}
// Then propagate the call up
return super.apply(data, cell_width, cell_height, out_active_rules, data_id_key, important_ids);
return super.getSpecificShapesForDatum(data, cell_width, cell_height, out_active_rules, data_id_key, important_ids);
}
}

Expand Down Expand Up @@ -705,7 +721,7 @@ class LinearInterpRuleSet extends ConditionRuleSet {
}
}

public apply(data:Datum, cell_width:number, cell_height:number, out_active_rules:ActiveRules|undefined, data_id_key:string&keyof Datum, important_ids?:ColumnProp<boolean>) {
public getSpecificShapesForDatum(data:Datum, cell_width:number, cell_height:number, out_active_rules:ActiveRules|undefined, data_id_key:string&keyof Datum, important_ids?:ColumnProp<boolean>) {
// First find value range
let value_min = Number.POSITIVE_INFINITY;
let value_max = Number.NEGATIVE_INFINITY;
Expand All @@ -727,7 +743,7 @@ class LinearInterpRuleSet extends ConditionRuleSet {
this.updateLinearRules();

// Then propagate the call up
return super.apply(data, cell_width, cell_height, out_active_rules, data_id_key, important_ids);
return super.getSpecificShapesForDatum(data, cell_width, cell_height, out_active_rules, data_id_key, important_ids);
}

protected updateLinearRules() {
Expand Down Expand Up @@ -1087,17 +1103,17 @@ class GradientCategoricalRuleSet extends RuleSet {
}

// RuleSet API
public apply(data:Datum, cell_width:number, cell_height:number, out_active_rules:ActiveRules|undefined, data_id_key:string&keyof Datum, important_ids?:ColumnProp<boolean>) {
public getSpecificShapesForDatum(data:Datum, cell_width:number, cell_height:number, out_active_rules:ActiveRules|undefined, data_id_key:string&keyof Datum, important_ids?:ColumnProp<boolean>) {

const shapes = [];
// check the type of datum (categorical or continuous) and delegate
// fetching of shapes to the appropriate RuleSet class
for (let i = 0; i < data.length; i++) {
const datum = data[i];
if ( this.isCategorical(datum) ) {
shapes.push( this.categoricalRuleSet.apply([datum], cell_width, cell_height, out_active_rules, data_id_key, important_ids)[0] );
shapes.push( this.categoricalRuleSet.getSpecificShapesForDatum([datum], cell_width, cell_height, out_active_rules, data_id_key, important_ids)[0] );
} else {
shapes.push( this.gradientRuleSet.apply([datum], cell_width, cell_height, out_active_rules, data_id_key, important_ids)[0] );
shapes.push( this.gradientRuleSet.getSpecificShapesForDatum([datum], cell_width, cell_height, out_active_rules, data_id_key, important_ids)[0] );
}
}
return shapes;
Expand Down
8 changes: 4 additions & 4 deletions src/js/oncoprintwebglcellview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -969,8 +969,8 @@ export default class OncoprintWebGLCellView {
if (this.rendering_suppressed) {
return;
}
this.specific_shapes[track_id] = model.getSpecificShapesForData(track_id, true, true);
this.universal_shapes[track_id] = model.getTrackUniversalShapes(track_id, true, true);
this.specific_shapes[track_id] = model.getSpecificShapesForData(track_id, true);
this.universal_shapes[track_id] = model.getTrackUniversalShapes(track_id, true);
}

private refreshCanvas(model:OncoprintModel) {
Expand Down Expand Up @@ -1378,8 +1378,8 @@ export default class OncoprintWebGLCellView {
for (let i=0; i<tracks.length; i++) {
const track_id = tracks[i];
const offset_y = cell_tops[track_id];
const universal_shapes = model.getTrackUniversalShapes(track_id, false, true);
const identified_shape_list_list = model.getSpecificShapesForData(track_id, false, true);
const universal_shapes = model.getTrackUniversalShapes(track_id, false);
const identified_shape_list_list = model.getSpecificShapesForData(track_id, false);
for (let j=0; j<identified_shape_list_list.length; j++) {
const id_sl = identified_shape_list_list[j];
const id = id_sl.id;
Expand Down
4 changes: 2 additions & 2 deletions src/test/gradientCategoricalRuleset.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe("GradientCategoricalRuleSet", function() {

it("Formats gradient value", function() {
var mixRuleSet = OncoprintRuleSet(mixParams);
var elements = mixRuleSet.apply([gradientDatumLargest, gradientDatumSmallest, naDatum], 12, 12, undefined, "id");
var elements = mixRuleSet.getSpecificShapesForDatum([gradientDatumLargest, gradientDatumSmallest, naDatum], 12, 12, undefined, "id");
assert.equal(elements.length, 3);
assert.deepEqual(elements[0][0].fill,[0,255,0,1]);
assert.deepEqual(elements[1][0].fill,[255,0,0,1]);
Expand All @@ -57,7 +57,7 @@ describe("GradientCategoricalRuleSet", function() {

it("Formats categorical value", function() {
var mixRuleSet = OncoprintRuleSet(mixParams);
var elements = mixRuleSet.apply([categoryDatum], 12, 12, undefined, "id");
var elements = mixRuleSet.getSpecificShapesForDatum([categoryDatum], 12, 12, undefined, "id");
assert.equal(elements.length, 1);
});

Expand Down

0 comments on commit ac6f2ed

Please sign in to comment.