changeset 30:3342c0518d4c draft

planemo upload for repository https://github.com/rolfverberg/galaxytools commit f8c4bdb31c20c468045ad5e6eb255a293244bc6c-dirty
author rv43
date Wed, 22 Mar 2023 12:52:20 +0000
parents 551d1ea2416d
children dd37989f425a
files tomo_combine.py tomo_combine.xml workflow/run_tomo.py
diffstat 3 files changed, 150 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tomo_combine.py	Wed Mar 22 12:52:20 2023 +0000
@@ -0,0 +1,102 @@
+#!/usr/bin/env python3
+
+import logging
+
+import argparse
+import pathlib
+import sys
+#import tracemalloc
+
+from workflow.run_tomo import Tomo
+
+#from memory_profiler import profile
+#@profile
+def __main__():
+    # Parse command line arguments
+    parser = argparse.ArgumentParser(
+            description='Reduce tomography data')
+    parser.add_argument('-i', '--input_file',
+            required=True,
+            type=pathlib.Path,
+            help='''Full or relative path to the input file (in Nexus format).''')
+    parser.add_argument('-o', '--output_file',
+            required=False,
+            type=pathlib.Path,
+            help='''Full or relative path to the output file (in yaml format).''')
+    parser.add_argument('--galaxy_flag',
+            action='store_true',
+            help='''Use this flag to run the scripts as a galaxy tool.''')
+    parser.add_argument('-l', '--log',
+#            type=argparse.FileType('w'),
+            default=sys.stdout,
+            help='Logging stream or filename')
+    parser.add_argument('--log_level',
+            choices=logging._nameToLevel.keys(),
+            default='INFO',
+            help='''Specify a preferred logging level.''')
+    parser.add_argument('--x_bounds',
+            required=False,
+            nargs=2,
+            type=int,
+            help='''Boundaries of reconstructed images in x-direction.''')
+    parser.add_argument('--y_bounds',
+            required=False,
+            nargs=2,
+            type=int,
+            help='''Boundaries of reconstructed images in y-direction.''')
+    args = parser.parse_args()
+
+    # Set log configuration
+    # When logging to file, the stdout log level defaults to WARNING
+    logging_format = '%(asctime)s : %(levelname)s - %(module)s : %(funcName)s - %(message)s'
+    level = logging.getLevelName(args.log_level)
+    if args.log is sys.stdout:
+        logging.basicConfig(format=logging_format, level=level, force=True,
+                handlers=[logging.StreamHandler()])
+    else:
+        if isinstance(args.log, str):
+            logging.basicConfig(filename=f'{args.log}', filemode='w',
+                    format=logging_format, level=level, force=True)
+        elif isinstance(args.log, io.TextIOWrapper):
+            logging.basicConfig(filemode='w', format=logging_format, level=level,
+                    stream=args.log, force=True)
+        else:
+            raise(ValueError(f'Invalid argument --log: {args.log}'))
+        stream_handler = logging.StreamHandler()
+        logging.getLogger().addHandler(stream_handler)
+        stream_handler.setLevel(logging.WARNING)
+        stream_handler.setFormatter(logging.Formatter(logging_format))
+
+    # Starting memory monitoring
+#    tracemalloc.start()
+
+    # Log command line arguments
+    logging.info(f'input_file = {args.input_file}')
+    logging.info(f'output_file = {args.output_file}')
+    logging.debug(f'log = {args.log}')
+    logging.debug(f'is log stdout? {args.log is sys.stdout}')
+    logging.debug(f'log_level = {args.log_level}')
+    logging.info(f'x_bounds = {args.x_bounds}')
+    logging.info(f'y_bounds = {args.y_bounds}')
+
+    # Instantiate Tomo object
+    tomo = Tomo(galaxy_flag=args.galaxy_flag)
+
+    # Read input file
+    data = tomo.read(args.input_file)
+
+    # Combine the reconstructed tomography stacks
+    data = tomo.combine_data(data, x_bounds=args.x_bounds, y_bounds=args.y_bounds)
+
+    # Write output file
+    if data is not None:
+        data = tomo.write(data, args.output_file)
+
+    # Displaying memory usage
+#    logging.info(f'Memory usage: {tracemalloc.get_traced_memory()}')
+ 
+    # stopping memory monitoring
+#    tracemalloc.stop()
+
+if __name__ == "__main__":
+    __main__()
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tomo_combine.xml	Wed Mar 22 12:52:20 2023 +0000
@@ -0,0 +1,47 @@
+<tool id="tomo_combine" name="Tomo Combine Reconstructed Stacks" version="0.2.0" python_template_version="3.9">
+    <description>Combine reconstructed tomography stacks</description>
+    <macros>
+        <import>tomo_macros.xml</import>
+    </macros>
+    <expand macro="requirements" />
+    <command detect_errors="exit_code"><![CDATA[
+        mkdir combine_pngs;
+        $__tool_directory__/tomo_combine.py
+        -i '$recon_stacks'
+        -c '$config'
+        --x_bounds $x_bounds.low $x_bounds.upp
+        --y_bounds $y_bounds.low $y_bounds.upp
+        --z_bounds $z_bounds.low $z_bounds.upp
+        --output_data 'output_data.npy'
+        --output_config 'output_config.yaml'
+        -l '$log'
+    ]]></command>
+    <inputs>
+        <expand macro="common_inputs"/>
+        <param name="recon_stacks" type='data' format='npz' optional='false' label="Reconstructed stacks"/>
+        <section name="x_bounds" title="Reconstructed range in x direction">
+            <param name="low" type="integer" value = "-1" label="Lower x-bound"/>
+            <param name="upp" type="integer" value = "-1" label="Upper x-bound"/>
+        </section>
+        <section name="y_bounds" title="Reconstructed range in y direction">
+            <param name="low" type="integer" value = "-1" label="Lower y-bound"/>
+            <param name="upp" type="integer" value = "-1" label="Upper y-bound"/>
+        </section>
+        <section name="z_bounds" title="Reconstructed range in z direction">
+            <param name="low" type="integer" value = "-1" label="Lower z-bound"/>
+            <param name="upp" type="integer" value = "-1" label="Upper z-bound"/>
+        </section>
+    </inputs>
+    <outputs>
+        <expand macro="common_outputs"/>
+        <data name="output_data" format="npy" label="Combined tomography stacks" from_work_dir="output_data.npy"/>
+        <collection name="combine_pngs" type="list" label="Recontructed slices midway in each combined dimension">
+            <discover_datasets pattern="__name_and_ext__" directory="combine_pngs"/>
+        </collection>
+        <data name="output_config" format="tomo.config.yaml" label="Output config combine reconstruction" from_work_dir="output_config.yaml"/>
+    </outputs>
+    <help><![CDATA[
+        Combine reconstructed tomography images.
+    ]]></help>
+    <expand macro="citations"/>
+</tool>
--- a/workflow/run_tomo.py	Tue Mar 21 18:46:02 2023 +0000
+++ b/workflow/run_tomo.py	Wed Mar 22 12:52:20 2023 +0000
@@ -302,7 +302,7 @@
         if not isinstance(nxentry, NXentry):
             raise ValueError(f'Invalid nxentry ({nxentry})')
         if center_rows is not None:
-            if self.galaxy_flag
+            if self.galaxy_flag:
                 is not is_int_pair(center_rows, ge=-1):
                     raise ValueError(f'Invalid parameter center_rows ({center_rows})')
                 if (center_rows[0] != -1 and center_rows[1] != -1 and