R/drawingIdeo.R

Defines functions drawingIdeo

Documented in drawingIdeo

#' @title Create a Ideogram(karyogram) object
#' 
#' @description This function draw an ideogram with hit data.
#' 
#' @usage drawingIdeo(hits, feature, featureName, organism = 'hg19', outpath = '~')
#' 
#' @param hits a GR object. This object made from makeInputs function.
#' @param feature a GR object. This object made from annotation function. 
#' @param featureName a character vector. This value used when saving the idegoram image file.
#' @param organism a character vector. This function serves 3 versions of organisms such as hg19, hg38 (Human)
#'                 and galGal6 (Chicken). Default is 'hg19'.
#' @param outpath a string vector. Type path to save a plot.
#'
#'  
#' @return Return an ideogram object.
#' 
#' 
#' @export

drawingIdeo = function(hits, feature, featureName, organism = 'hg19', outpath = '~'){
  library(stringr)
  library(karyoploteR)

  cat('---------- Drawing an ideogram ----------\n')
  cat(paste0('Start time : ', date(), '\n'))
  
  if(length(which(c('hg19', 'hg38', 'galGal6') %in% organism)) == 0){
    return(cat("[ERROR] You can use hg19/hg38/galGal6 data only. ( Input : ", paste(organism, collapse = ','), ")\n",
               '---------- Plotting process is halted. ----------\nFinish time : ', date(), '\n'))
  } else {}

  cat("---------- Loading chromosome data ----------\n")
  
  #chromosome length
  chr_length = readRDS(file = system.file("extdata", paste0(organism, '_chrom.rds'), package = "IRFinder"))

  feats = as.data.frame(feature, stringsAsFactors = FALSE)
  feats = feats[,c(1,2,3)]
  feats = data.frame(cbind(feats, 'gieStain' = rep('feats', nrow(feats))), stringsAsFactors = FALSE)
  tmp_len = data.frame(data.frame('chrom' = paste0('chr', chr_length[,1]), 'start' = 1, 'end' = chr_length[,2], 
                       stringsAsFactors = FALSE), 'gieStain' = rep('chr', nrow(chr_length)), stringsAsFactors = FALSE)
  names(feats) = names(tmp_len)
  feats = data.frame(rbind(feats, tmp_len), stringsAsFactors = FALSE)
  
  cat("Done.\n")

  cat("----------  Drawing an ideogram plot ----------\n")
  
  png(filename = paste0(outpath, '/Ideogram_', featureName, '_', organism, '.png'), width = 1200, height = 750)
  ideo = plotKaryotype(genome=regioneR::toGRanges(tmp_len), 
                       cytobands = feats)

  exp_data = kpPlotMarkers(ideo, hits[[2]], r1 = 0.5, labels = rep('E', length(hits[[2]])), line.color = 'blue',
                           label.color = 'blue', cex = 0.5)
  dev.off()

  cat('---------- Drawing an ideogram is finished. ----------\n')
  cat(paste0('Finish time : ', date(), '\n'))
  return(ideo)
}
bioinfo16/VVIPS documentation built on Nov. 4, 2019, 7:20 a.m.