-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfilter.py
More file actions
456 lines (406 loc) · 14.6 KB
/
Copy pathfilter.py
File metadata and controls
456 lines (406 loc) · 14.6 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
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
"""Command-line filtering for N-terminal biotinylation labels.
This script reads a spectral library export, validates precursor entries using
fragment-ion evidence, and writes a filtered output file in the same format.
"""
import os
import re
import time
import argparse
import numpy as np
import pandas as pd
# Source-specific column mappings and heuristics.
library_dict = {
'Spectronaut': {
'mod_pattern': '(UniMod:3)',
'ion_col_name': 'F.FrgIon',
'charge_col_name': 'FG.Charge',
'mod_peptide_col_name': 'PEP.GroupingKey',
'sample_col_name': 'R.FileName',
'pqColName': 'F.PredictedRelativeIntensity',
'msColName': 'F.MeasuredRelativeIntensity',
},
'MsFragger': {
'mod_pattern': '(UniMod:3)',
'ion_col_name': 'Annotation',
'charge_col_name': 'PrecursorCharge',
'mod_peptide_col_name': 'ModifiedPeptideSequence',
'sample_col_name': None,
'pqColName': None,
'msColName': None,
}
}
# Timer Related Functions
def getTime() -> float:
"""Get the current time for timer."""
return time.time()
def prettyTimer(seconds: float) -> str:
"""Better way to show elapsed time."""
m, s = divmod(seconds, 60)
h, m = divmod(m, 60)
return "%02dh:%02dm:%02ds" % (h, m, s)
def read_spectral_library(
file_path: str,
file_format: str = 'tsv'
) -> pd.DataFrame:
"""
Read a spectral library from a file.
Args:
file_path (str): Path to the spectral library file.
file_format (str): File format ('csv', 'tsv', 'txt', or 'excel').
Returns:
pd.DataFrame: A DataFrame containing the loaded spectral library.
Raises:
ValueError: If an unsupported file format is provided.
"""
if file_format == 'csv':
return pd.read_csv(
file_path,
sep=","
)
elif file_format in ('tsv', 'txt'):
return pd.read_csv(
file_path,
sep="\t"
)
elif file_format == 'excel':
return pd.read_excel(file_path)
else:
raise ValueError("Unsupported file format")
def write_spectral_library(
library: pd.DataFrame,
file_path: str,
file_format: str = 'tsv'
) -> None:
"""
Write the filtered spectral library to a file.
Args:
library (pd.DataFrame): The filtered spectral library DataFrame.
file_path (str): Path to the output file.
file_format (str): File format ('csv', 'tsv', 'txt', or 'excel').
Raises:
ValueError: If an unsupported file format is provided.
"""
if file_format == 'csv':
library.to_csv(
file_path,
sep=",",
index=False
)
elif file_format in ('tsv', 'txt'):
library.to_csv(
file_path,
sep="\t",
index=False
)
elif file_format == 'excel':
library.to_excel(
file_path,
index=False
)
else:
raise ValueError("Unsupported file format")
def process_spectral_library(
library: pd.DataFrame,
library_type: str,
verbose: bool = False
) -> pd.DataFrame:
"""
Process the spectral library data.
Args:
library (pd.DataFrame): The spectral library DataFrame.
library_type (str): Type of spectral library ('Spectronaut' or 'MsFragger').
verbose (bool): Whether to print verbose output (default: False).
Returns:
pd.DataFrame: The processed and filtered spectral library DataFrame.
"""
# Collect the source-specific column names once for readability.
ion_col_name = library_dict[library_type]['ion_col_name']
charge_col_name = library_dict[library_type]['charge_col_name']
mod_peptide_col_name = library_dict[library_type]['mod_peptide_col_name']
# Following columns are only used for Spectronaut
if library_type == 'Spectronaut':
sample_col_name = library_dict[library_type]['sample_col_name']
pqColName = library_dict[library_type]['pqColName']
msColName = library_dict[library_type]['msColName']
if verbose:
print(f"Filtering library from {library_type}...")
# Track precursor IDs that pass the validation steps.
valid_precursors = []
# Build a unique precursor identifier and select the columns used later.
if library_type == 'Spectronaut':
library["ID"] = (
library[mod_peptide_col_name] + "_" +
library[sample_col_name] + "_" +
library[charge_col_name].astype(str)
)
# Spectronaut exports include a predicted vs measured intensity check.
library["isWithin"] = (
library[msColName] > (library[pqColName] / 10)
)
cols1 = [
"ID", "isWithin",
mod_peptide_col_name,
ion_col_name,
charge_col_name
]
cols2 = [
'ID', 'isWithin',
'StrippedSequence',
'SequenceLength',
'ModifiedSequence',
'FragmentType',
'FragmentNumber'
]
else:
library["ID"] = (
library[mod_peptide_col_name] +
"_" +
library[charge_col_name].astype(str)
)
cols1 = [
"ID",
mod_peptide_col_name,
ion_col_name,
charge_col_name
]
cols2 = [
'ID',
'StrippedSequence',
'SequenceLength',
'ModifiedSequence',
'FragmentType',
'FragmentNumber'
]
if verbose:
# Print the number of unique precursors in the initial data
print(f"Initial Data - Number of Unique Precursors: {len(set(library['ID']))}")
# Work on a smaller table containing only the required columns.
data = library[cols1].copy()
# Keep only peptides with an explicit N-terminal modification annotation.
# TODO: In the future different labeling styles need to be considered
nterm_mod_pattern = re.compile(r'^\([^)]*\)')
data = data[
data[mod_peptide_col_name].astype(str).apply(
lambda x: bool(nterm_mod_pattern.search(x))
)
]
if verbose:
# Print the number of unique precursors after selecting Nterm label only precusors
print(f"Labeled at Nterm Only - Number of Unique Precursors: {len(set(data['ID']))}")
# Parse fragment ion type and ordinal position from the annotation column.
data[[
'FragmentType',
'FragmentNumber'
]] = data[ion_col_name].str.split(
"^" # Removes the trailing ^ and the number after it
).str[0].str.extract(
r'([a-zA-Z]+)(\d+)' # Extracts fragment and number
)
# Convert FragmentNumber to int
data["FragmentNumber"] = data["FragmentNumber"].astype(int)
# Split modified and stripped sequence representations used by the filter.
data["ModifiedSequence"] = data[mod_peptide_col_name].str.replace(
nterm_mod_pattern,
''
)
data["StrippedSequence"] = data["ModifiedSequence"].str.replace(
r'\(UniMod:\d+\)',
'',
regex=True
)
data["SequenceLength"] = data["StrippedSequence"].str.len()
# Only keep the columns we need
data = data[cols2]
# Record lysine positions on the unmodified peptide sequence.
data["KPositions"] = data.apply(
lambda row: [
m.start() for m in re.finditer(
r'K', row["StrippedSequence"]
)
],
axis=1
)
# Peptides without lysine residues pass immediately once N-terminally labeled.
subset = data[
data["KPositions"].str.len() == 0
].reset_index(drop=True).copy()
# Place the valid precursors without K in the valid_precursors list
if library_type == 'Spectronaut':
valid_precursors.extend(
subset[
subset["isWithin"]
]["ID"].unique()
)
else:
valid_precursors.extend(
subset["ID"].unique()
)
if verbose:
print(f"Number of Unique Valid Precursors (without any K): {len(valid_precursors)}")
# Continue only with peptides that contain at least one lysine residue.
data = data[
data["KPositions"].str.len() > 0
].reset_index(drop=True).copy()
# Determine which lysines are modified after removing the N-terminal label.
data["Modifications"] = data["ModifiedSequence"].str.findall(
r'\(UniMod:[0-9]+\)' # TODO: More robust to multiple mod labeling types
)
# Find the positions of the modifications
data["Positions"] = data.apply(
lambda row: [
m.start() - 1 for m in re.finditer(
r'\(UniMod:[0-9]+\)', # TODO: More robust to multiple mod labeling types
row["ModifiedSequence"]
)
],
axis=1
)
# Find the preceding amino acids per modification
data["PrecedingAminoAcids"] = data.apply(
lambda row: [
row["ModifiedSequence"][pos] if pos >= 0 else None for pos in row["Positions"]
],
axis=1
)
# Find the absolute (without mod labels) positions
data["AbsolutePositions"] = data.apply(
lambda row: [
pos - cumulative_length for pos, cumulative_length in zip(
row["Positions"],
(
np.cumsum([len(mod) for mod in row["Modifications"]]) -
[len(mod) for mod in row["Modifications"]]
)
)
],
axis=1
)
# Find the K positions after the fragment number for y-ion check
data["AfterFragmentKPos"] = data.apply(
lambda row: [
p for p in row["KPositions"] if p >= (row["SequenceLength"] - int(row["FragmentNumber"]))
],
axis=1
)
# Find the K positions before the fragment number for b-ion check
data["BeforeFragmentKPos"] = data.apply(
lambda row: [
p for p in row["KPositions"] if p < int(row["FragmentNumber"])
],
axis=1
)
# Find the modified K positions using the absolute positions
data["ModifiedKPositions"] = data.apply(
lambda row: [
p for p in row["KPositions"] if p in row["AbsolutePositions"]
],
axis=1
)
# Find the unmodified K positions using the absolute positions
data["UnmodifiedKPositions"] = data.apply(
lambda row: [
p for p in row["KPositions"] if p not in row["AbsolutePositions"]
],
axis=1
)
# Validate fragment evidence against modified and unmodified lysine positions.
data["correct_fragment_ion"] = data.apply(
lambda row: (
(
row["FragmentType"] == 'b' and
set(row["BeforeFragmentKPos"]).issubset(set(row["ModifiedKPositions"]))
) or
(
row["FragmentType"] == 'y' and
set(row["UnmodifiedKPositions"]).issubset(set(row["AfterFragmentKPos"]))
)
),
axis=1
)
# Keep the IDs that satisfy the fragment-ion checks.
if library_type == 'Spectronaut':
valid_precursors.extend(
data[
data["correct_fragment_ion"] &
data["isWithin"]
]["ID"].unique()
)
else:
valid_precursors.extend(
data[
data["correct_fragment_ion"]
]["ID"].unique()
)
if verbose:
print(f"Number of Unique Valid Precursors (all): {len(valid_precursors)}")
# Write the original rows back out for valid precursor IDs only.
filtered_library = library[
library["ID"].isin(valid_precursors)
].drop(columns=["ID"])
# Return the filtered library
return filtered_library
def main():
"""
Main function for processing spectral library data.
Parameters:
None
Returns:
None
"""
# Start timer
start_time = getTime()
# Parse arguments
parser = argparse.ArgumentParser(description="Process spectral library data")
parser.add_argument("file_name", help="Name of the spectral library file")
parser.add_argument("library_type", choices=["Spectronaut", "MsFragger"], help="Type of spectral library")
parser.add_argument("main_path", help="Main directory where the input file is located")
parser.add_argument("--output_dir", help="Output directory for the filtered library")
parser.add_argument("--output_file", help="Specify the output file name for the filtered library")
parser.add_argument("--verbose", action="store_true", help="Print verbose output")
args = parser.parse_args()
# Get the variables from the arguments
file_name = args.file_name
library_type = args.library_type
main_path = args.main_path
output_dir = args.output_dir
output_file = args.output_file
verbose = args.verbose
# Split the filename once so the output filename can reuse the same suffix.
file_root_name, file_extension = os.path.splitext(file_name)
file_format = file_extension.lstrip(".")
# Create check for library type
if library_type not in ['Spectronaut', 'MsFragger']:
raise ValueError("Unsupported library type, please choose from Spectronaut or MsFragger")
# Create check for file path
if not os.path.exists(os.path.join(main_path, file_name)):
raise ValueError("File is not found in the specified path with filename")
if verbose:
print(f"Reading {file_name} from {main_path}...")
# Construct the full file path using main_path
full_file_path = os.path.join(main_path, file_name)
# Read the spectral library
library = read_spectral_library(full_file_path, file_format)
# Apply the filter to the library
filtered_library = process_spectral_library(
library,
library_type,
verbose
)
# Default to writing next to the input with a _filtered suffix.
if not output_file:
output_file = file_root_name + "_filtered." + file_format
# If requested, create the output directory under main_path first.
if output_dir:
output_dir = os.path.join(main_path, output_dir)
if not os.path.exists(output_dir):
os.makedirs(output_dir)
output_file_path = os.path.join(output_dir, output_file)
else:
# Use main_path for output
output_file_path = os.path.join(main_path, output_file)
# Save the filtered library
write_spectral_library(filtered_library, output_file_path, file_format)
elapsed_time = getTime() - start_time
print(f"Filtering complete! Elapsed Time: {prettyTimer(elapsed_time)}")
if __name__ == "__main__":
main()