annotate data_manager/data_manager_ceas_fetch_annotations.py @ 4:cd6a48ffd093 draft default tip

Uploaded version 1.0.2-4.
author pjbriggs
date Wed, 26 Sep 2018 08:03:10 -0400
parents df9033b88b53
children
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
1
df9033b88b53 Fix data manager for Galaxy version v16.04 (was failing with import error for 'six' package)
pjbriggs
parents: 0
diff changeset
13 # Convenience functions mapping to JSON conversion
df9033b88b53 Fix data manager for Galaxy version v16.04 (was failing with import error for 'six' package)
pjbriggs
parents: 0
diff changeset
14 # (this idiom borrowed from lib/galaxy/utils/json.py)
df9033b88b53 Fix data manager for Galaxy version v16.04 (was failing with import error for 'six' package)
pjbriggs
parents: 0
diff changeset
15 import json
df9033b88b53 Fix data manager for Galaxy version v16.04 (was failing with import error for 'six' package)
pjbriggs
parents: 0
diff changeset
16 to_json_string = json.dumps
df9033b88b53 Fix data manager for Galaxy version v16.04 (was failing with import error for 'six' package)
pjbriggs
parents: 0
diff changeset
17 from_json_string = json.loads
0
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
18
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
19 # Download file from specified URL and put into local subdir
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
20
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
21 if __name__ == '__main__':
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
22 #Parse Command Line
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
23 parser = optparse.OptionParser()
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
24 options,args = parser.parse_args()
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
25 print "options: %s" % options
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
26 print "args : %s" % args
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
27 if len(args) != 2:
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
28 p.error("Need to supply JSON file name and description text")
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
29
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
30 # Read the JSON supplied from the data manager tool
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
31 # Results from this program will be returned via the
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
32 # same file
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
33 jsonfile = args[0]
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
34 params = from_json_string(open(jsonfile).read() )
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
35 print "%s" % params
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 # Extract the data from the input JSON
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
38 # 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
39 # for example of JSON
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
40 #
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
41 # We want the values set in the data manager XML
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
42 dbkey = params['param_dict']['dbkey']
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
43 description = args[1].strip()
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
44 identifier = params['param_dict']['unique_id'].strip()
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
45 # Where to put the output file
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
46 # 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
47 target_dir = params['output_data'][0]['extra_files_path']
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
48 os.mkdir(target_dir)
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
49
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
50 method = params['param_dict']['reference_source']['reference_source_selector']
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 # Dictionary for returning to data manager
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
53 data_manager_dict = {}
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
54 data_manager_dict['data_tables'] = dict()
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
55
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
56 # Download from URL
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
57 if method == 'web':
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
58 url = params['param_dict']['reference_source']['annotation_url']
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
59 print "Downloading: %s" % url
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
60 annotation_file_name = os.path.basename(url)
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
61 annotation_file_path = os.path.join(target_dir,annotation_file_name)
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
62 print "Annotation file name: %s" % annotation_file_name
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
63 print "Annotation file path: %s" % annotation_file_path
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
64 open(annotation_file_path,'wb').write(urllib2.urlopen(url).read())
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
65 if annotation_file_name.endswith('.gz'):
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
66 # Uncompress
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
67 uncompressed_file = annotation_file_path[:-3]
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
68 open(uncompressed_file,'wb').write(gzip.open(annotation_file_path,'rb').read())
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
69 # Remove gzipped file
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
70 os.remove(annotation_file_path)
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
71 annotation_file_name = os.path.basename(uncompressed_file)
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
72 annotation_file_path = uncompressed_file
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
73 # Update the identifier and description
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
74 if not identifier:
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
75 identifier = "%s_ceas_web" % dbkey
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
76 if not description:
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
77 description = "%s (%s)" % (os.path.splitext(annotation_file_name)[0],dbkey)
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
78 # Update the output dictionary
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
79 data_manager_dict['data_tables']['ceas_annotations'] = {
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
80 'value': identifier,
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
81 'dbkey': dbkey,
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
82 'name': description,
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
83 'path': annotation_file_name,
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
84 }
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
85 elif method == 'server':
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
86 # Pull in a file from the server
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
87 filename = params['param_dict']['reference_source']['annotation_filename']
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
88 create_symlink = params['param_dict']['reference_source']['create_symlink']
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
89 print "Canonical gene list file name: %s" % filename
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
90 print "Create symlink: %s" % create_symlink
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
91 target_filename = os.path.join(target_dir,os.path.basename(filename))
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
92 if create_symlink == 'copy_file':
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
93 shutil.copyfile(filename,target_filename)
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
94 else:
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
95 os.symlink(filename,target_filename)
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
96 # Update the identifier and description
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
97 if not identifier:
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
98 identifier = "%s_%s" % (dbkey,
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
99 os.path.splitext(os.path.basename(filename))[0])
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
100 if not description:
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
101 description = "%s: %s" % (dbkey,
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
102 os.path.splitext(os.path.basename(filename))[0])
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
103 # Update the output dictionary
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
104 data_manager_dict['data_tables']['ceas_annotations'] = {
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
105 'value': identifier,
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
106 'dbkey': dbkey,
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
107 'name': description,
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
108 'path': os.path.basename(filename),
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
109 }
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
110 elif method == 'from_wig':
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
111 # Make a reference file from a wig file
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
112 wig_file = params['param_dict']['reference_source']['wig_file']
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
113 gene_annotation = params['param_dict']['reference_source']['gene_annotation']
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
114 target_filename = os.path.join(target_dir,"%s_%s.%s" % (dbkey,
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
115 os.path.basename(wig_file),
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
116 gene_annotation))
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
117 print "Wig file: %s" % wig_file
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
118 print "Gene annotation: %s" % gene_annotation
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
119 print "Output file: %s" % os.path.basename(target_filename)
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
120 # Make a working directory
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
121 working_dir = tempfile.mkdtemp()
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
122 # Collect stderr in a file for reporting later
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
123 stderr_filen = tempfile.NamedTemporaryFile().name
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
124 # Build the command to run
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
125 cmd = "build_genomeBG -d %s -g %s -w %s -o %s" % (dbkey,
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
126 gene_annotation,
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
127 wig_file,
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
128 target_filename)
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
129 print "Running %s" % cmd
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
130 proc = subprocess.Popen(args=cmd,shell=True,cwd=working_dir,
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
131 stderr=open(stderr_filen,'wb'))
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
132 proc.wait()
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
133 # Copy stderr to stdout
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
134 with open(stderr_filen,'r') as fp:
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
135 sys.stdout.write(fp.read())
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
136 # Update identifier and description
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
137 if not identifier:
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
138 identifier = "%s_%s_%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 if not description:
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
142 description = "%s %s from %s" % (dbkey,
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
143 gene_annotation,
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
144 os.path.basename(wig_file))
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
145 # Update the output dictionary
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
146 data_manager_dict['data_tables']['ceas_annotations'] = {
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
147 'value': identifier,
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
148 'dbkey': dbkey,
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
149 'name': description,
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
150 'path': os.path.basename(target_filename),
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
151 }
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
152 else:
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
153 raise NotImplementedError("Method '%s' not implemented" % method)
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
154
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
155 #save info to json file
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
156 open(jsonfile,'wb').write(to_json_string(data_manager_dict))
f411ce97a351 Uploaded initial version 1.0.2-2
pjbriggs
parents:
diff changeset
157