From 85d2a86d970ee86bba1f8e1054557737716add6d Mon Sep 17 00:00:00 2001
From: Constantin Pape <constantin.pape@iwr.uni-heidelberg.de>
Date: Thu, 7 Nov 2019 17:41:38 +0100
Subject: [PATCH] Add functionality to autogenerate additional table file and
 add it to 0.6.3

---
 .../additional_tables.txt                     |  1 +
 .../additional_tables.txt                     |  6 ++++
 .../additional_tables.txt                     |  2 ++
 .../additional_tables.txt                     |  2 ++
 scripts/attributes/base_attributes.py         | 28 +++++++++++++++++++
 scripts/attributes/master.py                  |  8 +++++-
 scripts/files/copy_helper.py                  |  3 ++
 7 files changed, 49 insertions(+), 1 deletion(-)
 create mode 100644 data/0.6.3/tables/prospr-6dpf-1-whole-virtual-cells-labels/additional_tables.txt
 create mode 100644 data/0.6.3/tables/sbem-6dpf-1-whole-segmented-cells-labels/additional_tables.txt
 create mode 100644 data/0.6.3/tables/sbem-6dpf-1-whole-segmented-cilia-labels/additional_tables.txt
 create mode 100644 data/0.6.3/tables/sbem-6dpf-1-whole-segmented-nuclei-labels/additional_tables.txt

diff --git a/data/0.6.3/tables/prospr-6dpf-1-whole-virtual-cells-labels/additional_tables.txt b/data/0.6.3/tables/prospr-6dpf-1-whole-virtual-cells-labels/additional_tables.txt
new file mode 100644
index 0000000..fa898e9
--- /dev/null
+++ b/data/0.6.3/tables/prospr-6dpf-1-whole-virtual-cells-labels/additional_tables.txt
@@ -0,0 +1 @@
+profile_clust_curated.csv
diff --git a/data/0.6.3/tables/sbem-6dpf-1-whole-segmented-cells-labels/additional_tables.txt b/data/0.6.3/tables/sbem-6dpf-1-whole-segmented-cells-labels/additional_tables.txt
new file mode 100644
index 0000000..5f8b18c
--- /dev/null
+++ b/data/0.6.3/tables/sbem-6dpf-1-whole-segmented-cells-labels/additional_tables.txt
@@ -0,0 +1,6 @@
+cells_to_nuclei.csv
+extrapolated_intensity_correction.csv
+genes.csv
+morphology.csv
+regions.csv
+vc_assignments.csv
diff --git a/data/0.6.3/tables/sbem-6dpf-1-whole-segmented-cilia-labels/additional_tables.txt b/data/0.6.3/tables/sbem-6dpf-1-whole-segmented-cilia-labels/additional_tables.txt
new file mode 100644
index 0000000..7f85507
--- /dev/null
+++ b/data/0.6.3/tables/sbem-6dpf-1-whole-segmented-cilia-labels/additional_tables.txt
@@ -0,0 +1,2 @@
+cell_mapping.csv
+morphology.csv
diff --git a/data/0.6.3/tables/sbem-6dpf-1-whole-segmented-nuclei-labels/additional_tables.txt b/data/0.6.3/tables/sbem-6dpf-1-whole-segmented-nuclei-labels/additional_tables.txt
new file mode 100644
index 0000000..acc707e
--- /dev/null
+++ b/data/0.6.3/tables/sbem-6dpf-1-whole-segmented-nuclei-labels/additional_tables.txt
@@ -0,0 +1,2 @@
+extrapolated_intensity_correction.csv
+morphology.csv
diff --git a/scripts/attributes/base_attributes.py b/scripts/attributes/base_attributes.py
index fad0384..1df9f1a 100644
--- a/scripts/attributes/base_attributes.py
+++ b/scripts/attributes/base_attributes.py
@@ -160,6 +160,34 @@ def base_attributes(input_path, input_key, output_path, resolution,
     return label_ids
 
 
+def write_additional_table_file(table_folder):
+    # get all the file names in the table folder
+    file_names = os.listdir(table_folder)
+    file_names.sort()
+
+    # make sure we have the default table
+    default_name = 'default.csv'
+    if default_name not in file_names:
+        raise RuntimeError("Did not find the default table ('default.csv') in the table folder %s" % table_folder)
+
+    # don't write anything if we don't have additional tables
+    if len(file_names) == 1:
+        return
+
+    # write file for the additional tables
+    out_file = os.path.join(table_folder, 'additional_tables.txt')
+    with open(out_file, 'w') as f:
+        for name in file_names:
+            ext = os.path.splitext(name)[1]
+            # only add csv files
+            if ext != '.csv':
+                continue
+            # don't add the default table
+            if name == 'default.csv':
+                continue
+            f.write(name + '\n')
+
+
 # TODO this is un-tested !!!
 def propagate_attributes(id_mapping_path, old_table_path, output_path):
     """ Propagate all attributes to new ids. (column label id)
diff --git a/scripts/attributes/master.py b/scripts/attributes/master.py
index df607d9..7541b4c 100644
--- a/scripts/attributes/master.py
+++ b/scripts/attributes/master.py
@@ -1,7 +1,7 @@
 import os
 import h5py
 
-from .base_attributes import base_attributes, propagate_attributes
+from .base_attributes import base_attributes, propagate_attributes, write_additional_table_file
 from .cell_nucleus_mapping import map_cells_to_nuclei
 from .genes import gene_assignment_table, vc_assignment_table
 from .morphology import write_morphology_cells, write_morphology_nuclei
@@ -84,6 +84,8 @@ def make_cell_tables(old_folder, folder, name, tmp_folder, resolution,
                              extrapol_mask, 't00000/s00/0/cells',
                              extrapol_out, tmp_folder, target, max_jobs)
 
+    write_additional_table_file(table_folder)
+
 
 def make_nucleus_tables(old_folder, folder, name, tmp_folder, resolution,
                         target='slurm', max_jobs=100):
@@ -117,6 +119,8 @@ def make_nucleus_tables(old_folder, folder, name, tmp_folder, resolution,
                              extrapol_mask, 't00000/s00/0/cells',
                              extrapol_out, tmp_folder, target, max_jobs)
 
+    write_additional_table_file(table_folder)
+
 
 def make_cilia_tables(old_folder, folder, name, tmp_folder, resolution,
                       target='slurm', max_jobs=100):
@@ -144,3 +148,5 @@ def make_cilia_tables(old_folder, folder, name, tmp_folder, resolution,
     cilia_morphology(seg_path, seg_key,
                      base_out, morpho_out, resolution,
                      tmp_folder, target=target, max_jobs=max_jobs)
+
+    write_additional_table_file(table_folder)
diff --git a/scripts/files/copy_helper.py b/scripts/files/copy_helper.py
index 9d6c570..b7c6ad3 100644
--- a/scripts/files/copy_helper.py
+++ b/scripts/files/copy_helper.py
@@ -2,6 +2,7 @@ import os
 import shutil
 from .xml_utils import copy_xml_with_newpath, get_h5_path_from_xml
 from .sources import get_image_names, get_segmentation_names, get_segmentations
+from ..attributes.base_attributes import write_additional_table_file
 
 
 def copy_file(xml_in, xml_out):
@@ -28,6 +29,8 @@ def copy_tables(src_folder, dst_folder, name):
         rel_path = os.path.relpath(src_file, table_out)
         if not os.path.exists(dst_file):
             os.symlink(rel_path, dst_file)
+    # write the txt file for additional tables
+    write_additional_table_file(dst_folder)
 
 
 def copy_segmentation(src_folder, dst_folder, name):
-- 
GitLab