Skip to content

Instantly share code, notes, and snippets.

View GabrielHoffman's full-sized avatar

Gabriel Hoffman GabrielHoffman

View GitHub Profile
@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))
@GabrielHoffman
GabrielHoffman / splines_basis_Age.R
Last active November 27, 2023 20:37
Splines with standard basis
Age = 1:100
dsgn = ns(Age,df=2)
rownames(dsgn) = as.character(Age)
dsgn['66',]
dsgn = ns(Age[-c(1:5)],df=2)
rownames(dsgn) = as.character(Age[-c(1:5)])
dsgn['66',]