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