Nanostring-Biostats / CosMx-Analysis-Scratch-Space

This repository is an exploratory resource to accelerate opensource analysis of CosMx® Spatial Molecular Imager (SMI) data. Contained here are and writeups and vignettes addressing a variety of topics discussed when analyzing single-cell spatial data.
https://nanostring-biostats.github.io/CosMx-Analysis-Scratch-Space/
Other
34 stars 4 forks source link

faster #125

Closed patrickjdanaher closed 23 hours ago

patrickjdanaher commented 2 days ago

Replacing some slow inner logic in FOV QC. Confirmed the downstream results are unchanged.

patrickjdanaher commented 2 days ago

Note that github is showing the diff in a weird way.

The old function:

cellxgene2squarexbit <- function(counts, grid, genes, barcodes) {

number of bits:

nreportercycles <- nchar(barcodes[1]) / 2 nbits <- nreportercycles * 4

parse barcodes:

bitmat = matrix(0, length(setdiff(unique(grid), NA)), nbits) colnames(bitmat) <- paste0("reportercycle", rep(seq_len(nreportercycles), each = 4), c("B", "G", 'Y', "R")) rownames(bitmat) <- setdiff(unique(grid), NA) for (i in seq_len(nreportercycles)) { barcodeposition <- i*2 barcodehere <- substr(barcodes, barcodeposition, barcodeposition) for (col in c("B", "Y", "G", "R")) { tempgenes <- setdiff(genes[barcodehere == col], NA) tempgenes <- intersect(tempgenes, colnames(counts)) temptotal <- Matrix::rowSums(counts[, tempgenes, drop = FALSE]) tempsquaretotal <- by(temptotal, grid, mean) bitmat[names(tempsquaretotal), paste0("reportercycle", i, col)] <- tempsquaretotal } } return(bitmat) }