|
1 | 1 | import argparse |
| 2 | +import csv |
2 | 3 | import json |
| 4 | +from pathlib import Path |
3 | 5 | from string import Template |
4 | 6 |
|
5 | | -import pandas as pd |
6 | | -from ruamel.yaml import YAML |
| 7 | + |
| 8 | +SCRIPT_DIR = Path(__file__).resolve().parent |
| 9 | +CONFIG_PATH = SCRIPT_DIR.parent / "config" / "db_graph_atlas.yaml" |
| 10 | +OUTPUT_PATH = SCRIPT_DIR.parent / "templates" / "linkouts.tsv" |
| 11 | +ATLAS_LINK = Template("http://atlas.brain-map.org/atlas?atlas=$atlas_id#structure=$structure_id") |
| 12 | + |
| 13 | + |
| 14 | +def is_local_homba_term(node_id, prefix): |
| 15 | + return prefix and str(node_id).startswith(prefix) and not str(node_id).endswith("_ENTITY") |
| 16 | + |
| 17 | + |
| 18 | +def is_numeric_homba_id(local_id): |
| 19 | + # Numeric HOMBA accessions correspond to DHBA terms; AA accessions are HOMBA-only groupings. |
| 20 | + return str(local_id).isdigit() |
| 21 | + |
| 22 | + |
| 23 | +def parse_scalar(value): |
| 24 | + value = value.strip() |
| 25 | + if value.startswith(("'", '"')) and value.endswith(("'", '"')): |
| 26 | + return value[1:-1] |
| 27 | + return value |
| 28 | + |
| 29 | + |
| 30 | +def load_simple_yaml(path): |
| 31 | + config = {} |
| 32 | + current_section = None |
| 33 | + current_atlas = None |
| 34 | + |
| 35 | + for raw_line in path.read_text().splitlines(): |
| 36 | + if not raw_line.strip() or raw_line.lstrip().startswith("#"): |
| 37 | + continue |
| 38 | + |
| 39 | + indent = len(raw_line) - len(raw_line.lstrip(" ")) |
| 40 | + stripped = raw_line.strip() |
| 41 | + |
| 42 | + if indent == 0 and stripped.endswith(":"): |
| 43 | + current_section = stripped[:-1] |
| 44 | + config[current_section] = {"atlases": []} |
| 45 | + current_atlas = None |
| 46 | + continue |
| 47 | + |
| 48 | + if current_section is None: |
| 49 | + continue |
| 50 | + |
| 51 | + if indent == 2 and stripped.endswith(":"): |
| 52 | + continue |
| 53 | + |
| 54 | + if indent == 2 and ":" in stripped: |
| 55 | + key, value = stripped.split(":", 1) |
| 56 | + config[current_section][key.strip()] = parse_scalar(value) |
| 57 | + continue |
| 58 | + |
| 59 | + if indent == 4 and stripped.startswith("-"): |
| 60 | + current_atlas = {} |
| 61 | + config[current_section]["atlases"].append(current_atlas) |
| 62 | + remainder = stripped[1:].strip() |
| 63 | + if remainder: |
| 64 | + key, value = remainder.split(":", 1) |
| 65 | + current_atlas[key.strip()] = parse_scalar(value) |
| 66 | + continue |
| 67 | + |
| 68 | + if indent == 6 and ":" in stripped and current_atlas is not None: |
| 69 | + key, value = stripped.split(":", 1) |
| 70 | + current_atlas[key.strip()] = parse_scalar(value) |
| 71 | + |
| 72 | + return config |
| 73 | + |
7 | 74 |
|
8 | 75 | parser = argparse.ArgumentParser(description="Generate HOMBA linkout template.") |
9 | 76 | parser.add_argument("filepath", help="Path to the json version of the ontology") |
|
13 | 80 | ontology_json = json.loads(f.read()) |
14 | 81 |
|
15 | 82 | graph = ontology_json["graphs"][0] |
16 | | -with open("../config/db_graph_atlas.yaml", "r") as conf: |
17 | | - mapping = YAML(typ="safe").load(conf.read()) or {} |
18 | | - |
19 | | -link = Template("http://atlas.brain-map.org/atlas?atlas=$atlas_id#structure=$structure_id") |
| 83 | +mapping = load_simple_yaml(CONFIG_PATH) |
20 | 84 |
|
21 | 85 | seed = { |
22 | 86 | "ID": "ID", |
23 | | - "xref": "A OboInOwl:hasDbXref", |
| 87 | + "dhba_xref": "A OboInOwl:hasDbXref", |
| 88 | + "atlas_link": "A rdfs:seeAlso", |
24 | 89 | "prefLabel": "A skos:prefLabel", |
25 | 90 | } |
26 | 91 |
|
|
30 | 95 | if node.get("type") != "CLASS" or "lbl" not in node: |
31 | 96 | continue |
32 | 97 |
|
33 | | - matched = False |
| 98 | + node_id = str(node["id"]) |
| 99 | + pref_label = node["lbl"] |
| 100 | + tab.append({"ID": node_id, "dhba_xref": "", "atlas_link": "", "prefLabel": pref_label}) |
| 101 | + |
34 | 102 | for _, cfg in mapping.items(): |
35 | 103 | prefix = cfg.get("prefix") |
36 | | - if prefix and str(node["id"]).startswith(prefix) and not str(node["id"]).endswith("_ENTITY"): |
37 | | - atlases = cfg.get("atlases", []) |
38 | | - if atlases: |
39 | | - for atlas in atlases: |
40 | | - tab.append( |
41 | | - { |
42 | | - "ID": node["id"], |
43 | | - "xref": link.substitute( |
44 | | - atlas_id=atlas["id"], |
45 | | - structure_id=str(node["id"]).rsplit("_", 1)[-1], |
46 | | - ), |
47 | | - "prefLabel": "{} ({})".format(node["lbl"], cfg["species"]), |
48 | | - } |
49 | | - ) |
50 | | - else: |
51 | | - tab.append({"ID": node["id"], "xref": "", "prefLabel": node["lbl"]}) |
52 | | - matched = True |
| 104 | + if not is_local_homba_term(node_id, prefix): |
| 105 | + continue |
| 106 | + |
| 107 | + local_id = node_id.rsplit("_", 1)[-1] |
| 108 | + if not is_numeric_homba_id(local_id): |
53 | 109 | break |
54 | 110 |
|
55 | | - if not matched: |
56 | | - tab.append({"ID": node["id"], "xref": "", "prefLabel": node["lbl"]}) |
| 111 | + dhba_prefix = cfg.get("dhba_prefix", "") |
| 112 | + if dhba_prefix: |
| 113 | + tab.append( |
| 114 | + { |
| 115 | + "ID": node_id, |
| 116 | + "dhba_xref": dhba_prefix + local_id, |
| 117 | + "atlas_link": "", |
| 118 | + "prefLabel": "", |
| 119 | + } |
| 120 | + ) |
| 121 | + |
| 122 | + for atlas in cfg.get("atlases", []): |
| 123 | + tab.append( |
| 124 | + { |
| 125 | + "ID": node_id, |
| 126 | + "dhba_xref": "", |
| 127 | + "atlas_link": ATLAS_LINK.substitute(atlas_id=atlas["id"], structure_id=local_id), |
| 128 | + "prefLabel": "", |
| 129 | + } |
| 130 | + ) |
| 131 | + break |
57 | 132 |
|
58 | | -pd.DataFrame.from_records(tab).to_csv("../templates/linkouts.tsv", sep="\t", index=False) |
| 133 | +with open(OUTPUT_PATH, "w", newline="") as handle: |
| 134 | + writer = csv.DictWriter( |
| 135 | + handle, |
| 136 | + fieldnames=["ID", "dhba_xref", "atlas_link", "prefLabel"], |
| 137 | + delimiter="\t", |
| 138 | + lineterminator="\n", |
| 139 | + ) |
| 140 | + writer.writeheader() |
| 141 | + writer.writerows(tab) |
0 commit comments