changeset 59:feb2a5fc7c76 draft

"planemo upload for repository https://github.com/rolfverberg/galaxytools commit 9a07ab3099737ee0d99e82739b55048f89c36bc6"
author rv43
date Tue, 16 Aug 2022 16:55:50 +0000
parents 072a3637117e
children 52db7707ff48
files tomo_setup.py tomo_setup.xml
diffstat 2 files changed, 180 insertions(+), 143 deletions(-) [+]
line wrap: on
line diff
--- a/tomo_setup.py	Tue Aug 02 20:46:09 2022 +0000
+++ b/tomo_setup.py	Tue Aug 16 16:55:50 2022 +0000
@@ -19,27 +19,26 @@
     # Parse command line arguments
     parser = argparse.ArgumentParser(
             description='Setup tomography reconstruction')
-    parser.add_argument('-i', '--inputfiles',
-            nargs='+',
+    parser.add_argument('--inputconfig',
+            default='inputconfig.txt',
+            help='Input config from tool form')
+    parser.add_argument('--inputfiles',
             default='inputfiles.txt',
-            help='Input file datasets or collections')
+            help='Input file collections')
     parser.add_argument('-c', '--config',
-            help='Input config')
+            help='Input config file')
+    parser.add_argument('--num_theta',
+            help='Number of theta angles')
+    parser.add_argument('--theta_range',
+            help='Theta range (lower bound, upper bound)')
+    parser.add_argument('--output_config',
+            help='Output config')
+    parser.add_argument('--output_data',
+            help='Preprocessed tomography data')
     parser.add_argument('-l', '--log', 
             type=argparse.FileType('w'),
             default=sys.stdout,
             help='Log file')
-    parser.add_argument('-t', '--inputfile_types',
-            nargs='+',
-            default='collection',
-            help='Input files type (collection or a list of set types: dark, bright, or data)')
-    parser.add_argument('--theta_range',
-            help='Theta range (lower bound, upper bound, number of angles)')
-    parser.add_argument('--output_config',
-            help='Output config')
-    parser.add_argument('--output_data',
-            help='Preprocessed tomography data')
-    parser.add_argument('tomo_ranges', metavar='N', type=int, nargs='+')
     args = parser.parse_args()
 
     # Starting memory monitoring
@@ -54,72 +53,78 @@
     logging.basicConfig(format=logging_format, level=level, force=True,
             handlers=[logging.StreamHandler()])
 
-    logging.debug(f'config = {args.config}')
-    logging.debug(f'inputfiles = {args.inputfiles}')
-    logging.debug(f'inputfile_types = {args.inputfile_types}')
-    logging.debug(f'log = {args.log}')
+    logging.info(f'config = {args.config}')
+    logging.info(f'num_theta = {args.num_theta}')
+    if args.theta_range is None:
+        logging.info(f'theta_range = {args.theta_range}')
+    else:
+        logging.info(f'theta_range = {args.theta_range.split()}')
+    logging.info(f'output_config = {args.output_config}')
+    logging.info(f'output_data = {args.output_data}')
+    logging.info(f'log = {args.log}')
     logging.debug(f'is log stdout? {args.log is sys.stdout}')
-    logging.debug(f'theta_range = {args.theta_range.split()}')
-    logging.debug(f'output_config = {args.output_config}')
-    logging.debug(f'output_data = {args.output_data}')
-    logging.debug(f'tomoranges = {args.tomo_ranges}')
 
-    # Check input file type
-    if isinstance(args.inputfile_types, list):
-        if len(args.inputfile_types) == 1 and args.inputfile_types[0] == 'collection':
-            if len(args.inputfiles) != 1 or args.inputfiles[0] != 'inputfiles.txt':
-                raise ValueError('Inconsistent inputfiles and inputfile_types:\n'+
-                        f'inputfiles ({type(inputfiles)}):\n{inputfiles}\n'+
-                        f'inputfile_types ({type(inputfile_types)}):\n{inputfile_types}')
-            input_as_collection = True
-        else:
-            if len(args.inputfiles) != len(args.inputfile_types):
-                raise ValueError('Inconsistent inputfiles and inputfile_types:\n'+
-                        f'inputfiles ({type(inputfiles)}):\n{inputfiles}\n'+
-                        f'inputfile_types ({type(inputfile_types)}):\n{inputfile_types}')
-            input_as_collection = False
+    # Read tool config input
+    inputconfig = []
+    with open(args.inputconfig) as f:
+        inputconfig = [line.strip() for line in f if line.strip() and not line.startswith('#')]
+    assert(len(inputconfig) >= 6)
+    config_type = inputconfig[0]
+    input_type = inputconfig[1]
+    num_stack = int(inputconfig[2])
+    stack_types = [x.strip() for x in inputconfig[3].split()]
+    num_imgs = [int(x.strip()) for x in inputconfig[4].split()]
+    img_offsets = [int(x.strip()) for x in inputconfig[5].split()]
+    if config_type == 'config_manual':
+        assert(len(inputconfig) == 7)
+        ref_heights = [float(x.strip()) for x in inputconfig[6].split()]
     else:
-        raise ValueError(f'Invalid args.inputfile_types: {args.inputfile_types} '+
-                f'{type(args.inputfile_types)}')
+        ref_heights = None
+    logging.info(f'config_type = {config_type} {type(config_type)}')
+    logging.info(f'input_type = {input_type} {type(input_type)}')
+    logging.info(f'num_stack = {num_stack} {type(num_stack)}')
+    logging.info(f'stack_types = {stack_types} {type(stack_types)}')
+    logging.info(f'num_imgs = {num_imgs} {type(num_imgs)}')
+    logging.info(f'img_offsets = {img_offsets} {type(img_offsets)}')
+    logging.info(f'ref_heights = {ref_heights} {type(ref_heights)}')
 
+    # Read input files and collect data files info
     datasets = []
+    with open(args.inputfiles) as f:
+        for line in f:
+            if not line.strip() or line.startswith('#'):
+                continue
+            fields = [x.strip() for x in line.split('\t')]
+            filepath = fields[0]
+            element_identifier = fields[1] if len(fields) > 1 else fields[0].split('/')[-1]
+            datasets.append({'element_identifier' : fields[1], 'filepath' : filepath})
+    logging.debug(f'datasets:\n{datasets}')
+    print(f'datasets:\n{datasets}')
+
+    # Read and sort data files
     collections = []
-    if input_as_collection:
-        # Read input file collections and collect data files info
-        with open(args.inputfiles[0]) as cf:
-            for line in cf:
-                if not line.strip() or line.startswith('#'):
-                    continue
-                fields = [x.strip() for x in line.split('\t')]
-                filepath = fields[0]
-                element_identifier = fields[1] if len(fields) > 1 else fields[0].split('/')[-1]
-                datasets.append({'element_identifier' : fields[1], 'filepath' : filepath})
-        logging.debug(f'datasets:\n{datasets}')
-
-        # Read and sort data files
-        for dataset in datasets:
-            element_identifier = [x.strip() for x in dataset['element_identifier'].split('_')]
-            if len(element_identifier) > 1:
-                name = element_identifier[0]
+    for dataset in datasets:
+        element_identifier = [x.strip() for x in dataset['element_identifier'].split('_')]
+        if len(element_identifier) > 1:
+            name = element_identifier[0]
+        else:
+            name = 'other'
+        filepath = dataset['filepath']
+        print(f'element_identifier = {element_identifier} {len(element_identifier)}')
+        print(f'name = {name}')
+        print(f'filepath = {filepath}')
+        if not len(collections):
+            collections = [{'name' : name, 'filepaths' : [filepath]}]
+        else:
+            collection = [c for c in collections if c['name'] == name]
+            if len(collection):
+                collection[0]['filepaths'].append(filepath)
             else:
-                name = 'other'
-            filepath = dataset['filepath']
-            if not len(collections):
-                collections = [{'name' : name, 'filepaths' : [filepath]}]
-            else:
-                collection = [c for c in collections if c['name'] == name]
-                if len(collection):
-                    collection[0]['filepaths'].append(filepath)
-                else:
-                    collection = {'name' : name, 'filepaths' : [filepath]}
-                    collections.append(collection)
-    else:
-        # Collect input file datasets info
-        collections = [{'name' : filetype, 'filepaths' : [filepath]} 
-                for filetype, filepath in zip(args.inputfile_types, args.inputfiles)]
+                collection = {'name' : name, 'filepaths' : [filepath]}
+                collections.append(collection)
     logging.debug(f'collections:\n{collections}')
-    if len(args.tomo_ranges) != 2*len(collections):
-        raise ValueError('Inconsistent tomo ranges size.')
+    print(f'collections:\n{collections}')
+    return
 
     # Instantiate Tomo object
     tomo = Tomo(config_file=args.config, config_out=args.output_config, log_level=log_level,
@@ -171,12 +176,7 @@
     for stack in stack_info['stacks']:
         stack['img_offset'] = args.tomo_ranges[2*num_collections]
         stack['num'] = args.tomo_ranges[2*num_collections+1]
-        if input_as_collection:
-            tomo_files = [c['filepaths'] for c in collections
-                if c['name'] == f'set{stack["index"]}']
-        else:
-            assert(collections[num_collections]['name'] == 'data')
-            tomo_files = [collections[num_collections]['filepaths']]
+        tomo_files = [c['filepaths'] for c in collections if c['name'] == f'set{stack["index"]}']
         if len(tomo_files) != 1 or len(tomo_files[0]) < 1:
             exit(f'Unable to obtain tomography images for set {stack["index"]}')
         tomo_stack_files.append(tomo_files[0])
--- a/tomo_setup.xml	Tue Aug 02 20:46:09 2022 +0000
+++ b/tomo_setup.xml	Tue Aug 16 16:55:50 2022 +0000
@@ -1,4 +1,4 @@
-<tool id="tomo_setup" name="Tomo Setup" version="0.2.1" python_template_version="3.9">
+<tool id="tomo_setup" name="Tomo Setup" version="0.2.2" python_template_version="3.9">
     <description>Preprocess tomography images</description>
     <macros>
         <import>tomo_macros.xml</import>
@@ -7,39 +7,75 @@
     <command detect_errors="exit_code">
         <![CDATA[
             mkdir setup_pngs;
-            #if str( $input.type_selector ) == "collection"
-                cp '$inputfiles' inputfiles.txt &&
-            #end if
+            cp '$inputconfig' inputconfig.txt &&
+            cp '$inputfiles' inputfiles.txt &&
             $__tool_directory__/tomo_setup.py
-            #if str( $input.type_selector ) == "collection"
-                -i 'inputfiles.txt'
-                -t 'collection'
-            #else:
-                -i #for $s in $input.tomo_sets# '${s.inputs}' #end for#
-                -t #for $s in $input.tomo_sets# '${s.set_type.set_selector}' #end for#
+            --inputconfig inputconfig.txt
+            --inputfiles inputfiles.txt
+            #if str($config_type.config_selector) == "config_file"
+                -c '$config'
+                --num_theta '$config_type.num_theta'
+            #else
+                --num_theta '$config_type.thetas.num_theta'
+                --theta_range '$config_type.thetas.theta_start $config_type.thetas.theta_end'
             #end if
-            -c '$config'
-            --theta_range '$thetas.theta_start $thetas.theta_end $thetas.num_thetas'
+            --output_config 'output_config.yaml'
             --output_data 'output_data.npz'
-            --output_config 'output_config.yaml'
             -l '$log'
-
         ]]>
     </command>
-    <!--
-    #for $s in $input.tomo_sets# ${s.offset} ${s.num} ${s.ref_height} #end for
-    -->
     <configfiles>
-         <configfile name="inputfiles">
-             <![CDATA[#slurp
-                #if str( $input.type_selector ) == "collection"
-                    #for $s in $input.tomo_sets
+        <configfile name="inputconfig">
+            <![CDATA[#slurp
+                #set $count = 0
+                #for $s in $config_type.input.tomo_sets
+                    #set $count += 1
+                #end for
+                #echo str($config_type.config_selector) #
+                #echo str($config_type.input.type_selector) #
+                #echo str($count) #
+                #for $s in $config_type.input.tomo_sets
+                    #echo ' ' + str($s.set_type.set_selector)
+                #end for
+                #echo '\n'
+                #for $s in $config_type.input.tomo_sets
+                    #if str($s.set_type.set_selector) == "data"
+                        #echo ' ' + '-1'
+                    #else
+                        #echo ' ' + str($s.set_type.num)
+                    #end if
+                #end for
+                #echo '\n'
+                #for $s in $config_type.input.tomo_sets
+                    #echo ' ' + str($s.set_type.offset)
+                #end for
+                #echo '\n'
+                #if str($config_type.config_selector) == "config_manual"
+                    #for $s in $config_type.input.tomo_sets
+                        #if str($s.set_type.set_selector) == "data"
+                            #echo ' ' + str($s.set_type.ref_height)
+                        #else
+                            #echo ' ' + '0.0'
+                        #end if
+                    #end for
+                    #echo '\n'
+                #end if
+             ]]>
+        </configfile>
+        <configfile name="inputfiles">
+            <![CDATA[#slurp
+                #if str($config_type.input.type_selector) == "collections"
+                    #for $s in $config_type.input.tomo_sets
                         #for $input in $s.inputs
                             #echo str($input) + '\t' + $input.element_identifier #
                         #end for
                     #end for
+                #else
+                    #for $s in $config_type.input.tomo_sets
+                        #echo str($s.inputs) + '\t' + str($s.set_type.set_selector) #
+                    #end for
                 #end if
-            ]]>
+             ]]>
         </configfile>
     </configfiles>
     <inputs>
@@ -50,24 +86,7 @@
             </param>
             <when value="config_file">
                 <expand macro="common_inputs"/>
-                <param name="num_stack" type="select" label="number of stacks">
-                    <options>
-                        <filter type="data_meta" ref="config" key="num_stack"/>
-                    </options>
-                </param>
-                <section name="thetas" title="Tomography angles">
-                    <param name="theta_start" type="select" label="Lower theta range value (from config)">
-                        <options>
-                            <filter type="data_meta" ref="config" key="theta_start"/>
-                        </options>
-                    </param>
-                    <param name="theta_end" type="select" label="Upper theta range value (from config)">
-                        <options>
-                            <filter type="data_meta" ref="config" key="theta_end"/>
-                        </options>
-                    </param>
-                    <param name="num_thetas" type="integer" min="1" value="1" optional="false" label="Number of angles"/>
-                </section>
+                <param name="num_theta" type="integer" min="1" value="0" optional="false" label="Number of angles"/>
                 <conditional name="input">
                     <param name="type_selector" type="select" label="Choose the dataset format">
                         <option value="collections">datasets as collections</option>
@@ -76,14 +95,24 @@
                     <when value="collections">
                         <repeat name='tomo_sets' title="Tomography image collections">
                             <param name="inputs" type="data_collection" label="Image file collection"/>
-                            <param name="offset" type="integer" min="0" value="0" label="Image index offset"/>
-                            <!--
-                            <param name="ref_height" type="select" label="Reference height (from config)">
-                                <options>
-                                    <filter type="data_meta" ref="config" key="ref_height"/>
-                                </options>
-                            </param>
-                            -->
+                            <conditional name="set_type">
+                                <param name="set_selector" type="select" label="Choose the dataset type">
+                                    <option value="tdf">dark field</option>
+                                    <option value="tbf">bright field</option>
+                                    <option value="data">tomography field</option>
+                                </param>
+                                <when value="tdf">
+                                    <param name="offset" type="integer" min="0" value="0" label="Image index offset"/>
+                                    <param name="num" type="integer" min="1" value="1" label="Number of images"/>
+                                </when>
+                                <when value="tbf">
+                                    <param name="offset" type="integer" min="0" value="0" label="Image index offset"/>
+                                    <param name="num" type="integer" min="1" value="1" label="Number of images"/>
+                                </when>
+                                <when value="data">
+                                    <param name="offset" type="integer" min="0" value="0" label="Image index offset"/>
+                                </when>
+                            </conditional>
                         </repeat>
                     </when>
                     <when value="files">
@@ -105,13 +134,6 @@
                                 </when>
                                 <when value="data">
                                     <param name="offset" type="integer" min="0" value="0" label="Image index offset"/>
-                                    <!--
-                                    <param name="ref_height" type="select" label="Reference height (from config)">
-                                        <options>
-                                            <filter type="data_meta" ref="config" key="ref_height"/>
-                                        </options>
-                                    </param>
-                                    -->
                                 </when>
                             </conditional>
                         </repeat>
@@ -119,11 +141,10 @@
                 </conditional>
             </when>
             <when value="config_manual">
-                <param name="num_stack" type="integer" min="1" value="1" label="Number of stacks"/>
                 <section name="thetas" title="Tomography angles">
-                    <param name="theta_start" type="float" min="0.0" max="360.0" value="0.0" label="Lower bound"/>
-                    <param name="theta_end" type="float" min="0.0" max="360.0" value="0.0" label="Upper bound"/>
-                    <param name="num_thetas" type="integer" min="1" value="1" label="Number of angles"/>
+                    <param name="num_theta" type="integer" min="1" value="0" optional="false" label="Number of angles"/>
+                    <param name="theta_start" type="float" min="0.0" max="360.0" value="0.0" optional="false" label="Start angle"/>
+                    <param name="theta_end" type="float" min="0.0" max="360.0" value="180.0" optional="false" label="End angle"/>
                 </section>
                 <conditional name="input">
                     <param name="type_selector" type="select" label="Choose the dataset format">
@@ -133,8 +154,25 @@
                     <when value="collections">
                         <repeat name='tomo_sets' title="Tomography image collections">
                             <param name="inputs" type="data_collection" label="Image file collection"/>
-                            <param name="offset" type="integer" min="0" value="0" label="Image index offset"/>
-                            <param name="ref_height" type="float" value="0.0" label="Reference height"/>
+                            <conditional name="set_type">
+                                <param name="set_selector" type="select" label="Choose the dataset type">
+                                    <option value="tdf">dark field</option>
+                                    <option value="tbf">bright field</option>
+                                    <option value="data">tomography field</option>
+                                </param>
+                                <when value="tdf">
+                                    <param name="offset" type="integer" min="0" value="0" label="Image index offset"/>
+                                    <param name="num" type="integer" min="1" value="1" label="Number of images"/>
+                                </when>
+                                <when value="tbf">
+                                    <param name="offset" type="integer" min="0" value="0" label="Image index offset"/>
+                                    <param name="num" type="integer" min="1" value="1" label="Number of images"/>
+                                </when>
+                                <when value="data">
+                                    <param name="offset" type="integer" min="0" value="0" label="Image index offset"/>
+                                    <param name="ref_height" type="float" value="0.0" label="Reference height"/>
+                                </when>
+                            </conditional>
                         </repeat>
                     </when>
                     <when value="files">
@@ -167,9 +205,8 @@
     </inputs>
     <outputs>
         <expand macro="common_outputs"/>
-        <data name="inputfiles" format="txt" label="Input files" from_work_dir="inputfiles.txt" hidden="true">
-            <filter>input['type_selector'] == 'collections'</filter>
-        </data>
+        <data name="inputconfig" format="txt" label="Input config" from_work_dir="inputconfig.txt" hidden="false"/>
+        <data name="inputfiles" format="txt" label="Input files" from_work_dir="inputfiles.txt" hidden="false"/>
         <collection name="setup_pngs" type="list" label="Tomo setup images">
             <discover_datasets pattern="__name_and_ext__" directory="setup_pngs"/>
         </collection>