Whereas wouldn't be as fast as awk
, here is a rather dirty R solution:
library(dplyr)
library(tidyr)
library(magrittr)
# File 1
# the "fill" parameter is used since not all the rows have the same number of cols
file_1 <- read.table("file_1.txt", skip = 1, fill = TRUE)
# paste() function with argument collapse is applied to the "phenotype"
# columns of each row (these cols start at position 6 and goes until
# the number of cols: dim(x)[2] gives the number of cols of a table
phenotypes <- apply(file_1[, 6:dim(file_1)[2]], 1, function(x) paste(x, collapse = ""))
# Cols referring to the genomic position, ref and alteration (cols 1:5)
# are combined with the newly created "phenotypes" column
file_1 <- data.frame(file_1[, 1:5], phenotypes)
# colnames are added, could be avoided if dealt with at file reading step
names(file_1) <- c("Chr", "Start", "End", "Ref", "Alt", "Phenotypes")
# File 2
file_2 <- read.table("file_2.txt", skip = 1, fill = TRUE)
phenotypes <-apply(file_2[, 6:dim(file_2)[2]], 1, function(x) paste(x, collapse = ""))
file_2 <- data.frame(file_2[, 1:5], phenotypes)
names(file_2) <- c("Chr", "Start", "End", "Ref", "Alt", "Phenotypes")
# The two tables generated as above are merged using the appropriate
# columns as "anchors" in the "by" argument of full_join()
# The resulting Phenotypes.x and Phenotypes.y cols are concatenated
merged_tables <- full_join(file_1,
file_2,
by = c("Chr", "Start", "End", "Ref", "Alt"))
merged_tables$Phenotypes <- paste(merged_tables$Phenotypes.x,
merged_tables$Phenotypes.y,
sep = ",")
merged_tables$Phenotypes.x <- NULL
merged_tables$Phenotypes.y <- NULL
# Duplicated rows (in terms of genomic pos, ref and alteration) in any
# of the files result in duplicated rows in the merged table and are
# "handled" by aggregating/summarizing the info at the phenotype cols
# by using paste()
# strsplit() is used to "split" the "phenotypes" on "," in order to
# remove repeating phenotypes resulting from the full_join() call
merged_tables %<>% group_by(Chr, Start, End, Ref, Alt) %>%
summarize(Phenotypes = paste(Phenotypes, collapse = ",")) %>%
ungroup() %>%
mutate(Phenotypes = strsplit(Phenotypes, ","))
merged_tables$Phenotypes <- lapply(merged_tables$Phenotypes, function(x) paste(unique(unlist(x)), collapse = ","))
merged_tables$Phenotypes <- unlist(merged_tables$Phenotypes)
# A tibble: 3 x 6
Chr Start End Ref Alt Phenotypes
<int> <int> <int> <fct> <fct> <chr>
1 1 1000 1000 A T PhenoA,PhenoB
2 2 2000 2000 T G PhenoF,PhenoG,PhenoH,PhenoI
3 3 3000 3000 C G PhenoL,PhenoM,PhenoN
# The resulting table (tibble in this case) can be saved as a file
# using write.table() or write.xlsx()
paste()
in combination with its collapse
parameter is used because of the unequal numbers of columns in the files. fread()
of the data.table
package could be helpful for reading large files.
#FILE A
and#FILEB
lines actually part of the file? And is#OUTPUT
required in the output? Also, are the files small enough to fit at least one of them in memory? What is the field separator? Is it space(s)? Tabs? $\endgroup$Pheno F,
andPheno G
for example, right? $\endgroup$