-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphospho_maxquant_process.py
More file actions
184 lines (125 loc) · 5.89 KB
/
phospho_maxquant_process.py
File metadata and controls
184 lines (125 loc) · 5.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import re
import sys
import argparse
############################################################## FUNCTIONS
def checkAndIncr(prob, countVar, styProbTh):
if float(prob) >= styProbTh:
countVar = countVar + 1
return (countVar)
def getCountInPeptide(peptide, styProbTh):
s = doTheCount('S', peptide, styProbTh)
t = doTheCount('T', peptide, styProbTh)
y = doTheCount('Y', peptide, styProbTh)
return (s, t, y)
def doTheCount(residue, seq, styProbTh):
count = 0
val = re.findall(residue + '\(([0-9\.]+)\)', seq)
if len(val)>0:
if type(val) is list:
for v in val:
# print(seq + '\t' + v)
count = checkAndIncr(v, count, styProbTh)
else:
count = checkAndIncr(val.group(1), count, styProbTh)
return (count)
def residual_count(dict_seq, aa, styProbTh):
y_count = 0
for seq in dict_seq:
val = re.findall(aa+'\(([0-9\.]+)\)', seq)
if len(val)>0:
if type(val) is list:
for v in val:
# print(seq + '\t' + v)
y_count = checkAndIncr(v, y_count, styProbTh)
else:
y_count = checkAndIncr(val.group(1), y_count, styProbTh)
return (y_count)
############################################################## TOP LEVEL
def countAndPrint(fn_maxquant, protColumn, locProbColumn, locProbTh, styProbColumn, styProbTh, fn_out):
dict_prot = dict()
dict_seq = dict()
with open(fn_maxquant, 'r+') as fh:
isHeader = True
for line in fh:
if isHeader == True:
isHeader = False
continue
line = re.sub('[\n\r]$', '', line)
arr = line.split("\t")
# print(arr[protColumn]);
if re.match('^CON', arr[protColumn]) or re.match('^[\s]*$', arr[protColumn]):
continue
if float(arr[locProbColumn]) >= locProbTh:
if (arr[protColumn], arr[styProbColumn]) not in dict_prot:
(s, t, y) = getCountInPeptide(arr[styProbColumn], styProbTh)
dict_prot[(arr[protColumn], arr[styProbColumn])] = (s, t, y) # 'S\t' + str(s) + '\t' + 'T' + '\t' + str(t) + '\t' + str(y)
else:
# print("Already added " + arr[protColumn] + " " + arr[styProbColumn])
pass
if arr[styProbColumn] in dict_seq:
dict_seq[arr[styProbColumn]] = dict_seq[arr[styProbColumn]] + 1
else:
dict_seq[arr[styProbColumn]] = 1
#print(line)
#break
##### Printing the outputs:
s_count = residual_count(dict_seq, 'S', styProbTh)
t_count = residual_count(dict_seq, 'T', styProbTh)
y_count = residual_count(dict_seq, 'Y', styProbTh)
print("S: " + str(s_count))
print("T: " + str(t_count))
print("Y: " + str(y_count))
# print(dict_seq);
print("======")
print("Writing to file " + fn_out);
fhandle = open(fn_out, 'w+')
# print(dict_prot);
for (prot, peptide) in dict_prot:
(ser, thr, tyr) = dict_prot[(prot, peptide)]
fhandle.write(prot + "\t" + peptide + "\t" + 'S\t' + str(ser) + '\t' + 'T' + '\t' + str(thr) + '\t' + 'Y' + '\t' + str(tyr) + '\n')
fhandle.close()
############################################################## MAIN
def main():
# usage = "python " + sys.argv[0] + " <styFile.txt> <sty_colnum>"
# if len(sys.argv) != 2:
# sys.exit("Error: incorrect number of inputs\n" + usage + '\n\n')
# countAndPrint(sys.argv[1])
parser = argparse.ArgumentParser(description='Extract S, T and Y site counts (which are above thresholds) from MaxQuant data.')
parser.add_argument('--maxQuantInFile', nargs=1, required=True, help='An output file from MaxQuant, as input to this script.')
parser.add_argument('--protColumn', nargs=1, default=0, type=int, help="Column containing the protein identifier. Default is 0.")
parser.add_argument('--locProbColumn', nargs=1, default=5, type=int, help="Column containing the localization probability. Default is 5.")
parser.add_argument('--locProbTh', nargs=1, default=0.75, type=float, help="Threshold to filter localization probability (between 0 and 1). Default is 0.75.")
parser.add_argument('--styProbColumn', nargs=1, default=29, type=int, help="Column containing the phospho-(STY)-probabilities. Default is 29.")
parser.add_argument('--styProbTh', nargs=1, default=0.75, type=float, help="Threshold to filter phospho-(STY)-probabilities (between 0 and 1). Default is 0.75.")
parser.add_argument('--outfile', nargs=1, default="Output.txt", help="Output file to save the results to. Default is Output.txt.")
# parser.add_argument('--maxQuantInFile', nargs=1, required=True, help='An output file from MaxQuant, as input to this script.')
args = parser.parse_args()
if isinstance(args.protColumn, list):
print("is list");
args.protColumn = args.protColumn[0];
if isinstance(args.locProbColumn, list):
args.locProbColumn = args.locProbColumn[0];
if isinstance(args.locProbTh, list):
args.locProbTh = args.locProbTh[0];
if isinstance(args.styProbColumn, list):
args.styProbColumn = args.styProbColumn[0];
if isinstance(args.styProbTh, list):
args.styProbTh = args.styProbTh[0];
if isinstance(args.outfile, list):
args.outfile = args.outfile[0];
if not (args.locProbTh >= 0 and args.locProbTh <= 1):
#sys.stderr.write()
raise argparse.ArgumentTypeError("Error: Argument to --locProbTh should be between 0 and 1.")
if not (args.styProbTh >= 0 and args.styProbTh <= 1):
#sys.stderr.write()
raise argparse.ArgumentTypeError("Error: Argument to --styProbTh should be between 0 and 1.")
print(isinstance(args.protColumn, list))
countAndPrint(args.maxQuantInFile[0], args.protColumn, args.locProbColumn, args.locProbTh, args.styProbColumn, args.styProbTh, args.outfile)
if __name__ == '__main__':
main()
# LIST_SEQ = list()
"""
if len(sys.argv) != 3:
sys.stderr.write("Incorrect number of inputs " + '\n' + usage + '\n\n')
sys.exit()
"""