annotate json_collect_data_source.py @ 35:5c10dbaa9cc5 draft

Deleted selected files
author fabio
date Tue, 13 Jun 2017 16:39:45 -0400
parents d65de900967e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
32
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
1 #!/usr/bin/env python
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
2 import json
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
3 import optparse
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
4 import urllib
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
5 import os.path
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
6 import os
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
7 from operator import itemgetter
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
8 import tarfile
34
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
9 import zipfile
32
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
10
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
11 __version__ = "1.0.0"
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
12 CHUNK_SIZE = 2**20 #1mb
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
13 VALID_CHARS = '.-()[]0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ '
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
14
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
15
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
16 def splitext(path):
34
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
17 # extract the folder path and extension of a file from its path
32
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
18 for ext in ['.tar.gz', '.tar.bz2']:
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
19 if path.endswith(ext):
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
20 path, ext = path[:-len(ext)], path[-len(ext):]
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
21 break
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
22 else:
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
23 path, ext = os.path.splitext(path)
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
24 return path, ext[1:]
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
25
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
26
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
27 def chunk_write( source_stream, target_stream, source_method = "read", target_method="write" ):
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
28 source_method = getattr( source_stream, source_method )
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
29 target_method = getattr( target_stream, target_method )
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
30 while True:
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
31 chunk = source_method( CHUNK_SIZE )
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
32 if chunk:
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
33 target_method( chunk )
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
34 else:
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
35 break
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
36
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
37
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
38 def deconstruct_multi_filename( multi_filename ):
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
39 keys = [ 'primary', 'id', 'name', 'visible', 'file_type' ]
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
40 return ( dict( zip( keys, multi_filename.split('_') ) ) )
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
41
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
42
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
43 def construct_multi_filename( id, name, file_type ):
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
44 """ Implementation of *Number of Output datasets cannot be determined until tool run* from documentation_.
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
45 .. _documentation: http://wiki.galaxyproject.org/Admin/Tools/Multiple%20Output%20Files
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
46 """
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
47 filename = "%s_%s_%s_%s_%s" % ( 'primary', id, name, 'visible', file_type )
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
48 return filename
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
49
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
50
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
51 def download_from_query( query_data, target_output_filename ):
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
52 """ Download file from the json data and write it to target_output_filename.
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
53 """
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
54 query_url = query_data.get( 'url' )
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
55 query_file_type = query_data.get( 'extension' )
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
56 query_stream = urllib.urlopen( query_url )
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
57 output_stream = open( target_output_filename, 'wb' )
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
58 chunk_write( query_stream, output_stream )
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
59 query_stream.close()
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
60 output_stream.close()
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
61
34
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
62 def store_file_from_tarfile( file_object, target_output_filename, isString=False ):
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
63 # store the file_object (from tarfile) on the filesystem
32
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
64 output_stream = open( target_output_filename, 'wb' )
34
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
65 output_stream.write(file_object.read())
32
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
66 output_stream.close()
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
67
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
68
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
69 def download_extra_data( query_ext_data, base_path ):
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
70 """ Download any extra data defined in the JSON.
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
71 NOTE: the "path" value is a relative path to the file on our
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
72 file system. This is slightly dangerous and we should make every effort
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
73 to avoid a malicious absolute path to write the file elsewhere on the
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
74 filesystem.
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
75 """
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
76 for ext_data in query_ext_data:
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
77 if not os.path.exists( base_path ):
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
78 os.mkdir( base_path )
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
79 query_stream = urllib.urlopen( ext_data.get( 'url' ) )
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
80 ext_path = ext_data.get( 'path' )
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
81 os.makedirs( os.path.normpath( '/'.join( [ base_path, os.path.dirname( ext_path ) ] ) ) )
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
82 output_stream = open( os.path.normpath( '/'.join( [ base_path, ext_path ] ) ), 'wb' )
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
83 chunk_write( query_stream, output_stream )
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
84 query_stream.close()
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
85 output_stream.close()
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
86
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
87
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
88 def metadata_to_json( dataset_id, metadata, filename, ds_type='dataset', primary=False):
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
89 """ Return line separated JSON """
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
90 meta_dict = dict( type = ds_type,
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
91 ext = metadata.get( 'extension' ),
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
92 filename = filename,
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
93 name = metadata.get( 'name' ),
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
94 metadata = metadata.get( 'metadata', {} ) )
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
95 if metadata.get( 'extra_data', None ):
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
96 meta_dict[ 'extra_files' ] = '_'.join( [ filename, 'files' ] )
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
97 if primary:
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
98 meta_dict[ 'base_dataset_id' ] = dataset_id
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
99 else:
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
100 meta_dict[ 'dataset_id' ] = dataset_id
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
101 return "%s\n" % json.dumps( meta_dict )
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
102
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
103
34
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
104 def walk_on_archive(target_output_filename, check_ext, archive_library, archive_name, appdata_path, db_key="?"):
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
105 # fix archive name using valid chars only
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
106 archive_name = ''.join(e for e in archive_name if e in VALID_CHARS)
32
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
107 archive_name = archive_name.replace("_", "-").replace(".", "-")
34
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
108 if archive_library is "zipfile":
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
109 # iterate over entries inside the archive [zip]
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
110 with zipfile.ZipFile( target_output_filename, check_ext ) as zf:
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
111 for entry in zf.namelist():
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
112 # if entry is file
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
113 if entry.startswith("%s/" % entry.rstrip("/")) is False:
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
114 # retrieve file name
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
115 # the underscore character is reserved
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
116 filename = os.path.basename( entry.split("/")[-1] ).replace("_", "-")
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
117 # retrieve file extension
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
118 extension = splitext( filename )[1]
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
119 # if no extension use 'auto'
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
120 if (len(extension) == 0):
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
121 extension = "auto"
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
122 # pattern: (?P<identifier_0>[^_]+)_(?P<identifier_1>[^_]+)_(?P<ext>[^_]+)_(?P<dbkey>[^_]+)
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
123 filename_with_collection_prefix = archive_name + "_" + filename + "_" + extension + "_" + db_key
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
124 # store current entry on filesystem
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
125 zf.extract( filename_with_collection_prefix, appdata_path )
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
126 elif archive_library is "tarfile":
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
127 # iterate over entries inside the archive [gz, bz2, tar]
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
128 with tarfile.open( target_output_filename, check_ext ) as tf:
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
129 for entry in tf:
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
130 if entry.isfile():
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
131 fileobj = tf.extractfile( entry )
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
132 # retrieve file name
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
133 # the underscore character is reserved
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
134 filename = os.path.basename( (entry.name).split("/")[-1] ).replace("_", "-")
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
135 # retrieve file extension
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
136 extension = splitext( filename )[1]
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
137 # if no extension use 'auto'
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
138 if (len(extension) == 0):
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
139 extension = "auto"
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
140 # pattern: (?P<identifier_0>[^_]+)_(?P<identifier_1>[^_]+)_(?P<ext>[^_]+)_(?P<dbkey>[^_]+)
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
141 filename_with_collection_prefix = archive_name + "_" + filename + "_" + extension + "_" + db_key
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
142 target_entry_output_filename = os.path.join(appdata_path, filename_with_collection_prefix)
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
143 # store current entry on filesystem
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
144 store_file_from_tarfile( fileobj, target_entry_output_filename )
32
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
145 return True
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
146
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
147
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
148 def download_files_and_write_metadata(query_item, json_params, output_base_path, metadata_parameter_file, primary, appdata_path, options, args):
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
149 """ Main work function that operates on the JSON representation of
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
150 one dataset and its metadata. Returns True.
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
151 """
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
152 dataset_url, output_filename, \
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
153 extra_files_path, file_name, \
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
154 ext, out_data_name, \
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
155 hda_id, dataset_id = set_up_config_values(json_params)
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
156 extension = query_item.get( 'extension' )
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
157 url = query_item.get( 'url' )
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
158 filename = query_item.get( 'name' )
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
159
34
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
160 # the organize parameter is considered for archives only
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
161 organize = query_item.get( 'organize', None )
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
162 if organize is None:
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
163 organize = False
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
164 else:
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
165 if organize.lower() == "true":
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
166 organize = True
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
167 elif organize.lower() == "false":
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
168 organize = False
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
169 else:
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
170 # if organize parameter is malformed -> set organize to False
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
171 organize = False
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
172
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
173 # check file extension
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
174 # if the file is an archive -> do not write metadata and extract files
32
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
175 check_ext = ""
34
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
176 archive_library = None
32
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
177 if ( url.endswith( "gz" ) ):
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
178 check_ext = "r:gz"
34
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
179 archive_library = "tarfile"
32
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
180 elif ( url.endswith( "bz2" ) ):
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
181 check_ext = "r:bz2"
34
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
182 archive_library = "tarfile"
32
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
183 elif ( url.endswith( "tar" ) ):
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
184 check_ext = "r:"
34
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
185 archive_library = "tarfile"
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
186 elif ( url.endswith( "zip" ) ):
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
187 check_ext = "r"
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
188 archive_library = "zipfile"
32
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
189 isArchive = bool( check_ext and check_ext.strip() )
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
190
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
191 extra_data = query_item.get( 'extra_data', None )
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
192 if primary:
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
193 filename = ''.join( c in VALID_CHARS and c or '-' for c in filename )
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
194 name = construct_multi_filename( hda_id, filename, extension )
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
195 target_output_filename = os.path.normpath( '/'.join( [ output_base_path, name ] ) )
34
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
196 if (isArchive is False) or ((isArchive is True) and (organize is False)):
32
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
197 metadata_parameter_file.write( metadata_to_json( dataset_id, query_item,
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
198 target_output_filename,
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
199 ds_type='new_primary_dataset',
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
200 primary=primary) )
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
201 else:
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
202 target_output_filename = output_filename
34
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
203 if (isArchive is False) or ((isArchive is True) and (organize is False)):
32
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
204 metadata_parameter_file.write( metadata_to_json( dataset_id, query_item,
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
205 target_output_filename,
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
206 ds_type='dataset',
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
207 primary=primary) )
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
208
34
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
209 if (isArchive is False) or ((isArchive is True) and (organize is False)):
32
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
210 download_from_query( query_item, target_output_filename )
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
211 else:
34
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
212 # if the current entry is an archive download it inside appdata folder
32
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
213 target_output_path = os.path.join(appdata_path, filename)
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
214 download_from_query( query_item, target_output_path )
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
215 if extra_data:
34
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
216 # just download extra data
32
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
217 extra_files_path = ''.join( [ target_output_filename, 'files' ] )
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
218 download_extra_data( extra_data, extra_files_path )
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
219
34
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
220 # if the current file is an archive and want to organize the content
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
221 # -> decompress the archive and populate the collection (has to be defined in the tool xml schema)
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
222 if isArchive and organize:
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
223 # set the same db_key for each file inside the archive
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
224 # use the db_key associated to the archive (if it exists)
32
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
225 db_key = "?"
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
226 archive_metadata = query_item.get( 'metadata', None )
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
227 if archive_metadata is not None:
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
228 try:
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
229 db_key = archive_metadata.get( 'db_key' )
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
230 except:
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
231 pass
34
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
232 archive_name = query_item.get( 'name', None )
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
233 if archive_name is None:
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
234 archive_name = filename
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
235 # iterate over the archive content
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
236 walk_on_archive(target_output_path, check_ext, archive_library, archive_name, appdata_path, db_key)
32
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
237
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
238 return True
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
239
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
240
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
241 def set_up_config_values(json_params):
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
242 """ Parse json_params file and return a tuple of necessary configuration
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
243 values.
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
244 """
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
245 datasource_params = json_params.get( 'param_dict' )
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
246 dataset_url = datasource_params.get( 'URL' )
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
247 output_filename = datasource_params.get( 'output1', None )
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
248 output_data = json_params.get( 'output_data' )
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
249 extra_files_path, file_name, ext, out_data_name, hda_id, dataset_id = \
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
250 itemgetter('extra_files_path', 'file_name', 'ext', 'out_data_name', 'hda_id', 'dataset_id')(output_data[0])
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
251 return (dataset_url, output_filename,
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
252 extra_files_path, file_name,
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
253 ext, out_data_name,
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
254 hda_id, dataset_id)
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
255
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
256
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
257 def download_from_json_data( options, args ):
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
258 """ Parse the returned JSON data and download files. Write metadata
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
259 to flat JSON file.
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
260 """
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
261 output_base_path = options.path
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
262 appdata_path = options.appdata
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
263 if not os.path.exists(appdata_path):
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
264 os.makedirs(appdata_path)
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
265
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
266 # read tool job configuration file and parse parameters we need
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
267 json_params = json.loads( open( options.json_param_file, 'r' ).read() )
34
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
268 #print("json_params: "+str(json_params))
32
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
269
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
270 dataset_url, output_filename, \
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
271 extra_files_path, file_name, \
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
272 ext, out_data_name, \
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
273 hda_id, dataset_id = set_up_config_values(json_params)
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
274 # line separated JSON file to contain all dataset metadata
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
275 metadata_parameter_file = open( json_params['job_config']['TOOL_PROVIDED_JOB_METADATA_FILE'], 'wb' )
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
276
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
277 # get JSON response from data source
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
278 # TODO: make sure response is not enormous
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
279 query_params = json.loads(urllib.urlopen( dataset_url ).read())
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
280 # download and write files
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
281 primary = False
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
282 #primary = True
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
283 # query_item, hda_id, output_base_path, dataset_id
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
284 for query_item in query_params:
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
285 if isinstance( query_item, list ):
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
286 # TODO: do something with the nested list as a collection
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
287 for query_subitem in query_item:
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
288 primary = download_files_and_write_metadata(query_subitem, json_params, output_base_path,
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
289 metadata_parameter_file, primary, appdata_path, options, args)
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
290
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
291 elif isinstance( query_item, dict ):
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
292 primary = download_files_and_write_metadata(query_item, json_params, output_base_path,
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
293 metadata_parameter_file, primary, appdata_path, options, args)
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
294 metadata_parameter_file.close()
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
295
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
296 def __main__():
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
297 """ Read the JSON return from a data source. Parse each line and request
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
298 the data, download to "newfilepath", and write metadata.
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
299
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
300 Schema
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
301 ------
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
302
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
303 [ {"url":"http://url_of_file",
34
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
304 "name":"My Archive",
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
305 "extension":"tar.gz",
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
306 "organize":"true",
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
307 "metadata":{"db_key":"hg38"},
32
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
308 "extra_data":[ {"url":"http://url_of_ext_file",
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
309 "path":"rel/path/to/ext_file"}
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
310 ]
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
311 }
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
312 ]
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
313
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
314 """
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
315 # Parse the command line options
34
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
316 usage = "Usage: json_collect_data_source.py max_size --json_param_file filename [options]"
32
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
317 parser = optparse.OptionParser(usage = usage)
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
318 parser.add_option("-j", "--json_param_file", type="string",
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
319 action="store", dest="json_param_file", help="json schema return data")
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
320 parser.add_option("-p", "--path", type="string",
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
321 action="store", dest="path", help="new file path")
34
d65de900967e Uploaded 20170613
fabio
parents: 32
diff changeset
322 # set appdata: temporary directory in which the archives will be decompressed
32
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
323 parser.add_option("-a", "--appdata", type="string",
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
324 action="store", dest="appdata", help="appdata folder name")
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
325 parser.add_option("-v", "--version", action="store_true", dest="version",
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
326 default=False, help="display version and exit")
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
327
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
328 (options, args) = parser.parse_args()
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
329 if options.version:
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
330 print __version__
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
331 else:
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
332 download_from_json_data( options, args )
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
333
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
334
1edc869cd008 Uploaded 20170607
fabio
parents:
diff changeset
335 if __name__ == "__main__": __main__()