Skip to content

Commit

Permalink
Do not hardcode location of taxa.yaml file.
Browse files Browse the repository at this point in the history
Make the scripts/taxa.py script accept a command-line argument
indicating the location of the taxa.yaml file, instead of hardcoding
that location at the beginning of the script. Update the invocations of
that script in the Makefile accordingly.
  • Loading branch information
gouttegd committed Jan 8, 2025
1 parent 9a710df commit c4bbd7b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
6 changes: 3 additions & 3 deletions src/ontology/uberon.Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -1173,7 +1173,7 @@ $(TMPDIR)/composite-%.owl: $(TMPDIR)/collected-%.owl $(TMPDIR)/tax-merges.tsv |
# Step 2a: The "tax-merges.tsv" file used in the rule above is automatically
# derived from the list of species in config/taxa.yaml.
$(TMPDIR)/tax-merges.tsv: $(SCRIPTSDIR)/taxa.py config/taxa.yaml
python3 $(SCRIPTSDIR)/taxa.py make-merge-table > $@
python3 $(SCRIPTSDIR)/taxa.py make-merge-table config/taxa.yaml > $@

# Step 3: Annotate the result of step 2. This is a separate step only so
# that we can have explicit rules for composite-metazoan, -vertebrate,
Expand Down Expand Up @@ -1329,8 +1329,8 @@ EXTERN_BRIDGES = $(BRIDGEDIR)/uberon-bridge-to-mba.owl \
# 1. Prepare the ruleset file.
# The ruleset file is maintained with the help of the Python script
# to automatically insert the taxon-specific rules.
$(TMPDIR)/bridges.rules: $(SCRIPTSDIR)/taxa.py $(BRIDGEDIR)/bridges.rules
python3 $(SCRIPTSDIR)/taxa.py make-rules $(BRIDGEDIR)/bridges.rules > $@
$(TMPDIR)/bridges.rules: $(SCRIPTSDIR)/taxa.py config/taxa.yaml $(BRIDGEDIR)/bridges.rules
python3 $(SCRIPTSDIR)/taxa.py make-rules config/taxa.yaml $(BRIDGEDIR)/bridges.rules > $@

# 2. Generate the bridges from the "meta" mapping set and the CL set.
# Note that merging CL here is not strictly necessary, but doing so
Expand Down
39 changes: 22 additions & 17 deletions src/scripts/taxa.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,7 @@
import sys
import yaml

with open('config/taxa.yaml', 'r') as f:
taxa = yaml.load(f, yaml.CLoader)


def generate_prefix_declarations(f):
def generate_prefix_declarations(f, taxa):
"""Insert SSSOM/T-OWL prefix declarations for all species."""
for taxon in taxa['species']:
for bridge in taxon['bridging']:
Expand All @@ -50,13 +46,13 @@ def generate_prefix_declarations(f):
f.write(f"prefix {prefix}: <{namespace}>\n")


def generate_directives(f):
def generate_directives(f, taxa):
"""Insert SSSOM/T-OWL taxon declarations for all species."""
for taxon_id in [t['taxon_id'] for t in taxa['species']]:
f.write(f"declare({taxon_id});\n")


def generate_bridging_rules(f):
def generate_bridging_rules(f, taxa):
"""Insert SSSOM/T-OWL bridging rules for all species."""
for taxon in taxa['species']:
taxon_id = taxon['taxon_id']
Expand All @@ -78,23 +74,23 @@ def generate_bridging_rules(f):
""")


def process_rule_file(filein, fileout):
def process_rule_file(filein, fileout, taxa):
"""Insert all SSSOM/T-OWL instructions required to generate
cross-species bridges.
"""

for line in filein:
if line.strip() == '%INSERT-TAX-SPECIFIC-PREFIXES':
generate_prefix_declarations(fileout)
generate_prefix_declarations(fileout, taxa)
elif line.strip() == '%INSERT-TAX-SPECIFIC-DIRECTIVES':
generate_directives(fileout)
generate_directives(fileout, taxa)
elif line.strip() == '%INSERT-TAX-SPECIFIC-BRIDGES':
generate_bridging_rules(fileout)
generate_bridging_rules(fileout, taxa)
else:
fileout.write(line)


def generate_merge_table(fileout):
def generate_merge_table(fileout, taxa):
"""Generates a batch-file for uberon:merge-species."""
def_link_properties = taxa.get('defaults', {}).get('compositing', {}).get('unfold_over', ['BFO:0000050', 'BFO:0000066'])
def_preserved_properties = taxa.get('defaults', {}).get('compositing', {}).get('preserve', [])
Expand All @@ -108,17 +104,26 @@ def generate_merge_table(fileout):
fileout.write(f"{taxon_id}\t{label}\t{link_properties}\t{preserved_properties}\n")


usage = f"Usage: {sys.argv[0]} <make-rules TEMPLATE> | <make-merge-table>"
def read_taxa_file(file):
with open(file, 'r') as f:
return yaml.load(f, yaml.CLoader)


usage = f"Usage: {sys.argv[0]} <make-rules TAXA TEMPLATE> | <make-merge-table TAXA>"
if len(sys.argv) < 2:
sys.exit(usage)

cmd = sys.argv[1]
if cmd == 'make-rules':
if len(sys.argv) != 3:
if len(sys.argv) != 4:
sys.exit(usage)
with open(sys.argv[2], 'r') as fin:
process_rule_file(fin, sys.stdout)
taxa = read_taxa_file(sys.argv[2])
with open(sys.argv[3], 'r') as fin:
process_rule_file(fin, sys.stdout, taxa)
elif cmd == 'make-merge-table':
generate_merge_table(sys.stdout)
if len(sys.argv) != 3:
sys.exit(usage)
taxa = read_taxa_file(sys.argv[2])
generate_merge_table(sys.stdout, taxa)
else:
sys.exit(usage)

0 comments on commit c4bbd7b

Please sign in to comment.