Skip to content

Instantly share code, notes, and snippets.

View GabrielHoffman's full-sized avatar

Gabriel Hoffman GabrielHoffman

View GitHub Profile
@GabrielHoffman
GabrielHoffman / trajectory_finemapping.R
Last active June 10, 2024 19:58
Evaluate all variants for eQTL analysis
library(lme4)
set.seed(101)
dd <- expand.grid(f1 = factor(1:10),
f2 = LETTERS[1:20], g=1:20, rep=1:15,
KEEP.OUT.ATTRS=FALSE)
summary(mu <- 5*(-4 + with(dd, as.integer(f1) + 4*as.numeric(f2))))
dd$y <- rnbinom(nrow(dd), mu = mu, size = 0.5)
str(dd)
require("MASS")
@GabrielHoffman
GabrielHoffman / zenithPR_gsa_for_DESeq2.R
Created April 30, 2024 20:34
zenithPR_gsa for DESeq2
# Run code from
# https://diseaseneurogenomics.github.io/zenith/reference/zenithPR_gsa.html
#
# Then run DESeq() and zenithPR_gsa()
# object construction
dds <- DESeqDataSetFromMatrix(geneCounts[keep,], df_metadata, ~ gender)
# standard analysis
dds <- DESeq(dds)
@GabrielHoffman
GabrielHoffman / merge_mashr.R
Created April 23, 2024 13:18
Merging mashr analyses
library(dreamlet)
library(muscat)
library(mashr)
library(SingleCellExperiment)
data(example_sce)
# create pseudobulk for each sample and cell cluster
pb <- aggregateToPseudoBulk(example_sce[1:100, ],
@GabrielHoffman
GabrielHoffman / plot_within_btw_continuous.R
Last active March 21, 2024 14:55
Plot within and between correlations for a continuous property
# Plot within and between correlations for a continuous property
plot_within_btw = function( data, info){
library(tidyverse)
library(corrr)
if( ! all(c("ID", "Property") %in% colnames(info)) ){
stop("info must have column names ID and Property")
}
@GabrielHoffman
GabrielHoffman / mash_analysis.R
Last active March 4, 2024 17:59
Composite posterior test on mashr results
# Example code for analysis of
# 1) Trait specificity
# 2) Cell-type specificity
# 1) Trait specificity
######################
library(arrow)
@GabrielHoffman
GabrielHoffman / removeVarPartComponent.R
Created February 14, 2024 19:12
Remove variance partition component and recompute fractions
#' Remove variance partition component and recompute fractions
#'
#' @param vp result of \code{fitVarPart()}
#' @param exclude array of column names to exclude
removeVarPartComponent = function(vp, exclude){
# get column index of exclude
j = match(exclude, colnames(vp)[-c(1:2)])
# compute new variance fractions
@GabrielHoffman
GabrielHoffman / Isotonic.R
Last active February 8, 2024 15:55
Isotonic regression for calibrating predictions
# x is days, y is arbitrary scale
n = 100
x = 10 + 1:n
y = scale(x) + 10*scale(x^2) + rnorm(n)
# relationship is non-linear
# but we have stoing knowledge that it *must* be monotonic
plot(x, y)
@GabrielHoffman
GabrielHoffman / get_raw_counts.R
Created February 1, 2024 15:31
Get raw counts using zellkonverter
library(zellkonverter)
library(SingleCellExperiment)
file = "240118_PsychAD_FULL_1494_subclass_EN_NF_hvg3k_umap.h5ad"
sce = readH5AD(file, use_hdf5=TRUE, verbose=TRUE, raw=TRUE)
# normalized counts
assay(sce, "X")[1:3, 1:3]
# see "alternative expression", i.e. raw counts
@GabrielHoffman
GabrielHoffman / coloc.txt
Created January 22, 2024 18:46
Color from z-scores
Gene expression Y
Genotype matrix X
TODO
1) Compute sd(Y[i,]) for gene i
2) Compute var(X[,j]) for variant j
3) beta = z / sqrt(v*(n+z^2))
sd = 1 / sqrt(v*(n+z^2))
varbeta = sd^2
where v = var(X[,j])
library(mashr)
library(corrplot)
library(remaCor)
library(ggplot2)
load("/sc/arion/projects/roussp01a/sanan/230706_NPSAD_filePrep/mashr/ckpts/mashr_ckpt2.RData")
# how many genes are non-zero in each tissue?
ngenes = apply(betaTab, 2, function(x) sum(x!=0))