##Before you start, you could copy all your GWAS_clean4 plink files from yesterday's exercises into your current working directory, or start with the GWAS_clean4 files in your current folder

# MDS
plink --noweb --file GWAS_clean4 --genome --mds-plot 10

# R code
mydata = read.table("mds_components.txt", header=T)
mydata$pch[mydata$Group==1] <- 15
mydata$pch[mydata$Group==2] <- 16
mydata$pch[mydata$Group==3] <- 2
jpeg("mds.jpeg", height=1000, width=1000)
plot(mydata$C1, mydata$C2, pch=mydata$pch)
dev.off()

# Genomic inflation factor 
plink --noweb --file GWAS_clean4 --pheno pheno.txt --pheno-name Aff --logistic --adjust --out unadj
plink --noweb --file GWAS_clean4 --pheno pheno.txt --pheno-name Aff --covar plink.mds --covar-name C1 --logistic --adjust --out C1
plink --noweb --file GWAS_clean4 --pheno pheno.txt --pheno-name Aff --covar plink.mds --covar-name C1-C2 --logistic --adjust --out C1-C2
plink --noweb --file GWAS_clean4 --pheno pheno.txt --pheno-name Aff --covar plink.mds --covar-name C1-C3 --logistic --adjust --out C1-C3
plink --noweb --file GWAS_clean4 --pheno pheno.txt --pheno-name Aff --covar plink.mds --covar-name C1-C4 --logistic --adjust --out C1-C4
plink --noweb --file GWAS_clean4 --pheno pheno.txt --pheno-name Aff --covar plink.mds --covar-name C1-C5 --logistic --adjust --out C1-C5
plink --noweb --file GWAS_clean4 --pheno pheno.txt --pheno-name Aff --covar plink.mds --covar-name C1-C6 --logistic --adjust --out C1-C6
plink --noweb --file GWAS_clean4 --pheno pheno.txt --pheno-name Aff --covar plink.mds --covar-name C1-C7 --logistic --adjust --out C1-C7
plink --noweb --file GWAS_clean4 --pheno pheno.txt --pheno-name Aff --covar plink.mds --covar-name C1-C8 --logistic --adjust --out C1-C8
plink --noweb --file GWAS_clean4 --pheno pheno.txt --pheno-name Aff --covar plink.mds --covar-name C1-C9 --logistic --adjust --out C1-C9
plink --noweb --file GWAS_clean4 --pheno pheno.txt --pheno-name Aff --covar plink.mds --covar-name C1-C10 --logistic --adjust --out C1-C10

# R code QQ plot
broadqq <- function(pvals, title)
{
	observed <- sort(pvals)
	lobs <- -(log10(observed))
	
	expected <- c(1:length(observed))
	lexp <- -(log10(expected / (length(expected)+1)))

    	plot(c(0,7), c(0,7), col="red", lwd=3, type="l", xlab="Expected(-logP)", ylab="Observed(-logP)", xlim=c(0, max(lobs)), ylim=c(0, max(lobs)), las=1, xaxs="i", yaxs="i", bty="l", main = title)
    	points(lexp, lobs, pch=23, cex=.4, bg="black")}
    
jpeg("qqplot_compare.jpeg", height=1000, width=1000)
par(mfrow=c(2,1))
aff_unadj<-read.table("unadj.assoc.logistic", header=TRUE)
aff_unadj.add.p<-aff_unadj[aff_unadj$TEST==c("ADD"),]$P
broadqq(aff_unadj.add.p, "Some Trait Unadjusted")
aff_C1C2 <- read.table("C1-C2.assoc.logistic", header=TRUE)
aff_C1C2.add.p <- aff_C1C2[aff_C1C2$TEST==c("ADD"), ]$P
broadqq(aff_C1C2.add.p, "Some Trait Adjusted")
dev.off()

gws_unadj = aff_unadj[which(aff_unadj$P < 0.0000001), ]
gws_unadj
gws_adjusted = aff_C1C2[which(aff_C1C2$P < 0.0000001), ]
gws_adjusted

q()



