diff --git a/NAMESPACE b/NAMESPACE index 6ba36cb..610a4d0 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,10 +1,11 @@ -# Generated by roxygen2: do not edit by hand -import(raster) -import(wavelets) -import(ggplot2) -import(sp) -import(flux) -export(DataInput.txt) -export(PlotAUCImage) -export(SmoothSpectra) - +# Generated by roxygen2: do not edit by hand +import(raster) +import(wavelets) +import(ggplot2) +import(sp) +import(flux) +export(DataInput.txt) +export(PlotAUCImage) +export(PlotScanImage) +export(SmoothSpectra) + diff --git a/R/DataInput.R b/R/DataInput.R index 8524f94..14e1c2a 100644 --- a/R/DataInput.R +++ b/R/DataInput.R @@ -1,131 +1,139 @@ -######################################################################### -# rCRMI - R package for Confocal Raman Spectroscopy Imaging data processing and visualization -# Copyright (C) 2018 Lluc Sementé Fernàndez -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . -############################################################################ - -#' DataInput.txt -#' -#' @param path full path to .txt file. -#' @param numBands Number of .txt files to process. -#' -#' @description Reads a .txt file containing Raman data from Reinshaw devices and convert it to an RamanR data object. -#' -#' @return a rCRSIObj data object. -#' @export -#' - -DataInput.txt <- function(path = NULL, numBands = 1) -{ - writeLines("Select files...") - - #Data input read - PathToFile <- list() - for(i in 1:(numBands)) - { - if(is.null(path)) - { - PathToFile[[i]] <- file.choose() - }else - { - PathToFile[[i]] <- path[i] - } - } - - - writeLines("Starting the data structuring process...") - - rawData <- list() - for(i in 1:(numBands)) - { - tmp <- utils::read.table(file = PathToFile[[i]],header = FALSE) - if(i==1) - { - rawData <- tmp - } else - { - rawData <- cbind(rawData,tmp[,3:4]) - } - } - - #Raman Shift Axis, number of spectra & Raman band length - RamanShift <- array(dim = c(length(union(rawData[[3]],rawData[[3]])),numBands)) - for(i in 1:numBands) - { - p1 <- 3+(i-1)*2 - RamanShift[,i] <- union(rawData[[p1]],rawData[[p1]]) - } - BandLength <- (length(RamanShift)/numBands) - - #Coordenates - X <- union(rawData[[1]],rawData[[1]]) - Y <- union(rawData[[2]],rawData[[2]]) - numPixels <- length(X)*length(Y) - Coords <- list(X = X, Y = Y) - - #Data Shaping - RamanData <- array(dim=c(numBands*numPixels,BandLength)) - for(i in 1:numBands) - { - p <- 2*i+2 - for(j in 1:numPixels) - { - RamanData[j+numPixels*(i-1),] <- rawData[[p]][((j-1)*BandLength+1):(j*BandLength)] - } - } - - writeLines("Data structuring complete...") - return(rCRMIObj(RamanData, numPixels, numBands, RamanShift, BandLength, Coords)) -} - - -rCRMIObj <- function(RamanData, numPixels, numBands = 1, RamanShift, BandsLenght, Coords) -{ - rCRSIObj <- list() - - rCRSIObj$numPixels <- numPixels - rCRSIObj$numBands <- numBands - rCRSIObj$numSpectr <- numPixels*numBands - rCRSIObj$BandsLength <- BandsLenght - rCRSIObj$AbsCoords <- Coords - rCRSIObj$RelCoords <- list(X = 1:length(Coords$X), Y = 1:length(Coords$Y)) - rCRSIObj$PixelSize <- c(Coords$X[2]-Coords$X[1], Coords$Y[2]-Coords$Y[1]) - rCRSIObj$Data <- RamanData - - # ID <- gsub(x = as.character(Sys.time()),pattern = ("-"),replacement = "") - # ID <- gsub(x = ID,pattern = (":"),replacement = "") - # ID <- gsub(x = ID,pattern = (" "),replacement = "") - # rCRSIObj$ID <- ID - # - # if (numBands == 1 & BandsLenght > 391) - # { - # rCRSIObj$FullSpect <- TRUE - # } - # else rCRSIObj$FullSpect <- FALSE - - rCRSIObj$RamanShiftAxis <- RamanShift - rCRSIObj$AvrgSpectr <- RamanShift - - for (i in 1:numBands) - { - for (j in 1:numPixels) - { - rCRSIObj$AvrgSpectr[,i] <- 0 - } - } - rCRSIObj$AvrgSpectr <- AverageSpectrum(numBands, numPixels, rCRSIObj) - return (rCRSIObj) -} - +######################################################################### +# rCRMI - R package for Confocal Raman Spectroscopy Imaging data processing and visualization +# Copyright (C) 2018 Lluc Sementé Fernàndez +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +############################################################################ + +#' DataInput.txt +#' +#' @param path full path to .txt file. +#' @param numBands Number of .txt files to process. +#' +#' @description Reads a .txt file containing Raman data from Reinshaw devices and convert it to an RamanR data object. +#' +#' @return a rCRSIObj data object. +#' @export +#' + +DataInput.txt <- function(path = NULL, numBands = 1, IntensityThreshold = T) +{ + writeLines("Select files...") + + #Data input read + PathToFile <- list() + for(i in 1:(numBands)) + { + if(is.null(path)) + { + PathToFile[[i]] <- file.choose() + }else + { + PathToFile[[i]] <- path[i] + } + } + + + writeLines("Starting the data structuring process...") + + rawData <- list() + for(i in 1:(numBands)) + { + tmp <- utils::read.table(file = PathToFile[[i]],header = FALSE) + if(i==1) + { + rawData <- tmp + } else + { + rawData <- cbind(rawData,tmp[,3:4]) + } + } + + #Raman Shift Axis, number of spectra & Raman band length + RamanShift <- array(dim = c(length(union(rawData[[3]],rawData[[3]])),numBands)) + for(i in 1:numBands) + { + p1 <- 3+(i-1)*2 + RamanShift[,i] <- union(rawData[[p1]],rawData[[p1]]) + } + BandLength <- (length(RamanShift)/numBands) + + #Coordenates + X <- union(rawData[[1]],rawData[[1]]) + Y <- union(rawData[[2]],rawData[[2]]) + numPixels <- length(X)*length(Y) + Coords <- list(X = X, Y = Y) + + #Data Shaping + RamanData <- array(dim=c(numBands*numPixels,BandLength)) + for(i in 1:numBands) + { + p <- 2*i+2 + for(j in 1:numPixels) + { + RamanData[j+numPixels*(i-1),] <- rawData[[p]][((j-1)*BandLength+1):(j*BandLength)] + } + } + + return(rCRMIObj(RamanData, numPixels, numBands, RamanShift, BandLength, Coords, IntensityThreshold)) +} + + +rCRMIObj <- function(RamanData, numPixels, numBands = 1, RamanShift, BandsLenght, Coords, IntensityThreshold) +{ + rCRSIObj <- list() + + rCRSIObj$numPixels <- numPixels + rCRSIObj$numBands <- numBands + rCRSIObj$numSpectr <- numPixels*numBands + rCRSIObj$BandsLength <- BandsLenght + rCRSIObj$AbsCoords <- Coords + rCRSIObj$RelCoords <- list(X = 1:length(Coords$X), Y = 1:length(Coords$Y)) + rCRSIObj$PixelSize <- c(Coords$X[2]-Coords$X[1], Coords$Y[2]-Coords$Y[1]) + rCRSIObj$Data <- RamanData + rCRSIObj$RamanShiftAxis <- RamanShift + rCRSIObj$AvrgSpectr <- RamanShift + + for (i in 1:numBands) + { + for (j in 1:numPixels) + { + rCRSIObj$AvrgSpectr[,i] <- 0 + } + } + + + rCRSIObj$AvrgSpectr <- AverageSpectrum(numBands, numPixels, rCRSIObj) + + if(IntensityThreshold) + { + dens <- density(rCRSIObj$Data) + aucvec <- vector(length = (length(dens$x)-1)) + for(i in 2:length(dens$x)) + { + aucvec[i-1] <- flux::auc(x = dens$x[1:i], y = dens$y[1:i]) + } + + intThr <- dens$x[which(aucvec>0.99*max(aucvec))[1]] + + for(i in 1:rCRSIObj$BandsLength) + { + rCRSIObj$Data[which(rCRSIObj$Data[,i]>intThr),i] <- median(rCRSIObj$Data[,i]) + } + } + + writeLines("Data structuring complete...") + return (rCRSIObj) +} + diff --git a/R/PlotAUCimg.R b/R/PlotAUCimg.R index 0837cb6..aefd9e2 100644 --- a/R/PlotAUCimg.R +++ b/R/PlotAUCimg.R @@ -28,7 +28,7 @@ #' @export #' -PlotAUCImage <- function(rCRSIObj, intervals, numImg = 1) +PlotAUCImage <- function(rCRSIObj, intervals, numImg = 1, interpolate = T) { for(i in 1:numImg) { @@ -48,18 +48,20 @@ PlotAUCImage <- function(rCRSIObj, intervals, numImg = 1) from <- which.min(abs(rCRSIObj$RamanShiftAxis-intervals[2*i-1])) to <- which.min(abs(rCRSIObj$RamanShiftAxis-intervals[2*i])) - AUCimg <- matrix(nrow = length(rCRSIObj$RelCoords$Y), ncol = length(rCRSIObj$RelCoords$X)) + AUCvec <- rep(0, times = rCRSIObj$numPixels) - for(X in 1:ncol(AUCimg)) + for(i in 1:rCRSIObj$numPixels) { - for(Y in 1:nrow(AUCimg)) - { - AUCimg[Y,X] <- flux::auc(x = rCRSIObj$RamanShiftAxis[from:to], - y = rCRSIObj$Data[(Y+(X-1)*length(rCRSIObj$RelCoords$Y)), from:to] - ) - } + AUCvec[i] <- flux::auc(x = rCRSIObj$RamanShiftAxis[from:to], + y = rCRSIObj$Data[i,from:to] + ) } + AUCimg <- matrix(data = AUCvec, + nrow = length(rCRSIObj$RelCoords$X), + ncol = length(rCRSIObj$RelCoords$Y)) + + #Create the raster img <- raster::raster(nrows = nrow(AUCimg), ncols = ncol(AUCimg), @@ -69,12 +71,12 @@ PlotAUCImage <- function(rCRSIObj, intervals, numImg = 1) ymx = nrow(AUCimg) ) - raster::values(img) <- AUCimg + img <- raster::setValues(img,AUCimg) raster::plot(x = img, col = sp::bpy.colors(64), useRaster = T, - interpolate = T, + interpolate = interpolate ) graphics::title(main = paste("AUC: ",signif(rCRSIObj$RamanShiftAxis[from],5),"~",signif(rCRSIObj$RamanShiftAxis[to],5)), diff --git a/R/PlotScanimg.R b/R/PlotScanimg.R new file mode 100644 index 0000000..64f7afa --- /dev/null +++ b/R/PlotScanimg.R @@ -0,0 +1,67 @@ +######################################################################### +# rCRSI - R package for Confocal Raman Spectroscopy Imaging data processing and visualization +# Copyright (C) 2018 Lluc Sementé Fernàndez +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +############################################################################ + +#' PlotScanImage +#' +#' @param rCRSIObj A rCRSI object. +#' @param wavelength Vector containing pairs of indexes that define the area under one or more peaks. (Length must be numImg*2) +#' @param numImg Number of images to be plotted. +#' +#' +#' @description Generates and plots the image of the "Are under the curve"(AUC) within the given intervals. +#' +#' @export +#' + +PlotScanImage <- function(rCRSIObj, wavelength, numImg = 1, interpolate = T) +{ + + if(length(wavelength) != numImg) + { + stop("Error: Not enough wavelength for the number of images desired") + } + + for (i in 1:numImg) + { + AUCimg <- matrix(data = rCRSIObj$Data[,which.min(abs(rCRSIObj$RamanShiftAxis-wavelength))], + nrow = length(rCRSIObj$RelCoords$X), + ncol = length(rCRSIObj$RelCoords$Y)) + + #Create the raster + img <- raster::raster(nrows = nrow(AUCimg), + ncols = ncol(AUCimg), + xmn = 0, + xmx = ncol(AUCimg), + ymn = 0, + ymx = nrow(AUCimg) + ) + + img <- raster::setValues(img,AUCimg) + + raster::plot(x = img, + col = sp::bpy.colors(64), + useRaster = T, + interpolate = interpolate,colNA = "black" + ) + + graphics::title(main = paste("Wavelength: ",signif(rCRSIObj$RamanShiftAxis[which.min(abs(rCRSIObj$RamanShiftAxis-wavelength))],7)), + sub = paste("Pixel size: ", signif(rCRSIObj$PixelSize[1],3)," x ",signif(rCRSIObj$PixelSize[2],3)), + line = 2 + ) + } +}