Skip to content

Instantly share code, notes, and snippets.

@hussius
Last active November 17, 2016 09:38
Show Gist options
  • Save hussius/09e916fdb71f79b78904af55c6403951 to your computer and use it in GitHub Desktop.
Save hussius/09e916fdb71f79b78904af55c6403951 to your computer and use it in GitHub Desktop.
R script for merging Kallisto TPMs from output directories below path given as command-line argument
args = commandArgs(trailingOnly=TRUE)
path=args[1]
files=Sys.glob(paste0(path,"/*/abundance.tsv"))
#print(files)
merge_two <- function(x,y){
#print(dim(x))
if ("tpm" %in% colnames(x)){
x_ <- x[,c(1,5)]
}
else{
x_ <- x
}
y_ <- y[,c(1,5)]
z <- merge(x_, y_, by="target_id")
return(z)
}
read.delim.print <- function(x){
print(x)
read.delim(x, header=TRUE)
}
#x <- lapply(files, function(f) read.delim.print(f))
#suppressWarnings(mx <- Reduce(merge_two, x))
ctr = 0
#print(files[1])
mx <- read.delim(files[1],header=TRUE)
for(i in 2:length(files)){
f2 = files[i]
print(paste('Merge', ctr))
tmp <- merge_two(mx, read.delim(f2, header=TRUE))
mx <- tmp
ctr <- ctr + 1
}
rn <- mx[,1]
mx <- mx[-1]
rownames(mx) <- rn
colnames(mx) <- sapply(files, function(x) unlist(strsplit(x, split="/"))[2])
write.table(mx,file="kallisto_TPM.txt",quote=FALSE,sep="\t")
@hussius
Copy link
Author

hussius commented Nov 17, 2016

Originally implemented by reading all files into memory and merging by Reduce() applied to merge, this was rewritten to do it file by file due to memory issues on a machine where I tried to do this on 200 samples.

Run by

Rscript merge_kallisto_TPM.R directory above Kallisto output directories

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment