diff enrichment_v3.R @ 8:ddaa0c318d65 draft

planemo upload commit d23909db127a655212b79d6826033889812f59b5-dirty
author proteore
date Fri, 23 Mar 2018 10:43:32 -0400
parents 829cbdb71efa
children 70c0c8757f5f
line wrap: on
line diff
--- a/enrichment_v3.R	Thu Mar 08 11:25:05 2018 -0500
+++ b/enrichment_v3.R	Fri Mar 23 10:43:32 2018 -0400
@@ -32,7 +32,27 @@
 
 
 # loading topGO library
-library(topGO) 
+library(topGO)
+
+# Read file and return file content as data.frame
+readfile = function(filename, header) {
+  if (header == "true") {
+    # Read only first line of the file as header:
+    headers <- read.table(filename, nrows = 1, header = FALSE, sep = "\t", stringsAsFactors = FALSE, fill = TRUE, na.strings=c("", "NA"), blank.lines.skip = TRUE, quote = "")
+    #Read the data of the files (skipping the first row)
+    file <- read.table(filename, skip = 1, header = FALSE, sep = "\t", stringsAsFactors = FALSE, fill = TRUE, na.strings=c("", "NA"), blank.lines.skip = TRUE, quote = "")
+    # Remove empty rows
+    file <- file[!apply(is.na(file) | file == "", 1, all), , drop=FALSE]
+    #And assign the header to the data
+    names(file) <- headers
+  }
+  else {
+    file <- read.table(filename, header = FALSE, sep = "\t", stringsAsFactors = FALSE, fill = TRUE, na.strings=c("", "NA"), blank.lines.skip = TRUE, quote = "")
+    # Remove empty rows
+    file <- file[!apply(is.na(file) | file == "", 1, all), , drop=FALSE]
+  }
+  return(file)
+} 
 
 '%!in%' <- function(x,y)!('%in%'(x,y))
 
@@ -50,11 +70,11 @@
 # for each input, split the arguments with blank space as separator, unlist,
 # and delete the first element which is the input name (e.g --inputtype) 
 options.args <- sapply(listoptions,function(x){
-         unlist(strsplit(x, ' '))[-1]
+         unlist(strsplit(x, '[ \t\n]+'))[-1]
         })
 # same as the step above, except that only the names are kept
 options.names <- sapply(listoptions,function(x){
-  option <-  unlist(strsplit(x, ' '))[1]
+  option <-  unlist(strsplit(x, '[ \t\n]+'))[1]
 })
 names(options.args) <- unlist(options.names)
 
@@ -83,9 +103,9 @@
 if (typeinput=="tabfile"){
   
   if (header=="TRUE"){
-    sample = read.table(listfile,header=TRUE,sep="\t",na.strings="NA",fill=TRUE)  
+    sample = readfile(listfile, "true")  
   }else{
-    sample = read.table(listfile,header=FALSE,sep="\t",na.strings="NA",fill=TRUE)  
+    sample = readfile(listfile, "false")
   }
   sample = sample[,column]