|
| 1 | +from __future__ import unicode_literals |
| 2 | + |
| 3 | +from labkey.utils import create_server_context |
| 4 | +from labkey import domain |
| 5 | + |
| 6 | +labkey_server = 'localhost:8080' |
| 7 | +project_name = 'MySamples' # Project folder name |
| 8 | +context_path = 'labkey' |
| 9 | +server_context = create_server_context(labkey_server, project_name, context_path, use_ssl=False) |
| 10 | + |
| 11 | +################### |
| 12 | +# Create a SampleSet domain |
| 13 | +################### |
| 14 | +sampleset_domain_definition = { |
| 15 | + 'kind': 'SampleSet', |
| 16 | + 'domainDesign': { |
| 17 | + 'name': 'BloodSamples', |
| 18 | + 'description': 'Human blood samples.', |
| 19 | + 'fields': [{ |
| 20 | + 'name': "Name", |
| 21 | + 'rangeURI': "string" |
| 22 | + },{ |
| 23 | + 'name': "volume_mL", |
| 24 | + 'rangeURI': "int" |
| 25 | + },{ |
| 26 | + 'name': "Project", |
| 27 | + 'rangeURI': "string" |
| 28 | + },{ |
| 29 | + 'name': "DrawDate", |
| 30 | + 'rangeURI': "dateTime" |
| 31 | + },{ |
| 32 | + 'name': "ReceivedDate", |
| 33 | + 'rangeURI': "dateTime" |
| 34 | + },{ |
| 35 | + 'name': "ReceivedFrom", |
| 36 | + 'rangeURI': "string" |
| 37 | + },{ |
| 38 | + 'name': "ReceivingOperator", |
| 39 | + 'rangeURI': "string" |
| 40 | + },{ |
| 41 | + 'name': "TubeColor", |
| 42 | + 'rangeURI': "string" |
| 43 | + },{ |
| 44 | + 'name': "TubeType", |
| 45 | + 'rangeURI': "string" |
| 46 | + },{ |
| 47 | + 'name': "ProblemWithTube", |
| 48 | + 'rangeURI': "boolean" |
| 49 | + },{ |
| 50 | + 'name': "Comments", |
| 51 | + 'rangeURI': "string" |
| 52 | + }] |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +# domain.create returns the full Domain definition |
| 57 | +created_sampleset_domain = domain.create(server_context, sampleset_domain_definition) |
| 58 | + |
| 59 | +################### |
| 60 | +# Get a domain |
| 61 | +################### |
| 62 | +sampleset_domain = domain.get(server_context, 'samples', 'BloodSamples') |
| 63 | + |
| 64 | +# examine different fields from the domain |
| 65 | +print(sampleset_domain.name) |
| 66 | +print(sampleset_domain.fields[0].name) |
| 67 | + |
| 68 | +################### |
| 69 | +# Save a domain |
| 70 | +################### |
| 71 | +sampleset_domain.add_field({ |
| 72 | + 'name': 'canTransfuse', |
| 73 | + 'rangeURI': 'boolean' |
| 74 | +}) |
| 75 | + |
| 76 | +# Use infer fields to define additional fields |
| 77 | +fields_file = open('data/infer.tsv', 'rb') |
| 78 | +inferred_fields = domain.infer_fields(server_context, fields_file) |
| 79 | + |
| 80 | +for field in inferred_fields: |
| 81 | + sampleset_domain.add_field(field) |
| 82 | + |
| 83 | +domain.save(server_context, 'samples', 'BloodSamples', sampleset_domain) |
| 84 | + |
| 85 | +################### |
| 86 | +# Drop a domain |
| 87 | +################### |
| 88 | + |
| 89 | +drop_response = domain.drop(server_context, 'samples', 'BloodSamples') |
| 90 | +if 'success' in drop_response: |
| 91 | + print('The SampleSet domain was deleted.') |
0 commit comments