annotate proteinpilot_wrapper.py @ 0:7dcb26ce559c

Uploaded
author galaxyp
date Wed, 19 Dec 2012 00:22:55 -0500
parents
children 790d80981060
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
1 #!/usr/bin/env python
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
2 import optparse
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
3 import os
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
4 import sys
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
5 import tempfile
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
6 import shutil
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
7 import subprocess
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
8 import re
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
9 import time
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
10 from os.path import basename
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
11 import logging
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
12
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
13 assert sys.version_info[:2] >= ( 2, 6 )
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
14
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
15 log = logging.getLogger(__name__)
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
16
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
17 DEBUG = True
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
18
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
19 working_directory = os.getcwd()
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
20 tmp_stderr_name = tempfile.NamedTemporaryFile(dir = working_directory, suffix = '.stderr').name
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
21 tmp_stdout_name = tempfile.NamedTemporaryFile(dir = working_directory, suffix = '.stdout').name
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
22
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
23 def stop_err( msg ):
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
24 sys.stderr.write( "%s\n" % msg )
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
25 sys.exit()
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
26
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
27
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
28 def read_stderr():
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
29 stderr = ''
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
30 if(os.path.exists(tmp_stderr_name)):
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
31 with open(tmp_stderr_name, 'rb') as tmp_stderr:
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
32 buffsize = 1048576
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
33 try:
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
34 while True:
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
35 stderr += tmp_stderr.read(buffsize)
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
36 if not stderr or len(stderr) % buffsize != 0:
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
37 break
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
38 except OverflowError:
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
39 pass
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
40 return stderr
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
41
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
42
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
43 def execute(command, stdin=None):
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
44 try:
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
45 with open(tmp_stderr_name, 'wb') as tmp_stderr:
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
46 with open(tmp_stdout_name, 'wb') as tmp_stdout:
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
47 proc = subprocess.Popen(args=command, shell=True, stderr=tmp_stderr.fileno(), stdout=tmp_stdout.fileno(), stdin=stdin, env=os.environ)
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
48 returncode = proc.wait()
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
49 if returncode != 0:
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
50 raise Exception("Program returned with non-zero exit code %d. stderr: %s" % (returncode, read_stderr()))
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
51 finally:
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
52 print open(tmp_stderr_name, "r").read(64000)
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
53 print open(tmp_stdout_name, "r").read(64000)
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
54
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
55
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
56 def delete_file(path):
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
57 if os.path.exists(path):
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
58 try:
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
59 os.remove(path)
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
60 except:
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
61 pass
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
62
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
63 def delete_directory(directory):
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
64 if os.path.exists(directory):
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
65 try:
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
66 shutil.rmtree(directory)
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
67 except:
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
68 pass
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
69
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
70 def symlink(source, link_name):
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
71 import platform
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
72 if platform.system() == 'Windows':
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
73 try:
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
74 import win32file
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
75 win32file.CreateSymbolicLink(source, link_name, 1)
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
76 except:
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
77 shutil.copy(source, link_name)
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
78 else:
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
79 os.symlink(source, link_name)
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
80
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
81
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
82 def copy_to_working_directory(data_file, relative_path):
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
83 if os.path.abspath(data_file) != os.path.abspath(relative_path):
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
84 shutil.copy(data_file, relative_path)
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
85 return relative_path
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
86
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
87 def __main__():
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
88 run_script()
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
89
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
90 #ENDTEMPLATE
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
91
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
92 from string import Template
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
93
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
94 METHOD_TEMPLATE = """<UISETTINGS>
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
95 <UI_SAMPLE_TYPE>$sample_type</UI_SAMPLE_TYPE>
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
96 <UI_QUANT_TYPE>$quant_type</UI_QUANT_TYPE>
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
97 <UI_BACKGROUND_CORRECTION>$background_correction</UI_BACKGROUND_CORRECTION>
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
98 <UI_BIAS_CORRECTION>$bias_correction</UI_BIAS_CORRECTION>
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
99 <UI_CYS_ALKYLATION>$cys_alkylation</UI_CYS_ALKYLATION>
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
100 <UI_DIGESTION>$digestion</UI_DIGESTION>
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
101 <UI_SPECIAL_FACTOR>$special_factors</UI_SPECIAL_FACTOR>
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
102 <UI_INSTRUMENT>$instrument</UI_INSTRUMENT>
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
103 <UI_SPECIES></UI_SPECIES>
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
104 <UI_USER_NAME></UI_USER_NAME>
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
105 <UI_MACHINE_NAME></UI_MACHINE_NAME>
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
106 <UI_START_TIME></UI_START_TIME>
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
107 <UI_SEARCH_ID></UI_SEARCH_ID>
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
108 <UI_ID_FOCUS>$search_foci</UI_ID_FOCUS>
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
109 <UI_SEARCH_EFFORT>$search_effort</UI_SEARCH_EFFORT>
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
110 <UI_SEARCH_RESOURCE>$database_name</UI_SEARCH_RESOURCE>
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
111 <UI_MIN_UNUSED_PROTSCORE>$min_unused_protscore</UI_MIN_UNUSED_PROTSCORE>
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
112 <UI_PSPEP>$pspep</UI_PSPEP>
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
113 <UI_MAX_QUANT_LABELS>$max_quant_labels</UI_MAX_QUANT_LABELS>
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
114 $quant_labels
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
115 </UISETTINGS>
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
116 """
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
117
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
118 quant_special_cases = {
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
119 "iTRAQ 4plex (Peptide Labeled)": "iTRAQ4PLEX",
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
120 "iTRAQ 4plex (Protein Labeled)": "iTRAQ4PLEX",
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
121 "iTRAQ 8plex (Peptide Labeled)": "iTRAQ8PLEX",
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
122 "iTRAQ 8plex (Protein Labeled)": "iTRAQ8PLEX",
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
123 "mTRAQ (Peptide Labeled - M00, M04)": "mTRAQ_0-4",
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
124 "mTRAQ (Peptide Labeled - M00, M08)": "mTRAQ_0-8",
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
125 "mTRAQ (Peptide Labeled - M04, M08)": "mTRAQ_4-8",
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
126 "mTRAQ (Peptide Labeled - M00, M04, M08)": "mTRAQ_0-4-8",
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
127 "Proteolytic O-18 labeling": "Proteolytic O-18 v O-16",
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
128 "Cleavable ICAT": "ICAT9",
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
129 "ICPL Light, Heavy (Peptide Labeled)": "ICPL peptide",
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
130 "ICPL Light, Heavy (Protein Labeled)": "ICPL protein",
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
131 }
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
132
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
133
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
134 def get_env_property(name, default):
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
135 if name in os.environ:
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
136 return os.environ[name]
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
137 else:
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
138 return default
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
139
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
140
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
141 def build_quant_label(reagent, quant_type="Not Used", treatment="", minus2="0", minus1="0", plus1="0", plus2="0"):
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
142 return {
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
143 "reagent": reagent,
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
144 "type": quant_type,
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
145 "treatment": treatment,
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
146 "minus2": minus2,
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
147 "minus1": minus1,
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
148 "plus1": plus1,
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
149 "plus2": plus2,
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
150 }
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
151
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
152
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
153 def build_quant_labels(options, quant_type):
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
154 if quant_type == "iTRAQ8PLEX":
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
155 return [
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
156 build_quant_label("iTRAQ113", plus1="6.89", plus2="0.24"),
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
157 build_quant_label("iTRAQ114", minus1="0.94", plus1="5.9", plus2="0.16"),
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
158 build_quant_label("iTRAQ115", minus1="1.88", plus1="4.9", plus2="0.1"),
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
159 build_quant_label("iTRAQ116", minus1="2.82", plus1="3.9", plus2="0.07"),
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
160 build_quant_label("iTRAQ117", minus2="0.06", minus1="3.77", plus1="2.88"),
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
161 build_quant_label("iTRAQ118", minus2="0.09", minus1="4.71", plus1="1.91"),
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
162 build_quant_label("iTRAQ119", minus2="0.14", minus1="5.66", plus1="0.87"),
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
163 build_quant_label("iTRAQ121", minus2="0.27", minus1="7.44", plus1="0.18"),
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
164 ]
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
165 elif quant_type == "iTRAQ4PLEX":
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
166 return [
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
167 build_quant_label("iTRAQ114", minus1="1.00", plus1="5.9", plus2="0.20"),
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
168 build_quant_label("iTRAQ115", minus1="2.00", plus1="5.6", plus2="0.1"),
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
169 build_quant_label("iTRAQ116", minus1="3.00", plus1="4.5", plus2="0.1"),
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
170 build_quant_label("iTRAQ117", minus2="0.10", minus1="4.00", plus1="3.50", plus2="0.1"),
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
171 ]
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
172 else:
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
173 return []
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
174
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
175
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
176 def join_quant_labels(labels):
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
177 template = '<QUANT_LABEL_SETTING reagent="$reagent" type="$type" treatment="$treatment" minus2="$minus2" minus1="$minus1" plus1="$plus1" plus2="$plus2"/>'
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
178 return "\n".join([Template(template).substitute(quant_label) for quant_label in labels])
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
179
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
180
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
181 def handle_sample_type(options, parameter_dict):
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
182 sample_type = options.sample_type
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
183 if sample_type in quant_special_cases:
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
184 quant_type = quant_special_cases[sample_type]
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
185 else:
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
186 quant_type = sample_type
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
187 if options.quantitative.upper() != "TRUE":
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
188 quant_type = ""
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
189 parameter_dict["sample_type"] = sample_type
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
190 parameter_dict["quant_type"] = quant_type
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
191 parameter_dict["quant_labels"] = join_quant_labels(build_quant_labels(options, quant_type))
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
192
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
193
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
194 def setup_database(options):
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
195 PROTEINPILOT_DATABASE_DIR = get_env_property("PROTEIN_PILOT_DATABASE_FOLDER", "C:\\AB SCIEX\\ProteinPilot Data\\SearchDatabases")
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
196 database_path = options.database
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
197 database_name = options.database_name
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
198 database_name = database_name.replace(" ", "_")
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
199 (database_basename, extension) = os.path.splitext(database_name)
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
200 base = os.path.join(PROTEINPILOT_DATABASE_DIR, "gx_%s" % database_basename)
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
201 database_destination = get_unique_path(base, ".fasta")
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
202 symlink(database_path, database_destination)
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
203 return (database_destination, os.path.basename(os.path.splitext(database_destination)[0]))
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
204
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
205
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
206 def extract_list(parameter):
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
207 if parameter == None or parameter == "None":
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
208 parameter = ""
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
209 return parameter.replace(",", ";")
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
210
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
211
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
212 def setup_methods(options):
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
213 ## Setup methods file
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
214 (database_path, database_name) = setup_database(options)
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
215 special_factors = extract_list(options.special_factors)
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
216 search_foci = extract_list(options.search_foci)
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
217 method_parameters = {
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
218 "background_correction": options.background_correction,
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
219 "bias_correction": options.bias_correction,
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
220 "cys_alkylation": options.cys_alkylation,
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
221 "digestion": options.digestion,
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
222 "instrument": options.instrument,
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
223 "search_effort": options.search_effort,
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
224 "search_foci": search_foci,
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
225 "pspep": options.pspep,
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
226 "min_unused_protscore": options.min_unused_protscore,
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
227 "max_quant_labels": "3",
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
228 "database_name": database_name,
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
229 "quantitative": options.quantitative,
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
230 "special_factors": special_factors
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
231 }
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
232 handle_sample_type(options, method_parameters)
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
233 method_contents = Template(METHOD_TEMPLATE).substitute(method_parameters)
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
234 PROTEINPILOT_METHODS_DIR = get_env_property("PROTEIN_PILOT_METHODS_FOLDER", "C:\\ProgramData\\AB SCIEX\\ProteinPilot\\ParagonMethods\\")
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
235 methods_name = "gx_%s" % os.path.split(os.getcwd())[-1]
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
236 methods_path = os.path.join(PROTEINPILOT_METHODS_DIR, "%s.xml" % methods_name)
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
237 open(methods_path, "w").write(method_contents)
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
238 return (methods_name, methods_path, database_path)
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
239
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
240
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
241 def setup_inputs(inputs, input_names):
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
242 links = []
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
243 for input, input_name in zip(inputs, input_names):
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
244 if DEBUG:
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
245 print "Processing input %s with name %s and size %d" % (input, input_name, os.stat(input).st_size)
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
246 if not input_name.upper().endswith(".MGF"):
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
247 input_name = "%s.mgf" % input_name
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
248 link_path = os.path.abspath(input_name)
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
249 symlink(input, link_path)
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
250 links.append(link_path)
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
251 return ",".join(["<DATA type=\"MGF\" filename=\"%s\" />" % link for link in links])
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
252
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
253
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
254 def get_unique_path(base, extension):
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
255 """
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
256 """
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
257 return "%s_%d%s" % (base, int(time.time() * 1000), extension)
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
258
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
259
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
260 def move_pspep_output(options, destination, suffix):
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
261 if destination:
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
262 source = "%s__FalsePositiveAnalysis__%s.csv" % (options.output, suffix)
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
263 shutil.move(source, destination)
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
264
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
265
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
266 def run_script():
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
267 parser = optparse.OptionParser()
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
268 parser.add_option("--input", dest="input", action="append", default=[])
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
269 parser.add_option("--input_name", dest="input_name", action="append", default=[])
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
270 parser.add_option("--database")
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
271 parser.add_option("--database_name")
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
272 parser.add_option("--instrument")
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
273 parser.add_option("--sample_type") # TODO: Restrict values
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
274 parser.add_option("--bias_correction", default="False")
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
275 parser.add_option("--background_correction", default="False")
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
276 parser.add_option("--cys_alkylation", default="None")
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
277 parser.add_option("--digestion", default="Trypsin")
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
278 parser.add_option("--special_factors", default="")
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
279 parser.add_option("--search_foci", default="")
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
280 parser.add_option("--search_effort", default="Rapid")
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
281 parser.add_option("--min_unused_protscore", default="3")
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
282 parser.add_option("--quantitative", default="False")
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
283 parser.add_option("--pspep", default="TRUE")
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
284 parser.add_option("--output")
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
285 parser.add_option("--output_methods")
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
286 #parser.add_option("--output_pspep_peptide", default="")
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
287 #parser.add_option("--output_pspep_protein", default="")
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
288 #parser.add_option("--output_pspep_spectra", default="")
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
289 parser.add_option("--output_pspep_report", default="")
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
290 (options, args) = parser.parse_args()
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
291
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
292 (methods_name, methods_path, database_path) = setup_methods(options)
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
293 try:
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
294 group_file = "%s.group" % options.output
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
295 input_contents_template = """<PROTEINPILOTPARAMETERS>
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
296 <METHOD name="$methods_name" />
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
297 $inputs
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
298 <RESULT filename="$output" />
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
299 </PROTEINPILOTPARAMETERS>"""
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
300 input_parameters = {
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
301 "inputs": setup_inputs(options.input, options.input_name),
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
302 "output": group_file,
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
303 "methods_name": methods_name
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
304 }
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
305
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
306 input_contents = Template(input_contents_template).substitute(input_parameters)
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
307 open("input.xml", "w").write(input_contents)
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
308
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
309 protein_pilot_path = get_env_property("PROTEIN_PILOT_PATH", "")
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
310 if protein_pilot_path and not protein_pilot_path.endswith("\\"):
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
311 protein_pilot_path = "%s" % protein_pilot_path
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
312 execute("%sProteinPilot.exe input.xml" % protein_pilot_path)
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
313 shutil.move(group_file, options.output)
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
314 #move_pspep_output(options, options.output_pspep_spectra, "SpectralLevelData")
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
315 #move_pspep_output(options, options.output_pspep_peptide, "DistinctPeptideLevelData")
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
316 #move_pspep_output(options, options.output_pspep_protein, "ProteinLevelData")
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
317 if options.output_pspep_report:
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
318 source = "%s__FDR.xlsx" % (options.output)
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
319 shutil.move(source, options.output_pspep_report)
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
320 shutil.move(methods_path, options.output_methods)
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
321 finally:
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
322 delete_file(database_path)
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
323 delete_file(methods_path)
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
324
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
325 if __name__ == '__main__':
7dcb26ce559c Uploaded
galaxyp
parents:
diff changeset
326 __main__()