Newer
Older
for option in options:
fo[option] = fetch_option[option]
request = {
"method": method_name,
"params": [
self.token,
search_params,
fo
],
}
return request
def get_terms(self, vocabulary=None, start_with=None, count=None):
""" Returns information about existing vocabulary terms.
If a vocabulary code is provided, it only returns the terms of that vocabulary.
search_request = {}
if vocabulary is not None:
search_request = _gen_search_criteria({
"vocabulary": "VocabularyTerm",
"criteria": [{
"vocabulary": "Vocabulary",
"code": vocabulary
}]
})
search_request["@type"] = "as.dto.vocabulary.search.VocabularyTermSearchCriteria"
fetchopts = fetch_option['vocabularyTerm']
fetchopts['from'] = start_with
fetchopts['count'] = count
request = {
"method": "searchVocabularyTerms",
"params": [self.token, search_request, fetchopts]
}
resp = self._post_request(self.as_v3, request)
attrs = 'code vocabularyCode label description registrationDate modificationDate official ordinal'.split()
if len(resp['objects']) == 0:
terms = DataFrame(columns=attrs)
else:
objects = resp['objects']
parse_jackson(objects)
terms['vocabularyCode'] = terms['permId'].map(extract_attr('vocabularyCode'))
terms['registrationDate'] = terms['registrationDate'].map(format_timestamp)
terms['modificationDate'] = terms['modificationDate'].map(format_timestamp)
return Things(
openbis_obj = self,
entity = 'term',
df = terms[attrs],
identifier_name='code',
additional_identifier='vocabularyCode',
start_with = start_with,
count = count,
totalCount = resp.get('totalCount'),
def new_term(self, code, vocabularyCode, label=None, description=None):
return VocabularyTerm(
self, data=None,
code=code, vocabularyCode=vocabularyCode,
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
label=label, description=description
)
def get_term(self, code, vocabularyCode=None, only_data=False):
entity_def = get_definition_for_entity('VocabularyTerm')
search_request = {
"code": code,
"vocabularyCode": vocabularyCode,
"@type": "as.dto.vocabulary.id.VocabularyTermPermId"
}
fetchopts = get_fetchoption_for_entity('VocabularyTerm')
for opt in ['registrator']:
fetchopts[opt] = get_fetchoption_for_entity(opt)
request = {
"method": 'getVocabularyTerms',
"params": [
self.token,
[search_request],
fetchopts
],
}
resp = self._post_request(self.as_v3, request)
if resp is None or len(resp) == 0:
raise ValueError("no VocabularyTerm found with code='{}' and vocabularyCode='{}'".format(code, vocabularyCode))
else:
parse_jackson(resp)
for ident in resp:
if only_data:
return resp[ident]
else:
return VocabularyTerm(self, resp[ident])
def _get_any_entity(self, identifier, entity, only_data=False, method=None):
entity_def = get_definition_for_entity(entity)
# guess the method to fetch an entity
if method is None:
method = 'get' + entity + 's'
Swen Vermeul
committed
search_request = _type_for_id(identifier, entity)
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
fetchopts = get_fetchoption_for_entity(entity)
request = {
"method": method,
"params": [
self.token,
[search_request],
fetchopts
],
}
resp = self._post_request(self.as_v3, request)
if resp is None or len(resp) == 0:
raise ValueError('no {} found with identifier: {}'.format(entity, identifier))
else:
parse_jackson(resp)
for ident in resp:
if only_data:
return resp[ident]
else:
# get a dynamic instance of that class
klass = globals()[entity]
return klass(self, resp[ident])
def get_vocabularies(self, code=None, start_with=None, count=None):
""" Returns information about vocabulary
"""
sub_criteria = []
if code:
sub_criteria.append(_criteria_for_code(code))
criteria = {
"criteria": sub_criteria,
"@type": "as.dto.vocabulary.search.VocabularySearchCriteria",
"operator": "AND"
}
fetchopts = fetch_option['vocabulary']
fetchopts['from'] = start_with
fetchopts['count'] = count
for option in ['registrator']:
fetchopts[option] = fetch_option[option]
request = {
"method": "searchVocabularies",
}
resp = self._post_request(self.as_v3, request)
attrs = 'code description managedInternally internalNameSpace chosenFromList urlTemplate registrator registrationDate modificationDate'.split()
if len(resp['objects']) == 0:
vocs = DataFrame(columns=attrs)
else:
objects = resp['objects']
parse_jackson(resp)
vocs = DataFrame(objects)
vocs['registrationDate'] = vocs['registrationDate'].map(format_timestamp)
vocs['modificationDate'] = vocs['modificationDate'].map(format_timestamp)
vocs['registrator'] = vocs['registrator'].map(extract_person)
return Things(
openbis_obj = self,
entity = 'vocabulary',
df = vocs[attrs],
identifier_name = 'code',
start_with = start_with,
count = count,
totalCount = resp.get('totalCount'),
def get_vocabulary(self, code, only_data=False):
""" Returns the details of a given vocabulary (including vocabulary terms)
"""
return self._get_any_entity(code, 'Vocabulary', only_data=only_data, method='getVocabularies')
def new_tag(self, code, description=None):
""" Creates a new tag (for this user)
"""
return Tag(self, code=code, description=description)
def get_tags(self, code=None, start_with=None, count=None):
Swen Vermeul
committed
""" Returns a DataFrame of all tags
"""
search_criteria = get_search_type_for_entity('tag', 'AND')
criteria = []
fetchopts['from'] = start_with
fetchopts['count'] = count
for option in ['owner']:
fetchopts[option] = fetch_option[option]
if code:
criteria.append(_criteria_for_code(code))
search_criteria['criteria'] = criteria
request = {
"method": "searchTags",
"params": [
self.token,
search_criteria,
fetchopts
]
}
resp = self._post_request(self.as_v3, request)
return self._tag_list_for_response(response=resp['objects'], totalCount=resp['totalCount'])
Swen Vermeul
committed
def get_tag(self, permId, only_data=False):
Swen Vermeul
committed
""" Returns a specific tag
"""
just_one = True
identifiers = []
if isinstance(permId, list):
just_one = False
for ident in permId:
identifiers.append(_type_for_id(ident, 'tag'))
else:
identifiers.append(_type_for_id(permId, 'tag'))
fetchopts = fetch_option['tag']
for option in ['owner']:
fetchopts[option] = fetch_option[option]
Swen Vermeul
committed
request = {
"method": "getTags",
"params": [
self.token,
Swen Vermeul
committed
fetchopts
],
}
resp = self._post_request(self.as_v3, request)
if just_one:
if len(resp) == 0:
raise ValueError('no such tag found: {}'.format(permId))
parse_jackson(resp)
for permId in resp:
if only_data:
return resp[permId]
else:
return Tag(self, data=resp[permId])
return self._tag_list_for_response( response=list(resp.values()) )
def _tag_list_for_response(self, response, totalCount=0):
parse_jackson(response)
attrs = ['permId', 'code', 'description', 'owner', 'private', 'registrationDate']
if len(response) == 0:
tags = DataFrame(columns = attrs)
else:
tags = DataFrame(response)
tags['registrationDate'] = tags['registrationDate'].map(format_timestamp)
tags['permId'] = tags['permId'].map(extract_permid)
tags['description'] = tags['description'].map(lambda x: '' if x is None else x)
tags['owner'] = tags['owner'].map(extract_person)
return Things(
openbis_obj = self,
entity = 'tag',
df = tags[attrs],
identifier_name ='permId',
totalCount = totalCount,
def search_semantic_annotations(self,
permId=None, entityType=None, propertyType=None, only_data=False
):

yvesn
committed
""" Get a list of semantic annotations for permId, entityType, propertyType or
property type assignment (DataFrame object).
:param permId: permId of the semantic annotation.
:param entityType: entity (sample) type to search for.
:param propertyType: property type to search for
:param only_data: return result as plain data object.
:return: Things of DataFrame objects or plain data object
"""
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
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
2343
2344
criteria = []
typeCriteria = []
if permId is not None:
criteria.append({
"@type" : "as.dto.common.search.PermIdSearchCriteria",
"fieldValue" : {
"@type" : "as.dto.common.search.StringEqualToValue",
"value" : permId
}
})
if entityType is not None:
typeCriteria.append({
"@type" : "as.dto.entitytype.search.EntityTypeSearchCriteria",
"criteria" : [_criteria_for_code(entityType)]
})
if propertyType is not None:
typeCriteria.append({
"@type" : "as.dto.property.search.PropertyTypeSearchCriteria",
"criteria" : [_criteria_for_code(propertyType)]
})
if entityType is not None and propertyType is not None:
criteria.append({
"@type" : "as.dto.property.search.PropertyAssignmentSearchCriteria",
"criteria" : typeCriteria
})
else:
criteria += typeCriteria
saCriteria = {
"@type" : "as.dto.semanticannotation.search.SemanticAnnotationSearchCriteria",
"criteria" : criteria
}
objects = self._search_semantic_annotations(saCriteria)
if only_data:
return objects
Swen Vermeul
committed
attrs = ['permId', 'entityType', 'propertyType', 'predicateOntologyId', 'predicateOntologyVersion', 'predicateAccessionId', 'descriptorOntologyId', 'descriptorOntologyVersion', 'descriptorAccessionId', 'creationDate']
if len(objects) == 0:
annotations = DataFrame(columns=attrs)
else:
annotations = DataFrame(objects)
Swen Vermeul
committed
return Things(
openbis_obj = self,
entity = 'semantic_annotation',
df = annotations[attrs],
identifier_name = 'permId',
)
def _search_semantic_annotations(self, criteria):
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
fetch_options = {
"@type": "as.dto.semanticannotation.fetchoptions.SemanticAnnotationFetchOptions",
"entityType": {"@type": "as.dto.entitytype.fetchoptions.EntityTypeFetchOptions"},
"propertyType": {"@type": "as.dto.property.fetchoptions.PropertyTypeFetchOptions"},
"propertyAssignment": {
"@type": "as.dto.property.fetchoptions.PropertyAssignmentFetchOptions",
"entityType" : {
"@type" : "as.dto.entitytype.fetchoptions.EntityTypeFetchOptions"
},
"propertyType" : {
"@type" : "as.dto.property.fetchoptions.PropertyTypeFetchOptions"
}
}
}
request = {
"method": "searchSemanticAnnotations",
"params": [self.token, criteria, fetch_options]
}
resp = self._post_request(self.as_v3, request)
Swen Vermeul
committed
if len(resp['objects']) == 0:
return []
else:
objects = resp['objects']
parse_jackson(objects)
for object in objects:
object['permId'] = object['permId']['permId']
if object.get('entityType') is not None:
object['entityType'] = object['entityType']['code']
elif object.get('propertyType') is not None:
object['propertyType'] = object['propertyType']['code']
elif object.get('propertyAssignment') is not None:
object['entityType'] = object['propertyAssignment']['entityType']['code']
object['propertyType'] = object['propertyAssignment']['propertyType']['code']
object['creationDate'] = format_timestamp(object['creationDate'])
return objects
Swen Vermeul
committed
def get_semantic_annotations(self):
""" Get a list of all available semantic annotations (DataFrame object).
"""
objects = self._search_semantic_annotations({
"@type": "as.dto.semanticannotation.search.SemanticAnnotationSearchCriteria"
})
attrs = ['permId', 'entityType', 'propertyType', 'predicateOntologyId', 'predicateOntologyVersion', 'predicateAccessionId', 'descriptorOntologyId', 'descriptorOntologyVersion', 'descriptorAccessionId', 'creationDate']
Swen Vermeul
committed
if len(objects) == 0:
annotations = DataFrame(columns=attrs)
else:
annotations = DataFrame(objects)
return Things(
openbis_obj = self,
entity = 'semantic_annotation',
df = annotations[attrs],
identifier_name = 'permId',
)

yvesn
committed
def get_semantic_annotation(self, permId, only_data = False):
objects = self.search_semantic_annotations(permId=permId, only_data=True)
if len(objects) == 0:
raise ValueError("Semantic annotation with permId " + permId + " not found.")
object = objects[0]
if only_data:
return object
else:

yvesn
committed
return SemanticAnnotation(self, isNew=False, **object)
def get_plugins(self, start_with=None, count=None):
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
criteria = []
search_criteria = get_search_type_for_entity('plugin', 'AND')
search_criteria['criteria'] = criteria
fetchopts = fetch_option['plugin']
for option in ['registrator']:
fetchopts[option] = fetch_option[option]
request = {
"method": "searchPlugins",
"params": [
self.token,
search_criteria,
fetchopts,
],
}
resp = self._post_request(self.as_v3, request)
attrs = ['name', 'description', 'pluginType', 'pluginKind',
'entityKinds', 'registrator', 'registrationDate', 'permId']
if len(resp['objects']) == 0:
plugins = DataFrame(columns=attrs)
else:
objects = resp['objects']
parse_jackson(objects)
plugins = DataFrame(objects)
plugins['permId'] = plugins['permId'].map(extract_permid)
plugins['registrator'] = plugins['registrator'].map(extract_person)
plugins['registrationDate'] = plugins['registrationDate'].map(format_timestamp)
plugins['description'] = plugins['description'].map(lambda x: '' if x is None else x)
plugins['entityKinds'] = plugins['entityKinds'].map(lambda x: '' if x is None else x)
return Things(
openbis_obj = self,
entity = 'plugin',
df = plugins[attrs],
identifier_name = 'name',
start_with = start_with,
count = count,
totalCount = resp.get('totalCount'),
def get_plugin(self, permId, only_data=False, with_script=True):
Swen Vermeul
committed
search_request = _type_for_id(permId, 'plugin')
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
fetchopts = fetch_option['plugin']
options = ['registrator']
if with_script:
options.append('script')
for option in options:
fetchopts[option] = fetch_option[option]
request = {
"method": "getPlugins",
"params": [
self.token,
[search_request],
fetchopts
],
}
resp = self._post_request(self.as_v3, request)
parse_jackson(resp)
if resp is None or len(resp) == 0:
raise ValueError('no such plugin found: ' + permId)
else:
for permId in resp:
if only_data:
return resp[permId]
else:
return Plugin(self, data=resp[permId])
def new_plugin(self, pluginType= "MANAGED_PROPERTY", pluginKind = "JYTHON", **kwargs):
""" Note: not functional yet. Creates a new Plugin in openBIS. The attribute pluginKind must be one of
the following:
DYNAMIC_PROPERTY, MANAGED_PROPERTY, ENTITY_VALIDATION;
Usage::
o.new_plugin(
name = 'name of plugin',
description = '...',
pluginType = "ENTITY_VALIDATION",
script = "def a():\n pass",
available = True,
entityKind = None
)
"""
if pluginType not in [
'DYNAMIC_PROPERTY', 'MANAGED_PROPERTY', 'ENTITY_VALIDATION'
]:
raise ValueError(
"pluginType must be one of the following: DYNAMIC_PROPERTY, MANAGED_PROPERTY, ENTITY_VALIDATION")
return Plugin(self, pluginType=pluginType, pluginKind=pluginKind, **kwargs)
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
def new_property_type(self, **kwargs):
pass
def get_property_type(self, code):
pass
def get_property_types(self, start_with=None, count=None, **search_args):
fetchopts = fetch_option['propertyType']
fetchopts['from'] = start_with
fetchopts['count'] = count
search_criteria = get_search_criteria('propertyType', **search_args)
request = {
"method": "searchPropertyTypes",
"params": [
self.token,
search_criteria,
fetchopts,
],
}
attrs = openbis_definitions('propertyType')['attrs']
resp = self._post_request(self.as_v3, request)
if len(resp['objects']) == 0:
experiments = DataFrame(columns=attrs)
else:
objects = resp['objects']
parse_jackson(objects)
df = DataFrame(objects)
df['registrationDate'] = df['registrationDate'].map(format_timestamp)
df['registrator'] = df['registrator'].map(extract_person)
return Things(
openbis_obj = self,
entity = 'propertyType',
df = df[attrs],
start_with = start_with,
count = count,
totalCount = resp.get('totalCount'),
)
def get_sample_types(self, type=None, start_with=None, count=None):
Swen Vermeul
committed
""" Returns a list of all available sample types
"""
return self._get_types_of(
method_name = "searchSampleTypes",
entity = "Sample",
type_name = type,
optional_attributes = ["generatedCodePrefix"],
start_with = start_with,
count = count,
Swen Vermeul
committed
)
get_object_types = get_sample_types # Alias
Swen Vermeul
committed
def get_sample_type(self, type):
entityType = self._get_types_of(
method_name = "searchSampleTypes",
entity = "Sample",
type_name = type,
optional_attributes = ["generatedCodePrefix", "validationPluginId"]
)
return SampleType(self, data=entityType.data)
get_object_type = get_sample_type # Alias
def get_experiment_types(self, type=None, start_with=None, count=None):
Swen Vermeul
committed
""" Returns a list of all available experiment types
"""
return self._get_types_of(
method_name = "searchExperimentTypes",
entity = "Experiment",
type_name = type,
start_with = start_with,
count = count,
Swen Vermeul
committed
)
get_collection_types = get_experiment_types # Alias
Swen Vermeul
committed
def get_experiment_type(self, type):
Swen Vermeul
committed
return self._get_types_of(
method_name = "searchExperimentTypes",
entity = "Experiment",
type_name = type
Swen Vermeul
committed
)
except Exception:
raise ValueError("No such experiment type: {}".format(type))
get_collection_type = get_experiment_type # Alias
Swen Vermeul
committed
def get_material_types(self, type=None, start_with=None, count=None):
Swen Vermeul
committed
""" Returns a list of all available material types
"""
method_name = "searchMaterialTypes",
entity = "Material",
type_name = type,
start_with = start_with,
count = count,
Swen Vermeul
committed
def get_material_type(self, type):
try:
method_name = "searchMaterialTypes",
entity = "Material",
type_name = type
Swen Vermeul
committed
except Exception:
raise ValueError("No such material type: {}".format(type))
def get_dataset_types(self, type=None, start_with=None, count=None):
Swen Vermeul
committed
""" Returns a list (DataFrame object) of all currently available dataset types
"""
"searchDataSetTypes", "DataSet", type,
optional_attributes=['kind'],
start_with=start_with,
count=count,
)
Swen Vermeul
committed
def get_dataset_type(self, type):
return self._get_types_of(
method_name = "searchDataSetTypes",
entity = "DataSet",
type_name = type,
optional_attributes = ['kind']
)
Swen Vermeul
committed
self, method_name, entity, type_name=None,
start_with=None, count=None,
additional_attributes=None, optional_attributes=None
):
""" Returns a list of all available types of an entity.
If the name of the entity-type is given, it returns a PropertyAssignments object
if additional_attributes is None:
additional_attributes = []
if optional_attributes is None:
optional_attributes = []
search_request = {
"@type": "as.dto.{}.search.{}TypeSearchCriteria".format(
entity.lower(), entity
)
}
fetch_options = {
"@type": "as.dto.{}.fetchoptions.{}TypeFetchOptions".format(
entity.lower(), entity
)
}
fetch_options['from'] = start_with
fetch_options['count'] = count
if type_name is not None:
search_request = _gen_search_criteria({
"operator": "AND",
})
Swen Vermeul
committed
fetch_options['propertyAssignments'] = fetch_option['propertyAssignments']
if self.get_server_information().api_version > '3.3':
fetch_options['validationPlugin'] = fetch_option['plugin']
"params": [self.token, search_request, fetch_options],
}
resp = self._post_request(self.as_v3, request)
parse_jackson(resp)
if type_name is not None:
if len(resp['objects']) == 1:
return EntityType(
openbis_obj = self,
data = resp['objects'][0]
)
elif len(resp['objects']) == 0:
raise ValueError("No such {} type: {}".format(entity, type_name))
else:
raise ValueError("There is more than one entry for entity={} and type={}".format(entity, type_name))
Swen Vermeul
committed
types = []
attrs = self._get_attributes(type_name, types, additional_attributes, optional_attributes)
if len(resp['objects']) == 0:
types = DataFrame(columns=attrs)
Swen Vermeul
committed
objects = resp['objects']
parse_jackson(objects)
types = DataFrame(objects)
types['modificationDate'] = types['modificationDate'].map(format_timestamp)
return Things(
openbis_obj = self,
entity = entity.lower() + '_type',
df = types[attrs],
start_with = start_with,
count = count,
totalCount = resp.get('totalCount'),
Swen Vermeul
committed
Yves Noirjean
committed
def _get_attributes(self, type_name, types, additional_attributes, optional_attributes):
attributes = ['code', 'description'] + additional_attributes
attributes += [attribute for attribute in optional_attributes if attribute in types]
attributes += ['modificationDate']
if type_name is not None:
attributes += ['propertyAssignments']
return attributes
Swen Vermeul
committed
def is_session_active(self):
""" checks whether a session is still active. Returns true or false.
"""
Swen Vermeul
committed
return self.is_token_valid(self.token)
def is_token_valid(self, token=None):
Chandrasekhar Ramakrishnan
committed
"""Check if the connection to openBIS is valid.
This method is useful to check if a token is still valid or if it has timed out,
requiring the user to login again.
Chandrasekhar Ramakrishnan
committed
:return: Return True if the token is valid, False if it is not valid.
"""
if token is None:
token = self.token
if token is None:
return False
request = {
"method": "isSessionActive",
"params": [token],
Swen Vermeul
committed
try:
resp = self._post_request(self.as_v1, request)
except Exception as e:
return False
Swen Vermeul
committed
Swen Vermeul
committed
def get_dataset(self, permIds, only_data=False, props=None):
"""fetch a dataset and some metadata attached to it:
- properties
- sample
- parents
- children
- containers
- dataStore
- physicalData
- linkedData
:return: a DataSet object
"""
Swen Vermeul
committed
just_one = True
identifiers = []
if isinstance(permIds, list):
just_one = False
for permId in permIds:
identifiers.append(
_type_for_id(permId, 'dataset')
)
else:
identifiers.append(
_type_for_id(permIds, 'dataset')
)
Swen Vermeul
committed
fetchopts = fetch_option['dataSet']
for option in ['tags', 'properties', 'dataStore', 'physicalData', 'linkedData',
'experiment', 'sample', 'registrator', 'modifier']:
fetchopts[option] = fetch_option[option]
request = {
"params": [
self.token,
Swen Vermeul
committed
identifiers,
resp = self._post_request(self.as_v3, request)
Swen Vermeul
committed
if just_one:
if len(resp) == 0:
raise ValueError('no such dataset found: {}'.format(permIds))
Swen Vermeul
committed
parse_jackson(resp)
for permId in resp:
if only_data:
return resp[permId]
else:
return DataSet(
Swen Vermeul
committed
type = self.get_dataset_type(resp[permId]["type"]["code"]),
data = resp[permId]
)
else:
return self._dataset_list_for_response(response=list(resp.values()), props=props)
def _dataset_list_for_response(
self, response, props=None,
start_with=None, count=None, totalCount=0
):
Swen Vermeul
committed
"""returns a Things object, containing a DataFrame plus some additional information
"""
parse_jackson(response)
attrs = ['permId', 'type', 'experiment', 'sample',
'registrationDate', 'modificationDate',
'location', 'status', 'presentInArchive', 'size',
'properties'
]
Swen Vermeul
committed
if len(response) == 0:
datasets = DataFrame(columns=attrs)
else:
datasets = DataFrame(response)
datasets['registrationDate'] = datasets['registrationDate'].map(format_timestamp)
datasets['modificationDate'] = datasets['modificationDate'].map(format_timestamp)
datasets['experiment'] = datasets['experiment'].map(extract_nested_identifier)
datasets['sample'] = datasets['sample'].map(extract_nested_identifier)
datasets['type'] = datasets['type'].map(extract_code)
datasets['permId'] = datasets['code']
datasets['size'] = datasets['physicalData'].map(lambda x: x.get('size') if x else '')
datasets['status'] = datasets['physicalData'].map(lambda x: x.get('status') if x else '')
datasets['presentInArchive'] = datasets['physicalData'].map(lambda x: x.get('presentInArchive') if x else '')
Swen Vermeul
committed
datasets['location'] = datasets['physicalData'].map(lambda x: x.get('location') if x else '')
if props is not None:
if isinstance(props, str):
props = [props]
Swen Vermeul
committed
for prop in props:
datasets[prop.upper()] = datasets['properties'].map(lambda x: x.get(prop.upper(), ''))
attrs.append(prop.upper())
return Things(
openbis_obj = self,
entity = 'dataset',
df = datasets[attrs],
identifier_name = 'permId',
start_with=start_with,
count=count,
totalCount=totalCount,
Swen Vermeul
committed
def get_sample(self, sample_ident, only_data=False, withAttachments=False, props=None):
Chandrasekhar Ramakrishnan
committed
"""Retrieve metadata for the sample.
Get metadata for the sample and any directly connected parents of the sample to allow access
to the same information visible in the ELN UI. The metadata will be on the file system.
:param sample_identifiers: A list of sample identifiers to retrieve.
"""
Swen Vermeul
committed
just_one = True
identifiers = []
if isinstance(sample_ident, list):
just_one = False
for ident in sample_ident:
identifiers.append(
_type_for_id(ident, 'sample')
)
else:
identifiers.append(
_type_for_id(sample_ident, 'sample')
)
Swen Vermeul
committed
fetchopts = {"type": {"@type": "as.dto.sample.fetchoptions.SampleTypeFetchOptions"}}
options = ['tags', 'properties', 'attachments', 'space', 'experiment', 'registrator', 'modifier', 'dataSets']
options.append('project')
for option in options:
Swen Vermeul
committed
fetchopts[option] = fetch_option[option]
if withAttachments:
fetchopts['attachments'] = fetch_option['attachmentsWithContent']
for key in ['parents','children','container','components']:
fetchopts[key] = {"@type": "as.dto.sample.fetchoptions.SampleFetchOptions"}
"method": "getSamples",
"params": [
self.token,
Swen Vermeul
committed
identifiers,
Swen Vermeul
committed
fetchopts
resp = self._post_request(self.as_v3, request)
Swen Vermeul
committed
if just_one:
if len(resp) == 0:
raise ValueError('no such sample found: {}'.format(sample_ident))
parse_jackson(resp)
for sample_ident in resp:
if only_data:
return resp[sample_ident]
else:
return Sample(
openbis_obj = self,
type = self.get_sample_type(resp[sample_ident]["type"]["code"]),
data = resp[sample_ident]
)
Swen Vermeul
committed
else:
return self._sample_list_for_response(
response=list(resp.values()),
props=props,
)
Swen Vermeul
committed
def _sample_list_for_response(
self, response, props=None,
start_with=None, count=None, totalCount=0
):
Swen Vermeul
committed
"""returns a Things object, containing a DataFrame plus some additional information
"""
parse_jackson(response)
attrs = ['identifier', 'permId', 'experiment', 'sample_type',
'registrator', 'registrationDate', 'modifier', 'modificationDate']
if len(response) == 0:
samples = DataFrame(columns=attrs)
else:
samples = DataFrame(response)
samples['registrationDate'] = samples['registrationDate'].map(format_timestamp)
samples['modificationDate'] = samples['modificationDate'].map(format_timestamp)
samples['registrator'] = samples['registrator'].map(extract_person)
samples['modifier'] = samples['modifier'].map(extract_person)
samples['identifier'] = samples['identifier'].map(extract_identifier)
samples['permId'] = samples['permId'].map(extract_permid)
samples['experiment'] = samples['experiment'].map(extract_nested_identifier)
samples['sample_type'] = samples['type'].map(extract_nested_permid)
if props is not None:
if isinstance(props, str):
props = [props]
Swen Vermeul
committed
for prop in props:
samples[prop.upper()] = samples['properties'].map(lambda x: x.get(prop.upper(), ''))
attrs.append(prop.upper())
return Things(
openbis_obj = self,
entity = 'sample',
df = samples[attrs],
identifier_name = 'identifier',
start_with=start_with,
count=count,
totalCount=totalCount,
Swen Vermeul
committed
get_object = get_sample # Alias
Swen Vermeul
committed
def get_external_data_management_system(self, permId, only_data=False):
Chandrasekhar Ramakrishnan
committed
"""Retrieve metadata for the external data management system.
:param permId: A permId for an external DMS.
Chandrasekhar Ramakrishnan
committed
:param only_data: Return the result data as a hash-map, not an object.
"""
request = {
"method": "getExternalDataManagementSystems",
"params": [
self.token,
[{
"@type": "as.dto.externaldms.id.ExternalDmsPermId",