Commit 81c993d6 authored by Yakubov, Sergey's avatar Yakubov, Sergey
Browse files

update example to deal with multiple files

parent e492379b
Loading
Loading
Loading
Loading
+23 −13
Original line number Diff line number Diff line
@@ -3,11 +3,21 @@ import os,sys
from bioblend import galaxy
from bioblend.galaxy.tools.inputs import inputs


def wait_for_dataset(result_dataset_id, galaxy_instance, dataset_client):
    wait_res = dataset_client.wait_for_dataset(result_dataset_id, check=False)
    if wait_res["state"] == "ok":
        print("register successful")
        print("dataset is now in history under id: ", wait_res["hid"])
    else:
        details = galaxy_instance.histories.show_dataset_provenance(history_id, result_dataset_id)
        raise Exception("register failed", details)

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


file_to_register = sys.argv[1]
files_to_register = sys.argv[1:]

# connect to a Galaxy instance, create dataset client
galaxy_instance = galaxy.GalaxyInstance(url=galaxy_url, key=galaxy_user_api_key)
@@ -19,18 +29,18 @@ history_id = galaxy_instance.histories.get_histories()[0]["id"]
# history_id = galaxy_instance.histories.create_history(name="New history")["id"]

# set tool inputs, run tool and wait it finishes
# The neutrons_register tool takes a filepath
# and returns a single output file (hence ['outputs'][0]['id'])
tool_inputs = inputs().set("input", file_to_register)
result_dataset_id = galaxy_instance.tools.run_tool(history_id, 'neutrons_register', tool_inputs)['outputs'][0]['id']
res = dataset_client.wait_for_dataset(result_dataset_id, check=False)
# The neutrons_register tool takes a list of filepaths
# and returns multiple output files

n_files = len(files_to_register)
tool_inputs = inputs()
for i in range(n_files):
    tool_inputs = tool_inputs.set_param(f"series_{i}|input", files_to_register[i])

meta = galaxy_instance.tools.run_tool(history_id, 'neutrons_register', tool_inputs)
for output in meta['outputs']:
    wait_for_dataset(output['id'], galaxy_instance, dataset_client)

if res["state"] == "ok":
    print("register successful")
    print("dataset is now in history under id: ", res["hid"])
else:
    details = galaxy_instance.histories.show_dataset_provenance(history_id, result_dataset_id)
    raise Exception("register failed", details)

# 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)