utils/cbind_count_mats.R

# Jake Yeung
# cbind_count_mats.R
# 2021-06-27
# DESCRIPTION
# 
#     Input list of mats for CSV, output rds output of cbind.fill.lst
# 
# FOR HELP
# 
#     Rscript cbind_count_mats.R --help
# 
# AUTHOR:      Jake Yeung (j.yeung@hubrecht.eu)
# LAB:         Quantitative Biology Lab (https://www.hubrecht.eu/research-groups/van-oudenaarden-group/)
# CREATED ON:  2021-06-27
# LAST CHANGE: see git log
# LICENSE:     MIT License (see: http://opensource.org/licenses/MIT)

suppressPackageStartupMessages(library("argparse"))
library(scchicFuncs)
library(dplyr)
library(data.table)

cbind.fill.lst <- function(mats.lst, all.rnames, fill = 0){
  mats.lst.filled <- lapply(mats.lst, function(mat.tmp){
    missing.rnames <- all.rnames[!all.rnames %in% rownames(mat.tmp)]
    mat.tmp.to.fill <- matrix(data = fill, nrow = length(missing.rnames), ncol = ncol(mat.tmp), dimnames = list(missing.rnames, colnames(mat.tmp)))
    mat.tmp.bind <- rbind(mat.tmp, mat.tmp.to.fill)
    mat.tmp.bind <- mat.tmp.bind[all.rnames, ]
    return(mat.tmp.bind)
  })
  return(do.call(cbind, mats.lst.filled))
}


# create parser object
parser <- ArgumentParser()

# specify our desired options 
# by default ArgumentParser will add an help option 

parser$add_argument('-infile', metavar='INFILE', nargs="+", 
                                            help='Input files csv')
parser$add_argument('-outfile', metavar='OUTFILE',
                                            help='Output rds')
parser$add_argument('--from_bed', action="store_true", default=FALSE,
                                            help='Set if count mat generated by bed file. Otherwise it is a window with stepsize')
parser$add_argument("-v", "--verbose", action="store_true", default=TRUE,
                        help="Print extra output [default]")
                                        
# get command line options, if help option encountered print help and exit,
# otherwise if options not found on command line then set defaults, 
args <- parser$parse_args()

# print some progress messages to stderr if "quietly" wasn't requested
if ( args$verbose ) { 
    print("Arguments:")
    print(args)
}

print(args$from_bed)
dat.csv.lst <- lapply(args$infile, function(inf.tmp){
  print(inf.tmp)
  if (args$from_bed){
    mat.tmp <- ReadMatTSSFormat(inf.tmp, as.sparse = TRUE, add.coord = TRUE, sort.rnames = FALSE)
  } else {
    mat.tmp <- ReadMatSlideWinFormat(inf.tmp, as.sparse = TRUE, sort.rnames = FALSE, add.chromo = TRUE)
  }
  # rownames(mat.tmp) <- paste("chr", rownames(mat.tmp), sep = "")
  return(mat.tmp)
})
rnames.all <- sort(unique(unlist(lapply(dat.csv.lst, function(jmat) rownames(jmat)))))
mat.tmp <- cbind.fill.lst(dat.csv.lst, all.rnames = rnames.all)
saveRDS(mat.tmp, file = args$outfile)
# return(mat.tmp)
jakeyeung/scChIX documentation built on May 7, 2023, 9:14 a.m.