sedesign_to_factors: Convert SEDesign to data.frame of design factors

sedesign_to_factorsR Documentation

Convert SEDesign to data.frame of design factors

Description

Convert SEDesign to data.frame of design factors

Usage

sedesign_to_factors(
  sedesign,
  se = NULL,
  factor_names = NULL,
  factor_sep = "_",
  default_order = c("appearance", "mixedSort"),
  verbose = FALSE,
  ...
)

Arguments

sedesign

SEDesign object

se

SummarizedExperiment object which contains SummarizedExperiment::colData() with columns of design annotations. When present, values in the factor data.frame will be matched to values in these columns in order to re-use factor levels where possible. When factor_names is not defined, this step will also re-use the associated colnames from colData(se) when the values are matched.

factor_names

character vector of colnames to use for the resulting design factor colnames.

factor_sep

character string with expected delimiter between factors, default "_", for example "WT_Treated".

default_order

character string indicating how to order factor levels when a colname in the factor data.frame is not a factor in the matching colData() column.

  • "appearance": factor levels are defined in the order they appear in the sample data

  • "mixedSort": factor levels are defined by sorting data using jamba::mixedSort() which performs proper alphanumeric sorting, such that "Treat2" appears before "Treat10" for example.

verbose

logical indicating whether to print verbose output.

Details

This function is a utility function intended to convert SEDesign objects, which contain a design matrix, into a data.frame representing the experimental design factors used to produce the design matrix.

Value

data.frame with one column for each experiment design factor, where each column is converted to factor for non-numeric columns, and where colnames are defined as follows:

  • use factor_colnames when supplied

  • use the corresponding column in colData(se) when se is supplied

  • use factor# format for all other columns

See Also

Other jam experiment design: check_sedesign(), contrast_colors_by_group(), contrasts_to_factors(), filter_contrast_names(), groups_to_sedesign(), plot_sedesign()

Examples

isamples_1 <- paste0(
   rep(c("DMSO", "Etop", "DMSO", "Etop"), each=6),
   "_",
   rep(c("NF", "Flag"), each=12),
   "_",
   rep(c("WT", "KO", "WT", "KO", "WT", "D955N", "WT", "D955N"), each=3),
   "_",
   LETTERS[1:3])
# simple data.frame with group information
idf <- data.frame(jamba::rbindList(strsplit(isamples_1, "_")))[,1:3]
rownames(idf) <- isamples_1;
# convert to sedesign
sedesign <- groups_to_sedesign(idf)
plot_sedesign(sedesign, axis1=2, axis2=3, axis3=1, label_cex=0.5)

# prepare colData data.frame
cdf <- data.frame(check.names=FALSE, stringsAsFactors=FALSE,
   jamba::rbindList(strsplit(isamples_1, "_")))
colnames(cdf) <- c("Treatment", "Flag", "Genotype", "Rep")
rownames(cdf) <- sedesign@samples;
cdf

# prepare assay matrix
imatrix <- matrix(data=seq_len(nrow(cdf) * 10), ncol=nrow(cdf));
colnames(imatrix) <- rownames(cdf);
rownames(imatrix) <- paste0("row", 1:10);
# prepare se
se <- SummarizedExperiment::SummarizedExperiment(
   assays=list(raw=imatrix),
   colData=cdf)

sedesign_to_factors(sedesign, se=se)

# confirm first column contains proper factor order
sedesign_to_factors(sedesign, se=se)[,1]

# demonstrate reverse order of Treatment column levels
SummarizedExperiment::colData(se)$Treatment <- factor(
   SummarizedExperiment::colData(se)$Treatment,
   levels=c("Etop", "DMSO"))
sedesign_to_factors(sedesign, se=se)[,1]

# define Treatment column levels again
SummarizedExperiment::colData(se)$Treatment <- factor(
   SummarizedExperiment::colData(se)$Treatment,
   levels=c("DMSO", "Etop"))
sedesign_to_factors(sedesign, se=se)[,1]

# provide specific colnames to use from se object
sedesign_to_factors(sedesign,
   se=se,
   factor_names=c("Treatment", "Flag", "Genotyoe"))
sedesign_to_factors(sedesign,
   se=se,
   factor_names=c("Treatment", "Flag", "Genotyoe"))[,1]

# substring of colnames(colData(se)) is acceptable
sedesign_to_factors(sedesign,
   factor_names=c("Treat", "Flag", "Genotyoe"),
   se=se)[,1]


jmw86069/jamses documentation built on May 31, 2024, 1:36 p.m.