annotate Protein_report_processing.py @ 8:e1d6909836cd draft

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