Extract and analyze mutational signatures from somatic variants using SigProfiler or MutationalPatterns to characterize mutagenic processes. Use when identifying DNA damage mechanisms or etiology in cancer genomes.
Before using code patterns, verify installed versions match. If versions differ:
Python: pip show <package> then help(module.function) to check signatures
R: packageVersion('<pkg>') then ?function_name to verify parameters
If code throws ImportError, AttributeError, or TypeError, introspect the installed
package and adapt the example to match the actual API rather than retrying.
Somatic Mutational Signatures
"Extract mutational signatures from my tumor samples" → Decompose somatic mutation catalogs into mutational signatures (SBS, DBS, ID) to identify DNA damage mechanisms and mutagenic processes in cancer genomes.
Python: SigProfilerExtractor.sigpro() for de novo signature extraction
R: MutationalPatterns::fit_to_signatures() for fitting to COSMIC signatures
SigProfiler Workflow
Goal: Extract de novo mutational signatures and decompose to COSMIC reference signatures from somatic VCFs.
Approach: Generate a 96-trinucleotide-context mutation matrix with SigProfilerMatrixGenerator, extract signatures via NMF with SigProfilerExtractor, and fit to COSMIC with SigProfilerAssignment.
Install and Generate Matrix
from SigProfilerMatrixGenerator import install as genInstall
from SigProfilerMatrixGenerator.scripts import SigProfilerMatrixGeneratorFunc as matGen
# Install reference genome (one-time)
genInstall.install('GRCh38')
# Generate mutational matrix from VCF
# Input: Directory containing VCF files
# Output: SBS96 matrix (96 trinucleotide contexts)
matrices = matGen.SigProfilerMatrixGeneratorFunc(
project='my_project',
genome='GRCh38',
vcfFiles='/path/to/vcf_directory',
plot=True,
exome=False # Set True for WES
)
Extract Signatures
from SigProfilerExtractor import sigpro as sig
# De novo signature extraction
# Determines optimal number of signatures automatically
sig.sigProfilerExtractor(
input_type='matrix',
output='extraction_output',
input_data='my_project/output/SBS/my_project.SBS96.all',
reference_genome='GRCh38',
minimum_signatures=1,
maximum_signatures=10,
nmf_replicates=100,
cpu=-1 # Use all cores
)
Decompose to COSMIC Signatures
from SigProfilerAssignment import Analyzer as Analyze
# Fit to known COSMIC signatures
Analyze.cosmic_fit(
samples='my_project/output/SBS/my_project.SBS96.all',
output='assignment_output',
input_type='matrix',
genome_build='GRCh38',
signature_database='SBS_GRCh38_GRCh38'
)
MutationalPatterns (R)
Goal: Analyze mutational spectra and fit to COSMIC signatures using the MutationalPatterns R package.
Approach: Load VCFs as GRanges, generate a 96-context mutation matrix against the reference genome, then fit to known COSMIC signatures or extract de novo via NMF.