-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtargeted-decomp-metaphlan.R
214 lines (175 loc) · 11.5 KB
/
targeted-decomp-metaphlan.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
212
213
library(icesTAF)
library("metaMA")
library(lme4)
library(ggplot2)
library(pbnm)
for (fiber_subset in c("Arabinoxylan", "LCInulin", "Mix")) { #
print(fiber_subset)
load(paste("/home/slancast/Full_Log_",fiber_subset,"_metaphlan_df.RData",sep=""))
log_metaphlan <- as.matrix(t(log_metaphlan))
class(log_metaphlan) <- "numeric"
log_metaphlan <- log_metaphlan[rowVars(log_metaphlan) > 0,]
log_metaphlan <- data.frame(cbind(t(metaphlan_metadata),t(log_metaphlan)))
#This is only needed when combining terms in the mixed model
#Because of the complication of our project throwing out dosages
#Allows for fewer combinations of patient x dosage
#log_metaphlan <- log_metaphlan[!log_metaphlan$Dose %in% "WashoutFinal",]
#Only needed when directory is not present
#There are two ways of determining which metadata to use
#1 is to do it based on a priori medical knowledge of the event
#My guess is this would include things like sequencing plate, kidney damage, etc.
#The second would be to run over the metadata, and see which ones are
#Associated with the most analytes, and then combine those
#Perhaps a combination of these two approaches
mkdir(paste("~/metaphlan",fiber_subset,sep=""))
total_variance <- c()
eGFR <- gsub("[^0-9\\.]", "", log_metaphlan$eGFR) #removing any non-numeric characters from the data.
eGFR <- as.numeric(as.matrix(eGFR))
eGFR <- eGFR
eGFR_model <- round(eGFR, digits=0)
Hematocrit <- gsub("[^0-9\\.]", "", log_metaphlan$Hematocrit) #removing any non-numeric characters from the data.
Hematocrit <- as.numeric(as.matrix(Hematocrit))
Hematocrit <- Hematocrit * 10
Hematocrit_model <- round(Hematocrit, digits=0)
High.Sensitivity.CRP <- gsub("[^0-9\\.]", "", log_metaphlan$High.Sensitivity.CRP) #removing any non-numeric characters from the data.
High.Sensitivity.CRP <- as.numeric(as.matrix(High.Sensitivity.CRP))
High.Sensitivity.CRP <- High.Sensitivity.CRP * 10
High.Sensitivity.CRP_model <- round(High.Sensitivity.CRP, digits=0)
LDL.HDL.Ratio <- gsub("[^0-9\\.]", "", log_metaphlan$LDL.HDL.Ratio) #removing any non-numeric characters from the data.
LDL.HDL.Ratio <- as.numeric(as.matrix(LDL.HDL.Ratio))
LDL.HDL.Ratio <- LDL.HDL.Ratio * 10
LDL.HDL.Ratio_model <- round(LDL.HDL.Ratio, digits=0)
Hemoglobin.A1c <- gsub("[^0-9\\.]", "", log_metaphlan$Hemoglobin.A1c) #removing any non-numeric characters from the data.
Hemoglobin.A1c <- as.numeric(as.matrix(Hemoglobin.A1c))
Hemoglobin.A1c <- Hemoglobin.A1c * 10
Hemoglobin.A1c_model <- round(Hemoglobin.A1c, digits=0)
shannon_diversity <- gsub("[^0-9\\.]", "", log_metaphlan$shannon_diversity) #removing any non-numeric characters from the data.
shannon_diversity <- as.numeric(as.matrix(shannon_diversity))
shannon_diversity <- shannon_diversity * 10
shannon_diversity_model <- round(shannon_diversity, digits=0)
Triglyceride..Ser.Plas <- gsub("[^0-9\\.]", "", log_metaphlan$Triglyceride..Ser.Plas) #removing any non-numeric characters from the data.
Triglyceride..Ser.Plas <- as.numeric(as.matrix(Triglyceride..Ser.Plas))
Triglyceride..Ser.Plas <- Triglyceride..Ser.Plas * 10
Triglyceride..Ser.Plas_model <- round(Triglyceride..Ser.Plas, digits=0)
df <- data.frame(Participant=double(),
Dose = double(),
Metagenomic_seq_plate = double(),
Other = double())
total_stats <- data.frame(intercept=double(),
eGFR=double(),
Hematocrit=double(),
High.Sensitivity.CRP=double(),
LDL.HDL.Ratio=double(),
shannon_diversity=double(),
Hemoglobin.A1c=double(),
Triglyceride..Ser.Plas=double(),
total_var=double(),
analyte=double(),
dose_p=double(),
dose_p_unused=double(),
intercept_coef=double(),
eGFR_coef=double(),
Hematocrit_coef=double(),
High.Sensitivity.CRP_coef=double(),
LDL.HDL.Ratio_coef=double(),
shannon_diversity_coef=double(),
Hemoglobin.A1c_coef=double(),
Triglyceride..Ser.Plas_coef=double(),
stringsAsFactors=FALSE)
column_names <- colnames(total_stats)
stats <- c()
to_bind <- c()
counter <- 0
for (i in (nrow(metaphlan_metadata)+2):ncol(log_metaphlan)) {
#print(shapiro.test(as.numeric(log_metaphlan[,i])))
print(colnames(log_metaphlan)[i])
response <- as.numeric(as.matrix(log_metaphlan[,i]))
response <- response * 1000000
response <- round(response, digits=0)
if (sum(response) <= 30) next
return_list <- tryCatch({
model_nofiber <- lmer(response ~ 1 + (1|Participant) + (1|Metagenomic_seq_plate) + eGFR_model + Hematocrit_model + High.Sensitivity.CRP_model + LDL.HDL.Ratio_model + shannon_diversity_model + Hemoglobin.A1c_model + Triglyceride..Ser.Plas_model, data=log_metaphlan) #, family=poisson(link=identity) Poisson seems to work best at least for metagenome as the response variable
model <- lmer(response ~ 1 + (1|Dose) + (1|Participant) + (1|Metagenomic_seq_plate) + eGFR_model + Hematocrit_model + High.Sensitivity.CRP_model + LDL.HDL.Ratio_model + shannon_diversity_model + Hemoglobin.A1c_model + Triglyceride..Ser.Plas_model, data=log_metaphlan) #, family=poisson
pbmmG2 <- pbnm(model,model_nofiber,nsim=100,tasks=10,cores=1,seed=1) #Using pbnm to assign a p-value to the ffect of dose
#Calculating varous statistics for the data. Statistics are labeled after the command
variance <- as.data.frame(VarCorr(model)) ##Determining the variance composition of the random effect variables
to_bind <- variance$vcov/sum(variance$vcov)*100 #Making the variance as a proportion of total.
Vcov <- vcov(model, useScale = FALSE) #Covariance of the fixed effect variables
betas <- fixef(model) #coefficients of the fixed effect variables
se <- sqrt(diag(Vcov)) ####Beginning finding pvalue
zval <- betas / se ###Continuing finding p-value
pval <- 2 * pnorm(abs(zval), lower.tail = FALSE) #Finding p-value
#Adding pbnm pvalue to the stats, assuing it exists. If not, adding the confidence interval.
if (is.na(summary(pbmmG2)$`P(>=obs)`)) {
stats <- c(pval,sum(variance$vcov),colnames(log_metaphlan[i]),summary(pbmmG2)$`P(>=obs)`,summary(pbmmG2)$`#(>=obs)+noEst/runs`)
} else {stats <- c(pval,sum(variance$vcov),colnames(log_metaphlan[i]),summary(pbmmG2)$`P(>=obs)`,"NA")
}
stats <- c(stats, coef(summary(model))[,1])
#For matting thes "stats" object for the individual analyte
stats <- data.frame(t(stats))
colnames(stats) <- column_names
stats <- data.frame(stats)
}, #End of the trycatch first clause
error = function(err){print(err)
#Making sure that if there's an error the program will keep running
#Not sure why but this allows the program to keep running. If it ain't broke don't fix it.
to_bind = c()
stats = c()
}) #End of the total trycatch
#Appending the stats to the data frames,
to_bind <- c(to_bind)
df <- rbind(df,to_bind)
counter = counter + 1
print(counter)
total_stats <- rbind(total_stats,stats)
} #end data loop
#Adding p-adjusted column, and polishing final data frame for output
total_stats <- data.frame(total_stats)
padjusted <- p.adjust(as.numeric(as.matrix(total_stats$eGFR)), method = "BH", n = nrow(total_stats))
total_stats$eGFR_adj <- padjusted
padjusted <- p.adjust(as.numeric(as.matrix(total_stats$Hematocrit)), method = "BH", n = nrow(total_stats))
total_stats$Hematocrit_adj <- padjusted
padjusted <- p.adjust(as.numeric(as.matrix(total_stats$High.Sensitivity.CRP)), method = "BH", n = nrow(total_stats))
total_stats$HHigh.Sensitivity.CRP_adj <- padjusted
padjusted <- p.adjust(as.numeric(as.matrix(total_stats$LDL.HDL.Ratio)), method = "BH", n = nrow(total_stats))
total_stats$LDL.HDL.Ratio_adj <- padjusted
padjusted <- p.adjust(as.numeric(as.matrix(total_stats$Hemoglobin.A1c)), method = "BH", n = nrow(total_stats))
total_stats$Hemoglobin.A1c_adj <- padjusted
padjusted <- p.adjust(as.numeric(as.matrix(total_stats$Triglyceride..Ser.Plas)), method = "BH", n = nrow(total_stats))
total_stats$Triglyceride..Ser.Plas_adj <- padjusted
rownames(total_stats) <- make.names(total_stats$analyte, unique=TRUE) #with the random effects interaction term
df <- data.frame(df)
rownames(df) <- make.names(total_stats$analyte, unique=TRUE)
total_stats <- subset(total_stats, select=-c(analyte))
df <- df[,-5] #This is used because I've appended the names to the dataframe
write.table(df, file = paste("~/metaphlan",fiber_subset,"/numericCovar",fiber_subset,"WeekPart.txt",sep=""), sep="\t")
write.table(total_stats, file = paste("~/metaphlan",fiber_subset,"/numericStats",fiber_subset,"WeekPart.txt",sep=""), sep="\t")
#df <- read.table("/Users/SLancaster/Desktop/Ternary/Data/metaphlanVarianceMixWeekPartHematoHOMAIRTRI.txt", row.names=NULL)
#df <- df[,-1] #Only to be used when reading a df from a table
#df <- as.matrix(df) #Only to be used when reading a df from a table
pdf(paste("~/metaphlan",fiber_subset,"/numericHist",fiber_subset,"eGFRWeekPart.pdf",sep=""))
hist(as.numeric(as.matrix(total_stats$eGFR), main="eGFR"), breaks=40)
dev.off()
pdf(paste("~/metaphlan",fiber_subset,"/numericHist",fiber_subset,"HematocritWeekPart.pdf",sep=""))
hist(as.numeric(as.matrix(total_stats$Hematocrit), main="Hematocrit", xlab="p-value"), breaks=40)
dev.off()
pdf(paste("~/metaphlan",fiber_subset,"/numericHist",fiber_subset,"High.Sensitivity.CRP_modelWeekPart.pdf",sep=""))
hist(as.numeric(as.matrix(total_stats$High.Sensitivity.CRP), main="CRP", xlab="p-value"), breaks=40)
dev.off()
pdf(paste("~/metaphlan",fiber_subset,"/numericHist",fiber_subset,"LDL.HDL.Ratio_model.pdf",sep=""))
hist(as.numeric(as.matrix(total_stats$LDL.HDL.Ratio), main="LDL", xlab="p-value"), breaks=40)
dev.off()
pdf(paste("~/metaphlan",fiber_subset,"/numericHist",fiber_subset,"Hemoglobin.A1c_modelWeekPart.pdf",sep=""))
hist(as.numeric(as.matrix(total_stats$Hemoglobin.A1c), main="A1c", xlab="p-value"), breaks=40)
dev.off()
pdf(paste("~/metaphlan",fiber_subset,"/numericHist",fiber_subset,"shannon_diversity_modelWeekPart.pdf",sep=""))
hist(as.numeric(as.matrix(total_stats$shannon_diversity), main="A1c", xlab="p-value"), breaks=40)
dev.off()
pdf(paste("~/metaphlan",fiber_subset,"/numericHist",fiber_subset,"Triglyceride..Ser.Plas_modelWeekPart.pdf",sep=""))
hist(as.numeric(as.matrix(total_stats$Triglyceride..Ser.Plas), main="Triglyceride", xlab="p-value"), breaks=40)
dev.off()
pdf(paste("~/metaphlan",fiber_subset,"/numericHist",fiber_subset,"dose-p_WeekPart.pdf",sep=""))
hist(as.numeric(as.matrix(total_stats$dose_p), main="dose_p", xlab="p-value"), breaks=40)
dev.off()
alarm()
} #Ending the fiber_subset loop