Mercurial > repos > cathywise > truststore_import
comparison TrustStoreGalaxyImport.py @ 20:b94a8f55b1da
Un-gzip.
| author | Catherine Wise <catherine.wise@csiro.au> |
|---|---|
| date | Mon, 13 Jan 2014 08:51:17 +1100 |
| parents | 159d2159e745 |
| children | 8cf18ca6f13f |
comparison
equal
deleted
inserted
replaced
| 19:1cae026e4dcd | 20:b94a8f55b1da |
|---|---|
| 1 import sys | 1 import sys |
| 2 import shutil | 2 import shutil |
| 3 import gzip | |
| 3 from py_ts import TrustStoreClient, ts_utils | 4 from py_ts import TrustStoreClient, ts_utils |
| 5 from galaxy.datatypes.checkers import * | |
| 4 | 6 |
| 5 def printNice(elem, f, depth): | 7 def printNice(elem, f, depth): |
| 6 try: | 8 try: |
| 7 f.write('\t'*depth + elem.name + " (" + str(len(elem.fragments)) + " parts)\n") | 9 f.write('\t'*depth + elem.name + " (" + str(len(elem.fragments)) + " parts)\n") |
| 8 except AttributeError: | 10 except AttributeError: |
| 52 location = root | 54 location = root |
| 53 if filename and filename != "": | 55 if filename and filename != "": |
| 54 downloadMe = ts_utils.ts_utils.recurseToChildNamed(location, filename) | 56 downloadMe = ts_utils.ts_utils.recurseToChildNamed(location, filename) |
| 55 if downloadMe: | 57 if downloadMe: |
| 56 download = ts.getFile(store, downloadMe) | 58 download = ts.getFile(store, downloadMe) |
| 57 shutil.copy(download, outputFile) | 59 is_gzipped, is_valid = check_gzip(download) |
| 60 | |
| 61 if is_gzipped and not is_valid: | |
| 62 print "File is compressed (gzip) but not valid." | |
| 63 sys.exit(4) | |
| 64 elif is_gzipped and is_valid: | |
| 65 if link_data_only == 'copy_files': | |
| 66 # We need to uncompress the temp_name file, but BAM files must remain compressed in the BGZF format | |
| 67 CHUNK_SIZE = 2**20 # 1Mb | |
| 68 fd, uncompressed = tempfile.mkstemp(prefix='data_id_%s_upload_gunzip_' % dataset.dataset_id, dir=os.path.dirname(outputFile), text=False ) | |
| 69 gzipped_file = gzip.GzipFile(download, 'rb') | |
| 70 while 1: | |
| 71 try: | |
| 72 chunk = gzipped_file.read(CHUNK_SIZE) | |
| 73 except IOError: | |
| 74 os.close(fd) | |
| 75 os.remove(uncompressed) | |
| 76 print 'Problem decompressing gzipped data', dataset, json_file | |
| 77 sys.exit(4) | |
| 78 if not chunk: | |
| 79 break | |
| 80 os.write(fd, chunk) | |
| 81 os.close(fd) | |
| 82 gzipped_file.close() | |
| 83 | |
| 84 shutil.copy(uncompressed, outputFile) | |
| 85 else: | |
| 86 shutil.copy(download, outputFile) | |
| 58 else: | 87 else: |
| 59 print "File not found" | 88 print "File not found" |
| 60 sys.exit(4) | 89 sys.exit(4) |
| 61 else: | 90 else: |
| 62 with open(outputFile, 'w+') as f: | 91 with open(outputFile, 'w+') as f: |
