Skip to content

Commit

Permalink
changes to fix gl.play.history
Browse files Browse the repository at this point in the history
  • Loading branch information
mijangos81 committed Jul 19, 2021
1 parent 68b1e7b commit c3959b4
Show file tree
Hide file tree
Showing 24 changed files with 389 additions and 219 deletions.
19 changes: 12 additions & 7 deletions R/gl.blast.r
Original file line number Diff line number Diff line change
Expand Up @@ -292,16 +292,21 @@ gl.blast <- function(x, ref_genome, task = "megablast",
cat(report(paste(nrow(one_hit), " sequences were aligned after filtering")))
}

match_call <- paste0(names(match.call()),"_",as.character(match.call()),collapse = "_")

# creating file names
temp_blast_unfiltered <- tempfile(pattern =paste0("dartR_blast_unfiltered",paste0(names(match.call()),"_",as.character(match.call()),collapse = "_")))
temp_blast_filtered <- tempfile(pattern =paste0("dartR_blast_filtered",paste0(names(match.call()),"_",as.character(match.call()),collapse = "_")))

temp_one_hit <- tempfile(pattern =paste0("dartR_one_hit",paste0(names(match.call()),"_",as.character(match.call()),collapse = "_")))
temp_blast_unfiltered <- tempfile(pattern ="dartR_blast_unfiltered_")
temp_blast_filtered <- tempfile(pattern ="dartR_blast_filtered_")
temp_one_hit <- tempfile(pattern ="dartR_one_hit_")

# saving to tempdir
saveRDS(blast_res_unfiltered, file = temp_blast_unfiltered)
saveRDS(blast_res_filtered, file = temp_blast_filtered)
saveRDS(one_hit, file = temp_one_hit)
saveRDS(list(match_call,blast_res_unfiltered), file = temp_blast_unfiltered)
saveRDS(list(match_call,blast_res_filtered), file = temp_blast_filtered)
saveRDS(list(match_call,one_hit), file = temp_one_hit)

if(verbose>=2){
cat(report(" NOTE: Retrieve output files from tempdir using gl.list.reports() and gl.print.reports()\n"))
}

# ADD TO HISTORY
if (class(x)[1] == "genlight") {
Expand Down
34 changes: 8 additions & 26 deletions R/gl.filter.callrate.r
Original file line number Diff line number Diff line change
Expand Up @@ -425,52 +425,34 @@
}
}
}

# PRINTING OUTPUTS
# using package patchwork
p3 <- (p1/p2)
print(p3)

# SAVE INTERMEDIATES TO TEMPDIR
# creating temp file names
temp_plot <- tempfile(pattern =paste0("dartR_plot",paste0(names(match.call()),"_",as.character(match.call()),collapse = "_"),"_"))

# saving to tempdir
saveRDS(p3, file = temp_plot)
if(verbose>=2){
cat(report(" Saving the plot in ggplot format to the tempfile as",temp_plot,"using saveRDS\n"))
}

if(verbose>=2){
cat(report(" NOTE: Retrieve output files from tempdir using gl.list.reports() and gl.print.reports()\n"))
}

# Recalculate Call Rate to be safe
x <- utils.recalc.callrate(x,verbose=0)

# SAVE INTERMEDIATES TO TEMPDIR
# SAVE INTERMEDIATES TO TEMPDIR
# creating temp file names
temp_plot <- tempfile(pattern =paste0("dartR_plot",paste0(names(match.call()),"_",as.character(match.call()),collapse = "_"),"_"))
# temp_table <- tempfile(pattern = paste0("dartR_table",paste0(names(match.call()),"_",as.character(match.call()),collapse = "_"),"_"))

temp_plot <- tempfile(pattern = "dartR_plot_")
match_call <- paste0(names(match.call()),"_",as.character(match.call()),collapse = "_")
# saving to tempdir
saveRDS(p3, file = temp_plot)
saveRDS(list(match_call,p3), file = temp_plot)
if(verbose>=2){
cat(report(" Saving the ggplot to session tempfile\n"))
}

if(verbose>=2){
cat(report(" NOTE: Retrieve output files from tempdir using gl.list.reports() and gl.print.reports()\n"))
}

# ADD TO HISTORY

nh <- length(x2@other$history)
x2@other$history[[nh + 1]] <- match.call()


# FLAG SCRIPT END

if (verbose > 0) {
cat(report("\nCompleted:",funname,"\n"))
}


invisible(x2)
}
19 changes: 11 additions & 8 deletions R/gl.filter.parent.offspring.r
Original file line number Diff line number Diff line change
Expand Up @@ -291,24 +291,27 @@ gl.filter.parent.offspring <- function(x,
# using package patchwork
p3 <- (p1/p2) + plot_layout(heights = c(1, 4))
print(p3)
df <- outliers

# SAVE INTERMEDIATES TO TEMPDIR
# creating temp file names
temp_plot <- tempfile(pattern = paste0("dartR_plot",paste0(names(match.call()),"_",as.character(match.call()),collapse = "_"),"_"))
temp_table <- tempfile(pattern = paste0("dartR_table",paste0(names(match.call()),"_",as.character(match.call()),collapse = "_"),"_"))

temp_plot <- tempfile(pattern = "dartR_plot_")
temp_table <- tempfile(pattern = "dartR_table_")
match_call <- paste0(names(match.call()),"_",as.character(match.call()),collapse = "_")
# saving to tempdir
saveRDS(p3, file = temp_plot)
saveRDS(list(match_call,p3), file = temp_plot)
if(verbose>=2){
cat(report(" Saving the plot in ggplot format to the tempfile as",temp_plot,"using saveRDS\n"))
cat(report(" Saving the ggplot to session tempfile\n"))
}
saveRDS(outliers, file = temp_table)

saveRDS(list(match_call,df), file = temp_table)
if(verbose>=2){
cat(report(" Saving the report to the tempfile as",temp_table,"using saveRDS\n"))
cat(report(" Saving tabulation to session tempfile\n"))
}

if(verbose>=2){
cat(report(" NOTE: Retrieve output files from tempdir using gl.list.reports() and gl.print.reports()\n"))
}
}

# ADD ACTION TO HISTORY

Expand Down
20 changes: 9 additions & 11 deletions R/gl.grm.network.r
Original file line number Diff line number Diff line change
Expand Up @@ -214,19 +214,17 @@ gl.grm.network <- function(G,

# SAVE INTERMEDIATES TO TEMPDIR
# creating temp file names
temp_plot <- tempfile(pattern = paste0("dartR_plot",paste0(names(match.call()),"_",as.character(match.call()),collapse = "_"),"_"))

temp_plot <- tempfile(pattern = "dartR_plot_")
match_call <- paste0(names(match.call()),"_",as.character(match.call()),collapse = "_")
# saving to tempdir
saveRDS(p1, file = temp_plot)
if(verbose>=2){
cat(report(" Saving the ggplot to session tempfile\n"))
}

# FLAG SCRIPT END

if (verbose >= 1) {
cat(report("\nCompleted:", funname, "\n\n"))
saveRDS(list(match_call,p1), file = temp_plot)
if(verbose>=2){
cat(report(" Saving the ggplot to session tempfile\n"))
}

if(verbose>=2){
cat(report(" NOTE: Retrieve output files from tempdir using gl.list.reports() and gl.print.reports()\n"))
}

# RETURN

Expand Down
156 changes: 119 additions & 37 deletions R/gl.hwe.pop.r
Original file line number Diff line number Diff line change
@@ -1,71 +1,153 @@
#' Function to run Hardy-Weinberg tests over each loci and population
#' @name gl.hwe.pop
#' @title Hardy-Weinberg tests over loci and populations
#' @description
#' Hardy-Weinberg tests are performed for each loci in each of the populations
#' as defined by the pop slot in a genlight object.
#'
#' Does a Hardy-Weinberg-Test for each loci in each of the populations as defined by the pop slot in a genlight object. The function is completely rewritten as the previous function was based on package SNPassoc, which no longer is supported by CRAN. It now employs the \code{HardyWeinberg} package, which needs to be installed. The function that is used is \code{\link[HardyWeinberg]{HWExactStats}}, but there are several other great function implemented in the package regarding HWE. Therefore you can return the data in the format the HWE package expects, via \code{HWformat=TRUE} and then use this to run other functions of the package.
#' @param x a genlight object with a population defined [pop(x) does not return NULL]
#' @param pvalue the p-value for the HWE test.
#' @param plot a switch if a barplot is returned.
#' @param HWformat switch if data should be returned in HWformat (counts of Genotypes to be used in package \code{HardyWeinberg})
#' @return This functions performs a HWE test for every population (rows) and loci (columns) and returns a true false matrix. True is reported if the p-value of an HWE-test for a particular loci and population was below the specified threshold (pvalue, default=0.05). The thinking behind this approach is that loci that are not in HWE in several populations have most likely to be treated (e.g. filtered if loci under selection are of interest). If plot=TRUE a barplot on the on the loci and the sum of deviation over all population is returned. Loci that deviate in the majority of populations can be identified via colSums on the resulting matrix. The function returns a list with up to three components: 'HWE' is the matrix over loci and populations, 'plot' is a matrixplot (ggplot) which shows the significant results for population and loci (can be amended further using ggplot syntax), and finally if 'HWEformat=TRUE' the 'HWformat' entails SNP data for each population in 'HardyWeinberg'-format to be used with other functions of the package (e.g \code{\link[HardyWeinberg]{HWPerm}} or \code{\link[HardyWeinberg]{HWExactPrevious}}).
#' @export
#' @examples
#' @param plot.out If TRUE, returns a plot object compatible with ggplot,
#' otherwise returns a dataframe [default TRUE].
#' @param plot_theme User specified theme [default theme_dartR()].
#' @param plot_colours Vector with two colour names for the borders and fill [default two_colors].
#' [default discrete_palette].
#' @param verbose Verbosity: 0, silent or fatal errors; 1, begin and end; 2,
#' progress log ; 3, progress and results summary; 5, full report
#' [default NULL, unless specified using gl.set.verbosity].
#'
#' @details
#' This function employs the \code{HardyWeinberg} package, which needs to be
#' installed. The function that is used is \code{\link[HardyWeinberg]{HWExactStats}},
#' but there are several other great functions implemented in the package regarding HWE.
#' Therefore, this function can return the data in the format expected by the HWE
#' package expects, via \code{HWformat=TRUE} and then use this to run other
#' functions of the package.
#' This functions performs a HWE test for every population (rows) and loci
#' (columns) and returns a true false matrix. True is reported if the p-value of
#' an HWE-test for a particular loci and population was below the specified
#' threshold (pvalue, default=0.05). The thinking behind this approach is that
#' loci that are not in HWE in several populations have most likely to be treated
#' (e.g. filtered if loci under selection are of interest). If plot=TRUE a barplot
#' on the loci and the sum of deviation over all population is returned.
#' Loci that deviate in the majority of populations can be identified via colSums
#' on the resulting matrix.
#'
#' Plot themes can be obtained from \itemize{
#' \item \url{https://ggplot2.tidyverse.org/reference/ggtheme.html} and \item
#' \url{https://yutannihilation.github.io/allYourFigureAreBelongToUs/ggthemes/}
#' }
#'
#' Resultant ggplots and the tabulation are saved to the session's temporary directory.
#'
#' @return The function returns a list with up to three components:
#' \itemize{
#' \item 'HWE' is the matrix over loci and populations
#' \item 'plot' is a matrixplot (ggplot) which shows the significant results
#' for population and loci (can be amended further using ggplot syntax)
#' \item 'HWEformat=TRUE' the 'HWformat' entails SNP data for each population
#' in 'HardyWeinberg'-format to be used with other functions of the package
#' (e.g \code{\link[HardyWeinberg]{HWPerm}} or \code{\link[HardyWeinberg]{HWExactPrevious}}).
#' }
#' @author Arthur Georges -- Post to \url{https://groups.google.com/d/forum/dartr}
#' @examples
#' out <- gl.hwe.pop(bandicoot.gl[,1:33], pvalue=0.05, plot=TRUE, HWformat=FALSE)
#'
#' @export
#'


#########
### HWEperpop
########
gl.hwe.pop <- function(x, pvalue=0.05, plot=TRUE, HWformat=FALSE)
{
# CHECK IF PACKAGES ARE INSTALLED
gl.hwe.pop <- function(x,
pvalue = 0.05,
plot.out = TRUE,
plot_theme = theme_dartR(),
plot_colours = c("gray90","deeppink"),
HWformat = FALSE,
verbose = NULL) {

# SET VERBOSITY
verbose <- gl.check.verbosity(verbose)

# FLAG SCRIPT START
funname <- match.call()[[1]]
utils.flag.start(f=funname,build="Jackson",v=verbose)

# CHECK DATATYPE
datatype <- utils.check.datatype(x)

# FUNCTION SPECIFIC ERROR CHECKING
# check if packages is installed
pkg <- "HardyWeinberg"
if (!(requireNamespace(pkg, quietly = TRUE))) {
stop("Package",pkg," needed for this function to work. Please install it.") }
stop(error("Package",pkg," needed for this function to work. Please install it."))
}

if (!is(x, "genlight")) stop("Provided object is not a genlight object! Please provide a valid input format.")
# Set a population if none is specified (such as if the genlight object has been generated manually)
if (is.null(pop(x)) | is.na(length(pop(x))) | length(pop(x)) <= 0) {
if (verbose >= 2){
cat(important(" Population assignments not detected, individuals assigned to a single population labelled 'pop1'\n"))
}
pop(x) <- array("pop1",dim = nLoc(x))
pop(x) <- as.factor(pop(x))
}

if (is.null(pop(x))) stop("No population definintion is provided, therefore function terminates. If you want to have a test for a single population set population to a single population via: \npop(gl) <- rep(\"A\", nInd(gl))")
# DO THE JOB

pops <- adegenet::seppop(x)


out <- lapply(pops, function(x) {
pp <- as.matrix(x)
xx <- data.frame(AA=colSums(pp==0, na.rm=T), AB=colSums(pp==1, na.rm=T), BB=colSums(pp==2, na.rm=T))
HardyWeinberg::HWExactStats(xx)<pvalue
} )
}
)

#convert to matrix
output <- matrix(unlist(out), ncol = nLoc(x), byrow = TRUE)
rownames(output) <- names(pops)
colnames(output) <- locNames(x)

ggp <- NA
loci <- counts <- NA
if (plot) {
p1 <- NULL

if (plot.out) {
Var1 <- Var2 <- value <- NA
longData<-reshape2::melt((as.matrix(output))*1)
longData <- reshape2::melt((as.matrix(output))*1)

ggp <- ggplot(longData, aes(x = Var2, y = Var1,)) +
geom_raster(aes(fill=c("lightgrey", "red")[value+1])) +
p1 <- ggplot(longData, aes(x = Var2, y = Var1,)) +
geom_raster(aes(fill=plot_colours[value+1])) +
scale_fill_identity() +
labs(x="loci", y="population", title="HWE over population and loci") +
theme_bw() + theme(axis.text.x=element_text(size=5, angle=90, vjust=0.3),
axis.text.y=element_text(size=9),
plot.title=element_text(size=11))
labs(x="Loci", y="Populations", title="HWE over populations and loci") +
plot_theme +
# theme(axis.text.x=element_text(size=5, angle=90, vjust=0.3)) +
theme(axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank()
)

print(ggp)
# PRINTING OUTPUTS
print(p1)
}
out=NULL
if (HWformat)
{

out <- NULL

if (HWformat){
out <- lapply(pops, function(x) {
pp <- as.matrix(x)
xx <- data.frame(AA=colSums(pp==0, na.rm=T), AB=colSums(pp==1, na.rm=T), BB=colSums(pp==2, na.rm=T))
rownames(xx) <- locNames(x)
xx })


xx }
)
}

return(list(HWE=output, plot=ggp,HWformat=out))

# FLAG SCRIPT END

if (verbose >= 1) {
cat(report("\nCompleted:", funname, "\n\n"))
}

# RETURN
invisible(list(HWE = output, plot = p1, HWformat = out))

}
3 changes: 0 additions & 3 deletions R/gl.ibd.r
Original file line number Diff line number Diff line change
Expand Up @@ -263,11 +263,8 @@ nmc <- gsub("/", "_over_",names(match.call()))
nmc <- gsub("\\*", "x",nmc)

temp_plot <- tempfile(pattern =paste0("dartR_plot",paste0(nmc,"_",mc,collapse = "_")))


temp_table <- tempfile(pattern = paste0("dartR_table",paste0(nmc,"_",mc,collapse = "_"),"_"))


# saving to tempdir
saveRDS(p1, file = temp_plot)
if(verbose>=2){cat(report(" Saving the plot in ggplot format to the tempfile as",temp_plot,"using saveRDS\n"))}
Expand Down
12 changes: 8 additions & 4 deletions R/gl.list.reports.r
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,19 @@ gl.list.reports <- function(){
} else {

dd <- data.frame(nr=1:nh, reports=files_tempdir)

#max width
dd$reports = sapply(lapply(dd$reports, strwrap, width=80), paste, collapse="\n")
dd$function_call <- NA
for (i in 1:length(files_tempdir)) {
dd$function_call[i] <- readRDS(paste0(tempdir(),"/", files_tempdir[i]))[[1]]
}

dd$function_call <- strwrap( dd$function_call ,width =80)
colnames(dd) <- c("Report number","Report","Function call")

#set table theme
tt <- ttheme_default()
tt$rowhead$fg_params$x=0
tt$core$fg_params$fontsize=11
tt$core$fg_params$hjust=0
# tt$core$fg_params$hjust=1
tt$core$fg_params$x=c(rep(0.5, nh),0.2, rep(0.01, nh+1))
tt$core$fg_params$fontfamily="mono"
tt$core$fg_params$fontface="bold"
Expand Down
Loading

0 comments on commit c3959b4

Please sign in to comment.