R/plot.swarmset.R

#' Render a logo plot of concatamer forms of clones in working_swarm
#'
#' @param x A swarmset object populated with working_swarm.
#' @param sort_stacks If true, reorder sites from left to right.
#' @param stacks_per_line If NULL, this is set to the number of selected sites; otherwise, it limits plot width.
#' @param dotify If true, one amino-acid state per site will be left blank to indicate frequency of the TF form.
#' @param color_scheme Defines how to color the amino acids in each site.  Default is 'charge' but could also be 'monochrome', 'classic', 'hydrophobicity', or 'chemistry').
#' @param ... can include "aspect_ratio" to adjust Aspect ratio (width to height) of image and "format" to specify the output image format (default is 'png_print' (600dpi resolution) but can also be 'png' (96dpi), 'pdf', 'svg', or 'jpeg').
#'
#' @return An explicit path to the file generated, located in a directory removed at the end of the R session.  NB: You will need to copy this file during run time or else lose it when the R session ends.
#'
#' @family swarmset methods
#' @export
plot.swarmset <- function(x, sort_stacks=F, stacks_per_line=NULL, dotify=F,
                          color_scheme='charge', ...) {

    dots <- list(...)

    format = "png_print"
    if (!is.null(dots$format))
        format = dots$format

    aspect_ratio = 3
    if (!is.null(dots$aspect_ratio))
        aspect_ratio = dots$aspect_ratio

### TO DO: parse and match whatever is in ...
    if (sort_stacks) {

        site.order <- order(x$selected_sites$aln)
        x$selected_sites = x$selected_sites[site.order, ]

        ## need to restring the concatamers
        x$aln_concatamer <- x$aln_concatamer[, site.order]
#        colnames(x$aln_concatamer) =  colnames(x$aln_concatamer)[site.order]

        x$working_swarm$seq_concatamer <- sapply(1:nrow(x$aln_concatamer), function(i)
          seqinr::c2s(x$aln_concatamer[i,]) )
        names(x$working_swarm$seq_concatamer) <- rownames(x$aln_concatamer)

        x$working_swarm$dot_concatamer <- dotify.matrix(x$aln_concatamer,
            x$aln_concatamer[x$tf_index, ], as_logical=F)
        names(x$working_swarm$dot_concatamer) <- rownames(x$aln_concatamer)

        x$working_swarm$dotseq_concatamer <- sapply(1:nrow(x$working_swarm$dot_concatamer), 
	    function(i) seqinr::c2s(x$working_swarm$dot_concatamer[i,]) )
        names(x$working_swarm$dotseq_concatamer) <- rownames(x$working_swarm$dot_concatamer)
    }

    # provide a safe fall-back option
    if (is.null(color_scheme) | !color_scheme %in% c('monochrome', 'charge', 'classic', 'hydrophobicity', 'chemistry'))
        color_scheme = 'monochrome'

    color_option = paste(" -c", color_scheme)

    outfile <- make.logoplot(x$selected_sites,
	    x$working_swarm,
	    which(x$working_swarm$is_included),
	    paste0("swarmset-n", length(which(x$working_swarm$is_included))),
	    stacks_per_line = stacks_per_line,
	    dotify=dotify, aspect_ratio=aspect_ratio,
	    logo_format=format,
	    color_option=color_option)

#    if (format=="png") {
#        require(png, quietly=T)
#	logo.png <- readPNG(outfile, native=T, info=T)
#	currdim <- dim(logo.png)
#	par(oma=c(0,0,0,0), mar=c(0,0,0,0))
#	plot(0, 0, type='n', xlab='', ylab='',
#	    xlim=c(1/2, currdim[2]+1/2), ylim=c(1/2, currdim[1]+1/2),
#	    xaxs='i', yaxs='i', xaxt='n', yaxt='n', frame.plot=F)
#	rasterImage(logo.png, 1, 1, currdim[2], currdim[1], interpolate=F)
#    } else {
#        cat(paste("opening", outfile, "\n"))
#        system(paste("open", outfile))
#    }

     outfile
}
phraber/lassie documentation built on May 25, 2019, 6:01 a.m.