Newer
Older
return data['result']
Swen Vermeul
committed
raise ValueError('request to openBIS did not return either result nor error')
Swen Vermeul
committed
raise ValueError('internal error while performing post request')
class Vocabulary():
def __init__(self, data):
self.data = data
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
@property
def terms_kv(self):
return [
{voc["code"]:voc["label"] }
for voc
in sorted(self.data['objects'], key=lambda v: v["ordinal"])
]
@property
def terms(self):
return [
voc["code"]
for voc
in sorted(self.data['objects'], key=lambda v: v["ordinal"])
]
def _repr_html_(self):
html = """
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th>vocabulary term</th>
<th>label</th>
<th>description</th>
<th>vocabulary</th>
</tr>
</thead>
<tbody>
"""
for voc in sorted(
self.data['objects'],
key=lambda v: (v["permId"]["vocabularyCode"], v["ordinal"])
):
html += "<tr> <td>{}</td> <td>{}</td> <td>{}</td> <td>{}</td> </tr>".format(
voc['code'],
voc['label'],
voc['description'],
voc['permId']['vocabularyCode']
)
html += """
</tbody>
</table>
"""
return html
class PropertyHolder():
def __init__(self, openbis_obj, type):
self.__dict__['_openbis'] = openbis_obj
self.__dict__['_type'] = type
self.__dict__['_property_names'] = []
for prop in type.data['propertyAssignments']:
self._property_names.append(prop['propertyType']['code'].lower())
def _get_terms(self, vocabulary):
return self._openbis.get_terms(vocabulary)
def _all_props(self):
props = {}
for code in self._type.codes():
props[code] = getattr(self, code)
return props
def __getattr__(self, name):
if name.endswith('_'):
name = name.rstrip('_')
property_type = self._type.prop[name]['propertyType']
if property_type['dataTypeCode'] == 'CONTROLLEDVOCABULARY':
return self._openbis.get_terms(name)
else:
return { property_type["label"] : property_type["dataTypeCode"]}
else: return None
def __setattr__(self, name, value):
if name not in self._property_names:
raise KeyError("No such property: {}".format(name))
property_type = self._type.prop[name]['propertyType']
data_type = property_type['dataTypeCode']
if data_type == 'CONTROLLEDVOCABULARY':
voc = self._openbis.get_terms(name)
if value not in voc.terms:
raise ValueError("Value must be one of these terms: " + ", ".join(voc.terms))
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
elif data_type in ('INTEGER', 'BOOLEAN', 'VARCHAR'):
if not check_datatype(data_type, value):
raise ValueError("Value must be of type {}".format(data_type))
self.__dict__[name] = value
def __dir__(self):
return self._property_names
def _repr_html_(self):
html = """
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th>property</th>
<th>value</th>
</tr>
</thead>
<tbody>
"""
for prop in self._property_names:
value = ''
try:
value = getattr(self, prop)
except Exception:
pass
html += "<tr> <td>{}</td> <td>{}</td> </tr>".format(
prop,
value
)
html += """
</tbody>
</table>
"""
return html
class AttrHolder():
""" General class for both samples and experiments that hold all common attributes, such as:
- space
- experiment
- parents
- children
- tags
"""
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
def __init__(self, openbis_obj, type):
self.__dict__['_openbis'] = openbis_obj
self.__dict__['_type_obj'] = type
entity_type = type.data['@type'].split('.')[2].capitalize()
self.__dict__['_entity_type'] = entity_type
self.__dict__['_allowed_attrs'] = _definitions(entity_type)['attrs']
self.__dict__['_identifier'] = None
self.__dict__['_is_new'] = True
def __call__(self, data):
self.__dict__['_is_new'] = False
for key in "code permId identifier type container components attachments".split():
self.__dict__['_'+key] = data.get(key, None)
for key in "space project experiment".split():
d = data.get(key, None)
if d is not None:
d = d['permId']
self.__dict__['_'+key] = d
for key in "parents children".split():
self.__dict__['_'+key] = []
for item in data[key]:
self.__dict__['_'+key].append(item['identifier'])
for key in "tags".split():
self.__dict__['_'+key] = []
for item in data[key]:
self.__dict__['_'+key].append({
"code": item['code'],
"@type": "as.dto.tag.id.TagCode"
})
def _all_attrs(self):
defs = _definitions(self._entity_type)
attr2ids = _definitions('attr2ids')
ids2type = _definitions('ids2type')
request = {}
# look at all attributes available for that entity
for attr in defs['attrs']:
if '_'+attr in self.__dict__:
# in inserts and updates, space becomes spaceId, dataset becomes dataSetId etc.
ids = attr2ids[attr]
if self._is_new:
# handle multivalue attributes (parents, children, tags etc.)
if attr in defs['multi']:
items = self.__dict__.get('_'+attr, [])
if items == None:
items = []
request[ids] = items
else:
request[ids] = self.__dict__.get('_'+attr, None)
else:
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
# handle multivalue attributes (parents, children, tags etc.)
# we only cover the Set mechanism, which means we always update all items in a
# list
if attr in defs['multi']:
items = self.__dict__.get('_'+attr, [])
if items == None:
items = []
request[ids] = {
"actions": [
{
"items": items,
"@type": "as.dto.common.update.ListUpdateActionSet",
}
],
"@type": "as.dto.common.update.IdListUpdateValue"
}
else:
# handle single attribut4es (space, experiment, project, container, etc.)
value = self.__dict__.get('_'+attr, {})
if value is None:
pass
else:
isModified=False
if 'isModified' in value:
isModified=True
del value['isModified']
request[ids] = {
"@type": "as.dto.common.update.FieldUpdateValue",
"isModified": isModified,
"value": value,
}
if self.__dict__.get('_code', None) is None:
request['autoGeneratedCode'] = True
else:
pass
return request
def __getattr__(self, name):
""" handles all attribute requests dynamically. Values are returned in a sensible way,
for example the identifiers of parents, children and components are returned
as an array of values.
"""
int_name = '_'+name
if int_name in self.__dict__:
if int_name in ["_experiment"]:
exp = ''
try:
exp = self.__dict__[int_name]['identifier']['identifier']
except KeyError:
pass
return exp
elif isinstance(self.__dict__[int_name], list):
values = []
for item in self.__dict__[int_name]:
values.append(
item.get("code",
item.get("identifier",
item.get("permId", None)))
)
return values
elif isinstance(self.__dict__[int_name], dict):
return self.__dict__[int_name].get(name,
self.__dict__[int_name].get("code",
self.__dict__[int_name].get("identifier",
self.__dict__[int_name].get("permId", None))))
else:
return self.__dict__[int_name]
else:
return None
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
def __setattr__(self, name, value):
if name in ["parents", "children", "components"]:
if not isinstance(value, list):
value = [value]
objs = []
for val in value:
# fetch objects in openBIS, make sure they actually exists
obj = getattr(self._openbis, 'get_'+self._entity_type.lower())(val)
objs.append(obj)
self.__dict__['_'+name] = {
"@type": "as.dto.common.update.IdListUpdateValue",
"actions": [{
"@type": "as.dto.common.update.ListUpdateActionSet",
"items": [item._permId for item in objs]
}]
}
elif name in ["tags"]:
tags = []
for val in value:
tags.append({
"@type": "as.dto.tag.id.TagCode",
"code": val
})
self.__dict__['_tags'] = tags
elif name in ["space", "project", "experiment"]:
# fetch object in openBIS, make sure it actually exists
obj = getattr(self._openbis, "get_"+name)(value)
self.__dict__['_'+name] = obj['permId']
# mark attribute as modified, if it's an existing entity
if self.__dict__['_is_new']:
pass
else:
elif name in ["identifier", "project"]:
raise KeyError("you can not modify the {}".format(name))
elif name == "code":
if self.__dict__['_type_obj'].data['autoGeneratedCode']:
raise KeyError("for this {}Type you can not set a code".format(self.__dict__['_entity_type']))
else:
self.__dict__['_code'] = value
else:
raise KeyError("no such attribute: {}".format(name))
def get_type(self):
return self._type_obj
def get_parents(self):
return getattr(self._openbis, 'get_'+self._entity_type.lower()+'s')(withChildren=self.identifier)
def get_children(self):
return getattr(self._openbis, 'get_'+self._entity_type.lower()+'s')(withParents=self.identifier)
@property
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
def set_tags(self, tags):
tagIds = _tagIds_for_tags(tags, 'Set')
self.openbis.update_sample(self.permId, tagIds=tagIds)
def add_tags(self, tags):
tagIds = _tagIds_for_tags(tags, 'Add')
self.openbis.update_sample(self.permId, tagIds=tagIds)
def del_tags(self, tags):
tagIds = _tagIds_for_tags(tags, 'Remove')
self.openbis.update_sample(self.permId, tagIds=tagIds)
def _repr_html_(self):
html = """
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th>attribute</th>
<th>value</th>
</tr>
</thead>
<tbody>
"""
for key in ["identifier","permId", "code", "type", "space","project", "experiment", "parents", "children", "tags"]:
html += "<tr> <td>{}</td> <td>{}</td> </tr>".format(key, getattr(self, key, None))
html += """
</tbody>
</table>
"""
return html
class Sample():
""" A Sample is one of the most commonly used objects in openBIS.
"""
def __init__(self, openbis_obj, type, data=None, **kwargs):
self.__dict__['openbis'] = openbis_obj
self.__dict__['type'] = type
self.__dict__['p'] = PropertyHolder(openbis_obj, type)
self.__dict__['a'] = AttrHolder(openbis_obj, type)
if data is not None:
self._set_data(data)
if kwargs is not None:
for key in kwargs:
setattr(self, key, kwargs[key])
def _set_data(self, data):
# assign the attribute data to self.a by calling it
# (invoking the AttrHolder.__call__ function)
self.a(data)
self.__dict__['data'] = data
# put the properties in the self.p namespace (without checking them)
for key, value in data['properties'].items():
self.p.__dict__[key.lower()] = value
@property
def type(self):
return self.__dict__['type'].data['code']
@type.setter
def type(self, type_name):
sample_type = self.openbis.get_sample_type(type_name)
self.p.__dict__['_type'] = sample_type
self.a.__dict__['_type'] = sample_type
def __getattr__(self, name):
return getattr(self.__dict__['a'], name)
def __setattr__(self, name, value):
if name in ['set_properties', 'set_tags', 'add_tags']:
raise ValueError("These are methods which should not be overwritten")
setattr(self.__dict__['a'], name, value)
def _repr_html_(self):
html = self.a._repr_html_()
return html
def set_properties(self, properties):
self.openbis.update_sample(self.permId, properties=properties)
def save(self):
props = self.p._all_props()
attrs = self.a._all_attrs()
attrs["properties"] = props
if self.identifier is None:
# create a new sample
attrs["@type"] = "as.dto.sample.create.SampleCreation"
attrs["typeId"] = self.__dict__['type'].data['permId']
request = {
"method": "createSamples",
"params": [ self.openbis.token,
[ attrs ]
]
}
resp = self.openbis._post_request(self.openbis.as_v3, request)
new_sample_data = self.openbis.get_sample(resp[0]['permId'], only_data=True)
self._set_data(new_sample_data)
return self
else:
attrs["@type"] = "as.dto.sample.update.SampleUpdate"
attrs["sampleId"] = {
"permId": self.permId,
"@type": "as.dto.sample.id.SamplePermId"
}
request = {
"method": "updateSamples",
"params": [ self.openbis.token,
[ attrs ]
]
}
resp = self.openbis._post_request(self.openbis.as_v3, request)
print('Sample successfully updated')
self.openbis.delete_entity('sample', self.permId, reason)
def get_datasets(self):
return self.openbis.get_datasets(withSamples=[self.permId])
def get_projects(self):
return self.openbis.get_project(withSamples=[self.permId])
class Space(dict):
""" managing openBIS spaces
"""
def __init__(self, openbis_obj, *args, **kwargs):
super(Space, self).__init__(*args, **kwargs)
self.__dict__ = self
self.openbis = openbis_obj
""" Lists all samples in a given space. A pandas DataFrame object is returned.
"""
return self.openbis.get_samples(space=self.code, *args, **kwargs)
@property
def projects(self):
return self.openbis.get_projects(space=self.code)
def new_project(self, **kwargs):
return self.openbis.new_project(space=self.code, **kwargs)
@property
def experiments(self):
return self.openbis.get_experiments(space=self.code)
class Things():
"""An object that contains a DataFrame object about an entity available in openBIS.
"""
def __init__(self, openbis_obj, what, df, identifier_name='code'):
self.openbis = openbis_obj
self.what = what
self.df = df
self.identifier_name = identifier_name
def _repr_html_(self):
return self.df._repr_html_()
def __getitem__(self, key):
if self.df is not None and len(self.df) > 0:
row = None
if isinstance(key, int):
# get thing by rowid
row = self.df.loc[[key]]
elif isinstance(key, list):
# treat it as a normal dataframe
return self.df[key]
else:
# get thing by code
row = self.df[self.df[self.identifier_name]==key.upper()]
if row is not None:
# invoke the openbis.get_what() method
return getattr(self.openbis, 'get_'+self.what)(row[self.identifier_name].values[0])
class Experiment():
""" managing openBIS experiments
"""
def __init__(self, openbis_obj, data):
self.permId = data['permId']['permId']
self.identifier = data['identifier']['identifier']
self.properties = data['properties']
self.tags = extract_tags(data['tags'])
self.attachments = extract_attachments(data['attachments'])
self.project = data['project']['code']
self.data = data
def set_properties(self, properties):
self.openbis.update_experiment(self.permId, properties=properties)
def set_tags(self, tags):
tagIds = _tagIds_for_tags(tags, 'Set')
self.openbis.update_experiment(self.permId, tagIds=tagIds)
def add_tags(self, tags):
tagIds = _tagIds_for_tags(tags, 'Add')
self.openbis.update_experiment(self.permId, tagIds=tagIds)
def del_tags(self, tags):
tagIds = _tagIds_for_tags(tags, 'Remove')
self.openbis.update_experiment(self.permId, tagIds=tagIds)
def delete(self, reason):
self.openbis.delete_entity('experiment', self.permId, reason)
def _repr_html_(self):
html = """
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th>attribute</th>
<th>value</th>
</tr>
</thead>
<tbody>
<tr> <th>permId</th> <td>{}</td> </tr>
<tr> <th>identifier</th> <td>{}</td> </tr>
<tr> <th>project</th> <td>{}</td> </tr>
<tr> <th>properties</th> <td>{}</td> </tr>
<tr> <th>tags</th> <td>{}</td> </tr>
<tr> <th>attachments</th> <td>{}</td> </tr>
</tbody>
</table>
"""
return html.format(self.permId, self.identifier, self.project, self.properties, self.tags, self.attachments)
data["identifier"] = self.identifier
data["permId"] = self.permId
data["properties"] = self.properties
data["tags"] = self.tags
class PropertyAssignments():
""" holds are properties, that are assigned to an entity, eg. sample or experiment
"""
def __init__(self, openbis_obj, data):
self.openbis = openbis_obj
self.data = data
self.prop = {}
if self.data['propertyAssignments'] is None:
self.data['propertyAssignments'] = []
for pa in self.data['propertyAssignments']:
self.prop[pa['propertyType']['code'].lower()] = pa
def codes(self):
codes = []
for pa in self.data['propertyAssignments']:
codes.append(pa['propertyType']['code'].lower())
return codes
def _repr_html_(self):
html = """
<p>{}: <b>{}</b>
<p>description: {}</p>
<p>Code autogenerated: {}</p>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th>property</th>
<th>label</th>
<th>description</th>
<th>dataTypeCode</th>
<th>mandatory</th>
</tr>
</thead>
<tbody>
""".format(
self.data['@type'].split('.')[-1],
self.data['code'],
self.data['description'],
self.data['autoGeneratedCode']
)
for pa in self.data['propertyAssignments']:
html += "<tr> <th>{}</th> <td>{}</td> <td>{}</td> <td>{}</td> <td>{}</td> </tr>".format(
pa['propertyType']['code'].lower(),
pa['propertyType']['label'],
pa['propertyType']['description'],
pa['propertyType']['dataTypeCode'],
pa['mandatory']
)
html += """
</tbody>
</table>
"""
return html