Locked History Actions

attachment:step_7.py of Events/GCC2013/TrainingDay/API

Attachment 'step_7.py'

Download

   1 #!/usr/bin/env python
   2 
   3 """
   4 Step 7
   5 ======
   6 In this step, we'll do everything from steps 1 through 6 - then
   7 run a published workflow on the uploaded file and wait for it to finish.
   8 
   9 The output will be in a new history named 'Step 7'.
  10 """
  11 import os
  12 import sys
  13 import pprint
  14 import time
  15 
  16 import users_1
  17 import histories_3
  18 import tools_1
  19 import hdas_2
  20 
  21 # and we're using workflows_1
  22 import workflows_1
  23 
  24 NEW_HISTORY_NAME = 'Step 7'
  25 DATA_TO_UPLOAD = 'data/myIlluminaRun.solexa.fastq'
  26 
  27 # we'll also store the name of the workflow we want to run here
  28 SOLEXA_QC_WORKFLOW_NAME = 'Joined Solexa QC'
  29 # and the step id of that workflow that accepts an input dataset
  30 SOLEXA_QC_WORKFLOW_INPUT_STEP = 6
  31 
  32 # ----------------------------------------------------------------------------- main
  33 if __name__ == '__main__':
  34     try:
  35         # check the connection
  36         users = users_1.get_users()
  37 
  38         # create a new history
  39         new_history = histories_3.create_history( NEW_HISTORY_NAME )
  40         print 'created history!', new_history[ 'name' ]
  41         new_history_id = new_history[ 'id' ]
  42         new_history_details = histories_3.get_history( new_history_id )
  43 
  44         # upload a file
  45         tool_output_datasets = tools_1.upload_hda( new_history_id, DATA_TO_UPLOAD )
  46         uploaded_file_data = tool_output_datasets[ 'outputs' ][0]
  47         print 'uploaded hda!', uploaded_file_data[ 'name' ]
  48         uploaded_file_id = uploaded_file_data[ 'id' ]
  49 
  50         # and use get_hda to get details on the new, uploaded HDA
  51         uploaded_hda_details = hdas_2.get_hda( new_history_id, uploaded_file_id )
  52         uploaded_hda_state = uploaded_hda_details[ 'state' ]
  53 
  54         # wait for the upload to finish
  55         while uploaded_hda_state != 'ok':
  56             print '\t uploaded_hda_state:', uploaded_hda_state
  57             print '\t (waiting 4 seconds...)'
  58             time.sleep( 4.0 )
  59 
  60             # keep checking to get any new state the HDA might move into
  61             uploaded_hda_details = hdas_2.get_hda( new_history_id, uploaded_file_id )
  62             uploaded_hda_state = uploaded_hda_details[ 'state' ]
  63 
  64         # here's the new stuff...moving fast now
  65 
  66         # get the info of the all workflows available to us
  67         all_workflows = workflows_1.get_workflows()
  68 
  69         # let's search that info for the name of the one we want in the list of all workflows
  70         found_workflow = None
  71         for workflow in all_workflows:
  72             if workflow[ 'name' ] == SOLEXA_QC_WORKFLOW_NAME:
  73                 found_workflow = workflow
  74 
  75         if not found_workflow:
  76             # uh-oh: couldn't find the one we want - error out
  77             raise Exception( 'If you see this error, let one of the workhop presenters know' )
  78 
  79         print 'found workflow!', found_workflow[ 'name' ]
  80         # get some details on that workflow
  81         target_workflow_id = found_workflow[ 'id' ]
  82         target_workflow_details = workflows_1.get_workflow( target_workflow_id )
  83 
  84         # now we'll run it:
  85         print 'running',  found_workflow[ 'name' ], 'workflow...'
  86         # use the new history to store output, the uploaded file as the workflow input
  87         #   and make sure to send in the id of the step that accepts the input file
  88         workflow_output = workflows_1.run_single_input_workflow_on_hda( target_workflow_id,
  89             new_history_id, uploaded_file_id, SOLEXA_QC_WORKFLOW_INPUT_STEP )
  90         print 'workflow started!'
  91 
  92         # the 'outputs' list of the workflow_output dictionary are the ids of the HDAs the workflow creates
  93         output_hda_ids = workflow_output[ 'outputs' ]
  94 
  95         # we'll wait for them all to finish using a technique similar to the one we used to wait for the upload
  96         #   this time, we'll do it for each of the hdas output by the workflow
  97         for hda_id in output_hda_ids:
  98             # again, this is the same pattern as the upload above
  99             workflow_hda_details = hdas_2.get_hda( new_history_id, hda_id )
 100             workflow_hda_state = workflow_hda_details[ 'state' ]
 101 
 102             # we'll add the name, too - to make things clearer
 103             workflow_hda_name = workflow_hda_details[ 'name' ]
 104             print workflow_hda_name
 105 
 106             while workflow_hda_state != 'ok':
 107                 print '\t state:', workflow_hda_state
 108                 print '\t (waiting 4 seconds...)'
 109                 time.sleep( 4.0 )
 110 
 111                 # keep checking to get any new state the HDA might move into
 112                 workflow_hda_details = hdas_2.get_hda( new_history_id, hda_id )
 113                 workflow_hda_state = workflow_hda_details[ 'state' ]
 114 
 115             # this output is done
 116             print '\t ok'
 117 
 118         # all workflow HDAs are in the ok state!
 119         print 'workflow complete!'
 120         
 121     except Exception, exc:
 122         print 'Error running workflow on uploaded file:', str( exc )
 123         sys.exit( 1 )
 124 
 125     print 'Output:'
 126     #pprint.pprint( target_workflow_details, indent=2 )
 127     pprint.pprint( workflow_output, indent=2 )

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2013-06-24 16:01:09, 8.2 KB) [[attachment:Galaxy-Workflow-Joined_Solexa_QC.ga]]
  • [get | view] (2013-06-24 16:46:36, 14.0 KB) [[attachment:all-scripts.tar.gz]]
  • [get | view] (2013-06-24 16:01:23, 5.2 KB) [[attachment:common.py]]
  • [get | view] (2013-06-24 16:04:07, 0.9 KB) [[attachment:datasets_1.py]]
  • [get | view] (2013-06-24 16:04:15, 1.2 KB) [[attachment:datasets_2.py]]
  • [get | view] (2013-06-24 16:04:22, 3.4 KB) [[attachment:datasets_3.py]]
  • [get | view] (2013-06-24 16:04:33, 1.5 KB) [[attachment:hdas_1.py]]
  • [get | view] (2013-06-24 16:04:50, 1.7 KB) [[attachment:hdas_2.py]]
  • [get | view] (2013-06-24 16:05:07, 2.2 KB) [[attachment:hdas_3.py]]
  • [get | view] (2013-06-24 16:03:38, 0.9 KB) [[attachment:histories_1.py]]
  • [get | view] (2013-06-24 16:03:52, 2.1 KB) [[attachment:histories_2.py]]
  • [get | view] (2013-06-24 16:05:25, 2.2 KB) [[attachment:histories_3.py]]
  • [get | view] (2013-06-24 16:05:34, 3.3 KB) [[attachment:lddas_1.py]]
  • [get | view] (2013-06-24 16:05:41, 1.3 KB) [[attachment:libraries_1.py]]
  • [get | view] (2013-06-24 16:01:36, 0.8 KB) [[attachment:setup.py]]
  • [get | view] (2013-06-24 16:02:45, 1.1 KB) [[attachment:step_1.py]]
  • [get | view] (2013-06-24 16:07:15, 9.9 KB) [[attachment:step_10.py]]
  • [get | view] (2013-06-24 16:03:28, 1.4 KB) [[attachment:step_2.py]]
  • [get | view] (2013-06-24 16:06:01, 1.6 KB) [[attachment:step_3.py]]
  • [get | view] (2013-06-24 16:06:11, 1.1 KB) [[attachment:step_4.py]]
  • [get | view] (2013-06-24 16:06:20, 1.9 KB) [[attachment:step_5.py]]
  • [get | view] (2013-06-24 16:06:44, 2.5 KB) [[attachment:step_6.py]]
  • [get | view] (2013-06-24 16:06:53, 4.9 KB) [[attachment:step_7.py]]
  • [get | view] (2013-06-24 16:07:05, 5.9 KB) [[attachment:step_8.py]]
  • [get | view] (2013-06-30 13:51:46, 8.0 KB) [[attachment:step_9.py]]
  • [get | view] (2013-06-24 16:41:51, 1.8 KB) [[attachment:tools_1.py]]
  • [get | view] (2013-06-24 16:44:32, 2.0 KB) [[attachment:upload_to_history.py]]
  • [get | view] (2013-06-24 16:03:03, 1.3 KB) [[attachment:users_1.py]]
  • [get | view] (2013-06-24 16:44:44, 2.9 KB) [[attachment:workflows_1.py]]
 All files | Selected Files: delete move to page

You are not allowed to attach a file to this page.