annotate Protein_report_processing.py @ 80:6ad498eac0e2 draft default tip

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