Commit 0e196f53 authored by Yakubov, Sergey's avatar Yakubov, Sergey
Browse files

Merge branch '2-demo-of-running-galaxy-workflows-from-a-python-script' into 'main'

Resolve "Demo of running Galaxy workflows from a Python script"

Closes #2

See merge request !2
parents cfa46668 ab3e22af
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
#### Description
this examples imports `test.json` file and `test_workflow.ga` 
from a local filesystem to Galaxy, runs this workflow and prints the output.
The workflow is quite simple - it counts lines and words (like in example 1)
and as a second step returns the last line of the result (removes header). 
The workflow was prepared in advance in Galaxy Web interface and exported to a file.  

#### To run it:
see README in a root folder
+73 −0
Original line number Diff line number Diff line
# this examples imports test.json file and test_workflow.ga to Galaxy, runs this workflow and prints output
# the workflow is quite simple - it counts lines and words (like in example 1)
# and as a second step returns last line of the result (removes header)

import os

from bioblend import galaxy

galaxy_url = os.getenv("GALAXY_URL")
galaxy_user_api_key = os.getenv("GALAXY_API_KEY")

# connect to a Galaxy instance, create clients
galaxy_instance = galaxy.GalaxyInstance(url=galaxy_url, key=galaxy_user_api_key)
dataset_client = galaxy.datasets.DatasetClient(galaxy_instance)
invocations_client = galaxy.invocations.InvocationClient(galaxy_instance)

# get active history id
history_id = galaxy_instance.histories.get_histories()[0]["id"]
# or create a new one
# history_id = gi.histories.create_history(name="New history")["id"]

# upload input file and wait until upload is finished
uploaded_dataset_id = galaxy_instance.tools.upload_file('test.json', history_id)['outputs'][0]['id']
dataset_client.wait_for_dataset(uploaded_dataset_id)


# import workflow
workflow = galaxy_instance.workflows.import_workflow_from_local_path('test_workflow.ga')
workflow_id = workflow["id"]

# alteratively - use existing workflow
#workflows = galaxy_instance.workflows.get_workflows(workflow_id=None, name="test", published=False)
#workflow_id=workflows[0]["id"]


# prepare workflow input map, in our case - assign input to uploaded dataset id
inputs = galaxy_instance.workflows.get_workflow_inputs(workflow_id,label="input")
datamap = {
    inputs[0]: {"src": "hda", "id": uploaded_dataset_id},
}

# invoke workflow
invocation_id = galaxy_instance.workflows.invoke_workflow(
    workflow_id, inputs=datamap, history_id=history_id, import_inputs_to_history=False
)["id"]


# wait until workflow schedulled so that we could get last workflow step id
scheduled_invocation = invocations_client.wait_for_invocation(invocation_id)
last_step_id=scheduled_invocation["steps"][-1]["id"]

# from that id - get output dataset id
result_dataset_id = invocations_client.show_invocation_step(invocation_id,last_step_id)["outputs"]["out_file1"]["id"]

# wait for this dataset is ready
dataset_client.wait_for_dataset(result_dataset_id)

# download this dataset from Galaxy
exported_data = dataset_client.download_dataset(dataset_id=result_dataset_id,
                                                file_path=None,  # set file_path to output to file
                                                use_default_filename=False)
print(exported_data)


# uncomment to delete workflow
#galaxy_instance.workflows.delete_workflow(workflow_id)

# uncomment to delete datasets
# galaxy_instance.histories.delete_dataset(history_id, uploaded_dataset_id, purge=True)
# galaxy_instance.histories.delete_dataset(history_id, result_dataset_id, purge=True)

# uncomment to delete history and all data, purge=True might be not allowed by Galaxy admin
# galaxy_instance.histories.delete_history(history_id, purge=True)
+3 −0
Original line number Diff line number Diff line
{
  "hello": "world"
}
 No newline at end of file
+129 −0
Original line number Diff line number Diff line
{
    "a_galaxy_workflow": "true",
    "annotation": "",
    "format-version": "0.1",
    "name": "test",
    "steps": {
        "0": {
            "annotation": "",
            "content_id": null,
            "errors": null,
            "id": 0,
            "input_connections": {},
            "inputs": [
                {
                    "description": "",
                    "name": "input"
                }
            ],
            "label": "input",
            "name": "Input dataset",
            "outputs": [],
            "position": {
                "left": 0.0,
                "top": 1.953125
            },
            "tool_id": null,
            "tool_state": "{\"optional\": false, \"tag\": \"\"}",
            "tool_version": null,
            "type": "data_input",
            "uuid": "cc9eaf78-dd54-488e-a591-6ef7bfc7fc28",
            "workflow_outputs": [
                {
                    "label": null,
                    "output_name": "output",
                    "uuid": "a30e6c01-829d-4cca-8b60-8e28c5e69e4f"
                }
            ]
        },
        "1": {
            "annotation": "",
            "content_id": "wc_gnu",
            "errors": null,
            "id": 1,
            "input_connections": {
                "input1": {
                    "id": 0,
                    "output_name": "output"
                }
            },
            "inputs": [
                {
                    "description": "runtime parameter for tool Line/Word/Character count",
                    "name": "input1"
                }
            ],
            "label": null,
            "name": "Line/Word/Character count",
            "outputs": [
                {
                    "name": "out_file1",
                    "type": "tabular"
                }
            ],
            "position": {
                "left": 242.03123827980772,
                "top": 0.0
            },
            "post_job_actions": {},
            "tool_id": "wc_gnu",
            "tool_state": "{\"include_header\": \"true\", \"input1\": {\"__class__\": \"RuntimeValue\"}, \"options\": [\"lines\", \"words\"], \"__page__\": null, \"__rerun_remap_job_id__\": null}",
            "tool_version": "1.0.0",
            "type": "tool",
            "uuid": "ff48f34b-c6de-4947-b893-7fa2d0b74460",
            "workflow_outputs": [
                {
                    "label": null,
                    "output_name": "out_file1",
                    "uuid": "4e7752e9-05bc-46f7-a618-505df57baa15"
                }
            ]
        },
        "2": {
            "annotation": "",
            "content_id": "Show tail1",
            "errors": null,
            "id": 2,
            "input_connections": {
                "input": {
                    "id": 1,
                    "output_name": "out_file1"
                }
            },
            "inputs": [
                {
                    "description": "runtime parameter for tool Select last",
                    "name": "input"
                }
            ],
            "label": null,
            "name": "Select last",
            "outputs": [
                {
                    "name": "out_file1",
                    "type": "input"
                }
            ],
            "position": {
                "left": 510.2968878020569,
                "top": 31.03124936891274
            },
            "post_job_actions": {},
            "tool_id": "Show tail1",
            "tool_state": "{\"header\": \"false\", \"input\": {\"__class__\": \"RuntimeValue\"}, \"lineNum\": \"1\", \"__page__\": null, \"__rerun_remap_job_id__\": null}",
            "tool_version": "1.0.1",
            "type": "tool",
            "uuid": "c51df68e-6513-423d-9cbd-cc462c403d46",
            "workflow_outputs": [
                {
                    "label": null,
                    "output_name": "out_file1",
                    "uuid": "83d29c39-aa7a-435a-bdf0-072192f58b4d"
                }
            ]
        }
    },
    "tags": [],
    "uuid": "e11bc06e-7b1b-4f9e-96c8-99d71d6ef030",
    "version": 1
}
 No newline at end of file