R/MetaGenomic_class.R

Defines functions `[.MetaGenomic`

#--------------------------------------#
#'@export
MetaGenomic <- R6::R6Class(
	classname="MetaGenomic" ,
	public=list(
		data=NA,
		meta=NA,
		taxa=NA
	)
)


#--------------------------------------#
#'@noRd
MetaGenomic$set(which="public",
		name="initialize",
		value=function(data=NA,meta=NA,taxa=NA){
			self$data=data
			self$meta=meta
			self$taxa=taxa
			self$validate()
})

#--------------------------------------#
#'@noRd
MetaGenomic$set(which="public",
		name="validate",
		value=function(){
			assert(nrow(self$data)==nrow(self$meta), "samples number on data and meta are different")
			assert(ncol(self$data)==nrow(self$taxa), "taxa number on data and taxa are different")
			assert(!is.null(rownames(self$data)) || !is.null(colnames(self$data)), "data must have both row/sample and column/taxa names ")
			assert(!is.null(rownames(self$meta)) || !is.null(colnames(self$meta)), "meta must have both row/sample and column/info names ")
			assert(!is.null(rownames(self$taxa)) || !is.null(colnames(self$taxa)), "data must have both row/taxa and column/ranks names ")
			assert(all(rownames(self$data)==rownames(self$meta)), "at least one of the samples names does not coincide between data and meta")
			assert(all(colnames(self$data)==rownames(self$taxa)), "at least one of the taxa names does not coincide between data and taxa")
		})

#--------------------------------------#
#'@noRd
MetaGenomic$set(which="public",
		name="print",
		value=function(...){
			cat("Metagenomic Dataset consisting of:\n")
			cat("  -data: the counts data.frame with", self$nsample, "samples and",self$ntaxa,self$data_rank,"\n")
			cat("  -meta: the data.frame with the following information on the samples \n")
			cat("        [",self$meta_info,"]\n")
			cat("  -taxa: the data.frame with the taxanomic tree information on the",self$data_rank,"\n")
			cat("        [",self$ranks,"]\n")
			invisible(self)
			})

#--------------------------------------#
#' @export
`[.MetaGenomic` = function(x,i,j,...){
	return(MetaGenomic$new(
		data=x$data[i,j,drop=FALSE],
		meta=x$meta[i, ,drop=FALSE],
		taxa=x$taxa[j, ,drop=FALSE]
	))}
Fuschi/JAX documentation built on Dec. 17, 2021, 9:22 p.m.