Once you have defined your dataset schema (see Overview: Your TDR dataset schema), you can specify the schema of the data in TDR using the Swagger APIs following the step-by-step directions below.
Before you start
Before you start this process, you should have an outline of your data model in mind. See Overview: Your TDR dataset schema for more details and examples.
Things to establish
- What tables are needed to contain your data, and how are they related?
-
What is the “root entity” table?
The root entity table is the table that includes the primary input data for your dataset. In Terra, this often refers to data that will be used as inputs for a workflow.
Making data findable with a standardized schema
The more alike each dataset's schema is, the easier it will be for other people to find useful data in the data repo. Below are some references to help with common ontology and organization.
Defining the schema with a .JSON
The steps and examples below show how to write out a nested JavaScript Object Notation file (JSON) that describes the schema for that dataset - the tables and columns - as well as any relationships between those columns.
.JSON components
- Tables: Names and types for BigQuery tables and columns
- Relationships: Links between columns
Troubleshooting complex JSON formatting
Getting all of the brackets just right in a nested .JSON like this can be a little tricky! To avoid messing up the placement of a comma or a bracket, try a free online .JSON validator.
Remember to authorize Swagger every time you use it This article includes instructions on using API commands through the Swagger UI. All instructions related to Swagger require you to first authenticate yourself whenever you’ve opened a window with the Swagger UI.
Instructions
Click “Authorize” near the top of the page, check all of the boxes in the pop up and hit “Authorize” again, and then input the appropriate credentials to authenticate. Make sure you close the subsequent pop up without clicking the “Sign Out” button.
You should now be able to execute the commands below by clicking the “Try it out” button next to the command of your choice. For a more detailed description of this authentication step, see this article on Authenticating in Swagger.
1. Tables
Tables are declared as JSON objects with keys for
- A name with a max length of 63 characters (matching the regex '^[a-zA-Z0-9][_a-zA-Z0-9]*$')
- An optional partitioning “mode” with corresponding settings
- A list of columns
- An optional list of primary-key column names
- If a column’s name is included in the “primaryKeys” list of its parent table, it will be mapped to a BigQuery column with a REQUIRED mode. Otherwise, the column will be mapped to a NULLABLE mode.
- Important note: Setting column names as "primaryKeys" will not work if the column's "array_of" is set to "true", since primary keys can't be arrays.
The specified table name will be the name of the corresponding BigQuery table.
Reduce query runtime and cost by partitioning large tables
Tables can be partitioned in BigQuery to reduce query runtime and cost. The top-level “partitionMode” key in each table model specifies one of:
- “none”, to create an unpartitioned table
- “date”, to create a table partitioned by either ingest date or the values in a date column
- “int”, to create a table partitioned by the values in an integer column
BigQuery best practices suggest always partitioning your tables, using the ingest date if there is no other meaningful choice in the table’s columns. These options map directly to corresponding BigQuery partition settings; see the BigQuery docs for more details.
- If the “date” mode is specified, “datePartitionOptions” must also be specified. The options are a JSON object with a single key for “column”. The value of that key must be either “datarepo_ingest_date” (to partition by ingest date), or the name of a column in the table with datatype DATE or TIMESTAMP.
- If the “int” mode is specified, “intPartitionOptions” must also be specified. The options are a JSON object with four keys:
- “column”: The name of an INT64 or INTEGER column in the table
- “min”: The smallest value in the column that should be partitioned
- “max”: The largest value in the column that should be partitioned
- “interval”: The range-size to use when dividing values between “min” and “max” into partitions
2. Columns
Table columns are also represented as JSON objects, with keys for the following.
JSON object keys
- A name, with the same restrictions as table names
- A data-type (e.g., string, number, boolean)
- An “array_of” boolean
- A "required" boolean option
Any column with a true value for “array_of” will be mapped to a BigQuery column with a REPEATED mode.
3. Data Types
Data types are set per column (i.e., separately for each column block).
Example JSON (define table columns)
"columns": [{
"name": "sample_id",
"datatype": "string",
"array_of": false
},
{
"name": "BAM_File_Path",
"datatype": "fileref",
"array_of": false
}
]
Setting the data type of strings that are links to files (URIs)
To render a column's cells as links to cloud file paths when you export the snapshot to the data tab of your Terra workspace, you must set the data type to fileref in the schema.
Data types in TDR and BigQuery
Most TDR types “pass-through” to BigQuery types of the same name. A few extra types are supported by the TDR, either as a convenience or to add more semantic information to the table metadata.
TDR Datatype |
BigQuery Type |
Example |
BOOLEAN |
BOOLEAN |
TRUE and FALSE (add info about case sensitivity) |
BYTES |
BYTES |
Variable length binary data |
DATE |
DATE |
'YYYY-[M]M-[D]D' 4-digit year, 1 or 2 digit month and 1 or 2 digit date |
DATETIME |
DATETIME |
YYYY-[M]M-[D]D[( |T)[H]H:[M]M:[S]S[.F]] |
TIME |
TIME |
[H]H:[M]M:[S]S[.DDDDDD|.F] |
TIMESTAMP |
TIMESTAMP |
Format: YYYY-[M]M-[D]D[( |T)[H]H:[M]M:[S]S[.F]][time zone] |
FLOAT |
FLOAT |
|
FLOAT64 |
FLOAT |
|
INTEGER |
INTEGER |
|
INT64 |
INTEGER |
|
NUMERIC |
NUMERIC |
|
STRING |
STRING |
|
TEXT |
STRING |
|
FILEREF |
STRING |
|
DIRREF |
STRING |
Helpful hints for creating a valid JSONUsing a free online .JSON validator can be quite helpful when writing out the full .JSON. If you're struggling to create a valid .JSON, it may help to copy-paste the example code in the Swagger UI request body for that particular API. You can then make changes to the template incrementally while validating each change.
Note: certain parameters - such as "tables", "relationships", and "assets" - are expected to be lists, so make sure you include square brackets: [ ]
Example JSON object
The .JSON object below generates a dataset with two tables, a "subject" table with two columns, and a "sample" table with three columns. The tables and columns are defined in the “tables” section of the .JSON. The relationship between the matching columns is set in the “relationship” section.
Example JSON - Generate two tables connected by matching columns
{
"schema": {
"tables": [{
"name": "sample",
"columns": [{
"name": "sample_id",
"datatype": "string",
"array_of": false
},
{
"name": "BAM_File_path",
"datatype": "fileref",
"array_of": false
}
{
"name": "subject_id",
"datatype": "string",
"array_of": false
}
],
"primaryKey": [],
"partitionMode": "none",
"datePartitionOptions": null,
"intPartitionOptions": null,
"rowCount": null
},
{
"name": "subject",
"columns": [{
"name": "subject_id",
"datatype": "string",
"array_of": false
},
{
"name": "phenotype",
"datatype": "string",
"array_of": false
}
],
"primaryKey": [],
"partitionMode": "none",
"datePartitionOptions": null,
"intPartitionOptions": null,
"rowCount": null
}
],
"relationships": [{
"name": "subject",
"from": {
"table": "subject",
"column": "subject_id"
},
"to": {
"table": "sample",
"column": "subject_id"
}
}]
}
}
This .JSON object can be pasted into the "schema" parameter field of the .JSON used for dataset creation.
Helpful hints for creating a valid JSONUsing a free online .JSON validator can be pretty helpful when writing out the full .JSON. Suppose you're struggling to create a valid .JSON. In that case, it may help to copy-paste the example code in the Swagger UI request body for that particular API and incrementally make changes to the template while validating each change.
Note: certain parameters - such as "tables", "relationships", and "assets" - are expected to be lists. You'll want to make sure you include square brackets: [ ]
How to retrieve the schema of an existing dataset
You can view the .JSON for the schema of any dataset to which you have access using the retrieveDataset API endpoint. If you select "SCHEMA" in the include menu, the response body will contain only the schema for the dataset.
How to update a dataset's schema
You can update the schema of an existing dataset using the updateSchema API endpoint. The endpoint currently supports adding tables, adding non-required columns to an existing table, and adding relationships to an existing dataset.
Currently, you cannot delete or rename a table; just add.
The endpoint requires a description of the change and a list of changes to apply.
updateSchema request body (add new table and column)
{ "description": "Adding a table and column", "changes": { "addTables": [...], "addColumns": [...] } }
Add new tables
The following is an example API request payload to add a new table. Note the items in "addTables" follow the same format as the "tables" in the dataset schema definition.
updateSchema request body (add new table)
{ "description": "Adding a table", "changes": { "addTables": [ { "name": "project", "columns": [ { "name": "id", "datatype": "string", "required": true }, { "name": "collaborators", "datatype": "string", "array_of": true } ], "primaryKey": ["id"] } ] } }
Adding columns to existing tables
The following is an example API request payload to add new columns to existing tables. Note that the new columns cannot be set to required. Multiple tables can be updated in the same request:
updateSchema request body (add columns to an existing table)
{ "description": "Adding columns to existing tables", "changes": { "addColumns": [ { "tableName": "bam_file, "columns": [ { "name": "size", "datatype": "integer" } ] },
{
"tableName": "participant,
"columns": [
{
"name": "age",
"datatype": "integer"
},
{
"name": "weight",
"datatype": "integer"
}
]
} ] } }
Adding relationships to an existing dataset
The following is an example API request payload to add relationships to existing tables in a dataset. Note that the new columns cannot be set to required. Multiple tables can be updated in the same request.
updateSchema request body (add relationships to an existing dataset)
{ "description": "Adding relationships to existing tables", "changes": { "addRelationships": [ { "name": "string", "from": { "table": "string", "column": "string" },
"to": {
"table": "string",
"column": "string"
}
} ] } }