-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLarsenFunctionLibrary.R
409 lines (244 loc) · 14.7 KB
/
LarsenFunctionLibrary.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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#### Larsen Function Library
## Function creating z-scores from a vector of observations, x, the mean of the vector, mx, and the standard deviation of the vector, sd.
zscore = function(x,mx) {return((x-mx)/sd(x))}
## Function that computes the Median of MLB HRs over last 12 Seasons
# Function that finds the position of the median value in a vector
median_fun = function(x) {return(round(length(order(x))/2))}
# Variable for ordering the vector of interest (ML HRs)
ix = order(ML_HR)
# Optional step that calls the order of the vector to make sure it's ordered
ML_HR[ix]
# Creates a variable that stores the values of the vector from lowest to highest
SL = ML_HR[ix]
# Indexes the median value of the vector from the sorted list
SL[median_fun(ML_HR)]
### Note the test function for above (ML_HR)
## Another Function that gives the median of a vector of values
med = function(x) {return(median(x, na.rm = F))}
# Gives the median of ML HR
med(ML_HR)
## Create a normalizing function that takes a vector of numeric values, and for each value, subtracts the minimum value in the vector and divides by the range of values in the vector
normalize = function(x) {
return ((x - min(x)) / (max(x) - min(x)))
}
##### Calcium Imaging Functions #####
# To obtain variables to then input into the Data_prep function
# To Analyze Calcium Imaging Data #
Data_prep = function(Adjust, number_agonists, ideal_agonist_responses) {
# Adjust the baseline fluorescence for each cell to a value of 1 at time 0 in the new response dataframe
for (i in 1:ncol(ratio)){
for (j in 1:nrow(ratio)){
Adjust[j,i] = if_else(ratio[1,i] < 1, ratio[j,i]+(1-ratio[1,i]), ratio[j,i]+(1-ratio[1,i]))
}
}
# Account for any artefactual cells with negative values
for (i in 1:ncol(Adjust)){
for (j in 1:nrow(Adjust)){
Adjust[j,i] = if_else(Adjust[j,i] < 0, Adjust[j,i] + 1, Adjust[j,i])
}
}
##### First Agonist #####
# Set an arbitrary baseline average FL (pre-agonist) for all cells prior to addition of the first agonist in the 9th frame
# Take the average fluorescence over the first few frames
ratio_avg_baseline = (colSums(Adjust[1:9, 1:length(Adjust)], dims = 1))/9
# Calculate the response threshold for the first agonist (at frame 8; at exposure)
First_agonist_threshold = ratio_avg_baseline*0.10 + ratio_avg_baseline
# Determine responders to the first agonist
# Create an empty vector to fill with strings, classifying cells as either responders to the given agonist or not
First_agonist_responses = vector()
# Fill the vector with cells that responded to the agonist or not, based on the pre-determined, arbitrary threshold of what constitutes a "response"
for (i in 1:length(First_agonist_threshold)){
First_agonist_responses[i] = if_else(Adjust[25,i] >= First_agonist_threshold[i], "Responder", "Non-responder")
}
Adjust[25,] - ratio_avg_baseline[]
# See how many and also which cells are classified as "responders"
length(First_agonist_responses[First_agonist_responses == "Responder"])
which(First_agonist_responses == "Responder")
# Percent first agonist responders
first_agonist_responders = (length(First_agonist_responses[First_agonist_responses == "Responder"]))/ncol(Adjust)*100
first_agonist_responders
First_agonist_duration = vector()
# Establish an average response above which decay in response will be measured as a percentage
First_agonist_decay_average = (colSums(Adjust[30:44,1:length(Adjust)], dims = 1))/15
# Find an ideal neural response to the first agonist as a model response by cycling through the plots in the new directory on the server
#Adjust[,14]
# Use that plot number to find the ideal response's maximum response
max(Adjust[10:30,])
# Find at what frame that response occurs
which(Adjust[,] == max(Adjust[10:30,]))
# Find the decay (as a percentage from the onset (after peak) of the average)
((Adjust[30,] - First_agonist_decay_average[]) / Adjust[30,]*100)
# In this case, the response decays 14% from peak response to the average or baseline following addition of High Potassium
# Find the peak of decay (as a percentage from the onset of the average)
((Adjust[30,] / Adjust[35,])-1)*100
Adjust[35:44,] > First_agonist_decay_average[]
# In this case, the response decays 14.9% from peak response to the average or baseline following addition of first agonist
# Find the duration of responses to the first agonist; how long does the response last?
for (i in 1:length(First_agonist_responses)){
if (First_agonist_responses[i] == "Responder") {
First_agonist_duration[i] = length(which(Adjust[10:35,i] > First_agonist_decay_average[i]))
} else {
First_agonist_duration[i] = 0
}
}
# Find how many Responders to the first agonist had decays
length(First_agonist_duration[First_agonist_duration > 0])
# Find which Responders to the first agonist had decays
which(First_agonist_duration > 0) & which(First_agonist_responses == "Responder")
First_agonist_responses[]
First_agonist_duration[]
Adjust[25,] / Adjust[30,]
##### Second Agonist (CQ or Histamine) #####
# Determine responders to capsaicin
# Establish a baseline, accounting for the mean fluorescence in the 9 frames leading up to the second agonist addition
Second_agonist_baseline = (colSums(Adjust[36:44,1:length(Adjust)], dims = 1))/9
# Set the threshold 1 frame after exposure
Second_agonist_threshold = Second_agonist_baseline*0.10 + Second_agonist_baseline
# Create an empty vector to fill with strings, classifying cells as either responders to the given agonist or not
Second_agonist_responses = vector()
# Make response calls after addition in the 45th frame
for (i in 1:length(Second_agonist_threshold)){
Second_agonist_responses[i] = if_else(Adjust[45,i] > Second_agonist_threshold[i], "Responder", "Non-responder")
}
# See how many and also which cells are classified as "responders"
length(Second_agonist_responses[Second_agonist_responses == "Responder"])
which(Second_agonist_responses == "Responder")
second_agonist_responders = (length(Second_agonist_responses[Second_agonist_responses == "Responder"]))/ncol(Adjust)*100
second_agonist_responders
Second_agonist_duration = vector()
# Establish an average response where the rise in response asympotitcally slows (approaches ceiling); will be measured as a percentage
Second_agonist_decay_average = (colSums(Adjust[65:79,1:length(Adjust)], dims = 1))/15
# Find an ideal response to the Second agonist as a model response
#Adjust[,147]
# Find the ideal response's maximum response
max(Adjust[65:79,])
# Find at what frame that response occurs
which(Adjust[,] == max(Adjust[65:79,]))
# Find the decay (as a percentage from the onset (after peak) of the average)
((Adjust[66,] - Second_agonist_decay_average[]) / Adjust[66,]*100)
# In this case, the response decays 14% from peak response to the average or baseline following addition of High Potassium
# Find the peak of decay (as a percentage from the onset of the average)
((Adjust[66,] / Adjust[66,])-1)*100
Adjust[60:70,] > Second_agonist_decay_average[]
# In this case, the response decays 14.9% from peak response to the average or baseline following addition of second agonist
# Find the duration of responses to the second agonist
for (i in 1:length(Second_agonist_responses)){
if (Second_agonist_responses[i] == "Responder") {
Second_agonist_duration[i] = length(which(Adjust[60:70,i] > Second_agonist_decay_average[i]))
} else {
Second_agonist_duration[i] = 0
}
}
##### Capsaiscin Threshold for Classification #####
# Determine responders to capsaicin
# Establish a baseline, accounting for the mean fluorescence in the 8 frames leading up to the second agonist addition
Capsaicin_baseline = (colSums(Adjust[71:79,1:length(Adjust)], dims = 1))/9
# Set the threshold at frame 71; 1 frame after exposure
Capsaicin_agonist_threshold = Capsaicin_agonist_baseline*0.10 + Capsaicin_agonist_baseline
# Create an empty vector to fill with strings, classifying cells as either responders to the given agonist or not
Capsaicin_agonist_responses = vector()
# Make response calls after addition in the 80th frame
for (i in 1:length(Capsaicin_agonist_threshold)){
Capsaicin_agonist_responses[i] = if_else(Adjust[90,i] > Capsaicin_agonist_threshold[i], "Responder", "Non-responder")
}
# See how many and also which cells are classified as "responders"
length(Capsaicin_agonist_responses[Second_agonist_responses == "Responder"])
which(Capsaicin_agonist_responses == "Responder")
Capsaicin_agonist_responders = (length(Capsaicin_agonist_responses[Capsaicin_agonist_responses == "Responder"]))/ncol(Adjust)*100
Capsaicin_agonist_responders
Capsaicin_agonist_duration = vector()
# Establish an average response where the rise in response asympotitcally slows (approaches ceiling); will be measured as a percentage
Capsaicin_agonist_decay_average = (colSums(Adjust[90:95,1:length(Adjust)], dims = 1))/5
# Find an ideal response to the Second agonist as a model response
#Adjust[,147]
# Find the ideal response's maximum response
max(Adjust[85:95,])
# Find at what frame that response occurs
which(Adjust[,] == max(Adjust[85:95,]))
# Find the decay (as a percentage from the onset (after peak) of the average)
((Adjust[90,] - Second_agonist_decay_average[]) / Adjust[90,]*100)
# In this case, the response decays 14% from peak response to the average or baseline following addition of High Potassium
# Find the peak of decay (as a percentage from the onset of the average)
((Adjust[90,] / Adjust[90,])-1)*100
Adjust[90:95,] > Second_agonist_decay_average[]
# In this case, the response decays 14.9% from peak response to the average or baseline following addition of second agonist
# Find the duration of responses to Capsaicin
for (i in 1:length(Capsaicin_agonist_responses)){
if (Capsaicin_agonist_responses[i] == "Responder") {
Capsaicin_agonist_duration[i] = length(which(Adjust[85:104,i] > Capsaicin_agonist_decay_average[i]))
} else {
Capsaicin_agonist_duration[i] = 0
}
}
##### Neural Threshold for Classification #####
# Determine neurons
# Establish a baseline, accounting for the mean in the 8 frames leading up to the High K addition
Neural_baseline = (colSums(Adjust[80:86,1:length(Adjust)], dims = 1))/7
# Set the threshold at 5% above the baseline established 8 frames before High K addition
Neural_threshold = Neural_baseline*0.05 + Adjust[88,1:length(Adjust)]
# Create an empty vector to fill with strings, classifying cells as either neurons or not
High_K_responses = vector()
# Make response calls in the 110th frame to High K addition in the 109th frame
for (i in 1:length(Neural_threshold)){
High_K_responses[i] = if_else(Second_agonist_responses[i] == "Responder", "Neuron",
if_else(Adjust[89,i] - Neural_baseline[i] > 0.5, "Neuron", "Non-neuron"))
}
# See how many and also which cells are classified as "responders"
length(High_K_responses[High_K_responses == "Responder"])
which(High_K_responses == "Responder")
# What percent of the ROIs are neurons?
high_K_responders = (length(High_K_responses[High_K_responses == "Neuron"]))/ncol(Adjust)*100
high_K_responders
High_K_duration = vector()
# Establish an average response above which decay will be measured as a percentage in the first half of the time after addition (to total time)
High_K_decay_average = (colSums(Adjust[92:103,1:length(Adjust)], dims = 1))/12
# Find an ideal neural response to High Potassium as a model response
#Adjust[,7]
# Find the ideal response's maximum response
max(Adjust[,7])
# Find at what frame that response occurs
which(Adjust[,7] == max(Adjust[,7]))
# Find the decay (as a percentage from the onset (after peak) of the average)
((Adjust[92,7] - High_K_decay_average[7]) / Adjust[92,7]*100)
# In this case, the response decays 12% from peak response to the average or baseline following addition of High Potassium
# Find the duration of neural responses to High Potassium
for (i in 1:length(High_K_responses)){
if (High_K_responses[i] == "Neuron") {
High_K_duration[i] = length(which(Adjust[90:103,i] > High_K_decay_average[i]))
} else {
High_K_duration[i] = 0 }
}
# See how many and also which cells are classified as "Neurons"
length(High_K_responses[High_K_responses == "Neuron"])
which(High_K_responses == "Neuron")
# How many cells "respond" to the first agonist, yet aren't neurons?
length(which(High_K_responses == "Non-neuron" & First_agonist_responses == "Responder"))
##### Create a new dataframe that will store binary character response classifications of each cell to each reagent along with the response magnitude and duration #####
# Create a dataframe containing the responses of each cell for each agonist
Responses = data.frame()
for (i in 1:length(Adjust)) {
# 1st Ag Mag
Responses[i,1] = Adjust[9,i]
# 2nd Ag Mag
Responses[i,2] = Adjust [69,i]
# High K Mag
Responses[i,3] = Adjust [89,i]
# 1st Ag Duration
Responses[i,4] = First_agonist_duration[i]
# 2nd Ag Duration
Responses[i,5] = Second_agonist_duration[i]
# High K Duration
Responses[i,6] = High_K_duration[i]
# 1st Ag Class.
Responses[i,7] = First_agonist_responses[i]
# 2nd Ag Class.
Responses[i,8] = Second_agonist_responses[i]
# High K Class.
Responses[i,9] = High_K_responses[i]
}
colnames(Responses) = c("1st Agonist Response Magnitude", "2nd Agonist Response Magnitude", "High K Response Magnitude",
"1st Agonist Response Duration", "2nd Agonist Response Duration", "High K Response Duration",
"1st Agonist Responder", "2nd Agonist Responder", "High K Responder")
Response_DF = data.frame(First_agonist = c(First_agonist_responses), First_ag_Mag = Responses[,1], Capsaicin = c(Capsaicin_responses), Capsaicin_Mag = Responses[,2], High_K = c(High_K_responses), High_K_Mag = Responses[,3])
return (Responses, Agonist_proportions)
}