From da54a4064c52c13a584af34dd194d3c214d87319 Mon Sep 17 00:00:00 2001 From: Al-Murphy Date: Fri, 28 Jul 2023 10:46:01 +0200 Subject: [PATCH] bug fix: imputation ind with missing rs ids --- DESCRIPTION | 2 +- NEWS.md | 8 +++++ R/check_no_rs_snp.R | 3 +- R/compute_nsize.R | 84 +++++++++++++++++++++++---------------------- 4 files changed, 54 insertions(+), 43 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 95f3d64..6f5c71d 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: MungeSumstats Type: Package Title: Standardise summary statistics from GWAS -Version: 1.9.14 +Version: 1.9.15 Authors@R: c(person(given = "Alan", family = "Murphy", diff --git a/NEWS.md b/NEWS.md index 6a96724..ad57350 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,11 @@ +## CHANGES IN VERSION 1.9.15 + +### Bug fix +* Fix for imputation column when imputing RS ID from CHR:BP. Avoids crash and +ensures correct identification of imputed SNPs. +* Avoid running compute_nsize function when no imputation is wanted by user - +also avoids message output in this situation. + ## CHANGES IN VERSION 1.9.14 ### Bug fix diff --git a/R/check_no_rs_snp.R b/R/check_no_rs_snp.R index b0fa88c..71809f5 100644 --- a/R/check_no_rs_snp.R +++ b/R/check_no_rs_snp.R @@ -266,7 +266,8 @@ check_no_rs_snp <- function(sumstats_dt, path, ref_genome, snp_ids_are_rs_ids, miss_rs_chr_bp[, (format) := NULL] # join with full dataset # If IMPUTATION column added add it to other DT - if (imputation_ind && + if (imputation_ind && + "IMPUTATION_SNP" %in% names(sumstats_dt) && !"IMPUTATION_SNP" %in% names(miss_rs_chr_bp)) { miss_rs_chr_bp[, IMPUTATION_SNP := NA] } diff --git a/R/compute_nsize.R b/R/compute_nsize.R index 87085f8..a85a018 100644 --- a/R/compute_nsize.R +++ b/R/compute_nsize.R @@ -46,47 +46,49 @@ compute_nsize <- function(sumstats_dt, return_list=TRUE) { ## Avoid confusing BiocCheck. IMPUTATION_n <- IMPUTATION_Neff <- NULL; - #### Copy data.table #### - ## IMPORTANT!: This makes it so that any subsequent changes made to - ## the internal sumstats_dt variable (and the function output) will not - ## alter the original input data as well. - sumstats_dt <- data.table::copy(sumstats_dt) - #### Standardise the column names before continuing #### - if (standardise_headers) { - sumstats_dt <- - standardise_sumstats_column_headers_crossplatform( - sumstats_dt = sumstats_dt, - )[["sumstats_dt"]] - } - # if you want an Neff column for more than one method need to ensure - # you can tell which is which - append_method_name <- FALSE - if (is.vector(compute_n)) { - # sum makes an N column not an Neff column - if (length(compute_n[!compute_n == "sum"]) > 1) { - append_method_name <- TRUE - } - } - for (method in compute_n) { - compute_sample_size( - sumstats_dt = sumstats_dt, - method = method, - force_new = force_new, - append_method_name = append_method_name - ) - } - # if user wants information, give SNPs where Z-score calculated - ## Evaluate the conditions under which N would have been calculate by - ## compute_sample_size() - if (imputation_ind && - is.numeric(compute_n) && compute_n != 0L && - (!"N" %in% names(sumstats_dt))) { - sumstats_dt[, IMPUTATION_n := TRUE] - } - if (imputation_ind && - is.character(compute_n)) { - sumstats_dt[, IMPUTATION_Neff := TRUE] - } + if (!is.numeric(compute_n) || (is.numeric(compute_n) && compute_n != 0L)){ + #### Copy data.table #### + ## IMPORTANT!: This makes it so that any subsequent changes made to + ## the internal sumstats_dt variable (and the function output) will not + ## alter the original input data as well. + sumstats_dt <- data.table::copy(sumstats_dt) + #### Standardise the column names before continuing #### + if (standardise_headers) { + sumstats_dt <- + standardise_sumstats_column_headers_crossplatform( + sumstats_dt = sumstats_dt, + )[["sumstats_dt"]] + } + # if you want an Neff column for more than one method need to ensure + # you can tell which is which + append_method_name <- FALSE + if (is.vector(compute_n)) { + # sum makes an N column not an Neff column + if (length(compute_n[!compute_n == "sum"]) > 1) { + append_method_name <- TRUE + } + } + for (method in compute_n) { + compute_sample_size( + sumstats_dt = sumstats_dt, + method = method, + force_new = force_new, + append_method_name = append_method_name + ) + } + # if user wants information, give SNPs where Z-score calculated + ## Evaluate the conditions under which N would have been calculate by + ## compute_sample_size() + if (imputation_ind && + is.numeric(compute_n) && compute_n != 0L && + (!"N" %in% names(sumstats_dt))) { + sumstats_dt[, IMPUTATION_n := TRUE] + } + if (imputation_ind && + is.character(compute_n)) { + sumstats_dt[, IMPUTATION_Neff := TRUE] + } + } #### Return format #### if(return_list){ return(list("sumstats_dt" = sumstats_dt))