annotate tools/pepxml_viewer_wrapper.py @ 0:2e6912b6ee38 draft default tip

Uploaded
author galaxyp
date Wed, 08 Oct 2014 13:49:16 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
1 #!/usr/bin/env python
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
2 import optparse
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
3 import os
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
4 import sys
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
5 import tempfile
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
6 import subprocess
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
7 import shutil
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
8 import logging
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
9
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
10 assert sys.version_info[:2] >= ( 2, 6 )
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
11
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
12 log = logging.getLogger(__name__)
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
13 working_directory = os.getcwd()
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
14 tmp_stderr_name = tempfile.NamedTemporaryFile(dir=working_directory, suffix='.stderr').name
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
15 tmp_stdout_name = tempfile.NamedTemporaryFile(dir=working_directory, suffix='.stdout').name
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
16
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
17
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
18 def stop_err( msg ):
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
19 sys.stderr.write( "%s\n" % msg )
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
20 sys.exit()
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
21
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
22
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
23 def read_stderr():
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
24 stderr = ''
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
25 if(os.path.exists(tmp_stderr_name)):
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
26 with open(tmp_stderr_name, 'rb') as tmp_stderr:
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
27 buffsize = 1048576
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
28 try:
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
29 while True:
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
30 stderr += tmp_stderr.read(buffsize)
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
31 if not stderr or len(stderr) % buffsize != 0:
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
32 break
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
33 except OverflowError:
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
34 pass
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
35 return stderr
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
36
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
37
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
38 def execute(command, stdin=None):
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
39 with open(tmp_stderr_name, 'wb') as tmp_stderr:
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
40 with open(tmp_stdout_name, 'wb') as tmp_stdout:
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
41 proc = subprocess.Popen(args=command, shell=True, stderr=tmp_stderr.fileno(), stdout=tmp_stdout.fileno(), stdin=stdin, env=os.environ)
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
42 returncode = proc.wait()
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
43 if returncode != 0:
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
44 raise Exception("Program returned with non-zero exit code %d. stderr: %s" % (returncode, read_stderr()))
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
45
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
46
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
47 def delete_file(path):
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
48 if os.path.exists(path):
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
49 try:
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
50 os.remove(path)
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
51 except:
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
52 pass
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
53
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
54
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
55 def delete_directory(directory):
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
56 if os.path.exists(directory):
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
57 try:
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
58 shutil.rmtree(directory)
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
59 except:
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
60 pass
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
61
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
62
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
63 def symlink(source, link_name):
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
64 import platform
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
65 if platform.system() == 'Windows':
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
66 import win32file
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
67 win32file.CreateSymbolicLink(source, link_name, 1)
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
68 else:
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
69 os.symlink(source, link_name)
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
70
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
71
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
72 def copy_to_working_directory(data_file, relative_path):
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
73 if os.path.abspath(data_file) != os.path.abspath(relative_path):
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
74 shutil.copy(data_file, relative_path)
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
75 return relative_path
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
76
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
77
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
78 def __main__():
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
79 run_script()
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
80
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
81 #ENDTEMPLATE
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
82
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
83
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
84 def run_script():
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
85 parser = optparse.OptionParser()
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
86 parser.add_option("--input")
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
87 parser.add_option("--export_spreadsheet", action="store_true", dest="export_spreadsheet")
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
88 parser.add_option("--append_unmodified_peptide", action="store_true", dest="append_unmodified_peptide", default=False)
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
89 parser.set_defaults(export_spreadsheet=False)
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
90 (options, args) = parser.parse_args()
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
91
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
92 copy_to_working_directory(options.input, "input.pep.xml")
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
93 # Trans-Proteomic Pipeline - cgi-bin/PepXMLViewer.cgi
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
94 cmd = "PepXMLViewer.cgi -I input.pep.xml"
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
95 cmd = "%s %s" % (cmd, "-B exportSpreadsheet")
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
96 if options.export_spreadsheet:
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
97 cmd = "%s %s" % (cmd, "1")
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
98 else:
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
99 cmd = "%s %s" % (cmd, "0")
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
100 execute(cmd)
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
101 if options.append_unmodified_peptide:
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
102 from csv import reader
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
103 csv_opts = {'delimiter': '\t'}
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
104 first = True
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
105 with open("tmp.xls", "w") as output:
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
106 peptide_index = None
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
107 for row in reader(open("input.pep.xls", "r"), **csv_opts):
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
108 if first:
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
109 peptide_index = row.index("peptide")
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
110 row.append("unmodified_peptide")
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
111 output.write("\t".join(row))
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
112 output.write("\n")
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
113 first = False
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
114 else:
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
115 row.append(unmodify(row[peptide_index]))
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
116 output.write("\t".join(row))
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
117 output.write("\n")
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
118 execute("mv tmp.xls input.pep.xls")
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
119
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
120
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
121 def unmodify(peptide):
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
122 """
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
123
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
124 >>> from re import sub
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
125 >>> sub(r'\[(\-|\d|\.)+\]', '', 'A[-12.34]B')
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
126 'AB'
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
127 """
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
128 from re import sub
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
129 peptide = sub(r'\[(\-|\d|\.)+\]', '', peptide)
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
130 peptide = sub(r'^.\.|\..$|n|c', '', peptide)
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
131 #peptide = sub(r'\..$', '', peptide)
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
132 #peptide = sub(r'\[.+\]', '', peptide)
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
133 return peptide
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
134
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
135
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
136 if __name__ == '__main__':
2e6912b6ee38 Uploaded
galaxyp
parents:
diff changeset
137 __main__()