|
1 | 1 | import os
|
2 |
| -from pathlib import Path |
| 2 | +import shutil |
3 | 3 |
|
| 4 | +from pathlib import Path |
4 | 5 | from jinja2 import Template
|
5 | 6 | from urllib.parse import urlparse
|
6 | 7 |
|
7 | 8 | from tdta.tdt_export import db_to_cas
|
8 | 9 | from tdta.utils import read_project_config
|
| 10 | +from tdta.command_line_utils import runcmd |
| 11 | +from tdta.version_control import git_update_local |
9 | 12 |
|
10 | 13 | ANNOTATIONS_TEMPLATE = os.path.join(os.path.dirname(os.path.realpath(__file__)), "../../resources/annotation_template.md")
|
11 | 14 | TAXONOMY_TEMPLATE = os.path.join(os.path.dirname(os.path.realpath(__file__)), "../../resources/taxonomy_template.md")
|
12 | 15 |
|
13 | 16 |
|
14 |
| -def generate_documentation(sqlite_db: str, output_folder: str, project_config=None): |
| 17 | +def generate_documentation(sqlite_db: str, output_folder: str, project_config=None, git_push=True): |
15 | 18 | """
|
16 | 19 | Generate markdown documentation for a CAS database.
|
17 | 20 | Parameters:
|
18 | 21 | sqlite_db: Path to the CAS database.
|
19 | 22 | output_folder: Path to the output documentation folder.
|
20 | 23 | project_config: Project configuration.
|
21 | 24 | """
|
22 |
| - if not os.path.exists(output_folder): |
23 |
| - os.makedirs(output_folder) |
24 |
| - |
| 25 | + project_folder = Path(output_folder).parent.absolute() |
| 26 | + index_file = os.path.join(output_folder, "index.md") |
25 | 27 | 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 | + |
| 29 | + clear_docs_folder(cell_sets_folder, index_file, output_folder) |
28 | 30 |
|
29 | 31 | cas_obj = db_to_cas(sqlite_db)
|
30 | 32 | cas = cas_obj.to_dict()
|
31 | 33 | if project_config is None:
|
32 |
| - project_config = read_project_config(Path(output_folder).parent.absolute()) |
| 34 | + project_config = read_project_config(project_folder) |
33 | 35 | cas = transform_cas(cas, project_config)
|
34 | 36 |
|
| 37 | + generate_annotation_docs(cas, cell_sets_folder) |
| 38 | + generate_taxonomy_doc(cas, index_file, output_folder) |
| 39 | + |
| 40 | + if git_push: |
| 41 | + runcmd("cd {dir} && git add --all {docs_folder}".format(dir=project_folder, |
| 42 | + docs_folder=os.path.relpath(output_folder, project_folder))) |
| 43 | + git_update_local(project_folder.absolute().as_posix(), "Update project documentation") |
| 44 | + runcmd("cd {dir} && git push".format(dir=project_folder)) |
| 45 | + print("Taxonomy documentation sent to GitHub.") |
| 46 | + print("Github action is triggered to publish the documentation on the website. Please check the status of the action.") |
| 47 | + |
| 48 | + |
| 49 | +def generate_taxonomy_doc(cas, index_file, output_folder): |
| 50 | + """ |
| 51 | + Generate the taxonomy documentation (index.md). |
| 52 | + Parameters: |
| 53 | + cas: CAS object |
| 54 | + index_file: Path to the index file |
| 55 | + output_folder: Path to the output folder |
| 56 | + """ |
| 57 | + taxonomy_template = read_jinja_template(TAXONOMY_TEMPLATE) |
| 58 | + rendered_file = taxonomy_template.render(cas=cas) |
| 59 | + with open(index_file, "w") as fh: |
| 60 | + fh.write(rendered_file) |
| 61 | + print("Taxonomy documentation generated at {out_dir}".format(out_dir=output_folder)) |
| 62 | + |
| 63 | + |
| 64 | +def generate_annotation_docs(cas, cell_sets_folder): |
| 65 | + """ |
| 66 | + Generate markdown documentation for each cell set in the CAS. |
| 67 | + Parameters: |
| 68 | + cas: CAS object |
| 69 | + cell_sets_folder: Path to the cell sets folder |
| 70 | + """ |
35 | 71 | annotation_template = read_jinja_template(ANNOTATIONS_TEMPLATE)
|
36 |
| - cell_sets_folder = os.path.join(output_folder, "cell_sets") |
37 | 72 | for annotation in cas["annotations"]:
|
38 | 73 | rendered_file = annotation_template.render(annotation=annotation, metadata=cas)
|
39 | 74 | annotation_file_name = annotation["cell_set_accession"].replace(":", "_")
|
40 |
| - |
41 |
| - with open(os.path.join(cell_sets_folder, annotation_file_name + ".md"), "w") as fh: |
| 75 | + with open(os.path.join(cell_sets_folder, annotation_file_name + ".md"), "w") as fh: |
42 | 76 | fh.write(rendered_file)
|
43 | 77 |
|
44 |
| - taxonomy_template = read_jinja_template(TAXONOMY_TEMPLATE) |
45 |
| - rendered_file = taxonomy_template.render(cas=cas) |
46 |
| - with open(os.path.join(output_folder, "index.md"), "w") as fh: |
47 |
| - fh.write(rendered_file) |
| 78 | + |
| 79 | +def clear_docs_folder(cell_sets_folder, index_file, output_folder): |
| 80 | + """ |
| 81 | + Deletes the existing docs folder content. |
| 82 | + """ |
| 83 | + if not os.path.exists(output_folder): |
| 84 | + os.makedirs(output_folder) |
| 85 | + else: |
| 86 | + shutil.rmtree(cell_sets_folder, ignore_errors=True) |
| 87 | + if os.path.isfile(index_file): |
| 88 | + os.remove(index_file) |
| 89 | + os.makedirs(cell_sets_folder) |
48 | 90 |
|
49 | 91 |
|
50 | 92 | def transform_cas(cas, project_config):
|
|
0 commit comments