|
| 1 | +"""Produce BigQuery schema JSON file from metadata convention tsv file |
| 2 | +
|
| 3 | +DESCRIPTION |
| 4 | +This CLI takes a tsv metadata convention and creates a BigQuery schema JSON file. |
| 5 | +
|
| 6 | +EXAMPLE |
| 7 | +$ python convention_to_bq_schema.py metadata_convention.tsv |
| 8 | +
|
| 9 | +""" |
| 10 | + |
| 11 | +import argparse |
| 12 | +import csv |
| 13 | +import os |
| 14 | +import json |
| 15 | + |
| 16 | +REQUIRED_FIELDS = ['CellID', 'biosample_id', 'donor_id'] |
| 17 | + |
| 18 | + |
| 19 | +def create_parser(): |
| 20 | + """ |
| 21 | + Command Line parser for serialize_convention |
| 22 | +
|
| 23 | + Input: metadata convention tsv file |
| 24 | + """ |
| 25 | + # create the argument parser |
| 26 | + parser = argparse.ArgumentParser( |
| 27 | + description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter |
| 28 | + ) |
| 29 | + parser.add_argument('--output-path', '-p', help='Path for output file') |
| 30 | + parser.add_argument('input_convention', help='Metadata convention tsv file') |
| 31 | + return parser |
| 32 | + |
| 33 | + |
| 34 | +def process_row_type(type_info): |
| 35 | + type_map = { |
| 36 | + 'integer': 'integer', |
| 37 | + 'boolean': 'boolean', |
| 38 | + 'string': 'string', |
| 39 | + 'number': 'float', |
| 40 | + } |
| 41 | + return type_map.get(type_info).upper() |
| 42 | + |
| 43 | + |
| 44 | +def build_schema(input_convention): |
| 45 | + """ |
| 46 | + Build schema as a Python dictionary |
| 47 | + """ |
| 48 | + |
| 49 | + with open(input_convention) as tsvfile: |
| 50 | + reader = csv.DictReader(tsvfile, dialect='excel-tab') |
| 51 | + schema = [] |
| 52 | + for row in reader: |
| 53 | + entry = {} |
| 54 | + entry['name'] = row['attribute'] |
| 55 | + entry['type'] = process_row_type(row['type']) |
| 56 | + if row['attribute'] in REQUIRED_FIELDS: |
| 57 | + entry['mode'] = 'REQUIRED' |
| 58 | + # handle arrays of values |
| 59 | + if row['array']: |
| 60 | + entry['type'] = process_row_type(row['type']) |
| 61 | + entry['mode'] = 'REPEATED' |
| 62 | + else: |
| 63 | + entry['mode'] = 'NULLABLE' |
| 64 | + schema.append(entry) |
| 65 | + return schema |
| 66 | + |
| 67 | + |
| 68 | +def add_scp_fields_to_schema(schema): |
| 69 | + scp_entry = {'name': 'scp_accession', 'type': 'string', 'mode': 'REQUIRED'} |
| 70 | + schema.append(scp_entry) |
| 71 | + return schema |
| 72 | + |
| 73 | + |
| 74 | +def generate_output_name(inputname, path='', label='bq_schema'): |
| 75 | + """ |
| 76 | + Build output filename from inputname |
| 77 | + """ |
| 78 | + head, tail = os.path.split(inputname) |
| 79 | + name, suffix = os.path.splitext(tail) |
| 80 | + if label: |
| 81 | + labeledName = '.'.join([name, label, 'json']) |
| 82 | + else: |
| 83 | + labeledName = '.'.join([name, 'json']) |
| 84 | + if path: |
| 85 | + outputname = '/'.join([path, labeledName]) |
| 86 | + elif head: |
| 87 | + outputname = '/'.join([head, labeledName]) |
| 88 | + else: |
| 89 | + outputname = labeledName |
| 90 | + return outputname |
| 91 | + |
| 92 | + |
| 93 | +def write_schema(data, inputname, filepath=''): |
| 94 | + """ |
| 95 | + write BigQuery schema as json file |
| 96 | + """ |
| 97 | + filename = generate_output_name(inputname, filepath) |
| 98 | + with open(filename, 'w') as jsonfile: |
| 99 | + json.dump(data, jsonfile, sort_keys=True, indent=4) |
| 100 | + |
| 101 | + |
| 102 | +if __name__ == '__main__': |
| 103 | + args = create_parser().parse_args() |
| 104 | + input_convention = args.input_convention |
| 105 | + output_path = args.output_path |
| 106 | + schema = build_schema(input_convention) |
| 107 | + schema = add_scp_fields_to_schema(schema) |
| 108 | + write_schema(schema, input_convention, output_path) |
0 commit comments