annotate data_manager/data_manager_ceas_fetch_annotations.py @ 0:f411ce97a351 draft

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