R/plots.R

# Plotting functions to visuzalize labeled and total RNA based on synthesis and
# degradation rates
# 
# Author: waltzthoeni, demel
###############################################################################

#' DTA model function for labeled RNA
#' 
#' @param mu synthesis rate
#' @param lambda degradation rate
#' @param time time points
#' @return list with total and labeled RNA values per time point given the specified synthesis and degradation rate.
#' 
#' @author Thomas Walzthoeni
#' @export
dta.model.labeled <- function(
		mu = 1,
		lambda = 0.2,
		time = 0:120
) {
	# calculate the labeled RNA
	labeled <- (mu / lambda) * (1 - exp( -lambda * time))
	
	res.list <- list(
			labeled = labeled, 
			time = time, 
			total = mu / lambda, 
			mu = mu, 
			lambda = lambda
	)
	return(res.list)
}


#' Function to create a plot based on a DTA model result
#' 
#' @param dtalist list as generated by \code{dta.model.labeled} 
#' @param labeledcol color for newly synthesized, labeled RNA
#' @param totalcol color for total RNA
#' @param showformula logical; should the mathematical formulas be displayed in the plot
#' 
#' @examples
#' d1=dta.model.labeled(100,0.05)
#' dta.plot.labeled(d1,showformula=TRUE)
#' d2=dta.model.labeled(50,0.1)
#' dta.plot.labeled(d2,showformula=FALSE)
#' @author Thomas Walzthoeni
#' @export
dta.plot.labeled <- function(
		dtalist,
		labeledcol="blue",
		totalcol = "red",
		showformula=TRUE
) {
	
	x <- dtalist$time
	y <- dtalist$labeled
	total <- dtalist$total
	mu <- dtalist$mu
	lambda <- dtalist$lambda
	
	rangey <- range(y, total) * 1.2
	rangex <- range(x) * 1.0
	
	mu_str <- expression(mu)
	lam_str <- expression(lambda)
	
	main <- bquote(paste("Total and labeled RNA, ", mu, "=", 
					.(mu), ", ", lambda, "=", .(lambda)))
#	"=1"
	plot(x, y, type = "l", col = labeledcol, ylim = rangey, lty = 1, lwd = 4,
			main = main, ylab = "RNA", xlab = "Time")
	abline(total, 0, col = totalcol, lty = 2, lwd = 4)
	
	
	legend(0, rangey[2], c("Total RNA (steady state)","Labeled RNA"), cex = 0.8,
			col = c(totalcol,labeledcol), lty = c(2,1), lwd = c(4,4))
	formula_T <- expression("Total" == frac(mu, lambda))
	formula_L <- expression("Labeled RNA" == paste(frac(mu, lambda), 
					" ", (1-e^(-lambda*t))))
	
	if (showformula == TRUE) {
		text(rangex[2], total * 1.05, formula_T, cex = 1, adj = 1)
		text(rangex[2], rangey[2] * 0.1, formula_L, cex = 1, adj = 1)
	}
}


#' Exponential decay of unlabeled RNA
#' 
#' @param total total amount of RNA at beginning
#' @param lambda degradation rate
#' @param time time points
#' @return list with total and unlabeled RNA values per time point given the 
#' specified degradation rate
#' 
#' @examples 
#' decay=dta.model.decay(100,0.1)
#' 
#' @author Thomas Walzthoeni
#' @export
dta.model.decay <- function(
		total = 100,
		lambda = 0.2,
		time = 0:120
) {  
	# calculate the unlabeled RNA
	unlabeled <- total * exp(-lambda * time)
	halflife <- log(2) / lambda
	
	res.list <- list(
			unlabeled = unlabeled,
			time = time,
			total = total,
			lambda = lambda,
			halflife = halflife
	)
	return(res.list)
}


#' Function to plot the decay of the unlabeled RNA (remaining RNA)
#' 
#' @param decaylist list as generated by \code{dta.model.decay} 
#' @param unlabeledcol color for unlabeled RNA
#' @param totalcol color for total RNA
#' @param showformula logical; should the mathematical formulas be displayed
#'  in the plot
#' 
#' @examples
#' decay=dta.model.decay(100,0.1)
#' dta.plot.decay(decay,showformula=TRUE)
#' 
#' @author Thomas Walzthoeni
#' @export
dta.plot.decay <- function(
		decaylist,
		unlabeledcol = "yellow",
		totalcol = "red",
		showformula = TRUE
) {
	x <- decaylist$time
	y <- decaylist$unlabeled
	total <- decaylist$total
	lambda <- decaylist$lambda
	
	rangey <- range(y, total) * 1.2
	rangex <- range(x) * 1.0
	
	lam_str <- expression(lambda)
	
	main <- bquote(paste("Unlabeled RNA, ", lambda, "=", .(lambda), 
					", Total RNA=", .(total)))
	plot(x, y, type = "l", col = unlabeledcol, ylim = rangey, lty = 1, lwd = 4,
			main = main, ylab = "Unlabeled RNA", xlab = "Time")
	abline(total, 0, col = totalcol, lty = 2, lwd = 4)
	
	halflife <- decaylist$halflife 
	abline(h = 50, v = halflife, col = "lightgray", lty = 3, lwd = 2)
	
	legend(0, rangey[2], c("Total RNA (steady state)", "Unlabeled RNA"),
			cex = 0.8, col = c(totalcol, unlabeledcol),
			lty = c(2, 1), lwd = c(4, 4))
	
	formula2 <- expression("T"[1/2] == frac("ln(2)", lambda))
	formula <- expression("Unlabeled RNA" == 
					paste("Total RNA * ", e^(-lambda*t) ))
	
	if (showformula == TRUE) {
		text(rangex[2], rangey[2] * 0.2, formula, cex = 1, adj = 1)
		text(rangex[2], rangey[2] * 0.1, formula2, cex = 1, adj = 1)
	}
}



#' DTA model function for labeled RNA
#' 
#' @param mu synthesis rate
#' @param lambda degradation rate
#' @param time time points
#' @return list with labeled, unlabeled and total RNA values per time point 
#' given the specified synthesis and degradation rate.
#' 
#' @author Thomas Walzthoeni, Carina Demel
#' @export
dta.model <- function(
		mu = 1,
		lambda = 0.2,
		time = 0:120
) {
	# calculate the labeled RNA
	labeled <- (mu / lambda) * (1 - exp(-lambda * time))
	total <- mu / lambda
	unlabeled <- total * exp(-lambda * time)
	halflife <- log(2) / lambda
	
	res.list <- list(
			labeled = labeled,
			unlabeled = unlabeled,
			time = time,
			total = total,
			mu = mu,
			lambda = lambda,
			halflife = halflife
	)
	return(res.list)
}



#' Function to create a plot based on synthesis and degradation rates
#' 
#' @param mu synthesis rate in transcripts min^-1
#' @param lambda degradation rate [min^-1]
#' @param labeledcol color for newly synthesized, labeled RNA
#' @param unlabeledcol color for unlabeled RNA
#' @param totalcol color for total RNA
#' @param showformula logical; should the mathematical formulas be displayed 
#' in the plot
#' @return list with labeled, unlabeled and total RNA values per time point 
#' given the specified synthesis and degradation rate as generated by 
#' \code{dta.model}
#' 
#' @examples
#' res=dta.plot(mu=100,lambda=0.05,showformula=TRUE)
#' @author Thomas Walzthoeni, Carina Demel
#' @export
dta.plot <- function(
		mu,
		lambda,
		labeledcol = "blue",
		unlabeledcol = "yellow",
		totalcol = "red",
		showformula = TRUE
) {
	
	dtalist <- dta.model(mu, lambda)
	x <- dtalist$time
	y <- dtalist$labeled
	u <- dtalist$unlabeled
	total <- dtalist$total
	mu <- dtalist$mu
	lambda <- dtalist$lambda
	
	rangey <- range(y, total) * 1.2
	rangex <- range(x) * 1.0
	
	mu_str <- expression(mu)
	lam_str <- expression(lambda)
	
	main <- bquote(paste("Total, labeled and unlabeled RNA, ", mu, "=", 
					.(mu), ", ", lambda, "=", .(lambda)))
#	"=1"
	plot(x, y, type = "l", col = labeledcol, ylim = rangey, lty = 1, lwd = 4,
			main = main, ylab = "RNA", xlab = "Time")
	lines(x, u, col = unlabeledcol, lty = 3, lwd = 4)
	abline(total, 0, col = totalcol, lty = 2, lwd = 4)
	
	halflife <- dtalist$halflife 
	abline(h = 50, v = halflife, col = "lightgray", lty = 3, lwd = 2)
	
	
	legend(0, rangey[2], c("Total RNA (steady state)", "Labeled RNA",
					"Unlabeled RNA"), cex = 0.8,
			col =c(totalcol, labeledcol, unlabeledcol), lty = c(2, 1, 3),
			lwd = c(4, 4, 4))
	formula_T <- expression("Total" == frac(mu, lambda))
	formula_L <- expression("Labeled RNA" == paste(frac(mu, lambda), " ", 
					(1-e^(-lambda*t))))
	formula_U <- expression("Unlabeled RNA" == paste("Total RNA * ",
					e^ (-lambda*t) ))	
	formula_HL <- expression("T"[1/2] == frac("ln(2)", lambda))
	
	if (showformula == TRUE) {
		text(rangex[2], total * 1.05, formula_T, cex = 1, adj=1)
		text(rangex[2], total * 0.90, formula_L, cex = 1, adj=1)
		text(rangex[2], rangey[2] * 0.2, formula_HL, cex = 1, adj=1)
		text(rangex[2], rangey[2] * 0.1, formula_U, cex = 1, adj=1)
	}
	return(dtalist)
}
carinademel/RNAlife documentation built on May 13, 2019, 12:43 p.m.