Skip to content

Instantly share code, notes, and snippets.

@oss6
Last active May 1, 2023 20:37
Show Gist options
  • Save oss6/d9e4e840c24d9b64a89964181e92a273 to your computer and use it in GitHub Desktop.
Save oss6/d9e4e840c24d9b64a89964181e92a273 to your computer and use it in GitHub Desktop.
LC/MS Feature detection using xcms' centwave
# -------------------------------------------------------------------------------------------------
# Adapted from https://www.bioconductor.org/packages/release/bioc/vignettes/xcms/inst/doc/xcms.html
# The below example uses a subset of the data in the investigation of the metabolic consequences
# of knocking out the fatty acid amide hydrolase (FAAH) gene in mice.
if (!require("BiocManager", quietly = TRUE)) {
install.packages("BiocManager")
}
if (!("xcms" %in% rownames(installed.packages()))) {
BiocManager::install("xcms")
}
library(xcms)
library(faahKO)
library(RColorBrewer)
library(pander)
library(magrittr)
library(pheatmap)
library(SummarizedExperiment)
# Get the full path to the CDF files
cdfs <- dir(system.file("cdf", package = "faahKO"), full.names = TRUE, recursive = TRUE)[c(1, 2, 5, 6, 7, 8, 11, 12)]
# Create a phenodata data.frame
pd <- data.frame(
sample_name = sub(basename(cdfs), pattern = ".CDF", replacement = "", fixed = TRUE),
sample_group = c(rep("KO", 4), rep("WT", 4)),
stringsAsFactors = FALSE)
raw_data <- readMSData(files = cdfs, pdata = new("NAnnotatedDataFrame", pd), mode = "onDisk")
# Focus on a specific retention time window
raw_data <- filterRt(raw_data, c(2500, 3500))
# Define the rt and m/z range of the peak area to investigate
rtr <- c(2700, 2900)
mzr <- c(334.9, 335.1)
# Extract the chromatogram
chr_raw <- chromatogram(raw_data, mz = mzr, rt = rtr)
plot(chr_raw, col = group_colors[chr_raw$sample_group])
# Peak detection using the default parameters and setting the signal-to-noise ratio
xchr <- findChromPeaks(chr_raw, param = CentWaveParam(snthresh = 2))
head(chromPeaks(xchr))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment