-
Notifications
You must be signed in to change notification settings - Fork 3
/
table_json.py
executable file
·139 lines (117 loc) · 4.17 KB
/
table_json.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env python
import argparse
import csv
import json
import re
from ast import literal_eval
FIELDNAMES = ['Field','Definition','Ontology ID','Data Type','Values','Example']
SEPARATOR = ';'
QUOTE = '"'
def string_list_to_list(string):
to_list = [n.strip() for n in string.split(SEPARATOR)]
return to_list
def interface_label_to_property_key(interface_label):
property_key = re.sub(r'[^\w {}]', '_', interface_label).replace(' ', '_').replace('__', '_').lower()
property_key = re.sub(r'_$', '', property_key)
return property_key
def parse_properties_table(path_to_properties_table):
datatype_map = {
"String": "string",
"Date": "string",
"Int": "integer",
"Float": "number",
"Email": "string",
"Bioproject_ID": "string",
"Biosample_ID": "string",
"SRA_ID": "string",
"Genbank_ID": "string",
"GISAID_ID": "string",
"Enums":{
"type": "string",
"enum": "",
},
"Integer_or_Range": [
{
"type": "integer",
},
{
"type": "string",
"pattern": "\\d+-\\d+",
}
],
}
format_map = {
"String": None,
"Date": "date",
"Int": None,
"Float": None,
"Email": "email",
"Bioproject_ID": None,
"Biosample_ID": None,
"SRA_ID": None,
"Genbank_ID": None,
"GISAID_ID": None,
"Integer_or_Range": None,
'Enums': None
}
pattern_map = {
"String": None,
"Date": None,
"Int": None,
"Float": None,
"Email": None,
"Bioproject_ID": "^PRJ(N|E|D)([a-zA-Z]?)[0-9]+*",
"Biosample_ID": "^SAM(D|N|E([AG]?))[0-9]+",
"SRA_ID": "^(SRR|ERR|DRR)[0-9]+",
"Genbank_ID": "^([a-zA-Z]{2})\d*.\d{1}",
"GISAID_ID": "^EPI_ISL_\d*",
"Integer_or_Range": None,
"Enums": None
}
properties = {}
with open(path_to_properties_table) as f:
reader = csv.DictReader(f,
delimiter=SEPARATOR,
quotechar=QUOTE)
for row in reader:
print(row)
property_key = interface_label_to_property_key(row['Field'])
properties[property_key] = {}
properties[property_key]['description'] = row['Definition']
properties[property_key]['ontology'] = row['Ontology ID']
type = datatype_map[row['Data Type']]
properties[property_key]['type'] = type
format = format_map[row['Data Type']]
if format:
properties[property_key]['format'] = format
pattern = pattern_map[row['Data Type']]
if pattern:
properties[property_key]['pattern'] = pattern
examples = list(map(str.strip, row['Example'].split(';'))) # examples separated by semicolon
for i in range(len(examples)):
if properties[property_key]['type'] == 'integer':
examples[i] = int(examples[i])
elif properties[property_key]['type'] == 'number':
examples[i] = float(examples[i])
# Special case: enumns
if row['Data Type'] == "Enums":
type = datatype_map[row['Data Type']]
properties[property_key]['type'] = "string"
properties[property_key]['Enums'] = string_list_to_list(row['Values'])
properties[property_key]['examples'] = examples
return properties
def main(args):
schema = {
"$schema": "http://json-schema.org/draft/2019-09/schema#",
"type": "object",
"properties": {},
}
schema["properties"] = parse_properties_table(args.input)
#print(json.dumps(schema))
with open("QC_Contextual_Data_Tags_Specification.json", "w") as fh:
fh.write(json.dumps(schema))
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('input', help='Input schema attributes table')
args = parser.parse_args()
main(args)