Newer
Older
By default, all directories and their containing files are listed recursively. You can
turn off this option by setting recursive=False.
"""
request = {
"method" : "listFilesForDataSet",
"params" : [
self.openbis.token,
self.permid,
"id":"1"
self.data["dataStore"]["downloadUrl"] + '/datastore_server/rmi-dss-api-v1.json',
json.dumps(request),
verify=self.openbis.verify_certificates
)
data = resp.json()
if 'error' in data:
Swen Vermeul
committed
raise ValueError('Error from openBIS: ' + data['error'] )
elif 'result' in data:
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
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
@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['dataType'] == 'CONTROLLEDVOCABULARY':
return self._openbis.get_terms(name)
else:
return { property_type["label"] : property_type["dataType"]}
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['dataType']
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))
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
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
"""
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)
Swen Vermeul
committed
for key in "space project".split():
d = data.get(key, None)
if d is not None:
d = d['permId']
self.__dict__['_'+key] = d
Swen Vermeul
committed
for key in "experiment".split():
d = data.get(key, None)
if d is not None:
d = d['identifier']
self.__dict__['_'+key] = d
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
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:
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
# 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:
Swen Vermeul
committed
exp = self.__dict__[int_name]['identifier']
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
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
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
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
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
Swen Vermeul
committed
def __dir__(self):
return ['get_parents()', 'get_children()', 'a', 'space', 'project', 'experiment',
'project','tags', 'data']
@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])
Swen Vermeul
committed
def get_experiment(self):
try:
return self.openbis.get_experiment(self._experiment['identifier'])
except Exception:
pass
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>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']['dataType'],
pa['mandatory']
)
html += """
</tbody>
</table>
"""
return html