annotate SAINT_preprocessing_v6_mq_pep.py @ 28:5be5c9c81bda draft

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