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

https://bitbucket.org/cistrome/cistrome-harvard/ · Python · 141 lines · 82 code · 26 blank · 33 comment · 13 complexity · ca8c10f217b39573efe57dea0b7b46e5 MD5 · raw file

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