Skip to content

Commit

Permalink
fix MatrixExtra method calling
Browse files Browse the repository at this point in the history
  • Loading branch information
Sudaraka88 authored and Sudaraka88 committed Jul 16, 2024
1 parent a4559d3 commit 7ec2c7c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 37 deletions.
3 changes: 2 additions & 1 deletion R/LDSummaryPlot.R
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ genomewide_LDMap = function(lr_links_path, sr_links_path, plot_save_path, reduce
cat(paste("Reducing dimensionality from", length(pos_vec) ,"x", length(pos_vec), "-> "))
x = .mat(length(pos_vec), reducer)
cat(paste(ncol(x) ,"x", ncol(x), "...\n"))
reduced_mat = MatrixExtra::crossprod(x, MatrixExtra::crossprod(sprs, x))
# reduced_mat = MatrixExtra::crossprod(x, MatrixExtra::crossprod(sprs, x))
reduced_mat = crossprod(x, crossprod(sprs, x))
htm = as(reduced_mat, 'matrix')/reducer^2
nms = as.character(pos_vec[seq(from = 1, to = length(pos_vec), by = reducer-1)])
colnames(htm) = rownames(htm) = nms[1:nrow(htm)]
Expand Down
54 changes: 27 additions & 27 deletions R/computePairwiseMI.R
Original file line number Diff line number Diff line change
Expand Up @@ -229,11 +229,11 @@ perform_MI_computation_ACGTN = function(snp.dat, from, to, neff, hsq, cds_var, l
#
# }

tAf = spam::as.matrix(snp.dat$snp.matrix_A[from, ]); tAfh = MatrixExtra::tcrossprod(tAf, hsq); pAf = Matrix::rowSums(tAfh^2)
tCf = spam::as.matrix(snp.dat$snp.matrix_C[from, ]); tCfh = MatrixExtra::tcrossprod(tCf, hsq); pCf = Matrix::rowSums(tCfh^2)
tGf = spam::as.matrix(snp.dat$snp.matrix_G[from, ]); tGfh = MatrixExtra::tcrossprod(tGf, hsq); pGf = Matrix::rowSums(tGfh^2)
tTf = spam::as.matrix(snp.dat$snp.matrix_T[from, ]); tTfh = MatrixExtra::tcrossprod(tTf, hsq); pTf = Matrix::rowSums(tTfh^2)
tNf = spam::as.matrix(snp.dat$snp.matrix_N[from, ]); tNfh = MatrixExtra::tcrossprod(tNf, hsq); pNf = Matrix::rowSums(tNfh^2)
tAf = spam::as.matrix(snp.dat$snp.matrix_A[from, ]); tAfh = tcrossprod(tAf, hsq); pAf = Matrix::rowSums(tAfh^2)
tCf = spam::as.matrix(snp.dat$snp.matrix_C[from, ]); tCfh = tcrossprod(tCf, hsq); pCf = Matrix::rowSums(tCfh^2)
tGf = spam::as.matrix(snp.dat$snp.matrix_G[from, ]); tGfh = tcrossprod(tGf, hsq); pGf = Matrix::rowSums(tGfh^2)
tTf = spam::as.matrix(snp.dat$snp.matrix_T[from, ]); tTfh = tcrossprod(tTf, hsq); pTf = Matrix::rowSums(tTfh^2)
tNf = spam::as.matrix(snp.dat$snp.matrix_N[from, ]); tNfh = tcrossprod(tNf, hsq); pNf = Matrix::rowSums(tNfh^2)

if(fromISto){
tAt = tAf; tAth = tAfh; pAt = pAf
Expand All @@ -243,11 +243,11 @@ perform_MI_computation_ACGTN = function(snp.dat, from, to, neff, hsq, cds_var, l
tNt = tNf; tNth = tNfh; pNt = pNf
} else {
# to
tAt = spam::as.matrix(snp.dat$snp.matrix_A[to, ]); tAth = MatrixExtra::tcrossprod(tAt, hsq); pAt = Matrix::rowSums(tAth^2)
tCt = spam::as.matrix(snp.dat$snp.matrix_C[to, ]); tCth = MatrixExtra::tcrossprod(tCt, hsq); pCt = Matrix::rowSums(tCth^2)
tGt = spam::as.matrix(snp.dat$snp.matrix_G[to, ]); tGth = MatrixExtra::tcrossprod(tGt, hsq); pGt = Matrix::rowSums(tGth^2)
tTt = spam::as.matrix(snp.dat$snp.matrix_T[to, ]); tTth = MatrixExtra::tcrossprod(tTt, hsq); pTt = Matrix::rowSums(tTth^2)
tNt = spam::as.matrix(snp.dat$snp.matrix_N[to, ]); tNth = MatrixExtra::tcrossprod(tNt, hsq); pNt = Matrix::rowSums(tNth^2)
tAt = spam::as.matrix(snp.dat$snp.matrix_A[to, ]); tAth = tcrossprod(tAt, hsq); pAt = Matrix::rowSums(tAth^2)
tCt = spam::as.matrix(snp.dat$snp.matrix_C[to, ]); tCth = tcrossprod(tCt, hsq); pCt = Matrix::rowSums(tCth^2)
tGt = spam::as.matrix(snp.dat$snp.matrix_G[to, ]); tGth = tcrossprod(tGt, hsq); pGt = Matrix::rowSums(tGth^2)
tTt = spam::as.matrix(snp.dat$snp.matrix_T[to, ]); tTth = tcrossprod(tTt, hsq); pTt = Matrix::rowSums(tTth^2)
tNt = spam::as.matrix(snp.dat$snp.matrix_N[to, ]); tNth = tcrossprod(tNt, hsq); pNt = Matrix::rowSums(tNth^2)
# }

}
Expand All @@ -256,11 +256,11 @@ perform_MI_computation_ACGTN = function(snp.dat, from, to, neff, hsq, cds_var, l
### MATRIX MODE CODE DO NOT CHANGE BELOW
# from
# tAf = as(snp.dat$snp.matrix_A[from, ], 'unpackedMatrix'); tAfh = MatrixExtra::tcrossprod(tAf, hsq); pAf = Matrix::rowSums(tAfh^2) # crashes in linuxMint
tAf = as(snp.dat$snp.matrix_A[from, ], 'lgeMatrix'); tAfh = MatrixExtra::tcrossprod(tAf, hsq); pAf = Matrix::rowSums(tAfh^2)
tCf = as(snp.dat$snp.matrix_C[from, ], 'lgeMatrix'); tCfh = MatrixExtra::tcrossprod(tCf, hsq); pCf = Matrix::rowSums(tCfh^2)
tGf = as(snp.dat$snp.matrix_G[from, ], 'lgeMatrix'); tGfh = MatrixExtra::tcrossprod(tGf, hsq); pGf = Matrix::rowSums(tGfh^2)
tTf = as(snp.dat$snp.matrix_T[from, ], 'lgeMatrix'); tTfh = MatrixExtra::tcrossprod(tTf, hsq); pTf = Matrix::rowSums(tTfh^2)
tNf = as(snp.dat$snp.matrix_N[from, ], 'lgeMatrix'); tNfh = MatrixExtra::tcrossprod(tNf, hsq); pNf = Matrix::rowSums(tNfh^2)
tAf = as(snp.dat$snp.matrix_A[from, ], 'lgeMatrix'); tAfh = tcrossprod(tAf, hsq); pAf = Matrix::rowSums(tAfh^2)
tCf = as(snp.dat$snp.matrix_C[from, ], 'lgeMatrix'); tCfh = tcrossprod(tCf, hsq); pCf = Matrix::rowSums(tCfh^2)
tGf = as(snp.dat$snp.matrix_G[from, ], 'lgeMatrix'); tGfh = tcrossprod(tGf, hsq); pGf = Matrix::rowSums(tGfh^2)
tTf = as(snp.dat$snp.matrix_T[from, ], 'lgeMatrix'); tTfh = tcrossprod(tTf, hsq); pTf = Matrix::rowSums(tTfh^2)
tNf = as(snp.dat$snp.matrix_N[from, ], 'lgeMatrix'); tNfh = tcrossprod(tNf, hsq); pNf = Matrix::rowSums(tNfh^2)

if(fromISto){
tAt = tAf; tAth = tAfh; pAt = pAf
Expand All @@ -270,16 +270,16 @@ perform_MI_computation_ACGTN = function(snp.dat, from, to, neff, hsq, cds_var, l
tNt = tNf; tNth = tNfh; pNt = pNf
} else {
# to
tAt = as(snp.dat$snp.matrix_A[to, ], 'lgeMatrix'); tAth = MatrixExtra::tcrossprod(tAt, hsq); pAt = Matrix::rowSums(tAth^2)#+rt*0.5
tCt = as(snp.dat$snp.matrix_C[to, ], 'lgeMatrix'); tCth = MatrixExtra::tcrossprod(tCt, hsq); pCt = Matrix::rowSums(tCth^2)#+rt*0.5
tGt = as(snp.dat$snp.matrix_G[to, ], 'lgeMatrix'); tGth = MatrixExtra::tcrossprod(tGt, hsq); pGt = Matrix::rowSums(tGth^2)#+rt*0.5
tTt = as(snp.dat$snp.matrix_T[to, ], 'lgeMatrix'); tTth = MatrixExtra::tcrossprod(tTt, hsq); pTt = Matrix::rowSums(tTth^2)#+rt*0.5
tNt = as(snp.dat$snp.matrix_N[to, ], 'lgeMatrix'); tNth = MatrixExtra::tcrossprod(tNt, hsq); pNt = Matrix::rowSums(tNth^2)#+rt*0.5
tAt = as(snp.dat$snp.matrix_A[to, ], 'lgeMatrix'); tAth = tcrossprod(tAt, hsq); pAt = Matrix::rowSums(tAth^2)#+rt*0.5
tCt = as(snp.dat$snp.matrix_C[to, ], 'lgeMatrix'); tCth = tcrossprod(tCt, hsq); pCt = Matrix::rowSums(tCth^2)#+rt*0.5
tGt = as(snp.dat$snp.matrix_G[to, ], 'lgeMatrix'); tGth = tcrossprod(tGt, hsq); pGt = Matrix::rowSums(tGth^2)#+rt*0.5
tTt = as(snp.dat$snp.matrix_T[to, ], 'lgeMatrix'); tTth = tcrossprod(tTt, hsq); pTt = Matrix::rowSums(tTth^2)#+rt*0.5
tNt = as(snp.dat$snp.matrix_N[to, ], 'lgeMatrix'); tNth = tcrossprod(tNt, hsq); pNt = Matrix::rowSums(tNth^2)#+rt*0.5
}
}

den = neff + MatrixExtra::tcrossprod(snp.dat$r[from], snp.dat$r[to]) * 0.5
rft = MatrixExtra::t(MatrixExtra::tcrossprod(rf, rt))*0.25
den = neff + tcrossprod(snp.dat$r[from], snp.dat$r[to]) * 0.5
rft = t(tcrossprod(rf, rt))*0.25
rf = 0.5*rf
rt = 0.5*rt
# snp_i = 3
Expand Down Expand Up @@ -419,19 +419,19 @@ computeMI_Sprase = function(MI_t, tX, tY, pX, pY, rX, rY, RXY, uqX, uqY, den, nc
# pxy_t = spam::tcrossprod(tX, tY)
# pxy_t = spam::as.matrix(pxy_t) + 0.5# convert to dense mx (This could potentially be a very large matrix for large datasets, reconsider algorithm?)
# } else {
pxy_t = MatrixExtra::tcrossprod(tX, tY) + 0.5; pxy_t = as(pxy_t, "matrix") # convert to dense mx
pxy_t = tcrossprod(tX, tY) + 0.5; pxy_t = as(pxy_t, "matrix") # convert to dense mx
# }

# t0 = Sys.time();

uq_t = MatrixExtra::tcrossprod(uqX, uqY)
uq_t = tcrossprod(uqX, uqY)
# rX = 0.5*rX
# rY = 0.5*rY
# RXY = t(MatrixExtra::tcrossprod(rX, rY))
pXrX = MatrixExtra::tcrossprod(pX*rX, rep(1,length(pY)))
pXrX = tcrossprod(pX*rX, rep(1,length(pY)))
# pYrY = MatrixExtra::t(MatrixExtra::tcrossprod(pY*rY, rep(1,length(pY))))
pYrY = MatrixExtra::tcrossprod(rep(1,length(pX)), pY*rY) # same as above, already transposed
pxpy_tt = MatrixExtra::tcrossprod(pX, pY) # + RXY + pXrX + pYrY
pYrY = tcrossprod(rep(1,length(pX)), pY*rY) # same as above, already transposed
pxpy_tt = tcrossprod(pX, pY) # + RXY + pXrX + pYrY


# difftime(Sys.time(), t0)
Expand Down
25 changes: 16 additions & 9 deletions R/performPopulationStuctureCorrection.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,17 @@ estimate_Hamming_distance_weights = function(snp.dat, threshold = 0.1, mega_dset
return(invisible())
} else {

shared.snps.spam = spam::crossprod(snp.dat$snp.matrix_A)
shared.snps.spam = shared.snps.spam + spam::crossprod(snp.dat$snp.matrix_C)
shared.snps.spam = shared.snps.spam + spam::crossprod(snp.dat$snp.matrix_G)
shared.snps.spam = shared.snps.spam + spam::crossprod(snp.dat$snp.matrix_T)
shared.snps.spam = shared.snps.spam + spam::crossprod(snp.dat$snp.matrix_N)
# shared.snps.spam = spam::crossprod(snp.dat$snp.matrix_A)
# shared.snps.spam = shared.snps.spam + spam::crossprod(snp.dat$snp.matrix_C)
# shared.snps.spam = shared.snps.spam + spam::crossprod(snp.dat$snp.matrix_G)
# shared.snps.spam = shared.snps.spam + spam::crossprod(snp.dat$snp.matrix_T)
# shared.snps.spam = shared.snps.spam + spam::crossprod(snp.dat$snp.matrix_N)

shared.snps.spam = crossprod(snp.dat$snp.matrix_A)
shared.snps.spam = shared.snps.spam + crossprod(snp.dat$snp.matrix_C)
shared.snps.spam = shared.snps.spam + crossprod(snp.dat$snp.matrix_G)
shared.snps.spam = shared.snps.spam + crossprod(snp.dat$snp.matrix_T)
shared.snps.spam = shared.snps.spam + crossprod(snp.dat$snp.matrix_N)

## Is this a bug in the spam package? This should work:
# hdw.spam = 1/( spam::colSums( (snp.dat$nsnp - shared.snps.spam) < thresh) + 1)
Expand All @@ -47,19 +53,20 @@ estimate_Hamming_distance_weights = function(snp.dat, threshold = 0.1, mega_dset
}
} else {
snpmat_t = as(snp.dat$snp.matrix_A, 'dgCMatrix')
# shared.snps = MatrixExtra::crossprod((snpmat_t))
shared.snps = MatrixExtra::crossprod((snpmat_t))

snpmat_t = as(snp.dat$snp.matrix_C, 'dgCMatrix')
shared.snps = shared.snps + MatrixExtra::crossprod((snpmat_t))
shared.snps = shared.snps + crossprod((snpmat_t))

snpmat_t = as(snp.dat$snp.matrix_G, 'dgCMatrix')
shared.snps = shared.snps + MatrixExtra::crossprod((snpmat_t))
shared.snps = shared.snps + crossprod((snpmat_t))

snpmat_t = as(snp.dat$snp.matrix_T, 'dgCMatrix')
shared.snps = shared.snps + MatrixExtra::crossprod((snpmat_t))
shared.snps = shared.snps + crossprod((snpmat_t))

snpmat_t = as(snp.dat$snp.matrix_N, 'dgCMatrix')
shared.snps = shared.snps + MatrixExtra::crossprod((snpmat_t))
shared.snps = shared.snps + crossprod((snpmat_t))

hdw = 1/( Matrix::colSums( (snp.dat$nsnp - shared.snps) < thresh) + 1)
}
Expand Down

0 comments on commit 7ec2c7c

Please sign in to comment.