From 64774c079e014519ad6f9f9b6c24c42e9b44316e Mon Sep 17 00:00:00 2001 From: Taha Ahmed Date: Thu, 24 Feb 2011 16:24:34 +0100 Subject: [PATCH] Added the function Raman2df(). Note: There should be a possibility to supply (as an argument to Raman2df()) a custom sampleid which should then override the regexp- based algorithm (only if that argument was supplied). --- Renishaw.R | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 Renishaw.R diff --git a/Renishaw.R b/Renishaw.R new file mode 100644 index 0000000..5f9526b --- /dev/null +++ b/Renishaw.R @@ -0,0 +1,48 @@ +# Renishaw.R +# Functions to read data from the Renishaw Raman spectrometer +# Taha Ahmed, Feb 2011 + +# CONTENTS +# >>>> Raman2df + + + + + +################################################## +################### Raman2df ####################### +################################################## +Raman2df <- function(datafilename) { + # Function description: for reading Raman spectrum into dataframe + # + datafile <- file(datafilename, "r") + chifile <- readLines(datafile, n = -1) #read all lines of input file + close(datafile) + # + ##### + # A nice algorithm that extracts the filename from the datafilename argument + # and uses that as a sampleid in the returned dataframe + ##### + rgxp.sampleid <- "[^/]*(?=\\.\\w*)" ## THIS REQUIRES perl=TRUE + # Regular expression that extracts the filename out of a full path. + # Matches and extracts everything from the last forward slash (assuming Unix slashes) + # up until a dot folllowed by an arbitrary number of alphanumeric characters. + sampleidmtch <- regexpr(rgxp.sampleid, datafilename, perl=TRUE) + # Check that there was a match + if (sampleidmtch < 0) { + # -1 means no match + sampleid <- datafilename + # If match was unsuccessful we use the argument as passed to this function as sampleid + } + sampleid <- substr(datafilename, sampleidmtch, (sampleidmtch + attr(sampleidmtch, "match.length") - 1)) + # + ff <- data.frame(NULL) + zz <- textConnection(chifile, "r") + ff <- rbind(ff, data.frame(sampleid, + matrix(scan(zz, what = numeric(), sep = "\t"), + ncol = 2, byrow = T))) + close(zz) + names(ff) <- c("sampleid", "shift", "counts") + # + return(ff) +}