0
|
1 import sys
|
|
2 import os
|
|
3 from time import sleep
|
|
4
|
|
5 files = sys.argv[1] # read in a string of file names seperated by ", "
|
8
|
6 print files
|
0
|
7 # e.g. "Default_Protein_Report.txt, Default_Protein_Report_2.txt"
|
|
8 #bait = sys.argv[2] # SAINT formatted bait file
|
|
9 # still need a way to match files to bait identifiers
|
|
10 # or they can just be required to be put in the order of the bait file
|
|
11 quant_type = sys.argv[3] # what metric to use for quantification
|
9
|
12 print quant_type
|
0
|
13 # "#Validated Peptides", "#Peptides", "#Unique", "#Validated PSMs", "#PSMs"
|
|
14 db = sys.argv[4] # fasta database used in SearchGUI and PeptideShaker
|
9
|
15 print db
|
0
|
16 prey = sys.argv[5]
|
9
|
17 print prey
|
0
|
18 tool_path = sys.argv[7]
|
9
|
19 print tool_path
|
5
|
20 if db == "None":
|
|
21 db = str(tool_path) + "/SwissProt_HUMAN_2015_12.fasta"
|
0
|
22 make_bait = sys.argv[6]
|
9
|
23 print make_bait
|
0
|
24 bait_bool = sys.argv[8]
|
9
|
25 print bait_bool
|
0
|
26
|
|
27 def bait_create(baits, infile):
|
|
28 # Verifies the Baits are valid in the Scaffold file and writes the Bait.txt.
|
|
29 baits = make_bait.split()
|
|
30 i = 0
|
|
31 bait_file_tmp = open("bait.txt", "w")
|
|
32 order = []
|
|
33 bait_cache = []
|
|
34 while i < len(baits):
|
|
35 if baits[i+2] == "true":
|
|
36 T_C = "C"
|
|
37 else:
|
|
38 T_C = "T"
|
|
39 bait_line = baits[i] + "\t" + baits[i+1] + "\t" + T_C + "\n"
|
|
40 bait_cache.append(str(bait_line))
|
|
41 i = i + 3
|
|
42
|
|
43 for cache_line in bait_cache:
|
|
44 bait_file_tmp.write(cache_line)
|
|
45
|
|
46 bait_file_tmp.close()
|
|
47
|
|
48 if bait_bool == 'false':
|
|
49 bait_create(make_bait, infile)
|
|
50 bait = "bait.txt"
|
|
51 else:
|
|
52 bait_temp_file = open(sys.argv[9], 'r')
|
|
53 bait_cache = bait_temp_file.readlines()
|
|
54 bait_file_tmp = open("bait.txt", "wr")
|
|
55 for cache_line in bait_cache:
|
|
56 bait_file_tmp.write(cache_line)
|
|
57 bait_file_tmp.close()
|
|
58 bait = "bait.txt"
|
|
59
|
|
60 class ReturnValue1(object):
|
|
61 def __init__(self, sequence, gene):
|
|
62 self.seqlength = sequence
|
|
63 self.genename = gene
|
|
64
|
|
65 def read_tab(infile):
|
|
66 with open(infile,'r') as x:
|
|
67 output = []
|
|
68 for line in x:
|
|
69 line = line.strip()
|
|
70 temp = line.split('\t')
|
|
71 output.append(temp)
|
|
72 return output
|
|
73 def printProgress (iteration, total, prefix = '', suffix = '', decimals = 1, barLength = 100):
|
|
74 """
|
|
75 Call in a loop to create terminal progress bar
|
|
76 @params:
|
|
77 iteration - Required : current iteration (Int)
|
|
78 total - Required : total iterations (Int)
|
|
79 prefix - Optional : prefix string (Str)
|
|
80 suffix - Optional : suffix string (Str)
|
|
81 decimals - Optional : positive number of decimals in percent complete (Int)
|
|
82 barLength - Optional : character length of bar (Int)
|
|
83 """
|
|
84 formatStr = "{0:." + str(decimals) + "f}"
|
|
85 percents = formatStr.format(100 * (iteration / float(total)))
|
|
86 filledLength = int(round(barLength * iteration / float(total)))
|
|
87 bar = '=' * filledLength + '-' * (barLength - filledLength)
|
|
88 sys.stdout.write('\r%s |%s| %s%s %s' % (prefix, bar, percents, '%', suffix)),
|
|
89 sys.stdout.flush()
|
|
90 if iteration == total:
|
|
91 sys.stdout.write('\n')
|
|
92 sys.stdout.flush()
|
|
93 def get_info(uniprot_accession_in,fasta_db):
|
|
94 # Get aminoacid lengths and gene name.
|
|
95 error = open('error proteins.txt', 'a+')
|
|
96 data = open(fasta_db, 'r')
|
|
97 data_lines = data.readlines()
|
|
98 db_len = len(data_lines)
|
|
99 seqlength = 0
|
|
100 count = 0
|
|
101 for data_line in data_lines:
|
|
102 if ">sp" in data_line:
|
|
103 namer = data_line.split("|")[2]
|
|
104 if uniprot_accession_in == data_line.split("|")[1]:
|
|
105 match = count + 1
|
|
106 if 'GN=' in data_line:
|
|
107 lst = data_line.split('GN=')
|
|
108 lst2 = lst[1].split(' ')
|
|
109 genename = lst2[0]
|
|
110 if 'GN=' not in data_line:
|
|
111 genename = 'NA'
|
|
112 while ">sp" not in data_lines[match]:
|
|
113 if match <= db_len:
|
|
114 seqlength = seqlength + len(data_lines[match].strip())
|
|
115 match = match + 1
|
|
116 else:
|
|
117 break
|
|
118 return ReturnValue1(seqlength, genename)
|
|
119 if uniprot_accession_in == namer.split(" ")[0]:
|
|
120 match = count + 1
|
|
121 # Ensures consistent spacing throughout.
|
|
122 if 'GN=' in data_line:
|
|
123 lst = data_line.split('GN=')
|
|
124 lst2 = lst[1].split(' ')
|
|
125 genename = lst2[0]
|
|
126 if 'GN=' not in data_line:
|
|
127 genename = 'NA'
|
|
128 while ">sp" not in data_lines[match]:
|
|
129 if match <= db_len:
|
|
130 seqlength = seqlength + len(data_lines[match].strip())
|
|
131 match = match + 1
|
|
132 else:
|
|
133 break
|
|
134 return ReturnValue1(seqlength, genename)
|
|
135 count = count + 1
|
|
136 if seqlength == 0:
|
|
137 error.write(uniprot_accession_in + '\t' + "Uniprot not in Fasta" + '\n')
|
|
138 error.close
|
|
139 seqlength = 'NA'
|
|
140 genename = 'NA'
|
|
141 return ReturnValue1(seqlength, genename)
|
|
142 def concatenate_files(file_list_string, bait_file):
|
9
|
143 file_list = file_list_string.split(",")
|
0
|
144 bait = read_tab(bait_file)
|
|
145 master_table = []
|
|
146 header_check = 0
|
|
147 file_cnt = 0
|
|
148 table_cnt = 0
|
|
149 for i in file_list:
|
|
150 table = read_tab(i)
|
|
151 for j in table:
|
|
152 if table_cnt == 0:
|
|
153 if header_check == 0:
|
|
154 header_check +=1
|
|
155 j.append("Replicate")
|
|
156 j.append("Bait_Grouping")
|
|
157 master_table.append(j)
|
|
158 if table_cnt > 0:
|
|
159 j.append(bait[file_cnt][0])
|
|
160 j.append(bait[file_cnt][1])
|
|
161 master_table.append(j)
|
|
162 table_cnt +=1
|
|
163 file_cnt+=1
|
|
164 table_cnt = 0
|
|
165 if len(master_table[0]) < len(master_table[1]):
|
|
166 master_table[0] = ["#"] + master_table[0]
|
|
167 with open("merged_PeptideShaker.txt","w") as x:
|
|
168 for i in master_table:
|
|
169 x.write("\t".join(i))
|
|
170 x.write("\n")
|
|
171 return master_table
|
|
172 def make_inter(master_table,quant_type):
|
|
173 if len(master_table[0]) < len(master_table[1]):
|
|
174 master_table[0] = ["#"] + master_table[0]
|
|
175 replicate_index = master_table[0].index("Replicate")
|
|
176 grouping_index = master_table[0].index("Bait_Grouping")
|
|
177 accession_index = master_table[0].index("Main Accession")
|
10
|
178 quant_type = quant_type.replace("_", " ")
|
|
179 quant_type = r"#" + quant_type
|
0
|
180 Quant_index = master_table[0].index(quant_type)
|
|
181 inter_file = ""
|
|
182 for i in master_table[1:]:
|
|
183 line = []
|
|
184 line.append(i[replicate_index])
|
|
185 line.append(i[grouping_index])
|
|
186 line.append(i[accession_index])
|
|
187 line.append(i[Quant_index])
|
|
188 inter_file = inter_file + "\t".join(line) + "\n"
|
|
189 with open("inter.txt","w") as x:
|
|
190 x.write(inter_file)
|
|
191
|
|
192 def make_prey(concat_table,fasta_db):
|
|
193 input_data = concat_table
|
|
194 if len(input_data[0]) < len(input_data[1]):
|
|
195 input_data[0] = ["#"] + input_data[0]
|
|
196 accession_index = input_data[0].index("Main Accession")
|
|
197 proteins = []
|
|
198 for i in input_data[1:]:
|
|
199 proteins.append(i[accession_index])
|
|
200 output_file = open("prey.txt", 'w')
|
|
201 start = 0
|
|
202 end = len(proteins)
|
|
203
|
|
204 # Initial call to print 0% progress
|
|
205 printProgress(start, end, prefix = 'Progress:', suffix = 'Complete', barLength = 50)
|
|
206
|
|
207 for protein in proteins:
|
|
208 seq = get_info(protein,fasta_db).seqlength
|
|
209 GN = get_info(protein,fasta_db).genename
|
|
210 if seq != 'NA':
|
|
211 output_file.write(protein + "\t" + str(seq) + "\t" + str(GN) + "\n")
|
|
212 start+=1
|
|
213 printProgress(start, end, prefix = 'Progress:', suffix = 'Complete', barLength = 50)
|
|
214 output_file.close()
|
|
215 data = concatenate_files(files,bait)
|
|
216 make_inter(data, quant_type)
|
|
217 if prey == "true":
|
|
218 make_prey(data,db)
|
|
219
|
|
220 os.rename("bait.txt", sys.argv[2])
|
|
221 os.rename("inter.txt", sys.argv[10])
|
|
222 os.rename("prey.txt", sys.argv[11]) |