4
|
1 #######################################################################################
|
|
2 # Python-code: SAINT pre-processing from Scaffold "Samples Report" output
|
|
3 # Author: Brent Kuenzi
|
|
4 #######################################################################################
|
|
5 # This program reads in a raw Scaffold "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: Scaffold "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 import sys
|
|
27 import os.path
|
11
|
28 import re
|
4
|
29
|
|
30
|
|
31 infile = sys.argv[1]
|
|
32 #Scaffold "Samples Report" output.
|
|
33 prey = sys.argv[2]
|
|
34 # Y or N boolean from Galaxy.
|
|
35 fasta_db = sys.argv[3]
|
|
36 tool_path = sys.argv[8]
|
|
37 if fasta_db == "None":
|
|
38 fasta_db = str(tool_path) + "/SwissProt_HUMAN_2014_08.fasta"
|
|
39 make_bait = sys.argv[6]
|
|
40 bait_bool = sys.argv[9]
|
|
41
|
|
42
|
|
43 def bait_create(baits, infile):
|
|
44 # Verifies the Baits are valid in the Scaffold file and writes the Bait.txt.
|
|
45 baits = make_bait.split()
|
|
46 i = 0
|
|
47 bait_file_tmp = open("bait.txt", "w")
|
|
48 order = []
|
|
49 bait_cache = []
|
|
50 while i < len(baits):
|
|
51 if baits[i+2] == "true":
|
|
52 T_C = "C"
|
|
53 else:
|
|
54 T_C = "T"
|
|
55 bait_line = baits[i] + "\t" + baits[i+1] + "\t" + T_C + "\n"
|
|
56 read_infile = open(infile, "r")
|
|
57 for input_line in read_infile:
|
|
58 input_line = input_line.strip()
|
|
59 temp = input_line.split('\t')
|
13
|
60 if "Accession Number" in str(temp):
|
4
|
61 if baits[i] in temp:
|
|
62 number_bait = temp.index(str(baits[i]))
|
|
63 number_bait = number_bait - 9
|
|
64 bait_cache.append((number_bait, str(bait_line)))
|
|
65 # Locates the Bait names in the column names and then sets the Baits in the
|
|
66 # correct order in the cache thus the - 9 because the baits start at the 9th
|
|
67 # column.
|
|
68 else:
|
|
69 print "Error: bad bait " + str(baits[i])
|
|
70 sys.exit()
|
|
71 else:
|
|
72 pass
|
|
73 i = i + 3
|
|
74
|
|
75 bait_cache.sort()
|
|
76 for cache_line in bait_cache:
|
|
77 bait_file_tmp.write(cache_line[1])
|
|
78
|
|
79 bait_file_tmp.close()
|
|
80
|
|
81 if bait_bool == 'false':
|
|
82 bait_create(make_bait, infile)
|
|
83 baitfile = "bait.txt"
|
|
84 else:
|
|
85 bait_temp_file = open(sys.argv[10], 'r')
|
|
86 bait_cache = bait_temp_file.readlines()
|
|
87 bait_file_tmp = open("bait.txt", "wr")
|
|
88 for cache_line in bait_cache:
|
|
89 bait_file_tmp.write(cache_line)
|
|
90 bait_file_tmp.close()
|
|
91 baitfile = "bait.txt"
|
|
92
|
|
93
|
|
94 class ReturnValue1(object):
|
|
95 def __init__(self, sequence, gene):
|
|
96 self.seqlength = sequence
|
|
97 self.genename = gene
|
|
98
|
|
99
|
|
100 class ReturnValue2(object):
|
|
101 def __init__(self, getdata, getproteins, getheader):
|
|
102 self.data = getdata
|
|
103 self.proteins = getproteins
|
|
104 self.header = getheader
|
|
105
|
|
106
|
|
107 def main(Scaffold_input, baits):
|
|
108 bait_check(baitfile, Scaffold_input)
|
|
109 make_inter(Scaffold_input)
|
|
110 if prey == 'true':
|
|
111 make_prey(Scaffold_input)
|
|
112 no_error_inter(Scaffold_input)
|
|
113 os.rename('prey.txt', sys.argv[5])
|
|
114 elif prey == 'false':
|
|
115 if os.path.isfile('error proteins.txt') == True:
|
|
116 no_error_inter(Scaffold_input)
|
|
117 pass
|
|
118 elif prey != 'true' or 'false':
|
|
119 sys.exit("Invalid Prey Argument: Y or N")
|
|
120
|
|
121
|
|
122 def get_info(uniprot_accession_in):
|
|
123 # Get aminoacid lengths and gene name.
|
|
124 error = open('error proteins.txt', 'a+')
|
|
125 data = open(fasta_db, 'r')
|
|
126 data_lines = data.readlines()
|
|
127 db_len = len(data_lines)
|
|
128 seqlength = 0
|
|
129 count = 0
|
|
130 for data_line in data_lines:
|
|
131 if ">sp" in data_line:
|
|
132 namer = data_line.split("|")[2]
|
|
133 if uniprot_accession_in == data_line.split("|")[1]:
|
|
134 match = count + 1
|
|
135 if 'GN=' in data_line:
|
10
|
136 lst = data_line.split('GN=')
|
4
|
137 lst2 = lst[1].split(' ')
|
|
138 genename = lst2[0]
|
|
139 if 'GN=' not in data_line:
|
|
140 genename = 'NA'
|
|
141 while ">sp" not in data_lines[match]:
|
|
142 if match <= db_len:
|
|
143 seqlength = seqlength + len(data_lines[match].strip())
|
|
144 match = match + 1
|
|
145 else:
|
|
146 break
|
|
147 return ReturnValue1(seqlength, genename)
|
10
|
148 if uniprot_accession_in == namer.split(" ")[0]:
|
4
|
149 match = count + 1
|
|
150 # Ensures consistent spacing throughout.
|
|
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 if seqlength == 0:
|
|
166 error.write(uniprot_accession_in + '\t' + "Uniprot not in Fasta" + '\n')
|
|
167 error.close
|
|
168 seqlength = 'NA'
|
|
169 genename = 'NA'
|
|
170 return ReturnValue1(seqlength, genename)
|
|
171
|
|
172
|
|
173 def readtab(infile):
|
|
174 with open(infile, 'r') as input_file:
|
|
175 # read in tab-delim text
|
|
176 output = []
|
|
177 for input_line in input_file:
|
|
178 input_line = input_line.strip()
|
|
179 temp = input_line.split('\t')
|
|
180 output.append(temp)
|
|
181 return output
|
|
182
|
|
183
|
|
184 def read_Scaffold(Scaffold_input):
|
|
185 # Get data, proteins and header from Scaffold output
|
|
186 dupes = readtab(Scaffold_input)
|
|
187 cnt = 0
|
|
188 for Scaffold_line in dupes:
|
|
189 cnt += 1
|
|
190 if Scaffold_line[0] == '#':
|
|
191 # Finds the start of second header.
|
|
192 header_start = cnt-1
|
|
193 header = dupes[header_start]
|
|
194 prot_start = header.index("Accession Number")
|
|
195 data = dupes[header_start+1:len(dupes)-2]
|
|
196 # Cut off blank line and END OF FILE.
|
|
197 proteins = []
|
|
198 for Scaffold_line in data:
|
|
199 Scaffold_line[4] = Scaffold_line[4].split()[0]
|
|
200 # Removes the (+##) that sometimes is attached.
|
11
|
201 uniprot_re = re.compile("[OPQ][0-9][A-Z0-9]{3}[0-9]|[A-NR-Z][0-9]([A-Z][A-Z0-9]{2}[0-9]){1,2}")
|
12
|
202 for protein in data:
|
11
|
203 prot_id = uniprot_re.match(protein[prot_start])
|
12
|
204 if prot_id:
|
|
205 proteins.append(prot_id.group())
|
|
206 else:
|
|
207 prot_ids = protein[prot_start].split("|")
|
|
208 for prot_id in prot_ids:
|
|
209 if "_HUMAN" in prot_id:
|
|
210 proteins.append(prot_id)
|
|
211 elif "_YEAST" in prot_id:
|
|
212 proteins.append(prot_id)
|
|
213 elif "_MOUSE" in prot_id:
|
|
214 proteins.append(prot_id)
|
13
|
215 else:
|
12
|
216 print "Accession must be uniprot ID or gene name"
|
4
|
217 return ReturnValue2(data, proteins, header)
|
|
218
|
|
219
|
|
220 def make_inter(Scaffold_input):
|
|
221 bait = readtab(baitfile)
|
|
222 data = read_Scaffold(Scaffold_input).data
|
|
223 header = read_Scaffold(Scaffold_input).header
|
|
224 proteins = read_Scaffold(Scaffold_input).proteins
|
|
225 bait_index = []
|
|
226 for bait_line in bait:
|
|
227 bait_index.append(header.index(bait_line[0]))
|
|
228 # Find just the baits defined in bait file.
|
|
229 with open('inter.txt', 'w') as inter_file:
|
|
230 a = 0; l = 0
|
|
231 for bb in bait:
|
|
232 for lst in data:
|
|
233 inter_file.write(header[bait_index[l]] + '\t' + bb[1] + '\t' + proteins[a] + '\t'
|
|
234 + lst[bait_index[l]] + '\n')
|
|
235 a += 1
|
|
236 if a == len(proteins):
|
|
237 a = 0; l += 1
|
|
238
|
|
239
|
|
240 def make_prey(Scaffold_input):
|
|
241 proteins = read_Scaffold(Scaffold_input).proteins
|
|
242 output_file = open("prey.txt", 'w')
|
|
243 for protein in proteins:
|
|
244 protein = protein.replace("\n", "")
|
|
245 # Remove \n for input into function.
|
|
246 protein = protein.replace("\r", "")
|
|
247 # Ditto for \r.
|
|
248 seq = get_info(protein).seqlength
|
|
249 GN = get_info(protein).genename
|
|
250 if seq != 'NA':
|
|
251 output_file.write(protein + "\t" + str(seq) + "\t" + str(GN) + "\n")
|
|
252 output_file.close()
|
|
253
|
|
254
|
|
255 def no_error_inter(Scaffold_input):
|
|
256 # Remake inter file without protein errors from Uniprot.
|
|
257 err = readtab("error proteins.txt")
|
|
258 bait = readtab(baitfile)
|
|
259 data = read_Scaffold(Scaffold_input).data
|
|
260 header = read_Scaffold(Scaffold_input).header
|
|
261 bait_index = []
|
|
262 for bait_line in bait:
|
|
263 bait_index.append(header.index(bait_line[0]))
|
|
264 proteins = read_Scaffold(Scaffold_input).proteins
|
|
265 errors = []
|
|
266 for e in err:
|
|
267 errors.append(e[0])
|
|
268 with open('inter.txt', 'w') as y:
|
|
269 l = 0; a = 0
|
|
270 for bb in bait:
|
|
271 for lst in data:
|
|
272 if proteins[a] not in errors:
|
|
273 y.write(header[bait_index[l]] + '\t' + bb[1] + '\t' + proteins[a] + '\t'
|
|
274 + lst[bait_index[l]] + '\n')
|
|
275 a += 1
|
|
276 if a == len(proteins):
|
|
277 l += 1; a = 0
|
|
278
|
|
279
|
|
280 def bait_check(bait, Scaffold_input):
|
|
281 # Check that bait names share Scaffold header titles.
|
|
282 bait_in = readtab(bait)
|
|
283 header = read_Scaffold(Scaffold_input).header
|
|
284 for i in bait_in:
|
|
285 if i[0] not in header:
|
|
286 sys.exit("Bait must share header titles with Scaffold output")
|
|
287
|
|
288 if __name__ == '__main__':
|
|
289 main(infile, baitfile)
|
|
290
|
|
291 os.rename("inter.txt", sys.argv[4])
|
|
292 os.rename("bait.txt", sys.argv[7])
|