-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathsplit-cob-edit.py
More file actions
executable file
·99 lines (83 loc) · 3.41 KB
/
split-cob-edit.py
File metadata and controls
executable file
·99 lines (83 loc) · 3.41 KB
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
#!/usr/bin/env python3
#
# Split the cob-edit.tsv file into three ROBOT templates:
# cob-base.tsv cob-full.tsv cob-root.tsv
import argparse
import csv
import os
import re
def initialize_tsv(output_dir, filename, fieldnames, robot_row):
'''Initialize and return a DictWriter for a ROBOT template.'''
os.makedirs(output_dir, exist_ok=True)
path = os.path.join(output_dir, filename)
path_fh = open(path, 'w')
writer = csv.DictWriter(path_fh, fieldnames, delimiter='\t', lineterminator='\n', extrasaction='ignore')
writer.writeheader()
writer.writerow(robot_row)
return writer
def split(input_path, output_dir):
'''Split the COB term template into base, full, and root templates.'''
header_row = None
robot_row, terms, replacements = {}, {}, {}
with open(input_path) as f:
rows = csv.DictReader(f, delimiter='\t')
for row in rows:
if not header_row:
header_row = list(row.keys())
id = row['Ontology ID'].strip()
if id == '':
continue
if id == 'ID':
robot_row = row
continue
# Handle the Replacement columns
# by collecting a dictionary of replacements.
replacement_id = row['Replacement ID'].strip()
replacement_label = row['Replacement Label'].strip()
if replacement_label and replacement_label != '':
# removed exception for non-base IDs... TBD if needed/wanted
replacements[row['Label'].strip()] = {
'ID': id,
'Replacement Label': replacement_label,
'Replacement ID': replacement_id,
}
# Otherwise just add this row to 'terms'.
else:
terms[id] = row
ignore = [
'COB Module', 'COB Module Reason',
'Replacement ID', 'Replacement Label',
'Comment'
]
fieldnames = [x for x in header_row if x not in ignore]
# Apply replacements to the axioms in all the logical columns
axiom_cols = [
'Parent Class', 'Subclass Axiom', 'Equivalent Class Axiom',
'Disjoint Class', 'Parent Property', 'Domain', 'Range',
'Inverse Property'
]
for id, row in terms.items():
for repl_label, repl_dict in replacements.items():
for col in axiom_cols:
if col not in row:
continue
row[col] = re.sub(repl_label, repl_dict['Replacement Label'], row[col])
base_writer = initialize_tsv(output_dir, 'cob-base.tsv', fieldnames, robot_row)
full_writer = initialize_tsv(output_dir, 'cob-full.tsv', fieldnames, robot_row)
root_writer = initialize_tsv(output_dir, 'cob-root.tsv', fieldnames, robot_row)
for id, row in terms.items():
module = row['COB Module'].strip()
if module == 'BASE':
base_writer.writerow(row)
if module not in ['', 'ROOT']:
full_writer.writerow(row)
if module != '':
root_writer.writerow(row)
def main():
parser = argparse.ArgumentParser(description='Split the main COB term template into multiple module templates')
parser.add_argument('input', type=str, help='The input COB term template')
parser.add_argument('outdir', type=str, help='The output directory')
args = parser.parse_args()
split(args.input, args.outdir)
if __name__ == '__main__':
main()