annotate Protein_report_processing.py @ 61:0b4450bdc6a9 draft

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