Skip to content

Instantly share code, notes, and snippets.

View LTLA's full-sized avatar
🤯
Breaking hearts and minds

Aaron Lun LTLA

🤯
Breaking hearts and minds
View GitHub Profile
@LTLA
LTLA / 3d_chisq.R
Created November 24, 2022 06:43
Three-factor chi-squared test of independence
# Demonstrate that this is chi-squared distributed
# with xyz - (x-1) - (y-1) - (z-1) -1 d.f.
stat <- numeric(1000)
for (i in seq_along(stat)) {
N <- 1000
x <- rmultinom(1, size=N, prob=rep(1/27, 27))
dim(x) <- c(3,3,3)
p1 <- vapply(1:3, function(i) sum(x[i,,]), 1) / N
p2 <- vapply(1:3, function(i) sum(x[,i,]), 1) / N
@LTLA
LTLA / manual_svg.js
Created November 9, 2022 07:05
Manual scatterplot to SVG
function range(x) {
let maxed = Number.NEGATIVE_INFINITY;
let mined = Number.POSITIVE_INFINITY;
for(const y of x) {
if (y > maxed) {
maxed = y;
}
if (y < mined) {
mined = y;
}
@LTLA
LTLA / downsample_by_neighbors.R
Created August 10, 2022 20:40
Downsample code based on the neighbors
mat <- matrix(rnorm(100000), ncol=10)
library(BiocNeighbors)
res <- findKNN(mat, k=20)
d <- res$distance[,ncol(res$distance)]
processed <- logical(nrow(mat))
candidates <- logical(nrow(mat))
while (!all(processed)) {
@LTLA
LTLA / hcl_random.R
Created July 19, 2022 05:12
Decent HCL random color generation
# The hue is evenly distributed along the circle;
# we oscillate the luminescence to provide a better
# distinction between similar colors.
n <- 30
i <- head(seq(0, 360, length.out=n+1), -1)
cols <- hcl(i, c=50, l=80 + 10 * sin(i/30 * pi))
plot(seq_along(cols), col=cols, pch=16, cex=5)
@LTLA
LTLA / tiledb_segfault_script.R
Last active May 16, 2021 01:02
TileDB segfault script (mac only)
library(tiledb)
dim <- c(500L, 200L)
context <- tiledb_ctx()
collected <- vector("list", length(dim))
for (i in seq_along(dim)) {
collected[[i]] <- tiledb_dim(ctx=context, paste0("d", i), c(1L, dim[i]), tile=100L, type="INT32")
}
dom <- tiledb_domain(ctx=context, dims=collected)
@LTLA
LTLA / iSEE_launcher.R
Last active February 2, 2020 08:56
iSEE launching from link
############# The usual ################
library(scRNAseq)
# Example data ----
sce <- ReprocessedAllenData(assays="tophat_counts")
class(sce)
library(scater)
sce <- logNormCounts(sce, exprs_values="tophat_counts")
@LTLA
LTLA / multicore_efficiency.R
Created October 19, 2019 00:28
Efficiency problems with MulticoreParam
library(scRNAseq)
hESCs <- LaMannoBrainData('human-es')
# Trivial function to get something working on two matrices.
FUN <- function(x, y) { list(colSums(x), colSums(y)) }
library(BiocParallel)
BPPARAM1 <- SerialParam()
BPPARAM5 <- MulticoreParam(5)
@LTLA
LTLA / weighted.R
Last active March 6, 2019 03:17
Weighted pseudo-bulk samples
library(edgeR)
g <- factor(rep(1:4, c(50, 20, 10, 5)))
N <- 20000
mus <- 100
# mus <- 100 * 2^rnorm(N*length(g)) # Uncomment for variable mu
y <- matrix(rnbinom(N*length(g), mu=mus, size=1), nrow=N, byrow=TRUE)
design <- model.matrix(~gl(2,2))
# Summation.
#include "beachtest.h"
// Don't forget to set appropriate PKG_CXXFLAGS.
#include <omp.h>
#include <iostream>
extern "C"{
SEXP parallel_reader(SEXP input) {
@LTLA
LTLA / parallel_crossprod.R
Last active September 22, 2018 17:01
Parallelized calculation of the cross-product, computed with as much memory efficiency as possible.
library(BiocParallel)
library(DelayedArray)
#' @importFrom stats start end
.grid_iterate <- function(x, grid = NULL) {
b <- 0L
function() {
if (b == length(grid)) {
return(NULL)
}