Skip to content

Commit 6df6a14

Browse files
authored
Merge pull request #13 from EMSL-Computing/develop
Develop
2 parents 13634ad + 29cb349 commit 6df6a14

19 files changed

Lines changed: 122 additions & 51 deletions

R/assign_class.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ assign_class <- function(ftmsObj, boundary_set = "bs1", calc_ratios = TRUE){
7474

7575
comp_class = rep(NA, nrow(temp))
7676
comp_class[forms] = classes
77-
class_colname = paste(boundary_set, "_class", sep = "")
77+
class_colname = make.unique(c(colnames(ftmsObj$e_meta), paste(boundary_set, "_class", sep = "")))
78+
class_colname = class_colname[length(class_colname)]
7879

7980
# add the column to e_meta #
8081
temp[,class_colname] = comp_class

R/attributes_methods.R

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,32 @@ setGroupDF <- function(ftmsObj, group_df) {
7474
invisible(ftmsObj)
7575
}
7676

77+
#' Set the valence_DF attribute
78+
#'
79+
#' Stores a dataframe of various valence combinations in the valence_DF attribute of the ftmsObject
80+
#'
81+
#' @param ftmsObj an object of type ftmsData
82+
#' @param valences a dataframe with columns C, H, N, O, S, P and values representing valences for each element
83+
#' @return updated ftmsObj
84+
#'
85+
setDBEValenceDF <- function(ftmsObj, valences) {
86+
if (!inherits(ftmsObj, "ftmsData")) {
87+
stop("ftmsObj must be of type ftmsData")
88+
}
89+
if (!inherits(valences, "data.frame")){
90+
stop("valence_DF must be a data frame")
91+
}
92+
if(!all(colnames(valences) %in% c('C', 'H', 'N', 'O', 'S', 'P'))){
93+
stop("valence_DF must have column names: 'C, H, N, O, S, P'")
94+
}
95+
if(!all(sapply(valences, is.numeric))){
96+
stop("valence_DF must have numeric columns")
97+
}
98+
99+
attr(ftmsObj, "valence_DF") <- valences
100+
return(ftmsObj)
101+
}
102+
77103
#' Get data scale
78104
#'
79105
#' Get the data scale (e.g. 'abundance', 'pres', 'log2', 'log10', 'log')

R/calc_dbe.R

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#' Calculate double bond equivalent (DBE) and double bond equivalent minus Oxygen (DBE-O) values for peaks where empirical formula is available
44
#'
55
#' @param ftmsObj an object of class 'peakData' or 'compoundData', typically a result of \code{\link{as.peakData}} or \code{\link{mapPeaksToCompounds}}.
6-
#' @param valences a named list with the valence for each element. Names must be any of 'C', 'H', 'N', 'O', 'S', 'P'. Values must be integers corresponding to the valence for each element. Defaults to NULL, in which case the valences that result in the formula given in the details section are used.
6+
#' @param valences a dataframe with columns giving the valence for each element. Names must be any of 'C', 'H', 'N', 'O', 'S', 'P'. Values must be integers corresponding to the valence for each element. Defaults to NULL, in which case the valences that result in the formula given in the details section are used.
77
#'
88
#' @details
99
#' \tabular{ll}{
@@ -27,14 +27,13 @@ calc_dbe <- function(ftmsObj, valences = NULL){
2727

2828
# get coefficients that will multiply each elemental count. Each coefficient is equal to {valence}-2
2929
if(is.null(valences)){
30-
# coefficients that result in the equation given in @details
31-
coefs <- list('C' = 2, 'H' = -1, 'N' = 1, 'O' = 0, 'S' = 0, 'P' = 1)
30+
coefs <- data.frame('C' = 2, 'H' = -1, 'N' = 1, 'O' = 0, 'S' = 0, 'P' = 1) # coefficients that result in the equation given in @details
3231
}
3332
else{
34-
cond1 <- inherits(valences, 'list')
35-
cond2 <- all(names(valences) %in% c('C', 'H', 'N', 'O', 'S', 'P'))
36-
if(!all(cond1, cond2)) stop("argument valences must be a named list of integers with names 'C', 'H', 'N', 'O', 'S', 'P' and values representing the valence for each element.")
37-
coefs <- lapply(c('C', 'H', 'N', 'O', 'S', 'P'), function(x) if(!is.null(valences[[x]])) valences[[x]]-2 else 2)
33+
if(!inherits(valences, 'data.frame')) stop('valences must be a data frame')
34+
if(!all(names(valences) %in% c('C', 'H', 'N', 'O', 'S', 'P'))) stop("valences dataframe must have column names column names 'C', 'H', 'N', 'O', 'S', 'P'")
35+
if(!all(sapply(valences, is.numeric)) | any(is.na(rowSums(valences)))) stop('valences dataframe must contain all numeric, nonmissing values')
36+
coefs <- lapply(c('C', 'H', 'N', 'O', 'S', 'P'), function(x) if(!is.null(valences[[x]])) valences[[x]]-2 else 0) %>% data.frame()
3837
names(coefs) <- c('C', 'H', 'N', 'O', 'S', 'P')
3938
}
4039

@@ -49,23 +48,36 @@ calc_dbe <- function(ftmsObj, valences = NULL){
4948
S_counts = if(getSulfurColName(ftmsObj) %in% colnames(temp)) temp[,getSulfurColName(ftmsObj)] else 0
5049
P_counts = if(getPhosphorusColName(ftmsObj) %in% colnames(temp)) temp[,getPhosphorusColName(ftmsObj)] else 0
5150

52-
temp$DBE = 1 + 0.5*(coefs[['C']]*C_counts + coefs[['H']]*H_counts + coefs[['N']]*N_counts + coefs[['O']]*O_counts + coefs[['S']]*S_counts + coefs[['P']]*P_counts)
51+
dbe_cols <- NULL
5352

54-
temp$DBE_O = 1 + 0.5*(2*C_counts - H_counts + N_counts + P_counts) - O_counts
53+
for(i in 1:nrow(coefs)){
54+
row <- coefs[i,]
55+
DBE_id <- make.unique(c(colnames(temp$e_meta), paste0('DBE_', i)))[length(colnames(temp$e_meta)) + 1] #makes DBE_id unique if column already exists
56+
dbe_cols <- c(dbe_cols, DBE_id)
57+
58+
temp[DBE_id] = 1 + 0.5*(row[['C']]*C_counts + row[['H']]*H_counts + row[['N']]*N_counts + row[['O']]*O_counts + row[['S']]*S_counts + row[['P']]*P_counts)
59+
}
60+
61+
# reassign valences in case they input one with missing columns and it was fixed in the first if-else statement
62+
valences <- coefs + 2
63+
rownames(valences) = dbe_cols
5564

65+
# DBE_O and DBE_AI have fixed valences
66+
temp$DBE_O = 1 + 0.5*(2*C_counts - H_counts + N_counts + P_counts) - O_counts
5667
temp$DBE_AI = 1 + C_counts - O_counts - S_counts - 0.5*(N_counts + P_counts + H_counts)
5768

5869
if(length(which(is.na(temp[,getMFColName(ftmsObj)]))) > 0){
59-
temp$DBE[which(is.na(temp[,getMFColName(ftmsObj)]))] = NA
70+
temp[which(is.na(temp[,getMFColName(ftmsObj)])), dbe_cols] = NA
6071
temp$DBE_O[which(is.na(temp[,getMFColName(ftmsObj)]))] = NA
6172
temp$DBE_AI[which(is.na(temp[,getMFColName(ftmsObj)]))] = NA
6273
}
63-
74+
6475
ftmsObj$e_meta = temp
6576

66-
ftmsObj = setDBEColName(ftmsObj, "DBE")
77+
ftmsObj = setDBEColName(ftmsObj, dbe_cols)
6778
ftmsObj = setDBEoColName(ftmsObj, "DBE_O")
6879
ftmsObj = setDBEAIColName(ftmsObj, "DBE_AI")
80+
ftmsObj = setDBEValenceDF(ftmsObj, valences)
6981

7082
return(ftmsObj)
7183
}

R/colname_methods.R

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ getKendrickMassColName <- function(ftmsObj) {
601601
#' contains Kendrick mass data for Kendrick plots.
602602
#'
603603
#' @param ftmsObj an object of type ftmsData
604-
#' @param cnames column name
604+
#' @param cnames column names
605605
#' @return updated ftmsObj
606606
#'
607607
#'
@@ -637,7 +637,7 @@ getKendrickDefectColName <- function(ftmsObj) {
637637
#' contains Kendrick defect data for Kendrick plots.
638638
#'
639639
#' @param ftmsObj an object of type ftmsData
640-
#' @param cnames column name
640+
#' @param cnames column names
641641
#' @return updated ftmsObj
642642
#'
643643
#'
@@ -867,7 +867,7 @@ setGibbsColName <- function(ftmsObj, cname) {
867867

868868
#' Get the name of the double-bond equivalent column
869869
#'
870-
#' Gets the name of the column in the e\_meta element that contains double-bond equivalent values
870+
#' Gets the name of the column in the e_meta element that contains double-bond equivalent values
871871
#'
872872
#' @param ftmsObj an object of type ftmsData
873873
#' @return name of double-bond equivalent column
@@ -885,18 +885,18 @@ getDBEColName <- function(ftmsObj){
885885
#' contains double-bond equivalent information.
886886
#'
887887
#' @param ftmsObj an object of type ftmsData
888-
#' @param cname column name
888+
#' @param cnames column names
889889
#' @return updated ftmsObj
890890
#'
891891
#'
892-
setDBEColName <- function(ftmsObj, cname) {
892+
setDBEColName <- function(ftmsObj, cnames) {
893893
if (!inherits(ftmsObj, "ftmsData")) {
894894
stop("ftmsObj must be of type ftmsData")
895895
}
896-
if (!(cname %in% names(ftmsObj$e_meta))) {
897-
stop(sprintf("Column '%s' is not found in the e_meta data", cname))
896+
if (!all(cnames %in% names(ftmsObj$e_meta))) {
897+
stop(sprintf("Columns '%s' are not found in the e_meta data", paste(cnames, collapse = ',')))
898898
}
899-
attr(ftmsObj, "cnames")$dbe_cname <- cname
899+
attr(ftmsObj, "cnames")$dbe_cname <- cnames
900900
return(ftmsObj)
901901
}
902902

R/compound_calcs.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
#'
5252
#' # Specify extra arguments for calc_kendrick and calc_dbe.
5353
#' calc_args = list('calc_kendrick' = list('base_compounds' = c('CH2', 'CO2', 'H2')),
54-
#' 'calc_dbe' = list('valences' = list('C'=5, 'H' = 4, 'S' = 5)))
54+
#' 'calc_dbe' = list('valences' = data.frame('C'=5, 'H' = 4, 'S' = 5)))
5555
#'
5656
#' peakdata_processed <- compound_calcs(examplePeakData, calc_args = calc_args)
5757
#'

R/group_summary_functions.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
n_present <- function(x, data_scale) {
88
if (data_scale %in% c('pres', 'abundance')) {
99
if (identical(dim(x), NULL)) { # vector
10-
res <- as.integer(x>0)
10+
res <- as.integer(x>0 & !is.na(x))
1111
} else { # 2-dimensional
12-
res <- as.integer(rowSums(x>0))
12+
res <- as.integer(rowSums(x>0, na.rm = TRUE))
1313
}
1414
} else {
1515
if (identical(dim(x), NULL)) { # vector
1616
res <- as.integer(!is.na(x))
1717
} else { # 2-dimensional
18-
res <- as.integer(rowSums(!is.na(x)))
18+
res <- as.integer(rowSums(!is.na(x), na.rm = TRUE))
1919
}
2020
}
2121
return(data.frame(n_present=res))

R/kendrickPlot.R

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,6 @@ kendrickPlot <- function(ftmsObj, title=NA, colorPal=NA, colorCName=NA, vkBounda
105105
colorCName <- "Class"
106106
}
107107

108-
xrange <- nice_axis_limits(ftmsObj$e_meta[, km_col])
109-
yrange <- nice_axis_limits(ftmsObj$e_meta[, kd_col])
110-
111108
# if ftmsObj is a comparison summary object, remove all NA rows from e_data/e_meta
112109
if (inherits(ftmsObj, "comparisonSummary")) {
113110
ind <- !is.na(dplyr::pull(ftmsObj$e_data, colorCName))
@@ -122,7 +119,7 @@ kendrickPlot <- function(ftmsObj, title=NA, colorPal=NA, colorCName=NA, vkBounda
122119
ftmsObj$e_meta$Hover <- hovertext
123120

124121
p <- scatterPlot(ftmsObj, km_col, kd_col, colorCName = colorCName, colorPal=colorPal, xlabel=xlabel, ylabel=ylabel,
125-
legendTitle=legendTitle, title=title, xrange=xrange, yrange=yrange, logColorCol=logColorCol, hoverTextCName="Hover")
122+
legendTitle=legendTitle, title=title, logColorCol=logColorCol, hoverTextCName="Hover")
126123

127124
p
128125

R/scatterPlot.R

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#' @param yrange y-axis bounds
1414
#' @param logColorCol TRUE/FALSE, should the color column be log-transformed? Default is FALSE.
1515
#' @param hoverTextCName column name for hover (mouseover) text, must be a column of \code{ftmsObj$e_data} or \code{ftmsObj$e_meta}
16+
#' @param zero.min TRUE/FALSE, if an axis range is not provided, should the automatic range minimum be set to 0?
1617
#'
1718
#' @return plotly object
1819
#' @export
@@ -22,7 +23,7 @@
2223
#' scatterPlot(exampleProcessedPeakData, "NOSC", "DBE", colorCName="HtoC_ratio", legendTitle="H:C Ratio", title="DBE vs NOSC for exampleProcessedPeakData")
2324
#' }
2425
scatterPlot <- function(ftmsObj, xCName, yCName, colorCName=NA, colorPal=NA, xlabel=xCName, ylabel=yCName,
25-
legendTitle=colorCName, title=NA, xrange=NA, yrange=NA, logColorCol=FALSE, hoverTextCName=NA) {
26+
legendTitle=colorCName, title=NA, xrange=NA, yrange=NA, logColorCol=FALSE, hoverTextCName=NA, zero.min=FALSE) {
2627

2728
if (missing(ftmsObj)) stop("ftmsObj must be provided")
2829
if (missing(xCName)) stop("xCName must be provided")
@@ -156,10 +157,10 @@ scatterPlot <- function(ftmsObj, xCName, yCName, colorCName=NA, colorPal=NA, xla
156157
plot_data <- plot_data[!ind.na, ]
157158

158159
if (identical(xrange, NA)) {
159-
xrange <- ftmsRanalysis:::nice_axis_limits(plot_data[, xCName])
160+
xrange <- ftmsRanalysis:::nice_axis_limits(plot_data[, xCName], zero.min = zero.min)
160161
}
161162
if (identical(yrange, NA)) {
162-
yrange <- ftmsRanalysis:::nice_axis_limits(plot_data[, yCName])
163+
yrange <- ftmsRanalysis:::nice_axis_limits(plot_data[, yCName], zero.min = zero.min)
163164
}
164165

165166
p <- plotly::plot_ly(plot_data, x=plot_data[,xCName], y=plot_data[,yCName])

R/vanKrevelenPlot.R

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,6 @@ vanKrevelenPlot <- function(ftmsObj, title=NA, colorPal=NA, colorCName=NA, vkBou
9090

9191
}
9292

93-
xrange <- nice_axis_limits(ftmsObj$e_meta[, OC.col], zero.min=TRUE)
94-
yrange <- nice_axis_limits(ftmsObj$e_meta[, HC.col], zero.min=TRUE)
95-
9693
# if ftmsObj is a comparison summary object, remove all NA rows from e_data/e_meta
9794
if (inherits(ftmsObj, "comparisonSummary")) {
9895
ind <- !is.na(dplyr::pull(ftmsObj$e_data, colorCName))
@@ -107,7 +104,7 @@ vanKrevelenPlot <- function(ftmsObj, title=NA, colorPal=NA, colorCName=NA, vkBou
107104
ftmsObj$e_meta$Hover <- hovertext
108105

109106
p <- scatterPlot(ftmsObj, OC.col, HC.col, colorCName = colorCName, colorPal=colorPal, xlabel=xlabel, ylabel=ylabel,
110-
legendTitle=legendTitle, title=title, xrange=xrange, yrange=yrange, logColorCol=logColorCol, hoverTextCName="Hover")
107+
legendTitle=legendTitle, title=title, logColorCol=logColorCol, hoverTextCName="Hover", zero.min=TRUE)
111108

112109
if (showVKBounds) {
113110
if (vk_color_different_than_pts) {

man/calc_dbe.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)