|
| 1 | +import os |
| 2 | +from pathlib import Path |
| 3 | + |
| 4 | +from jinja2 import Template |
| 5 | +from urllib.parse import urlparse |
| 6 | + |
| 7 | +from tdta.tdt_export import db_to_cas |
| 8 | +from tdta.utils import read_project_config |
| 9 | + |
| 10 | +ANNOTATIONS_TEMPLATE = os.path.join(os.path.dirname(os.path.realpath(__file__)), "../../resources/annotation_template.md") |
| 11 | +TAXONOMY_TEMPLATE = os.path.join(os.path.dirname(os.path.realpath(__file__)), "../../resources/taxonomy_template.md") |
| 12 | + |
| 13 | + |
| 14 | +def generate_documentation(sqlite_db: str, output_folder: str, project_config=None): |
| 15 | + """ |
| 16 | + Generate markdown documentation for a CAS database. |
| 17 | + Parameters: |
| 18 | + sqlite_db: Path to the CAS database. |
| 19 | + output_folder: Path to the output documentation folder. |
| 20 | + project_config: Project configuration. |
| 21 | + """ |
| 22 | + if not os.path.exists(output_folder): |
| 23 | + os.makedirs(output_folder) |
| 24 | + |
| 25 | + cas_obj = db_to_cas(sqlite_db) |
| 26 | + cas = cas_obj.to_dict() |
| 27 | + if project_config is None: |
| 28 | + project_config = read_project_config(Path(output_folder).parent.absolute()) |
| 29 | + cas = transform_cas(cas, project_config) |
| 30 | + |
| 31 | + annotation_template = read_jinja_template(ANNOTATIONS_TEMPLATE) |
| 32 | + for annotation in cas["annotations"]: |
| 33 | + rendered_file = annotation_template.render(annotation=annotation, metadata=cas) |
| 34 | + annotation_file_name = annotation["cell_set_accession"].replace(":", "_") |
| 35 | + |
| 36 | + with open(os.path.join(output_folder, annotation_file_name + ".md"), "w") as fh: |
| 37 | + fh.write(rendered_file) |
| 38 | + |
| 39 | + taxonomy_template = read_jinja_template(TAXONOMY_TEMPLATE) |
| 40 | + rendered_file = taxonomy_template.render(cas=cas) |
| 41 | + with open(os.path.join(output_folder, "taxonomy.md"), "w") as fh: |
| 42 | + fh.write(rendered_file) |
| 43 | + |
| 44 | + |
| 45 | +def transform_cas(cas, project_config): |
| 46 | + """ |
| 47 | + Adds extra data to cas for visualisation purposes. |
| 48 | + """ |
| 49 | + add_purl(cas, project_config["id"]) |
| 50 | + add_parents(cas) |
| 51 | + transform_annotation_transfer(cas) |
| 52 | + |
| 53 | + return cas |
| 54 | + |
| 55 | + |
| 56 | +def transform_annotation_transfer(cas): |
| 57 | + for annotation in cas["annotations"]: |
| 58 | + if "transferred_annotations" in annotation: |
| 59 | + for transferred_annotation in annotation["transferred_annotations"]: |
| 60 | + parsed_url = urlparse(transferred_annotation["source_taxonomy"]) |
| 61 | + path_parts = parsed_url.path.split('/') |
| 62 | + taxonomy_id = path_parts[-2] |
| 63 | + purl_base = f"{parsed_url.scheme}://{parsed_url.netloc}/taxonomy/{taxonomy_id}#" |
| 64 | + transferred_annotation["purl_base"] = purl_base |
| 65 | + |
| 66 | + |
| 67 | +def add_purl(cas, project_id): |
| 68 | + cas["purl_base"] = f"https://purl.brain-bican.org/taxonomy/{project_id}#" |
| 69 | + if "cellannotation_url" not in cas: |
| 70 | + cas["cellannotation_url"] = f"https://purl.brain-bican.org/taxonomy/{project_id}/{project_id}.json" |
| 71 | + |
| 72 | + |
| 73 | +def add_parents(cas): |
| 74 | + parents = build_hierarchy(cas["annotations"]) |
| 75 | + for annotation in cas["annotations"]: |
| 76 | + annotation["parents"] = parents[annotation["cell_set_accession"]] |
| 77 | + |
| 78 | + |
| 79 | +def build_hierarchy(annotations): |
| 80 | + """ |
| 81 | + Build a hierarchy of cell sets. Keys of the dicts are cell set accessions, values are lists of parent cell set |
| 82 | + accessions ordered from highest to lowest. |
| 83 | + """ |
| 84 | + hierarchy = {} |
| 85 | + annotation_dict = {annotation['cell_set_accession']: annotation for annotation in annotations} |
| 86 | + |
| 87 | + def get_hierarchy(annotation): |
| 88 | + if 'parent_cell_set_accession' not in annotation: |
| 89 | + return [] |
| 90 | + parent_accession = annotation['parent_cell_set_accession'] |
| 91 | + parent_annotation = annotation_dict.get(parent_accession) |
| 92 | + if parent_annotation: |
| 93 | + return get_hierarchy(parent_annotation) + [parent_accession] |
| 94 | + return [] |
| 95 | + |
| 96 | + for annotation in annotations: |
| 97 | + cell_set_accession = annotation['cell_set_accession'] |
| 98 | + hierarchy[cell_set_accession] = get_hierarchy(annotation) |
| 99 | + |
| 100 | + return hierarchy |
| 101 | + |
| 102 | + |
| 103 | +def read_jinja_template(template_path): |
| 104 | + """ |
| 105 | + Read Jinja template from file. |
| 106 | + """ |
| 107 | + with open(template_path, 'r') as file: |
| 108 | + template = Template(file.read(), trim_blocks=True) |
| 109 | + return template |
| 110 | + |
0 commit comments