Skip to content

Commit

Permalink
snpgdsLDpruning: show a progress bar
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengxwen committed Feb 14, 2024
1 parent 346c5d9 commit 7a6be82
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 14 deletions.
5 changes: 5 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ UTILITIES

o fix the compiler warning: -Wformat-security

o set 'maf=0.005' & 'missing.rate=0.05' by default in `snpgdsLDpruning()`
(for WGS data)

o show a progress bar in `snpgdsLDpruning()` when 'verbose=TRUE'


CHANGES IN VERSION 1.34.0
-------------------------
Expand Down
10 changes: 5 additions & 5 deletions R/LD.R
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ snpgdsLDMat <- function(gdsobj, sample.id=NULL, snp.id=NULL, slide=250L,
#

snpgdsLDpruning <- function(gdsobj, sample.id=NULL, snp.id=NULL,
autosome.only=TRUE, remove.monosnp=TRUE, maf=NaN, missing.rate=NaN,
method=c("composite", "r", "dprime", "corr"),
slide.max.bp=500000L, slide.max.n=NA, ld.threshold=0.2,
start.pos=c("random", "first", "last"), num.thread=1L, verbose=TRUE)
autosome.only=TRUE, remove.monosnp=TRUE, maf=0.005, missing.rate=0.05,
method=c("composite", "r", "dprime", "corr"), slide.max.bp=500000L,
slide.max.n=NA, ld.threshold=0.2, start.pos=c("random", "first", "last"),
num.thread=1L, verbose=TRUE)
{
# check
ws <- .InitFile2(
Expand Down Expand Up @@ -199,7 +199,7 @@ snpgdsLDpruning <- function(gdsobj, sample.id=NULL, snp.id=NULL,
first = 1L,
last = n.tmp)
rv <- .Call(gnrLDpruning, startidx, position[flag],
slide.max.bp, slide.max.n, ld.threshold, method)
slide.max.bp, slide.max.n, ld.threshold, method, verbose)

# output
L <- rep(FALSE, length(total.snp.ids))
Expand Down
2 changes: 1 addition & 1 deletion man/snpgdsLDpruning.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
}
\usage{
snpgdsLDpruning(gdsobj, sample.id=NULL, snp.id=NULL, autosome.only=TRUE,
remove.monosnp=TRUE, maf=NaN, missing.rate=NaN,
remove.monosnp=TRUE, maf=0.005, missing.rate=0.05,
method=c("composite", "r", "dprime", "corr"), slide.max.bp=500000L,
slide.max.n=NA, ld.threshold=0.2, start.pos=c("random", "first", "last"),
num.thread=1L, verbose=TRUE)
Expand Down
15 changes: 14 additions & 1 deletion src/dGenGWAS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1544,7 +1544,6 @@ CdProgression::CdProgression(int type, bool show)
{
fShow = show;
Init(0, false);

switch (fType = type)
{
case 0:
Expand All @@ -1553,6 +1552,9 @@ CdProgression::CdProgression(int type, bool show)
case 1:
TimeInterval = 5 * CLOCKS_PER_SEC;
break;
case 2:
TimeInterval = 15 * CLOCKS_PER_SEC;
break;
}
}

Expand All @@ -1571,6 +1573,7 @@ void CdProgression::Init(C_Int64 TotalCnt, bool ShowInit)
fTotal = TotalCnt;
fCurrent = fPercent = 0;
OldTime = clock();
current_bar = 0;
if (ShowInit) ShowProgress();
}

Expand Down Expand Up @@ -1618,6 +1621,16 @@ void CdProgression::ShowProgress()
// fflush(stdout);
break;
}
case 2:
{
int n = (int)round(fPercent * 0.20);
if (n > current_bar)
{
string s(n - current_bar, '=');
Rprintf("%s", s.c_str());
current_bar = n;
}
}
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/dGenGWAS.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// dGenGWAS.h: Workspace of Genome-Wide Association Studies
//
// Copyright (C) 2011-2017 Xiuwen Zheng
// Copyright (C) 2011-2024 Xiuwen Zheng
//
// This file is part of SNPRelate.
//
Expand Down Expand Up @@ -414,6 +414,8 @@ namespace GWAS
int fPercent;
bool fShow;
clock_t TimeInterval, OldTime;
private:
int current_bar; // used when type=2
};


Expand Down
23 changes: 17 additions & 6 deletions src/genLD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// genLD.cpp: Linkage Disequilibrium (LD) Analysis on GWAS
//
// Copyright (C) 2011-2018 Xiuwen Zheng
// Copyright (C) 2011-2024 Xiuwen Zheng
//
// This file is part of SNPRelate.
//
Expand Down Expand Up @@ -753,7 +753,7 @@ namespace LD

COREARRAY_DLL_LOCAL void Perform_LD_Pruning(int StartIdx, int *pos_bp,
int slide_max_bp, int slide_max_n, const double LD_threshold,
C_BOOL *out_SNP)
C_BOOL *out_SNP, bool verbose)
{
// initial variables
nPackedSamp = (MCWorkingGeno.Space().SampleNum() % 4 > 0) ?
Expand All @@ -769,12 +769,16 @@ namespace LD
CdBufSpace BufSNP(MCWorkingGeno.Space(), true, CdBufSpace::acInc);
ListGeno.push_back(TSNP(StartIdx, pos_bp[StartIdx], nPackedSamp));
BufSNP.ReadPackedGeno(StartIdx, &(ListGeno.back().genobuf[0]));
CdProgression progress(2, verbose);
if (verbose) Rprintf("|");

// for-loop, increasing
progress.Init(MCWorkingGeno.Space().SNPNum()-StartIdx-1, verbose);
for (int i=StartIdx+1; i < MCWorkingGeno.Space().SNPNum(); i++)
{
// load genotypes
BufSNP.ReadPackedGeno(i, &buf[0]);
progress.Forward(1, verbose);
// detect LD
int TotalCnt = 0, ValidCnt = 0;
list<TSNP>::iterator it;
Expand Down Expand Up @@ -807,12 +811,13 @@ namespace LD
// -----------------------------------------------------
// decreasing searching: i --> i-1

if (verbose) Rprintf("|");
ListGeno.clear();
for (int i=StartIdx; i < MCWorkingGeno.Space().SNPNum(); i++)
{
if (out_SNP[i])
{
// check whether it is in the sliding window
// check whether it is in the sliding window of StartIdx
if ((abs(i - StartIdx) <= slide_max_n) &&
(abs(pos_bp[i] - pos_bp[StartIdx]) <= slide_max_bp))
{
Expand All @@ -824,11 +829,13 @@ namespace LD
}

BufSNP.SetAccessFlag(CdBufSpace::acDec);
// for-loop, descreasing
progress.Init(StartIdx-1, verbose);
// for-loop, decreasing
for (int i=StartIdx-1; i >= 0; i--)
{
// load genotypes
BufSNP.ReadPackedGeno(i, &buf[0]);
progress.Forward(1, verbose);
// detect LD
int TotalCnt = 0, ValidCnt = 0;
list<TSNP>::iterator it;
Expand Down Expand Up @@ -857,6 +864,8 @@ namespace LD
memmove(&(ListGeno.front().genobuf[0]), &buf[0], nPackedSamp);
}
}

if (verbose) Rprintf("|\n");
}
}

Expand Down Expand Up @@ -947,15 +956,17 @@ COREARRAY_DLL_EXPORT SEXP gnrLDMat(SEXP method, SEXP NumSlide, SEXP MatTrim,

/// Prune SNPs based on LD
COREARRAY_DLL_EXPORT SEXP gnrLDpruning(SEXP StartIdx, SEXP pos_bp,
SEXP slide_max_bp, SEXP slide_max_n, SEXP LD_threshold, SEXP method)
SEXP slide_max_bp, SEXP slide_max_n, SEXP LD_threshold, SEXP method,
SEXP verbose)
{
COREARRAY_TRY

vector<C_BOOL> flag(MCWorkingGeno.Space().SNPNum());
LD::LD_Method = Rf_asInteger(method);
LD::Perform_LD_Pruning(Rf_asInteger(StartIdx)-1, INTEGER(pos_bp),
Rf_asInteger(slide_max_bp), Rf_asInteger(slide_max_n),
Rf_asReal(LD_threshold), &flag[0]);
Rf_asReal(LD_threshold), &flag[0],
Rf_asLogical(verbose) == TRUE);

PROTECT(rv_ans = NEW_LOGICAL(MCWorkingGeno.Space().SNPNum()));
int *p = LOGICAL(rv_ans);
Expand Down

0 comments on commit 7a6be82

Please sign in to comment.