R/Utils.R
2fec20fe
 
4ad24353
 .counter <- function(zero = 0) {
94a33e57
     i <- zero
     function() {
         i <<- i + 1
         toString <- as.character(i)
     }
193ae520
 }
 
4ad24353
 .add_metadata <- function(files) {
94a33e57
     x <- scan(files, what="", sep="\n")
     y <- strsplit(x, "\t")
40ec852b
     names(y) <- vapply(y, `[[`,character(1), 1)
94a33e57
     listMeta <- lapply(y, `[`, -1)
193ae520
 }
 
4ad24353
 .schema_header <- function(datasetName) {
94a33e57
     schema_name <- list.files(datasetName, pattern = "*.schema$",
                                 full.names = TRUE)
dda0cfa0
     
971c90b9
     schema_name_xml <- list.files(datasetName, pattern = "*.xml$",
                                 full.names = TRUE)
     
40ec852b
     if(!length(schema_name) && !length(schema_name_xml))
94a33e57
         stop("schema not present")
45ada540
     
dda0cfa0
     if(!length(schema_name))
         xml_schema <- xml2::read_xml(schema_name_xml)
     else
         xml_schema <- xml2::read_xml(schema_name)
     
94a33e57
     list_field <- xml2::as_list(xml_schema)
     vector_field <- unlist(list_field)
193ae520
 }
6ac6e0b7
 
4ad24353
 .schema_type_coordinate <- function(datasetName) {
a5e2122b
     schema_name <- list.files(datasetName, pattern = "*.schema$",
                               full.names = TRUE)
     
     schema_name_xml <- list.files(datasetName, pattern = "*.xml$",
                                   full.names = TRUE)
     
     if(!length(schema_name) && !length(schema_name_xml))
         stop("schema not present")
     
     if(!length(schema_name))
         xml_schema <- xml2::read_xml(schema_name_xml)
     else
         xml_schema <- xml2::read_xml(schema_name)
     
     gmql_schema_tag <- xml2::xml_children(xml_schema)
     all_attrs <- xml2::xml_attrs(gmql_schema_tag)
     all_attrs_list <- as.list(all_attrs[[1]])
 }
 
43b72c07
 # aggregates factory
4ad24353
 .aggregates <- function(meta_data,class) {
94a33e57
     if(!is.list(meta_data))
         stop("meta_data: invalid input")
45ada540
     
40ec852b
     if(!all(vapply(meta_data, function(x) is(x,class), logical(1))))
94a33e57
         stop("All elements must be META_AGGREGATES object")
45ada540
     
94a33e57
     names <- names(meta_data)
     if(is.null(names))
     {
4f1ecbce
         warning("You did not assign a names to a list.\nWe build it for you")
40ec852b
         names <- vapply(meta_data, take_value.META_AGGREGATES,character(1))
94a33e57
     }
     else
     {
         if("" %in% names)
45ada540
             stop("No partial names assignment is allowed")
94a33e57
     }
40ec852b
     aggregate_matrix <- t(vapply(meta_data, function(x) {
94a33e57
         new_value = as.character(x)
         matrix <- matrix(new_value)
40ec852b
     },character(2)))
45ada540
     
94a33e57
     m_names <- matrix(names)
     metadata_matrix <- cbind(m_names,aggregate_matrix)
43b72c07
 }
 
 
94a33e57
 # meta join condition
4ad24353
 .join_condition <- function(cond) {
c93d8511
     cond_matrix <- NULL
0ad65e7f
     def <- cond$condition$def
c93d8511
     if(!is.null(def))
         cond_matrix <- rbind(cond_matrix, def)
     
0ad65e7f
     exact <- cond$condition$exact
c93d8511
     if(!is.null(exact))
         cond_matrix <- rbind(cond_matrix, exact)
     
0ad65e7f
     full <- cond$condition$full
c93d8511
     if(!is.null(full))
         cond_matrix <- rbind(cond_matrix, full)
     cond_matrix
bdfe862e
 }
2deed4ee
 
4ad24353
 .check_input <- function(value) {
94a33e57
     if(!is.character(value))
         stop("no valid data")
45ada540
     
94a33e57
     if(length(value)>1)
         stop("no multiple string")
8dc4d3af
 }
2deed4ee
 
4ad24353
 .check_logical <- function(value) {
94a33e57
     if(!is.logical(value))
         stop("no valid data")
45ada540
     
94a33e57
     if(length(value)>1)
         stop("no multiple string")
8dc4d3af
 }
18697071
 
4ad24353
 .is_login_expired <- function(url) {
     if(exists("GMQL_credentials", envir = .GlobalEnv)) {
         if(exists("authToken", where = GMQL_credentials)) {
18697071
             authToken <- GMQL_credentials$authToken
             url <- sub("/*[/]$","",url)
             h <- c('Accept' = 'Application/json', 'X-Auth-Token' = authToken)
             URL <- paste0(url,"/user")
             req <- httr::GET(URL,httr::add_headers(h))
4ad24353
             if(req$status_code != 200)
18697071
                 return(TRUE)
             else
                 return(FALSE)
         }
     }
     return(TRUE)
 }