calder 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #!/usr/bin/env Rscript
  2. suppressPackageStartupMessages(library(optparse))
  3. suppressPackageStartupMessages(library(CALDER))
  4. AVAILABLE_REFERENCE_TRACKS_GENOMES <- c("hg19", "hg38", "mm9", "mm10")
  5. INPUT_TYPES <- c("hic", "cool")
  6. CHROMS_TO_REMOVE <- c("ALL", "M", "chrM", "MT", "chrMT", "Y", "chrY")
  7. parse_arguments <- function(){
  8. # Creating the argument parsing options
  9. option_list = list(
  10. make_option(c("-i", "--input"), action="store", default=NA, type='character',
  11. help="Input Hi-C contacts"),
  12. make_option(c("-t", "--type"), action="store", default='hic', type='character',
  13. help="The type of input: hic or cool [default %default]"),
  14. make_option(c("-b", "--bin_size"), action="store", default=50000, type='integer',
  15. help="Bin size to use for the analysis [default %default]"),
  16. make_option(c("-g", "--genome"), action="store", default="hg19", type='character',
  17. help="Genome assembly to use [default %default]"),
  18. make_option(c("-f", "--feature_track"), action="store", default=NA, type='character',
  19. help="Genomic feature track to be used to determine A/B compartment direction
  20. when genome == 'others'. The track should presumably have higher values
  21. in A than in B compartmnets. [default %default]"),
  22. make_option(c("-c", "--chromosomes"), action='store', default='all', type='character',
  23. help="Chromosomes to analyze, separated by comma. [default %default]"),
  24. make_option(c("-p", "--nproc"), action="store", default=1, type='integer',
  25. help="Number of cores to use [default %default]"),
  26. make_option(c("-o", "--outpath"), action="store", default=NA, type='character',
  27. help="Path to the output folder"),
  28. make_option(c("-k", "--keep_intermediate"), action="store_true", default=FALSE, type='logical',
  29. help="Keep intermediate data after done [default %default]"),
  30. make_option(c("-a", "--adaptive"), action="store_true", default=FALSE, type='logical',
  31. help="Use adaptive resolution choice [default %default]")
  32. )
  33. parser <- OptionParser(usage = "%prog [options]", option_list=option_list)
  34. opt <- parse_args(parser)
  35. # Checking if input path exists
  36. if(is.na(opt$input)){
  37. print_help(parser)
  38. stop(paste0("Input path (", opt$input,") does not exist"))
  39. }
  40. # Checking if output path is provided
  41. if(is.na(opt$outpath)){
  42. stop("Output path was not provided")
  43. }
  44. # Check that the input type is one of the possible ones
  45. if(!(opt$type %in% INPUT_TYPES)){
  46. stop(paste0("Input type ", opt$input_type, " not available"))
  47. }
  48. # Check if the provided genome is in the list of available reference genomes
  49. # or if a feature track is provided
  50. if((!(opt$genome %in% AVAILABLE_REFERENCE_TRACKS_GENOMES)) || (file.exists(opt$feature_track))){
  51. # in this case, we just assign it the name 'others'
  52. opt$genome = "others"
  53. }
  54. writeLines(c(
  55. "*******************************",
  56. "* CALDER *",
  57. "*******************************",
  58. paste0("[Parameters] Input: ", opt$input),
  59. paste0("[Parameters] Input type: ", opt$type),
  60. paste0("[Parameters] Bin size: ", opt$bin_size),
  61. paste0("[Parameters] Genome: ", opt$genome),
  62. paste0("[Parameters] Feature Track: ", opt$feature_track),
  63. paste0("[Parameters] Chromosomes: ", opt$chromosomes),
  64. paste0("[Parameters] N. cores: ", opt$nproc),
  65. paste0("[Parameters] Output: ", opt$outpath),
  66. paste0("[Parameters] Keep Intermediate data: ", opt$keep_intermediate),
  67. paste0("[Parameters] Use adaptive resolution: ", opt$adaptive)
  68. ))
  69. return(opt)
  70. }
  71. sanitize_chroms <- function(chroms){
  72. res <- lapply(chroms, function(x){
  73. if(startsWith(x, "chr")){
  74. return(substring(x, 4))
  75. } else{
  76. return(x)
  77. }
  78. })
  79. return(res)
  80. }
  81. handle_input_hic <- function(opt){
  82. suppressPackageStartupMessages(library(strawr))
  83. chromsizes <- readHicChroms(opt$input)
  84. if(opt$chromosomes == "all"){
  85. chroms <- chromsizes[!(toupper(chromsizes$name) %in% toupper(CHROMS_TO_REMOVE)), "name"]
  86. }
  87. else{
  88. chrom_list <- strsplit(opt$chromosomes, ",")[[1]]
  89. chroms <- chromsizes[chromsizes$name %in% chrom_list, "name"]
  90. }
  91. chroms <- sanitize_chroms(chroms)
  92. CALDER(contact_file_hic = opt$input,
  93. chrs = chroms,
  94. bin_size = opt$bin_size,
  95. genome = opt$genome,
  96. save_dir=opt$outpath,
  97. save_intermediate_data=TRUE,
  98. single_binsize_only=!opt$adaptive,
  99. n_cores = opt$nproc,
  100. sub_domains=TRUE)
  101. }
  102. handle_input_cool <- function(opt){
  103. intermediate_data_dir = file.path(opt$outpath, "intermediate_data")
  104. dir.create(intermediate_data_dir, recursive=TRUE, showWarnings=FALSE)
  105. system(paste0("cooler dump --table chroms --out ",
  106. file.path(intermediate_data_dir, "chroms.txt"),
  107. " --header ",
  108. opt$input))
  109. chroms <- read.table(file.path(intermediate_data_dir, "chroms.txt"), sep="\t", header=TRUE)
  110. if(opt$chromosomes == "all"){
  111. chroms <- chroms[!( toupper(chroms$name) %in% toupper(CHROMS_TO_REMOVE) ), "name"]
  112. }
  113. else{
  114. chrom_list <- strsplit(opt$chromosomes, ",")[[1]]
  115. chroms <- chroms[chroms$name %in% chrom_list, "name"]
  116. }
  117. dump_paths <- list()
  118. for(chrom in chroms){
  119. cat(paste0("[Pre-processing] Dumping ", chrom, "\n"))
  120. chrom_dump_path <- file.path(intermediate_data_dir, paste0(chrom, "_dump.txt"))
  121. dump_paths <- c(dump_paths, chrom_dump_path)
  122. if(! file.exists(chrom_dump_path)){
  123. system(paste0("cooler dump --table pixels --range ",
  124. chrom,
  125. " --join --balanced ",
  126. opt$input,
  127. " | cut -f2,5,8 | awk '{if ($3) print;}' > ",
  128. chrom_dump_path))
  129. }
  130. }
  131. chroms <- sanitize_chroms(chroms)
  132. names(dump_paths) <- chroms
  133. CALDER(contact_file_dump=dump_paths,
  134. chrs=chroms,
  135. bin_size=opt$bin_size,
  136. genome=opt$genome,
  137. save_dir=opt$outpath,,
  138. single_binsize_only=!opt$adaptive,
  139. save_intermediate_data=TRUE,
  140. n_cores=opt$nproc,
  141. sub_domains=TRUE)
  142. }
  143. opt <- parse_arguments()
  144. if(opt$type == "hic"){
  145. handle_input_hic(opt)
  146. } else if(opt$type == "cool"){
  147. handle_input_cool(opt)
  148. } else {
  149. stop("Unknown input type")
  150. }
  151. # Cleaning the output
  152. intermediate_data_dir = file.path(opt$outpath, "intermediate_data")
  153. if(dir.exists(intermediate_data_dir) && (!opt$keep_intermediate)){
  154. cat('[Post-processing] Removing intermediate data\n')
  155. unlink(intermediate_data_dir, recursive=TRUE)
  156. }
  157. exec_time_file = "./total_execution.time"
  158. if(file.exists(exec_time_file)){
  159. cat("[Post-processing] Removing total_execution.time\n")
  160. file.remove(exec_time_file)
  161. }