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"
|
16
|
56 bait_cache.append(str(bait_line))
|
4
|
57 i = i + 3
|
|
58
|
|
59 for cache_line in bait_cache:
|
|
60 bait_file_tmp.write(cache_line[1])
|
|
61
|
|
62 bait_file_tmp.close()
|
|
63
|
|
64 if bait_bool == 'false':
|
|
65 bait_create(make_bait, infile)
|
|
66 baitfile = "bait.txt"
|
|
67 else:
|
|
68 bait_temp_file = open(sys.argv[10], 'r')
|
|
69 bait_cache = bait_temp_file.readlines()
|
|
70 bait_file_tmp = open("bait.txt", "wr")
|
|
71 for cache_line in bait_cache:
|
|
72 bait_file_tmp.write(cache_line)
|
|
73 bait_file_tmp.close()
|
|
74 baitfile = "bait.txt"
|
|
75
|
|
76
|
|
77 class ReturnValue1(object):
|
|
78 def __init__(self, sequence, gene):
|
|
79 self.seqlength = sequence
|
|
80 self.genename = gene
|
|
81
|
|
82
|
|
83 class ReturnValue2(object):
|
|
84 def __init__(self, getdata, getproteins, getheader):
|
|
85 self.data = getdata
|
|
86 self.proteins = getproteins
|
|
87 self.header = getheader
|
|
88
|
|
89
|
|
90 def main(Scaffold_input, baits):
|
|
91 bait_check(baitfile, Scaffold_input)
|
|
92 make_inter(Scaffold_input)
|
|
93 if prey == 'true':
|
|
94 make_prey(Scaffold_input)
|
|
95 no_error_inter(Scaffold_input)
|
|
96 os.rename('prey.txt', sys.argv[5])
|
|
97 elif prey == 'false':
|
|
98 if os.path.isfile('error proteins.txt') == True:
|
|
99 no_error_inter(Scaffold_input)
|
|
100 pass
|
|
101 elif prey != 'true' or 'false':
|
|
102 sys.exit("Invalid Prey Argument: Y or N")
|
|
103
|
|
104
|
|
105 def get_info(uniprot_accession_in):
|
|
106 # Get aminoacid lengths and gene name.
|
|
107 error = open('error proteins.txt', 'a+')
|
|
108 data = open(fasta_db, 'r')
|
|
109 data_lines = data.readlines()
|
|
110 db_len = len(data_lines)
|
|
111 seqlength = 0
|
|
112 count = 0
|
|
113 for data_line in data_lines:
|
|
114 if ">sp" in data_line:
|
|
115 namer = data_line.split("|")[2]
|
|
116 if uniprot_accession_in == data_line.split("|")[1]:
|
|
117 match = count + 1
|
|
118 if 'GN=' in data_line:
|
10
|
119 lst = data_line.split('GN=')
|
4
|
120 lst2 = lst[1].split(' ')
|
|
121 genename = lst2[0]
|
|
122 if 'GN=' not in data_line:
|
|
123 genename = 'NA'
|
|
124 while ">sp" not in data_lines[match]:
|
|
125 if match <= db_len:
|
|
126 seqlength = seqlength + len(data_lines[match].strip())
|
|
127 match = match + 1
|
|
128 else:
|
|
129 break
|
|
130 return ReturnValue1(seqlength, genename)
|
10
|
131 if uniprot_accession_in == namer.split(" ")[0]:
|
4
|
132 match = count + 1
|
|
133 # Ensures consistent spacing throughout.
|
|
134 if 'GN=' in data_line:
|
|
135 lst = data_line.split('GN=')
|
|
136 lst2 = lst[1].split(' ')
|
|
137 genename = lst2[0]
|
|
138 if 'GN=' not in data_line:
|
|
139 genename = 'NA'
|
|
140 while ">sp" not in data_lines[match]:
|
|
141 if match <= db_len:
|
|
142 seqlength = seqlength + len(data_lines[match].strip())
|
|
143 match = match + 1
|
|
144 else:
|
|
145 break
|
|
146 return ReturnValue1(seqlength, genename)
|
|
147 count = count + 1
|
|
148 if seqlength == 0:
|
|
149 error.write(uniprot_accession_in + '\t' + "Uniprot not in Fasta" + '\n')
|
|
150 error.close
|
|
151 seqlength = 'NA'
|
|
152 genename = 'NA'
|
|
153 return ReturnValue1(seqlength, genename)
|
|
154
|
|
155
|
|
156 def readtab(infile):
|
|
157 with open(infile, 'r') as input_file:
|
|
158 # read in tab-delim text
|
|
159 output = []
|
|
160 for input_line in input_file:
|
|
161 input_line = input_line.strip()
|
|
162 temp = input_line.split('\t')
|
|
163 output.append(temp)
|
|
164 return output
|
|
165
|
|
166
|
|
167 def read_Scaffold(Scaffold_input):
|
|
168 # Get data, proteins and header from Scaffold output
|
|
169 dupes = readtab(Scaffold_input)
|
|
170 cnt = 0
|
|
171 for Scaffold_line in dupes:
|
|
172 cnt += 1
|
|
173 if Scaffold_line[0] == '#':
|
|
174 # Finds the start of second header.
|
|
175 header_start = cnt-1
|
|
176 header = dupes[header_start]
|
|
177 prot_start = header.index("Accession Number")
|
|
178 data = dupes[header_start+1:len(dupes)-2]
|
|
179 # Cut off blank line and END OF FILE.
|
|
180 proteins = []
|
|
181 for Scaffold_line in data:
|
|
182 Scaffold_line[4] = Scaffold_line[4].split()[0]
|
|
183 # Removes the (+##) that sometimes is attached.
|
11
|
184 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
|
185 for protein in data:
|
11
|
186 prot_id = uniprot_re.match(protein[prot_start])
|
12
|
187 if prot_id:
|
|
188 proteins.append(prot_id.group())
|
|
189 else:
|
|
190 prot_ids = protein[prot_start].split("|")
|
|
191 for prot_id in prot_ids:
|
|
192 if "_HUMAN" in prot_id:
|
|
193 proteins.append(prot_id)
|
|
194 elif "_YEAST" in prot_id:
|
|
195 proteins.append(prot_id)
|
|
196 elif "_MOUSE" in prot_id:
|
|
197 proteins.append(prot_id)
|
13
|
198 else:
|
14
|
199 print "Accession must be uniprot ID or gene"
|
4
|
200 return ReturnValue2(data, proteins, header)
|
|
201
|
|
202
|
|
203 def make_inter(Scaffold_input):
|
|
204 bait = readtab(baitfile)
|
|
205 data = read_Scaffold(Scaffold_input).data
|
|
206 header = read_Scaffold(Scaffold_input).header
|
|
207 proteins = read_Scaffold(Scaffold_input).proteins
|
|
208 bait_index = []
|
|
209 for bait_line in bait:
|
|
210 bait_index.append(header.index(bait_line[0]))
|
|
211 # Find just the baits defined in bait file.
|
|
212 with open('inter.txt', 'w') as inter_file:
|
|
213 a = 0; l = 0
|
|
214 for bb in bait:
|
|
215 for lst in data:
|
|
216 inter_file.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(Scaffold_input):
|
|
224 proteins = read_Scaffold(Scaffold_input).proteins
|
|
225 output_file = open("prey.txt", 'w')
|
|
226 for protein in proteins:
|
|
227 protein = protein.replace("\n", "")
|
|
228 # Remove \n for input into function.
|
|
229 protein = protein.replace("\r", "")
|
|
230 # Ditto for \r.
|
|
231 seq = get_info(protein).seqlength
|
|
232 GN = get_info(protein).genename
|
|
233 if seq != 'NA':
|
|
234 output_file.write(protein + "\t" + str(seq) + "\t" + str(GN) + "\n")
|
|
235 output_file.close()
|
|
236
|
|
237
|
|
238 def no_error_inter(Scaffold_input):
|
|
239 # Remake inter file without protein errors from Uniprot.
|
|
240 err = readtab("error proteins.txt")
|
|
241 bait = readtab(baitfile)
|
|
242 data = read_Scaffold(Scaffold_input).data
|
|
243 header = read_Scaffold(Scaffold_input).header
|
|
244 bait_index = []
|
|
245 for bait_line in bait:
|
|
246 bait_index.append(header.index(bait_line[0]))
|
|
247 proteins = read_Scaffold(Scaffold_input).proteins
|
|
248 errors = []
|
|
249 for e in err:
|
|
250 errors.append(e[0])
|
|
251 with open('inter.txt', 'w') as y:
|
|
252 l = 0; a = 0
|
|
253 for bb in bait:
|
|
254 for lst in data:
|
|
255 if proteins[a] not in errors:
|
|
256 y.write(header[bait_index[l]] + '\t' + bb[1] + '\t' + proteins[a] + '\t'
|
|
257 + lst[bait_index[l]] + '\n')
|
|
258 a += 1
|
|
259 if a == len(proteins):
|
|
260 l += 1; a = 0
|
|
261
|
|
262
|
|
263 def bait_check(bait, Scaffold_input):
|
|
264 # Check that bait names share Scaffold header titles.
|
|
265 bait_in = readtab(bait)
|
|
266 header = read_Scaffold(Scaffold_input).header
|
|
267 for i in bait_in:
|
|
268 if i[0] not in header:
|
|
269 sys.exit("Bait must share header titles with Scaffold output")
|
|
270
|
|
271 if __name__ == '__main__':
|
|
272 main(infile, baitfile)
|
|
273
|
|
274 os.rename("inter.txt", sys.argv[4])
|
|
275 os.rename("bait.txt", sys.argv[7])
|