# 
# A script to plot BAM file coverage within the hic-te pipeline
# 
library(karyoploteR)
library(GenomicRanges)
library(Rsamtools)

args = commandArgs(trailingOnly=TRUE)

# create a custom genome GRanges structure
chr_len <- read.table(args[1])
colnames(chr_len) <- c("chr","end")
chr_len$start <- rep(1,length(chr_len[,1]))
chr_len_gr <- makeGRangesFromDataFrame(chr_len)

# plot the bam file
png(file = paste0("plotBamCoverage.png"), units="in", width=6, height=6, res=300)
kp <- plotKaryotype(genome = chr_len_gr)
kp <- kpPlotBAMDensity(kp, data=args[2])

# Plotting centromeres not ideal yet but works
# cm <- read.table("~/git/hic-te/data/Osat_regions.gff3")
# colnames(cm) <- c("chr","source","so","start","end","score","strand","frame","name")
# cm$gieStain = rep("acen",length(cm[,1]))
# kp <- plotKaryotype(genome = chr_len_gr, cytobands=makeGRangesFromDataFrame(rbind(chr_len[,c(1,3,2)],cm[,c(1,4,5)])))
# kp <- kpPlotBAMDensity(kp, data="Osativa_323_v7.0_fragments_toHiC_All_sampled_sorted.bam")
dev.off()
