-
Notifications
You must be signed in to change notification settings - Fork 0
/
fromFragPipe_tocoADAPTr.py
343 lines (236 loc) · 9.99 KB
/
fromFragPipe_tocoADAPTr.py
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
"""
Author: Carolina Rojas Ramirez
Date: 12/06/2023
Script to add a FPOP mods only column to psm.tsv
"""
import tkinter
from tkinter import filedialog
import pandas as pd
import os
import sys
import re
import numpy as np
#set up Tkinter
root = tkinter.Tk()
root.withdraw()
def modstring_processing(assignedmod_str):
"""
Function to select FPOP mods only
:param assignedmod_str:str
:return: str
"""
FPOP_massoffsetls = [15.9949, 31.9898, 47.9847, 13.9793, -43.0534,
-22.0320, -23.0159, -10.0319, 4.9735, -30.0106, -27.9949, -43.9898,
-25.0316, -9.0367, 67.9874]
returnls = []
# Iterate over proteins
if isinstance(assignedmod_str, str):
assignedmod_spl = assignedmod_str.split(",")
for item in assignedmod_spl:
for fpopmod in FPOP_massoffsetls:
if str(fpopmod) in item:
returnls.append(item)
returnstr = ",".join(returnls)
return returnstr
def modstring_processingDIA(massoffsets_str, unimodict_val):
"""
Function to select FPOP mods only
:param assignedmod_str:str
:return: str
"""
print(massoffsets_str)
outls = []
FPOP_massoffsetls = [15.9949, 31.9898, 47.9847, 13.9793, -43.0534,
-22.0320, -23.0159, -10.0319, 4.9735, -30.0106, -27.9949, -43.9898,
-25.0316, -9.0367, 67.9874]
# Including localization
modregex = re.compile(
"\([0-9]*.[0-9]*\)|\(Unimod:[0-9]*.[0-9]*\)",
re.I)
mod = modregex.finditer(massoffsets_str)
if mod:
# If modifications were found iteriate over each one
truelocationoffset = 0
for item in mod:
print(f"moditem = {item}")
# get modification string
locations = item.span()
# Extract modification
massshift = massoffsets_str[locations[0]:locations[1]]
# Remove parenthesis
massoffset = massshift[1:-1]
# Extract residue after the modification string
truelocationoffset += abs(locations[0] - locations[1])
#Handeling n-termcases
if locations[0] == 0:
residuestr = massoffsets_str[locations[1]]
truelocation = 1
else:
try:
residuestr = massoffsets_str[locations[1]]
# Mod at the c-term
except IndexError:
residuestr = massoffsets_str[locations[0]-1]
truelocation = locations[1] + 1 - truelocationoffset
# print(f"moditem = {massoffset}")
# For sanity check reconstrcut mass offset string (can simplified later)
# print(unimodict)
print(truelocationoffset)
if "UniMod" in massoffset:
massoffset_spl = massoffset.split(":")
massoffset_unimodval = massoffset_spl[1]
massoffset_val = unimodict_val[int(massoffset_unimodval)]
residuemassoffset = f"{truelocation}{residuestr}({massoffset_val})"
else:
residuemassoffset = f"{truelocation}{residuestr}({round(float(massoffset), 4)})"
# Ignored C57 mods
if residuemassoffset == "C(UniMod:4)":
continue
else:
outls.append(residuemassoffset)
outstr = ','.join(outls)
return outstr
def precursorwithFPOPcolumn(psmoverwrite_bool):
"""
Function to add FPOP only mods column
:param FilePath: Absolute path to tsv file
:param file_type: File type (not extension but is it a psm, peptide or protein TSV file)
:return: it depends
"""
#Starting analysis
FilePath = filedialog.askopenfilename(title="Choose report file")
print(f"~*~*~Reading file = {FilePath}~*~*~")
#Create dataframe from file
dataframe_item = pd.read_csv(FilePath, sep='\t')
#How many lines there are in file
rows = dataframe_item.shape[0]
print(f"rows = {rows}")
modifiedpsms =dataframe_item["Modified.Sequence"]
fragpipeinstallfolder = filedialog.askdirectory(title="Choose FragPipe Installation folder")
unimodfile_path = os.path.join(fragpipeinstallfolder, "tools", "UniModData.tsv")
unimoddf = pd.read_csv(unimodfile_path, sep="\t")
# unimod dictionary
unimodict = {}
for idx in unimoddf.index:
monomass = round(unimoddf.loc[idx, "monoMass"], 4)
unimodid = unimoddf.loc[idx, "id"]
if np.isnan(unimodid) or unimodid in unimodict.keys():
continue
else:
unimodict[int(unimodid)] = float(monomass)
FPOPonlycol = []
# Iteriate over modified PSMs
for modstring in modifiedpsms:
# print(modstring)
newstr = modstring_processingDIA(modstring, unimodict)
FPOPonlycol.append(newstr)
dataframe_item.insert(16, "FPOP Modifications", FPOPonlycol, True)
outputdir = os.path.dirname(FilePath)
#If user want to keep a psm file copy without FPOP column
if psmoverwrite_bool:
outputfilename = os.path.join(outputdir, "report.tsv")
else:
outputfilename = os.path.join(outputdir, "report_coADAPTr.tsv")
dataframe_item.to_csv(outputfilename, sep = "\t", index=False)
# columns = dataframe_item.columns
# print(columns)
# print(f"Priot to error = {file_type}")
def psmwithFPOPcolumn(FilePath, psmoverwrite_bool):
"""
Function to add FPOP only mods column
:param FilePath: Absolute path to tsv file
:param file_type: File type (not extension but is it a psm, peptide or protein TSV file)
:return: it depends
"""
#Starting analysis
print(f"~*~*~Reading file = {FilePath}~*~*~")
#Create dataframe from file
dataframe_item = pd.read_csv(FilePath, sep='\t')
#How many lines there are in file
rows = dataframe_item.shape[0]
print(f"rows = {rows}")
modifiedpsms =dataframe_item["Assigned Modifications"]
FPOPonlycol = []
# Iteriate over modified PSMs
for modstring in modifiedpsms:
# print(modstring)
newstr = modstring_processing(modstring)
FPOPonlycol.append(newstr)
dataframe_item.insert(26, "FPOP Modifications", FPOPonlycol, True)
outputdir = os.path.dirname(FilePath)
#If user want to keep a psm file copy without FPOP column
if psmoverwrite_bool:
outputfilename = os.path.join(outputdir, "psm.tsv")
else:
outputfilename = os.path.join(outputdir, "fpop_psm.tsv")
dataframe_item.to_csv(outputfilename, sep = "\t", index=False)
# columns = dataframe_item.columns
# print(columns)
# print(f"Priot to error = {file_type}")
def multiexperiment_psm(psmoverwrite = None):
"""
Function to gather PSMs counts (organized by residue and mass shifts) from all experiments
:param mode: none, takes aggreagted PSMs files organized by residue and mass shift. Set to "PSMs" to calculate PSMs for each experiment
:return: void
"""
folder = filedialog.askdirectory(title = "Choose FragPipe results folder")
os.chdir(folder)
print(f"folder = {folder}")
diritems = [x for x in os.listdir(folder)]
# print(f"files = {diritems}")
# For each experiments
for item in diritems:
# print(f"item = {item}")
# Continue if the item is not a folder
if item.find(".") > -1:
continue
else:
# if the item is a file
subdiritem = os.path.join(folder, item)
# print(f"subdirectory = {subdiritem}")
subdirfiles = [x for x in os.listdir(subdiritem)]
# If calculating residues and mass shift modification from a psm.tsv file
for file in subdirfiles:
if "psm.tsv" == file:
# print(f"file = {file}")
filepath = os.path.join(subdiritem, file)
psmwithFPOPcolumn(filepath, psmoverwrite)
def main():
"""
Main fcuntion to enable terminal usage
@return: void
"""
#Checking Python version
majorversion = sys.version_info.major
minorversion = sys.version_info.minor
if majorversion < 3:
print("Python version must be at least 3.7")
elif majorversion == 3 and minorversion < 7:
print("Python version must be at least 3.7")
else:
print("Python version is at least greater than or equal to 3.7")
# Example adding a second attribute ['.\\fromFragPipe_tocoADAPTr.py', 'True']
systemarguments = sys.argv
# If there are two arguments it means and attribute was passed
if len(systemarguments) == 2:
# Second argument better be a bool
overwriteattribute = systemarguments[1]
if overwriteattribute.lower() == "true":
# PSM file will be overwritten
print("PSM file will be overwritten. A FPOP only column will be added, but it will be saved under the same name.")
multiexperiment_psm(psmoverwrite=True)
elif overwriteattribute.lower() == "false":
# PSM file will be overwritten
print("A copy of the PSM file with the FPOP only column and the name fpop_psm.tsv will be created.")
multiexperiment_psm(psmoverwrite=False)
else:
print("Second attribute must be True or False.")
# Assume that no second argument equals false
elif len(systemarguments) == 1:
print("A copy of the PSM file with the FPOP only column and the name fpop_psm.tsv will be created.")
multiexperiment_psm()
else:
print("There are no system arguments?!")
if __name__ == '__main__':
# main()
precursorwithFPOPcolumn(False)