#' @include 02_class_metabarcoding.data.R NULL #' Read frequencies krigging #' #' Extrapolates read frequencies from a \code{\link{metabarcoding.data}} object in space for a finer resolution #' #' @param x a vector or matrix from a row-normalized read table #' \code{\link{metabarcoding.data}} object #' @param min.coord a vector of length = 2 indicating the minimum values of x and y #' coordinates to be used for the predicted grid #' @param max.coord a vector of length = 2 indicating the maximum values of x and y #' coordinates to be used for the predicted grid #' @param grid.grain an integer indicating the resolution (i.e. nb of subpoints) in x and y #' coordinates required for the predicted grid #' @param coords a dataframe containing the x and y coordinates of the abundances #' from x to be extrapolated. #' @param otus.table a motus data.frame containing motus informations of x #' @param cutoff a cutoff below which abundances are set to 0. #' This threshold also determines the value to be added to 0 values for log10 #' transformation #' @param return.metabarcoding.data if \code{TRUE}, returns a \code{\link{metabarcoding.data}} object. Default is \code{FALSE} #' #' @return either a dataframe or a S3 object with a structure similar to \code{\link{metabarcoding.data}} object. #' The number of samples corresponds to the predicted points. #' The two last columns (if \code{return.metabarcoding.data==F}) or sample data.frame contains x y coordinates of the predicted grid #' The all but last two columns (if \code{return.metabarcoding.data==F}) or read matrix contains the predicted log10 transformed relative abundances #' instead of reads counts #' If \code{return.metabarcoding.data==F} the motus data.frame contains the motus informations from x #' #' @examples #' #' data(termes) #' #Create dummy spatial coordinates #' attr(termes, "samples")[c("x", "y")] = expand.grid(1:7,1:3) #' #' #compute frequencies #' attr(termes, "layers")[["reads.freq"]] = normalize(termes, MARGIN=1)$reads #' #' # Getting extrapolations #' termes.pred = extrapol.freq(attr(termes, "layers")[["reads.freq"]], min.coord=c(1,1), max.coord=c(7,3), #' grid.grain=100,termes$samples[,c("x", "y")], termes$motus, cutoff=1e-3) #' #' head(termes.pred$reads) #' @seealso \code{\link{map.extrapol.freq}} as well as \code{sp} and \code{gstat} packages #' @author Lucie Zinger #' @export extrapol.freq = function(x, min.coord, max.coord, grid.grain=100, coords, otus.table, cutoff=1e-3, return.metabarcoding.data = FALSE) { require(gstat) require(sp) #predicted grid setting new.x = seq(min.coord[1], max.coord[1], length.out = grid.grain) new.y = seq(min.coord[2], max.coord[2], length.out = grid.grain) grid.p=expand.grid(new.x, new.y) colnames(grid.p)=c("x", "y") S=sp::SpatialPoints(grid.p); sp::gridded(S)<-TRUE m=gstat::vgm(50, "Exp", 100) #krigging preds = apply(x, 2, function(otu) { otu[otuabs(log10(cutoff))]=log10(cutoff) z[z>0] = 0 spj=as.data.frame(cbind(x$samples,z)) colnames(spj)=c("x", "y", "z") map.out=levelplot(z~x+y, spj, col.regions=topo.colors(100), at=seq(log10(cutoff),log10(1), by=0.2), colorkey=list(at=seq(log10(cutoff),log10(1), by=0.2), labels=list(at=seq(log10(cutoff),log10(1), by=0.2), labels=round(10^seq(log10(cutoff),log10(1), by=0.2),3))), aspect = "iso", contour=F, main=list(label=x$motus[index, "id"], cex=0.7)) if(!is.null(path)) { png(file=file.out, width=800, height=800) print(map.out) if(!is.null(add.points)) { n = (max(spj[,"y"])-min(spj["y"]))/length(unique(spj[,"y"]))*adj trellis.focus("panel", 1, 1, highlight=FALSE) lpoints(add.points[,"x"], add.points[,"y"], cex=0.7, lwd=3, col="red") ltext(add.points[,"x"], add.points[,"y"]+n, add.points[,-match(c("x", "y"), colnames(add.points))], col="red", cex=1.5) trellis.unfocus() } dev.off() } else { print(map.out) if(!is.null(add.points)) { n = (max(spj[,"y"])-min(spj["y"]))/length(unique(spj[,"y"]))*adj trellis.focus("panel", 1, 1, highlight=FALSE) lpoints(add.points[,"x"], add.points[,"y"], cex=0.7, lwd=3, col="red") ltext(add.points[,"x"], add.points[,"y"]+n, add.points[,-match(c("x", "y"), colnames(add.points))], col="red", cex=1) trellis.unfocus() } } }