-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy path.Rhistory
512 lines (512 loc) · 15.2 KB
/
.Rhistory
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
}
if (i+1 <= nrow(TRTVtokens))
{
if ((("r" == TRTVtokens$Category[i])||(f_found&&("R" == TRTVtokens$Category[i])))&&("U" == TRTVtokens$Category[i+1]))
{
#look for a number at the start of Source in i+1 to go with the +/- or -
numStringPos <-regexpr("^\\s*[+-]{0,1}[0-9]*\\.{0,1}[0-9]+",TRTVtokens$Source[i+1])
numStringPosStop <- attr(numStringPos,"match.length")+numStringPos-1
num = substr(TRTVtokens$Source[i+1],numStringPos, numStringPosStop)
#print(paste("Source=",TRTVtokens$Source[i+1]))
#print(paste("numStringPos=",numStringPos))
#print(paste("numStringPosStop=",numStringPosStop))
#print(paste("num =",num))
#
# if a number was found at the end of the following row...
if (numStringPosStop<nchar(TRTVtokens$Source[i+1]))
{
# make a row for the text before the number as a "U" row
tokens_U <- data.frame(Source=substr(TRTVtokens$Source[i+1],numStringPosStop+1,nchar(TRTVtokens$Source[i+1])),
Category = "U",
CTCode = NA,
Value = NA,
stringsAsFactors = FALSE)
# make a new row for the new "n" row
tokens_n <- data.frame(Source=num,
Category = "n",
CTCode = NA,
Value = num,
stringsAsFactors = FALSE)
#print(tokens_n)
#make the new tokens table
if (i+2 <= nrow(TRTVtokens))
{
TRTVtokens <- rbind(slice(TRTVtokens,1:i),tokens_n,tokens_U,slice(TRTVtokens,i+2:nrow(TRTVtokens)))
}
else
{
TRTVtokens <- rbind(slice(TRTVtokens,1:i),tokens_n,tokens_U)
}
}
else
{
# A number was not found at the end of the previous row to go with the r.
}
#print(tokens_U)
}
}
i <- i+1
}
return(TRTVtokens)
}
# if the same component listed in two consecutive rows (ignoring unknown rows) the source probably is providing a synonym. Ignore the duplicate.
cleanTokens_syn <-function(TRTVtokens)
{
i <- 2
while (i <= nrow(TRTVtokens))
{
if( ("c" == TRTVtokens$Category[i] ))
{
#we have found a component. See if the next row is also a component row, ignoring rows of category "U"
j <- i +1
while ((j <= nrow(TRTVtokens))&&("U" == TRTVtokens$Category[j]))
{
j <- j+1
}
if ((j <= nrow(TRTVtokens))&&("c" == TRTVtokens$Category[j])&&(TRTVtokens$Value[i] == TRTVtokens$Value[j]))
{
#we found that row j is also a a component row and has the same Value as row i. Change row j to be ignored.
TRTVtokens$Category[j] <- "i"
}
}
i <- i+1
}
return(TRTVtokens)
}
cleanTokens_R <-function(TRTVtokens)
{ # ignore R before f. Call this after cleanTokens_rn(). If you decide to change this to process R before f, also change the behaviour in the cleanTokens_ru() function.
f_found <-FALSE
i <- 1
while (i <= nrow(TRTVtokens))
{
if( ("f" == TRTVtokens$Category[i] ))
{
f_found <- TRUE
}
if ("R" == TRTVtokens$Category[i])
{
if (f_found)
{
# no change
}
else
{
#we found an R before an "f"
# ignor the R now and ignore the number before and
TRTVtokens$Category[i] <- "i"
}
}
i <- i+1
}
return(TRTVtokens)
}
makeXV <- function(TRTVtokens)
{
# For best results, please apply the cleaning functions to TRTVtokens before calling this function.
#
# This function receives a table of the tokenized TRTV text string and examines the categories assigned to
# the tokens with extensive use of regular expressions to create a XV table like the XV
# example in the PHUSE whitepaper "Recommendations for Exchanging Vehicle details" that is similar to the
# AC domain in SENDIG-AR.
# Let's start by creating regular expressions of the main parts of a vehicle text string starting with vehicle components
# each component is either
# an amount and unit of measure (nu) followed by
# a component name (c)
component_regx <- "(nuc)"
# or
# a component name (c) followed by
# an optional amount and unit of measure (au)?
#
component_regx <- paste("(",component_regx,"|","(c(nu)?))",sep="")
# Each dilluent has 1 component preceded by an "f" category token.
#
diluent_regx <- paste("f",component_regx,sep="")
# Each attribute has
# an attribute name
# an attribute value
# optionally an atribute tolerance range
attribute_regx <-paste("(an(rn|(R)n)?)",sep="")
# We will later put these regular expressions together to describe the treatment vehicle text string.
#Put the categories together into a single string and delete the tokens that are Unknown and those that should be ignored.
category_noU <- str_replace_all(paste(p$Category,collapse=""),"U|i","")
#The first row of our output data frame is the full text string. Since this function doesn't receive the original text
#string, it is recreated using the TRTVtokens$Source.
XVtable <- data.frame(STUDYID = NA,
DOMAIN = "XV",
XVSEQ = 1,
XVGRPID = 1,
XVSGRPID = NA,
XVPARMCD = "TRTV",
XVPARM = "Treatment Vehicle",
XVSPARM = NA,
XVVAL = paste0(TRTVtokens$Source,collapse = " "),
XVVALU = NA,
XVVALNF = NA,
stringsAsFactors=FALSE)
# Before continuing, let's make sure we have a string we can understand
# I expect one or more components
# optionally one diluent
# optionally one or more attributes
if (TRUE == grepl(paste0("^",component_regx,"+(",diluent_regx,")?(",attribute_regx,")*$",sep=""),category_noU))
{
# The regular expression we just used includes repetitions of component_regx and attribute_regx. Let's determine the number of repetitions of each.
# We will use this to create a regular expression with the correct number of repetitions, because we can then get an index telling us the start of each group.
#
# To determine the number of repetitions of component_regx, let's extract from category_nuU the portion before the the diluent_regx or attribute_regx
if (grepl(paste("(",diluent_regx,")|(",attribute_regx,")",sep=""),category_noU))
{
m <- regexec(paste("(",diluent_regx,")|(",attribute_regx,")",sep=""),category_noU) # m is assigned the character position of each match in category_nuU and 0 for parts that have no match.
end_pos <- min(m[[1]][which(m[[1]]>0)])-1 # Find the first position of a match and subtract 1.
} else
{
# we have no match, use the whole string
end_pos <- nchar(category_noU)
}
x <- substr(category_noU,1,end_pos)
component_reps <- str_count(x,component_regx)
#
# To determine the number of repetitions of attribute_regx, let's extract from category_noU the portion we didn't include last time.
# This will include the final character of the previous search in the new search. This does no harm and avoids needing to check to see if the previous search's end is the end of the string.
x <- substr(category_noU,end_pos,nchar(category_noU))
attribute_reps <- str_count(x,attribute_regx)
#print(paste0("components = ",component_reps," attributes = ",attribute_reps))
# Now that we have the number of repetitions lets make a regular expression with the right number of repetitions of the components
trtv_regx <- "^";
i <- 1
while (i <= component_reps)
{
trtv_regx <- paste0(trtv_regx,component_regx,sep="")
i <- i+1
}
trtv_regx <- paste0(trtv_regx,"(",diluent_regx,")?",sep="")
i <- 1
while (i <= attribute_reps)
{
trtv_regx <- paste0(trtv_regx,"(",attribute_regx,")")
i<- i+1
}
trtv_regx <- paste0(trtv_regx,"$")
# testing the results
#print(category_noU)
#print(paste0("trtv_regx = ",trtv_regx))
#print(paste0(grepl(trtv_regx,category_noU)))
if (debug)
# store the charactor positions of each "(" in m[[1]][1]
m <- regexec(trtv_regx,category_noU)
#print(m)
# create xref to convert from character positions in m to rows in TRTVtokens. We only need this because we deleted the U and i categories.
xref <- c()
i <-1
j <-1
while(j<=nrow(TRTVtokens))
{
if (grepl("U|i",TRTVtokens$Category[j]))
{
#skip this row
}
else
{
xref[i] <- j
i <- i+1
}
j <- j+1
}
# Create the XVtable start by adding rows for the components ((nuc)|(c(nu)?))
# item 2 will have nuc
# item 3 will have cnu
# item 4 will have nu
# if any part is missing, the value in m will be zero.
#
component_i <- 1 #start the loop with the first component
m_i <- 1 #start processing from the beginning element of m[[1]][]
seq <- 2 #The vlue of the next row's XVSEQ
while (component_i <= component_reps)
{
if (m[[1]][m_i+2] > 0)
{
# we have nuc
XVrow <- data.frame(STUDYID = NA,
DOMAIN = "XV",
XVSEQ = seq,
XVGRPID = 1,
XVSGRPID = component_i,
XVPARMCD = "TRTVC",
XVPARM = "Treatment Vehicle Component",
XVSPARM = TRTVtokens$Value[xref[2+m[[1]][m_i+2]]], #Since we have nuc, 2 tokens from here is c
XVVAL = TRTVtokens$Value[xref[0+m[[1]][m_i+2]]], #Since we have nuc, 0 tokens from here is n
XVVALU = TRTVtokens$Value[xref[1+m[[1]][m_i+2]]], #Since we have nuc, 1 token from here is u
XVVALNF = NA,
stringsAsFactors=FALSE)
XVtable <- rbind(XVtable,XVrow)
seq <-seq +1
}
if ((m[[1]][m_i+3]>0)&&(m[[1]][m_i+4]>0))
{
# we have cnu
XVrow <- data.frame(STUDYID = NA,
DOMAIN = "XV",
XVSEQ = seq,
XVGRPID = 1,
XVSGRPID = component_i,
XVPARMCD = "TRTVC",
XVPARM = "Treatment Vehicle Component",
XVSPARM = TRTVtokens$Value[xref[0+m[[1]][m_i+3]]], #Since we have cnu, 0 tokens from here is c
XVVAL = TRTVtokens$Value[xref[1+m[[1]][m_i+3]]], #Since we have cnu, 1 tokens from here is n
XVVALU = TRTVtokens$Value[xref[2+m[[1]][m_i+3]]], #Since we have cnu, 2 tokens from here is u
XVVALNF = NA,
stringsAsFactors=FALSE)
XVtable <- rbind(XVtable,XVrow)
seq <-seq +1
}
if ((m[[1]][m_i+3]>0)&&(m[[1]][m_i+4]==0))
{
# we have c
XVrow <- data.frame(STUDYID = NA,
DOMAIN = "XV",
XVSEQ = seq,
XVGRPID = 1,
XVSGRPID = component_i,
XVPARMCD = "TRTVC",
XVPARM = "Treatment Vehicle Component",
XVSPARM = TRTVtokens$Value[xref[0+m[[1]][m_i+3]]], #Since we have c, 0 tokens from here is c
XVVAL = NA,
XVVALU = NA,
XVVALNF = NA,
stringsAsFactors=FALSE)
XVtable <- rbind(XVtable,XVrow)
seq <-seq +1
}
#print(XVtable)
m_i <-m_i +4 #There are 4 items in m for each component.
component_i <- component_i +1
}
#Add rows to XVtable for diluent f((nuc)|(c(nu)?))
if (m[[1]][m_i+1] >0)
{
#We havea diluent. The code for a component above is copied here. I should have created a function.
m_i <- m_i +1
if (m[[1]][m_i+2] > 0)
{
# we have nuc
XVrow <- data.frame(STUDYID = NA,
DOMAIN = "XV",
XVSEQ = seq,
XVGRPID = 1,
XVSGRPID = component_i,
XVPARMCD = "TRTVC",
XVPARM = "Treatment Vehicle Component",
XVSPARM = TRTVtokens$Value[xref[2+m[[1]][m_i+2]]],
XVVAL = TRTVtokens$Value[xref[0+m[[1]][m_i+2]]],
XVVALU = TRTVtokens$Value[xref[1+m[[1]][m_i+2]]],
XVVALNF = NA,
stringsAsFactors=FALSE)
XVtable <- rbind(XVtable,XVrow)
seq <-seq +1
}
if ((m[[1]][m_i+3]>0)&&(m[[1]][m_i+4]>0))
{
# we have cnu
XVrow <- data.frame(STUDYID = NA,
DOMAIN = "XV",
XVSEQ = seq,
XVGRPID = 1,
XVSGRPID = component_i,
XVPARMCD = "TRTVC",
XVPARM = "Treatment Vehicle Component",
XVSPARM = TRTVtokens$Value[xref[0+m[[1]][m_i+3]]],
XVVAL = TRTVtokens$Value[xref[1+m[[1]][m_i+3]]],
XVVALU = TRTVtokens$Value[xref[2+m[[1]][m_i+3]]],
XVVALNF = NA,
stringsAsFactors=FALSE)
XVtable <- rbind(XVtable,XVrow)
seq <-seq +1
}
if ((m[[1]][m_i+3]>0)&&(m[[1]][m_i+4]==0))
{
# we have c
XVrow <- data.frame(STUDYID = NA,
DOMAIN = "XV",
XVSEQ = seq,
XVGRPID = 1,
XVSGRPID = component_i,
XVPARMCD = "TRTVC",
XVPARM = "Treatment Vehicle Component",
XVSPARM = TRTVtokens$Value[xref[0+m[[1]][m_i+3]]],
XVVAL = NA,
XVVALU = NA,
XVVALNF = "QS",
stringsAsFactors=FALSE)
XVtable <- rbind(XVtable,XVrow)
seq <-seq +1
}
#print(XVtable)
m_i <-m_i +4
}
#Add rows to XVtable for the attributes (an(r|(R)n)?)
attribute_i <- 1
while (attribute_i <= attribute_reps)
{
if (m[[1]][m_i+3]>0)
{
#we have an "anRn"
#create row for "an"
lowest <- as.numeric(TRTVtokens$Value[xref[1+m[[1]][m_i+1]]])
highest <- as.numeric(TRTVtokens$Value[xref[3+m[[1]][m_i+1]]])
XVrow <- data.frame(STUDYID = NA,
DOMAIN = "XV",
XVSEQ = seq,
XVGRPID = 1,
XVSGRPID = component_i,
XVPARMCD = "TRTVP",
XVPARM = "Treatment Vehicle Property",
XVSPARM = TRTVtokens$Value[xref[0+m[[1]][m_i+1]]],
XVVAL = (highest+lowest)/2,
XVVALU = NA,
XVVALNF = NA,
stringsAsFactors=FALSE)
XVtable <- rbind(XVtable,XVrow)
seq <-seq +1
#create row for "Rn"
XVrow <- data.frame(STUDYID = NA,
DOMAIN = "XV",
XVSEQ = seq,
XVGRPID = 1,
XVSGRPID = component_i,
XVPARMCD = "TRTVC",
XVPARM = "Treatment Vehicle Component",
XVSPARM = paste0(TRTVtokens$Value[xref[0+m[[1]][m_i+1]]]," Tolerance Range"),
XVVAL = (highest-lowest)/2,
XVVALU = NA,
XVVALNF = NA,
stringsAsFactors=FALSE)
XVtable <- rbind(XVtable,XVrow)
seq <-seq +1
}
else
{
if (m[[1]][m_i+2]>0)
{
#we have an "anrn"
#create row for "an"
XVrow <- data.frame(STUDYID = NA,
DOMAIN = "XV",
XVSEQ = seq,
XVGRPID = 1,
XVSGRPID = component_i,
XVPARMCD = "TRTVP",
XVPARM = "Treatment Vehicle Property",
XVSPARM = TRTVtokens$Value[xref[0+m[[1]][m_i+1]]],
XVVAL = TRTVtokens$Value[xref[1+m[[1]][m_i+1]]],
XVVALU = NA,
XVVALNF = NA,
stringsAsFactors=FALSE)
XVtable <- rbind(XVtable,XVrow)
seq <-seq +1
#create row for "rn"
XVrow <- data.frame(STUDYID = NA,
DOMAIN = "XV",
XVSEQ = seq,
XVGRPID = 1,
XVSGRPID = component_i,
XVPARMCD = "TRTVC",
XVPARM = "Treatment Vehicle Component",
XVSPARM = paste0(TRTVtokens$Value[xref[0+m[[1]][m_i+1]]]," Tolerance Range"),
XVVAL = TRTVtokens$Value[xref[3+m[[1]][m_i+1]]],
XVVALU = NA,
XVVALNF = NA,
stringsAsFactors=FALSE)
XVtable <- rbind(XVtable,XVrow)
seq <-seq +1
}
else
{
if ((m[[1]][m_i+1]>0))
{
#we have "an" without "rn" or "Rn"
#create row for "an"
XVrow <- data.frame(STUDYID = NA,
DOMAIN = "XV",
XVSEQ = seq,
XVGRPID = 1,
XVSGRPID = component_i,
XVPARMCD = "TRTVP",
XVPARM = "Treatment Vehicle Property",
XVSPARM = TRTVtokens$Value[xref[0+m[[1]][m_i+1]]],
XVVAL = TRTVtokens$Value[xref[1+m[[1]][m_i+1]]],
XVVALU = NA,
XVVALNF = NA,
stringsAsFactors=FALSE)
XVtable <- rbind(XVtable,XVrow)
seq <-seq +1
}
}
}
m_i <-m_i +3
attribute_i <- attribute_i+1
}
} else
{
return(NA)
}
return(XVtable)
}
#####################################################################################################33
#
#pathBase = "C:\\003\\TRTV-parse\\"
debug <- TRUE
pathBase <- "C:\\Project\\src\\R\\TRTV\\"
logFile <- paste0(pathBase,"output\\trtv-parse.log")
OutCsvFile <- NA
# read the table of Treatment Vehicles that need to be parsed. Each line is a copy of the TRTV parameer from ts.xpt in a SEND package
trtv <- read.table(paste0(pathBase,"trtv"),sep="|");
# Create a log file for debug
if (debug){
sink(logFile, append=TRUE)
}
#read the table of component names that are already sorted from longest text string to shortest text string.
tokens <- read.table(paste0(pathBase,"tokens.csv"),sep=",", header = TRUE,stringsAsFactors=FALSE);
tRow <- 1
nSuccess <- 0
while (tRow <= nrow(trtv))
{
#print(paste0("Vehicle:",as.character(trtv[tRow, ])))
p<-vehicleTokenize(as.character((trtv[tRow, ])))
p<-cleanTokens_syn(cleanTokens_R(cleanTokens_ur(p)))
#print(p$Category)
category_noU <- str_replace_all(paste(p$Category,collapse=""),"U|i","")
#print(grepl("^((nuc)|(c(nu)?))+(f((nuc)|(c(nu)?)))?(an(rn)?)*$",category_noU))
t<-makeXV(p)
if (is.null(ncol(t)))
{
print(paste0("Failed! Row:", tRow, "Vehicle: ", p))
#print(p)
}
else
{
#print(t)
OutCsvFile <- paste0(pathBase,"output\\xv",tRow, ".csv")
write.csv(t,file=OutCsvFile,na="",row.names=FALSE)
nSuccess <- nSuccess +1
}
#print("")
tRow <-tRow+1
}
print(paste0("We eavluated ",nrow(trtv)," treatment vehicle descriptions and created XV tables for ",nSuccess," of them = ",100*nSuccess/nrow(trtv),"%"))
# revert sink back to console
sink(type = "message")
sink()
source("C:/Project/src/R/TRTV/parseTRTV.R")
sink(type = "message")
sink()
source("C:/Project/src/R/TRTV/parseTRTV.R")
source("C:/Project/src/R/TRTV/parseTRTV.R")
source("C:/Project/src/R/TRTV/parseTRTV.R")
source("C:/Project/src/R/TRTV/parseTRTV.R")
source("C:/Project/src/R/TRTV/parseTRTV.R")
source("C:/Project/src/R/TRTV/parseTRTV.R")
source("C:/Project/src/R/TRTV/parseTRTV.R")
source("C:/Project/src/R/TRTV/parseTRTV.R")
source("C:/Project/src/R/TRTV/parseTRTV.R")
source("C:/Project/src/R/TRTV/parseTRTV.R")
source("C:/Project/src/R/TRTV/parseTRTV.R")
source("C:/Project/src/R/TRTV/parseTRTV.R")