generate_compartments_bed_fun.R 1.1 KB

12345678910111213141516171819202122
  1. ## Yuanlong LIU, 09-08-2018
  2. generate_compartments_bed <- function(input_mat, p_thresh, out_file_name, chr, window.sizes=3, stat_window_size=NULL, bin_size)
  3. {
  4. input_mat_extended = data.frame(chr=paste0('chr', chr), pos_start=0:(nrow(input_mat)-1), pos_end=1:nrow(input_mat), mat=input_mat)
  5. res_input_mat = TopDom_v2(input_mat_extended, window.size=NULL, NULL, T, p_thresh=p_thresh, window.sizes=window.sizes, stat_window_size=stat_window_size, domain_size_min=NULL)
  6. cat('[', as.character(chr),'] Computing compartments\n')
  7. # return(res_input_mat)
  8. to_id = as.numeric(rownames(input_mat)[res_input_mat$domain$to.id])
  9. from_id = as.numeric(rownames(input_mat)[res_input_mat$domain$from.id])
  10. start_poses = (from_id-1)*bin_size + 1
  11. # end_poses = start_poses + n2one*bin_size
  12. end_poses = start_poses + bin_size
  13. input_mat_compartments_bed = data.frame(paste0('chr', chr), as.character(start_poses), as.character(end_poses) )
  14. if(!is.null(out_file_name)) write.table( input_mat_compartments_bed, file=out_file_name, quote=FALSE, row.names=FALSE, col.names=FALSE, sep=' ' )
  15. return( res_input_mat )
  16. }