UNPKG

bakana

Version:

Backend for kana's single-cell analyses. This supports single or multiple samples, execution in Node.js or the browser, in-memory caching of results for iterative analyses, and serialization to/from file for redistribution.

137 lines (115 loc) 4.61 kB
import * as scran from "scran.js"; import * as utils from "./utils/general.js"; import * as nutils from "./utils/normalization.js"; import * as qc_module from "./crispr_quality_control.js"; import * as filter_module from "./cell_filtering.js"; export const step_name = "crispr_normalization"; /** * This step performs normalization and log-transformation on the QC-filtered CRISPR count matrix from the {@linkplain CellFilteringState}. * It wraps the [`normalizeCounts`](https://kanaverse.github.io/scran.js/global.html#normalizeCounts) functions * from [**scran.js**](https://github.com/kanaverse/scran.js). * * Methods not documented here are not part of the stable API and should not be used by applications. * @hideconstructor */ export class CrisprNormalizationState { #qc; #filter; #parameters; #cache; constructor(qc, filter, parameters = null, cache = null) { if (!(qc instanceof qc_module.CrisprQualityControlState)) { throw new Error("'qc' should be a CrisprQualityControlState object"); } this.#qc = qc; if (!(filter instanceof filter_module.CellFilteringState)) { throw new Error("'filter' should be a CellFilteringState object"); } this.#filter = filter; this.#parameters = (parameters === null ? {} : parameters); this.#cache = (cache === null ? {} : cache); this.changed = false; } free() { utils.freeCache(this.#cache.matrix); utils.freeCache(this.#cache.sf_buffer); } /*************************** ******** Getters ********** ***************************/ valid() { let filtered = this.#filter.fetchFilteredMatrix(); return filtered.has("CRISPR"); } /** * @return {external:ScranMatrix} A {@linkplain external:ScranMatrix ScranMatrix} object containing the normalized CRISPR abundances, * available after running {@linkcode CrisprNormalizationState#compute compute}. */ fetchNormalizedMatrix() { if (!("matrix" in this.#cache)) { this.#raw_compute(); } return this.#cache.matrix; } /** * @return {Float64WasmArray} Array of length equal to the number of cells, * containing the CRISPR-derived size factor for each cell. * This is available after running {@linkcode CrisprNormalizationState#compute compute}. */ fetchSizeFactors() { return this.#cache.sf_buffer; } /** * @return {object} Object containing the parameters. */ fetchParameters() { return { ...this.#parameters }; // avoid pass-by-reference links. } /*************************** ******** Compute ********** ***************************/ #raw_compute() { var mat = this.#filter.fetchFilteredMatrix().get("CRISPR"); let raw_sf = nutils.subsetSums(this.#qc, this.#filter, mat); let block = this.#filter.fetchFilteredBlock(); let buffer = utils.allocateCachedArray(raw_sf.length, "Float64Array", this.#cache, "sf_buffer"); scran.centerSizeFactors(raw_sf, { block: block, buffer: buffer }); utils.freeCache(this.#cache.matrix); this.#cache.matrix = scran.normalizeCounts(mat, { sizeFactors: buffer, allowZeros: true }); return; } /** * This method should not be called directly by users, but is instead invoked by {@linkcode runAnalysis}. * * @param {object} parameters - Parameter object, equivalent to the `crispr_normalization` property of the `parameters` of {@linkcode runAnalysis}. * * @return The object is updated with new results. */ compute(parameters) { parameters = utils.defaultizeParameters(parameters, CrisprNormalizationState.defaults()); this.changed = false; if (this.#qc.changed || this.#filter.changed) { if (this.valid()) { this.#raw_compute(); this.changed = true; } } this.#parameters = parameters; return; } /** * @return {object} Object containing default parameters, * see the `parameters` argument in {@linkcode CrisprNormalizationState#compute compute} for details. */ static defaults() { return {}; } /************************* ******** Saving ********* *************************/ serialize(handle) { let ghandle = handle.createGroup(step_name); let phandle = ghandle.createGroup("parameters"); let rhandle = ghandle.createGroup("results"); } }