How to create a dataset schema (TDR)

Anton Kovalsky
  • Updated

Before you can run the createDataset request (see How to create a dataset), you need to define the structure of the data you’ll be ingesting by specifying the schema of the data.

The schema’s function is to outline

  • The tables that contain the data (i.e. sample, subject, etc.)
  • The metadata (columns) in each table (i.e. BAM file, subject_age)
  • Any relationships between tables (i.e. what samples correspond to which subjects)

The schema is a template for the data you’ll ingest later

You’ll specify the number and names of the data categories – the tables and columns within the tables – and any associations between columns in separate tables, if multiple tables contain the same data category (for instance, if you have a “subject” table and a “sample” table, and both tables contain a column of the same subject IDs).

The schema is defined with a JSON

You will input a nested JavaScript Object Notation (JSON) object file with your schema in the “schema” section of the request body of the createDataset Swagger API endpoint. Below is more information on dataset schemas and step-by-step instructions for formatting the schema .JSON you will need in the create a dataset step. 

Dataset schema overview

Tabular data in the TDR must be organized with a dataset schema. The dataset schema defines tables of primary data and metadata(e.g. “Subject” and "Sample" and "Demographics" tables in the diagram below). Each table is connected to other tables in the dataset by unique IDs. 

TDR-example-schema.png

A schema includes

  • Entities (i.e. tables)
    The entity is the primary object the table contains. Each row in the table is a distinct entity with a unique ID key. (i.e. a “subject” entity for phenotypic data or “sample” entity for genomic data). 
  • Metadata attributes (i.e. columns)
    Each table column contains a different sort of metadata (i.e. age or ancestry or lab results in the subject table or links to genomic data files in cloud storage in the sample table)
  • Associations 
    The unique identifiers that link data between tables (i.e. a subject_id column in the sample table that links samples with the subject)

Defining the schema with a .JSON

The steps and examples below show how to write out a nested .JSON that describes the schema for that dataset - the tables and columns - as well as any relationships between those columns.

.JSON components

  1. Tables: Names and types for BigQuery tables and columns
  2. Relationships: Links between columns

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. 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 their 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:

  • A name, with the same restrictions as table names
  • A data-type (e.g. string, number, boolean)
  • An “array_of” boolean

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)

One of the most important data types is fileref. You must set this data type in the schema if you want that column's cells to be rendered as links to cloud file paths when you export the snapshot to the data tab of your Terra workspace.

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]]
(more details here)

TIME

TIME

[H]H:[M]M:[S]S[.DDDDDD|.F]
(more details here)

TIMESTAMP

TIMESTAMP

Format: YYYY-[M]M-[D]D[( |T)[H]H:[M]M:[S]S[.F]][time zone]
(more details here)

FLOAT

FLOAT

 

FLOAT64

FLOAT

 

INTEGER

INTEGER

 

INT64

INTEGER

 

NUMERIC

NUMERIC

 

STRING

STRING

 

TEXT

STRING

 

FILEREF

STRING

 

DIRREF

STRING

 

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 the columns they contain are defined in the “tables section of the .JSON. The relationship between the matching columns is set in the relationship section.

TDR_Relationship-between-subject-table-and-sample-table.png

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 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, and 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: [ ] 

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.

2021-09-21_09-27-28.png

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 and adding non-required columns to an existing table. 

The endpoint requires a description of the change being made along with a list of changes to apply. The changes can include adding new tables, new columns, or both.

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"
}
]
} ] } }

Was this article helpful?

0 out of 0 found this helpful

Have more questions? Submit a request

Comments

0 comments

Please sign in to leave a comment.