#!/usr/bin/env python

"""
Step 7
======
In this step, we'll do everything from steps 1 through 6 - then
run a published workflow on the uploaded file and wait for it to finish.

The output will be in a new history named 'Step 7'.
"""
import os
import sys
import pprint
import time

import users_1
import histories_3
import tools_1
import hdas_2

# and we're using workflows_1
import workflows_1

NEW_HISTORY_NAME = 'Step 7'
DATA_TO_UPLOAD = 'data/myIlluminaRun.solexa.fastq'

# we'll also store the name of the workflow we want to run here
SOLEXA_QC_WORKFLOW_NAME = 'Joined Solexa QC'
# and the step id of that workflow that accepts an input dataset
SOLEXA_QC_WORKFLOW_INPUT_STEP = 6

# ----------------------------------------------------------------------------- main
if __name__ == '__main__':
    try:
        # check the connection
        users = users_1.get_users()

        # create a new history
        new_history = histories_3.create_history( NEW_HISTORY_NAME )
        print 'created history!', new_history[ 'name' ]
        new_history_id = new_history[ 'id' ]
        new_history_details = histories_3.get_history( new_history_id )

        # upload a file
        tool_output_datasets = tools_1.upload_hda( new_history_id, DATA_TO_UPLOAD )
        uploaded_file_data = tool_output_datasets[ 'outputs' ][0]
        print 'uploaded hda!', uploaded_file_data[ 'name' ]
        uploaded_file_id = uploaded_file_data[ 'id' ]

        # and use get_hda to get details on the new, uploaded HDA
        uploaded_hda_details = hdas_2.get_hda( new_history_id, uploaded_file_id )
        uploaded_hda_state = uploaded_hda_details[ 'state' ]

        # wait for the upload to finish
        while uploaded_hda_state != 'ok':
            print '\t uploaded_hda_state:', uploaded_hda_state
            print '\t (waiting 4 seconds...)'
            time.sleep( 4.0 )

            # keep checking to get any new state the HDA might move into
            uploaded_hda_details = hdas_2.get_hda( new_history_id, uploaded_file_id )
            uploaded_hda_state = uploaded_hda_details[ 'state' ]

        # here's the new stuff...moving fast now

        # get the info of the all workflows available to us
        all_workflows = workflows_1.get_workflows()

        # let's search that info for the name of the one we want in the list of all workflows
        found_workflow = None
        for workflow in all_workflows:
            if workflow[ 'name' ] == SOLEXA_QC_WORKFLOW_NAME:
                found_workflow = workflow

        if not found_workflow:
            # uh-oh: couldn't find the one we want - error out
            raise Exception( 'If you see this error, let one of the workhop presenters know' )

        print 'found workflow!', found_workflow[ 'name' ]
        # get some details on that workflow
        target_workflow_id = found_workflow[ 'id' ]
        target_workflow_details = workflows_1.get_workflow( target_workflow_id )

        # now we'll run it:
        print 'running',  found_workflow[ 'name' ], 'workflow...'
        # use the new history to store output, the uploaded file as the workflow input
        #   and make sure to send in the id of the step that accepts the input file
        workflow_output = workflows_1.run_single_input_workflow_on_hda( target_workflow_id,
            new_history_id, uploaded_file_id, SOLEXA_QC_WORKFLOW_INPUT_STEP )
        print 'workflow started!'

        # the 'outputs' list of the workflow_output dictionary are the ids of the HDAs the workflow creates
        output_hda_ids = workflow_output[ 'outputs' ]

        # we'll wait for them all to finish using a technique similar to the one we used to wait for the upload
        #   this time, we'll do it for each of the hdas output by the workflow
        for hda_id in output_hda_ids:
            # again, this is the same pattern as the upload above
            workflow_hda_details = hdas_2.get_hda( new_history_id, hda_id )
            workflow_hda_state = workflow_hda_details[ 'state' ]

            # we'll add the name, too - to make things clearer
            workflow_hda_name = workflow_hda_details[ 'name' ]
            print workflow_hda_name

            while workflow_hda_state != 'ok':
                print '\t state:', workflow_hda_state
                print '\t (waiting 4 seconds...)'
                time.sleep( 4.0 )

                # keep checking to get any new state the HDA might move into
                workflow_hda_details = hdas_2.get_hda( new_history_id, hda_id )
                workflow_hda_state = workflow_hda_details[ 'state' ]

            # this output is done
            print '\t ok'

        # all workflow HDAs are in the ok state!
        print 'workflow complete!'
        
    except Exception, exc:
        print 'Error running workflow on uploaded file:', str( exc )
        sys.exit( 1 )

    print 'Output:'
    #pprint.pprint( target_workflow_details, indent=2 )
    pprint.pprint( workflow_output, indent=2 )
