PageRenderTime 37ms CodeModel.GetById 21ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/galaxy/tools/parameters/__init__.py

https://bitbucket.org/cistrome/cistrome-harvard/
Python | 128 lines | 122 code | 3 blank | 3 comment | 0 complexity | 48a90c66a3dc2ceca1a4b2e50770b271 MD5 | raw file
  1"""
  2Classes encapsulating Galaxy tool parameters.
  3"""
  4
  5from basic import *
  6from grouping import *
  7from galaxy.util.json import *
  8
  9
 10def visit_input_values( inputs, input_values, callback, name_prefix="", label_prefix="" ):
 11    """
 12    Given a tools parameter definition (`inputs`) and a specific set of
 13    parameter `values`, call `callback` for each non-grouping parameter,
 14    passing the parameter object, value, a constructed unique name,
 15    and a display label.
 16
 17    If the callback returns a value, it will be replace the old value.
 18
 19    FIXME: There is redundancy between this and the visit_inputs methods of
 20           Repeat and Group. This tracks labels and those do not. It would
 21           be nice to unify all the places that recursively visit inputs.
 22    """
 23    for input in inputs.itervalues():
 24        if isinstance( input, Repeat ) or isinstance( input, UploadDataset ):
 25            for i, d in enumerate( input_values[ input.name ] ):
 26                index = d['__index__']
 27                new_name_prefix = name_prefix + "%s_%d|" % ( input.name, index )
 28                new_label_prefix = label_prefix + "%s %d > " % ( input.title, i + 1 )
 29                visit_input_values( input.inputs, d, callback, new_name_prefix, new_label_prefix )
 30        elif isinstance( input, Conditional ):
 31            values = input_values[ input.name ]
 32            current = values["__current_case__"]
 33            label_prefix = label_prefix
 34            new_name_prefix = name_prefix + input.name + "|"
 35            visit_input_values( input.cases[current].inputs, values, callback, new_name_prefix, label_prefix )
 36        else:
 37            new_value = callback( input,
 38                                  input_values[input.name],
 39                                  prefixed_name=name_prefix + input.name,
 40                                  prefixed_label=label_prefix + input.label )
 41            if new_value:
 42                input_values[input.name] = new_value
 43
 44
 45def check_param( trans, param, incoming_value, param_values, source='html' ):
 46    """
 47    Check the value of a single parameter `param`. The value in
 48    `incoming_value` is converted from its HTML encoding and validated.
 49    The `param_values` argument contains the processed values of
 50    previous parameters (this may actually be an ExpressionContext
 51    when dealing with grouping scenarios).
 52    """
 53    value = incoming_value
 54    error = None
 55    try:
 56        if value is not None or isinstance( param, DataToolParameter ):
 57            # Convert value from HTML representation
 58            if source == 'html':
 59                value = param.from_html( value, trans, param_values )
 60            else:
 61                value = param.from_json( value, trans, param_values )
 62            # Only validate if late validation is not needed
 63            if not param.need_late_validation( trans, param_values ):
 64                # Allow the value to be converted if necessary
 65                filtered_value = param.filter_value( value, trans, param_values )
 66                # Then do any further validation on the value
 67                param.validate( filtered_value, trans.history )
 68        elif value is None and isinstance( param, SelectToolParameter ):
 69            # An empty select list or column list
 70            param.validate( value, trans.history )
 71    except ValueError, e:
 72        error = str( e )
 73    return value, error
 74
 75
 76def params_to_strings( params, param_values, app ):
 77    """
 78    Convert a dictionary of parameter values to a dictionary of strings
 79    suitable for persisting. The `value_to_basic` method of each parameter
 80    is called to convert its value to basic types, the result of which
 81    is then json encoded (this allowing complex nested parameters and
 82    such).
 83    """
 84    rval = dict()
 85    for key, value in param_values.iteritems():
 86        if key in params:
 87            value = params[ key ].value_to_basic( value, app )
 88        rval[ key ] = str( to_json_string( value ) )
 89    return rval
 90
 91
 92def params_from_strings( params, param_values, app, ignore_errors=False ):
 93    """
 94    Convert a dictionary of strings as produced by `params_to_strings`
 95    back into parameter values (decode the json representation and then
 96    allow each parameter to convert the basic types into the parameters
 97    preferred form).
 98    """
 99    rval = dict()
100    for key, value in param_values.iteritems():
101        value = json_fix( from_json_string( value ) )
102        if key in params:
103            value = params[key].value_from_basic( value, app, ignore_errors )
104        rval[ key ] = value
105    return rval
106
107
108def params_to_incoming( incoming, inputs, input_values, app, name_prefix="" ):
109    """
110    Given a tool's parameter definition (`inputs`) and a specific set of
111    parameter `input_values` objects, populate `incoming` with the html values.
112
113    Useful for e.g. the rerun function.
114    """
115    for input in inputs.itervalues():
116        if isinstance( input, Repeat ) or isinstance( input, UploadDataset ):
117            for i, d in enumerate( input_values[ input.name ] ):
118                index = d['__index__']
119                new_name_prefix = name_prefix + "%s_%d|" % ( input.name, index )
120                params_to_incoming( incoming, input.inputs, d, app, new_name_prefix )
121        elif isinstance( input, Conditional ):
122            values = input_values[ input.name ]
123            current = values["__current_case__"]
124            new_name_prefix = name_prefix + input.name + "|"
125            incoming[ new_name_prefix + input.test_param.name ] = values[ input.test_param.name ]
126            params_to_incoming( incoming, input.cases[current].inputs, values, app, new_name_prefix )
127        else:
128            incoming[ name_prefix + input.name ] = input.to_html_value( input_values.get( input.name ), app )