R/plotfunction_DNA.R

Defines functions trans_color DNA_plot DNA_fractions

# these functions are derived from the ones in the file `plotfunction.R`

trans_color = function(x){
  if (x == "g") return("red")
  else if (x == "c") return("orange")
  else if (x == "t") return("green")
  else return ("yellow")
}


# the following plot function plots the segmented DNA given the sequence `data` and the change points `cp`
DNA_plot <- function(data, cp, ver_height = 10, seg=TRUE){
  if (!seg){
    col_ls <- sapply(data, trans_color)
    y = rep(ver_height, length(data))
    plot(1:length(data), y, pch="|", cex=5, col=col_ls, ylim = c(-10,10), xlab=" ", ylab=" ", xaxt="n", yaxt="n", main="DNA Plot")
    legend("top", legend=c("Adenine", "Thynine", "Cytosine", "Guanine")
           ,col = c("yellow", "green", "orange", "red"), pch=15, box.lty = 0, horiz=TRUE)
  }
  else {
    #par(bg="gray90")
    par(bg="white")
    cp = c(1, cp, length(data))

    col_ls <- sapply(data, trans_color)
    y = rep(ver_height, length(data))
    plot(1:length(data), y, pch="|", cex=1, col=col_ls, ylim = c(-length(cp)*5-20,50), xlab=" ", ylab=" ", xaxt="n", yaxt="n", main="DNA segments")

    for (i in 1:(length(cp)-1)){
      col_ls = sapply(data[cp[i]:(cp[i+1]-1)], trans_color)
      points((cp[i]:(cp[i + 1]-1)), rep(i*-5, length(cp[i]:(cp[i + 1]-1))), pch="|", cex=1, col=col_ls)
      #abline(v=cp[i], col="blue", lwd=3, lty=2)
    }
    legend("top", legend=c("Adenine", "Thynine", "Cytosine", "Guanine")
           ,col = c("yellow", "green", "orange", "red")
           ,pch=15, box.lty = 0, horiz=TRUE, bg = "white")
  }

}

DNA_fractions <- function(data, cp){

  par(bg="white")

  bases=c("a", "t", "g", "c")



  sum_matrix = matrix(0, 4, length(data))

  for (i in seq_along(bases))
  {
    sum_matrix[i,] = cumsum(data==bases[i])
  }

  plot(0, xlim = c(0, length(data)), ylim=c(0, max(sum_matrix[,length(data)])), type='n', axes=T, xlab='Index', ylab='Number of nucleotide', )


  for (i in seq_along(bases))
  {
    lines(1:length(data), sum_matrix[i,], col=trans_color(bases[i]))
  }
  for (point in cp)
  {
    abline(v=point, lty=2)
  }

  legend("top", legend=c("Adenine", "Thynine", "Cytosine", "Guanine")
         ,col = c("yellow", "green", "orange", "red")
         ,pch=15, box.lty = 0, horiz=TRUE, bg = "white")
  }
aaaaana/changepoints documentation built on May 26, 2019, 1:35 p.m.