Mercurial > repos > galaxyp > ms_wiff_loader
comparison ms_wiff_loader.py @ 0:5bc64a2d5f91 draft default tip
Uploaded
| author | galaxyp |
|---|---|
| date | Tue, 10 Mar 2015 18:26:36 -0400 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:5bc64a2d5f91 |
|---|---|
| 1 #!/usr/bin/env python | |
| 2 import optparse | |
| 3 import os | |
| 4 import sys | |
| 5 import tempfile | |
| 6 import shutil | |
| 7 import subprocess | |
| 8 import re | |
| 9 import logging | |
| 10 import urllib2 | |
| 11 from urlparse import urlparse | |
| 12 | |
| 13 | |
| 14 assert sys.version_info[:2] >= (2, 6) | |
| 15 | |
| 16 log = logging.getLogger(__name__) | |
| 17 | |
| 18 CHUNK_SIZE = 2**20 #1mb | |
| 19 | |
| 20 def stop_err(msg): | |
| 21 sys.stderr.write("%s\n" % msg) | |
| 22 sys.exit() | |
| 23 | |
| 24 def download_from_url( url, output_dir, basename=None, ext=None ): | |
| 25 o = urlparse(url) | |
| 26 src_parts = os.path.basename(o.path).split('.',1) | |
| 27 file_name = "%s.%s" % ( basename if basename else src_parts[0], ext if ext else src_parts[1] ) | |
| 28 file_path = os.path.join(output_dir,file_name) | |
| 29 reader = urllib2.urlopen( url ) | |
| 30 writer = open(file_path,'wb') | |
| 31 while True: | |
| 32 data = reader.read( CHUNK_SIZE ) | |
| 33 if data: | |
| 34 writer.write( data ) | |
| 35 else: | |
| 36 break | |
| 37 writer.close() | |
| 38 reader.close() | |
| 39 return file_path | |
| 40 | |
| 41 def __main__(): | |
| 42 parser = optparse.OptionParser() | |
| 43 parser.add_option( '-a', '--archive', dest='archive', default=None, help='URL to archive containing: <name>.wiff file <name>.wiff.scan <name>.wiff.mtd files' ) | |
| 44 parser.add_option( '-w', '--wiff', dest='wiff', default=None, help='URL to <name>.wiff file' ) | |
| 45 parser.add_option( '-s', '--scan', dest='scan', default=None, help='URL to <name>.wiff.scan file' ) | |
| 46 parser.add_option( '-m', '--mtd', dest='mtd', default=None, help='URL to <name>.wiff.mtd file' ) | |
| 47 parser.add_option( '-n', '--name', dest='name', default=None, help='base name for files' ) | |
| 48 parser.add_option( '-o', '--output_dir', dest='output_dir', default=None, help='dir to copy files into' ) | |
| 49 parser.add_option( '-f', '--output_file', dest='output_file', default=None, help='Galaxy dataset file' ) | |
| 50 (options, args) = parser.parse_args() | |
| 51 | |
| 52 if not (options.archive or options.wiff): | |
| 53 stop_err("No wiff input file specified") | |
| 54 output_dir = os.getcwd() | |
| 55 if options.output_dir: | |
| 56 output_dir = options.output_dir | |
| 57 if not os.path.exists( output_dir ): | |
| 58 os.makedirs(output_dir) | |
| 59 basename = options.name | |
| 60 rval = ['<html><head><title>Wiff Composite Dataset %s</title></head><body><p/>' % (basename if basename else '')] | |
| 61 rval.append('This composite dataset is composed of the following files:<p/><ul>') | |
| 62 if options.wiff: | |
| 63 file_path = download_from_url (options.wiff, output_dir, basename=basename, ext='wiff') | |
| 64 rel_path = os.path.basename(file_path) | |
| 65 os.symlink( rel_path, os.path.join(output_dir,'wiff')) | |
| 66 rval.append( '<li><a href="%s" type="application/octet-stream">%s</a></li>' % ( rel_path, rel_path ) ) | |
| 67 print >> sys.stdout, "wiff: %s" % options.wiff | |
| 68 if options.scan: | |
| 69 file_path = download_from_url (options.scan, output_dir, basename=basename, ext='wiff.scan') | |
| 70 rel_path = os.path.basename(file_path) | |
| 71 os.symlink( rel_path, os.path.join(output_dir,'wiff_scan')) | |
| 72 rval.append( '<li><a href="%s" type="application/octet-stream">%s</a></li>' % ( rel_path, rel_path ) ) | |
| 73 print >> sys.stdout, "scan: %s" % options.scan | |
| 74 if options.mtd: | |
| 75 file_path = download_from_url (options.mtd, output_dir, basename=basename, ext='wiff.mtd') | |
| 76 rel_path = os.path.basename(file_path) | |
| 77 os.symlink( rel_path, os.path.join(output_dir,'wiff_mtd')) | |
| 78 rval.append( '<li><a href="%s" type="application/octet-stream">%s</a></li>' % ( rel_path, rel_path ) ) | |
| 79 print >> sys.stdout, "mtd: %s" % options.mtd | |
| 80 if options.output_file: | |
| 81 rval.append( '</ul></div></body></html>' ) | |
| 82 f = open(options.output_file,'a') | |
| 83 f.write("\n".join( rval )) | |
| 84 f.close() | |
| 85 | |
| 86 if __name__ == '__main__': | |
| 87 __main__() |
