Skip to content
Snippets Groups Projects
Commit e9e1417b authored by Swen Vermeul's avatar Swen Vermeul
Browse files

get_samples altered to get_sample. Changed corresponding tests

parent 470eff57
No related branches found
No related tags found
No related merge requests found
...@@ -105,6 +105,7 @@ class Openbis: ...@@ -105,6 +105,7 @@ class Openbis:
self.token = f.read() self.token = f.read()
if not self.is_token_valid(): if not self.is_token_valid():
self.token = None self.token = None
os.remove(self.token_filename)
except FileNotFoundError: except FileNotFoundError:
self.token = None self.token = None
...@@ -170,6 +171,8 @@ class Openbis: ...@@ -170,6 +171,8 @@ class Openbis:
requiring the user to login again. requiring the user to login again.
:return: Return True if the token is valid, False if it is not valid. :return: Return True if the token is valid, False if it is not valid.
""" """
if self.token is None:
return False
request = { request = {
"method": "isSessionActive", "method": "isSessionActive",
...@@ -189,51 +192,39 @@ class Openbis: ...@@ -189,51 +192,39 @@ class Openbis:
self.token, self.token,
[ [
{ {
"@id": 0,
"permId": permid, "permId": permid,
"@type": "as.dto.dataset.id.DataSetPermId" "@type": "as.dto.dataset.id.DataSetPermId"
} }
], ],
{ {
"@id": 0,
"parents": { "parents": {
"@id": 1,
"@type": "as.dto.dataset.fetchoptions.DataSetFetchOptions" "@type": "as.dto.dataset.fetchoptions.DataSetFetchOptions"
}, },
"children": { "children": {
"@type": "as.dto.dataset.fetchoptions.DataSetFetchOptions" "@type": "as.dto.dataset.fetchoptions.DataSetFetchOptions"
}, },
"containers": { "containers": {
"@id": 3,
"@type": "as.dto.dataset.fetchoptions.DataSetFetchOptions" "@type": "as.dto.dataset.fetchoptions.DataSetFetchOptions"
}, },
"physicalData": { "physicalData": {
"@id": 5,
"@type": "as.dto.dataset.fetchoptions.PhysicalDataFetchOptions" "@type": "as.dto.dataset.fetchoptions.PhysicalDataFetchOptions"
}, },
"linkedData": { "linkedData": {
"@id": 6,
"@type": "as.dto.dataset.fetchoptions.LinkedDataFetchOptions", "@type": "as.dto.dataset.fetchoptions.LinkedDataFetchOptions",
"externalDms": None,
"sort": None
}, },
"dataStore": { "dataStore": {
"@id": 9,
"@type": "as.dto.datastore.fetchoptions.DataStoreFetchOptions", "@type": "as.dto.datastore.fetchoptions.DataStoreFetchOptions",
"sort": None
}, },
"sample": { "sample": {
"@id": 14,
"@type": "as.dto.sample.fetchoptions.SampleFetchOptions" "@type": "as.dto.sample.fetchoptions.SampleFetchOptions"
}, },
"properties": { "properties": {
"@id": 15,
"@type": "as.dto.property.fetchoptions.PropertyFetchOptions" "@type": "as.dto.property.fetchoptions.PropertyFetchOptions"
}, },
"@type": "as.dto.dataset.fetchoptions.DataSetFetchOptions" "@type": "as.dto.dataset.fetchoptions.DataSetFetchOptions"
} }
], ],
"id": "1", "id": permid,
"jsonrpc": "2.0" "jsonrpc": "2.0"
} }
...@@ -243,48 +234,42 @@ class Openbis: ...@@ -243,48 +234,42 @@ class Openbis:
return DataSet(self, permid, resp[permid]) return DataSet(self, permid, resp[permid])
def get_samples(self, sample_identifiers): def get_sample(self, sample_ident):
"""Retrieve metadata for the sample. """Retrieve metadata for the sample.
Get metadata for the sample and any directly connected parents of the sample to allow access 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. 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. :param sample_identifiers: A list of sample identifiers to retrieve.
""" """
if not isinstance(sample_identifiers, list): search_request = None
sample_identifiers = [sample_identifiers]
searches = []
for ident in sample_identifiers:
# assume we got a sample identifier e.g. /TEST/TEST-SAMPLE
match = re.match('/', ident)
if match:
searches.append({
"identifier": ident,
"@type": "as.dto.sample.id.SampleIdentifier"
})
continue
# assume we got a sample identifier e.g. /TEST/TEST-SAMPLE
match = re.match('/', sample_ident)
if match:
search_request = {
"identifier": sample_ident,
"@type": "as.dto.sample.id.SampleIdentifier"
}
else:
# look if we got a PermID eg. 234567654345-123 # look if we got a PermID eg. 234567654345-123
match = re.match('\d+\-\d+', ident) match = re.match('\d+\-\d+', sample_ident)
if match: if match:
searches.append({ search_request = {
"permId": ident, "permId": sample_ident,
"@type": "as.dto.sample.id.SamplePermId" "@type": "as.dto.sample.id.SamplePermId"
}) }
continue else:
raise ValueError(
raise ValueError( '"' + sample_ident + '" is neither a Sample Identifier nor a PermID'
'"' + ident + '" is neither a Sample Identifier nor a PermID' )
)
sample_request = { sample_request = {
"method": "getSamples", "method": "getSamples",
"params": [ "params": [
self.token, self.token,
searches, [
search_request,
],
{ {
"type": { "type": {
"@type": "as.dto.sample.fetchoptions.SampleTypeFetchOptions" "@type": "as.dto.sample.fetchoptions.SampleTypeFetchOptions"
...@@ -293,21 +278,26 @@ class Openbis: ...@@ -293,21 +278,26 @@ class Openbis:
"@type": "as.dto.property.fetchoptions.PropertyFetchOptions" "@type": "as.dto.property.fetchoptions.PropertyFetchOptions"
}, },
"parents": { "parents": {
"@id": 20,
"@type": "as.dto.sample.fetchoptions.SampleFetchOptions", "@type": "as.dto.sample.fetchoptions.SampleFetchOptions",
"properties": { "properties": {
"@type": "as.dto.property.fetchoptions.PropertyFetchOptions" "@type": "as.dto.property.fetchoptions.PropertyFetchOptions"
}
},
"children": {
"@type": "as.dto.sample.fetchoptions.SampleFetchOptions",
"properties": {
"@type": "as.dto.property.fetchoptions.PropertyFetchOptions"
} }
}, },
"dataSets": { "dataSets": {
"@type": "as.dto.dataset.fetchoptions.DataSetFetchOptions", "@type": "as.dto.dataset.fetchoptions.DataSetFetchOptions",
"properties": { "properties": {
"@type": "as.dto.property.fetchoptions.PropertyFetchOptions" "@type": "as.dto.property.fetchoptions.PropertyFetchOptions"
} }
} }
} }
], ],
"id": ident, "id": sample_ident,
"jsonrpc": "2.0" "jsonrpc": "2.0"
} }
return self.post_request(self.v3_as, sample_request) return self.post_request(self.v3_as, sample_request)
...@@ -427,3 +417,49 @@ class DataSet(Openbis): ...@@ -427,3 +417,49 @@ class DataSet(Openbis):
else: else:
raise ValueError('general error while performing post request') raise ValueError('general error while performing post request')
class Sample(Openbis):
"""objects which contain samples"""
def __init__(self, openbis_obj, permid, data):
self.openbis = openbis_obj
self.permid = permid
self.data = data
self.v1_ds = '/datastore_server/rmi-dss-api-v1.json'
self.downloadUrl = self.data['dataStore']['downloadUrl']
@staticmethod
def ensure_folder_exists(folder):
if not os.path.exists(folder):
os.makedirs(folder)
def download(self):
base_url = self.downloadUrl + '/datastore_server/' + self.permid + '/'
for file in self.get_file_list(recursive=True):
if file['isDirectory']:
folder = os.path.join(self.openbis.hostname, self.permid)
DataSet.ensure_folder_exists(folder)
else:
download_url = base_url + file['pathInDataSet'] + '?sessionID=' + self.openbis.token
filename = os.path.join(self.openbis.hostname, self.permid, file['pathInDataSet'])
DataSet.download_file(download_url, filename)
def get_parents(self):
parents = []
for item in self.data['parents']:
parent = self.openbis.get_dataset(item['code'])
if parent is not None:
parents.append(parent)
return parents
def get_children(self):
children = []
for item in self.data['children']:
child = self.openbis.get_dataset(item['code'])
if child is not None:
children.append(child)
return children
...@@ -16,22 +16,21 @@ def test_token(openbis_instance): ...@@ -16,22 +16,21 @@ def test_token(openbis_instance):
assert another_instance.is_token_valid() is True assert another_instance.is_token_valid() is True
def test_get_sample_by_id(openbis_instance):
def test_get_samples_by_id(openbis_instance): response = openbis_instance.get_sample('/TEST/TEST-SAMPLE-2-CHILD-1')
response = openbis_instance.get_samples('/TEST/TEST-SAMPLE-2-CHILD-1')
assert response is not None assert response is not None
assert response['/TEST/TEST-SAMPLE-2-CHILD-1'] is not None assert response['/TEST/TEST-SAMPLE-2-CHILD-1'] is not None
def test_get_samples_by_permid(openbis_instance): def test_get_sample_by_permid(openbis_instance):
response = openbis_instance.get_samples('20130415091923485-402') response = openbis_instance.get_sample('20130415091923485-402')
assert response is not None assert response is not None
assert response['20130415091923485-402'] is not None assert response['20130415091923485-402'] is not None
def test_get_parents(openbis_instance): def test_get_parents(openbis_instance):
id = '/TEST/TEST-SAMPLE-2' id = '/TEST/TEST-SAMPLE-2'
response = openbis_instance.get_samples(id) response = openbis_instance.get_sample(id)
assert response is not None assert response is not None
assert 'parents' in response[id] assert 'parents' in response[id]
assert 'identifier' in response[id]['parents'][0] assert 'identifier' in response[id]['parents'][0]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment