8
|
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 import sys
|
|
10 import urllib2
|
|
11 import os.path
|
|
12 #######################################################################################
|
|
13 ## REQUIRED INPUT ##
|
|
14
|
|
15 # 1) infile: Scaffold "Samples Report" output
|
|
16 # 2) baitfile: SAINT formatted bait file generated in Galaxy
|
|
17 # 3) prey: Y or N for generating a prey file (requires internet connection)
|
|
18 #######################################################################################
|
|
19 infile = sys.argv[1] #Scaffold "Samples Report" output
|
|
20 prey = sys.argv[2] # Y or N
|
|
21 fasta_db = sys.argv[3]
|
|
22 tool_path = sys.argv[8]
|
|
23 if fasta_db == "None":
|
|
24 fasta_db = str(tool_path) + "/SwissProt_HUMAN_2014_08.fasta"
|
13
|
25 make_bait= sys.argv[6]
|
8
|
26
|
|
27
|
|
28 baits = make_bait.split()
|
|
29 i = 0
|
|
30 bait_file_tmp = open("bait.txt", "wr")
|
|
31 order = []
|
|
32 bait_cache = []
|
|
33
|
|
34 while i < len(baits):
|
|
35 if baits[i+2] == "true":
|
|
36 T_C = "C"
|
|
37 else:
|
|
38 T_C = "T"
|
|
39 line1 = baits[i] + "\t" + baits[i+1] + "\t" + T_C + "\n"
|
|
40 q = open(infile,"r")
|
|
41 for line2 in q:
|
|
42 line2 = line2.strip()
|
|
43 temp = line2.split('\t')
|
|
44 if "Quantitative Variance" in str(temp):
|
|
45 if baits[i] in temp:
|
|
46 number_bait = temp.index(str(baits[i]))
|
|
47 number_bait = number_bait - 9
|
|
48 bait_cache.append((number_bait, str(line1)))
|
|
49 else:
|
|
50 print "Error: bad bait " + str(baits[i])
|
|
51 sys.exit()
|
|
52 else:
|
|
53 pass
|
|
54 i = i + 3
|
|
55
|
|
56 bait_cache.sort()
|
|
57 for line in bait_cache:
|
|
58 bait_file_tmp.write(line[1])
|
|
59
|
|
60 bait_file_tmp.close()
|
|
61 baitfile = "bait.txt"
|
|
62
|
|
63 class ReturnValue1(object):
|
|
64 def __init__(self, sequence, gene):
|
|
65 self.seqlength = sequence
|
|
66 self.genename = gene
|
|
67 class ReturnValue2(object):
|
|
68 def __init__(self, getdata, getproteins, getheader):
|
|
69 self.data = getdata
|
|
70 self.proteins = getproteins
|
|
71 self.header = getheader
|
|
72
|
|
73 def main(scaffold_input, baitfile):
|
|
74 bait_check(baitfile, scaffold_input)
|
|
75 make_inter(scaffold_input)
|
|
76 if prey == 'true':
|
|
77 make_prey(scaffold_input)
|
|
78 no_error_inter(scaffold_input)
|
|
79 os.rename('prey.txt', sys.argv[5])
|
|
80 elif prey == 'false':
|
|
81 if os.path.isfile('error proteins.txt') == True:
|
|
82 no_error_inter(scaffold_input)
|
|
83 pass
|
|
84 elif prey != 'true' or 'false':
|
|
85 sys.exit("Invalid Prey Argument: Y or N")
|
|
86
|
|
87 def get_info(uniprot_accession_in): # get aa lengths and gene name
|
|
88 error = open('error proteins.txt', 'a+')
|
|
89 # while True:
|
|
90 # i = 0
|
|
91 # try:
|
|
92 # data = urllib2.urlopen("http://www.uniprot.org/uniprot/" + uniprot_accession_in + ".fasta")
|
|
93 # break
|
|
94 # except urllib2.HTTPError, err:
|
|
95 # i = i + 1
|
|
96 # if i == 50:
|
|
97 # sys.exit("More than 50 errors. Check your file or try again later.")
|
|
98 # if err.code == 404:
|
|
99 # error.write(uniprot_accession_in + '\t' + "Invalid URL. Check protein" + '\n')
|
|
100 # seqlength = 'NA'
|
|
101 # genename = 'NA'
|
|
102 # return ReturnValue1(seqlength, genename)
|
|
103 # elif err.code == 302:
|
|
104 # sys.exit("Request timed out. Check connection and try again.")
|
|
105 # else:
|
|
106 # sys.exit("Uniprot had some other error")
|
|
107 # lines = data.readlines()
|
|
108 # if lines == []:
|
|
109 # error.write(uniprot_accession_in + '\t' + "Blank Fasta" + '\n')
|
|
110 # error.close
|
|
111 # seqlength = 'NA'
|
|
112 # genename = 'NA'
|
|
113 # return ReturnValue1(seqlength, genename)
|
|
114 # if lines != []:
|
|
115 # seqlength = 0
|
|
116 # header = lines[0]
|
|
117 # for line in lines[1:]:
|
|
118 # line = line.replace("\n","") # strip \n or else it gets counted in the length
|
|
119 # seqlength += len(line)
|
|
120 # if 'GN=' in header:
|
|
121 # lst = header.split('GN=')
|
|
122 # lst2 = lst[1].split(' ')
|
|
123 # genename = lst2[0]
|
|
124 # error.close
|
|
125 # return ReturnValue1(seqlength, genename)
|
|
126 # if 'GN=' not in header:
|
|
127 # genename = 'NA'
|
|
128 # error.close
|
|
129 # return ReturnValue1(seqlength, genename)
|
|
130 data = open(fasta_db,'r')
|
|
131 lines = data.readlines()
|
|
132 db_len = len(lines)
|
|
133 seqlength = 0
|
|
134 count = 0
|
|
135 for i in lines:
|
|
136 if ">sp" in i:
|
17
|
137 namer = i.split("|")[2]
|
8
|
138 if uniprot_accession_in == i.split("|")[1]:
|
|
139 match = count+1
|
|
140 if 'GN=' in i:
|
|
141 lst = i.split('GN=')
|
|
142 lst2 = lst[1].split(' ')
|
|
143 genename = lst2[0]
|
|
144 if 'GN=' not in i:
|
|
145 genename = 'NA'
|
|
146 while ">sp" not in lines[match]:
|
|
147 if match <= db_len:
|
|
148 seqlength = seqlength + len(lines[match].strip())
|
|
149 match = match + 1
|
|
150 else:
|
|
151 break
|
|
152 return ReturnValue1(seqlength, genename)
|
17
|
153 elif uniprot_accession_in == namer.split(" "):
|
|
154 match = count+1
|
|
155 if 'GN=' in i:
|
|
156 lst = i.split('GN=')
|
|
157 lst2 = lst[1].split(' ')
|
|
158 genename = lst2[0]
|
|
159 if 'GN=' not in i:
|
|
160 genename = 'NA'
|
|
161 while ">sp" not in lines[match]:
|
|
162 if match <= db_len:
|
|
163 seqlength = seqlength + len(lines[match].strip())
|
|
164 match = match + 1
|
|
165 else:
|
|
166 break
|
|
167 return ReturnValue1(seqlength, genename)
|
8
|
168 count = count + 1
|
|
169
|
|
170
|
|
171 if seqlength == 0:
|
|
172 error.write(uniprot_accession_in + '\t' + "Uniprot not in Fasta" + '\n')
|
|
173 error.close
|
|
174 seqlength = 'NA'
|
|
175 genename = 'NA'
|
|
176 return ReturnValue1(seqlength, genename)
|
|
177
|
|
178 def readtab(infile):
|
|
179 with open(infile,'r') as x: # read in tab-delim text
|
|
180 output = []
|
|
181 for line in x:
|
|
182 line = line.strip()
|
|
183 temp = line.split('\t')
|
|
184 output.append(temp)
|
|
185 return output
|
|
186 def read_scaffold(scaffold_input): # Get data, proteins and header from scaffold output
|
|
187 dupes = readtab(scaffold_input)
|
|
188 cnt = 0
|
|
189 for i in dupes:
|
|
190 cnt += 1
|
|
191 if i[0] == '#': # 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] # cut off blank line and END OF FILE
|
|
196 proteins = []
|
|
197 for i in data:
|
|
198 i[4] = i[4].split()[0] # removes the (+##) that sometimes is attached
|
|
199 for protein in data:
|
|
200 proteins.append(protein[prot_start])
|
|
201 return ReturnValue2(data, proteins, header)
|
|
202 def make_inter(scaffold_input):
|
|
203 bait = readtab(baitfile)
|
|
204 data = read_scaffold(scaffold_input).data
|
|
205 header = read_scaffold(scaffold_input).header
|
|
206 proteins = read_scaffold(scaffold_input).proteins
|
|
207 bait_index = []
|
|
208 for i in bait:
|
|
209 bait_index.append(header.index(i[0])) # Find just the baits defined in bait file
|
|
210 with open('inter.txt', 'w') as y:
|
|
211 a = 0; l=0
|
|
212 for bb in bait:
|
|
213 for lst in data:
|
|
214 y.write(header[bait_index[l]] + '\t' + bb[1] + '\t' + proteins[a] + '\t' + lst[bait_index[l]] + '\n')
|
|
215 a+=1
|
|
216 if a == len(proteins):
|
|
217 a = 0; l+=1
|
|
218 def make_prey(scaffold_input):
|
|
219 proteins = read_scaffold(scaffold_input).proteins
|
|
220 output_file = open("prey.txt",'w')
|
|
221 for a in proteins:
|
|
222 a = a.replace("\n","") # remove \n for input into function
|
|
223 a = a.replace("\r","") # ditto for \r
|
|
224 seq = get_info(a).seqlength
|
|
225 GN = get_info(a).genename
|
|
226 if seq != 'NA':
|
|
227 output_file.write(a+"\t"+str(seq)+ "\t" + str(GN) + "\n")
|
|
228 output_file.close()
|
|
229 def no_error_inter(scaffold_input): # remake inter file without protein errors from Uniprot
|
|
230 err = readtab("error proteins.txt")
|
|
231 bait = readtab(baitfile)
|
|
232 data = read_scaffold(scaffold_input).data
|
|
233 header = read_scaffold(scaffold_input).header
|
|
234 bait_index = []
|
|
235 for i in bait:
|
|
236 bait_index.append(header.index(i[0]))
|
|
237 proteins = read_scaffold(scaffold_input).proteins
|
|
238 errors = []
|
|
239 for e in err:
|
|
240 errors.append(e[0])
|
|
241 with open('inter.txt', 'w') as y:
|
|
242 l = 0; a = 0
|
|
243 for bb in bait:
|
|
244 for lst in data:
|
|
245 if proteins[a] not in errors:
|
|
246 y.write(header[bait_index[l]] + '\t' + bb[1] + '\t' + proteins[a] + '\t' + lst[bait_index[l]] + '\n')
|
|
247 a+=1
|
|
248 if a == len(proteins):
|
|
249 l += 1; a = 0
|
|
250 def bait_check(bait, scaffold_input): # check that bait names share header titles
|
|
251 bait_in = readtab(bait)
|
|
252 header = read_scaffold(scaffold_input).header
|
|
253 for i in bait_in:
|
|
254 if i[0] not in header:
|
|
255 sys.exit("Bait must share header titles with Scaffold output")
|
|
256
|
|
257 if __name__ == '__main__':
|
|
258 main(infile, baitfile)
|
|
259
|
17
|
260 os.rename("inter.txt", sys.argv[4])
|
8
|
261 os.rename("bait.txt", sys.argv[7])
|