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