diff strhlp.R @ 0:e66bb061af06 draft

planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit 3529b25417f8e1a5836474c9adec4b696d35099d-dirty
author prog
date Tue, 12 Jul 2016 12:02:37 -0400
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/strhlp.R	Tue Jul 12 12:02:37 2016 -0400
@@ -0,0 +1,72 @@
+if ( ! exists('trim')) { # Do not load again if already loaded
+
+	#######################
+	# WHITESPACE TRIMMING #
+	#######################
+	
+	# Trim leading whitespaces
+	trim.leading <- function (x)  sub("^\\s+", "", x)
+	
+	# Trim trailing whitespaces
+	trim.trailing <- function (x) sub("\\s+$", "", x)
+	
+	# Trim leading and trailing whitespaces
+	trim <- function (x) gsub("^\\s+|\\s+$", "", x)
+	
+	#############
+	# SPLITTING #
+	#############
+	
+	# s         The string to split.
+	# sep       The separator on which to split.
+	# trim      Trim whitespaces for the resulting elements.
+	# unlist    Unlist the result, So that for a single string (i.e.: s has length 1), it returns a vector of strings instead of a list of vectors of strings.
+	# RETURN    A list of strings.
+	split.str <- function(s, sep = ',', trim = TRUE, unlist = FALSE) {
+		v <- strsplit(s, sep)
+		if (trim) v <- lapply(v, trim)
+		if (unlist) v <- unlist(v)
+		return(v)
+	}
+
+	########################
+	# SPLIT KEY/VALUE LIST #
+	########################
+
+	split.kv.list <- function(s, sep = ',', kvsep = '=') {
+
+		# Split
+		kvs <- strsplit(strsplit(s, sep)[[1]], kvsep)
+
+		# Get keys
+		k <- vapply(kvs, function(x) x[[1]], FUN.VALUE = '')
+		v <- vapply(kvs, function(x) x[[2]], FUN.VALUE = '')
+
+		# Set names
+		names(v) <- k
+
+		return(v)
+	}
+
+	#########################
+	# CONCAT KEY/VALUE LIST #
+	#########################
+
+	concat.kv.list <- function(x, sep = ',', kvsep = '=') {
+
+		k <- names(x)
+
+		s = paste(paste(names(x), x, sep = kvsep), collapse = sep)
+
+		return(s)
+	}
+
+	#################
+	# REMOVE QUOTES #
+	#################
+
+	remove.quotes <- function(s) {
+		return(sub('^["\']?([^\'"]*)["\']?$', '\\1', s, perl = TRUE))
+	}
+
+} # end of load safe guard