PageRenderTime 24ms CodeModel.GetById 8ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/galaxy/tools/actions/history_imp_exp.py

https://bitbucket.org/cistrome/cistrome-harvard/
Python | 141 lines | 82 code | 26 blank | 33 comment | 14 complexity | ca8c10f217b39573efe57dea0b7b46e5 MD5 | raw file
  1import tempfile, os
  2from __init__ import ToolAction
  3from galaxy.util.odict import odict
  4from galaxy.tools.imp_exp import JobImportHistoryArchiveWrapper, JobExportHistoryArchiveWrapper
  5
  6import logging
  7log = logging.getLogger( __name__ )
  8
  9class ImportHistoryToolAction( ToolAction ):
 10    """Tool action used for importing a history to an archive. """
 11
 12    def execute( self, tool, trans, incoming = {}, set_output_hid = False, overwrite = True, history=None, **kwargs ):
 13        #
 14        # Create job.
 15        #
 16        job = trans.app.model.Job()
 17        session = trans.get_galaxy_session()
 18        job.session_id = session and session.id
 19        if history:
 20            history_id = history.id
 21        elif trans.history:
 22            history_id = trans.history.id
 23        else:
 24            history_id = None
 25        job.history_id = history_id
 26        job.tool_id = tool.id
 27        job.user_id = trans.user.id
 28        start_job_state = job.state #should be job.states.NEW
 29        job.state = job.states.WAITING #we need to set job state to something other than NEW, or else when tracking jobs in db it will be picked up before we have added input / output parameters
 30        trans.sa_session.add( job )
 31        trans.sa_session.flush() #ensure job.id are available
 32
 33        #
 34        # Setup job and job wrapper.
 35        #
 36
 37        # Add association for keeping track of job, history relationship.
 38
 39        # Use abspath because mkdtemp() does not, contrary to the documentation,
 40        # always return an absolute path.
 41        archive_dir = os.path.abspath( tempfile.mkdtemp() )
 42        jiha = trans.app.model.JobImportHistoryArchive( job=job, archive_dir=archive_dir )
 43        trans.sa_session.add( jiha )
 44
 45        #
 46        # Add parameters to job_parameter table.
 47        #
 48
 49        # Set additional parameters.
 50        incoming[ '__DEST_DIR__' ] = jiha.archive_dir
 51        for name, value in tool.params_to_strings( incoming, trans.app ).iteritems():
 52            job.add_parameter( name, value )
 53
 54        job.state = start_job_state #job inputs have been configured, restore initial job state
 55        job.set_handler(tool.get_job_handler(None))
 56        trans.sa_session.flush()
 57
 58        # Queue the job for execution
 59        trans.app.job_queue.put( job.id, tool.id )
 60        trans.log_event( "Added import history job to the job queue, id: %s" % str(job.id), tool_id=job.tool_id )
 61
 62        return job, odict()
 63
 64class ExportHistoryToolAction( ToolAction ):
 65    """Tool action used for exporting a history to an archive. """
 66
 67    def execute( self, tool, trans, incoming = {}, set_output_hid = False, overwrite = True, history=None, **kwargs ):
 68        #
 69        # Get history to export.
 70        #
 71        history = None
 72        for name, value in incoming.iteritems():
 73            if isinstance( value, trans.app.model.History ):
 74                history_param_name = name
 75                history = value
 76                del incoming[ history_param_name ]
 77                break
 78
 79        if not history:
 80            raise Exception( 'There is no history to export.' )
 81
 82        #
 83        # Create the job and output dataset objects
 84        #
 85        job = trans.app.model.Job()
 86        session = trans.get_galaxy_session()
 87        job.session_id = session and session.id
 88        if history:
 89            history_id = history.id
 90        else:
 91            history_id = trans.history.id
 92        job.history_id = history_id
 93        job.tool_id = tool.id
 94        if trans.user:
 95            # If this is an actual user, run the job as that individual.  Otherwise we're running as guest.
 96            job.user_id = trans.user.id
 97        start_job_state = job.state #should be job.states.NEW
 98        job.state = job.states.WAITING #we need to set job state to something other than NEW, or else when tracking jobs in db it will be picked up before we have added input / output parameters
 99        trans.sa_session.add( job )
100
101        # Create dataset that will serve as archive.
102        archive_dataset = trans.app.model.Dataset()
103        trans.sa_session.add( archive_dataset )
104
105        trans.sa_session.flush() #ensure job.id and archive_dataset.id are available
106        trans.app.object_store.create( archive_dataset ) # set the object store id, create dataset (if applicable)
107
108        #
109        # Setup job and job wrapper.
110        #
111
112        # Add association for keeping track of job, history, archive relationship.
113        jeha = trans.app.model.JobExportHistoryArchive( job=job, history=history, \
114                                                        dataset=archive_dataset, \
115                                                        compressed=incoming[ 'compress' ] )
116        trans.sa_session.add( jeha )
117
118        job_wrapper = JobExportHistoryArchiveWrapper( job )
119        cmd_line = job_wrapper.setup_job( trans, jeha, include_hidden=incoming[ 'include_hidden' ], \
120                                            include_deleted=incoming[ 'include_deleted' ] )
121
122        #
123        # Add parameters to job_parameter table.
124        #
125
126        # Set additional parameters.
127        incoming[ '__HISTORY_TO_EXPORT__' ] = history.id
128        incoming[ '__EXPORT_HISTORY_COMMAND_INPUTS_OPTIONS__' ] = cmd_line
129        for name, value in tool.params_to_strings( incoming, trans.app ).iteritems():
130            job.add_parameter( name, value )
131
132        job.state = start_job_state #job inputs have been configured, restore initial job state
133        job.set_handler(tool.get_job_handler(None))
134        trans.sa_session.flush()
135
136
137        # Queue the job for execution
138        trans.app.job_queue.put( job.id, tool.id )
139        trans.log_event( "Added export history job to the job queue, id: %s" % str(job.id), tool_id=job.tool_id )
140
141        return job, odict()