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