R/plot_genome_manhattan.R

Defines functions plot_genome_manhattan

Documented in plot_genome_manhattan

#' @title Mapping manhattan plot
#'
#' @description Generates a manhattan plot of association with sex from RADSex mapping results.
#'
#' @param mapping_file_path Path to a mapping results file generated by RADSex map.
#'
#' @param contig_lengths_file_path Path to a contig lengths file generated by RADSex map.
#'
#' @param chromosomes_names_file_path Path to a chromosomes names file, i.e. a tabulated file with name in the reference genome file as the first column and
#' corresponding chromosome name as the second column. If the chromosomes names in the reference genome file start with "LG", "NC", or "CHR" (case unsensitive),
#' chromosomes can be detected automatically (default NULL).
#'
#' @param plot.unplaced If TRUE, unplaced contigs will be plotted as a supercontig (default TRUE).
#'
#' @param output_file_path Path to the plot output file. If the output file is not specified, the circular plot will be plotted in the default device (default NULL).
#'
#' @param width Width of the output file in inches (default 14).
#'
#' @param height Height of the output file in inches (default 7).
#'
#' @param dpi Resolution of the output file (default 300).
#'
#' @param point.size Size of a point in the plot (default 0.5)
#'
#' @param point.palette Color palette for the dots (default c("dodgerblue3", "darkgoldenrod2"))
#'
#' @param background.palette Color palette for the background (default c("grey85", "grey100"))
#'
#' @param signif.threshold Significance threshold for association with sex (default 0.05).
#'
#' @param significance.line.color Color for significance line, set to NULL for no line (default "black").
#'
#' @param significance.line.type Linetype for the significance line, as usually defined in R (default 2).
#'
#' @param significance.text.position X and Y axis offset for the significance text, as fractions of total axis length (default c(0.05, 0.05)).
#'
#' @examples
#' plot_genome_manhattan("mapping_results.tsv", "contig_lengths.tsv", chromosomes_names_file_path = "chromosomes_names.tsv",
#'                       output_file_path = "mapping_results.png", point.size = 1,
#'                       signif.threshold = 0.01, point.palette = c("red3", "dodgerblue2"))
#'

plot_genome_manhattan <- function(mapping_file_path, contig_lengths_file_path, chromosomes_names_file_path = NULL, plot.unplaced = TRUE,
                                  output_file_path = NULL, width = 14, height = 7, dpi = 300,
                                  point.size = 0.5, signif.threshold = 0.05,
                                  point.palette = c("dodgerblue3", "darkgoldenrod2"), background.palette = c("grey85", "grey100"),
                                  significance.line.color = "black", significance.line.type = 2, significance.text.position = c(0.05, 0.05)) {

    # Check that all files exist and can be opened
    if (!file.exists(mapping_file_path)) {
        stop(paste0("The specified input file (", mapping_file_path, ") does not exist."))
    }

    if (!file.exists(contig_lengths_file_path)) {
        stop(paste0("The specified contig lengths file (", contig_lengths_file_path, ") does not exist."))
    }

    if (!is.null(chromosomes_names_file_path)) {
        if (!file.exists(chromosomes_names_file_path)) {
            stop(paste0("The specified chromosomes names file (", chromosomes_names_file_path, ") does not exist."))
        }
    }

    # Load input files
    mapping_data <- load_mapping_results(mapping_file_path, contig_lengths_file_path,
                                         contig_names_file_path = chromosomes_names_file_path, plot.unplaced = plot.unplaced)

    # Generate plot
    manhattan_plot <- mapping_manhattan_plot(mapping_data,
                                             point.size = point.size, signif.threshold = signif.threshold,
                                             point.palette = point.palette,
                                             background.palette = background.palette,
                                             significance.line.color = significance.line.color,
                                             significance.line.type = significance.line.type,
                                             significance.text.position = significance.text.position)

    # Save plot to output file if specified
    if (!is.null(output_file_path)) {
        cowplot::ggsave(output_file_path, plot = manhattan_plot, width = width, height = height, dpi = dpi)
    } else {
        print(manhattan_plot)
    }
}
RomainFeron/radsex-vis documentation built on Feb. 28, 2019, 7:42 p.m.