2014-02-10 11:59:58 -05:00
#!/usr/bin/env python
2016-02-17 20:27:09 +00:00
# Dan Blankenberg
# Very simple example of using the API to run Data Managers
# Script makes the naive assumption that dbkey==sequence id, which in many cases is not true nor desired
# *** This script is not recommended for use as-is on a production server ***
2017-09-25 19:42:33 +01:00
2014-02-10 11:59:58 -05:00
import optparse
import time
2020-10-06 18:49:19 +01:00
from urllib.parse import urljoin
2014-02-10 11:59:58 -05:00
2025-03-15 16:43:25 +00:00
from common import (
2017-12-01 11:09:57 +00:00
get ,
post ,
2022-02-02 15:26:21 -05:00
)
2014-02-10 11:59:58 -05:00
DEFAULT_SLEEP_TIME = 3
FETCH_GENOME_TOOL_ID = "testtoolshed.g2.bx.psu.edu/repos/blankenberg/data_manager_fetch_genome_all_fasta/data_manager_fetch_genome_all_fasta/0.0.1"
2017-08-17 00:33:02 +01:00
BUILD_INDEX_TOOLS_ID = [
"testtoolshed.g2.bx.psu.edu/repos/blankenberg/data_manager_bwa_index_builder/bwa_index_builder_data_manager/0.0.1" ,
"testtoolshed.g2.bx.psu.edu/repos/blankenberg/data_manager_bwa_index_builder/bwa_color_space_index_builder_data_manager/0.0.1" ,
]
2014-02-10 11:59:58 -05:00
2016-02-17 20:27:09 +00:00
2017-08-17 00:33:02 +01:00
def run_tool ( tool_id , history_id , params , api_key , galaxy_url , wait = True , sleep_time = None , ** kwargs ):
2014-02-10 11:59:58 -05:00
sleep_time = sleep_time or DEFAULT_SLEEP_TIME
2017-08-17 00:33:02 +01:00
tools_url = urljoin ( galaxy_url , "api/tools" )
2014-02-10 11:59:58 -05:00
payload = {
2016-05-17 23:52:13 +01:00
"tool_id" : tool_id ,
2014-02-10 11:59:58 -05:00
}
if history_id :
payload [ "history_id" ] = history_id
2017-08-17 00:33:02 +01:00
payload [ "inputs" ] = params
rval = post ( api_key , tools_url , payload )
2014-02-10 11:59:58 -05:00
if wait :
2017-08-17 00:33:02 +01:00
outputs = list ( rval [ "outputs" ])
2014-02-10 11:59:58 -05:00
while outputs :
finished_datasets = []
2017-08-17 00:33:02 +01:00
for i , dataset_dict in enumerate ( outputs ):
if dataset_is_terminal ( dataset_dict [ "id" ], api_key = api_key , galaxy_url = galaxy_url ):
finished_datasets . append ( i )
2020-10-06 18:49:19 +01:00
for _ in reversed ( finished_datasets ):
2017-08-17 00:33:02 +01:00
outputs . pop ( 0 )
2014-02-10 11:59:58 -05:00
if wait and outputs :
2017-08-17 00:33:02 +01:00
time . sleep ( sleep_time )
2016-02-17 20:27:09 +00:00
2014-02-10 11:59:58 -05:00
return rval
2016-02-17 20:27:09 +00:00
2017-08-17 00:33:02 +01:00
def get_dataset_state ( hda_id , api_key , galaxy_url ):
2024-04-27 16:03:22 +01:00
datasets_url = urljoin ( galaxy_url , f "api/datasets/ { hda_id } " )
2017-08-17 00:33:02 +01:00
dataset_info = get ( api_key , datasets_url )
2014-02-10 11:59:58 -05:00
return dataset_info [ "state" ]
2016-02-17 20:27:09 +00:00
2017-08-17 00:33:02 +01:00
def dataset_is_terminal ( hda_id , api_key , galaxy_url ):
dataset_state = get_dataset_state ( hda_id , api_key , galaxy_url )
return dataset_state in [ "ok" , "error" ]
2016-02-17 20:27:09 +00:00
2016-11-14 21:12:14 +00:00
2014-02-10 11:59:58 -05:00
if __name__ == "__main__" :
parser = optparse . OptionParser ()
2017-08-17 00:33:02 +01:00
parser . add_option ( "-k" , "--key" , dest = "api_key" , action = "store" , type = "string" , default = None , help = "API Key." )
parser . add_option (
"-u" ,
"--url" ,
dest = "base_url" ,
action = "store" ,
type = "string" ,
default = "http://localhost:8080" ,
help = "Base URL of Galaxy Server" ,
)
parser . add_option (
"-d" ,
"--dbkey" ,
dest = "dbkeys" ,
action = "append" ,
type = "string" ,
default = [],
help = "List of dbkeys to download and Index" ,
)
parser . add_option (
"-s" ,
"--sleep_time" ,
dest = "sleep_time" ,
action = "store" ,
type = "int" ,
default = DEFAULT_SLEEP_TIME ,
help = "How long to sleep between check loops" ,
)
2026-01-19 22:33:34 +00:00
options , args = parser . parse_args ()
2016-02-17 20:27:09 +00:00
# check options
2017-08-17 00:33:02 +01:00
assert options . api_key is not None , ValueError ( "You must specify an API key." )
assert options . dbkeys , ValueError ( "You must specify at least one dbkey to use." )
2016-02-17 20:27:09 +00:00
# check user is admin
2017-08-17 00:33:02 +01:00
configuration_options = get ( options . api_key , urljoin ( options . base_url , "api/configuration" ))
2016-02-17 20:27:09 +00:00
if "library_import_dir" not in configuration_options : # hack to check if is admin user
2016-06-25 19:52:38 -04:00
print (
"Warning: Data Managers are only available to admin users. The API Key provided does not appear to belong to an admin user. Will attempt to run anyway."
)
2016-02-17 20:27:09 +00:00
# Fetch Genomes
2014-02-10 11:59:58 -05:00
dbkeys = {}
for dbkey in options . dbkeys :
if dbkey not in dbkeys :
2017-08-17 00:33:02 +01:00
dbkeys [ dbkey ] = run_tool (
FETCH_GENOME_TOOL_ID ,
None ,
{
"dbkey" : dbkey ,
"reference_source|reference_source_selector" : "ucsc" ,
"reference_source|requested_dbkey" : dbkey ,
},
options . api_key ,
options . base_url ,
wait = False ,
)
2014-02-10 11:59:58 -05:00
else :
2024-04-27 16:03:22 +01:00
f "dbkey ( { dbkey } ) was specified more than once, skipping additional specification."
2016-02-17 20:27:09 +00:00
2016-06-25 19:52:38 -04:00
print ( "Genomes Queued for downloading." )
2016-02-17 20:27:09 +00:00
# Start indexers
2014-02-10 11:59:58 -05:00
indexing_tools = []
while dbkeys :
for dbkey , value in dbkeys . items ():
2017-08-17 00:33:02 +01:00
if dataset_is_terminal ( value [ "outputs" ][ 0 ][ "id" ], options . api_key , options . base_url ):
del dbkeys [ dbkey ]
2014-02-10 11:59:58 -05:00
for tool_id in BUILD_INDEX_TOOLS_ID :
2017-08-17 00:33:02 +01:00
indexing_tools . append (
run_tool (
tool_id , None , { "all_fasta_source" : dbkey }, options . api_key , options . base_url , wait = False
)
2022-02-02 15:26:21 -05:00
)
2014-02-10 11:59:58 -05:00
if dbkeys :
2017-08-17 00:33:02 +01:00
time . sleep ( options . sleep_time )
2016-02-17 20:27:09 +00:00
2016-06-25 19:52:38 -04:00
print ( "All genomes downloaded and indexers now queued." )
2016-02-17 20:27:09 +00:00
# Wait for indexers to finish
2014-02-10 11:59:58 -05:00
while indexing_tools :
2017-08-17 00:33:02 +01:00
for i , indexing_tool_value in enumerate ( indexing_tools ):
if dataset_is_terminal ( indexing_tool_value [ "outputs" ][ 0 ][ "id" ], options . api_key , options . base_url ):
2016-06-25 19:52:38 -04:00
print ( "Finished:" , indexing_tool_value )
2014-02-10 11:59:58 -05:00
del indexing_tools [ i ]
break
if indexing_tools :
2017-08-17 00:33:02 +01:00
time . sleep ( options . sleep_time )
2016-02-17 20:27:09 +00:00
2016-06-25 19:52:38 -04:00
print ( "All indexers have been run, please check results." )