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 / intron_exon_counter.R
Last active November 17, 2022 17:49
A script to count reads in introns and exons
# Reading in the annotation.
library(TxDb.Mmusculus.UCSC.mm10.ensGene)
intronic <- intronicParts(TxDb.Mmusculus.UCSC.mm10.ensGene, linked.to.single.gene.only=TRUE)
exonic <- exonicParts(TxDb.Mmusculus.UCSC.mm10.ensGene, linked.to.single.gene.only=TRUE)
IX <- data.frame(GeneID=intronic$gene_id, Chr=as.character(seqnames(intronic)),
Start=start(intronic), End=end(intronic), Strand=strand(intronic), stringsAsFactors=FALSE)
EX <- data.frame(GeneID=exonic$gene_id, Chr=as.character(seqnames(exonic)),
Start=start(exonic), End=end(exonic), Strand=strand(exonic), stringsAsFactors=FALSE)
@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)
}
#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 / 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.
@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 / 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 / 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 / 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 / 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 / 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;
}