calder 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. if(file.exists(opt$feature_track)){
  70. opt$feature_track <- read.table(opt$feature_track)
  71. }
  72. return(opt)
  73. }
  74. sanitize_chroms <- function(chroms){
  75. res <- lapply(chroms, function(x){
  76. if(startsWith(x, "chr")){
  77. return(substring(x, 4))
  78. } else{
  79. return(x)
  80. }
  81. })
  82. return(res)
  83. }
  84. handle_input_hic <- function(opt){
  85. suppressPackageStartupMessages(library(strawr))
  86. chromsizes <- readHicChroms(opt$input)
  87. if(opt$chromosomes == "all"){
  88. chroms <- chromsizes[!(toupper(chromsizes$name) %in% toupper(CHROMS_TO_REMOVE)), "name"]
  89. }
  90. else{
  91. chrom_list <- strsplit(opt$chromosomes, ",")[[1]]
  92. chroms <- chromsizes[chromsizes$name %in% chrom_list, "name"]
  93. }
  94. chroms <- sanitize_chroms(chroms)
  95. CALDER(contact_file_hic = opt$input,
  96. chrs = chroms,
  97. bin_size = opt$bin_size,
  98. genome = opt$genome,
  99. save_dir=opt$outpath,
  100. save_intermediate_data=TRUE,
  101. feature_track=opt$feature_track,
  102. single_binsize_only=!opt$adaptive,
  103. n_cores = opt$nproc,
  104. sub_domains=TRUE)
  105. }
  106. handle_input_cool <- function(opt){
  107. intermediate_data_dir = file.path(opt$outpath, "intermediate_data")
  108. dir.create(intermediate_data_dir, recursive=TRUE, showWarnings=FALSE)
  109. system(paste0("cooler dump --table chroms --out ",
  110. file.path(intermediate_data_dir, "chroms.txt"),
  111. " --header ",
  112. opt$input))
  113. chroms <- read.table(file.path(intermediate_data_dir, "chroms.txt"), sep="\t", header=TRUE)
  114. if(opt$chromosomes == "all"){
  115. chroms <- chroms[!( toupper(chroms$name) %in% toupper(CHROMS_TO_REMOVE) ), "name"]
  116. }
  117. else{
  118. chrom_list <- strsplit(opt$chromosomes, ",")[[1]]
  119. chroms <- chroms[chroms$name %in% chrom_list, "name"]
  120. }
  121. dump_paths <- list()
  122. for(chrom in chroms){
  123. cat(paste0("[Pre-processing] Dumping ", chrom, "\n"))
  124. chrom_dump_path <- file.path(intermediate_data_dir, paste0(chrom, "_dump.txt"))
  125. dump_paths <- c(dump_paths, chrom_dump_path)
  126. if(! file.exists(chrom_dump_path)){
  127. system(paste0("cooler dump --table pixels --range ",
  128. chrom,
  129. " --join --balanced ",
  130. opt$input,
  131. " | cut -f2,5,8 | awk '{if ($3) print;}' > ",
  132. chrom_dump_path))
  133. }
  134. }
  135. chroms <- sanitize_chroms(chroms)
  136. names(dump_paths) <- chroms
  137. CALDER(contact_file_dump=dump_paths,
  138. chrs=chroms,
  139. bin_size=opt$bin_size,
  140. genome=opt$genome,
  141. save_dir=opt$outpath,
  142. feature_track=opt$feature_track,
  143. single_binsize_only=!opt$adaptive,
  144. save_intermediate_data=TRUE,
  145. n_cores=opt$nproc,
  146. sub_domains=TRUE)
  147. }
  148. opt <- parse_arguments()
  149. if(opt$type == "hic"){
  150. handle_input_hic(opt)
  151. } else if(opt$type == "cool"){
  152. handle_input_cool(opt)
  153. } else {
  154. stop("Unknown input type")
  155. }
  156. # Cleaning the output
  157. intermediate_data_dir = file.path(opt$outpath, "intermediate_data")
  158. if(dir.exists(intermediate_data_dir) && (!opt$keep_intermediate)){
  159. cat('[Post-processing] Removing intermediate data\n')
  160. unlink(intermediate_data_dir, recursive=TRUE)
  161. }
  162. exec_time_file = "./total_execution.time"
  163. if(file.exists(exec_time_file)){
  164. cat("[Post-processing] Removing total_execution.time\n")
  165. file.remove(exec_time_file)
  166. }