annotate ms_wiff_loader.py @ 0:5bc64a2d5f91 draft default tip

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