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 | #' Load GEO Dataset.
#'
#' \code{loadPreloaded} returns the file with serialized ExpressionSets using
#' ProtoBuf, that were preloaded on server.
#'
#' @param name String, containing filename. Assuming
#' that in the directory with preloaded files \code{preloadedDir}
#' exists file \code{filename.rda} with list of ExpressionSets \code{ess}.
#'
#' @param exactName If you know, that inside file is object with name
#' \code{exactName}, you can specify it to load only this object.
#' Otherwise, whole file will be loaded.
#'
#' @return File with ProtoBuf-serialized ExpressionSet-s
#' that were loaded from specified file.
#'
#' @import Biobase
loadPreloaded <- function(name, exactName = NULL) {
preloadedDir <- getOption("phantasusPreloadedDir")
if (is.null(preloadedDir)) {
stop("Specify the directory with presaved files")
} else if (!dir.exists(preloadedDir)) {
stop("No such directory")
}
fileToLoad <- file.path(preloadedDir, paste0(name, '.rda'))
if (file.exists(fileToLoad)) {
x <- load(fileToLoad) # must return the object ess
loaded <- get(x)
wrongFormat <- paste("Wrong format.",
"File must contain either ExpressionSet",
"or list of ExpressionSets")
ess <- NULL
if (class(loaded) == "ExpressionSet") {
ess <- list()
ess[[x]] <- loaded
} else if (class(loaded) != "list") {
stop(wrongFormat)
} else {
ess <- loaded
}
files <- list()
seriesNames <- names(ess)
if (is.null(seriesNames)) {
seriesNames <- paste0(name, "_", 1:length(ess))
}
if (!is.null(exactName) && exactName == name) {
exactName <- NULL
}
if (!is.null(exactName) && !(exactName %in% seriesNames)) {
stop("There is not such object in this file")
}
if (!is.null(exactName)) {
ess <- list(ess[[exactName]])
}
for (i in 1:length(ess)) {
if (class(ess[[i]]) != "ExpressionSet") {
stop(wrongFormat)
}
assign("es", ess[[i]], envir = parent.frame())
files[[seriesNames[[i]]]] <- writeToList(ess[[i]])
}
f <- tempfile(pattern = "gse", tmpdir = getwd(), fileext = ".bin")
writeBin(protolite::serialize_pb(files), f)
jsonlite::toJSON(basename(f))
} else {
stop("No such file")
}
}
#' Check existence of phantasusPreloadedDir
#'
#' \code{preloadedDirExists} checks if there is specified
#' directory with preloaded files.
#'
#' @return Boolean value.
preloadedDirExists <- function() {
preloadedDir <- getOption("phantasusPreloadedDir")
jsonlite::toJSON(!is.null(preloadedDir) && dir.exists(preloadedDir))
}
#' Check names inside preloaded file
#'
#' \code{checkPreloadedNames} checks names of ExpressionSets that
#' are included in file \code{name}
#'
#' @param name String, containing filename. Assuming
#' that in the directory with preloaded files \code{preloadedDir}
#' exists file \code{filename.rda} with list of ExpressionSets \code{ess}.
#'
#' @return Vector of names serialized in JSON format.
#'
checkPreloadedNames <- function(name) {
if (!jsonlite::fromJSON(preloadedDirExists())) {
stop("No such directory")
}
preloadedDir <- getOption("phantasusPreloadedDir")
fileToLoad <- file.path(preloadedDir, paste0(name, '.rda'))
if (!file.exists(fileToLoad)) {
fileToLoadGct <- file.path(preloadedDir, paste0(name, '.gct'))
if (!file.exists(fileToLoadGct)) {
stop("No such file")
}
es <- read.gct(fileToLoadGct)
ess <- list(es=es)
names(ess) <- name
save(ess, file=fileToLoad, compress = TRUE)
}
x <- load(fileToLoad) # must return the object ess
loaded <- get(x)
answer <- c()
if (class(loaded) == "ExpressionSet") {
answer <- c(x)
} else if (class(loaded) == "list") {
if (!is.null(names(loaded))) {
answer <- names(loaded)
}
else {
if (length(loaded) > 1) {
answer <- paste0(name, "_", 1:length(loaded))
} else {
answer <- c(name)
}
}
} else {
wrongFormat <- paste("Wrong format.",
"File must contain either ExpressionSet",
"or list of ExpressionSets")
stop(wrongFormat)
}
jsonlite::toJSON(answer)
}
|