-
Notifications
You must be signed in to change notification settings - Fork 0
/
GWAS_Program.R
211 lines (161 loc) · 5.56 KB
/
GWAS_Program.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
rm(list=ls())
set.seed(1234567)
setwd("H:/Final Thesis/Gnome Analysis/R Codes")
source("Functions4GWAS.R")
##########################
## READ DATA ####
##########################
# Read the new files
pop <- read.csv("POP_DNA.csv", sep = ",")
pt <- read.csv("PT.csv", sep = ",")
gene.location <- read.csv("SiteFile.csv", sep = ",")
org_zero <- read.csv("KO_file_0.csv", sep = ",")
org_one <- read.csv("KO_file_1.csv", sep = ",")
gen <- read.csv("GEN.csv", sep = ",")
#lod <- read.csv("LOD.csv", sep = ",")
# Define the size of the data
N = 999 #Number of individuals
genome.size = 160000 #Length of the genome
##########################################
# Create a genome matrix G with letters
#Gtext <- matrix(0, N, genome.size)
#for (i in 1:N)
#{
# Gtext[i,] <- unlist(strsplit(substr(pop$genome[i], 1,genome.size), ""))
#}
#pop$genome[1]
#save(Gtext, file = "Gtext.RData")
load("Gtext.RData")
##########################
## CREATE SNP DATA ####
##########################
#SNP_data <- G_to_binary(Gtext) #Recode the matrix Gtext to binary
#save(SNP_data, file = "SNP_data.RData")
load("SNP_data.RData")
###################################################################
## LOOK AT HOW SIMILAR THE GENOMES ARE FOR DIFFERENT INDIVIDUALS####
######################################################################
test1 <- colMeans(SNP_data)<0.95 #MAF > 5 percent
subset1 <- seq(1, sum(test1), length.out=5000) #Enough with 5000 markers to compute relatedness
GR <- Relatedness(SNP_data[,test1][,subset1], PLOT=TRUE)
#library(RColorBrewer)
#heatmap(GR)
##########################
## GET THE PHENOTYPES ####
##########################
Phenotype <- get.phenotype(pt, trait="scoreIndividual")
boxplot(Phenotype, main="Boxplot for scoreIndividual Phenotype")
hist(Phenotype)
# ***** Check Relationship of features (with Mean) *****
phenotype_pairs <- Phenotype_Mean()
pairs(phenotype_pairs)
# ***** Check Relationship of features (with Variance) *****
phenotype_pairs_var <- Phenotype_Var()
pairs(phenotype_pairs_var)
#####################################
## COMPUTE THE HERITABILITY ########
#####################################
h2 <- compute.heritability(Phenotype, GR)
cat("Estimated heritability:", h2, "\n")
#########################################################################################
## HOW WELL CAN WE PREDICT INDIVIDUAL PHENOTYPES BASED ON THE GENETIC DATA ONLY ########
## This model assumes all SNP have a small and normally distributed effect on the trait
## and that these small effects are additive.
#########################################################################################
CP <- cross.predict(Phenotype, GR)
par(mfrow=c(1,1))
plot(y=CP$predicted, x=CP$unobserved.phenotypes,
main="Predicting Phenotypes",
xlab="Observed", ylab="Predicted",
ylim=range(c(CP$predicted,CP$unobserved.phenotypes))
)
abline(0,1, col="grey")
cat("Correlation between predicted and observed", cor(CP$predicted, CP$unobserved.phenotypes), "\n")
# Creating a Genome matrix G
G <- matrix(0, N, genome.size)
for (i in 1:N)
{
G[i,] <- unlist(strsplit(substr(pop$genome[i], 1,genome.size), ""))
}
#A function to change letters to 0 or 1
G_to_binary <- function(G) {
M <- matrix(0, nrow(G), ncol(G))
for (j in 1:ncol(G)) {
most.common <- names(which.max(table(G[,j])))
M[G[,j]==most.common,j] = 1
}
return(M)
}
summary(h2)
##########################
## ANALYSIS OF DATA ####
##########################
#summary(Phenotype)
hist(Phenotype)
SNP_data <- G_to_binary(G) #Recode the matrix G to be able to compute correlations
#View(SNP_data)
GWAS_results <- numeric(genome.size)
for (j in 1:genome.size) {
if (sum(SNP_data[,j]) < (N-5)) {
mod1 <- lm(Phenotype~SNP_data[,j])
P_value <- summary(mod1)$coef[2,4]
} else {
P_value=1
}
GWAS_results[j] <- -log10(P_value)
}
table(G[,which.max(GWAS_results)])
hist(GWAS_results)
plot(GWAS_results, xlab="Nucleotide position", ylab="-log10(P-value)")
median(GWAS_results)
which.max(GWAS_results)
#GWAS_results[28418]
"genomic inflation factor"
# genome-wide association study
indx = (which.max(GWAS_results) - 100): (which.max(GWAS_results) +100)
subset_SNPdata= SNP_data[,indx]
Sub_SNPdata <- GWAS_results[]
#abline(h = 7.3)
# ********** New Analysis ***********
SNP_data = subset_SNPdata
MAF <- 1-colSums(SNP_data)/nrow(SNP_data) #Compute minor allele frequency
plot(MAF)
hist(MAF)
test <- MAF>0
print(sum(test)) #How many positions have varying nucleotide information?
SNP_data <- SNP_data[,test]
M <- matrix(NA, ncol(SNP_data), ncol(SNP_data))
#mm = matrix(NA, ncol(SNP_data)^2, 3)
k=0
for ( i in 1:(ncol(SNP_data)-1) ) {
for (j in (i+1):ncol(SNP_data)) {
k=k+1
M[i,j] <- cor(SNP_data[,i], SNP_data[,j])
}
}
M1 <- M
M1[is.na(M)]=0
M2<- M1+t(M1)
diag(M2)=1
n <- ncol(M2)
step = 100
indx = step
kk <- ceiling(sqrt(n/step))
diag(M2)=NA
library(RColorBrewer)
pdf()
while(indx < n) {
heatmap(M2[(indx-step+1):indx,(indx-step+1):indx], Colv=NA, Rowv=NA)
legend(x="bottomright", legend=c("min", "ave", "max"),
fill=colorRampPalette(brewer.pal(8, "Oranges"))(3))
indx = indx +step
}
heatmap(M2[(indx-step+1):n,(indx-step+1):n], Colv=NA, Rowv=NA)
legend(x="bottomright", legend=c("min", "ave", "max"),
fill=colorRampPalette(brewer.pal(8, "Oranges"))(3))
dev.off()
# *********************
lm(Phenotype~SNP_data[,1])
mod1 = lm(Phenotype~SNP_data[,1])
summary(mod1)
cor.test(Phenotype, SNP_data[,1])