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')
|
|
60 if "Quantitative Variance" in str(temp):
|
|
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}")
|
|
202 for protein in data:
|
|
203 prot_id = uniprot_re.match(protein[prot_start])
|
|
204 proteins.append(prot_id.group())
|
4
|
205 return ReturnValue2(data, proteins, header)
|
|
206
|
|
207
|
|
208 def make_inter(Scaffold_input):
|
|
209 bait = readtab(baitfile)
|
|
210 data = read_Scaffold(Scaffold_input).data
|
|
211 header = read_Scaffold(Scaffold_input).header
|
|
212 proteins = read_Scaffold(Scaffold_input).proteins
|
|
213 bait_index = []
|
|
214 for bait_line in bait:
|
|
215 bait_index.append(header.index(bait_line[0]))
|
|
216 # Find just the baits defined in bait file.
|
|
217 with open('inter.txt', 'w') as inter_file:
|
|
218 a = 0; l = 0
|
|
219 for bb in bait:
|
|
220 for lst in data:
|
|
221 inter_file.write(header[bait_index[l]] + '\t' + bb[1] + '\t' + proteins[a] + '\t'
|
|
222 + lst[bait_index[l]] + '\n')
|
|
223 a += 1
|
|
224 if a == len(proteins):
|
|
225 a = 0; l += 1
|
|
226
|
|
227
|
|
228 def make_prey(Scaffold_input):
|
|
229 proteins = read_Scaffold(Scaffold_input).proteins
|
|
230 output_file = open("prey.txt", 'w')
|
|
231 for protein in proteins:
|
|
232 protein = protein.replace("\n", "")
|
|
233 # Remove \n for input into function.
|
|
234 protein = protein.replace("\r", "")
|
|
235 # Ditto for \r.
|
|
236 seq = get_info(protein).seqlength
|
|
237 GN = get_info(protein).genename
|
|
238 if seq != 'NA':
|
|
239 output_file.write(protein + "\t" + str(seq) + "\t" + str(GN) + "\n")
|
|
240 output_file.close()
|
|
241
|
|
242
|
|
243 def no_error_inter(Scaffold_input):
|
|
244 # Remake inter file without protein errors from Uniprot.
|
|
245 err = readtab("error proteins.txt")
|
|
246 bait = readtab(baitfile)
|
|
247 data = read_Scaffold(Scaffold_input).data
|
|
248 header = read_Scaffold(Scaffold_input).header
|
|
249 bait_index = []
|
|
250 for bait_line in bait:
|
|
251 bait_index.append(header.index(bait_line[0]))
|
|
252 proteins = read_Scaffold(Scaffold_input).proteins
|
|
253 errors = []
|
|
254 for e in err:
|
|
255 errors.append(e[0])
|
|
256 with open('inter.txt', 'w') as y:
|
|
257 l = 0; a = 0
|
|
258 for bb in bait:
|
|
259 for lst in data:
|
|
260 if proteins[a] not in errors:
|
|
261 y.write(header[bait_index[l]] + '\t' + bb[1] + '\t' + proteins[a] + '\t'
|
|
262 + lst[bait_index[l]] + '\n')
|
|
263 a += 1
|
|
264 if a == len(proteins):
|
|
265 l += 1; a = 0
|
|
266
|
|
267
|
|
268 def bait_check(bait, Scaffold_input):
|
|
269 # Check that bait names share Scaffold header titles.
|
|
270 bait_in = readtab(bait)
|
|
271 header = read_Scaffold(Scaffold_input).header
|
|
272 for i in bait_in:
|
|
273 if i[0] not in header:
|
|
274 sys.exit("Bait must share header titles with Scaffold output")
|
|
275
|
|
276 if __name__ == '__main__':
|
|
277 main(infile, baitfile)
|
|
278
|
|
279 os.rename("inter.txt", sys.argv[4])
|
|
280 os.rename("bait.txt", sys.argv[7])
|