Skip to content

Commit ddccaa8

Browse files
committed
fixed issue with
1 parent 83948b9 commit ddccaa8

28 files changed

Lines changed: 61 additions & 62 deletions

R/bray.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#' where \eqn{A_i} and \eqn{B_i} are the abundances of the \eqn{i}-th feature in sample \eqn{A} and \eqn{B}, respectively.
1212
#' When weighted is set to FALSE, counts are replaced by presence/absence data.
1313
#'
14-
#' @param x A \link[base]{matrix}, \link[Matrix]{sparseMatrix} or \link[Matrix]{denseMatrix}.
14+
#' @param x A \link[base]{matrix}, \link[Matrix]{sparseMatrix} or \link[Matrix]{Matrix}.
1515
#' @param weighted A boolean value, to use abundances (\code{weighted = TRUE}) or absence/presence (\code{weighted=FALSE}) (default: TRUE).
1616
#' @param threads A wholenumber, the number of threads to use in \link[RcppParallel]{setThreadOptions} (default: 1).
1717
#' @return A column x column \link[stats]{dist} object.

R/canberra.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#' where \eqn{A_i} and \eqn{B_i} are the abundances of the \eqn{i}-th feature in sample \eqn{A} and \eqn{B}, respectively. NZ are the number of non-zero entries.
1212
#' When weighted is set to FALSE, counts are replaced by presence/absence data.
1313
#'
14-
#' @param x A \link[base]{matrix}, \link[Matrix]{sparseMatrix} or \link[Matrix]{denseMatrix}.
14+
#' @param x A \link[base]{matrix}, \link[Matrix]{sparseMatrix} or \link[Matrix]{Matrix}.
1515
#' @param weighted A boolean value, to use abundances (\code{weighted = TRUE}) or absence/presence (\code{weighted=FALSE}) (default: TRUE).
1616
#' @param threads A wholenumber, the number of threads to use in \link[RcppParallel]{setThreadOptions} (default: 1).
1717
#' @return A column x column \link[stats]{dist} object.

R/cosine.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#' where \eqn{A_i} and \eqn{B_i} are the abundances of the \eqn{i}-th feature in sample \eqn{A} and \eqn{B}, respectively.
1212
#' When weighted is set to FALSE, counts are replaced by presence/absence data.
1313
#'
14-
#' @param x A \link[base]{matrix}, \link[Matrix]{sparseMatrix} or \link[Matrix]{denseMatrix}.
14+
#' @param x A \link[base]{matrix}, \link[Matrix]{sparseMatrix} or \link[Matrix]{Matrix}.
1515
#' @param weighted A boolean value, to use abundances (\code{weighted = TRUE}) or absence/presence (\code{weighted=FALSE}) (default: TRUE).
1616
#' @param threads A wholenumber, the number of threads to use in \link[RcppParallel]{setThreadOptions} (default: 1).
1717
#' @return A column x column \link[stats]{dist} object.

R/diversity.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#' This function is built into the class \link{omics} with method \code{alpha_diversity()} and inherited by other omics classes, such as;
77
#' \link{metagenomics} and \link{proteomics}.
88
#'
9-
#' @param x A \link[base]{matrix}, \link[Matrix]{sparseMatrix} or \link[Matrix]{denseMatrix}.
9+
#' @param x A \link[base]{matrix}, \link[Matrix]{sparseMatrix} or \link[Matrix]{Matrix}.
1010
#' @param metric A character variable for metric; shannon, simpson or invsimpson.
1111
#' @param normalize A boolean variable for sample normalization by column sums.
1212
#' @param base Input for \link[base]{log} to use natural logarithmic scale, log2, log10 or other.

R/hill_taxa.R

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#' Code is adapted from \link[hillR]{hill_taxa} and uses \link[Matrix]{sparseMatrix} in triplet format over the dense matrix.
55
#' The code is much faster and memory efficient, while still being mathematical correct.
66
#'
7-
#' @param x A \link[base]{matrix} or \link[Matrix]{sparseMatrix}.
7+
#' @param x A \link[base]{matrix}, \link[Matrix]{sparseMatrix} or \link[Matrix]{Matrix}.
88
#' @param q A wholenumber for 0, 1 or 2, default is 0.
99
#' @param normalize A boolean variable for sample normalization by column sums.
1010
#' @param base Input for \link[base]{log} to use natural logarithmic scale, log2, log10 or other.
@@ -48,8 +48,9 @@ hill_taxa <- function (x,
4848

4949
## Error handling
5050
#--------------------------------------------------------------------#
51-
if (is.vector(x))
52-
cli::cli_abort("Input must be a matrix of class matrix or Matrix, not a vector.")
51+
if (inherits(x, "denseMatrix") || inherits(x, "matrix") || inherits(x, "sparseMatrix")) {
52+
x <- as(x, "CsparseMatrix")
53+
} else cli::cli_abort("Input isn't a base::matrix, dense- or sparseMatrix.")
5354

5455
if (!is.numeric(x@x))
5556
cli::cli_abort("input data must be numeric")
@@ -62,10 +63,6 @@ hill_taxa <- function (x,
6263

6364
## MAIN
6465
#--------------------------------------------------------------------#
65-
if (inherits(x, "denseMatrix") || inherits(x, "matrix") || inherits(x, "sparseMatrix")) {
66-
x <- as(x, "CsparseMatrix")
67-
} else cli::cli_abort("Input isn't a matrix, dense- or sparseMatrix.")
68-
6966
if (normalize) {
7067
total <- rep(Matrix::colSums(x), base::diff(x@p))
7168
x@x <- x@x / total

R/jaccard.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#' where \eqn{A_i} and \eqn{B_i} are the abundances of the \eqn{i}-th feature in sample \eqn{A} and \eqn{B}, respectively.
1212
#' When weighted is set to FALSE, abundances are changed to 1 (classical Jaccard for binary data).
1313
#'
14-
#' @param x A \link[base]{matrix}, \link[Matrix]{sparseMatrix} or \link[Matrix]{denseMatrix}.
14+
#' @param x A \link[base]{matrix}, \link[Matrix]{sparseMatrix} or \link[Matrix]{Matrix}.
1515
#' @param weighted A boolean value, to use abundances (\code{weighted = TRUE}) or absence/presence (\code{weighted=FALSE}) (default: TRUE).
1616
#' @param threads A wholenumber, the number of threads to use in \link[RcppParallel]{setThreadOptions} (default: 1).
1717
#' @return A column x column \link[stats]{dist} object.

R/jsd.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#' and \eqn{D_{KL}} is the Kullback-Leibler divergence.
1313
#' When weighted is set to FALSE, counts are replaced by presence/absence data.
1414
#'
15-
#' @param x A \link[base]{matrix}, \link[Matrix]{sparseMatrix} or \link[Matrix]{denseMatrix}.
15+
#' @param x A \link[base]{matrix}, \link[Matrix]{sparseMatrix} or \link[Matrix]{Matrix}.
1616
#' @param weighted A boolean value, to use abundances (\code{weighted = TRUE}) or absence/presence (\code{weighted=FALSE}) (default: TRUE).
1717
#' @param threads A wholenumber, the number of threads to use in \link[RcppParallel]{setThreadOptions} (default: 1).
1818
#' @return A column x column \link[stats]{dist} object.

R/manhattan.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#' where \eqn{A_i} and \eqn{B_i} are the abundances of the \eqn{i}-th feature in sample \eqn{A} and \eqn{B}, respectively.
1212
#' When weighted is set to FALSE, counts are replaced by presence/absence data.
1313
#'
14-
#' @param x A \link[base]{matrix}, \link[Matrix]{sparseMatrix} or \link[Matrix]{denseMatrix}.
14+
#' @param x A \link[base]{matrix}, \link[Matrix]{sparseMatrix} or \link[Matrix]{Matrix}.
1515
#' @param weighted A boolean value, to use abundances (\code{weighted = TRUE}) or absence/presence (\code{weighted=FALSE}) (default: TRUE).
1616
#' @param threads A wholenumber, the number of threads to use in \link[RcppParallel]{setThreadOptions} (default: 1).
1717
#' @return A column x column \link[stats]{dist} object.

R/metagenomics-class.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ metagenomics <- R6::R6Class(
1818
cloneable = TRUE,
1919
inherit = omics,
2020
active = list(
21-
#' @field treeData A "phylo" class, see \link[ape].
21+
#' @field treeData A "phylo" class, see \link[ape]{as.phylo}.
2222
treeData = function(value) {
2323
# Restores omics class components
2424
private$tmp_link(

R/omics-class.R

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ omics <- R6::R6Class(
7171
invisible(self)
7272
} else stop("Data input requires to be of the same class as `featureData`")
7373
},
74-
#' @field countData A dense or sparse \link{Matrix}.
74+
#' @field countData A dense or sparse \link[Matrix]{Matrix}.
7575
countData = function(value) {
7676
# Restores omics class components
7777
private$tmp_link(
@@ -103,7 +103,7 @@ omics <- R6::R6Class(
103103
#' @description
104104
#' Wrapper function that is inherited and adapted for each omics class.
105105
#' The omics classes requires a metadata samplesheet, that is validated by the metadata_schema.json.
106-
#' It requires a column `SAMPLE_ID` and optionally a `SAMPLEPAIR_ID` or `FEATURE_ID` can be supplied.
106+
#' It requires a column `SAMPLE_ID` and optionally a `SAMPLEPAIR_ID` can be supplied.
107107
#' The `SAMPLE_ID` will be used to link the metaData to the countData, and will act as the key during subsetting of other columns.
108108
#' To create a new object use [`new()`](#method-new) method. Do notice that the abstract class only checks if the metadata is valid!
109109
#' The `countData` and `featureData` will not be checked, these are handled by the sub-classes.
@@ -125,24 +125,14 @@ omics <- R6::R6Class(
125125
cli::cli_alert_success("Metadata template passed the JSON validation.")
126126

127127
#--------------------------------------------------------------------#
128-
## Checking for duplicated sample and feature identifiers
128+
## Checking for duplicated sample identifiers
129129
#--------------------------------------------------------------------#
130130

131131
cli::cli_alert_info("Checking for duplicated identifiers ..")
132132

133133
duplicated_sample_ids <- any(duplicated(private$.metaData, by = private$.sample_id))
134-
135-
if (column_exists(private$.feature_id, private$.metaData)) {
136-
duplicated_feature_ids <- any(duplicated(private$.metaData, by = private$.feature_id))
137-
} else {
138-
duplicated_feature_ids <- FALSE
139-
}
140-
141-
if (duplicated_sample_ids) {
134+
if (duplicated_sample_ids)
142135
cli::cli_abort("Found duplicated SAMPLE_ID, make sure SAMPLE_ID column contains unique identifiers!")
143-
} else if (duplicated_feature_ids) {
144-
cli::cli_abort("Found duplicated FEATURE_ID, make sure FEATURE_ID column contains unique identifiers!")
145-
}
146136

147137
#--------------------------------------------------------------------#
148138
## Disable samplepair_id if not supplied
@@ -167,15 +157,20 @@ omics <- R6::R6Class(
167157
### featureData ###
168158
#-------------------#
169159
if (!is.null(featureData)) {
160+
duplicated_feature_ids <- FALSE
170161
private$.featureData <- private$check_table(featureData)
171162

172-
if (column_exists(private$.feature_id, private$.metaData)) {
173-
FEATURE_ID <- private$.metaData[[ private$.feature_id ]]
163+
if (column_exists(private$.feature_id, private$.featureData)) {
164+
duplicated_feature_ids <- any(duplicated(private$.featureData, by = private$.feature_id))
165+
166+
if (duplicated_feature_ids)
167+
cli::cli_abort("Found duplicated FEATURE_ID, make sure FEATURE_ID column contains unique identifiers!")
168+
174169
} else {
170+
175171
FEATURE_ID <- paste0("feature_", 1:nrow(private$.featureData))
172+
private$.featureData[, private$.feature_id := FEATURE_ID]
176173
}
177-
178-
private$.featureData[, private$.feature_id := FEATURE_ID]
179174
cli::cli_alert_success("featureData is loaded.")
180175
}
181176

@@ -193,12 +188,13 @@ omics <- R6::R6Class(
193188
if (is.null(countData_with_rownames)) {
194189
FEATURE_ID <- paste0("feature_", 1:nrow(private$.countData))
195190
private$.featureData <- private$.featureData[, (private$.feature_id) := FEATURE_ID]
191+
rownames(private$.countData) <- FEATURE_ID
196192
} else {
197193
private$.featureData <- private$.featureData[, (private$.feature_id) := countData_with_rownames]
198-
}
199-
200-
rownames(private$.countData) <- private$.featureData[[ private$.feature_id ]]
194+
}
201195
cli::cli_alert_warning("Placeholder featureData created.")
196+
} else {
197+
rownames(private$.countData) <- private$.featureData[[ private$.feature_id ]]
202198
}
203199
}
204200

@@ -223,7 +219,6 @@ omics <- R6::R6Class(
223219
#' Acceptable column headers:
224220
#' * SAMPLE_ID (required)
225221
#' * SAMPLEPAIR_ID (optional)
226-
#' * FEATURE_ID (optional)
227222
#' * CONTRAST_ (optional), used for [`autoFlow()`](#method-autoFlow).
228223
#' * VARIABLE_ (optional), not supported yet.
229224
#'
@@ -864,7 +859,7 @@ omics <- R6::R6Class(
864859
if (!is.null(col_name))
865860
self$removeNAs(col_name)
866861

867-
# Convert sparse matrix to data.table
862+
# Converts matrix to data.table
868863
counts <- matrix_to_dtable(private$.countData)
869864

870865
# Fetch unfiltered and filtered features
@@ -1775,6 +1770,7 @@ omics <- R6::R6Class(
17751770

17761771
# Keep only common samples based on metaData
17771772
if (!is.null(private$.countData)) {
1773+
private$.countData <- private$check_matrix(private$.countData)
17781774
common_samples <- base::intersect(private$.metaData[[ private$.sample_id ]], colnames(private$.countData))
17791775
private$.countData <- private$.countData[, common_samples, drop = FALSE]
17801776
private$.metaData <- private$.metaData[private$.metaData[[ private$.sample_id ]] %in% common_samples, ]
@@ -1785,18 +1781,27 @@ omics <- R6::R6Class(
17851781
if (!column_exists(private$.feature_id, private$.featureData))
17861782
cli::cli_abort("{private$.feature_id} doesn't exist in featureData.")
17871783

1784+
private$.featureData <- private$check_table(private$.featureData)
17881785
colnames(private$.featureData) <- gsub("\\s+", "_", colnames(private$.featureData))
17891786

17901787
# Keep only common tips based on treeData
17911788
if (!is.null(private$.treeData)) {
17921789
common_tips <- base::intersect(private$.treeData$tip.label, private$.featureData[[ private$.feature_id ]])
1790+
1791+
if (length(common_tips) == 0)
1792+
cli::cli_abort("None FEATURE_IDs are matching, check if FEATURE_ID exists in `treeData` tip labels!")
1793+
17931794
private$.treeData <- ape::keep.tip(private$.treeData, common_tips)
17941795
private$.featureData <- private$.featureData[private$.featureData[[ private$.feature_id ]] %in% common_tips, ]
17951796
}
17961797

17971798
# Keep only common features based on countData
17981799
if (!is.null(private$.countData)) {
17991800
common_features <- base::intersect(private$.featureData[[ private$.feature_id ]], rownames(private$.countData))
1801+
1802+
if (length(common_features) == 0)
1803+
cli::cli_abort("None FEATURE_IDs are matching, check if FEATURE_ID exists in `countData` rownames!")
1804+
18001805
private$.featureData <- private$.featureData[private$.featureData[[ private$.feature_id ]] %in% common_features, ]
18011806
private$.countData <- private$.countData[common_features, ]
18021807
private$removeZeros()
@@ -1863,6 +1868,8 @@ omics <- R6::R6Class(
18631868
if (!is.null(dt$V1)) {
18641869
dt_rownames <- dt$V1
18651870
dt[, V1 := NULL]
1871+
} else {
1872+
dt_rownames <- NULL
18661873
}
18671874
# Convert to matrix format
18681875
mat <- Matrix::Matrix(

0 commit comments

Comments
 (0)