/
objects.py
522 lines (444 loc) · 19.2 KB
/
objects.py
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
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
from __future__ import print_function
from __future__ import absolute_import
from builtins import str
import pandas as pd
from builtins import object
import warnings
def _check_api(obj_type):
from transcriptic import api
if not api:
raise RuntimeError("You have to be logged in to be able to create %s objects" % obj_type)
return api
class ProtocolPreview(object):
def __init__(self, protocol, connection):
self.protocol = protocol
self.preview_url = connection.preview_protocol(protocol)
def _repr_html_(self):
return """<iframe src="%s" frameborder="0" allowtransparency="true" \
style="height:500px" seamless></iframe>""" % self.preview_url
class _BaseObject(object):
"""Base object which other objects inherit from"""
# TODO: Inherit more stuff from here. Need to ensure web has unified fields for objects
def __init__(self, obj_type, obj_id, attributes, connection=None):
# If attributes and connection are explicitly provided, just return and not do any smart parsing
if attributes and connection:
self.connection = connection
self.attributes = attributes
else:
if not connection:
self.connection = _check_api(obj_type)
else:
self.connection = connection
(self.id, self.name) = self.load_object(obj_type, obj_id)
if not attributes:
self.attributes = self.connection._get_object(self.id)
else:
self.attributes = attributes
def load_object(self, obj_type, obj_id):
"""Find and match object by name"""
#TODO: Remove the try/except statement and properly handle cases where objects are not found
try:
objects = getattr(self.connection, obj_type + 's')()
except:
return (obj_id, str(obj_id))
matched_objects = []
for obj in objects:
# Special case here since we use both 'name' and 'title' for object names
if 'name' in obj:
if obj_id == obj['name'] or obj_id == obj['id']:
matched_objects.append((obj['id'], obj['name']))
if 'title' in obj:
if obj_id == obj['title'] or obj_id == obj['id']:
matched_objects.append((obj['id'], obj['title']))
if len(matched_objects) == 0:
raise TypeError("%s is not found in your %ss." % (obj_id, obj_type))
elif len(matched_objects) == 1:
return matched_objects[0]
else:
print ("More than 1 match found. Defaulting to the first match: %s" % (matched_objects[0]))
return matched_objects[0]
class Project(_BaseObject):
"""
A Project object contains helper methods for managing your runs. You can view the runs associated with this project
as well as submit runs to the project.
Example Usage:
.. code-block:: python
myProject = Project("My Project")
projectRuns = myProject.runs()
myRunId = projectRuns.query("title == 'myRun'").id.item()
myRun = Run(myRunId)
Attributes
----------
id : str
Project id
name: str
Project name
attributes: dict
Master attributes dictionary
connection: transcriptic.config.Connection
Transcriptic Connection object associated with this specific object
"""
def __init__(self, project_id, attributes=None, connection=None):
"""
Initialize a Project by providing a project name/id. The attributes and connection parameters are generally
not specified unless one wants to manually initialize the object.
Parameters
----------
project_id: str
Project name or id in string form
attributes: Optional[dict]
Attributes of the project
connection: Optional[transcriptic.config.Connection]
Connection context. The default context object will be used unless explicitly provided
"""
super(Project, self).__init__('project', project_id, attributes, connection)
self._runs = pd.DataFrame()
def runs(self, use_cache=True):
"""
Get the list of runs belonging to the project
Parameters
----------
use_cache: Boolean
Determines whether the cached list of runs is returned
Returns
-------
DataFrame
Returns a DataFrame of runs, with the id and title as columns
"""
if self._runs.empty and use_cache:
temp = self.connection.env_args
self.connection.update_environment(project_id=self.id)
project_runs = self.connection.runs()
self._runs = pd.DataFrame([[pr['id'], pr['title']] for pr in project_runs])
self._runs.columns = ['id', 'Name']
self.connection.env_args = temp
return self._runs
def submit(self, protocol, title, test_mode=False):
"""
Submit a run to this project
Parameters
----------
protocol: dict
Autoprotocol Protocol in dictionary form, can be generated using Protocol.as_dict()
title: Optional[str]
Title of run. Run-id will automatically be used as name if field is not provided
test_mode: Optional[boolean]
Determines if run will be submitted will be treated as a test run or a run that is meant for execution
Returns
-------
Run
Returns a run object if run is successfully submitted
"""
response = self.connection.submit_run(protocol, project_id=self.id, title=title, test_mode=test_mode)
return Run(response['id'], response)
class Run(_BaseObject):
"""
A Run object contains helper methods for accessing Run-related information such as Instructions, Datasets
and monitoring data
Example Usage:
.. code-block:: python
myRun = Run('r12345')
myRun.data
myRun.instructions
Attributes
----------
id : str
Run id
name: str
Run name
data: DataFrame
DataFrame of all datasets which belong to this run
instructions: DataFrame
DataFrame of all Instruction objects which belong to this run
project_id : str
Project id which run belongs to
attributes: dict
Master attributes dictionary
connection: transcriptic.config.Connection
Transcriptic Connection object associated with this specific object
"""
def __init__(self, run_id, attributes=None, connection=None):
"""
Initialize a Run by providing a run name/id. The attributes and connection parameters are generally
not specified unless one wants to manually initialize the object.
Parameters
----------
run_id: str
Run name or id in string form
attributes: Optional[dict]
Attributes of the run
connection: Optional[transcriptic.config.Connection]
Connection context. The default context object will be used unless explicitly provided
"""
super(Run, self).__init__('run', run_id, attributes, connection)
self.project_id = self.attributes['project']['id']
self._instructions = pd.DataFrame()
self._data = pd.DataFrame()
@property
def instructions(self):
if self._instructions.empty:
instruction_list = [Instruction(dict(x, **{'project_id': self.project_id, 'run_id': self.id}),
connection=self.connection)
for x in self.attributes["instructions"]]
self._instructions = pd.DataFrame(instruction_list)
self._instructions.columns = ["Instructions"]
self._instructions.insert(0, "Name", [inst.name for inst in self._instructions.Instructions])
self._instructions.insert(1, "Started", [inst.started_at for inst in self._instructions.Instructions])
self._instructions.insert(2, "Completed", [inst.completed_at for inst in self._instructions.Instructions])
return self._instructions
@property
def data(self):
"""
Find and generate a list of Dataset objects which are associated with this run
Returns
-------
DataFrame
Returns a DataFrame of datasets, with Name, Dataset and DataType as columns
"""
if self._data.empty:
datasets = self.connection.datasets(project_id=self.project_id, run_id=self.id)
data_dict = {k: Dataset(datasets[k]["id"], dict(datasets[k], title=k),
connection=self.connection)
for k in list(datasets.keys()) if datasets[k]}
self._data = pd.DataFrame(sorted(list(data_dict.items()), key=lambda x: x[0]))
self._data.columns = ["Name", "Datasets"]
self._data.insert(1, "DataType", ([ds.operation for ds in self._data.Datasets]))
return self._data
def monitoring(self, instruction_id, data_type='pressure'):
"""
View monitoring data of a given instruction
Parameters
----------
instruction_id: str
Instruction id in string form
data_type: str
Monitoring data type, defaults to 'pressure'
Returns
-------
DataFrame
Returns a pandas dataframe of the monitoring data
"""
response = self.connection.monitoring_data(
project_id=self.project_id,
run_id=self.id,
instruction_id=instruction_id,
data_type=data_type
)
return pd.DataFrame(response['results'])
def _repr_html_(self):
return """<iframe src="%s" frameborder="0" allowtransparency="true" \
style="height:450px" seamless></iframe>""" % \
self.connection.get_route('view_run', project_id=self.project_id, run_id=self.id)
class Dataset(_BaseObject):
"""
A Dataset object contains helper methods for accessing data related information
Attributes
----------
id : str
Dataset id
name: str
Dataset name
data : DataFrame
DataFrame of well-indexed data values. Note that associated metadata is found in attributes dictionary
container: Container
Container object that was used for this dataset
operation: str
Operation used for generating the dataset
data_type: str
Data type of this dataset
attributes: dict
Master attributes dictionary
connection: transcriptic.config.Connection
Transcriptic Connection object associated with this specific object
"""
def __init__(self, data_id, attributes=None, connection=None):
"""
Initialize a Dataset by providing a data name/id. The attributes and connection parameters are generally
not specified unless one wants to manually initialize the object.
Parameters
----------
data_id: str
Dataset name or id in string form
attributes: Optional[dict]
Attributes of the dataset
connection: Optional[transcriptic.config.Connection]
Connection context. The default context object will be used unless explicitly provided
"""
super(Dataset, self).__init__('dataset', data_id, attributes, connection)
# TODO: Get BaseObject to handle dataset name
self.name = self.attributes["title"]
self.id = data_id
self.operation = self.attributes["instruction"]["operation"]["op"]
self.data_type = self.attributes["data_type"]
self._data = pd.DataFrame()
self.container = Container(self.attributes["container"]["id"], attributes=self.attributes["container"],
connection=connection)
@property
def data(self, key="*"):
if self._data.empty:
# Get all data initially (think about lazy loading in the future)
self._data = pd.DataFrame(self.connection.dataset(data_id=self.id, key="*"))
self._data.columns = [x.upper() for x in self._data.columns]
if key == "*":
return self._data
else:
return self._data[key]
def _repr_html_(self):
return """<iframe src="%s" frameborder="0" allowtransparency="true" \
style="height:500px" seamless></iframe>""" % \
self.connection.get_route('view_data', data_id=self.id)
class Instruction(object):
"""
An Instruction object contains information related to the current instruction such as the start,
completed time as well as warps associated with the instruction.
Note that Instruction objects are usually created as part of a run and not created explicity.
Additionally, if diagnostic information is available, one can click on the `Show Diagnostics Data`
button to view relevant diagnostic information.
Example Usage:
.. code-block:: python
myRun = Run('r12345')
myRun.instructions
# Access instruction object
myRun.instructions.Instructions[1]
myRun.instructions.Instructions[1].warps
Attributes
----------
id : str
Instruction id
name: str
Instruction name
warps : DataFrame
DataFrame of warps in the instruction
started_at : str
Time where instruction begun
completed_at : str
Time where instruction ended
device_id: str
Id of device which instruction was executed on
attributes: dict
Master attributes dictionary
connection: transcriptic.config.Connection
Transcriptic Connection object associated with this specific object
"""
def __init__(self, attributes, connection=None):
"""
Parameters
----------
attributes : dict
Instruction attributes
connection: Optional[transcriptic.config.Connection]
Connection context. The default context object will be used unless explicitly provided
"""
self.connection = connection
self.attributes = attributes
self.id = attributes["id"]
self.name = attributes["operation"]["op"]
self.started_at = attributes["started_at"]
self.completed_at = attributes["completed_at"]
if len(attributes["warps"]) > 0:
device_id_set = set([warp["device_id"] for warp in self.attributes["warps"]])
self.device_id = device_id_set.pop()
if len(device_id_set) > 1:
warnings.warn("There is more than one device involved in this instruction. Please contact "
"Transcriptic for assistance.")
else:
self.device_id = None
self._warps = pd.DataFrame()
@property
def warps(self):
if self._warps.empty:
warp_list = self.attributes["warps"]
self._warps = pd.DataFrame(x['command'] for x in warp_list)
self._warps.columns = [x.title() for x in self._warps.columns.tolist()]
self._warps.insert(1, "Started", [x["reported_started_at"] for x in warp_list])
self._warps.insert(2, "Completed", [x["reported_completed_at"] for x in warp_list])
return self._warps
def _repr_html_(self):
return """<iframe src="%s" frameborder="0" allowtransparency="true" \
style="height:1000px" seamless></iframe>""" % \
self.connection.get_route('view_instruction', run_id= self.attributes["run_id"],
project_id= self.attributes["project_id"], instruction_id=self.id)
class Container(_BaseObject):
"""
A Container object represents a container from the Transcriptic LIMS and
contains relevant information on the container type as well as the
aliquots present in the container.
Example Usage:
.. code-block:: python
my_container = container("ct186apgz6a374")
my_container.well_map
my_container.aliquots
my_container.container_type.col_count
my_container.container_type.robotize("B1")
my_container.container_type.humanize(12)
Attributes
----------
name: str
Name of container
well_map: dict
Well mapping with well indices for keys and well names as values
aliquots: DataFrame
DataFrame of aliquots present in the container
container_type: autoprotocol.container_type.ContainerType
Autoprotocol ContainerType object with many useful container type
information and functions.
cover: str
Cover type of container
storage: str
Storage condition of container
Example Usage:
.. code-block:: python
my_container = container("ct186apgz6a374")
my_container.well_map
my_container.container_type.col_count
my_container.container_type.robotize("B1")
my_container.container_type.humanize(12)
"""
def __init__(self, container_id, attributes=None, connection=None):
"""
Initialize a Container by providing a container name/id. The attributes and connection parameters are generally
not specified unless one wants to manually initialize the object.
Parameters
----------
container_id: str
Container name or id in string form
attributes: Optional[dict]
Attributes of the container
connection: Optional[transcriptic.config.Connection]
Connection context. The default context object will be used unless explicitly provided
"""
super(Container, self).__init__('container', container_id, attributes, connection)
# TODO: Unify container "label" with name, add Containers route
self.id = container_id
self.cover = self.attributes["cover"]
self.name = self.attributes["label"]
self.storage = self.attributes["storage_condition"]
self.well_map = {aliquot["well_idx"]: aliquot["name"]
for aliquot in self.attributes["aliquots"]}
self.container_type = self._parse_container_type()
self._aliquots = pd.DataFrame()
def _parse_container_type(self):
"""Helper function for parsing container string into container object"""
from autoprotocol.container_type import _CONTAINER_TYPES
container_type = self.attributes["container_type"]
# Return the corresponding AP-Py container object for now. In the future, consider merging
# the current and future dictionary when instantianting container_type
try:
return _CONTAINER_TYPES[container_type["shortname"]]
except KeyError:
warnings.warn("ContainerType given is not supported yet in AP-Py")
return None
@property
def aliquots(self):
if self._aliquots.empty:
aliquot_list = self.attributes["aliquots"]
self._aliquots = pd.DataFrame([dict({'Name': x['name'], 'Id': x['id'],
'Volume': x['volume_ul']}, **x['properties'])
for x in aliquot_list])
return self._aliquots
def __repr__(self):
"""
Return a string representation of a Container using the specified name.
(ex. Container('my_plate'))
"""
return "Container(%s)" % (str(self.name))