## load package
library("MALDIquant")
## create four MassPeaks objects and add them to the list
p <- list(createMassPeaks(mass=1:2, intensity=1:2),
createMassPeaks(mass=1:3, intensity=1:3),
createMassPeaks(mass=1:4, intensity=1:4),
createMassPeaks(mass=1:5, intensity=1:5))
## only keep peaks which occur in all MassPeaks objects
filteredPeaks <- filterPeaks(p, minFrequency=1)
## compare result
intensities <- intensityMatrix(filteredPeaks)
## peaks at mass 3,4,5 are removed
all(dim(intensities) == c(4, 2)) # TRUE
all(intensities[,1] == 1) # TRUE
all(intensities[,2] == 2) # TRUE
## only keep peaks which occur in all MassPeaks objects in a group
## (e.g. useful for technical replicates)
groups <- factor(c("a", "a", "b", "b"), levels=c("a", "b"))
filteredPeaks <- filterPeaks(p, minFrequency=1, labels=groups)
## peaks at mass 3 were removed in group "a"
filteredPeaks[groups == "a"]
## peaks at mass 5 were removed in group "b"
filteredPeaks[groups == "b"]
## only keep peaks which occur at least twice in a group
groups <- factor(c("a", "a", "b", "b", "b"), levels=c("a", "b"))
filteredPeaks <- filterPeaks(c(p, p[[3]]), minNumber=2, labels=groups)
## peaks at mass 3 were removed in group "a"
filteredPeaks[groups == "a"]
## peaks at mass 5 were removed in group "b"
filteredPeaks[groups == "b"]
## apply different minFrequency arguments to each group
groups <- factor(c("a", "a", "b", "b", "b"), levels=c("a", "b"))
filteredPeaks <- filterPeaks(c(p, p[[3]]), minFrequency=c(1, 2/3), labels=groups)
intensityMatrix(filteredPeaks)
# 1 2 3 4
#[1,] 1 2 NA NA
#[2,] 1 2 NA NA
#[3,] 1 2 3 4
#[4,] 1 2 3 4
#[4,] 1 2 3 4
## demonstrate the use of mergeWhitelists
groups <- factor(c("a", "a", "b", "b"), levels=c("a", "b"))
## default behaviour
filteredPeaks <- filterPeaks(p, minNumber=2, labels=groups)
intensityMatrix(filteredPeaks)
# 1 2 3 4
#[1,] 1 2 NA NA
#[2,] 1 2 NA NA
#[3,] 1 2 3 4
#[4,] 1 2 3 4
## use mergeWhitelists=TRUE to keep peaks of group "a" that match all filtering
## criteria in group "b"
## (please note that mass == 3 is not removed in the second MassPeaks object)
filteredPeaks <- filterPeaks(p, minNumber=2, labels=groups,
mergeWhitelists=TRUE)
intensityMatrix(filteredPeaks)
# 1 2 3 4
#[1,] 1 2 NA NA
#[2,] 1 2 3 NA
#[3,] 1 2 3 4
#[4,] 1 2 3 4
Run the code above in your browser using DataLab