removed setDT and replaced with dplyr alt in position_count_bp.R

This commit is contained in:
Tanushree Tunstall 2022-08-14 14:19:09 +01:00
parent 65d697d3a2
commit da8f8d90d4
5 changed files with 210 additions and 110 deletions

View file

@ -36,6 +36,8 @@ site_snp_count_bp <- function (plotdf
, subtitle_size = 20
, subtitle_colour = "pink")
{
plotdf = as.data.frame(plotdf)
# dim of plotdf
cat(paste0("\noriginal df dimensions:"
, "\nNo. of rows:", nrow(plotdf)
@ -45,14 +47,32 @@ site_snp_count_bp <- function (plotdf
#-------------------------------------------
# adding column: snpcount for each position
#-------------------------------------------
setDT(plotdf)[, pos_count := .N, by = .(eval(parse(text = df_colname)))]
#setDT(plotdf)[, pos_count_check := .N, by = .(eval(parse(text = df_colname)))]
# from dplyr
plotdf = plotdf %>%
dplyr::add_count(eval(parse(text = df_colname)))
class(plotdf)
plotdf = as.data.frame(plotdf)
class(plotdf)
nc_change = which(colnames(plotdf) == "n")
colnames(plotdf)[nc_change] <- "pos_count"
class(plotdf)
# if (all(plotdf$pos_count==plotdf$pos_count_check) ){
# cat("\nPASS: pos_count column created")
# plotdf = plotdf[, !colnames(plotdf)%in%c("pos_count_check")]
# }else{
# stop("\nAbort: pos count numbes mismatch from dplyr and data.table")
# }
cat("\nCumulative nssnp count\n"
, table(plotdf$pos_count))
# calculating total no. of mutations
tot_muts = sum(table(plotdf$pos_count))
# sanity check
if(tot_muts == nrow(plotdf)){
cat("\nPASS: total number of mutations match"