R/splitSpectraGroups.R
splitSpectraGroups.Rd
This function takes an existing Spectra
object and uses your
instructions to split the existing spectra$groups
into new groups.
The new groups are added to the existing Spectra
object (a
list) as new elements. This allows one to use different combinations of
factors than were originally encoded in the Spectra
object.
The option also exists to replace the color scheme with one which
corresponds to the new factors.
splitSpectraGroups(spectra, inst = NULL, rep.cols = NULL, ...)
An object of S3 class Spectra()
.
A list giving the name of the new element to be created from a set of target strings given in a character vector. See the example for the syntax.
Optional. A vector giving new colors which correspond to
the levels of inst
. Only possible if inst
has only one
element, as the possible combinations of levels and colors may get
complicated.
Additional arguments to be passed downstream. Currently not used.
An object of S3 class Spectra
, modified to have
additional elements as specified by inst
.
The items in the character vector are grepped among the existing
spectra$groups
entries; when found, they are placed in a new element
of Spectra
. In the example, all spectra$groups
entries
containing "G" are coded as "G" in a new element called spectra$env
,
and any entries containing "T" are handled likewise. This amounts to a sort
of recoding of factors (the example demonstrates this). Every entry in
spectra$groups
should be matched by one of the entries in the
character vector. If not, you will get NA
entries. Also, if the targets
in the character vector are not unique, your results will reflect the order
of the levels. Since this is a grep process, you can pass any valid grep
string as the target.
If rep.cols
is provided, these colors are mapped one for one onto the
levels of the the first element of inst
. This provides a different
means of changing the sample color encoding than conColScheme
.
conColScheme
Additional documentation at https://bryanhanson.github.io/ChemoSpec/
data(metMUD2)
levels(metMUD2$groups) # original factor encoding
#> [1] "AbcD" "AbCD" "ABcD" "ABCD"
# Split those original levels into 2 new ones (re-code them)
new.grps <- list(geneBb = c("B", "b"), geneCc = c("C", "c"))
res <- splitSpectraGroups(metMUD2, new.grps)
#> Additional data was found: geneBb
#> Additional data was found: geneCc
str(res) # note two new elements, "geneBb" and "geneCc"
#> List of 11
#> $ freq : num [1:1500] 5 5 4.99 4.99 4.99 ...
#> $ data : num [1:20, 1:1500] 0.204 0.208 0.206 0.214 0.219 ...
#> ..- attr(*, "dimnames")=List of 2
#> .. ..$ : NULL
#> .. ..$ : NULL
#> $ names : chr [1:20] "sample_1" "sample_2" "sample_3" "sample_4" ...
#> $ groups : Factor w/ 4 levels "AbcD","AbCD",..: 4 4 4 4 4 3 3 3 3 3 ...
#> $ colors : chr [1:20] "black" "black" "black" "black" ...
#> $ sym : num [1:20] 0 0 0 0 0 1 1 1 1 1 ...
#> $ alt.sym: chr [1:20] "w" "w" "w" "w" ...
#> $ unit : chr [1:2] "ppm" "intensity"
#> $ desc : chr "metMUD2: 4 groups; 4 metabolites: 2 +correlated, 2 -correlated, 1 random"
#> $ geneBb : Factor w/ 2 levels "B","b": 1 1 1 1 1 1 1 1 1 1 ...
#> $ geneCc : Factor w/ 2 levels "C","c": 1 1 1 1 1 2 2 2 2 2 ...
#> - attr(*, "class")= chr "Spectra"
sumSpectra(res) # reports on extra elements
#> Additional data was found: geneBb
#> Additional data was found: geneCc
#> Additional data was found: geneBb
#> Additional data was found: geneCc
#>
#> metMUD2: 4 groups; 4 metabolites: 2 +correlated, 2 -correlated, 1 random
#>
#> There are 20 spectra in this set.
#> The y-axis unit is intensity.
#>
#> The frequency scale runs from
#> 5 to 0 ppm
#> There are 1500 frequency values.
#> The frequency resolution is
#> 0.003335557 ppm/point.
#>
#>
#> The spectra are divided into 4 groups:
#>
#> group no. color symbol alt.sym
#> 1 AbcD 5 blue 3 z
#> 2 AbCD 5 green 2 y
#> 3 ABcD 5 red 1 x
#> 4 ABCD 5 black 0 w
#>
#> Additional data was found: geneBb
#> Additional data was found: geneCc
#>
#> *** Note: this is an S3 object
#> of class 'Spectra'
# Note that if you want to use a newly created group in
# plotScores and other functions to drive the color scheme
# and labeling, you'll have to update the groups element:
res$groups <- as.factor(paste(res$geneBb, res$geneCc, sep = ""))