-
Notifications
You must be signed in to change notification settings - Fork 1
/
SENDColumnData.R
308 lines (297 loc) · 13.9 KB
/
SENDColumnData.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
# These functions work together with the SendDataFactory
sameConfig <- function(aSex,aTestCD,aSpec,aSpecies,aStrain) {
if (exists("lastSetTestConfig")) {
# check if the same one
compareTestConfigSettings <<- paste(aSex,aTestCD,aSpec,aSpecies,aStrain,sep=";;;")
if (lastSetTestConfigSettings==compareTestConfigSettings) {
answer <- TRUE
printDebug(paste("Matches same settings",compareTestConfigSettings))
} else {
answer <- FALSE
}
} else {
answer <- FALSE
}
answer
}
getOrres <- function(aDomain,aSex,aTestCD,aSpec,aSpecies,aStrain,iDay,aTime, aTreatment){
aDomainConfig <- getConfig(aDomain)
## If Domain is numeric
if(aDomain %in% c("BG", "BW", "EG", "FW", "LB", "PC", "PP", "VS","OM")){
## If config found
if(exists("aDomainConfig") && !is.null(aDomainConfig)) {
printDebug(paste(" Debug 1 Determining mean: ",aDomain," sex: ",aSex," testcd: ",aTestCD))
testcd_ind <- str_which(names(aDomainConfig), "TESTCD")
mean_ind <- str_which(names(aDomainConfig), "STRESM")
sd_ind <- str_which(names(aDomainConfig), "STRESSD")
eltm_ind <- str_which(names(aDomainConfig), "ELTM")
spec_ind <- str_which(names(aDomainConfig), paste0(aDomain,"SPEC"))
aValueMean <- aDomainConfig[aDomainConfig$SEX == aSex &
aDomainConfig[,testcd_ind] == aTestCD,
mean_ind]
aValueSD <- aDomainConfig[aDomainConfig$SEX == aSex &
aDomainConfig[,testcd_ind] == aTestCD,
sd_ind]
# for some domains, check as well to species and strain level
if(aDomain %in% c("BG", "BW", "EG", "FW", "LB","VS")){
aValueMean <- aDomainConfig[aDomainConfig$SEX == aSex & aDomainConfig$SPECIES == aSpecies & aDomainConfig$STRAIN == aStrain &
aDomainConfig[,testcd_ind] == aTestCD,
mean_ind]
if (is.null(aValueMean)) {
stop(paste("Unable to find configurated mean value for this species",aSpecies,"and strain",aStrain))
}
aValueSD <- aDomainConfig[aDomainConfig$SEX == aSex & aDomainConfig$SPECIES == aSpecies & aDomainConfig$STRAIN == aStrain &
aDomainConfig[,testcd_ind] == aTestCD,
sd_ind]
}
# for some domains, check as well to species, strain level and specimen
if(aDomain %in% c("OM")){
aValueMean <- aDomainConfig[aDomainConfig$SEX == aSex & aDomainConfig$SPECIES == aSpecies & aDomainConfig$STRAIN == aStrain &
aDomainConfig[,testcd_ind] == aTestCD &
aDomainConfig[,spec_ind] == aSpec,
mean_ind]
if (is.null(aValueMean)) {
stop(paste("Unable to find configurated mean value for this species",aSpecies,"strain",aStrain,"and specimen",aSpec))
}
aValueSD <- aDomainConfig[aDomainConfig$SEX == aSex & aDomainConfig$SPECIES == aSpecies & aDomainConfig$STRAIN == aStrain &
aDomainConfig[,testcd_ind] == aTestCD &
aDomainConfig[,spec_ind] == aSpec,
sd_ind]
}
# for some domains, check as well to the time level
if(aDomain %in% c("PC")){
printDebug(paste("Getting to the time level for mean",aTime))
aValueMean <- aDomainConfig[aDomainConfig$SEX == aSex &
aDomainConfig[,testcd_ind] == aTestCD &
aDomainConfig[,eltm_ind] == aTime,
mean_ind]
if (is.null(aValueMean)) {
stop(paste("Unable to find configurated mean value for this time",aTime))
}
printDebug(paste("Getting to the time level for sd",aTime))
aValueSD <- aDomainConfig[aDomainConfig$SEX == aSex &
aDomainConfig[,testcd_ind] == aTestCD &
aDomainConfig[,eltm_ind] == aTime,
sd_ind]
}
# if got more than one, average it
printDebug(paste(" In getORRES mean is",aValueMean,"and SD",aValueSD))
aValueMean <- mean(aValueMean)
aValueSD <- mean(aValueSD)
printDebug(paste(" In getORRES, mean of these mean is",aValueMean,"and SD",aValueSD))
aValue <- rnorm(1, aValueMean, aValueSD)
printDebug(paste(" Resultant value",aValue))
# if this results in negative, adjust up
if (aValue <= 0) {
aValue <- aValue + aValueSD
}
printDebug(paste(" Adjusted",aValue))
# OM needs more units
if (aDomain=="OM") {
aValue <- round(aValue, digits=4)
} else {
aValue <- round(aValue, digits=2)
}
# now increase or decrease this based upon synthetic time response
aTimeResponse <- getTimeResponse(aDomain,aSex,aTestCD,aSpec,aSpecies,aStrain,iDay, aTreatment)
aValue <- aValue + aTimeResponse
printDebug(paste(" DEBUG Domain: ",aDomain," sex: ",aSex," testcd: ",aTestCD))
printDebug(paste(" DEBUG Test cd: ",testcd_ind," mean index and value: ",mean_ind,aValueMean,aValue))
## If config not found
} else {
aValue <- round(runif(1, 2.0, 100), digits=2)
}
## If domain is catagorical
} else {
## not numeric and If config is found:
if(!is.null(aDomainConfig)) {
# print(paste(" DEBUG using factor and proportion ",aTestCD,aSex,aSpecies,aStrain,aSpec))
testcd_ind <- str_which(names(aDomainConfig), "TESTCD")
fact_ind <- str_which(names(aDomainConfig), "(STRESC)$")
prop_ind <- str_which(names(aDomainConfig), "(FREQ)$")
spec_ind <- str_which(names(aDomainConfig), paste0(aDomain,"SPEC"))
if (sameConfig(aSex,aTestCD,aSpec,aSpecies,aStrain)) {
testConfig <- lastSetTestConfig
} else {
## Pull proportions for this sex,testcd
# check if config includes spec
if (identical(spec_ind, integer(0))) {
testConfig <- aDomainConfig[aSex==aDomainConfig$SEX &
aTestCD==aDomainConfig[,testcd_ind],]
} else {
testConfig <- aDomainConfig[aSex==aDomainConfig$SEX &
aTestCD==aDomainConfig[,testcd_ind] &
aSpec==aDomainConfig[,spec_ind],]
}
# for some domains, check as well to species and strain level
if(aDomain %in% c("MA", "MI", "CL")){
if (identical(spec_ind, integer(0))) {
testConfig <- aDomainConfig[aSex==aDomainConfig$SEX & aDomainConfig$SPECIES == aSpecies & aDomainConfig$STRAIN == aStrain &
aTestCD==aDomainConfig[,testcd_ind],]
} else {
testConfig <- aDomainConfig[aSex==aDomainConfig$SEX & aDomainConfig$SPECIES == aSpecies & aDomainConfig$STRAIN == aStrain &
aTestCD==aDomainConfig[,testcd_ind] &
aSpec==aDomainConfig[,spec_ind],]
}
lastSetTestConfig <<- testConfig
lastSetTestConfigSettings <<- paste(aSex,aTestCD,aSpec,aSpecies,aStrain,sep=";;;")
} # end of test for same configuration
}
totalProportion <- sum(testConfig[,prop_ind])
## If Proportions don't sum to 1 the sample() function will normalize
#if(totalProportion != 1) {
# warning(paste0("Total Proportion for: ",aTestCD," does not sum to 1: ", totalProportion,", Normalizing to 1"))
#}
# if none found, may be due to no available for this specimen, skip row
if (nrow(testConfig)==0) {
aValue <- "Skip Row"
skipDataRow <<- TRUE
# print("Debug - no value found")
} else {
# print(paste(" DEBUG determining value ",aTestCD,prop_ind,fact_ind))
aValue <- sample(testConfig[,fact_ind], size = 1, prob = testConfig[,prop_ind])
}
## If config is not found
} else {
nameList <- getCodeList(paste(aDomain,"STRESC",sep=""))
aValue <- CTRandomName(nameList)
}
}
lastOrres <<- aValue
aValue
}
getOrresUnit <- function(aCol,aDomain,aSex,aTestCD,aSpecies,aStrain){
# should be tied by configuration to the ORRES value
aDomainConfig <- getConfig(aDomain)
## If config found
if(exists("aDomainConfig") && !is.null(aDomainConfig)) {
# print(paste(" Debug 1 Determining mean: ",aDomain," sex: ",aSex," testcd: ",aTestCD))
unitInd <- str_which(names(aDomainConfig), "(STRESU)$")
testcd_ind <- str_which(names(aDomainConfig), "TESTCD")
} else {
stop(paste("Unable to find configurated unit for this species",aSpecies,"and strain",aStrain))
}
# get unit from configuration matching sex
aValue <- "Not yet set"
# for some domains, check as well to species and strain level
if(aDomain %in% c("BG", "BW", "EG", "FW", "LB","OM","VS")){
aValue1 <- unique(aDomainConfig[aDomainConfig$SEX == aSex & aDomainConfig$SPECIES == aSpecies & aDomainConfig$STRAIN == aStrain &
aDomainConfig[,testcd_ind] == aTestCD,unitInd])
} else {
aValue1 <- unique(aDomainConfig[aDomainConfig$SEX == aSex & aDomainConfig[,testcd_ind] == aTestCD,unitInd])
}
if (!is.null(aValue1)) {
aValue <- aValue1
}
lastOrresu <<- aValue
aValue
}
getCatVal <- function(aDomain,aTestCD){
# Get category based on unique value of testCd
aDomainConfig <- getConfig(aDomain)
## If config found
if(exists("aDomainConfig") && !is.null(aDomainConfig)) {
testcd_ind <- str_which(names(aDomainConfig), "TESTCD")
catInd <- str_which(names(aDomainConfig), "(CAT)$")
} else {
stop(paste("Unable to find configuration"))
}
# get unit from configuration matching testcd
aValue <- "Not yet set"
aValue <- unique(aDomainConfig[aDomainConfig[,testcd_ind] == aTestCD,catInd])[1]
aValue
}
getSpecVal <- function(aDomain,aTestCD){
# Get specimen based on unique value of testCd
aDomainConfig <- getConfig(aDomain)
## If config found
if(exists("aDomainConfig") && !is.null(aDomainConfig)) {
testcd_ind <- str_which(names(aDomainConfig), "TESTCD")
specInd <- str_which(names(aDomainConfig), "(SPEC)$")
} else {
stop(paste("Unable to find configuration"))
}
# get from configuration matching testcd
aValue <- "Not yet set"
aValue <- unique(aDomainConfig[aDomainConfig[,testcd_ind] == aTestCD,specInd])[1]
aValue
}
getStresc <- function(aCol){
# for now assume it is the same as the last orres
lastOrres
}
getStresuUnit <- function() {
# for now assume it is the same as the orresu
lastOrresu
}
# returns column data based upon the column name
getColumnData <- function (aCol,aSex,aTreatment,anAnimal,aRow,aDomain,aStudyID,aTestCD,iDay,aSpec,aSpecies,aStrain,aSENDVersion,aTime,theArm) {
aData <- ""
aSeqCol <- paste(aDomain,"SEQ",sep="")
aTestCDCol <- paste(aDomain,"TESTCD",sep="")
aTestCol <-paste(aDomain,"TEST",sep="")
aORRESCol <- paste(aDomain,"ORRES",sep="")
aSTRESCCol <-paste(aDomain,"STRESC",sep="")
aSTRESNCol <- paste(aDomain,"STRESN",sep="")
aORRESUCol <- paste(aDomain,"ORRESU",sep="")
aSTRESUCol <- paste(aDomain,"STRESU",sep="")
aSpecCol <- paste(aDomain,"SPEC",sep="")
aDay <- paste(aDomain,"DY",sep="")
aDayEnd <- paste(aDomain,"ENDY",sep="")
aDate <- paste(aDomain,"DTC",sep="")
aDateEnd <- paste(aDomain,"ENDTC",sep="")
aNOMDYCol <- paste(aDomain,"NOMDY",sep="")
aNOMLBLCol <- paste(aDomain,"NOMLBL",sep="")
aELTM <- paste(aDomain,"ELTM",sep="")
aTPT <- paste(aDomain,"TPT",sep="")
aCatCol <- paste(aDomain,"CAT",sep="")
aTPTNUM <- paste(aDomain,"TPTNUM",sep="")
aTPTREF <- paste(aDomain,"TPTREF",sep="")
aBLFL <- paste(aDomain,"BLFL",sep="")
aData <- NA
# Next line for help in debugging
printDebug(paste(" Debug 3 Getting column data for:",aCol,aSex,aTreatment,anAnimal,aRow,aDomain,aStudyID,aTestCD,aSpec,aTime,sep=":"))
if (aCol=="DOMAIN") aData <- aDomain
if (aCol=="STUDYID") {aData <- aStudyID}
if (aCol==aSeqCol) {aData <- aRow}
if (aCol=="USUBJID") {aData <- createUSubjIDfromSubjID(aStudyID,anAnimal)}
if (aCol==aCatCol && (aDomain=="CL" || aDomain=="LB")) {
aData <- getCatVal(aDomain,aTestCD)
printDebug(paste(" retreived cat value: ",aDomain,aTestCD,aData))
}
if (aCol==aTestCDCol) {
aData <- getSENDTestCode(aCol,aTestCD)
}
if (aCol==aTestCol) {aData <- getSENDLastTestCodeName(aCol,aDomain)}
if (aCol==aORRESCol) aData <- getOrres(aDomain,aSex,aTestCD,aSpec,aSpecies,aStrain,iDay,aTime, aTreatment)
if (aCol==aORRESUCol) {aData <- getOrresUnit(aCol,aDomain,aSex,aTestCD,aSpecies,aStrain)}
if (aCol==aSTRESCCol) {aData <- getStresc(aCol)}
if (aCol==aSTRESNCol) {aData <- suppressWarnings(as.numeric(lastOrres))}
if (aCol==aSTRESUCol) {aData <- getStresuUnit()}
if (aCol==aDay) {aData <- iDay}
if (aCol==aDayEnd && aDomain=="FW") {aData <- iDay+7}
if (aCol==aDate) {aData <- as.character(as.Date(getStartDate())+iDay-1)}
if (aCol==aDateEnd && aDomain=="FW") {aData <- as.character(as.Date(getStartDate())+iDay-1+7)}
if (aSENDVersion=="3.0") {
if (aCol=="VISITDY") {aData <- iDay}
} else {
if (aCol==aNOMDYCol) {aData <- iDay}
if (aCol==aNOMLBLCol) {aData <- paste("Day",iDay)}
}
# set baseline as first day, unless PC domain
if (aCol==aBLFL && aDomain != "PC") {
if (iDay==1) {
aData <- "Y"
}
}
if (aCol==aSpecCol) aData <- aSpec
# some specimen look up instead
if (aCol==aSpecCol && (aDomain=="LB")) {aData <- getSpecVal(aDomain,aTestCD)}
if (aCol==aELTM) aData <- aTime
if (aCol==aTPT) aData <- getTPT(aDomain,aSex,aTestCD,aSpec,aSpecies,aTime)
if (aCol==aTPTNUM) aData <- getTPTNUM(aDomain,aSex,aTestCD,aSpec,aSpecies,aTime)
if (aCol==aTPTREF) aData <- paste("Day",iDay,"Dosing")
# return the data
printDebug(paste(" aData returned: ",aData))
aData
}