comparison SAINT_preprocessing.py @ 4:019e60bd3f7f draft

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