PageRenderTime 51ms CodeModel.GetById 19ms app.highlight 23ms RepoModel.GetById 2ms app.codeStats 0ms

/lib/galaxy/tools/parameters/input_translation.py

https://bitbucket.org/cistrome/cistrome-harvard/
Python | 106 lines | 92 code | 3 blank | 11 comment | 0 complexity | a269703b68af08684b66818fcf4ab666 MD5 | raw file
  1"""
  2Tool Input Translation.
  3"""
  4
  5import logging
  6from galaxy.util.bunch import Bunch
  7
  8log = logging.getLogger( __name__ )
  9
 10class ToolInputTranslator( object ):
 11    """
 12    Handles Tool input translation.
 13    This is used for data source tools
 14
 15    >>> from galaxy.util import Params
 16    >>> from elementtree.ElementTree import XML
 17    >>> translator = ToolInputTranslator.from_element( XML(
 18    ... '''
 19    ... <request_param_translation>
 20    ...  <request_param galaxy_name="URL_method" remote_name="URL_method" missing="post" />
 21    ...  <request_param galaxy_name="URL" remote_name="URL" missing="" >
 22    ...     <append_param separator="&amp;" first_separator="?" join="=">
 23    ...         <value name="_export" missing="1" />
 24    ...         <value name="GALAXY_URL" missing="0" />
 25    ...     </append_param>
 26    ...  </request_param>
 27    ...  <request_param galaxy_name="dbkey" remote_name="db" missing="?" />
 28    ...  <request_param galaxy_name="organism" remote_name="org" missing="unknown species" />
 29    ...  <request_param galaxy_name="table" remote_name="hgta_table" missing="unknown table" />
 30    ...  <request_param galaxy_name="description" remote_name="hgta_regionType" missing="no description" />
 31    ...  <request_param galaxy_name="data_type" remote_name="hgta_outputType" missing="tabular" >
 32    ...   <value_translation>
 33    ...    <value galaxy_value="tabular" remote_value="primaryTable" />
 34    ...    <value galaxy_value="tabular" remote_value="selectedFields" />
 35    ...    <value galaxy_value="wig" remote_value="wigData" />
 36    ...    <value galaxy_value="interval" remote_value="tab" />
 37    ...    <value galaxy_value="html" remote_value="hyperlinks" />
 38    ...    <value galaxy_value="fasta" remote_value="sequence" />
 39    ...   </value_translation>
 40    ...  </request_param>
 41    ... </request_param_translation>
 42    ... ''' ) )
 43    >>> params = Params( { 'db':'hg17', 'URL':'URL_value', 'org':'Human', 'hgta_outputType':'primaryTable'  } )
 44    >>> translator.translate( params )
 45    >>> print params
 46    {'hgta_outputType': 'primaryTable', 'data_type': 'tabular', 'table': 'unknown table', 'URL': 'URL_value?GALAXY_URL=0&_export=1', 'org': 'Human', 'URL_method': 'post', 'db': 'hg17', 'organism': 'Human', 'dbkey': 'hg17', 'description': 'no description'}
 47    """
 48    @classmethod
 49    def from_element( cls, elem ):
 50        """Loads the proper filter by the type attribute of elem"""
 51        rval = ToolInputTranslator()
 52        for req_param in elem.findall( "request_param" ):
 53            # req_param tags must look like <request_param galaxy_name="dbkey" remote_name="GENOME" missing="" />
 54            #trans_list = []
 55            remote_name = req_param.get( "remote_name" )
 56            galaxy_name = req_param.get( "galaxy_name" )
 57            missing = req_param.get( "missing" )
 58            value_trans = {}
 59            append_param = None
 60
 61            value_trans_elem = req_param.find( 'value_translation' )
 62            if value_trans_elem:
 63                for value_elem in value_trans_elem.findall( 'value' ):
 64                    remote_value = value_elem.get( "remote_value" )
 65                    galaxy_value = value_elem.get( "galaxy_value" )
 66                    if None not in [ remote_value, galaxy_value ]:
 67                        value_trans[ remote_value ] = galaxy_value
 68
 69            append_param_elem = req_param.find( "append_param" )
 70            if append_param_elem:
 71                separator = append_param_elem.get( 'separator', ',' )
 72                first_separator = append_param_elem.get( 'first_separator', None )
 73                join_str = append_param_elem.get( 'join', '=' )
 74                append_dict = {}
 75                for value_elem in append_param_elem.findall( 'value' ):
 76                    value_name = value_elem.get( 'name' )
 77                    value_missing = value_elem.get( 'missing' )
 78                    if None not in [ value_name, value_missing ]:
 79                        append_dict[ value_name ] = value_missing
 80                append_param = Bunch( separator = separator, first_separator = first_separator, join_str = join_str, append_dict = append_dict )
 81
 82            rval.param_trans_dict[ remote_name ] = Bunch( galaxy_name = galaxy_name, missing = missing, value_trans = value_trans, append_param = append_param  )
 83
 84        return rval
 85
 86    def __init__( self ):
 87        self.param_trans_dict = {}
 88
 89    def translate( self, params ):
 90        """
 91        update params in-place
 92        """
 93        for remote_name, translator in self.param_trans_dict.iteritems():
 94            galaxy_name = translator.galaxy_name #NB: if a param by name galaxy_name is provided, it is always thrown away unless galaxy_name == remote_name
 95            value = params.get( remote_name, translator.missing ) #get value from input params, or use default value specified in tool config
 96            if translator.value_trans and value in translator.value_trans:
 97                value = translator.value_trans[ value ]
 98            if translator.append_param:
 99                for param_name, missing_value in translator.append_param.append_dict.iteritems():
100                    param_value = params.get( param_name, missing_value )
101                    if translator.append_param.first_separator and translator.append_param.first_separator not in value:
102                        sep = translator.append_param.first_separator
103                    else:
104                        sep = translator.append_param.separator
105                    value += '%s%s%s%s' % ( sep, param_name, translator.append_param.join_str, param_value )
106            params.update( { galaxy_name: value } )