annotate proteinpilot_wrapper.py @ 1:790d80981060

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