From 7d6044143bd0d3a31bbbaf8f904b18bdd74da792 Mon Sep 17 00:00:00 2001 From: Taha Ahmed Date: Thu, 16 Feb 2012 22:38:42 +0100 Subject: [PATCH] Implemented a switch (arg) that allows new behaviour (optional) ... without breaking code that relies on old behaviour (default). --- ProvideSampleId.R | 25 +++++++++++++++---------- ProvideSampleId.tex | 12 +++++++----- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/ProvideSampleId.R b/ProvideSampleId.R index b3a9e99..1469185 100644 --- a/ProvideSampleId.R +++ b/ProvideSampleId.R @@ -1,16 +1,21 @@ -ProvideSampleId <- function (pathexpfile) { +ProvideSampleId <- function (pathexpfile, implementation = "filename") { # Returns a "unique" sample ID when supplied # with a path to an experimental file. - ## Note: the sample ID must derive directly from the file or path. + # The second arg is optional, defaults to "old" behaviour, + # but can be set to "dirname" for another behaviour + # The second arg was added so as not to break older code. + ## Note to myself: the sample ID must derive directly from the file or path. - # basename(dirname()) returns the name of the lowest sub-directory - # split()[[1]][2] splits the dirname at the hyphen and returns the sampleid - sampleid <- strsplit(x = basename(dirname(pathexpfile)), - split = "-")[[1]][2] - -# # basename() returns the filename sans path -# # sub() returns the filename sans extension -# sampleid <- sub("\\.[\\w]+$", "", basename(pathexpfile), perl = TRUE) + if (implementation == "dirname") { + # basename(dirname()) returns the name of the lowest sub-directory + # split()[[1]][2] splits the dirname at the hyphen and returns the sampleid + sampleid <- strsplit(x = basename(dirname(pathexpfile)), + split = "-")[[1]][2] + } else { + # basename() returns the filename sans path + # sub() returns the filename sans extension + sampleid <- sub("\\.[\\w]+$", "", basename(pathexpfile), perl = TRUE) + } #### The code below is the old ProvideSampleId() function # ### OBS! Only very rudimentary error-checking. diff --git a/ProvideSampleId.tex b/ProvideSampleId.tex index c7d96c3..6716d20 100644 --- a/ProvideSampleId.tex +++ b/ProvideSampleId.tex @@ -1,6 +1,8 @@ -The following function, \Rfun{ProvideSampleId()}, strives to supply a unique sample ID from any path supplied to it. Of course, a certain structure on the part of the path and filename are assumed, namely: -\begin{itemize} -\item That the filename (not including the extension) consists of three alphanumeric strings separated by hyphens. -\item That\ldots -\end{itemize} +The aim with \Rfun{ProvideSampleId()} is to supply a unique sample ID from any full path supplied to it. + +The function solves this using two approaches: +\begin{enumerate} +\item using only the filename, +\item using only the name of the subdirectory in question. +\end{enumerate}