This commit is contained in:
bkmartinjr
2018-05-05 13:15:10 -07:00
parent d91d42f2c4
commit 90c7daf571
62 changed files with 2398 additions and 1912 deletions
+50 -63
View File
@@ -1,9 +1,8 @@
// jshint esversion: 6
import uri from "urijs";
import * as globals from "../globals";
import _ from "lodash";
import {
parseRGB
} from "../util/parseRGB";
import { parseRGB } from "../util/parseRGB";
/*
https://medium.com/@jacobp100/you-arent-using-redux-middleware-enough-94ffe991e6
@@ -21,27 +20,26 @@ import {
This is nice because we keep a lot of filtering business logic centralized (what it means in practice to be selected)
*/
const updateCellSelectionMiddleware = (store) => {
return (next) => {
return (action) => {
const updateCellSelectionMiddleware = store => {
return next => {
return action => {
const s = store.getState();
/* this is a hardcoded map of the things we need to keep an eye on and update global cell selection in response to */
const filterJustChanged =
action.type === "color by expression" ||
action.type === "color by continuous metadata" ||
action.type === "color by categorical metadata"
;
action.type === "color by categorical metadata";
if (
!filterJustChanged ||
!s.controls.allCellsOnClient
) {
return next(action); /* if the cells haven't loaded or the action wasn't a color change, bail */
if (!filterJustChanged || !s.controls.allCellsOnClient) {
return next(
action
); /* if the cells haven't loaded or the action wasn't a color change, bail */
}
let currentSelectionWithUpdatedColors = s.controls.currentCellSelection.slice(0);
let currentSelectionWithUpdatedColors = s.controls.currentCellSelection.slice(
0
);
let colorScale;
/*
@@ -54,45 +52,32 @@ const updateCellSelectionMiddleware = (store) => {
*/
if (action.type === "color by categorical metadata") {
colorScale = d3.scaleOrdinal().range(globals.ordinalColors);
_.each(currentSelectionWithUpdatedColors, (cell, i) => {
let c = colorScale(
cell[action.colorAccessor]
);
let c = colorScale(cell[action.colorAccessor]);
currentSelectionWithUpdatedColors[i]["__color__"] = c;
currentSelectionWithUpdatedColors[i]["__colorRGB__"] = parseRGB(c);
})
});
}
if (action.type === "color by continuous metadata") {
colorScale = d3.scaleLinear()
colorScale = d3
.scaleLinear()
.domain([0, action.rangeMaxForColorAccessor])
.range([1,0])
.range([1, 0]);
_.each(currentSelectionWithUpdatedColors, (cell, i) => {
let c = d3.interpolateViridis(
colorScale(
cell[action.colorAccessor]
)
);
let c = d3.interpolateViridis(colorScale(cell[action.colorAccessor]));
currentSelectionWithUpdatedColors[i]["__color__"] = c;
currentSelectionWithUpdatedColors[i]["__colorRGB__"] = parseRGB(c);
})
});
}
if (action.type === "color by expression") {
const indexOfGene = 0; /* we only get one, this comes from server as needed now */
const indexOfGene = 0 /* we only get one, this comes from server as needed now */
const expressionMap = {}
const expressionMap = {};
/*
converts [{cellname: cell123, e}, {}]
@@ -101,50 +86,52 @@ const updateCellSelectionMiddleware = (store) => {
cell789: [0, 8]
}
*/
_.each(action.data.data.cells, (cell) => { /* this action is coming directly from the server */
expressionMap[cell.cellname] = cell.e
})
_.each(action.data.data.cells, cell => {
/* this action is coming directly from the server */
expressionMap[cell.cellname] = cell.e;
});
const minExpressionCell = _.minBy(action.data.data.cells, (cell) => {
return cell.e[indexOfGene]
})
const minExpressionCell = _.minBy(action.data.data.cells, cell => {
return cell.e[indexOfGene];
});
const maxExpressionCell = _.maxBy(action.data.data.cells, (cell) => {
return cell.e[indexOfGene]
})
const maxExpressionCell = _.maxBy(action.data.data.cells, cell => {
return cell.e[indexOfGene];
});
// console.log('middle', action, expressionMap, minExpressionCell)
colorScale = d3.scaleLinear()
.domain([minExpressionCell.e[indexOfGene], maxExpressionCell.e[indexOfGene]])
.range([1,0]) /* invert viridis... probably pass this scale through to others */
colorScale = d3
.scaleLinear()
.domain([
minExpressionCell.e[indexOfGene],
maxExpressionCell.e[indexOfGene]
])
.range([
1,
0
]); /* invert viridis... probably pass this scale through to others */
_.each(currentSelectionWithUpdatedColors, (cell, i) => {
let c = d3.interpolateViridis(
colorScale(
expressionMap[cell.CellName][indexOfGene]
)
colorScale(expressionMap[cell.CellName][indexOfGene])
);
currentSelectionWithUpdatedColors[i]["__color__"] = c;
currentSelectionWithUpdatedColors[i]["__colorRGB__"] = parseRGB(c);
})
});
}
/*
append the result of all the filters to the action the user just triggered
*/
let modifiedAction = Object.assign(
{},
action,
{
currentSelectionWithUpdatedColors,
colorScale
}
)
let modifiedAction = Object.assign({}, action, {
currentSelectionWithUpdatedColors,
colorScale
});
return next(modifiedAction);
}
}
};
};
};
export default updateCellSelectionMiddleware;