% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/qcByCmdscale.R
\name{qcByCmdscale}
\alias{qcByCmdscale}
\title{Quality control using classical MDS and outlier detection}
\usage{
qcByCmdscale(
  CrcBiomeScreenObject,
  TaskName = NULL,
  outdir = tempdir(),
  normalize_method = NULL,
  threshold_sd = 1,
  plot = FALSE
)
}
\arguments{
\item{CrcBiomeScreenObject}{A \code{CrcBiomeScreenObject} containing normalized microbiome data, sample metadata, etc.}

\item{TaskName}{A character string used to label the output plot and PDF filename.}

\item{outdir}{The output directory where plots will be saved (default: tempdir()).description}

\item{normalize_method}{A character string indicating the normalization method used (e.g., \code{"GMPR"}). Used for labeling only.}

\item{threshold_sd}{Numeric value indicating how many standard deviations above the mean
distance should be considered an outlier (default is 1).}

\item{plot}{Logical value indicating whether to generate and save the MDS plot (default is TRUE).}
}
\value{
A modified \code{CrcBiomeScreenObject} where:
\itemize{
\item \code{NormalizedData} contains filtered data with outliers removed.
\item \code{SampleData} is updated to exclude outlier samples.
\item \code{OutlierSamples} is a character vector of sample IDs identified as outliers.
\item \code{OriginalNormalizedData} stores the unfiltered data matrix before QC.
}

A A \code{CrcBiomeScreen} object. with outliers.
}
\description{
This function performs quality control on microbiome data by applying classical
multidimensional scaling (MDS) on the relative abundance matrix. It identifies outlier samples
based on their Euclidean distance to the centroid in the first two MDS dimensions.
}
\details{
Outlier samples are removed from the normalized matrix and sample metadata, and the results
are visualized and saved as a PDF.

The function calculates the Euclidean distance between samples in the 2D MDS space.
Samples whose distance to the centroid exceeds the threshold (mean + \code{threshold_sd} * SD)
are considered outliers.

A PDF plot is saved to the working directory, showing sample positions in MDS space
with outliers highlighted in red.
}
\examples{
# Minimal toy object for QC example
toy_sampledata <- data.frame(
  sample_id = paste0("S", 1:4),
  study_condition = c("control", "CRC", "control", "CRC"),
  row.names = paste0("S", 1:4)
)

# Samples in rows and features in columns
toy_norm <- data.frame(
  g1 = c(1, 2, 1, 3),
  g2 = c(2, 3, 1, 2),
  g3 = c(3, 4, 1, 1),
  row.names = paste0("S", 1:4)
)

toy_obj <- new(
  "CrcBiomeScreen",
  AbsoluteAbundance = data.frame(),
  RelativeAbundance = data.frame(),
  TaxaData = data.frame(),
  SampleData = toy_sampledata,
  NormalizedData = toy_norm,
  ModelData = list()
)

qc_obj <- qcByCmdscale(
  toy_obj,
  TaskName = "ToyQC",
  normalize_method = "GMPR",
  threshold = 1
)
}
