Skip to content

Commit

Permalink
Add workflow to make OBI Excel sheet for editing templates (#1351)
Browse files Browse the repository at this point in the history
* Add workflow to make OBI Excel sheet for templates

* Add workflow to README
  • Loading branch information
beckyjackson authored May 13, 2021
1 parent 1e0818a commit d9b8be2
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
build/
catalog-v001.xml
junk/
*.xlsx
12 changes: 12 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,22 @@ MODULE_NAMES := assays\
sequence-analysis\
value-specifications
MODULE_FILES := $(foreach x,$(MODULE_NAMES),src/ontology/modules/$(x).owl)
TEMPLATE_FILES := $(foreach x,$(MODULE_NAMES),src/ontology/templates/$(x).tsv)

.PHONY: modules
modules: $(MODULE_FILES)

obi.xlsx: src/scripts/tsv2xlsx.py $(TEMPLATE_FILES)
python3 $< $@ $(wordlist 2,100,$^)

.PHONY: update-tsv
update-tsv: update-tsv-files sort

.PHONY: update-tsv-files
update-tsv-files:
$(foreach x,$(MODULE_NAMES),python3 src/scripts/xlsx2tsv.py obi.xlsx $(x) src/ontology/templates/$(x).tsv;)



### Build
#
Expand Down
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,25 @@ Our ontology terms come in three groups. Depending on what type of term you want

See below for a full list of files, build instructions, and instructions on using Git and GitHub for OBI.

### Editing Templates in Excel

If you wish to edit a template or templates in Excel, rather than copy & pasting the template, we ask that you follow this workflow to preserve quoting. Going back and forth with Excel can cause some unintentional changes to double quotes within templates.

First, install the python requirements:
```
python3 -m pip install -r requirements.txt
```

Then, make the Excel sheet:
```
make obi.xlsx
```

Edit the sheet, save it, and finally, run the following to update the TSV versions of the templates:
```
make update-tsv
```

### Finding Terms

To find where a term lives, you can use [`src/scripts/locate.py`](src/scripts/locate.py). This requires you first to build a database from the merged OBI file:
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
openpyxl
25 changes: 25 additions & 0 deletions src/scripts/tsv2xlsx.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env python3

import argparse
import csv
import os

from openpyxl import Workbook

parser = argparse.ArgumentParser(description="Merge TSV files into Excel")
parser.add_argument("output", type=str, help="Excel file to create")
parser.add_argument("inputs", type=str, nargs="+", help="TSV files to include")
args = parser.parse_args()

wb = Workbook()
wb.remove(wb.active)

for path in args.inputs:
title, extension = os.path.splitext(os.path.basename(path))
ws = wb.create_sheet(title)
with open(path, "r") as tsvin:
tsv = csv.reader(tsvin, delimiter="\t", quoting=csv.QUOTE_NONE, escapechar='"')
for row in tsv:
ws.append(row[:])

wb.save(args.output)
31 changes: 31 additions & 0 deletions src/scripts/xlsx2tsv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env python3

import argparse
import csv
import os

from openpyxl import load_workbook

parser = argparse.ArgumentParser(description="Read TSV from Excel")
parser.add_argument("input", type=str, help="Excel file to read")
parser.add_argument("sheet", type=str, help="Sheet name to read")
parser.add_argument("output", type=str, help="TSV file to output")
args = parser.parse_args()

wb = load_workbook(args.input)
ws = wb[args.sheet]

rows = []
for row in ws:
values = []
for cell in row:
if cell.value is None:
values.append("")
else:
values.append(cell.value)
if row:
rows.append(values)

with open(args.output, "w") as f:
writer = csv.writer(f, delimiter="\t", lineterminator="\n", quoting=csv.QUOTE_NONE, escapechar='"')
writer.writerows(rows)

0 comments on commit d9b8be2

Please sign in to comment.