Skip to content

Instantly share code, notes, and snippets.

@nievergeltlab
nievergeltlab / infomaf.sh
Last active August 15, 2018 17:00
Extract relevant columns from info .dosage files that I made for PGC
fo
for study in brya ftca gsdc mrsc nhrv nhs2 nss1 nss2 onga pts1 psy2 psy3
do
#cat "$study"/infoeur/*.dosage | awk '{if(NR == 1 || ($1 != "CHR" && $6 > 0 && $6 < 1)) print $2,$1,$3,$4,$5,$6,$7}' > "$study".mafinfo
cat "$study"/infoeur/*.dosage | awk '{if(NR == 1 || ($1 != "CHR" && $6 > 0 && $6 < 1)) print $2,$4,$5,$6,$7}' | LC_ALL=C sort -k1b,1 > "$study".mafinfo
done
#Note: Assumes that the SNP is the first column of your gwas results file. If it is not, change "sort k1b,1"
to the column number where the SNP is. e.g. if SNP is in column 2, change to sort k2b,2 gwas_results.assoc.txt
@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 / mice.r
Created February 7, 2018 22:06
examples of how to do MICE
library(mice)
#Continuous and categorical variables should be defined prior to MI. This will prevent nonsensical imputations from happening - e.g. cohorts are 1,2,3,4, you impute, then suddenly someone is in cohort 1.5
#Note feb 5: Put other surrogate mobility variables in noting imputation
#I prefer to make lists of all continuous and categorical variables going in
contvars <- scan(what="character",sep="/n")
AGE
@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"))
@nievergeltlab
nievergeltlab / kill_jobs.sh
Last active September 29, 2017 16:23
When a bunch of the same process are running, kill it!
for commandz in $(ps aux | grep R | awk '{print $2}'); do kill $commandz;done
@nievergeltlab
nievergeltlab / co
Last active September 18, 2017 21:07
Say you generate 2 sets of p-values using 2 forms of analysis. This plots them against eachother and calculates r.
zcat daner_pts_mrsc_mix_am-qc.hg19.ch.fl.gz | awk '{print $2,$11}' | grep -v NA > daner_pts_mrsc_mix_am-qc_short2
awk '{print $2,$12}' mrsc_gemma_pcs > mrsc_gemma_pcs_short2
gzip -d mrsc_gemma_pcs_short2.gz
gzip -d daner_pts_mrsc_mix_am-qc_short2.gz
LC_ALL=C join <(awk '{if (NR==1)$1="SNP", $2="P"; else print}' mrsc_gemma_pcs_short2 | LC_ALL=C sort -k1b,1 ) <(LC_ALL=C sort -k1b,1 daner_pts_mrsc_mix_am-qc_short2) > files_joined
sort -g -k2 files_joined > files_joined2
grep -v P files_joined2 > files_joined3
@nievergeltlab
nievergeltlab / get_gsid.sh
Created August 30, 2017 17:56
Get sample ID from genome studio final report, for many files
for files in $(ls *.txt)
do
samplename=$(awk 'NR==12{print $1}' $files)
echo $files $samplename >> sample_ids.txt
samplename=""
done
@nievergeltlab
nievergeltlab / bg_fix.pbs
Created August 29, 2017 15:17
Dosage to best guess, handler for ricopili script, to correct best guess to include low freq variants
#PBS -lnodes=1
#PBS -lwalltime=02:05:00
#!/bin/bash
#Make walltime a function of N?
while getopts i:f:o:p:d:n: option
do
case "${option}"
@nievergeltlab
nievergeltlab / ricopili_pheno.r
Created August 28, 2017 18:46
Make ricopili phenotypes. Investigators give their own subject IDs, which usually need to be linked to the ricopili IDs by modifying the ricopili assigned names.
setwd('C:/Users/adam/Desktop/starrs_cdrisc')
dat <- read.csv('CD_RISC_v2.csv',header=T,stringsAsFactors=F,na.strings=c("NA","#N/A"))
dat <- subset(dat,visit <= 3)
library(plyr)
cdr_max <- ddply(dat, ~studyid,colwise(.fun=max,'CDR_SUM',na.rm=T))
cdr_max <- cdr_max[-which(cdr_max$CDR_SUM == -Inf),]
@nievergeltlab
nievergeltlab / ManhattanPlotterFunction_colorfixed_max10ylim2_pgc_v2.R
Last active April 12, 2021 21:17
Alternative example of Manhattan plot. Assumes set column names from PLINK. The .sh file contains code for user to run the R script file. Improvement: Uses fread to read data quickly. Improvement: set boundary around p-value to color red, set p-val
#############################Begin Function#############################
#First we define the function with required parameters and optional parameters with their defaults
ManhattanPlot_AJS_cut <- function(chr, pos, pvalue, snp, genomesig = 0, genomesug = 0,photoname = "Manhattan Plot", outname = "ManhattanPlot_AJS", colors = c("navyblue","gray69"), sigcolor = "darkred", sugcolor = "indianred", ncex = .5, plotsymbol = 16, sugcex = 1, sigcex = 1.5, sigsnpcolor = "red", nonautosome = c(23,24,25,26),xlabel = "Chromosomal Positions",ylabel = "-log10(p-value)", pdf = "TRUE",topsnplabels = "FALSE", pvalue_miss = "NA",highlight_p=5e-8,highlightboundary=50000) {
#bind the input data into one table and prune out missing data based on the pvalue_miss option and then
#redefines the input variables chr, pos, pvalue, snp to reflect the cleaned data