privefl / bigsnpr

R package for the analysis of massive SNP arrays.
https://privefl.github.io/bigsnpr/
183 stars 43 forks source link

error from : corr$add_columns(corr0, nrow(corr)) #424

Closed SoleilChenxu closed 1 year ago

SoleilChenxu commented 1 year ago

Hi Privefl, i got the error when calculating LD matrix about no function in _corr$addcolumns(corr0, nrow(corr)). I was confusing if i could do the same for chr 2 to 22 as chr1 instead of using if...else function. That is, for all chromosomes, _ld <- Matrix::colSums(corr0^2) corr <- as_SFBM(corr0, tmp)_

Thanks in advance for your anwser.

privefl commented 1 year ago

You need to create corr with corr <- as_SFBM(corr0, tmp) for the first chromosome, and then you just add more data to it for the other chromosomes.

Is this your question?

SoleilChenxu commented 1 year ago

Oh yes that makes sense. Now I understand, because I only take chromosome 21 and 22 for example because of the partition limit. Is it possible to run the LD_matrix by choromosome and in parallel? Thanks!

privefl commented 1 year ago

What do you mean exactly by "run the LD_matrix"?

SoleilChenxu commented 1 year ago

I mean the step to calculate the correlations between variants. In the tutorial this step is processed from chromosome 1 to chromosome 22 right?

for (chr in 1:22) {

  # print(chr)

  ## indices in 'df_beta'
  ind.chr <- which(df_beta$chr == chr)
  ## indices in 'G'
  ind.chr2 <- df_beta$`_NUM_ID_`[ind.chr]

  corr0 <- snp_cor(G, ind.col = ind.chr2, size = 3 / 1000,
                   infos.pos = POS2[ind.chr2], ncores = NCORES)

  if (chr == 1) {
    ld <- Matrix::colSums(corr0^2)
    corr <- as_SFBM(corr0, tmp, compact = TRUE)
  } else {
    ld <- c(ld, Matrix::colSums(corr0^2))
    corr$add_columns(corr0, nrow(corr))
  }
}

My question is whether I could calculate the correlation several times and merge the data later.

privefl commented 1 year ago

Yes, you could compute corr0 with snp_corr() for each chromosome separately (or use the ones precomputed). And then merge all those with $add_columns. It's what I actually do in most of my papers.

SoleilChenxu commented 1 year ago

Okay this helps. Thank you very much!