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