annotate protxml2html_wrapper.py @ 0:156ef965340e draft default tip

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