Skip to content

Commit

Permalink
isotopia update
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Kopf committed May 28, 2014
1 parent b2df89d commit 7975a1e
Show file tree
Hide file tree
Showing 17 changed files with 62 additions and 59 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
Package: isotopia
Type: Package
Title: Work with isotopic data in R
Version: 0.2
Version: 0.3
Date: 2014-04-05
Author: Sebastian Kopf
Maintainer: Sebastian Kopf <[email protected]>
Description: R interface for working with isotopic data (abundances, ratios,
delta values, etc.).
fractionation factors, delta values, etc.).
License: GPL-3
LazyLoad: yes
Roxygen: list(wrap = FALSE)
Expand Down
7 changes: 3 additions & 4 deletions R/arithmetic.R
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,7 @@ setMethod("+", signature(e1 = "Abundance", e2 = "Abundance"), function(e1, e2) {
#' @usage delta +- delta
#' @details
#' \code{delta+-delta} is a shorthand for calculating the isotopic mass balance of
#' two \code{\link{delta}} objects, see \code{\link{mass_balance}} for details,
#' will use the global \code{\link{exact_mass_balance}} setting for the calculation
#' two \code{\link{delta}} objects, see \code{\link{mass_balance}} for details
#' @name arithmetic
#' @rdname arithmetic
NULL
Expand Down Expand Up @@ -329,9 +328,9 @@ setMethod("/", signature(e1 = "FractionationFactor", e2 = "FractionationFactor")

#' @usage delta / delta
#' @details
#' \code{delta/delta} creates an \code{\link{alpha}} fractionation factor object that
#' \code{delta/delta} creates an \code{\link{fractionation_factor}} object that
#' describes the fractionation factor between the two compounds, requires the reference
#' name of the two delta values to be identical. This is a shorthand for the \link{frac_factor} function.
#' name of the two delta values to be identical. This is a shorthand for the \link{to_ff} function.
#' @name arithmetic
#' @rdname arithmetic
NULL
Expand Down
12 changes: 6 additions & 6 deletions R/attribs.R
Original file line number Diff line number Diff line change
Expand Up @@ -154,23 +154,23 @@ setMethod("weight", signature("Isoval", "numeric"), function(iso, weight) {
#' primitive data value(s).
#'
#' @param notation specifiy which notation to return the value in (default is the notation
#' that hte object is in)
#' that the object is in)
#' @return In the case of a single isotope object (Isoval), returns the numeric
#' vector of raw values stored in the object (same as \code{\link{as.numeric}}).
#' vector of raw values stored in the object (same as \code{\link[base]{as.numeric}}).
#' In the case of an isotope system (Isosys),
#' returns the data frame underlying the object with all its isotope value
#' objects also replaced with their numeric raw values. To just get the data
#' frame but keep the isotope values intact, use \code{\link{as.data.frame}} instead.
#' @seealso \code{\link{as.numeric}}, \code{\link{as.data.frame}}, \code{\link[base]{as.data.frame}} (base method)
#' @seealso \code{\link[base]{as.numeric}}, \code{\link{as.data.frame}}, \code{\link[base]{as.data.frame}} (base method)
#' @family data type attributes
#' @method get_value
#' @export
setGeneric("get_value", function(iso, notation = "raw") standardGeneric("get_value"))
setGeneric("get_value", function(iso, notation = iso@notation) standardGeneric("get_value"))

#' @method get_value
#' @export
setMethod("get_value", "ANY", function(iso, notation = "raw") stop("get_value not defined for objects of class ", class(iso)))
setMethod("get_value", "numeric", function(iso, notation = "raw") iso) # allow this for simplicity so this is similiar to as_numeric
setMethod("get_value", "ANY", function(iso, notation) stop("get_value not defined for objects of class ", class(iso)))
setMethod("get_value", "numeric", function(iso, notation = NULL) iso) # allow this for simplicity so this is similiar to as_numeric
setMethod("get_value", "Isoval", function(iso, notation = iso@notation) as.numeric(switch_notation(iso, notation)))
setMethod("get_value", "Isosys", function(iso, notation = NULL) {
data.frame(lapply(iso,
Expand Down
11 changes: 9 additions & 2 deletions R/conversion.R
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ setMethod("to_ratio", "Delta", function(iso) {
else if (length(stds) > 1)
message("No reference ratio registered with the delta value, tried to find one from the registered standards but found multiple, delta: ", get_label(iso))
else if (length(stds) == 1) {
message("Successfully found a matching standard to convert delta value without registered reference: ", get_label(stds[[1]]), ": ", get_value(stds[[1]]))
message("Successfully found a registered standard to convert delta value: ", get_label(stds[[1]]), ": ", signif(get_value(stds[[1]]), 4))
iso@ref_ratio <- get_value(stds[[1]])
}
}
Expand Down Expand Up @@ -200,6 +200,7 @@ setMethod("to_abundance", "Intensities", function(iso) to_abundance(to_ratio(iso

# delta to abundance
setMethod("to_abundance", "Delta", function(iso) {
# FIXME: add warning for isotope systems with more than one ratio?
r <- to_ratio(iso)
to_abundance(r)
})
Expand All @@ -213,7 +214,7 @@ setMethod("to_abundance", "Delta", function(iso) {
#'
#' @usage to_ff(iso1, iso2)
#' @details
#' The \code{frac_factor(...)} function calculates the fractionation factor between two isotope data objects
#' The \code{to_ff(...)} function calculates the fractionation factor between two isotope data objects
#' (for example two \code{\link{delta}} values, two \code{\link{ratio}}, or two \code{\link{ff}}).
#' All calculatinos are only permissible if the isotope values have matching
#' attributes and fractionation factors will be returend in the default notation
Expand Down Expand Up @@ -324,6 +325,12 @@ setMethod("to_delta", signature(iso = "FractionationFactor", ref_ratio = "Ratio"

# ratio to delta =========

# ratio to delta (with numeric ref ratio)
setMethod("to_delta", signature("Ratio", "missing"), function(iso, ref_ratio) {
stop("Can't convert from a ratio to a delta value without a reference ratio.")
})


# ratio to delta (with numeric ref ratio)
setMethod("to_delta", signature("Ratio", "numeric"), function(iso, ref_ratio) {
to_delta(iso, update_iso(ratio(ref_ratio), list(isoname = iso@isoname, major = iso@isoname)))
Expand Down
11 changes: 5 additions & 6 deletions R/initialization.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ ab <- abundance

#' Fractionation factor
#'
#' FIXME, change description
#' Generate a fractionation factor (alpha value = ratio of two isotope ratios) object. See \link{isotopia} for general information on initializing
#' and converting isotope data objects.
#' Fractionation factors can be easily converted from values in one notation to values in
#' Generate a fractionation factor object. Can be initialized in \code{alpha} and
#' \code{epsilon} notation. Fractionation factors can be easily converted from values in one notation to values in
#' another notation by using \code{\link{switch_notation}}.
#'
#' See \link{isotopia} for general information on initializing
#' and converting to other isotope data objects.
#' The \code{ff} function is a shorthand for \code{fractionation_factor} but otherwise identical.
#'
#' @param ... - numeric vectors (can be named) to turn into fractionation factors
Expand Down Expand Up @@ -90,8 +90,7 @@ ff <- fractionation_factor
#' another notation by using \code{\link{switch_notation}}.
#'
#' For mass balance calculations with delta values, simply add the appropriate weights (if different from
#' the default) and use \code{delta(...) + delta(...)}. Use \code{\link{exact_mass_balance}(TRUE/FALSE)}
#' to adjust how these are calculated.
#' the default) and use \code{delta(...) + delta(...)}.
#'
#' @param ... - numeric vectors (can be named) to turn into delta values
#' @param major - name of the major isotope in the isotope system [optional]
Expand Down
18 changes: 9 additions & 9 deletions R/operations.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,26 @@ NULL
#' is also implemented with an \code{\link{arithmetic}} shorthand.
#'
#' @param ... - any number of weighted isotope value objects (have to be all either abundance or delta)
#' @param exact - whether to calculate mass balance of delta values exactly (default FALSE),
#' use exact_mass_balance to set this paramter globally
#' @param exact - whether to calculate mass balance of delta values exactly (default FALSE), not
#' fully implemented yet
#' @return weighted abundance or delta value object that represents the combination of the parameters
#' @family operations
#' @method mass_balance
#' @export
setGeneric("mass_balance", function(iso, iso2, ..., exact = exact_mass_balance()) standardGeneric("mass_balance"))
setGeneric("mass_balance", function(iso, iso2, ..., exact = get_iso_opts("exact_mass_balance")) standardGeneric("mass_balance"))

#' @method mass_balance
#' @export
setMethod("mass_balance", "ANY", function(iso, iso2, ..., exact = exact_mass_balance()) stop("mass_balance not defined for objects of class ", class(iso), ", ", class(iso2)))
setMethod("mass_balance", "ANY", function(iso, iso2, ..., exact = get_iso_opts("exact_mass_balance")) stop("mass_balance not defined for objects of class ", class(iso), ", ", class(iso2)))

setMethod("mass_balance", signature("Abundance", "Abundance"), function(iso, iso2, ..., exact = exact_mass_balance()) {
setMethod("mass_balance", signature("Abundance", "Abundance"), function(iso, iso2, ..., exact = get_iso_opts("exact_mass_balance")) {
# consider implementing a performance optimized version for many additions
all <- c(list(iso2), list(...))
for (i in all)
iso = iso + i
iso
})
setMethod("mass_balance", signature("Delta", "Delta"), function(iso, iso2, ..., exact = exact_mass_balance()) {
setMethod("mass_balance", signature("Delta", "Delta"), function(iso, iso2, ..., exact = get_iso_opts("exact_mass_balance")) {
if (exact)
stop("not implemented yet!") # should be implemented here rather than in the operators

Expand All @@ -38,15 +38,15 @@ setMethod("mass_balance", signature("Delta", "Delta"), function(iso, iso2, ...,
iso
})

setMethod("mass_balance", signature("Deltas", "Deltas"), function(iso, iso2, ..., exact = exact_mass_balance()) {
setMethod("mass_balance", signature("Deltas", "Deltas"), function(iso, iso2, ..., exact = get_iso_opts("exact_mass_balance")) {
stop("not implemented yet")
})

#' Fractionate an isotopic value
#'
#' This function calculates the outcome of isotopic fractionation by a \code{\link{fractionation_fractor}}
#' This function calculates the outcome of isotopic fractionation by a \code{\link{fractionation_factor}}
#' and can be applied to \code{\link{ratio}} data, \code{\link{delta}} values or other
#' \code{\link{fractionation_fractor}} objects.
#' \code{\link{fractionation_factor}} objects.
#' @param frac the fractionation factor \code{\link{ff}} used to fractionate the isotope value
#' @param iso the isotope object to fractionate
#' @return an object of the same type as \code{iso}
Expand Down
2 changes: 1 addition & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ drop_list <- function(l) {
#'
#' This small utility function is just a convenient wrapper for running
#' isotope calculations silently without outputting any of the warnings or
#' messages (it uses \link{suppressMessages} and \link{suppressWarnings}
#' messages (it uses \link[base]{suppressMessages} and \link[base]{suppressWarnings}
#' internally) that might occur. Use with care to suppress warnings, you
#' might end up hiding important information.
#' @export
Expand Down
14 changes: 7 additions & 7 deletions R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ setLoadActions(function(ns) {
exact_mass_balance = FALSE
)

# suppressWarnings({
# register_standard(ratio(`2H` = to_ratio(abundance(0.00015574)), major = "1H", compound = "VSMOW")) # from IUPAC report
# register_standard(ratio(`13C` = 0.011237, major = "12C", compound = "VPDB"))
# register_standard(ratio(`15N` = 0.003677, major = "14N", compound = "Air"))
# register_standard(ratio(`18O` = 0.0020052, major = "16O", compound = "VSMOW"))
# register_standard(ratio(`34S` = 0.0045005, major = "32S", compound = "CDT"))
# })
suppressWarnings({
register_standard(ratio(`2H` = to_ratio(abundance(0.00015574)), major = "1H", compound = "VSMOW")) # from IUPAC report
register_standard(ratio(`13C` = 0.011237, major = "12C", compound = "VPDB"))
register_standard(ratio(`15N` = 0.003677, major = "14N", compound = "Air"))
register_standard(ratio(`18O` = 0.0020052, major = "16O", compound = "VSMOW"))
register_standard(ratio(`34S` = 0.0045005, major = "32S", compound = "CDT"))
})
})
7 changes: 3 additions & 4 deletions man/arithmetic.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ have matching attributes (isotope name, major isotope, etc.).
two \code{\link{abundance}} objects, see \code{\link{mass_balance}} for details

\code{delta+-delta} is a shorthand for calculating the isotopic mass balance of
two \code{\link{delta}} objects, see \code{\link{mass_balance}} for details,
will use the global \code{\link{exact_mass_balance}} setting for the calculation
two \code{\link{delta}} objects, see \code{\link{mass_balance}} for details

\code{alpha - 1} is a shorthand for converting a fractionation factor from
alpha to epsilon notation. The ff object has to be in alpha notation,
Expand Down Expand Up @@ -69,8 +68,8 @@ This is a shorthand for the \link{to_ff} function.
either the denominator names or numerator names of the two objects are identical (i.e. they "cancel").
This is a shorthand for the \link{to_ff} function.

\code{delta/delta} creates an \code{\link{alpha}} fractionation factor object that
\code{delta/delta} creates an \code{\link{fractionation_factor}} object that
describes the fractionation factor between the two compounds, requires the reference
name of the two delta values to be identical. This is a shorthand for the \link{frac_factor} function.
name of the two delta values to be identical. This is a shorthand for the \link{to_ff} function.
}

3 changes: 1 addition & 2 deletions man/delta.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ another notation by using \code{\link{switch_notation}}.
}
\details{
For mass balance calculations with delta values, simply add the appropriate weights (if different from
the default) and use \code{delta(...) + delta(...)}. Use \code{\link{exact_mass_balance}(TRUE/FALSE)}
to adjust how these are calculated.
the default) and use \code{delta(...) + delta(...)}.
}
\examples{
delta(50, notation = "permil") # enter as permil value
Expand Down
4 changes: 2 additions & 2 deletions man/fractionate.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ fractionate(frac, iso)
an object of the same type as \code{iso}
}
\description{
This function calculates the outcome of isotopic fractionation by a \code{\link{fractionation_fractor}}
This function calculates the outcome of isotopic fractionation by a \code{\link{fractionation_factor}}
and can be applied to \code{\link{ratio}} data, \code{\link{delta}} values or other
\code{\link{fractionation_fractor}} objects.
\code{\link{fractionation_factor}} objects.
}
\note{
Several of these calculations are also
Expand Down
8 changes: 4 additions & 4 deletions man/fractionation_factor.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ See \code{\link{switch_notation}} on details how to convert between notations.}
\item{cbot}{- name of the compound representing the bottom isotope ratio [optional]}
}
\description{
FIXME, change description
Generate a fractionation factor (alpha value = ratio of two isotope ratios) object. See \link{isotopia} for general information on initializing
and converting isotope data objects.
Fractionation factors can be easily converted from values in one notation to values in
Generate a fractionation factor object. Can be initialized in \code{alpha} and
\code{epsilon} notation. Fractionation factors can be easily converted from values in one notation to values in
another notation by using \code{\link{switch_notation}}.
}
\details{
See \link{isotopia} for general information on initializing
and converting to other isotope data objects.
The \code{ff} function is a shorthand for \code{fractionation_factor} but otherwise identical.
}
\seealso{
Expand Down
8 changes: 4 additions & 4 deletions man/get_value.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
\alias{get_value}
\title{Retrieve isotope object's primitive values}
\usage{
get_value(iso, notation = "raw")
get_value(iso, notation = iso@notation)
}
\arguments{
\item{notation}{specifiy which notation to return the value in (default is the notation
that hte object is in)}
that the object is in)}
}
\value{
In the case of a single isotope object (Isoval), returns the numeric
vector of raw values stored in the object (same as \code{\link{as.numeric}}).
vector of raw values stored in the object (same as \code{\link[base]{as.numeric}}).
In the case of an isotope system (Isosys),
returns the data frame underlying the object with all its isotope value
objects also replaced with their numeric raw values. To just get the data
Expand All @@ -22,7 +22,7 @@ This function returns an isotope object's (single value or isotope system)
primitive data value(s).
}
\seealso{
\code{\link{as.numeric}}, \code{\link{as.data.frame}}, \code{\link[base]{as.data.frame}} (base method)
\code{\link[base]{as.numeric}}, \code{\link{as.data.frame}}, \code{\link[base]{as.data.frame}} (base method)

Other data type attributes: \code{\link{get_label}},
\code{\link{get_name}}, \code{\link{get_units}};
Expand Down
6 changes: 3 additions & 3 deletions man/mass_balance.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
\alias{mass_balance}
\title{Calculate isotope mass balance}
\usage{
mass_balance(iso, iso2, ..., exact = exact_mass_balance())
mass_balance(iso, iso2, ..., exact = get_iso_opts("exact_mass_balance"))
}
\arguments{
\item{...}{- any number of weighted isotope value objects (have to be all either abundance or delta)}

\item{exact}{- whether to calculate mass balance of delta values exactly (default FALSE),
use exact_mass_balance to set this paramter globally}
\item{exact}{- whether to calculate mass balance of delta values exactly (default FALSE), not
fully implemented yet}
}
\value{
weighted abundance or delta value object that represents the combination of the parameters
Expand Down
2 changes: 1 addition & 1 deletion man/quietly.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ quietly(expr)
\description{
This small utility function is just a convenient wrapper for running
isotope calculations silently without outputting any of the warnings or
messages (it uses \link{suppressMessages} and \link{suppressWarnings}
messages (it uses \link[base]{suppressMessages} and \link[base]{suppressWarnings}
internally) that might occur. Use with care to suppress warnings, you
might end up hiding important information.
}
Expand Down
2 changes: 1 addition & 1 deletion man/to_ff.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ isotope \code{\link{fraction_factor}} object if parameters can be converted, an
Calculate/convert to an isotope \code{\link{fractionation_factor}}
}
\details{
The \code{frac_factor(...)} function calculates the fractionation factor between two isotope data objects
The \code{to_ff(...)} function calculates the fractionation factor between two isotope data objects
(for example two \code{\link{delta}} values, two \code{\link{ratio}}, or two \code{\link{ff}}).
All calculatinos are only permissible if the isotope values have matching
attributes and fractionation factors will be returend in the default notation
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-05-data.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ test_that("Testing that reference standards can be properly registered and retri
expect_error(get_standard(name = "VSMOW"), "More than one")
expect_error(get_standard(minor = "3H"), "No reference ratio registered")
expect_equal(get_standard(minor = "2H", major = "1H", name = "VSMOW"), ratio(`2H` = 0.00015575, major = "1H", compound = "VSMOW"))
expect_message(to_ratio(delta(`2H` = -100, major = "1H", ref = "VSMOW")), "Successfully found a matching standard")
expect_message(to_ratio(delta(`2H` = -100, major = "1H", ref = "VSMOW")), "Successfully found a registered standard")
expect_equal(to_ratio(delta(`2H` = 100, major = "1H", ref = "VSMOW")), ratio(`2H` = 1.1 * 0.00015575, major = "1H"))
expect_is(get_iso_opts("standards"), "data.frame")
})
Expand Down

0 comments on commit 7975a1e

Please sign in to comment.