Before using code patterns, verify installed versions match. If versions differ:
Python: pip show <package> then help(module.function) to check signatures
CLI: <tool> --version then <tool> --help to confirm flags
If code throws ImportError, AttributeError, or TypeError, introspect the installed
package and adapt the example to match the actual API rather than retrying.
Spectral Library Management
"Build a spectral library for DIA analysis" → Create, filter, and manage spectral libraries from DDA experiments or predicted spectra for use in DIA quantification workflows.
CLI: spectrast (TPP) for consensus library building from search results
CLI: Prosit/DeepLC for deep learning-predicted spectral libraries
Python: pandas for library format conversion and quality filtering
Build Library from DDA Data
SpectraST (TPP)
# Build library from search results
spectrast -cNlibrary.splib -cAC search_results.pep.xml
# Filter library for quality
spectrast -cNfiltered.splib -cAQ library.splib
# Convert to other formats
spectrast -cNlibrary.tsv -cM library.splib
import pandas as pd
library = pd.read_csv('library.tsv', sep='\t')
# Basic statistics
print(f"Precursors: {library['ModifiedSequence'].nunique()}")
print(f"Proteins: {library['ProteinId'].nunique()}")
print(f"Transitions per precursor: {len(library) / library['ModifiedSequence'].nunique():.1f}")
# RT distribution
import matplotlib.pyplot as plt
rts = library.groupby('ModifiedSequence')['NormalizedRetentionTime'].first()
plt.hist(rts, bins=50)
plt.xlabel('Normalized RT')
plt.ylabel('Precursors')
plt.savefig('rt_distribution.png')
# Charge state distribution
charges = library.groupby('ModifiedSequence')['PrecursorCharge'].first()
print(charges.value_counts())
Merge Libraries
Goal: Combine multiple spectral libraries into a single non-redundant library, keeping the highest-quality spectra for each precursor.
Approach: Concatenate library tables, rank precursors by total fragment intensity, and deduplicate by keeping the best-scoring entry per precursor-fragment combination.
import pandas as pd
# Load libraries
lib1 = pd.read_csv('library1.tsv', sep='\t')
lib2 = pd.read_csv('library2.tsv', sep='\t')
# Concatenate and remove duplicates
# Keep entry with highest total intensity per precursor
combined = pd.concat([lib1, lib2])
# Calculate total intensity per precursor
precursor_intensity = combined.groupby('ModifiedSequence')['LibraryIntensity'].sum()
# Keep best precursor entries
combined['total_int'] = combined['ModifiedSequence'].map(precursor_intensity)
combined = combined.sort_values('total_int', ascending=False)
combined = combined.drop_duplicates(subset=['ModifiedSequence', 'FragmentType', 'FragmentSeriesNumber'])
combined = combined.drop('total_int', axis=1)
combined.to_csv('merged_library.tsv', sep='\t', index=False)