|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import os |
| 4 | +import time |
| 5 | +import yaml |
| 6 | +import argparse |
| 7 | +import subprocess |
| 8 | +import logging as log |
| 9 | +from bioblend import galaxy |
| 10 | +from subprocess import CalledProcessError |
| 11 | + |
| 12 | + |
| 13 | +def main( data ): |
| 14 | + """ |
| 15 | + Load files into a Galaxy data library. |
| 16 | + By default all test-data tools from all installed tools |
| 17 | + will be linked into a data library. |
| 18 | + """ |
| 19 | + |
| 20 | + log.info("Importing data libraries.") |
| 21 | + |
| 22 | + url = "http://localhost:8080" |
| 23 | + # The environment variables are set by the parent container |
| 24 | + admin_email = os.environ.get('GALAXY_DEFAULT_ADMIN_USER', 'admin@galaxy.org') |
| 25 | + admin_pass = os.environ.get('GALAXY_DEFAULT_ADMIN_PASSWORD', 'admin') |
| 26 | + |
| 27 | + # Establish connection to galaxy instance |
| 28 | + gi = galaxy.GalaxyInstance(url=url, email=admin_email, password=admin_pass) |
| 29 | + |
| 30 | + jc = galaxy.jobs.JobsClient(gi) |
| 31 | + |
| 32 | + folders = dict() |
| 33 | + |
| 34 | + libraries = yaml.load(data) |
| 35 | + for lib in libraries['libraries']: |
| 36 | + folders[lib['name']] = lib['files'] |
| 37 | + |
| 38 | + if folders: |
| 39 | + log.info("Create 'Test Data' library.") |
| 40 | + lib = gi.libraries.create_library('Training Data', 'Data pulled from online archives.') |
| 41 | + lib_id = lib['id'] |
| 42 | + |
| 43 | + for fname, urls in folders.items(): |
| 44 | + log.info("Creating folder: %s" % fname) |
| 45 | + folder = gi.libraries.create_folder( lib_id, fname ) |
| 46 | + for url in urls: |
| 47 | + gi.libraries.upload_file_from_url( |
| 48 | + lib_id, |
| 49 | + url, |
| 50 | + folder_id = folder[0]['id'], |
| 51 | + ) |
| 52 | + |
| 53 | + no_break = True |
| 54 | + while True: |
| 55 | + no_break = False |
| 56 | + for job in jc.get_jobs(): |
| 57 | + if job['state'] != 'ok': |
| 58 | + no_break = True |
| 59 | + if not no_break: |
| 60 | + break |
| 61 | + time.sleep(3) |
| 62 | + |
| 63 | + |
| 64 | + time.sleep(20) |
| 65 | + log.info("Finished importing test data.") |
| 66 | + |
| 67 | + |
| 68 | +if __name__ == '__main__': |
| 69 | + parser = argparse.ArgumentParser( |
| 70 | + description='Populate the Galaxy data library with test data.' |
| 71 | + ) |
| 72 | + parser.add_argument("-v", "--verbose", help="Increase output verbosity.", |
| 73 | + action="store_true") |
| 74 | + parser.add_argument('-i', '--infile', type=argparse.FileType('r')) |
| 75 | + |
| 76 | + #TODO: Add options to override the admin_user and admin_password + specify |
| 77 | + # files to upload via command line interface. |
| 78 | + |
| 79 | + args = parser.parse_args() |
| 80 | + if args.verbose: |
| 81 | + log.basicConfig(level=log.DEBUG) |
| 82 | + |
| 83 | + main( args.infile ) |
| 84 | + |
0 commit comments