1
+ # ' tcplPlotLoadWllt
2
+ # ' Replaces NA dtxsid and chnm with a string description of the sample's well type(s)
3
+ # '
4
+ # ' @param dat dataset
5
+ # ' @param type mc or sc
6
+ # '
7
+ # ' @return dat with updated dtxsid/chnm if they are NA
8
+ # ' @import data.table
9
+ # ' @importFrom dplyr group_by summarize select left_join case_when
10
+ tcplPlotLoadWllt <- function (dat = NULL , type = " mc" ) {
11
+
12
+ # variable binding for R CMD check
13
+ dsstox_substance_id <- chnm <- aeid <- acid <- wllt <- wllt_desc <- NULL
14
+
15
+ # determine missing chemical info
16
+ missing <- dat [is.na(dsstox_substance_id ) | is.na(chnm ), .(spid , aeid )]
17
+ if (nrow(missing ) > 0 ) {
18
+ acid_map <- tcplLoadAcid(fld = " aeid" , val = missing $ aeid )[,.(aeid ,acid )]
19
+ missing <- missing [acid_map , on = " aeid" ]
20
+ l0_dat <- tcplLoadData(type = type , lvl = 0 , fld = list (" spid" ," acid" ),
21
+ list (missing $ spid , missing $ acid ))
22
+
23
+ # replace with string describing the well type(s)
24
+ wllts <- l0_dat | > select(acid , spid , wllt ) | > group_by(acid , spid ) | >
25
+ summarize(wllt_desc = dplyr :: case_when(
26
+ all(unique(wllt ) %in% c(' c' , ' p' )) ~ " Gain-of-signal control" ,
27
+ all(unique(wllt ) %in% c(' m' , ' o' )) ~ " Loss-of-signal control" ,
28
+ all(unique(wllt ) == ' n' ) ~ " Neutral/negative control" ,
29
+ all(unique(wllt ) == ' b' ) ~ " Blank" ,
30
+ all(unique(wllt ) == ' v' ) ~ " Viability control" ,
31
+ all(! is.na(unique(wllt ))) && all(! is.null(unique(wllt ))) ~ paste0(" Well type: " , paste(unique(wllt ), collapse = " , " )),
32
+ .default = NA
33
+ ), .groups = " drop" ) | > left_join(acid_map , by = " acid" ) | > as.data.table()
34
+ no_wllt <- wllts [is.na(wllt_desc ), spid ]
35
+ if (length(no_wllt )) warning(paste0(" wllt for SPID(s): " , paste(no_wllt , collapse = " , " ), " missing. Leaving dsstox_substance_id and chnm as NA." ))
36
+ dat <- left_join(dat , wllts , by = c(" aeid" , " spid" ))
37
+ dat [is.na(dsstox_substance_id ) | is.na(chnm ), c(" dsstox_substance_id" , " chnm" ) : = wllt_desc ]
38
+ }
39
+ dat
40
+
41
+ }
42
+
43
+
1
44
# ' tcplPlotSetYRange
2
45
# '
3
46
# ' @param dat dataset
@@ -47,17 +90,69 @@ tcplPlotSetYRange <- function(dat, yuniform, yrange, type) {
47
90
yrange
48
91
}
49
92
93
+ # ' tcplPlotCalcAspectRatio
94
+ # '
95
+ # ' @param type string of mc or sc indicating if it is single or multi conc
96
+ # ' @param verbose should the plot return a table with parameters
97
+ # ' @param multi Boolean, by default TRUE for "pdf" if the number of plots exceeds
98
+ # ' one. Prints variable number of plots per page depending on 'verbose' and 'type'
99
+ # ' settings.
100
+ # ' @param nrows Integer, number of rows each compare plot uses
101
+ # ' @param output How should the plot be presented. To work with the plot in
102
+ # ' environment, use "ggplot"; to interact with the plot in application, use
103
+ # ' "console"; or to save as a file type, use "pdf", "jpg", "png", "svg", or "tiff".
104
+ # ' @param group.threshold Integer of length 1, minimum number of samples in a
105
+ # ' given plot where comparison plots, instead of coloring models by sample, should
106
+ # ' delineate curve color by a given group.fld. By default 9.
107
+ # ' @param nrow Integer, number of rows in multiplot. By default 2.
108
+ # ' @param ncol Integer, number of columns in multiplot. By default 3, 2 if verbose,
109
+ # ' 1 for compare plots.
110
+ # ' @param flags Boolean, by default FALSE. If TRUE, level 6 flags are displayed
111
+ # ' within output.
112
+ # '
113
+ # ' @return a list of validated parameters for plotting
114
+ tcplPlotCalcAspectRatio <- function (type = " mc" , verbose = FALSE , multi = FALSE , nrows = 0 ,
115
+ output = c(" ggplot" , " console" , " pdf" , " png" , " jpg" , " svg" , " tiff" ),
116
+ group.threshold = 9 , nrow = NULL , ncol = NULL , flags = FALSE ) {
117
+
118
+ # assign nrow = ncol = 1 for output="pdf" and multi=FALSE to plot one plot per page
119
+ if (output == " pdf" && multi == FALSE )
120
+ nrow <- ncol <- 1
121
+ w <- 7
122
+ h <- 5
123
+ if (verbose && any(nrows > 1 & nrows < group.threshold ))
124
+ w <- 12
125
+ if (type == " sc" && any(! verbose , all(nrows == 1 | nrows > = group.threshold )))
126
+ w <- 5
127
+ if (type == " sc" )
128
+ h <- 6
129
+ if (verbose && any(nrows > 1 & nrows < group.threshold ))
130
+ h <- max(2 + max(nrows [nrows > 1 & nrows < group.threshold ]), 6 )
131
+ if (any(nrows > = group.threshold ) && flags )
132
+ h <- max(h , 8 )
133
+ if (is.null(nrow ))
134
+ nrow <- max(round(10 / h ), 1 )
135
+ if (is.null(ncol ))
136
+ ncol <- max(round(14 / w ), 1 )
137
+ list (w = w ,h = h ,nrow = nrow ,ncol = ncol )
138
+ }
50
139
51
140
# ' tcplPlotValidate
52
141
# '
142
+ # ' @param dat data.table containing plot-prepared data
53
143
# ' @param type string of mc or sc indicating if it is single or multi conc
144
+ # ' @param compare Character vector, the field(s) to join samples on to create comparison
145
+ # ' plots
146
+ # ' @param by Parameter to divide files into e.g. "aeid".
54
147
# ' @param flags bool - should we return flags
55
148
# ' @param output how should the plot be formatted
56
149
# ' @param multi are there multiple plots
57
150
# ' @param verbose should the plot return a table with parameters
58
151
# '
59
152
# ' @return a list of validated parameters for plotting
60
- tcplPlotValidate <- function (type = " mc" , flags = NULL , output = " none" , multi = NULL , verbose = FALSE ) {
153
+ tcplPlotValidate <- function (dat = NULL , type = " mc" , compare = " m4id" , by = NULL ,
154
+ flags = NULL , output = c(" ggplot" , " console" , " pdf" , " png" , " jpg" , " svg" , " tiff" ),
155
+ multi = NULL , verbose = FALSE ) {
61
156
# set lvl based on type
62
157
lvl <- 5
63
158
if (type == " sc" ) {
@@ -66,28 +161,39 @@ tcplPlotValidate <- function(type = "mc", flags = NULL, output = "none", multi =
66
161
warning(" 'flags' was set to TRUE - no flags exist for plotting single concentration" )
67
162
flags <- FALSE
68
163
}
164
+ if (compare == " m4id" ) compare <- " s2id"
69
165
}
70
-
71
- # default assign multi=TRUE for output="pdf"
72
- if (output == " pdf" && is.null(multi )) {
73
- multi <- TRUE
166
+
167
+ if (! is.null(by ) && length(by ) > 1 ) stop(" 'by' must be of length 1." )
168
+ if (length(output ) > 1 ) output <- output [1 ] else if (length(output ) == 0 ) stop(" 'output' cannot be NULL" )
169
+
170
+ if (! is.null(dat ) && ! is.data.table(dat )) {
171
+ if (! is.list(dat ) || ! is.data.table(dat [[1 ]])) {
172
+ stop(" 'dat' must be a data.table or a list of data.tables." )
173
+ }
174
+ if (! compare %in% c(" m4id" , " s2id" )) {
175
+ warning(" 'dat' provided as list of list of data tables, meaning compare plots are already subdivided. 'compare' field will be ignored and have no effect." )
176
+ }
177
+ if (! is.null(by )) {
178
+ warning(" Using 'by' can have unintended consequences when 'dat' is provided as a list of data.tables. Instead, consider adding a custom field to group comparison plots, and specify using the 'compare' parameter. Then, use 'by' to split plots into files." )
179
+ }
74
180
}
181
+
75
182
# forced assign multi=FALSE for output = c("console","png","jpg","svg","tiff"), verbose=FALSE for output="console"
76
183
if (output != " pdf" ) {
77
184
multi <- FALSE
78
- if (output == " console" ) {
79
- verbose <- FALSE
80
- }
185
+ if (output == " console" ) verbose <- FALSE
81
186
}
82
187
83
- list (lvl = lvl , type = type , flags = flags , output = output , multi = multi , verbose = verbose )
188
+ list (lvl = lvl , compare = compare , flags = flags , output = output , multi = multi , verbose = verbose )
84
189
}
85
190
86
191
87
192
# ' tcplLegacyPlot
88
193
# '
89
194
# ' @return a ggplot based on old plotting methodology
90
195
tcplLegacyPlot <- function () {
196
+ stop(" Legacy plotting is currently unsupported." )
91
197
# VARIABLE BINDING
92
198
fld <- val <- lvl <- multi <- fileprefix <- NULL
93
199
if (length(output ) > 1 ) output <- output [1 ]
0 commit comments