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

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