annotate join_files.R @ 0:ada3968ebf26 draft default tip

planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
author iuc
date Wed, 09 Aug 2017 17:44:39 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
1 # libraries
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
2 library(data.table)
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
3 library(parallel)
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
4
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
5 # inputs
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
6 args <- commandArgs()
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
7
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
8 input <- gsub("--in=", "", args[grepl("--in=", args)])
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
9 header <- as.integer(gsub("--he=", "", args[grepl("--he=", args)]))
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
10 join_col <- gsub("--jc=", "", args[grepl("--jc=", args)])
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
11 separator <- gsub("--sep=", "", args[grepl("--sep=", args)])
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
12 null_char <- gsub("--nc=", "", args[grepl("--nc=", args)])
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
13 output <- gsub("--out=", "", args[grepl("--out=", args)])
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
14
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
15 # test VARS
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
16 # input <- list("test-data/df1.txt", "test-data/df2.txt", "test-data/df3.txt")
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
17 #
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
18 # input <- list("test-data/df_big_1.txt", "test-data/df_big_2.txt", "test-data/df_big_3.txt",
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
19 # "test-data/df_big_4.txt", "test-data/df_big_5.txt", "test-data/df_big_6.txt",
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
20 # "test-data/df_big_7.txt", "test-data/df_big_8.txt", "test-data/df_big_9.txt",
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
21 # "test-data/df_big_10.txt")
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
22 # header <- 1
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
23 # join_col <- 1
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
24 # separator <- "ta"
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
25 # null_char <- 0
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
26 # output <- "out"
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
27
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
28 if(header > 0){
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
29 header <- TRUE
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
30 }else{
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
31 header <- FALSE
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
32 }
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
33 join_col <- as.integer(join_col)
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
34
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
35 # read files into list
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
36 df_list <- lapply(input, function(x){as.data.frame(fread(x))})
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
37
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
38
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
39 #### fix the ids name for all read in tables
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
40 df_list <- lapply(df_list, function(x){
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
41 names_x <- names(x)
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
42 names_x[names_x == "ids"] <- "id" # to join correctly
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
43 names_x[join_col] <- "ids"
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
44 names(x) <- names_x
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
45 x
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
46 })
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
47
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
48 # generate unique ids string
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
49 df0 <- lapply(df_list, function(x){
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
50 x[join_col]
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
51 })
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
52
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
53 df0 <- data.frame(ids=unique(do.call(rbind, df0)))
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
54 df_list <- append(df0, df_list)
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
55 df_list[[1]] <- data.frame(ids=df_list[[1]])
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
56
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
57
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
58
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
59 ids <- df_list[[1]]
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
60 ids <- data.frame(ids = ids[order(ids$ids), "ids" ])
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
61 merged_df <- mclapply(2:length(df_list), function(x){
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
62 merged_sub <- merge(x = ids, y = df_list[[x]], by = "ids", all.x = T, sort = F)
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
63 merged_sub <- merged_sub[order(merged_sub$ids), ]
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
64 merged_sub[-1]
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
65 }, mc.cores=4)
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
66
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
67 df <- cbind(ids, do.call(cbind, merged_df))
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
68
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
69 # benchmarking
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
70 # library(microbenchmark)
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
71 # microbenchmark(
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
72 # df1 <- lapply(2:length(df_list), function(x){
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
73 # merge(df_list[[1]], df_list[[x]], by = "ids", all.x = T)[-1]
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
74 # }),
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
75 # merged_df <- mclapply(2:length(df_list), function(x){
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
76 # merge(x = ids, y = df_list[[x]], by = "ids", all.x = T, sort = F)[-1]}, mc.cores=7)
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
77 # ,times = 4
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
78 # )
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
79
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
80 # change null_char
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
81 df[is.na(df)] <- null_char
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
82 # separator <- "ta"
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
83 # print(separator)
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
84 delim <- list(ta = "\t", do = ".", co = ",", un = "_", da = "-", sp = " ")
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
85 # print(separator)
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
86 separator <- delim[[separator]]
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
87 # write data.frame to file
ada3968ebf26 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/join_files_by_id commit a835a0b20127f485a6af4b466bb29d58d63541ea
iuc
parents:
diff changeset
88 write.table(df, file = output, sep = separator, row.names = F, quote = F, col.names = header)