R/plotIndividual_Tracks.R

Defines functions plotIndividual_Tracks

Documented in plotIndividual_Tracks

#### plotIndividual_Tracks.R Wu Lab, Johns Hopkins University Author:
#### Xiaona Tang Date: Aug 15, 2018

## plotIndividual_Tracks-methods
##' @name plotIndividual_Tracks
##' @aliases plotIndividual_Tracks
##' @title plotIndividual_Tracks
##' @rdname plotIndividual_Tracks-methods
##' @docType methods
##' @description Plot individual tracks one by one, with grid layout.
##' Tracks are sorted by their lengths.
##'              
##' @usage
##'
##' plotIndividual_Tracks(trackll=trackll,grid.size=c(1000,1000),
##' resolution=0.107,t.interval=0.5)
##'
##' @param trackll trajectory list generated by createTrackll() and 
##' processing. 
##' @param grid.size The size of each plot grid in nanometers. Default 1000 nm 
##' for x and y axis, respectively.
##' @param resolution ratio of pixel to uM.
##' @param t.interval Time interval for image aquisition. Default 0.5 sec 
##' (500ms).
##' 
##' @return
##' \itemize{
##' \item{PDF:} One PDF file with 120 tracks on each page.
##' }
##' @details Plot individual track/trajectory one by one, and the tracks will 
##' be layout in 15X8 grids.
##' 
##'
##' @examples
##'
##' # Generate trackll, and process, 
##' # e.g. mask region of interest, tracks from multiple files should not be 
##' # merged.
##' folder=system.file('extdata','HSF',package='sojourner')
##' trackll=createTrackll(folder=folder, input=3)
##' trackll=filterTrack(trackll,filter=c(7,Inf))
##' trackll=maskTracks(folder,trackll)
##' trackll=mergeTracks(folder, trackll)
##' 
##' # Plot individual tracks,
##' plotIndividual_Tracks(trackll,grid.size=c(1000,1000),resolution=0.107,
##'                      t.interval=0.5)

##' @export plotIndividual_Tracks
##' @importFrom lattice panel.xyplot
##' @importFrom lattice panel.number
##' @importFrom lattice xyplot

############################################################################### 



## Function for plotting individual tracks one by one, with grid layout
## (120 tracks/page).  Sorted with track length.

plotIndividual_Tracks <- function(trackll = trackll, grid.size = c(1000, 
    1000), resolution = 0.107, t.interval = 0.5) {
    
    # library(grid)
    
    
    traj.df <- setNames(data.frame(matrix(ncol = 5, nrow = 0)), c("x", 
        "y", "z", "Frame", "trajNo"))
    for (i in c(seq_along(trackll[[1]]))) {
        traj.n <- trackll[[1]][[i]][, c("x", "y", "z")]
        traj.n$x <- (traj.n$x - min(traj.n$x)) * resolution * 1000
        traj.n$y <- (traj.n$y - min(traj.n$y)) * resolution * 1000
        f.n <- rep(i, length(traj.n$x))
        l.n <- rep(length(traj.n$x), length(traj.n$x))
        traj.n$trajNo <- unlist(f.n)
        traj.n$trajLength <- unlist(l.n)
        traj.df <- rbind(traj.df, traj.n)
    }
    traj.df <- traj.df[order(traj.df$trajLength, decreasing = TRUE), ]
    new.trajSeq <- sort(table(traj.df$trajNo), decreasing = TRUE)
    new.trajNo <- c()
    for (i in c(seq_along(new.trajSeq))) {
        f.n <- rep(i, new.trajSeq[[i]])
        new.trajNo <- append(new.trajNo, f.n)
    }
    traj.df$new.trajNo <- unlist(new.trajNo)
    traj.df <- transform(traj.df, new.trajNo = factor(new.trajNo))
    
    dwellTimes <- c()
    for (i in c(seq_along(trackll[[1]]))) {
        dwellTimes = append(dwellTimes, paste(new.trajSeq[[i]] * t.interval, 
            "s", sep = ""))
    }
    
    ############ Using the grid library to add text to each panel ##############
    
    pdf(paste("plotIndividualTraj-", names(trackll), "-", format(Sys.time(), 
        "%Y%m%d_%H%M%S"), ".pdf", sep = ""), width = 11.7, height = 8.3)
    
    
    trajplot <- lattice::xyplot(traj.df$y ~ traj.df$x | traj.df$new.trajNo, 
        type = "l", col = "red", xlab = "x displacement (nm)", 
        ylab = "y displacement (nm)", 
        xlim = c(1, grid.size[1]), ylim = c(1, grid.size[2]), 
            main = paste(names(trackll), 
            "   n=", length(trackll[[1]])), panel = function(x, y, ...) {
            
            lattice::panel.xyplot(x, y, ...)
            grid::grid.text(dwellTimes[lattice::panel.number()], 
                just = "right", grid::unit(0.98, "npc"), 
                grid::unit(0.9, "npc"), gp = grid::gpar(col = "blue", 
                    fontface = "bold"))
            
        }, layout = c(15, 8), as.table = TRUE)
    
    print(trajplot)
    
    dev.off()
    
    cat("The plot has been saved as a PDF in the working directory.")
}

Try the sojourner package in your browser

Any scripts or data that you put into this service are public.

sojourner documentation built on March 29, 2021, 6 p.m.