annotate Protein_report_processing.py @ 12:ad33728cecc0 draft

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