comparison data_manager/data_manager_ceas_fetch_annotations.py @ 4:4e2883bb058d draft

Synchronise version with one on main toolshed (version of 30/06/2015)
author pjbriggs
date Wed, 10 Aug 2016 11:04:42 -0400
parents d9032bb158b9
children e853be203962
comparison
equal deleted inserted replaced
3:82df5af2995f 4:4e2883bb058d
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # 2 #
3 3
4 import sys 4 import sys
5 import os 5 import os
6 import subprocess
7 import tempfile
6 import optparse 8 import optparse
7 import urllib2 9 import urllib2
8 import gzip 10 import gzip
11 import shutil
9 12
10 from galaxy.util.json import from_json_string, to_json_string 13 from galaxy.util.json import from_json_string, to_json_string
11 14
12 # Download file from specified URL and put into local subdir 15 # Download file from specified URL and put into local subdir
13 16
14 if __name__ == '__main__': 17 if __name__ == '__main__':
15 #Parse Command Line 18 #Parse Command Line
16 parser = optparse.OptionParser() 19 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 options,args = parser.parse_args()
20 print "options: %s" % options 21 print "options: %s" % options
21 print "args : %s" % args 22 print "args : %s" % args
22 if len(args) != 2: 23 if len(args) != 2:
23 p.error("Need to supply JSON file name and description text") 24 p.error("Need to supply JSON file name and description text")
33 # See https://wiki.galaxyproject.org/Admin/Tools/DataManagers/HowTo/Define?highlight=%28\bAdmin%2FTools%2FDataManagers\b%29 34 # See https://wiki.galaxyproject.org/Admin/Tools/DataManagers/HowTo/Define?highlight=%28\bAdmin%2FTools%2FDataManagers\b%29
34 # for example of JSON 35 # for example of JSON
35 # 36 #
36 # We want the values set in the data manager XML 37 # We want the values set in the data manager XML
37 dbkey = params['param_dict']['dbkey'] 38 dbkey = params['param_dict']['dbkey']
38 description = params['param_dict']['description'] 39 description = args[1].strip()
40 identifier = params['param_dict']['unique_id'].strip()
39 # Where to put the output file 41 # Where to put the output file
40 # Nb we have to make this ourselves, it doesn't exist by default 42 # Nb we have to make this ourselves, it doesn't exist by default
41 target_dir = params['output_data'][0]['extra_files_path'] 43 target_dir = params['output_data'][0]['extra_files_path']
42 os.mkdir(target_dir) 44 os.mkdir(target_dir)
43 45
46 method = params['param_dict']['reference_source']['reference_source_selector']
47
44 # Dictionary for returning to data manager 48 # Dictionary for returning to data manager
45 data_manager_dict = {} 49 data_manager_dict = {}
50 data_manager_dict['data_tables'] = dict()
46 51
47 # Download from URL 52 # Download from URL
48 if options.url is not None: 53 if method == 'web':
49 print "Downloading: %s" % options.url 54 url = params['param_dict']['reference_source']['annotation_url']
50 annotation_file_name = os.path.basename(options.url) 55 print "Downloading: %s" % url
56 annotation_file_name = os.path.basename(url)
51 annotation_file_path = os.path.join(target_dir,annotation_file_name) 57 annotation_file_path = os.path.join(target_dir,annotation_file_name)
52 print "Annotation file name: %s" % annotation_file_name 58 print "Annotation file name: %s" % annotation_file_name
53 print "Annotation file path: %s" % annotation_file_path 59 print "Annotation file path: %s" % annotation_file_path
54 open(annotation_file_path,'wb').write(urllib2.urlopen(options.url).read()) 60 open(annotation_file_path,'wb').write(urllib2.urlopen(url).read())
55 if annotation_file_name.endswith('.gz'): 61 if annotation_file_name.endswith('.gz'):
56 # Uncompress 62 # Uncompress
57 uncompressed_file = annotation_file_path[:-3] 63 uncompressed_file = annotation_file_path[:-3]
58 open(uncompressed_file,'wb').write(gzip.open(annotation_file_path,'rb').read()) 64 open(uncompressed_file,'wb').write(gzip.open(annotation_file_path,'rb').read())
59 # Remove gzipped file 65 # Remove gzipped file
60 os.remove(annotation_file_path) 66 os.remove(annotation_file_path)
61 annotation_file_name = os.path.basename(uncompressed_file) 67 annotation_file_name = os.path.basename(uncompressed_file)
62 annotation_file_path = uncompressed_file 68 annotation_file_path = uncompressed_file
69 # Update the identifier and description
70 if not identifier:
71 identifier = "%s_ceas_web" % dbkey
72 if not description:
73 description = "%s (%s)" % (os.path.splitext(annotation_file_name)[0],dbkey)
63 # Update the output dictionary 74 # Update the output dictionary
64 data_manager_dict['data_tables'] = dict()
65 data_manager_dict['data_tables']['ceas_annotations'] = { 75 data_manager_dict['data_tables']['ceas_annotations'] = {
76 'value': identifier,
66 'dbkey': dbkey, 77 'dbkey': dbkey,
67 'name': description, 78 'name': description,
68 'value': annotation_file_name, 79 'path': annotation_file_name,
80 }
81 elif method == 'server':
82 # Pull in a file from the server
83 filename = params['param_dict']['reference_source']['annotation_filename']
84 create_symlink = params['param_dict']['reference_source']['create_symlink']
85 print "Canonical gene list file name: %s" % filename
86 print "Create symlink: %s" % create_symlink
87 target_filename = os.path.join(target_dir,os.path.basename(filename))
88 if create_symlink == 'copy_file':
89 shutil.copyfile(filename,target_filename)
90 else:
91 os.symlink(filename,target_filename)
92 # Update the identifier and description
93 if not identifier:
94 identifier = "%s_%s" % (dbkey,
95 os.path.splitext(os.path.basename(filename))[0])
96 if not description:
97 description = "%s: %s" % (dbkey,
98 os.path.splitext(os.path.basename(filename))[0])
99 # Update the output dictionary
100 data_manager_dict['data_tables']['ceas_annotations'] = {
101 'value': identifier,
102 'dbkey': dbkey,
103 'name': description,
104 'path': os.path.basename(filename),
105 }
106 elif method == 'from_wig':
107 # Make a reference file from a wig file
108 wig_file = params['param_dict']['reference_source']['wig_file']
109 gene_annotation = params['param_dict']['reference_source']['gene_annotation']
110 target_filename = os.path.join(target_dir,"%s_%s.%s" % (dbkey,
111 os.path.basename(wig_file),
112 gene_annotation))
113 print "Wig file: %s" % wig_file
114 print "Gene annotation: %s" % gene_annotation
115 print "Output file: %s" % os.path.basename(target_filename)
116 # Make a working directory
117 working_dir = tempfile.mkdtemp()
118 # Collect stderr in a file for reporting later
119 stderr_filen = tempfile.NamedTemporaryFile().name
120 # Build the command to run
121 cmd = "build_genomeBG -d %s -g %s -w %s -o %s" % (dbkey,
122 gene_annotation,
123 wig_file,
124 target_filename)
125 print "Running %s" % cmd
126 proc = subprocess.Popen(args=cmd,shell=True,cwd=working_dir,
127 stderr=open(stderr_filen,'wb'))
128 proc.wait()
129 # Copy stderr to stdout
130 with open(stderr_filen,'r') as fp:
131 sys.stdout.write(fp.read())
132 # Update identifier and description
133 if not identifier:
134 identifier = "%s_%s_%s" % (dbkey,
135 gene_annotation,
136 os.path.basename(wig_file))
137 if not description:
138 description = "%s %s from %s" % (dbkey,
139 gene_annotation,
140 os.path.basename(wig_file))
141 # Update the output dictionary
142 data_manager_dict['data_tables']['ceas_annotations'] = {
143 'value': identifier,
144 'dbkey': dbkey,
145 'name': description,
146 'path': os.path.basename(target_filename),
69 } 147 }
70 else: 148 else:
71 raise NotImplementedError("Non-download options not implemented") 149 raise NotImplementedError("Method '%s' not implemented" % method)
72 150
73 #save info to json file 151 #save info to json file
74 open(jsonfile,'wb').write(to_json_string(data_manager_dict)) 152 open(jsonfile,'wb').write(to_json_string(data_manager_dict))
75 153