-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmultiplot1_DBDnEC.R
231 lines (185 loc) · 7.88 KB
/
multiplot1_DBDnEC.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
##############################################################################################################
# Plots for Naima's paper
# Baseline dataset with NEGATIVE slope
# run on cluster
##############################################################################################################
#### cluster
#### load Naima's data
#----------------------
setwd("C:/Users/carme/Dropbox/Work_Files/shapiro_lab/Naima_project/cluster") #laptop
#setwd("/home/cmurall/naimaproject") #cluster
#EMP_data <-read.delim("EMP_table.from_biom_w_taxonomy.txt", header= TRUE) #takes ~3.6 mins
load(file="emp2.RData")
#or run Naima's code (see "randomGPO_data.R" file for this)
#### libraries
#-------------
library(tictoc)
library(gridExtra)
library(ggplot2)
library(lattice)
library(grid)
library(plyr)
library(dplyr)
library(magrittr)
library(gamlss)
#### used across code
#--------------------
nRows <-22014 # no. of ASVs
nCols <-2000 # no. of samples
num<-nRows*nCols #total no. of elements
p <- c(1, 5504, 11007, 16510, 22014) #c(1, 5504, 11007, 16510, 22014) #(0,25%,50%,75%, 100%) elements are tested
no<- length(p) #number of plots in a row
pl <-list() #for plots
lambda <-0.01 #lambda
perlist<-c(0, 25, 50, 75, 100) #for labelling top row of images and images
#### 1. add DBD ----------------------------------------------------------------------------------------------
# create Poisson distributed data per element, only one lambda for whole dataset:
set.seed(3579)
simMat<-matrix(NA,nRows,nCols) #mat for filling
for (i in 1: num) {
simMat[i]<-rpois(1, lambda) # closer lambda is to zero the more zero inflated the distribution is
}
write.csv(simMat, file="simMat1_DBD.csv")
# loop to add DBD
for (i in 1:no){
#ensure still using same base dataset:
simMat <-read.csv(file="simMat1_DBD.csv", header = TRUE) #or fromGPO
simMat<-simMat[,-1] #drop first column because it's not data
#make simData a P/A matrix
simData<-data.frame(simMat) #make into dataframe
fun<-function(x) replace(x, x>0, 1) #function, replace nonzero elements into 1
paData <-simData %>% mutate_all(fun) #make data presence/absence
#for filling mulitple elements:
for (k in 1:nCols){
for(j in 1:p[i]){ #check p elements per column
ind<-which(paData[,k]==0, arr.ind = TRUE) #indices of zero elements in column
if (any(ind!=0)) rind<-sample(ind,1) else next #randomly select a zero element to replace
el<-sample(paData[,k],1) #choose an element in a column at random
if (el>0) paData[,k][rind]<-1 #if el is nonzero, fill rind with 1
}}
#paData is now transformed
#save dataset
percent<-perlist[[i]] #percent label
name<-paste0("paData1_DBD", percent,".csv") #create label
write.csv(paData, file=name) #save dataset
#get number of ASVs per sample (by column)
vecSum <-c()
for(l in 1:nCols){
x<-sum(paData[,l])
vecSum<-append(vecSum,x)
}
# new dataframe for results
df <-data.frame()
df<-cbind(vecSum)
colnames(df)<-c("no.species")
#get number of genera per sample
x1<-c()
for(m in 1:nCols){
ind <-which(paData[,m]>0, arr.ind = TRUE) #indices for nonzero elements
vecGen <-emp2$genus[ind] #genera for those indices
count <-nrow(plyr::count(vecGen))#freq of genera in vecGen, count no. rows which gives no. of genera in vecGen
x1<-append(x1,count)#store
}
x1 <-as.data.frame(x1)
#fill results df
df <-as.data.frame(df)
df <- bind_cols(df, x1) #bind
colnames(df)<-c("no.species","no.gen")
ratio<- df$no.species/df$no.gen
df$ratio <-ratio
head(df)
#plot ASV:#genera vs. # of genera
pl[[i]]<-ggplot(df, aes(x = no.gen, y=ratio))+geom_point()+
#xlim(0,12)+#ylim(-5,50)+
geom_smooth(method = "lm") +
theme(axis.title.x=element_blank(),axis.title.y=element_blank(), plot.title = element_text(hjust = 0.5, face = "plain")) +
ggtitle(as.character(percent))
}
#### 2. add EC ----------------------------------------------------------------------------------------------
# create Poisson distributed data per element, only one lambda for whole dataset:
set.seed(3579) #diff seed than for DBD
simMat<-matrix(NA,nRows,nCols) #mat for filling
rm(i)
for (i in 1: num) {
simMat[i]<-rpois(1, lambda) # closer lambda is to zero the more zero inflated the distribution is
}
write.csv(simMat, file="simMat1_EC.csv")
# loop to add EC
rm(i)
for (i in 1:no){
#ensure still using same base dataset:
simMat <-read.csv(file="simMat1_EC.csv", header = TRUE) #or fromGPO
simMat<-simMat[,-1] #drop first column because it's not data
#make simData a P/A matrix
simData<-data.frame(simMat) #make into dataframe
fun<-function(x) replace(x, x>0, 1) #function, replace nonzero elements into 1
paData <-simData %>% mutate_all(fun) #make data presence/absence
#for removing mulitple elements:
for (k in 1:nCols){
for(j in 1:p[i]){ #check p elements per column
ind<-which(paData[,k]!=0, arr.ind = TRUE) #indices of nonzero elements in column
if (any(ind!=0)) rind<-sample(ind,1) else next #randomly select a nonzero element to replace #if (any(ind!=0)) makes sure that ind isn't empty
el<-sample(paData[,k],1) #choose an element in the column at random
if (el>0) paData[,k][rind]<-0 else next #if el is nonzero, replace element at position rind with 0
}}
#paData is now transformed
#save dataset
percent<-perlist[[i]] #percent label
name<-paste0("paData1_EC", percent,".csv") #create label
write.csv(paData, file=name) #save dataset
#get number of ASVs per sample (by column)
vecSum <-c()
for(l in 1:nCols){
x<-sum(paData[,l])
vecSum<-append(vecSum,x)
}
# new dataframe for results
df <-data.frame()
df<-cbind(vecSum)
colnames(df)<-c("no.species")
#get number of genera per sample
x1<-c()
for(m in 1:nCols){
ind <-which(paData[,m]>0, arr.ind = TRUE) #indices for nonzero elements
vecGen <-emp2$genus[ind] #genera for those indices
count <-nrow(plyr::count(vecGen))#freq of genera in vecGen, count no. rows which gives no. of genera in vecGen
x1<-append(x1,count)#store
}
x1 <-as.data.frame(x1)
#fill results df
df <-as.data.frame(df)
df <- bind_cols(df, x1) #bind
colnames(df)<-c("no.species","no.gen")
ratio<- df$no.species/df$no.gen
df$ratio <-ratio
head(df)
#plot ASV:#genera vs. # of genera
pl[[(i+no)]]<-ggplot(df, aes(x = no.gen, y=ratio))+geom_point()+
#xlim(0,12)+#ylim(-5,50)+
geom_smooth(method = "lm") + theme(axis.title.x=element_blank(),axis.title.y=element_blank()) #+ ggtitle("rpois, 75% ")
}
#### 3. final plot ------------------------------------------------------------------------------------------
# plot all runs in one plot
fullpl<-grid.arrange(grobs=pl, nrow=2, top="Poisson dataset, lambda = 0.01",
left = textGrob("ASV:Genus", rot = 90, vjust = 0.5),
bottom = textGrob("number of genera"))#,
# missing label of rows, DBD top, EC bottom
#save plot
ggsave(fullpl,filename="multiplot1.pdf")#, width = 14.0, height = 5.0, units = "cm")
ggsave(fullpl,filename="multiplot1png.png")
#save data of plot (so it can be reformated)
save(pl, file="multiplot1_data.RData")
#load using: load("multiplot2_data.RData")
#then, change like this:
# pl + ggtitle("better title")
# #reconfiguring the multi-plot figure:
# pl[[1]]<-pl[[1]]+ylim(1,2.5)+xlim(0,320)
# pl[[2]]<-pl[[2]]+ylim(1,2.5)+xlim(0,320)
# pl[[3]]<-pl[[3]]+ylim(1,2.5)+xlim(0,320)
# pl[[4]]<-pl[[4]]+ylim(1,2.5)+xlim(0,320)
# pl[[5]]<-pl[[5]]+ylim(1,2.5)+xlim(0,320)
# pl[[6]]<-pl[[6]]+ylim(1,2.)+xlim(0,320)
# pl[[7]]<-pl[[7]]+ylim(1,2.)+xlim(0,320)
# pl[[8]]<-pl[[8]]+ylim(1,2.)+xlim(0,320)
# pl[[9]]<-pl[[9]]+ylim(1,2.)+xlim(0,320)
# pl[[10]]<-pl[[10]]+ylim(1,2.)+xlim(0,320)