-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathharmonize_matrices_RsQTL.R
46 lines (35 loc) · 2.04 KB
/
harmonize_matrices_RsQTL.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# HARMONIZE_MATRICES_RSQTL.R
# LAST UPDATED BY LIAM FLINN SPURR ON NOVEMBER 26, 2019
# load packages
suppressMessages(library(tidyverse))
suppressMessages(library(data.table))
handle_command_args <- function(args) {
# make sure all flags are paired
if(length(args) %% 2 != 0) stop("Command line arguments supplied incorrectly!")
# load the flags into a "dictionary"
arg_df <<- data.frame(cbind(flag = args[seq(1, length(args), by = 2)], value = args[seq(2, length(args), by = 2)])) %>%
mutate_all(as.character)
# load in matrices
vaf <<- data.frame(fread(arg_df$value[arg_df$flag == "-r"]))
spl <<- data.frame(fread(arg_df$value[arg_df$flag == "-s"]))
covar <<- data.frame(fread(arg_df$value[arg_df$flag == "-c"]))
}
# take arguments from the command line
args <- commandArgs(trailingOnly = TRUE)
handle_command_args(args)
# get the samples present in each
v.names <- colnames(vaf)[-1]
s.names <- colnames(spl)[-1]
c.names <- colnames(covar)[-1]
# get the samples in all 3 matrices
conc <- intersect(intersect(v.names, s.names), c.names)
# select only the concordant samples from all 3 matrices
vaf.clean <- vaf[,c("SNV", conc)]
spl.clean <- spl[,c("intron", conc)]
covar.clean <- covar[,c("id", conc)]
# write the fixed outputs to a file
write.table(vaf.clean, paste0("output/", gsub(".*/|.txt", "", arg_df$value[arg_df$flag == "-r"]), "_harmonized.txt"), quote = F, row.names = F, sep = '\t')
write.table(spl.clean, paste0("output/", gsub(".*/|.txt", "", arg_df$value[arg_df$flag == "-s"]), "_harmonized.txt"), quote = F, row.names = F, sep = '\t')
write.table(covar.clean, paste0("output/", gsub(".*/|.txt", "", arg_df$value[arg_df$flag == "-c"]), "_harmonized.txt"), quote = F, row.names = F, sep = '\t')
cat(paste0("Matrices containing concordant sample lists saved to output/", gsub(".*/|.txt", "", arg_df$value[arg_df$flag == "-r"]), "_harmonized.txt, output/",
gsub(".*/|.txt", "", arg_df$value[arg_df$flag == "-s"]), "_harmonized.txt, and output/", gsub(".*/|.txt", "", arg_df$value[arg_df$flag == "-c"]), "_harmonized.txt.\n"))