Skip to content

Commit 214cee8

Browse files
authored
Merge pull request #49 from brain-bican/docs
jinja documentation fixes
2 parents 07f758e + 3af76c2 commit 214cee8

File tree

5 files changed

+56
-15
lines changed

5 files changed

+56
-15
lines changed

resources/annotation_template.md

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1+
---
2+
title: {{annotation.cell_set_accession}}
3+
weight: {{annotation.weight}}
4+
---
15
## {{annotation.cell_label}} ({{annotation.cell_set_accession}})
26
{% if 'parents' in annotation %}
37
<b>Hierarchy: </b>
48
{% for parent in annotation.parents %}
5-
[{{parent}}]({{metadata.purl_base}}{{parent|replace(":", "_")}}) >
9+
[{{parent}}](../{{parent|replace(":", "_")}}) >
610
{% endfor %}
7-
[{{annotation.cell_set_accession}}]({{metadata.purl_base}}{{annotation.cell_set_accession|replace(":", "_")}})
11+
[{{annotation.cell_set_accession}}](../{{annotation.cell_set_accession|replace(":", "_")}})
812
{% endif %}
913

14+
**PURL:** [{{metadata.purl_base}}{{annotation.cell_set_accession|replace(":", "_")}}]({{metadata.purl_base}}{{annotation.cell_set_accession|replace(":", "_")}})
15+
1016
---
1117

1218
{% set labelset = metadata.labelsets|selectattr("name", "==", annotation.labelset) | list | first %}
@@ -15,7 +21,7 @@
1521

1622
{% if 'parent_cell_set_accession' in annotation %}
1723
{% set parent_annotation = metadata.annotations|selectattr("cell_set_accession", "==", annotation.parent_cell_set_accession) | list | first %}
18-
**Parent Cell Set:** {{parent_annotation.cell_label}} ([{{annotation.parent_cell_set_accession}}]({{metadata.purl_base}}{{annotation.parent_cell_set_accession|replace(":", "_")}}))
24+
**Parent Cell Set:** {{parent_annotation.cell_label}} ([{{annotation.parent_cell_set_accession}}](../{{annotation.parent_cell_set_accession|replace(":", "_")}}))
1925
{% else %}
2026
**Parent Cell Set:** -
2127
{% endif %}
@@ -43,7 +49,7 @@
4349
| Rationale DOIs |
4450
|----------------|
4551
{% for doi in annotation.rationale_dois %}
46-
|{{doi}}|
52+
|[{{doi}}]({{doi}})|
4753
{% endfor %}
4854
{% endif %}
4955

@@ -69,7 +75,7 @@
6975
| Transferred cell label | Source taxonomy | Source node accession | Algorithm name | Comment |
7076
|------------------------|-----------------|-----------------------|----------------|---------|
7177
{% for at in annotation.transferred_annotations %}
72-
|{{at.transferred_cell_label}}|{{at.source_taxonomy}}|[{{at.source_node_accession}}]({{at.purl_base}}{{at.source_node_accession|replace(":", "_")}})|{{at.algorithm_name}}|{{at.comment}}|
78+
|{{at.transferred_cell_label}}|[{{at.source_taxonomy}}]({{at.purl_base}})|[{{at.source_node_accession}}]({{at.purl_base}}{{at.source_node_accession|replace(":", "_")}})|{{at.algorithm_name}}|{{at.comment}}|
7379
{% endfor %}
7480
{% endif %}
7581

resources/taxonomy_template.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
---
2+
weight: -10
3+
---
14
## {{cas.title}}
25

36
{{cas.description}}
@@ -20,6 +23,8 @@
2023

2124
---
2225

26+
**Taxonomy PURL:** [{{cas.purl_base}}]({{cas.purl_base}})
27+
2328
**Cell Annotation Schema Version:** {{cas.cellannotation_schema_version}}
2429

2530
**Cell Annotation Timestamp:** {{cas.cellannotation_timestamp}}

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
setup(
1010
name="tdta",
11-
version="0.1.0.dev18",
11+
version="0.1.0.dev20",
1212
description="The aim of this project is to provide taxonomy development tools custom actions.",
1313
long_description=README,
1414
long_description_content_type="text/markdown",

src/tdta/documentation.py

+37-6
Original file line numberDiff line numberDiff line change
@@ -22,32 +22,38 @@ def generate_documentation(sqlite_db: str, output_folder: str, project_config=No
2222
if not os.path.exists(output_folder):
2323
os.makedirs(output_folder)
2424

25+
cell_sets_folder = os.path.join(output_folder, "cell_sets")
26+
if not os.path.exists(cell_sets_folder):
27+
os.makedirs(cell_sets_folder)
28+
2529
cas_obj = db_to_cas(sqlite_db)
2630
cas = cas_obj.to_dict()
2731
if project_config is None:
2832
project_config = read_project_config(Path(output_folder).parent.absolute())
2933
cas = transform_cas(cas, project_config)
3034

3135
annotation_template = read_jinja_template(ANNOTATIONS_TEMPLATE)
36+
cell_sets_folder = os.path.join(output_folder, "cell_sets")
3237
for annotation in cas["annotations"]:
3338
rendered_file = annotation_template.render(annotation=annotation, metadata=cas)
3439
annotation_file_name = annotation["cell_set_accession"].replace(":", "_")
3540

36-
with open(os.path.join(output_folder, annotation_file_name + ".md"), "w") as fh:
41+
with open(os.path.join(cell_sets_folder, annotation_file_name + ".md"), "w") as fh:
3742
fh.write(rendered_file)
3843

3944
taxonomy_template = read_jinja_template(TAXONOMY_TEMPLATE)
4045
rendered_file = taxonomy_template.render(cas=cas)
41-
with open(os.path.join(output_folder, "taxonomy.md"), "w") as fh:
46+
with open(os.path.join(output_folder, "index.md"), "w") as fh:
4247
fh.write(rendered_file)
4348

4449

4550
def transform_cas(cas, project_config):
4651
"""
4752
Adds extra data to cas for visualisation purposes.
4853
"""
49-
add_purl(cas, project_config["id"])
54+
add_purl(cas, project_config)
5055
add_parents(cas)
56+
add_weigth(cas)
5157
transform_annotation_transfer(cas)
5258

5359
return cas
@@ -60,22 +66,47 @@ def transform_annotation_transfer(cas):
6066
parsed_url = urlparse(transferred_annotation["source_taxonomy"])
6167
path_parts = parsed_url.path.split('/')
6268
taxonomy_id = path_parts[-2]
63-
purl_base = f"{parsed_url.scheme}://{parsed_url.netloc}/taxonomy/{taxonomy_id}#"
69+
purl_base = f"{parsed_url.scheme}://{parsed_url.netloc}/taxonomy/{taxonomy_id}/"
6470
transferred_annotation["purl_base"] = purl_base
6571

6672

67-
def add_purl(cas, project_id):
68-
cas["purl_base"] = f"https://purl.brain-bican.org/taxonomy/{project_id}#"
73+
def add_purl(cas, project_config):
74+
project_id = project_config["id"]
75+
purl_base = get_project_purl(project_config)
76+
cas["purl_base"] = purl_base
6977
if "cellannotation_url" not in cas:
7078
cas["cellannotation_url"] = f"https://purl.brain-bican.org/taxonomy/{project_id}/{project_id}.json"
7179

7280

81+
def get_project_purl(project_config):
82+
if "custom_purl" in project_config:
83+
purl_base = project_config["custom_purl"]
84+
if not purl_base.endswith("/"):
85+
purl_base += "/"
86+
else:
87+
project_id = project_config["id"]
88+
purl_base = f"https://purl.brain-bican.org/taxonomy/{project_id}/"
89+
return purl_base
90+
91+
7392
def add_parents(cas):
7493
parents = build_hierarchy(cas["annotations"])
7594
for annotation in cas["annotations"]:
7695
annotation["parents"] = parents[annotation["cell_set_accession"]]
7796

7897

98+
def add_weigth(cas):
99+
"""
100+
Add weight to annotations to be used for sorting pages.
101+
"""
102+
for annotation in cas["annotations"]:
103+
order = annotation["cell_set_accession"].replace(":", "_").split("_")[-1]
104+
if order.isdigit():
105+
annotation["weight"] = int(order)
106+
else:
107+
annotation["weight"] = 10 - len(annotation["parents"])
108+
109+
79110
def build_hierarchy(annotations):
80111
"""
81112
Build a hierarchy of cell sets. Keys of the dicts are cell set accessions, values are lists of parent cell set

src/test/generate_docs_test.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,10 @@ def setUp(self):
1717
shutil.rmtree(TEST_OUTPUT)
1818

1919
def test_documentation_generation(self):
20-
generate_documentation(TEST_DB, TEST_OUTPUT, project_config={"id": "CS202210140"})
20+
generate_documentation(TEST_DB, TEST_OUTPUT, project_config={"id": "CS202210140",
21+
"custom_purl": "https://purl.brain-bican.org/taxonomy/CS202210140/CS202210140_non-neuronal/"})
2122
self.assertTrue(os.path.exists(TEST_OUTPUT))
2223

23-
self.assertEqual(True, False) # add assertion here
24-
2524
def test_hierarchy_breadcrumb(self):
2625
with open("./test_data/CS202210140.json") as f:
2726
siletti = json.load(f)

0 commit comments

Comments
 (0)