forked from LabKey/labkey-api-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdomain_example.py
More file actions
110 lines (96 loc) · 3.16 KB
/
domain_example.py
File metadata and controls
110 lines (96 loc) · 3.16 KB
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
#
# Copyright (c) 2018 LabKey Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from __future__ import unicode_literals
from labkey.utils import create_server_context
from labkey import domain
labkey_server = 'localhost:8080'
project_name = 'Study' # Project folder name
context_path = 'labkey'
server_context = create_server_context(labkey_server, project_name, context_path, use_ssl=False)
###################
# Create a list domain
###################
list_domain_definition = {
'kind': 'IntList', # or 'VarList'
'domainDesign': {
'name': 'BloodTypes',
'description': 'All known human blood types',
'fields': [{
'name': 'rowId',
'rangeURI': 'int'
}, {
'name': 'type',
'rangeURI': 'string'
}]
},
# 'options' allows for passing properties specific to a domain type (e.g. list, dataset, etc)
'options': {
'keyName': 'rowId',
'keyType': 'AutoIncrementInteger'
}
}
# domain.create returns the full Domain definition
created_list_domain = domain.create(server_context, list_domain_definition)
###################
# Create a study dataset domain
###################
dataset_domain_definition = {
'kind': 'StudyDatasetDate', # or 'StudyDatasetVisit'
'domainDesign': {
'name': 'Blood Levels',
'fields': [{
'name': 'vitd',
'label': 'Vitamin D (ng/mL)',
'rangeURI': 'double'
}, {
'name': 'type',
'rangeURI': 'int',
'lookupSchema': 'lists',
'lookupQuery': 'BloodTypes',
'setMeasure': True
}]
}
}
dataset_domain = domain.create(server_context, dataset_domain_definition)
###################
# Get a domain
###################
list_domain = domain.get(server_context, 'lists', 'BloodTypes')
# examine different from the domain
print(list_domain.name)
print(list_domain.fields[0].name)
###################
# Save a domain
###################
list_domain.add_field({
'name': 'canTransfuse',
'rangeURI': 'boolean'
})
# Use infer fields to define additional fields
fields_file = open('data/infer.tsv', 'rb')
inferred_fields = domain.infer_fields(server_context, fields_file)
for field in inferred_fields:
list_domain.add_field(field)
domain.save(server_context, 'lists', 'BloodTypes', list_domain)
###################
# Drop a domain
###################
drop_response = domain.drop(server_context, 'study', 'Blood Levels')
if 'success' in drop_response:
print('The dataset domain was deleted.')
drop_response = domain.drop(server_context, 'lists', 'BloodTypes')
if 'success' in drop_response:
print('The list domain was deleted.')