valid_variables_script.R 13.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
rm(list=ls())

dir_name <- readline("Introduce the name of the directory please: ")

setwd(dir_name)

source("dependency_installer.R")


dep_list = c("jsonlite", "stringr","DSI","DSOpal","DSLite", "fields", "metafor", "ggplot2", "gridExtra", "data.table", "dsBaseClient", "openxlsx")
install_dependencies(dep_list)

#source("connection_parameters.R")
#source("necessary_functions_connection.R")

16
codebook <- read.csv("new_harmon.csv" , sep = ",")
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

codebook_col_names <- as.data.frame(codebook$Harmonised.variable.name)

names(codebook_col_names) <- c("col_names")

setwd(paste(dir_name ,"/harmonized_data", sep=""))

file_name <- readline("Introduce the name of the file to check the values: ")
harmonized_data <- ""

if (grepl(".csv" , file_name , fixed = TRUE)){
  harmonized_data <- read.csv(file_name)
}else if (grepl(".xlsx" , file_name , fixed = TRUE)){
  harmonized_data <- read.xlsx(file_name)
}


categoric_vars = c("DMRGENDR", "DMRBORN", "DMRRETH1", "DMROCCU", "DMRHREDU", "DSXOS", "DSXHO", "DSXIC", "TRXAV","TRXRIB","TRXLR","TRXRM","TRXIA","TRXIB","TRXCH","TRXAB","TRXCS","TRXHEP","TRXAF","TRXCP","TRXOT","TRXECM","TRXIV","TRXNIV","TRXNO","TRXOX","TRXRR","TRXTR","TRXVA","TRXPE","TRXPV","TRXIT","TRXNMB","TRXAC","TRXINA","TRXIS","TRXIM","TRXVC","TRXVD","TRXZN",                         "CSXCOT","CSXCTR","SMXASAH","SMXFEA","SMXCOA","SMXSTA","SMXSBA","SMXRNA","SMXMYA","SMXARA","SMXCPA","SMXAPA","SMXINA","SMXNAA","SMXDIA","SMXFAA","SMXHEA","SMXCNA","SMXACA","SMXSLA","SMXTLA","SMXSYA","SMXWHA","SMXLYA","SMXANA","SMXIWA","SMXSRA","SMXBLA","CMXPRG","CMXCVD","CMXCMP","CMXHT","CMXDI","CMXCKD","CMXCLD","CMXCPD","CMXASM","CMXCND","CMXRHE","CMXCCI","CMXCBD","CMXDE","CMXPU","CMXST","CMXLY","CMXAP","RFXSM","RFXFSM","RFXOB","RFXTB","RFXIMD","RFXHIV","RFXAIDS","RFXUI","RFXHC","RFXONC","RFXMN",                         "HMRACI","HMRARB","HMRAHO","HMRNS","HMROS","HMRCS","HMRIS","HMRAV","HMRAB","HMRCOV","IMDXCT","IMDXCTCR","IMDXCTTE","IMDXCTAB","IMDXXR","IMDXPN",                         "COXRD","COXAR","COXPM","COXMOD","COXPT","COXEC","COXSH","COXIO","COXPE","COXST","COXDIC","COXRIO","COXKF","COXHF","COXBC")


#----------------------------------------------------------------------------

#Test if column names are valid
check_column_names <- function(col_names){
  
  str_res <- "The column names:"
  valid_colnames <- c()
44
  repeated_colnames <- c()
45 46
  for(i in 1:(nrow(col_names))){
    col_name <- col_names[i,1]
47 48
    number_of_column <- check_valid_name(col_name) 
    if( number_of_column == 0){
49
      str_res<- paste(str_res, col_name, sep=" ")
50
    }else if (number_of_column == 1){
51
      valid_colnames = c(valid_colnames, col_name)
52 53
    }else{
      repeated_colnames = c(repeated_colnames , col_name)
54 55 56 57 58
    }
  }
  
  str_res<- paste(str_res,"are not registered in the harmonized data codebook \n", sep=" ")
  
59
  new_list <- list("not_colnames" = str_res , "colnames" = valid_colnames , "repeated_colnames" = repeated_colnames)
60 61 62 63 64 65 66
  return (new_list)
  
}

#Test if a single variable name is valid
check_valid_name <- function(col_name){
  
67
  valid <- 0
68
  
69 70 71 72 73
  if(col_name %in% codebook_col_names$col_names){
    
    valid <- length(grep(col_name, names(harmonized_data)))
  
  }
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
  
  return (valid)
  
}

remove_space <- function(x){
  searchString <- ' '
  replacementString <- ''
  res = sub(searchString,replacementString,x)
  return(res)
}

remove_spaces_from_ds <- function(ds){
  
  res<- lapply(ds,remove_space )
  
  return(as.data.frame(res))
  
}

is_number <- function(x){
  res <- FALSE
  
  
  if(length(x)!=0){
    x <- str_replace(x,",",".")
    
    aux <- as.numeric(x)
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
    
    if(!is.na(aux))
      res <- TRUE
  }
  
  
  return(res)
  
}

check_values_not_categoric <- function(values, colname){
  
  valid_vals <- values
  
  possible_vals <- possible_values(colname)
  
  for(i in  1:length(values)){
    
    res<- FALSE
    
    value <- values[[i]]
    
    if(is_number(value)){
      value <- str_replace(value,",",".")
      value <- as.numeric(value)
    }
    
    if(is.null(value)){
      res <- TRUE
    }
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
    else if( value == "NA" | value == "nan" | value == ".")
      res <- TRUE
    else{
      
      if(nrow(possible_vals) == 2 & (!grepl("DAT",colname, fixed=TRUE)) & colname !="CSXCTR"){
        
        if(colname=="LBXBEH" | colname=="LBXBEHn" | colname=="LBXBEM"){
          lower = possible_vals[1,1][[1]]
          higher = possible_vals[2,1][[1]]
          
        }else{
          bounds <- as.data.frame(strsplit(possible_vals[1,1], '-'))
          lowerAux <- str_replace(bounds [1,1],",",".")
          higherAux <- str_replace(bounds [2,1],",",".")
          lower <- as.numeric(remove_space(lowerAux))
          higher <- as.numeric(remove_space(higherAux))
        }
        
        
        if ((value >= lower & value <= higher))
          res <- TRUE
      }
      
      if(nrow(possible_vals) == 3 | colname=="CSXCTR"){
        if(value == 0 | value == 1)
          res <- TRUE
      }
      
      if(nrow(possible_vals) > 3){
        
        lower <- strtoi(remove_space(possible_vals[1,1]))
        higher_b <- nrow(possible_vals)-1
        higher <- strtoi(remove_space(possible_vals[higher_b,1]))
        
        if ((value >= lower & value <= higher))
          res <- TRUE
      }
    }
    
    if(res == FALSE)
      valid_vals[i] <- res
    
    
  }
  
  return(valid_vals)
}

possible_values <- function(x){
  
  if(x=="LBXBEH" | x=="LBXBEHn" | x=="LBXBEM")
    res <- t(as.data.frame(list(-20,20)))
  
  else{
    
    possible_value <- subset(harmonized_data,harmonized_data$harmonized.variable.name==x)[1,5]
    res <- strsplit(x=possible_value,split="/")
  }
  
  return(as.data.frame(res))
}

possible_values_categoric <- function(x){
  
  possible_value <- subset(harmonized_data,harmonized_data$harmonized.variable.name==x)[1,4]
  res <- strsplit(x=possible_value,split="/")
  
  return(as.data.frame(res))
}

check_values_categoric <- function(values, colname){
  
  possible_vals <- possible_values_categoric(colname)
  
  res <- TRUE
  
  for(i in 1:length(values)){
    if(!(values[[i]] %in% as.matrix(remove_spaces_from_ds(possible_vals)))){
      res <- FALSE
    }
    
  }
  
  return(res)
  
}


get_values_from_quantiles <- function(x){
  
  data_summary <- summary(x)
  
  low_quantile <- data_summary[[1]][3][[1]][[1]]
  
  high_quantile <- data_summary[[1]][3][[1]][[7]]
  
  return(list(low_quantile,high_quantile))
  
  
}

notify_error <- function(invalid_name_list,invalid_value_list,wrong_categoric, wrong_categoric_values, missing_numeric){
  
  res <- ""
  
  if(length(invalid_name_list) !=0){
    res <- "There are invalid values in the numeric fields:"
    
    
    for(i in 1:length(invalid_name_list)){
      res <- paste(res, invalid_name_list[i], sep=" ")
    }
    
    res <- paste(res, "\n", sep="")
    
    for(i in 1:length(invalid_name_list)){
      res <- paste(res, error_message(invalid_name_list[i], invalid_value_list[[i]]), sep=" ")
    }
  }
  
  
  if(length(wrong_categoric)!=0){
    
    res <- paste(res, "\n############################################################################ \n", sep="")
    
    res <- paste(res,"\nThe following categoric values are invalid:", sep=" ")
    
    for(i in 1:length(wrong_categoric)){
      res <- paste(res, wrong_categoric[i], sep=" ")
    }
    
    res <- paste(res, "\n", sep="")
    
    for(i in 1:length(wrong_categoric)){
      res <- paste(res, error_message_categoric(wrong_categoric[i], wrong_categoric_values[[i]]), sep=" ")
    }
  }
  
  if(length(missing_numeric)!=0){
    
    res <- paste(res, "\n############################################################################ \n", sep="")
    
    res<- paste(res, "\nThe following fields are missing a numeric field:")
    
    for(i in 1:length(wrong_categoric)){
      res <- paste(res, missing_numeric[i], sep=" ")
    }
  }
  
  res <- paste(res, "\n", sep="")
  
  
  return(res)
  
}

error_message_categoric <- function(colname, invalid_values){
  
  res<- "\nValues in the field"
  res<- paste(res, colname, sep=" ")
  res<- paste(res, "should be", sep=" ")
  
  range <- possible_values_categoric(colname)
  
  for(i in 1:nrow(range)){
    res <- paste(res, remove_space(range[i,1]), sep=" ")
  }
  
  
  res<- paste(res, "\nBut values were:", sep=" ")
  
  for(j in 1:length(invalid_values)){
    res<- paste(res, invalid_values[[j]], sep=" ")
  }
  
  res<- paste(res, "\n\n", sep="")
  
  
  return(res)
  
}

error_message <- function(colname, invalid_values){
  
  res<- "\nValues in the field"
  res<- paste(res, colname, sep=" ")
  res<- paste(res, "should be", sep=" ")
  
  if(grepl("numeric", colname,fixed=TRUE))
    new_colname <- strsplit(x=colname,split="_")[[1]][1]
  else
    new_colname <- colname
  
  range <- subset(harmonized_data, harmonized_data$harmonized.variable.name == new_colname)
  range <- range[5]
  range <- as.data.frame(strsplit(range[1,1], '/'))
  
  #Range of values or null
  if(nrow(range) == 2 & !grepl("DAT",colname, fixed=TRUE)){
    bounds <- as.data.frame(strsplit(range[1,1], '-'))
    lower <- remove_space(bounds [1,1])
    higher <- remove_space(bounds [2,1])
    res<- paste(res, "numbers between", sep=" ")
    res<- paste(res, lower, sep=" ")
    res<- paste(res, "and", sep=" ")
    res<- paste(res, higher, sep=" ")
    res<- paste(res, "(both included)", sep=" ")
  }
  
  if(nrow(range) == 3){
    res<- paste(res, "0 or 1", sep=" ")
  }
  
  if(nrow(range) > 3){
    lower <- strtoi(remove_space(range[1,1]))
    higher_b <- nrow(range)-1
    higher <- strtoi(remove_space(range[higher_b,1]))
    res<- paste(res, "numbers between", sep=" ")
    res<- paste(res, lower, sep=" ")
    res<- paste(res, "and", sep=" ")
    res<- paste(res, higher, sep=" ")
    res<- paste(res, "(both included)", sep=" ")
  }
  
  if(grepl("DAT",colname, fixed=TRUE)){
    res<- paste(res, "dates with the following format: dd/mm/yyyy", sep=" ")
  }
  
  
  res<- paste(res, "\nBut values were:", sep=" ")
  
  for(j in 1:length(invalid_values)){
    res<- paste(res, invalid_values[[j]], sep=" ")
  }
  
  res<- paste(res, "\n", sep="")
  
  if(!is_number(invalid_values[[1]]))
    res<- paste(res, "(It's missing a \"numeric\" field)", sep="")
  
  res<- paste(res, "\n", sep="")
  
  return(res)
  
}

380
check_valid_values <- function(valid_colnames){
381 382 383 384 385 386 387 388 389 390 391
  
  invalid_name_list <- c()
  cannot_analyse_list <- c()
  invalid_values_list <- list()
  wrong_categoric_values <- list()
  wrong_categoric <- c()
  missing_numeric <- c()
  j<- 1
  k <- 1
  
  for(i in  1:(nrow(valid_colnames))){
392 393 394 395 396 397
    name <- names(valid_colnames_with_data)[i]
    if("DMRBORN" == name | grepl("DAT",colname, fixed=TRUE) | "ISO" == name | "BEF" == name){
      next
    }
    
    column <- valid_colnames[,i]
398
    
399
    data_table <- as.data.frame(table(column))
400
    
401 402 403 404 405
    values <- row.names(data_table)
    
    numeric_col<- paste(valid_colnames[,i],"_numeric", sep="")
    
    if( name %in% categoric_vars ){
406
      
407 408
      #is_numeric <- grepl("numeric",valid_colnames[i,1], fixed=TRUE)
      has_numeric <-  numeric_col %in% valid_colnames$`valid_data_colnames(data_colnames)`
409
      
410 411
      if(!has_numeric)
        missing_numeric <- c(missing_numeric, valid_colnames[i,1])
412
      
413 414
      
      if(!check_values_categoric(values,valid_colnames[i,1])){
415
        
416 417
        print("Wrong categoric value:")
        print(valid_colnames[i,1])
418
        
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
        wrong_categoric <- c(wrong_categoric, valid_colnames[i,1])
        wrong_categoric_values[[k]] <- values
        k <- k+1
      }
      
    }else{
      
      if(grepl("numeric", valid_colnames[i,1],fixed=TRUE))
        new_colname <- strsplit(x=valid_colnames[i,1],split="_")[[1]][1]
      else
        new_colname <- valid_colnames[i,1]
      
      valid <- check_values_not_categoric(values, new_colname)
      
      if (FALSE %in% valid){
        invalid_name_list <- c(invalid_name_list,valid_colnames[i,1])
        invalid_values_list[[j]] <- values
        j <- j+1
437 438 439 440
      }
      
      
    }
441 442 443 444
    
    
    
    
445 446 447 448 449 450 451 452 453 454 455 456 457 458
  }
  
  missing_numeric
  if(length(invalid_name_list)>0 | length(wrong_categoric)>0 | length(missing_numeric)>0){
    res <- notify_error(invalid_name_list, invalid_values_list, wrong_categoric, wrong_categoric_values, missing_numeric)
  }else{
    res <- "All values are valid \n"
  }
  
  if(length(cannot_analyse_list)>0){
    res <- paste(res, "\n############################################################################ \n", sep="")
    
    res <- paste(res, notify_unable_analyse(cannot_analyse_list), sep="\n" )
  }
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
  
  
  
  return(res)
  
}

notify_unable_analyse <- function(x){
  
  res <- "\nCould not obtain data from the fields:"
  
  for(i in 1:length(x)){
    res <- paste(res, x[i], sep=" ")
  }
  
  return (res)
  
  
}

data_colnames <- as.data.frame(colnames(harmonized_data))

check_valid_columns <- check_column_names(data_colnames)

columns_not_valid <- check_valid_columns$not_colnames

valid_colnames <- as.data.frame(check_valid_columns$colnames)
names(valid_colnames) = c("valid_colnames")
488
valid_colnames_with_data <- subset(harmonized_data , select = valid_colnames$valid_colnames)
489 490 491


result <- ""
492
result<-check_valid_values(valid_colnames_with_data)
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
print(check_valid_columns)
#datashield.logout(connections) 
cat(result)



file_name<- paste(hospital_name,"_invalid_values.txt", sep="")


dir.create("../invalid_values", showWarnings = FALSE)
setwd("../invalid_values")

cat(check_valid_columns,file=file_name,sep="\n")
cat(result,file=file_name,append=TRUE)

#datashield.logout(connections) 
509