Skip to content

Instantly share code, notes, and snippets.

@nievergeltlab
nievergeltlab / heritability.r
Last active May 26, 2023 10:42
Convert heritability from observed scale to liability scale
#K=pop prevalence
#P=proportion of cases in study
#hsq=Heritability estimate (on observed scale)
#bigT = liability threshold
#tau = density of gaussian
K=0.0659
P=0.0659
h2=0.0365
zv <- dnorm(qnorm(K))
@nievergeltlab
nievergeltlab / ldsc_results_extractor.sh
Created January 5, 2023 19:40
Extract LDSC results from a list of .log files. Makes it easy to put many results into a table.
for files in $(ls | grep log | grep -v munge | grep ldsc)
do
h2snp=$(cat $files | grep "Observed scale" | sed 's/(//g' | sed 's/)//g' | awk '{print $5,$6, $5/$6}')
intercept=$(cat $files | grep "Intercept" | sed 's/(//g' | sed 's/)//g' | awk '{print $2,$3, ($2-1)/$3}' )
ratio=$(cat $files | grep "Ratio" | sed 's/(//g' | sed 's/)//g' | awk '{print $2,$3, $2/$3}' )
lambda=$(cat $files | grep "Lambda" | sed 's/(//g' | sed 's/)//g' | awk '{print $3}' )
chisq=$(cat $files | grep "Mean Chi" | sed 's/(//g' | sed 's/)//g' | awk '{print $3}' )
outname=$(echo $files | sed 's/.log//g')
@nievergeltlab
nievergeltlab / tin_gwas1
Last active January 10, 2023 18:21
sample code for running BOLTLmm
BOLT-LMM_v2.3.2/bolt \
--bed=pca/UKB_tinnitus_eur_unrelated_{1..22}.bed \
--bim=pca/UKB_tinnitus_eur_unrelated_{1..22}.bim \
--fam=pca/UKB_tinnitus_eur_unrelated_1.fam \
--LDscoresFile=BOLT-LMM_v2.3.2/tables/LDSCORE.1000G_EUR.tab.gz \
--geneticMapFile=BOLT-LMM_v2.3.2/tables/genetic_map_hg19_withX.txt.gz \
--lmmForceNonInf \
--numThreads=6 \
--bgenFile=/mnt/ukbb/adam/bgen/ukb_imp_chr{1..22}_v3.bgen \
--bgenMinMAF=1e-3 \
@nievergeltlab
nievergeltlab / replicate_region.r
Created August 17, 2022 20:37
Determine if a SNP is within a locus by applying a TRUE/FALSE criteria to each required bound. If all bounds are true, the SNP is within a given locus..
d1$replicated <- NA
#All 3 must be true
for (i in 1:dim(d1)[1])
{
d1[i,]$replicated <- any(d1[i,]$chr == d2$CHR & d1[i,]$pos >= d2$START & d1[i,]$pos <= d2$STOP)
}
@nievergeltlab
nievergeltlab / nb_logodds.r
Created June 14, 2022 16:35
Following Sroka 2018, likelihood calculation for a nb glm model with a log odds link This provides odds ratios for count data!
#Y is an Nx1 vector of the phenotype
#x is a NxN matrix of covariates
#b is beta (supplied by optim)
#d is a dispersion parameter (supplied by optim)
likfun1 <- function(y,x,theta)
{
#the beta parameters will be the first N-1 inputs supplied by optim, dispersion the last
b=theta[-length(theta)]
d=theta[length(theta)]
loglik1 <- sum ( log(gamma(y+d)) -log(gamma(y + 1)) - log(gamma(d)) +
@nievergeltlab
nievergeltlab / get_ns.r
Created June 30, 2021 16:46
Intersect phenotype and covariate files to get total N from each phenotype file.
for file in $(ls pheno | grep .pheno)
do
fname=$(echo $file | awk 'BEGIN {FS="_"}{print $2}')
fname2=$(echo $file | awk 'BEGIN {FS="_"}{print $3}' | sed 's/.pheno//g')
# scount=$(awk '{print $3}' pheno/$file | grep -v NA | wc -l | awk '{print $1}')
scount=$(LC_ALL=C join <(awk '{print $1"_"$2,$3}' pheno/$file | LC_ALL=C sort -k1b,1 ) <( awk '{print $1"_"$2}' pheno/p2_"$fname"_eur_"$fname2"_agesex.cov | LC_ALL=C sort -k1b,1) | awk '{print $2}' | grep -v NA | wc -l | awk '{print $1}')
echo $fname $fname2 $scount
head -n1 pheno/$file
@nievergeltlab
nievergeltlab / plink_merge.sh
Last active June 30, 2021 16:09
Merge long list of PLINK files
@nievergeltlab
nievergeltlab / hwe.r
Last active June 29, 2021 21:41
HWE calculation in R, based on knowing the counts
nAA=76
nAa=1501
naa=7823
n=nAA+nAa+naa
pA=(2*nAA+nAa)/(2*n)
pa=1-pA
obs= c(nAA,nAa,naa)
exp=c(n*pA^2,2*n*pA*pa,n*pa^2)
val=sum(((obs-exp)^2)/exp)
1-pchisq(val,df=1)
@nievergeltlab
nievergeltlab / join.sh
Last active June 29, 2021 21:35
Join two files using the join command. Adjust for localization issues that cause improper sorting.
LC_ALL=C sort -k1b,1 file1.txt > file1.txt.sorted
LC_ALL=C sort -k1b,1 file2.txt > file2.txt.sorted
LC_ALL=C join file1.txt.sorted file2.txt.sorted > joined.txt
@nievergeltlab
nievergeltlab / factor_combos.r
Last active June 29, 2021 21:23
Generate a matrix that gives all combinations of levels across factors
expand.grid(study= c("pgc","hou"), imputation = c("unimputed","imputed"), subjs = c("pgbd","pgbd_va"), pheno = c("enter_M_v2","nonresponder","eventnonresponder"))