| 1 | 1 #!/usr/bin/env python | 
|  | 2 # | 
|  | 3 | 
|  | 4 import sys | 
|  | 5 import os | 
|  | 6 import optparse | 
|  | 7 import urllib2 | 
|  | 8 import gzip | 
|  | 9 | 
|  | 10 from galaxy.util.json import from_json_string, to_json_string | 
|  | 11 | 
|  | 12 # Download file from specified URL and put into local subdir | 
|  | 13 | 
|  | 14 if __name__ == '__main__': | 
|  | 15     #Parse Command Line | 
|  | 16     parser = optparse.OptionParser() | 
|  | 17     parser.add_option('--download',dest='url',action='store', | 
|  | 18                       type="string",default=None,help='URL to download') | 
|  | 19     options,args = parser.parse_args() | 
|  | 20     print "options: %s" % options | 
|  | 21     print "args   : %s" % args | 
|  | 22     if len(args) != 2: | 
|  | 23         p.error("Need to supply JSON file name and description text") | 
|  | 24 | 
|  | 25     # Read the JSON supplied from the data manager tool | 
|  | 26     # Results from this program will be returned via the | 
|  | 27     # same file | 
|  | 28     jsonfile = args[0] | 
|  | 29     params = from_json_string(open(jsonfile).read() ) | 
|  | 30     print "%s" % params | 
|  | 31 | 
|  | 32     # Extract the data from the input JSON | 
|  | 33     # See https://wiki.galaxyproject.org/Admin/Tools/DataManagers/HowTo/Define?highlight=%28\bAdmin%2FTools%2FDataManagers\b%29 | 
|  | 34     # for example of JSON | 
|  | 35     # | 
|  | 36     # We want the values set in the data manager XML | 
|  | 37     dbkey = params['param_dict']['dbkey'] | 
|  | 38     description = params['param_dict']['description'] | 
|  | 39     # Where to put the output file | 
|  | 40     # Nb we have to make this ourselves, it doesn't exist by default | 
|  | 41     target_dir = params['output_data'][0]['extra_files_path'] | 
|  | 42     os.mkdir(target_dir) | 
|  | 43 | 
|  | 44     # Dictionary for returning to data manager | 
|  | 45     data_manager_dict = {} | 
|  | 46 | 
|  | 47     # Download from URL | 
|  | 48     if options.url is not None: | 
|  | 49         print "Downloading: %s" % options.url | 
|  | 50         annotation_file_name = os.path.basename(options.url) | 
|  | 51         annotation_file_path = os.path.join(target_dir,annotation_file_name) | 
|  | 52         print "Annotation file name: %s" % annotation_file_name | 
|  | 53         print "Annotation file path: %s" % annotation_file_path | 
|  | 54         open(annotation_file_path,'wb').write(urllib2.urlopen(options.url).read()) | 
|  | 55         if annotation_file_name.endswith('.gz'): | 
|  | 56             # Uncompress | 
|  | 57             uncompressed_file = annotation_file_path[:-3] | 
|  | 58             open(uncompressed_file,'wb').write(gzip.open(annotation_file_path,'rb').read()) | 
|  | 59             # Remove gzipped file | 
|  | 60             os.remove(annotation_file_path) | 
|  | 61             annotation_file_name = os.path.basename(uncompressed_file) | 
|  | 62             annotation_file_path = uncompressed_file | 
|  | 63         # Update the output dictionary | 
|  | 64         data_manager_dict['data_tables'] = dict() | 
|  | 65         data_manager_dict['data_tables']['ceas_annotations'] = { | 
|  | 66             'dbkey': dbkey, | 
|  | 67             'name': description, | 
|  | 68             'value': annotation_file_name, | 
|  | 69         } | 
|  | 70     else: | 
|  | 71         raise NotImplementedError("Non-download options not implemented") | 
|  | 72 | 
|  | 73     #save info to json file | 
|  | 74     open(jsonfile,'wb').write(to_json_string(data_manager_dict)) | 
|  | 75 |