-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathE_COLI_Respirometry_standard_markdown_FINAL_DEBUGGED.Rmd
324 lines (252 loc) · 10.3 KB
/
E_COLI_Respirometry_standard_markdown_FINAL_DEBUGGED.Rmd
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
---
title: "ECOLI Standard Respirometry Data Analysis"
author: "Caroline Fleming Ianniello (adapted from S. Speroff with help from Brian Kennedy and Ethan Deyle)"
date: "last updated 12/1/2023"
output: html_document
---
# Some important notes for working with this code:
## We are having a tough time getting around a bug that makes the last column of the output data be 2.12. To work around this, you need to 1) add a dummy column of data to the end of your data (that will just have a 2.12 output), and 2) put the number of vials as the # of experimental vials (NOT including negative control) + dummy column, and 3) throw out the last value of 2.12 on the output csv
##Remember that negative control always has to be A1 vial, and that order goes A1-D1, etc.
## LINES TO ADAPT FOR EACH RUN:
### Line 31: Input data file straight from PreSens software
### Line 109: Number of vials filled (SEE NOTE ABOVE)
### Line 190: CSV output name
## R hates 0s, so we want to replace with something small in the weights column
```{r setup, include=FALSE, echo=FALSE}
#For some reason knitr is wanting me to set the WD this way
# require("knitr")
# opts_knit$set(root.dir = "C:/Users/carol/Documents/PhD Boston University 2019-/Chapter_2_Ecotox/Summer_2022_N_Ecotox/EXP_3/EXP_3B_resp")
```
```{r Set working directory and install packages}
#setwd("C:/Users/carol/Documents/PhD Boston University 2019-/Ecotoxicity/Summer 2022 N Ecotox/Experiment1/RunA_7_26_22")
#setting directory a different way
#opts_knit$set(root.dir="C:/Users/carol/Documents/PhD Boston University 2019-/Ecotoxicity/Summer 2022 N Ecotox/Experiment1/RunA_7_26_22")
#{r setup, include=FALSE, echo=FALSE}
#require("knitr")
#opts_knit$set(root.dir = "C:/Users/carol/Documents/PhD Boston University 2019-/Ecotoxicity/Summer 2022 N Ecotox/Experiment1/RunA_7_26_22")
#getwd()
#installing new respR
#install.packages("respR")
# library necessary packages
#install.packages("knitr")
library("knitr")
library(respR)
library(tidyverse)
#readr is installed in tidyverse
```
```{r EDIT ME - IMPORT DATA!}
# data import
#the following is a command from readr
data <- read_csv("EXP1A_1_07_26_22_NO_e_coli_Oxygen.csv", skip=12, na=c("No Sensor","NA"))
```
```{r Import & clean CSV}
# rename headers
names(data)[names(data) == "Time/Min."] <- "Minutes"
# remove unnecessary columns (keep only data/time, minutes, and data)
data <- data[,1:26] #sometimes you have to change this last number, check it out
# new time elapsed (sec) interval column
for (i in 1:nrow(data)){
x <- nrow(data)
s <- seq(from = 0, by = 15, length.out = x)
data$Time.Elapsed.Sec <- (rep(s))
}
# new time elapsed (min) interval column
for (i in 1:nrow(data)){
data$Time.Elapsed.Min <- data$Time.Elapsed.Sec/60
}
#Rename Date/Time, Date.Time
names(data)[1] <-"Date.Time"
# final data check
#str(data)
#make sure all are numeric
data$A1<-as.numeric(data$A1)
data$A2 <- as.numeric(data$A2)
data$A3 <- as.numeric(data$A3)
data$A4 <- as.numeric(data$A4)
data$A5 <- as.numeric(data$A5)
data$A6 <- as.numeric(data$A6)
data$B1<-as.numeric(data$B1)
data$B2 <- as.numeric(data$B2)
data$B3 <- as.numeric(data$B3)
data$B4 <- as.numeric(data$B4)
data$B5 <- as.numeric(data$B5)
data$B6 <- as.numeric(data$B6)
data$C1<-as.numeric(data$C1)
data$C2 <- as.numeric(data$C2)
data$C3 <- as.numeric(data$C3)
data$C4 <- as.numeric(data$C4)
data$C5 <- as.numeric(data$C5)
data$C6 <- as.numeric(data$C6)
data$D1<-as.numeric(data$D1)
data$D2 <- as.numeric(data$D2)
data$D3 <- as.numeric(data$D3)
data$D4 <- as.numeric(data$D4)
data$D5 <- as.numeric(data$D5)
data$D6 <- as.numeric(data$D6)
# data$E1 <- as.numeric(data$E1) #dummy data, you may want to delete me!!
```
```{r cut off everything past four and a half hours}
data<-subset(data,data$Minutes<=270)
#270 minutes is four and a half hours
```
```{r Scale negative O2 control and re-zero baseline}
# calculate the mean value of our negative control in a new df
#selecting columns with data - column with negative control
#note: na.rm=TRUE tells R to ignore NAs
neg <- data[,3:26] - mean(data$A1, na.rm=TRUE)
#Trying something out where I subtract each data column by the negative control at that moment
#neg <- data[,3:27] - data$A1
#mean(data$A1, na.rm=TRUE)
# create new columns in neg to match the time values in data
library(tibble)
neg <- neg %>%
add_column(Minutes = data$Minutes,
.before = "A1")
neg <- neg %>%
add_column(Date.Time = data$Date.Time,
.before = "Minutes")
neg <- neg %>%
add_column(Time.Elapsed.Sec = data$Time.Elapsed.Sec,
.after = "D6")
neg <- neg %>%
add_column(Time.Elapsed.Min = data$Time.Elapsed.Min,
.after = "Time.Elapsed.Sec")
# change name back to data
data <- neg
```
```{r EDIT ME - TEMPLATE UPLOAD FOR WEIGHTS}
getwd()
resp_info <- read.csv("SA_template_resp_EXP1A_1_07_26_22_NO_e_coli_Oxygen.csv", header=TRUE) # NUMBER ORDER, NOT LETTER ORDER
volume= as.numeric(resp_info$volume)
t= as.numeric(resp_info$temp)
S= as.numeric(resp_info$salinity)
P= as.numeric(resp_info$pressure)
```
```{r num vials filled}
#RUN BASED ON NUMBERS NOT LETTERS! (NEED IT IN ORDER)
#enter number of vials filled in experiment
num_vials_filled <- 24
```
```{r generate background respiration rate, SW and then SW + E. coli}
#background SW rate of SW only
library(respR)
# Make a SW control dataframe using columns 4 &5 #MAKE SURE THIS IS IN MINUTES
SW.data <- na.omit(data[c(28, 4)])
# SW inspect object, with all oxygen values pooled
SW_insp <- inspect(SW.data,
time = 1,
oxygen = 2,
plot = TRUE)
# check assumptions of inspect object
print(SW_insp)
# Calc background rate of sw subset data_speroff
SW.rate <- calc_rate.bg(SW.data,
time = 1,
oxygen = 2)
# print calculated rates & average rate
print(SW.rate)
getwd()
#Now for SW and E. coli together
SW.EC.data <- na.omit(data[c(28, 4)]) #time.elapsed.min and the column with SW + E_coli
#MAY NEED TO CHANGE THE 4 IF E COLI CONTROL IS IN A DIFFERENT COLUMN
#For exp 3 resp run #3, e coli is in 4
#All others it is in 5!!
# Inspect
SW_EC_insp <- inspect(SW.EC.data,
time = 1,
oxygen = c(2),
plot = TRUE)
# check assumptions of inspect object
print(SW_EC_insp)
# Calc background rate of data
SW.EC.rate <- calc_rate.bg(SW.EC.data,
time = 1,
oxygen = c(2))
SW.EC.rate
```
```{r Negative oxygen control}
# Make a -O2 control data_speroffframe using column 3
NEG.O2.data<- na.omit(data[c(28, 3)])
# Inspect
NEG_O2_insp <- inspect(NEG.O2.data,
time = 1,
oxygen = 2,
plot = TRUE)
# Print
print(NEG_O2_insp)
# Calc background rate of sw subset
NEG.O2.rate <- calc_rate.bg(NEG.O2.data,
time = 1,
oxygen = 2)
# print calculated rates & average rate
print(NEG.O2.rate)
```
```{r For loop}
# create empty output vector to fill in in for loop
output_area_specific_O2 <- rep("NA", num_vials_filled)
# selecting columns with data based on number of vials filled
data2 <- data[, c(1:2, 3:(3 + num_vials_filled), 27:28)] #update last columns for a full run
#running for loop for number of vials filled
#IT WAS BREAKING ON #1 SO I CHANGED IT TO 2: num_vials_filled
for (i in 2:num_vials_filled) {
temp.data <- data2[, c(ncol(data2), i+3)]
# temp.data <- data2[c(ncol(data2), i+3)]
# temp.data <- na.omit(temp.data) #get rid of NAs
#checking inspect file for any flags
#I want the time.elapsed.min rather than minutes column to be selected
temp_inspect <- inspect(temp.data,
time = 1, #time column 1
oxygen = 2, #oxygen column 2
plot = FALSE)
print(paste("vial", i, "NA Time Elapsed", temp_inspect$checks[1])) # FALSE is good
print(paste("vial", i, "sequential", temp_inspect$checks[2]))
print(paste("vial", i, "duplicated", temp_inspect$checks[3]))
print(paste("vial", i, "evenly-spaced", temp_inspect$checks[4]))
print(paste("vial", i, "NA Oxygen", temp_inspect$checks[5]))
if (temp_inspect$checks[1]== TRUE) {
print(paste("ALERT", "vial", i, "NA Time Elapsed FAILED"))
}
if (temp_inspect$checks[2]== TRUE) {
print(paste("ALERT", "vial", i, "sequential FAILED"))
}
if (temp_inspect$checks[3]==TRUE) {
print(paste("ALERT", "vial", i, "duplicated FAILED"))
}
if (temp_inspect$checks[4]==TRUE) {
print(paste("ALERT", "vial", i, "evenly-spaced FAILED"))
}
if (temp_inspect$checks[5]==TRUE) {
print(paste("ALERT", "vial", i, "NA Oxygen FAILED"))
}
temp_rate <- calc_rate(temp_inspect,
from = 1,
to = nrow(temp.data),
by = "row")
#now adjusting the rate to E. coli + SW
temp_adjusted_rate <- adjust_rate(temp_rate, SW.EC.rate) #adjust to background seawater respiration WITH E COLI
area_animal= as.numeric(resp_info[1, i + 8]) #selecting 8 because that is the first with real data -- first vial column will always be negative control
MO2_temp <- convert_rate(temp_adjusted_rate,
oxy.unit = "%Air", # original O2 units
time.unit = "min", # time units
output.unit = "umol/hr/cm2", # desired rate units
volume = 0.0019, # chamber liquid volume, IN LITERS
area=convert_val(area_animal, from="cm2"), # area in GRAMS
t = t, # temperature
S = S, # salinity
P = P) # atmospheric pressure
#now we extract the area specific rates and put them in the empty vector we made
output_area_specific_O2[i] <- as.numeric(MO2_temp$summary$rate.a.spec)
}
t
S
P
area_animal
output_area_specific_O2[6]
```
```{r EDIT ME - OUTPUT FILE}
#taking output from output_area_specific_O2 and putting it into a CSV by vial name
names <- c("B1", "C1", "D1", "A2", "B2", "C2", "D2", "A3", "B3", "C3", "D3" ,"A4" ,"B4", "C4" ,"D4", "A5", "B5", "C5" ,"D5" ,"A6", "B6", "C6", "D6")
names_temp <- names[1: num_vials_filled]
output_temp<-rbind(names_temp,output_area_specific_O2)
write.csv(output_temp, "TESTECOLI1.csv", row.names=FALSE)
```