knitr::opts_chunk$set(eval = TRUE, 
                      echo = TRUE, 
                      cache = FALSE, 
                      include = TRUE, 
                      collapse = FALSE, 
                      dependson = NULL, 
                      engine = "R", # Chunks will always have R code, unless noted
                      error = TRUE,
                      fig.align = "center"
                      )

Load library & data

library("MicroRaman")
# hs.x <- hs_import(path = "./test_data/")
# summary(hs.x)

# Load data from package
data("hs_example")
hs.x <- hs_example
summary(hs.x)

Tidy data

# Spectra IDs contain full/absolute path 
# This is annoying for downstream analysis
# These can be shortened as such:
head(hs.x@data$filename, 5)
hs.x <- hs_tidy_filenames(hs.x, remove_pattern = "path")
head(hs.x@data$filename, 5)

Preprocess data

# Denoise/clean
hs.x.proc <- hs_preprocess(hs.x, smooth = FALSE)
summary(hs.x.proc)

# Resample if desired
hs.x.proc.r <- hs_resample(hs.x.proc, sample = 60, replace = FALSE)
summary(hs.x.proc.r)

Contrast analysis

# Make contrast between first 5 cell spectra and 50 other cell spectra
hs.x.cont <- hs_contrast(hs.x.proc, comp1 = c(1:5), comp2 = c(10:60))
head(hs.x.cont)

PCA analysis

hs.x.pca <- hs_PCA(hs.x.proc)
head(hs.x.pca)

Hierarchical clustering analysis

# Cluster
hs.x.hclust <- hs_hclust(hs.x.proc, clust_method = "hclust")
summary(hs.x.hclust)

# Draw tree
plot(hs.x.hclust)

# Draw cut-off
hs_hclust_cutoff(hs.x.hclust, h = 0.5)

Hierarchical clustering analysis with bootstrap support

This bootstrap clustering takes some minutes to run, that's why it its commented out here.

# Cluster
# hs.x.pvclust <- hs_hclust(hs.x.proc, clust_method = "pvclust", nboot = 10, dist_method = "SCA")
# 
# # Plot tree
# library("pvclust")
# plot(hs.x.pvclust)
# 
# # Plot confidence threshold
# pvclust::pvrect(hs.x.pvclust, alpha = 0.05)

Cluster cells using PAM

hs.type <- hs_type(hs.x.proc, PCA.var = 0.90)
head(hs.type)

tSNE analysis

hs.x.tsne <- hs_tsne(hs.x.proc)
head(hs.x.tsne)

Phenotypic heterogeneity analysis

# On normalized data
hs_diversity <- hs_phenoRam(hs.x.proc, preprocess = FALSE)
head(hs_diversity)

# Using peak detection
hs_diversity_peak <- hs_phenoRam(hs.x.proc, preprocess = FALSE,
  peak_detection = TRUE, peak_method = "MAD")
head(hs_diversity_peak)

# Check sample size effect 
hs_coll_curves <- hs_coll_curve(hs.x.proc, intervals = 10, nboot = 100, plot_fig = TRUE)
head(hs_coll_curves)

Random forest model

# Create mock-up metadata
mock_meta <- data.frame(Spectrum_ID = rownames(hs.x.proc@data$spc),
group = factor(c(rep(1, 30), rep(2, 34))))

# Calculate metrics
hs.RF <- hs_RF(
  hs.x = hs.x.proc,
  metadata = mock_meta,
  spectrumID_col = "Spectrum_ID",
  target_var = "group",
  ntree = 500,
  p_train = 0.75
)

# Trained model
print(hs.RF[[1]])

# Confusion matrix
print(hs.RF[[2]])

# Variable importance metric
caret::varImp(hs.RF[[1]])

# Perform predictions of group labels on "new data"
hs_RF_pred(hs.x = hs.x.proc, model = hs.RF[[1]])

Convenience functions

# Convert from hyperspec to maldiquant object
mq.conv <- hs_conv_mq(hs.x.proc)
head(mq.conv)

# Vice versa
hs.conv <- mq_conv_hs(mq.conv)
head(hs.conv)



CMET-UGent/MicroRaman documentation built on July 25, 2020, 6:20 p.m.