Skip to content

Commit 730e175

Browse files
authored
Merge pull request #1271 from Sage-Bionetworks/new_tutorial
Add Create JSON Schema tutorial
2 parents f3e52e3 + 63234bb commit 730e175

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
JSON Schema is a tool used to validate data. In Synapse, JSON Schemas can be used to validate the metadata applied to an entity such as project, file, folder, table, or view, including the [annotations](https://help.synapse.org/docs/Annotating-Data-With-Metadata.2667708522.html) applied to it. To learn more about JSON Schemas, check out [JSON-Schema.org](https://json-schema.org/).
2+
3+
Synapse supports a subset of features from [json-schema-draft-07](https://json-schema.org/draft-07). To see the list of features currently supported, see the [JSON Schema object definition](https://rest-docs.synapse.org/rest/org/sagebionetworks/repo/model/schema/JsonSchema.html) from Synapse's REST API Documentation.
4+
5+
In this tutorial, you will learn how to create these JSON Schema using an existing data model.
6+
7+
## Tutorial Purpose
8+
You will create a JSON schema using your data model.
9+
10+
## Prerequisites
11+
* You have a working [installation](../installation.md) of the Synapse Python Client.
12+
* You have a data model, see this [example data model](https://github.com/Sage-Bionetworks/schematic/blob/develop/tests/data/example.model.column_type_component.csv).
13+
14+
## 1. Imports
15+
16+
```python
17+
{!docs/tutorials/python/tutorial_scripts/schema_operations.py!lines=1-2}
18+
```
19+
20+
## 2. Set up your variables
21+
22+
```python
23+
{!docs/tutorials/python/tutorial_scripts/schema_operations.py!lines=4-10}
24+
```
25+
26+
To create a JSON Schema you need a data model, and the data types you want to create.
27+
The data model must be in either CSV or JSON-LD form. The data model may be a local path or a URL.
28+
[Example data model](https://github.com/Sage-Bionetworks/schematic/blob/develop/tests/data/example.model.column_type_component.csv).
29+
30+
The data types must exist in your data model. This can be a list of data types, or `None` to create all data types in the data model.
31+
32+
## 3. Log into Synapse
33+
```python
34+
{!docs/tutorials/python/tutorial_scripts/schema_operations.py!lines=12-13}
35+
```
36+
37+
38+
## 4. Create the JSON Schema
39+
Create the JSON Schema(s)
40+
```python
41+
{!docs/tutorials/python/tutorial_scripts/schema_operations.py!lines=15-23}
42+
```
43+
You should see the first JSON Schema for the datatype(s) you selected printed.
44+
It will look like [this schema](https://repo-prod.prod.sagebase.org/repo/v1/schema/type/registered/dpetest-test.schematic.Patient).
45+
46+
47+
## Source Code for this Tutorial
48+
49+
<details class="quote">
50+
<summary>Click to show me</summary>
51+
52+
```python
53+
{!docs/tutorials/python/tutorial_scripts/schema_operations.py!}
54+
```
55+
</details>
56+
57+
58+
## Reference
59+
- [JSON Schema Object Definition](https://rest-docs.synapse.org/rest/org/sagebionetworks/repo/model/schema/JsonSchema.html)
60+
- [JSON Schema Draft 7](https://json-schema.org/draft-07)
61+
- [JSON-Schema.org](https://json-schema.org/)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from synapseclient import Synapse
2+
from synapseclient.extensions.curator import generate_jsonschema
3+
4+
# Path or URL to your data model (CSV or JSONLD format)
5+
# Example: "path/to/my_data_model.csv" or "https://raw.githubusercontent.com/example.csv"
6+
DATA_MODEL_SOURCE = "tests/unit/synapseclient/extensions/schema_files/example.model.csv"
7+
# List of component names/data types to create schemas for, or None for all components/data types
8+
# Example: ["Patient", "Biospecimen"] or None
9+
DATA_TYPE = ["Patient"]
10+
# Directory where JSON Schema files will be saved
11+
OUTPUT_DIRECTORY = "./"
12+
13+
syn = Synapse()
14+
syn.login()
15+
16+
schemas, file_paths = generate_jsonschema(
17+
data_model_source=DATA_MODEL_SOURCE,
18+
output_directory=OUTPUT_DIRECTORY,
19+
data_type=DATA_TYPE,
20+
data_model_labels="class_label",
21+
synapse_client=syn,
22+
)
23+
24+
print(schemas[0])

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ nav:
4444
# - Team: tutorials/python/team.md
4545
- Upload data in bulk: tutorials/python/upload_data_in_bulk.md
4646
- Download data in bulk: tutorials/python/download_data_in_bulk.md
47+
- Creating JSON Schema: tutorials/python/schema_operations.md
4748
- Working with JSON Schema: tutorials/python/json_schema.md
4849
# - Move Files and Folders: tutorials/python/move_files_and_folders.md
4950
# - Migrate data to other storage locations: tutorials/python/migrate_data_to_other_storage_locations.md

0 commit comments

Comments
 (0)