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