comparison data_manager/fetch_mlst_data.py @ 4:577ff220eaea draft default tip

Uploaded
author ulfschaefer
date Mon, 11 Jul 2016 06:15:30 -0400
parents c01ac945b067
children
comparison
equal deleted inserted replaced
3:eb9321fec577 4:577ff220eaea
34 import csv 34 import csv
35 import shutil 35 import shutil
36 from urlparse import urlparse 36 from urlparse import urlparse
37 import time 37 import time
38 import subprocess 38 import subprocess
39 39 from json import dumps
40 try: 40 from json import loads
41 from galaxy.util.json import from_json_string, to_json_string
42 has_galaxy = True
43 except ImportError:
44 sys.stderr.write("Will download some stuff to the current dir but can't do Galaxy stuff!")
45 has_galaxy = False
46 41
47 # -------------------------------------------------------------------------------------------------- 42 # --------------------------------------------------------------------------------------------------
48 43
49 def parse_args(): 44 def parse_args():
50 parser = ArgumentParser(description='Download MLST datasets by species' 45 parser = ArgumentParser(description='Download MLST datasets by species'
137 assert len(found_species) == 1 132 assert len(found_species) == 1
138 species_info = found_species[0] 133 species_info = found_species[0]
139 species_name_underscores = species_info.name.replace(' ', '_') 134 species_name_underscores = species_info.name.replace(' ', '_')
140 timestamp = time.strftime("%Y%m%d%H%M%S") 135 timestamp = time.strftime("%Y%m%d%H%M%S")
141 136
142 params = None 137 params = loads(open(args.outfile).read())
143 if has_galaxy == True: 138 folder = os.path.join(params['output_data'][0]['extra_files_path'], species_name_underscores, timestamp)
144 params = from_json_string(open(args.outfile).read())
145 folder = os.path.join(params['output_data'][0]['extra_files_path'], species_name_underscores, timestamp)
146 else:
147 folder = os.path.join(os.path.dirname(os.path.realpath(__file__)), species_name_underscores, timestamp)
148 139
149 if not os.path.isdir(folder): 140 if not os.path.isdir(folder):
150 os.makedirs(folder) 141 os.makedirs(folder)
151 142
152 profile_doc = url.urlopen(species_info.profiles_url) 143 profile_doc = url.urlopen(species_info.profiles_url)
172 163
173 get_reference(folder, args.reference) 164 get_reference(folder, args.reference)
174 165
175 166
176 # do Galaxy stuff 167 # do Galaxy stuff
177 if has_galaxy == True: 168 data_manager_dict = {}
178 data_manager_dict = {} 169 data_manager_dict['data_tables'] = {}
179 data_manager_dict['data_tables'] = {} 170 name = "%s-%s" % (species_info.name, timestamp)
180 name = "%s-%s" % (species_info.name, timestamp) 171 data_manager_dict['data_tables']['mlst_data'] = [dict(value=species_name_underscores,
181 data_manager_dict['data_tables']['mlst_data'] = [dict(value=species_name_underscores, 172 dbkey=species_name_underscores,
182 dbkey=species_name_underscores, 173 name=name,
183 name=name, 174 time_stamp=timestamp,
184 time_stamp=timestamp, 175 file_path=folder)]
185 file_path=folder)] 176 #save info to json file
186 #save info to json file 177 with open(args.outfile, 'wb') as fjson:
187 with open(args.outfile, 'wb') as fjson: 178 fjson.write(dumps(data_manager_dict))
188 fjson.write(to_json_string(data_manager_dict))
189 179
190 # end of main -------------------------------------------------------------------------------------- 180 # end of main --------------------------------------------------------------------------------------
191 181
192 def get_reference(folder, acc): 182 def get_reference(folder, acc):
193 183