-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathlabkey.experiment.R
More file actions
235 lines (192 loc) · 7.92 KB
/
labkey.experiment.R
File metadata and controls
235 lines (192 loc) · 7.92 KB
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
##
# Copyright (c) 2018 LabKey Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##
labkey.experiment.SAMPLE_DERIVATION_PROTOCOL <- "Sample Derivation Protocol"
## Create an ExpData object
##
labkey.experiment.createData <- function(config, dataClassId = NULL, dataClassName = NULL, dataFileUrl = NULL)
{
## check required parameters
if (missing(config))
stop (paste("A list of ExpObject config params must be specified in the config parameter."))
data <- config
if (!is.null(dataFileUrl))
data$dataFileURL = dataFileUrl
if (!is.null(dataClassId) || !is.null(dataClassName))
{
dataClass <- list()
if (!is.null(dataClassId))
dataClass$id = dataClassId
if (!is.null(dataClassName))
dataClass$name = dataClassName
data$dataClass = dataClass
}
return (data)
}
## Create an ExpMaterial object
##
labkey.experiment.createMaterial <- function(config, sampleSetId = NULL, sampleSetName = NULL)
{
## check required parameters
if (missing(config))
stop (paste("A list of ExpObject config params must be specified in the config parameter."))
material <- config
if (!is.null(sampleSetId) || !is.null(sampleSetName))
{
sampleSet <- list()
if (!is.null(sampleSetId))
sampleSet$id = sampleSetId
if (!is.null(sampleSetName))
sampleSet$name = sampleSetName
material$sampleSet = sampleSet
}
return (material)
}
## Create an ExpRun object
##
labkey.experiment.createRun <- function(config, dataInputs = NULL, dataOutputs = NULL, dataRows = NULL, materialInputs = NULL, materialOutputs = NULL)
{
## check required parameters
if (missing(config))
stop (paste("A list of ExpObject config params must be specified in the config parameter."))
run <- config
## validate run properties if provided
if (!is.null(run$properties) && !is.list(run$properties))
stop (paste("config properties must be a list of key value pairs."))
if (!is.null(dataInputs))
{
if (!is.list(dataInputs))
stop (paste("dataInputs must be a list of data objects, see labkey.experiment.createData."))
## ensure dataInputs is serialized as an array of objects
run$dataInputs = ensureNestedList(dataInputs)
}
if (!is.null(dataOutputs))
{
if (!is.list(dataOutputs))
stop (paste("dataOutputs must be a list of data objects, see labkey.experiment.createData."))
run$dataOutputs = ensureNestedList(dataOutputs)
}
if (!is.null(materialInputs))
{
if (!is.list(materialInputs))
stop (paste("materialInputs must be a list of material objects, see labkey.experiment.createMaterial."))
run$materialInputs = ensureNestedList(materialInputs)
}
if (!is.null(materialOutputs))
{
if (!is.list(materialOutputs))
stop (paste("materialOutputs must be a list of material objects, see labkey.experiment.createMaterial."))
run$materialOutputs = ensureNestedList(materialOutputs)
}
if (!is.null(dataRows))
{
if (!is.data.frame(dataRows))
stop (paste("dataRows must be a data frame."))
## build Assay object tree based on R lists
nrows <- nrow(dataRows)
ncols <- ncol(dataRows)
cnames <- colnames(dataRows)
rowsVector <- vector(mode="list", length=nrows)
for (j in 1:nrows)
{
cvalues <- as.list(dataRows[j,])
names(cvalues) <- cnames
rowsVector[[j]] <- cvalues
}
run$dataRows <- rowsVector
}
return (run)
}
## Helper to ensure the passed object is serialized to JSON as an array of objects
##
ensureNestedList <- function(data)
{
if (is.null(names(data)))
data
else
data <- list(data)
return (data)
}
labkey.experiment.saveBatch <- function(baseUrl=NULL, folderPath, assayConfig = NULL, protocolName = NULL, batchPropertyList = NULL, runList)
{
baseUrl=labkey.getBaseUrl(baseUrl)
## Validate required parameters
if (missing(folderPath)) stop (paste("A value must be specified for folderPath."))
if (missing(runList)) stop (paste("A value must be specified for runList."))
if (is.null(assayConfig) && is.null(protocolName))
stop (paste("Either an assay config list or protocolName must be specified. The assay configuration must contain either an assayId or both assayName and providerName"))
## Now post form with batch object filled out
url <- labkey.buildURL(baseUrl, "assay", "saveAssayBatch.api", folderPath)
if (!is.null(assayConfig))
params = assayConfig
else if (!is.null(protocolName))
{
params = list()
params$protocolName = protocolName
}
params$batch = c(batchPropertyList, list(runs = ensureNestedList(runList)))
response <- labkey.post(url, toJSON(params, auto_unbox=TRUE))
return (fromJSON(response, simplifyVector=FALSE, simplifyDataFrame=FALSE))
}
labkey.experiment.saveRuns <- function(baseUrl=NULL, folderPath, protocolName, runList)
{
baseUrl=labkey.getBaseUrl(baseUrl)
## Validate required parameters
if (missing(folderPath)) stop (paste("A value must be specified for folderPath."))
if (missing(protocolName)) stop (paste("A value must be specified for protocolName."))
if (missing(runList)) stop (paste("A value must be specified for runList."))
## Now post form with runs object filled out
url <- labkey.buildURL(baseUrl, "assay", "saveAssayRuns.api", folderPath)
if (!is.null(runList))
{
params = list()
params$protocolName = protocolName
}
params$runs = ensureNestedList(runList)
response <- labkey.post(url, toJSON(params, auto_unbox=TRUE))
return (fromJSON(response, simplifyVector=FALSE, simplifyDataFrame=FALSE))
}
## Get lineage parent/child relationships and information for exp objects by LSID(s)
##
## Optional parameters (passed via options) include:
## parents: (boolean) include parent objects in the lineage
## children: (boolean) include child objects in the lineage
## depth: (integer) the depth of the lineage to retrieve
## expType: the type of experiment objects to retrieve lineage for
## cpasType: the type of CPAS object to retrieve lineage for
## runProtocolLsid: the LSID of the run protocol to retrieve lineage for
## includeProperties: (boolean) include properties in the lineage response
## includeInputsAndOutputs: (boolean) include inputs and outputs in the lineage response
## includeRunSteps: (boolean) include run steps in the lineage response
##
labkey.experiment.lineage <- function(baseUrl=NULL, folderPath, lsids, options = NULL)
{
baseUrl=labkey.getBaseUrl(baseUrl)
## check required parameters
if (missing(folderPath))
stop (paste("A value must be specified for folderPath."))
if (missing(lsids))
stop (paste("A value must be specified for lsids."))
if (!missing(lsids) & !(is.vector(lsids) && is.atomic(lsids)))
stop (paste("The lsids parameter must be a vector data structure."))
if (!missing(options) & !is.list(options))
stop (paste("The options parameter must be a list data structure."))
params <- list(lsids=lsids)
if (!missing(options))
params <- c(params, options)
url <- labkey.buildURL(baseUrl, "experiment", "lineage.api", folderPath)
response <- labkey.post(url, toJSON(params, auto_unbox=TRUE))
return (fromJSON(response, simplifyVector=FALSE, simplifyDataFrame=FALSE))
}