Mercurial > repos > matt-shirley > json_data_source
annotate json_data_source.py @ 7:ac7b4cab83cd default tip
Fix syntax.
| author | Matt Shirley <mdshw5@gmail.com> |
|---|---|
| date | Wed, 27 Aug 2014 09:34:17 -0400 |
| parents | 46b589e9747a |
| children |
| rev | line source |
|---|---|
| 0 | 1 #!/usr/bin/env python |
| 2 import json | |
| 3 import optparse | |
| 4 import urllib | |
| 5 import os.path | |
|
4
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
6 import os |
|
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
7 from operator import itemgetter |
| 0 | 8 |
| 9 CHUNK_SIZE = 2**20 #1mb | |
|
4
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
10 VALID_CHARS = '.-()[]0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ' |
| 0 | 11 |
| 12 | |
| 13 def chunk_write( source_stream, target_stream, source_method = "read", target_method="write" ): | |
| 14 source_method = getattr( source_stream, source_method ) | |
| 15 target_method = getattr( target_stream, target_method ) | |
| 16 while True: | |
| 17 chunk = source_method( CHUNK_SIZE ) | |
| 18 if chunk: | |
| 19 target_method( chunk ) | |
| 20 else: | |
| 21 break | |
| 22 | |
| 23 | |
| 24 def deconstruct_multi_filename( multi_filename ): | |
| 25 keys = [ 'primary', 'id', 'name', 'visible', 'file_type' ] | |
| 26 return ( dict( zip( keys, multi_filename.split('_') ) ) ) | |
| 27 | |
| 28 | |
| 29 def construct_multi_filename( id, name, file_type ): | |
| 30 """ Implementation of *Number of Output datasets cannot be determined until tool run* from documentation_. | |
| 31 .. _documentation: http://wiki.galaxyproject.org/Admin/Tools/Multiple%20Output%20Files | |
| 32 """ | |
| 33 filename = "%s_%s_%s_%s_%s" % ( 'primary', id, name, 'visible', file_type ) | |
| 34 return filename | |
| 35 | |
| 36 | |
| 37 def download_from_query( query_data, target_output_filename ): | |
| 38 """ Download file from the json data and write it to target_output_filename. | |
| 39 """ | |
| 40 query_url = query_data.get( 'url' ) | |
| 41 query_file_type = query_data.get( 'extension' ) | |
| 42 query_stream = urllib.urlopen( query_url ) | |
| 43 output_stream = open( target_output_filename, 'wb' ) | |
| 44 chunk_write( query_stream, output_stream ) | |
| 45 query_stream.close() | |
| 46 output_stream.close() | |
| 47 | |
| 48 | |
| 49 def download_extra_data( query_ext_data, base_path ): | |
|
4
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
50 """ Download any extra data defined in the JSON. |
|
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
51 NOTE: the "path" value is a relative path to the file on our |
|
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
52 file system. This is slightly dangerous and we should make every effort |
|
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
53 to avoid a malicious absolute path to write the file elsewhere on the |
|
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
54 filesystem. |
|
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
55 """ |
| 0 | 56 for ext_data in query_ext_data: |
|
4
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
57 if not os.path.exists( base_path ): |
|
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
58 os.mkdir( base_path ) |
| 0 | 59 query_stream = urllib.urlopen( ext_data.get( 'url' ) ) |
|
4
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
60 ext_path = ext_data.get( 'path' ) |
|
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
61 os.makedirs( os.path.normpath( '/'.join( [ base_path, os.path.dirname( ext_path ) ] ) ) ) |
|
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
62 output_stream = open( os.path.normpath( '/'.join( [ base_path, ext_path ] ) ), 'wb' ) |
| 0 | 63 chunk_write( query_stream, output_stream ) |
| 64 query_stream.close() | |
| 65 output_stream.close() | |
| 66 | |
| 67 | |
|
4
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
68 def metadata_to_json( dataset_id, metadata, filename, ds_type='dataset', primary=False): |
|
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
69 """ Return line separated JSON """ |
|
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
70 meta_dict = dict( type = ds_type, |
|
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
71 ext = metadata.get( 'extension' ), |
|
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
72 filename = filename, |
|
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
73 name = metadata.get( 'name' ), |
|
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
74 metadata = metadata.get( 'metadata' ) ) |
|
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
75 if metadata.get( 'extra_data', None ): |
|
5
33fa019735a4
Save extra files path as extra_files, not extra_data. Changed type of dataset for new primary datasets. db_key becomes dbkey. Added example json files.
Matt Shirley <mdshw5@gmail.com>
parents:
4
diff
changeset
|
76 meta_dict[ 'extra_files' ] = '_'.join( [ filename, 'files' ] ) |
|
4
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
77 if primary: |
|
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
78 meta_dict[ 'base_dataset_id' ] = dataset_id |
|
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
79 else: |
|
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
80 meta_dict[ 'dataset_id' ] = dataset_id |
|
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
81 return "%s\n" % json.dumps( meta_dict ) |
|
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
82 |
|
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
83 |
|
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
84 def download_files_and_write_metadata(query_item, json_params, output_base_path, metadata_parameter_file, primary): |
|
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
85 """ Main work function that operates on the JSON representation of |
|
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
86 one dataset and its metadata. Returns True. |
|
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
87 """ |
|
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
88 dataset_url, output_filename, \ |
|
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
89 extra_files_path, file_name, \ |
|
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
90 ext, out_data_name, \ |
|
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
91 hda_id, dataset_id = set_up_config_values(json_params) |
|
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
92 extension = query_item.get( 'extension' ) |
|
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
93 filename = query_item.get( 'url' ) |
|
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
94 extra_data = query_item.get( 'extra_data', None ) |
|
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
95 if primary: |
|
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
96 filename = ''.join( c in VALID_CHARS and c or '-' for c in filename ) |
|
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
97 name = construct_multi_filename( hda_id, filename, extension ) |
|
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
98 target_output_filename = os.path.normpath( '/'.join( [ output_base_path, name ] ) ) |
|
5
33fa019735a4
Save extra files path as extra_files, not extra_data. Changed type of dataset for new primary datasets. db_key becomes dbkey. Added example json files.
Matt Shirley <mdshw5@gmail.com>
parents:
4
diff
changeset
|
99 metadata_parameter_file.write( metadata_to_json( dataset_id, query_item, |
|
33fa019735a4
Save extra files path as extra_files, not extra_data. Changed type of dataset for new primary datasets. db_key becomes dbkey. Added example json files.
Matt Shirley <mdshw5@gmail.com>
parents:
4
diff
changeset
|
100 target_output_filename, |
| 7 | 101 ds_type='new_primary_dataset', |
|
5
33fa019735a4
Save extra files path as extra_files, not extra_data. Changed type of dataset for new primary datasets. db_key becomes dbkey. Added example json files.
Matt Shirley <mdshw5@gmail.com>
parents:
4
diff
changeset
|
102 primary=primary) ) |
|
4
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
103 else: |
|
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
104 target_output_filename = output_filename |
|
5
33fa019735a4
Save extra files path as extra_files, not extra_data. Changed type of dataset for new primary datasets. db_key becomes dbkey. Added example json files.
Matt Shirley <mdshw5@gmail.com>
parents:
4
diff
changeset
|
105 metadata_parameter_file.write( metadata_to_json( dataset_id, query_item, |
|
33fa019735a4
Save extra files path as extra_files, not extra_data. Changed type of dataset for new primary datasets. db_key becomes dbkey. Added example json files.
Matt Shirley <mdshw5@gmail.com>
parents:
4
diff
changeset
|
106 target_output_filename, |
| 7 | 107 ds_type='dataset', |
|
5
33fa019735a4
Save extra files path as extra_files, not extra_data. Changed type of dataset for new primary datasets. db_key becomes dbkey. Added example json files.
Matt Shirley <mdshw5@gmail.com>
parents:
4
diff
changeset
|
108 primary=primary) ) |
|
4
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
109 download_from_query( query_item, target_output_filename ) |
|
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
110 if extra_data: |
|
6
46b589e9747a
Remove underscore from extra_files_path.
Matt Shirley <mdshw5@gmail.com>
parents:
5
diff
changeset
|
111 extra_files_path = ''.join( [ target_output_filename, 'files' ] ) |
|
46b589e9747a
Remove underscore from extra_files_path.
Matt Shirley <mdshw5@gmail.com>
parents:
5
diff
changeset
|
112 download_extra_data( extra_data, extra_files_path ) |
|
4
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
113 return True |
|
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
114 |
|
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
115 |
|
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
116 def set_up_config_values(json_params): |
|
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
117 """ Parse json_params file and return a tuple of necessary configuration |
|
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
118 values. |
|
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
119 """ |
|
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
120 datasource_params = json_params.get( 'param_dict' ) |
|
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
121 dataset_url = datasource_params.get( 'URL' ) |
|
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
122 output_filename = datasource_params.get( 'output1', None ) |
|
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
123 output_data = json_params.get( 'output_data' ) |
|
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
124 extra_files_path, file_name, ext, out_data_name, hda_id, dataset_id = \ |
|
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
125 itemgetter('extra_files_path', 'file_name', 'ext', 'out_data_name', 'hda_id', 'dataset_id')(output_data[0]) |
|
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
126 return (dataset_url, output_filename, |
|
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
127 extra_files_path, file_name, |
|
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
128 ext, out_data_name, |
|
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
129 hda_id, dataset_id) |
|
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
130 |
|
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
131 |
| 0 | 132 def download_from_json_data( options, args ): |
| 133 """ Parse the returned JSON data and download files. Write metadata | |
| 134 to flat JSON file. | |
| 135 """ | |
|
4
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
136 output_base_path = options.path |
|
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
137 # read tool job configuration file and parse parameters we need |
| 0 | 138 json_params = json.loads( open( options.json_param_file, 'r' ).read() ) |
|
4
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
139 dataset_url, output_filename, \ |
|
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
140 extra_files_path, file_name, \ |
|
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
141 ext, out_data_name, \ |
|
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
142 hda_id, dataset_id = set_up_config_values(json_params) |
|
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
143 # line separated JSON file to contain all dataset metadata |
|
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
144 metadata_parameter_file = open( json_params['job_config']['TOOL_PROVIDED_JOB_METADATA_FILE'], 'wb' ) |
| 0 | 145 |
| 146 # get JSON response from data source | |
| 147 # TODO: make sure response is not enormous | |
| 148 query_params = json.loads(urllib.urlopen( dataset_url ).read()) | |
| 149 # download and write files | |
|
4
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
150 primary = False |
|
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
151 # query_item, hda_id, output_base_path, dataset_id |
| 0 | 152 for query_item in query_params: |
| 153 if isinstance( query_item, list ): | |
|
4
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
154 # TODO: do something with the nested list as a collection |
| 0 | 155 for query_subitem in query_item: |
|
4
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
156 primary = download_files_and_write_metadata(query_subitem, json_params, output_base_path, |
|
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
157 metadata_parameter_file, primary) |
| 0 | 158 |
| 159 elif isinstance( query_item, dict ): | |
|
4
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
160 primary = download_files_and_write_metadata(query_item, json_params, output_base_path, |
|
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
161 metadata_parameter_file, primary) |
|
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
162 metadata_parameter_file.close() |
| 0 | 163 |
| 164 def __main__(): | |
| 165 """ Read the JSON return from a data source. Parse each line and request | |
| 166 the data, download to "newfilepath", and write metadata. | |
| 167 | |
| 168 Schema | |
| 169 ------ | |
| 170 | |
| 171 [ {"url":"http://url_of_file", | |
| 172 "name":"encode WigData", | |
| 173 "extension":"wig", | |
| 174 "metadata":{"db_key":"hg19"}, | |
| 175 "extra_data":[ {"url":"http://url_of_ext_file", | |
| 176 "path":"rel/path/to/ext_file"} | |
| 177 ] | |
| 178 } | |
| 179 ] | |
| 180 | |
| 181 """ | |
| 182 # Parse the command line options | |
| 183 usage = "Usage: json_data_source.py max_size --json_param_file filename [options]" | |
| 184 parser = optparse.OptionParser(usage = usage) | |
| 185 parser.add_option("-j", "--json_param_file", type="string", | |
| 186 action="store", dest="json_param_file", help="json schema return data") | |
| 187 parser.add_option("-p", "--path", type="string", | |
|
4
96103d66b7af
Properly handle extra data paths, write complete line separated JSON for Galaxy to scoop up and set metadata.
Matt Shirley <mdshw5@gmail.com>
parents:
3
diff
changeset
|
188 action="store", dest="path", help="new file path") |
| 0 | 189 |
| 190 (options, args) = parser.parse_args() | |
| 191 download_from_json_data( options, args ) | |
| 192 | |
| 193 | |
| 194 if __name__ == "__main__": __main__() |
