groups2contrasts | R Documentation |
Define experimental contrasts from sample groups
groups2contrasts(
iFactors,
groupColumns = NULL,
iSamples = NULL,
iDesign = NULL,
factorOrder = NULL,
omitGrep = "[-,]",
maxDepth = 2,
currentDepth = 1,
factorSep = "_",
contrastSep = "-",
renameFirstDepth = TRUE,
returnDesign = FALSE,
removePairs = NULL,
makeUnique = TRUE,
addContrastNamesDF = NULL,
preControlTerms = NULL,
verbose = FALSE,
...
)
iFactors |
vector of sample groups with one entry per sample, or data.frame whose colnames are experimental factors, and rows are samples. |
groupColumns |
character vector or NULL, to define an optional
subset of colnames when |
iSamples |
character vector or NULL, optionally used to subset the sample identifiers used in subsequent steps. Note that only groups and contrasts that contain samples will be defined. |
iDesign |
optional numeric design matrix, an optional method of defining sample-to-group mapping. |
factorOrder |
integer vector, optionally used to define the
order of factor contrasts when there are multiple experimental
factors. It can be helpful to force a secondary factor to be
compared before a primary factor especially in two-way contrasts.
Note that |
omitGrep |
character grep pattern used to exclude secondary factors from contrasts, mainly used internally by this function. |
maxDepth |
integer value, the maximum number of factor "depth"
to define contrasts, for example |
currentDepth |
integer value used internally by |
factorSep , contrastSep |
character values used as delimiter in factor and contrast names, respectively. |
renameFirstDepth |
logical used internally for iterative calls
to |
returnDesign |
logical indicating whether to return the full
set of design ( |
removePairs |
list of pairwise vectors of factors which should not be compared, or NULL to include all comparisons. The values in each vector should be factor levels that should not be compared. When the vector contains only one value, it removes contrasts where that factor is not changed, which is relevant when there are two or more factors. |
makeUnique |
logical indicating whether to make output contrasts unique. |
addContrastNamesDF |
data.frame or NULL, optionally used to append
to the calculated |
preControlTerms |
character vector or NULL, optionally used to
help define factor order, for example |
verbose |
logical indicating whether to print verbose output. |
... |
additional arguments are ignored. |
This function is intended to define statistical contrasts that compare one factor at a time. For two-factor designs, it will create two-way contrasts, defined as the contrast of pairwise contrasts.
Input can be a character vector of group names, where by
default each factor is separated by an underscore "_"
.
An example might be:
iFactors <- c("Control_Wildtype", "Control_Knockout", "Treated_Wildtype", "Treated_Knockout")
In that case, there are two factors. The first factor
contains factor levels c("Control", "Treated")
, and
the second factor contains factor levels
c("Wildtype", "Knockout")
.
Input can also be a data.frame
(or compatible table-like
object including data.table
and tibble
). Each column
is considered a factor. From the example above, we can
create a data.frame
using jamba::rbindList()
,
see the Examples for more detail.
jamba::rbindList(strsplit(iFactors, "_"))
Lastly, if the input is a named vector, or a data.frame
with rownames,
This function will change any "-"
in a factor name to
"."
prior to detecting valid contrasts. Note that
groups2contrasts()
does not call base::make.names()
because that function too aggressively converts characters
to "."
. If data must be compliant with the rules used
by base::make.names()
, run that function prior to calling
groups2contrasts()
.
list of data matrices: iDesign
numeric design matrix;
iContrasts
numeric contrast matrix; contrastNames
data.frame
showing the full factor breakdown, with colnames "contrastName"
which shows a text contrast suitable for use in limma::makeContrasts()
.
When returnDesign=FALSE
the output is only the contrastNames
data.frame.
Other jam RNA-seq functions:
assignGRLexonNames()
,
closestExonToJunctions()
,
combineGRcoverage()
,
defineDetectedTx()
,
detectedTxInfo()
,
exoncov2polygon()
,
flattenExonsBy()
,
getGRcoverageFromBw()
,
internal_junc_score()
,
makeTx2geneFromGtf()
,
make_ref2compressed()
,
prepareSashimi()
,
runDiffSplice()
,
sortSamples()
,
spliceGR2junctionDF()
Other jam design functions:
curateDFtoDF()
,
curateVtoDF()
# first define a vector of sample groups
iGroups <- jamba::nameVector(paste(rep(c("WT", "KO"), each=6),
rep(c("Control", "Treated"), each=3),
sep="_"));
iGroups <- factor(iGroups, levels=unique(iGroups));
iGroups;
iDesignL <- groups2contrasts(iGroups, returnDesign=TRUE);
iDesignL$iDesign;
iDesignL$iContrasts;
# now you can visualize the samples used in each contrast
iDesignL$iDesign %*% iDesignL$iContrasts;
# you can adjust the order of factor levels per comparison
groups2contrasts(as.character(iGroups))$contrastName
# make "WT" the first control term
groups2contrasts(as.character(iGroups), preControlTerms=c("WT"), factorOrder=2:1)$contrastName
# prevent comparisons of WT to WT, or KO to KO
groups2contrasts(as.character(iGroups),
removePairs=list(c("WT"), c("KO")))
# input as a data.frame with ordered factor levels
iFactors <- data.frame(Genotype=factor(c("WT","WT","KO","KO"),
levels=c("WT","KO")),
Treatment=factor(c("Treated","Control"),
levels=c("Control","Treated")))
iFactors;
groups2contrasts(iFactors)
# Again remove WT-WT and KO-KO contrasts
groups2contrasts(iFactors,
removePairs=list(c("WT"), c("KO")))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.