annotate pepxml_viewer_wrapper.py @ 0:9156a440afed draft default tip

Improved some datatype handling
author galaxyp
date Thu, 20 Jun 2013 11:09:24 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
1 #!/usr/bin/env python
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
2 import optparse
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
3 import os
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
4 import sys
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
5 import tempfile
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
6 import shutil
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
7 import subprocess
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
8 import re
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
9 from os.path import basename
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
10 import logging
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
11
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
12 assert sys.version_info[:2] >= ( 2, 6 )
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
13
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
14 log = logging.getLogger(__name__)
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
15 working_directory = os.getcwd()
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
16 tmp_stderr_name = tempfile.NamedTemporaryFile(dir = working_directory, suffix = '.stderr').name
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
17 tmp_stdout_name = tempfile.NamedTemporaryFile(dir = working_directory, suffix = '.stdout').name
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
18
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
19 def stop_err( msg ):
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
20 sys.stderr.write( "%s\n" % msg )
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
21 sys.exit()
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
22
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
23 def read_stderr():
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
24 stderr = ''
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
25 if(os.path.exists(tmp_stderr_name)):
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
26 with open(tmp_stderr_name, 'rb') as tmp_stderr:
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
27 buffsize = 1048576
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
28 try:
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
29 while True:
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
30 stderr += tmp_stderr.read(buffsize)
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
31 if not stderr or len(stderr) % buffsize != 0:
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
32 break
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
33 except OverflowError:
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
34 pass
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
35 return stderr
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
36
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
37 def execute(command, stdin=None):
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
38 with open(tmp_stderr_name, 'wb') as tmp_stderr:
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
39 with open(tmp_stdout_name, 'wb') as tmp_stdout:
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
40 proc = subprocess.Popen(args=command, shell=True, stderr=tmp_stderr.fileno(), stdout=tmp_stdout.fileno(), stdin=stdin, env=os.environ)
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
41 returncode = proc.wait()
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
42 if returncode != 0:
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
43 raise Exception, "Program returned with non-zero exit code %d. stderr: %s" % (returncode, read_stderr())
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
44
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
45 def delete_file(path):
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
46 if os.path.exists(path):
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
47 try:
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
48 os.remove(path)
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
49 except:
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
50 pass
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
51
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
52 def delete_directory(directory):
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
53 if os.path.exists(directory):
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
54 try:
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
55 shutil.rmtree(directory)
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
56 except:
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
57 pass
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
58
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
59 def symlink(source, link_name):
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
60 import platform
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
61 if platform.system() == 'Windows':
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
62 import win32file
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
63 win32file.CreateSymbolicLink(source, link_name, 1)
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
64 else:
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
65 os.symlink(source, link_name)
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
66
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
67
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
68 def copy_to_working_directory(data_file, relative_path):
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
69 if os.path.abspath(data_file) != os.path.abspath(relative_path):
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
70 shutil.copy(data_file, relative_path)
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
71 return relative_path
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
72
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
73 def __main__():
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
74 run_script()
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
75
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
76 #ENDTEMPLATE
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
77
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
78
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
79 def run_script():
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
80 parser = optparse.OptionParser()
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
81 parser.add_option("--input")
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
82 parser.add_option("--export_spreadsheet", action="store_true", dest="export_spreadsheet")
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
83 parser.set_defaults(export_spreadsheet=False)
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
84 (options, args) = parser.parse_args()
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
85
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
86 copy_to_working_directory(options.input, "input.pep.xml")
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
87 cmd = "PepXMLViewer.cgi -I input.pep.xml"
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
88 cmd = "%s %s" % (cmd, "-B exportSpreadsheet")
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
89 if options.export_spreadsheet:
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
90 cmd = "%s %s" % (cmd, "1")
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
91 else:
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
92 cmd = "%s %s" % (cmd, "0")
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
93 execute(cmd)
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
94
9156a440afed Improved some datatype handling
galaxyp
parents:
diff changeset
95 if __name__ == '__main__': __main__()