annotate Protein_report_processing.py @ 3:ae7aad717696 draft

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