6
|
1 #######################################################################################
|
|
2 # Python-code: SAINT pre-processing from MaxQuant "Samples Report" output
|
|
3 # Author: Brent Kuenzi
|
|
4 #######################################################################################
|
|
5 # This program reads in a raw MaxQuant "Samples Report" output and a user generated
|
|
6 # bait file and autoformats it into prey and interaction files for SAINTexpress
|
|
7 # analysis
|
|
8 #######################################################################################
|
|
9 # Copyright (C) Brent Kuenzi.
|
|
10 # Permission is granted to copy, distribute and/or modify this document
|
|
11 # under the terms of the GNU Free Documentation License, Version 1.3
|
|
12 # or any later version published by the Free Software Foundation;
|
|
13 # with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
|
|
14 # A copy of the license is included in the section entitled "GNU
|
|
15 # Free Documentation License".
|
|
16 #######################################################################################
|
|
17 ## REQUIRED INPUT ##
|
|
18
|
|
19 # 1) infile: MaxQuant "Samples Report" output
|
|
20 # 2) baitfile: SAINT formatted bait file generated in Galaxy
|
|
21 # 3) fasta_db: fasta database for use (defaults to SwissProt_HUMAN_2014_08.fasta)
|
|
22 # 4) prey: Y or N for generating a prey file
|
|
23 # 5) make_bait: String of bait names, assignment, and test or control boolean
|
|
24 #######################################################################################
|
|
25
|
|
26
|
|
27 import sys
|
|
28 import os
|
|
29
|
|
30
|
|
31 mq_file = sys.argv[1]
|
|
32 ins_path = sys.argv[8]
|
|
33 names_path = str(ins_path) + r"uniprot_names.txt"
|
29
|
34 fasta_db = sys.argv[3]
|
|
35
|
|
36 # Uses faster names list for filtering when default db used.
|
|
37 if fasta_db == "None":
|
|
38 cmd = (r"Rscript "+ str(ins_path) +"pre_process_protein_name_set.R " + str(mq_file) +
|
|
39 " " + str(names_path))
|
|
40 os.system(cmd)
|
|
41 else:
|
|
42 cmd = (r"Rscript "+ str(ins_path) +"pre_process_protein_name_set.R " + str(mq_file) +
|
|
43 " " + str(fasta_db)
|
|
44 os.system(cmd)
|
6
|
45
|
|
46 infile = "./tukeys_output.txt"
|
|
47 # The MaxQuant "Samples Report" output.
|
|
48 prey = sys.argv[2]
|
|
49 # Y or N boolean from Galaxy.
|
|
50 if fasta_db == "None":
|
|
51 fasta_db = str(ins_path) + "SwissProt_HUMAN_2014_08.fasta"
|
|
52 make_bait = sys.argv[6]
|
|
53 bait_bool = sys.argv[9]
|
|
54
|
|
55 def bait_create(baits, infile):
|
|
56 # Takes the Bait specified by the user and makes them into a Bait file and includes a
|
|
57 # check to make sure they are using valid baits.
|
|
58 baits = make_bait.split()
|
|
59 i = 0
|
|
60 bait_file_tmp = open("bait.txt", "w")
|
|
61 order = []
|
|
62 bait_cache = []
|
|
63 while i < len(baits):
|
|
64 if baits[i+2] == "true":
|
|
65 T_C = "C"
|
|
66 else:
|
|
67 T_C = "T"
|
|
68 bait_line = baits[i] + "\t" + baits[i+1] + "\t" + T_C + "\n"
|
|
69 read_infile = open(infile, "r")
|
|
70 for input_line in read_infile :
|
|
71 input_line = input_line.replace("\"", "")
|
|
72 input_line = input_line.replace(r"Intensity.", "")
|
|
73 # R coerces "-" into "." changes them back and remove Intensity from the Bait names.
|
|
74 input_line = input_line.replace(r".", r"-")
|
|
75 temp = input_line.split()
|
|
76 if "mapped_protein" in str(temp):
|
|
77 if baits[i] in temp:
|
|
78 number_bait = temp.index(str(baits[i]))
|
|
79 number_bait = number_bait - 9
|
|
80 bait_cache.append((number_bait, str(bait_line)))
|
|
81 # Locates the Bait names in the column names and then sets the Baits in the
|
|
82 # correct order in the cache thus the - 9 because the baits start at the 9th
|
|
83 # column.
|
|
84 else:
|
|
85 print "Error: bad bait " + str(baits[i])
|
|
86 sys.exit()
|
|
87 else:
|
|
88 pass
|
|
89 i = i + 3
|
|
90 # Writes cache to Bait file.
|
|
91 bait_cache.sort()
|
|
92 for line in bait_cache:
|
|
93 bait_file_tmp.write(line[1])
|
|
94
|
|
95 bait_file_tmp.close()
|
|
96
|
|
97
|
|
98 if bait_bool == 'false':
|
|
99 bait_create(make_bait, infile)
|
|
100 baitfile = "bait.txt"
|
|
101 else:
|
|
102 bait_temp_file = open(sys.argv[10], 'r')
|
|
103 bait_cache = bait_temp_file.readlines()
|
|
104 bait_file_tmp = open("bait.txt", "wr")
|
|
105 for line in bait_cache:
|
|
106 bait_file_tmp.write(line)
|
|
107 bait_file_tmp.close()
|
|
108 baitfile = "bait.txt"
|
|
109
|
|
110
|
|
111 class ReturnValue1(object):
|
|
112 def __init__(self, sequence, gene):
|
|
113 self.seqlength = sequence
|
|
114 self.genename = gene
|
|
115 class ReturnValue2(object):
|
|
116 def __init__(self, getdata, getproteins, getheader):
|
|
117 self.data = getdata
|
|
118 self.proteins = getproteins
|
|
119 self.header = getheader
|
|
120
|
|
121
|
|
122 def main(MaxQuant_input, make_bait):
|
|
123 #bait_check(baitfile, MaxQuant_input)
|
|
124 make_inter(MaxQuant_input)
|
|
125 if prey == 'true':
|
|
126 make_prey(MaxQuant_input)
|
|
127 no_error_inter(MaxQuant_input)
|
|
128 os.rename('prey.txt', sys.argv[5])
|
|
129 elif prey == 'false':
|
|
130 if os.path.isfile('error proteins.txt') == True:
|
|
131 no_error_inter(MaxQuant_input)
|
|
132 pass
|
|
133 elif prey != 'true' or 'false':
|
|
134 sys.exit("Invalid Prey Argument: Y or N")
|
|
135 os.rename('inter.txt', sys.argv[4])
|
|
136 os.rename("bait.txt", sys.argv[7])
|
|
137
|
|
138
|
|
139 def get_info(uniprot_accession_in):
|
|
140 # Get aa lengths and gene name.
|
|
141 error = open('error proteins.txt', 'a+')
|
|
142 data = open(fasta_db, 'r')
|
|
143 data_lines = data.readlines()
|
|
144 db_len = len(data_lines)
|
|
145 seqlength = 0
|
|
146 count = 0
|
|
147 for data_line in data_lines:
|
|
148 if ">sp" in data_line:
|
|
149 if uniprot_accession_in == data_line.split("|")[1]:
|
|
150 match = count + 1
|
|
151 if 'GN=' in data_line:
|
|
152 lst = data_line.split('GN=')
|
|
153 lst2 = lst[1].split(' ')
|
|
154 genename = lst2[0]
|
|
155 if 'GN=' not in data_line:
|
|
156 genename = 'NA'
|
|
157 while ">sp" not in data_lines[match]:
|
|
158 if match <= db_len:
|
|
159 seqlength = seqlength + len(data_lines[match].strip())
|
|
160 match = match + 1
|
|
161 else:
|
|
162 break
|
|
163 return ReturnValue1(seqlength, genename)
|
|
164 count = count + 1
|
|
165
|
|
166
|
|
167 if seqlength == 0:
|
|
168 error.write(uniprot_accession_in + '\t' + "Uniprot not in Fasta" + '\n')
|
|
169 error.close
|
|
170 seqlength = 'NA'
|
|
171 genename = 'NA'
|
|
172 return ReturnValue1(seqlength, genename)
|
|
173
|
|
174
|
|
175 def readtab(infile):
|
|
176 with open(infile, 'r') as input_file:
|
|
177 # Read in tab-delim text file.
|
|
178 output = []
|
|
179 for input_line in input_file:
|
|
180 input_line = input_line.strip()
|
|
181 temp = input_line.split('\t')
|
|
182 output.append(temp)
|
|
183 return output
|
|
184
|
|
185
|
|
186 def read_MaxQuant(MaxQuant_input):
|
|
187 # Get data, proteins and header from MaxQuant output.
|
|
188 dupes = readtab(MaxQuant_input)
|
|
189 header_start = 0
|
|
190 header = dupes[header_start]
|
|
191 for var_MQ in header:
|
|
192 var_MQ = var_MQ.replace(r"\"", "")
|
|
193 var_MQ = var_MQ.replace(r"Intensity.", r"")
|
|
194 var_MQ = var_MQ.replace(r".", r"-")
|
|
195 data = dupes[header_start+1:len(dupes)]
|
|
196 # Cut off blank line and END OF FILE.
|
|
197 proteins = []
|
|
198 for protein in data:
|
|
199 proteins.append(protein[0])
|
|
200 return ReturnValue2(data, proteins, header)
|
|
201
|
|
202
|
|
203 def make_inter(MaxQuant_input):
|
|
204 bait = readtab(baitfile)
|
|
205 data = read_MaxQuant(MaxQuant_input).data
|
|
206 header = read_MaxQuant(MaxQuant_input).header
|
|
207 proteins = read_MaxQuant(MaxQuant_input).proteins
|
|
208 bait_index = []
|
|
209 for bait_item in bait:
|
|
210 bait_index.append(header.index("mapped_protein") + 1)
|
|
211 # Find just the baits defined in bait file.
|
|
212 with open('inter.txt', 'w') as y:
|
|
213 a = 0; l = 0
|
|
214 for bb in bait:
|
|
215 for lst in data:
|
|
216 y.write(header[bait_index[l]] + '\t' + bb[1] + '\t' + proteins[a] + '\t'
|
|
217 + lst[bait_index[l]] + '\n')
|
|
218 a += 1
|
|
219 if a == len(proteins):
|
|
220 a = 0; l += 1
|
|
221
|
|
222
|
|
223 def make_prey(MaxQuant_input):
|
|
224 proteins = read_MaxQuant(MaxQuant_input).proteins
|
|
225 output_file = open("prey.txt", 'w')
|
|
226 for a in proteins:
|
|
227 a = a.replace("\n", "")
|
|
228 # Remove \n for input into function.
|
|
229 a = a.replace("\r", "")
|
|
230 # Ditto for \r.
|
|
231 seq = get_info(a).seqlength
|
|
232 GN = get_info(a).genename
|
|
233 if seq != 'NA':
|
|
234 output_file.write(a+"\t"+str(seq)+ "\t" + str(GN) + "\n")
|
|
235 output_file.close()
|
|
236
|
|
237
|
|
238 def no_error_inter(MaxQuant_input):
|
|
239 # Remake inter file without protein errors from Uniprot.
|
|
240 err = readtab("error proteins.txt")
|
|
241 bait = readtab(baitfile)
|
|
242 data = read_MaxQuant(MaxQuant_input).data
|
|
243 header = read_MaxQuant(MaxQuant_input).header
|
|
244 header = [MQ_var.replace(r"\"", "") for MQ_var in header]
|
|
245 header = [MQ_var.replace(r"Intensity.", r"") for MQ_var in header]
|
|
246 header = [MQ_var.replace(r".", r"-") for MQ_var in header]
|
|
247 bait_index = []
|
|
248 for bait_item in bait:
|
|
249 bait_index.append(header.index(bait_item[0]))
|
|
250 proteins = read_MaxQuant(MaxQuant_input).proteins
|
|
251 errors = []
|
|
252 for e in err:
|
|
253 errors.append(e[0])
|
|
254 with open('inter.txt', 'w') as input_file:
|
|
255 l = 0; a = 0
|
|
256 for bb in bait:
|
|
257 for lst in data:
|
|
258 if proteins[a] not in errors:
|
|
259 input_file.write(header[bait_index[l]] + '\t' + bb[1] + '\t' + proteins[a] + '\t'
|
|
260 + lst[bait_index[l]] + '\n')
|
|
261 a += 1
|
|
262 if a == len(proteins):
|
|
263 l += 1; a = 0
|
|
264
|
|
265
|
|
266 def bait_check(bait, MaxQuant_input):
|
|
267 # Check that bait names share header titles.
|
|
268 bait_in = readtab(bait)
|
|
269 header = read_MaxQuant(MaxQuant_input).header
|
|
270 for bait in bait_in:
|
|
271 if bait[0] not in header:
|
|
272 sys.exit("Bait must share header titles with MaxQuant output")
|
|
273
|
|
274 if __name__ == '__main__':
|
|
275 main(infile, make_bait)
|