Skip to content

Commit 10b6fb1

Browse files
author
labkey-ians
committed
Spec 23821 : Python API for saveBatch
fixed a bug in save_batch and added sample files for load/save using the API
1 parent 8041696 commit 10b6fb1

File tree

3 files changed

+93
-4
lines changed

3 files changed

+93
-4
lines changed

labkey/experiment.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,8 @@ def from_data(data):
240240

241241
def to_json(self):
242242
data = super(Run, self).to_json()
243-
data['dataInputs'] = self.data_inputs
243+
244+
data['dataInputs'] = [data_input.to_json() for data_input in self.data_inputs]
244245
data['dataRows'] = self.data_rows
245246
data['experiments'] = self.experiments
246247
data['filePathRoot'] = self.file_path_root
@@ -256,24 +257,45 @@ def __init__(self, **kwargs):
256257

257258
self.source_protocol = kwargs.pop('source_protocol', kwargs.pop('sourceProtocol', None))
258259
self.run = kwargs.pop('run', None) # TODO Check if this should be a Run instance
259-
self.target_applications = kwargs.pop('target_applications', kwargs.pop('targetApplications'), None)
260-
self.successor_runs = kwargs.pop('successor_runs', kwargs.pop('sucessorRuns', None)) # sic
260+
self.target_applications = kwargs.pop('target_applications', kwargs.pop('targetApplications', None))
261+
self.successor_runs = kwargs.pop('successor_runs', kwargs.pop('successorRuns', kwargs.pop('sucessorRuns', None))) # sic
261262
self.cpas_type = kwargs.pop('cpas_type', kwargs.pop('cpasType', None))
262263

263264
@staticmethod
264265
def from_data(data):
265266
return ProtocolOutput(**data)
266267

268+
def to_json(self):
269+
data = super(ProtocolOutput, self).to_json()
270+
271+
data['sourceProtocol'] = self.source_protocol
272+
data['run'] = self.run
273+
data['targetApplications'] = self.target_applications
274+
data['sucessorRuns'] = self.successor_runs
275+
data['cpasType'] = self.cpas_type
276+
277+
return data
278+
267279

268280
class Data(ProtocolOutput):
269281
def __init__(self, **kwargs):
270282
super(Data, self).__init__(**kwargs)
271283

272284
self.data_type = kwargs.pop('data_type', kwargs.pop('dataType', None))
273285
self.data_file_url = kwargs.pop('data_file_url', kwargs.pop('dataFileURL', None))
274-
self.pipeline_path = kwargs.pop('pipeline_path', kwargs.pop('pipeline_path', None))
286+
self.pipeline_path = kwargs.pop('pipeline_path', kwargs.pop('pipelinePath', None))
275287
self.role = kwargs.pop('role', None)
276288

277289
@staticmethod
278290
def from_data(data):
279291
return Data(**data)
292+
293+
def to_json(self):
294+
data = super(Data, self).to_json()
295+
296+
data['dataFileURL'] = self.data_file_url
297+
data['dataType'] = self.data_type
298+
data['pipelinePath'] = self.pipeline_path
299+
data['role'] = self.role
300+
301+
return data

samples/load_batch_example.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Sample to get a batch
2+
3+
from labkey.utils import create_server_context
4+
from labkey.experiment import load_batch
5+
6+
print("Create a server context")
7+
project_name = 'ModuleAssayTest' # Project folder name
8+
server_context = create_server_context('localhost:8080', project_name, 'labkey', use_ssl=False)
9+
10+
print("Load an Assay batch from the server")
11+
assay_id = 1168 # provide one from your server
12+
batch_id = 95 # provide one from your server
13+
run_group = load_batch(assay_id, batch_id, server_context)
14+
15+
if run_group is not None:
16+
print("Batch Id: " + str(run_group.id))
17+
print("Created By: " + run_group.created_by)
18+

samples/save_batch_example.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Sample code to submit a single batch of Assay data containing a run with three rows of data
2+
3+
from labkey.utils import create_server_context
4+
from labkey.experiment import save_batch, save_batches, Batch, Run
5+
6+
assay_id = 1168 # provide one from your server. rowid from the URL query parameters
7+
8+
print("Create a server context")
9+
project_name = 'ModuleAssayTest'
10+
server_context = create_server_context('localhost:8080', project_name, 'labkey', use_ssl=False)
11+
12+
# Run data rows
13+
dataRows = [
14+
{
15+
# ColumnName : Value
16+
"SampleId": "Monkey 1"
17+
, "TimePoint": "2008/11/02 11:22:33"
18+
, "DoubleData": 4.5
19+
, "HiddenData": "another data point"
20+
}, {
21+
"SampleId": "Monkey 2"
22+
, "TimePoint": "2008/11/02 14:00:01"
23+
, "DoubleData": 3.1
24+
, "HiddenData": "fozzy bear"
25+
}, {
26+
"SampleId": "Monkey 3"
27+
, "TimePoint": "2008/11/02 14:00:01"
28+
, "DoubleData": 1.5
29+
, "HiddenData": "jimbo"
30+
}
31+
]
32+
33+
# Generate the Run object(s)
34+
runTest = Run()
35+
runTest.name = 'python upload'
36+
runTest.data_rows = dataRows
37+
runTest.properties['RunFieldName'] = 'Run Field Value'
38+
39+
# Generate the Batch object(s)
40+
batch = Batch()
41+
batch.runs = [runTest]
42+
batch.name = 'python batch'
43+
batch.properties['PropertyName'] = 'Property Value'
44+
45+
# Execute save api
46+
RESULT = save_batch(assay_id, batch, server_context)
47+
# RESULT = save_batches(assay_id, [batch1, batch2], server_context)
48+
print(RESULT.id)
49+

0 commit comments

Comments
 (0)