-
Notifications
You must be signed in to change notification settings - Fork 1
/
normfluordat.R
180 lines (146 loc) · 7.24 KB
/
normfluordat.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
#' Title: Cleans and normalizes DAT files obtained from experiments using the FLUOstar Omega microplate reader (from BMG LABTECH).
#'
#' @description
#' The simplest case scenario entails inputting the name or directory of a DAT file as a string,
#' the number of rows denoted by the tnp (test, negative, positive) parameter,
#' and the number of cycles (selected by the user when running the FLUOstar instrument).
#' The program takes these three baseline parameters, performs cleaning and normalization of the DAT file,
#' and then appends an attribute called “Cycle_Number” to the normalized data frame.
#'
#' @author Tingwei Adeck
#'
#' @param dat A string ("dat_1.dat") if the file is found within the present working directory (pwd) OR a path pointing directly to a ".dat" file.
#' @param tnp A numeric value indicating the number of rows used. TNP is used as an acronym for Test, Negative, Positive.
#' @param cycles A numeric value indicating the number of cycles selected by the user when running the FLUOstar instrument.
#' @param rows_used A character vector of the rows used; ru = c('A','B','C').
#' @param cols_used A numeric vector of the columns used; cu = c(1,2,3).
#' @param user_specific_labels A character vector manually prepared by the user to denote the wells used on the microplate reader; usl = c('A1','B1','C1').
#' @param read_direction A string input with two choices, “vertical” or “horizontal.”
#' The user indicates “vertical” if the user intends to have a final data frame
#' with samples arranged as sample type triplets (A1, B1, C1, A1, B1, C1)
#' OR “horizontal” if the user intends to have a final data frame with samples.
#' @param na_omit Takes a string "yes" OR "no".
#'
#' @import utils
#'
#' @return A normalized data frame with an appended "Cycle_Number" attribute. The “Cycle_Number” attribute is the X-variable.
#'
#' @export
#'
#' @seealso [normfluodat()]
#'
#' @note This function has less optimized space and time complexities than @seealso [normfluodat()].
#' In real-time the difference in optimization is not noticeable.
#' This function also takes less parameters than the more optimized version of the function.
#' Use @seealso [normfluodat()] for better approximation of attribute names.
#'
#' @examples fpath <- system.file("extdata", "dat_1.dat", package = "normfluodbf", mustWork = TRUE)
#' normalized_fluo_dat <- normfluordat(dat=fpath, tnp = 3, cycles = 40)
normfluordat <- function(dat, tnp, cycles, rows_used = NULL, cols_used= NULL, user_specific_labels = NULL, read_direction = NULL, na_omit = NULL){
df <- utils::read.table(dat)
df <- clean_odddat_optimus(df)
usl = user_specific_labels
if(is.null(read_direction) || read_direction == 'vertical'){
if(na_omit == 'yes' || is.null(na_omit)){
cleaned_dat_t <- resample_dat_scale_naretainer(df,tnp=tnp,cycles=cycles)
#name the columns
ru = rows_used
cu = cols_used
usl = user_specific_labels
sample_col_names <- dat_col_names_prime(dat,cleaned_dat_t, ru, cu, usl)
colnames(cleaned_dat_t) <- sample_col_names
cleaned_dat_t <- cleaned_dat_t[ , colSums(is.na(cleaned_dat_t))==0]
fluor_threshold_check(cleaned_dat_t)
#normalize
cleaned_dat_t <- as.data.frame(cleaned_dat_t)
cleaned_dat_t <- as.data.frame(lapply(cleaned_dat_t[1:ncol(cleaned_dat_t)], min_max_norm))
if(ncol(df) == 1){
colnames(cleaned_dat_t) <- sample_col_names
cleaned_dat_t <-unique_identifier(cleaned_dat_t)
cleaned_dat_t = cleaned_dat_t %>% dplyr::relocate('Cycle_Number')
return(cleaned_dat_t)
} else {
cleaned_dat_t <-unique_identifier(cleaned_dat_t)
cleaned_dat_t = cleaned_dat_t %>% dplyr::relocate('Cycle_Number')
return(cleaned_dat_t)
}
} else if(na_omit == 'no' || !is.null(na_omit)){
cleaned_dat_t <- resample_dat_scale_naretainer(df,tnp=tnp,cycles=cycles)
fluor_threshold_check(cleaned_dat_t)
#name the columns
ru = rows_used
cu = cols_used
usl = user_specific_labels
sample_col_names <- dat_col_names_prime(dat,cleaned_dat_t, ru, cu, usl)
colnames(cleaned_dat_t) <- sample_col_names
n <- 'na_dataframe'
assign( paste0(n, '_rawv'), as.data.frame(cleaned_dat_t), envir = parent.frame())
#normalize
cleaned_dat_t <- as.data.frame(cleaned_dat_t)
cleaned_dat_t <- as.data.frame(lapply(cleaned_dat_t[1:ncol(cleaned_dat_t)], min_max_norm))
if(ncol(df) == 1){
colnames(cleaned_dat_t) <- sample_col_names
cleaned_dat_t <-unique_identifier(cleaned_dat_t)
cleaned_dat_t = cleaned_dat_t %>% dplyr::relocate('Cycle_Number')
return(cleaned_dat_t)
} else {
cleaned_dat_t <-unique_identifier(cleaned_dat_t)
cleaned_dat_t = cleaned_dat_t %>% dplyr::relocate('Cycle_Number')
return(cleaned_dat_t)
}
}
} else if(!is.null(read_direction) || read_direction == 'horizontal'){
if(na_omit == 'yes' || is.null(na_omit)){
cleaned_dat_t <- resample_dat_scale_alt_bf_na(df,tnp=tnp,cycles=cycles)
#name the columns
ru = rows_used
cu = cols_used
#usl = user_specific_labels
#rd = read_direction
sample_col_names <- dat_col_names_horizontal(dat,cleaned_dat_t, ru, cu)
#sample_col_names <- dat_col_names_optimus(dat,cleaned_dat_t, ru, cu, usl, rd)
colnames(cleaned_dat_t) <- sample_col_names
cleaned_dat_t <- cleaned_dat_t[ , colSums(is.na(cleaned_dat_t))==0]
fluor_threshold_check(cleaned_dat_t)
#normalize
cleaned_dat_t <- as.data.frame(cleaned_dat_t)
cleaned_dat_t <- as.data.frame(lapply(cleaned_dat_t[1:ncol(cleaned_dat_t)], min_max_norm))
if(ncol(df) == 1){
colnames(cleaned_dat_t) <- sample_col_names
cleaned_dat_t <-unique_identifier(cleaned_dat_t)
cleaned_dat_t = cleaned_dat_t %>% dplyr::relocate('Cycle_Number')
return(cleaned_dat_t)
} else {
cleaned_dat_t <-unique_identifier(cleaned_dat_t)
cleaned_dat_t = cleaned_dat_t %>% dplyr::relocate('Cycle_Number')
return(cleaned_dat_t)
}
} else if(na_omit == 'no' || !is.null(na_omit)){
cleaned_dat_t <- resample_dat_scale_alt_bf_na(df,tnp=tnp,cycles=cycles)
fluor_threshold_check(cleaned_dat_t)
#name the columns
ru = rows_used
cu = cols_used
#usl = user_specific_labels
#rd = read_direction
sample_col_names <- dat_col_names_horizontal(dat,cleaned_dat_t, ru, cu)
#sample_col_names <- dat_col_names_optimus(dat,cleaned_dat_t, ru, cu, usl, rd)
colnames(cleaned_dat_t) <- sample_col_names
n <- 'na_dataframe'
assign( paste0(n, '_rawh'), as.data.frame(cleaned_dat_t), envir = parent.frame())
#normalize
cleaned_dat_t <- as.data.frame(cleaned_dat_t)
cleaned_dat_t <- as.data.frame(lapply(cleaned_dat_t[1:ncol(cleaned_dat_t)], min_max_norm))
if(ncol(df) == 1){
colnames(cleaned_dat_t) <- sample_col_names
cleaned_dat_t <-unique_identifier(cleaned_dat_t)
cleaned_dat_t = cleaned_dat_t %>% dplyr::relocate('Cycle_Number')
return(cleaned_dat_t)
} else {
cleaned_dat_t <-unique_identifier(cleaned_dat_t)
cleaned_dat_t = cleaned_dat_t %>% dplyr::relocate('Cycle_Number')
return(cleaned_dat_t)
}
}
}
}