comparison h-concatenate.R @ 1:defa57c7775e draft

planemo upload for repository https://github.com/computational-metabolomics/dma-tools-galaxy commit 6c48bd51987a28401de6cf5e49b1b30e5e73fe16-dirty
author tomnl
date Tue, 27 Mar 2018 07:18:42 -0400
parents
children
comparison
equal deleted inserted replaced
0:d2940fcb7104 1:defa57c7775e
1 library(optparse)
2
3 option_list <- list(
4 make_option(c("-f", "--files"), type="character"),
5 make_option(c("-c", "--columns"), type="character"),
6 make_option(c("-o", "--out_dir"), type="character"),
7 make_option(c("-u", "--uc"), type="character")
8 )
9
10 # store options
11 opt<- parse_args(OptionParser(option_list=option_list), args = commandArgs(trailingOnly = TRUE))
12
13 print(sessionInfo())
14 print(opt)
15
16 file_paths <- trimws(strsplit(opt$files, ';')[[1]])
17 file_paths <- file_paths[file_paths != ""]
18
19 columns <- trimws(strsplit(opt$columns, ';')[[1]])
20 columns <- columns[columns != ""]
21
22 columns <- lapply(columns, function(x){trimws(strsplit(x, ',')[[1]])})
23
24
25 readIn <- function(f, c){
26 print('#### reading in file with columns ####')
27 print(f)
28 print(c)
29 df <- read.table(f, header = TRUE, sep='\t', stringsAsFactors=FALSE)
30 if (sum(colnames(df) %in% c)==0){
31 print('PLEASE CHECK: Selected columns not in file!!')
32 print(colnames(df))
33 print(c)
34 q()
35 }
36
37 df <- df[ , (colnames(df) %in% c)]
38 if (length(c)==1){
39 df <- data.frame(df)
40 colnames(df) <- c
41 }
42
43 return(df)
44 }
45
46 m <- mapply(readIn, file_paths, columns, SIMPLIFY = FALSE)
47
48 m <- unname(m)
49 merged <- do.call(cbind, m)
50
51 if(!is.null(opt$uc)){
52 uc <- trimws(strsplit(opt$uc, ',')[[1]])
53 uc <- uc[uc != ""]
54 print(colnames(merged))
55 print(uc)
56 merged <- merged[uc]
57 }
58
59 print(head(merged))
60
61 write.table(merged, file.path(opt$out_dir, 'combined_table.tsv'), row.names=FALSE, sep='\t')
62
63