Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
91 commits
Select commit Hold shift + click to select a range
7c70ec5
Modified files for strain-specific residual variance option
jb621-star Mar 20, 2023
ca93fb2
Fixed 'cvt' variable and missing 0 < geno < 2 checks
jb621-star Mar 21, 2023
763a0b5
Fixed errors except for MphInitial errors
jb621-star Apr 6, 2023
18e15e3
Fixed parameter names, attempted to integrate residual variance as an…
jb621-star Jul 18, 2023
72f5a24
More bug fixes
jb621-star Jul 28, 2023
f76326b
Added PARAM 'CheckResidvar' so that if no vector is supplied, it is r…
jb621-star Jul 28, 2023
58e370f
Update gemma.cpp
jb621-star Oct 7, 2024
924daa5
Update mvlmm.cpp
jb621-star Oct 7, 2024
db72327
Update mvlmm.cpp
jb621-star Oct 7, 2024
363ec00
Update mvlmm.cpp
jb621-star Oct 8, 2024
c40699b
Update mvlmm.cpp
jb621-star Oct 8, 2024
96d5f17
Update gemma.cpp
jb621-star Oct 8, 2024
112d499
Update gemma.cpp
jb621-star Oct 8, 2024
6f1d2cd
Update gemma_io.cpp
jb621-star Oct 8, 2024
057d27a
Update gemma_io.h
jb621-star Oct 8, 2024
a8352e8
Update lmm.cpp
jb621-star Oct 8, 2024
f858b30
Update mvlmm.h
jb621-star Oct 8, 2024
587c181
Update param.cpp
jb621-star Oct 8, 2024
e85449d
Update param.h
jb621-star Oct 8, 2024
ad54957
Update param.h
jb621-star Oct 8, 2024
b0da2e4
Update param.h
jb621-star Oct 22, 2024
9580e68
Update gemma.cpp
jb621-star Oct 22, 2024
1b9c986
Update gemma_io.cpp
jb621-star Oct 22, 2024
e193600
Update gemma_io.h
jb621-star Oct 22, 2024
8409e49
Update lmm.cpp
jb621-star Oct 22, 2024
06b5f62
Update param.h
jb621-star Oct 22, 2024
be69a55
Update param.cpp
jb621-star Oct 22, 2024
2b25992
Update gemma.cpp
jb621-star Oct 22, 2024
e1d92d0
Update mvlmm.h
jb621-star Oct 22, 2024
1caa0d1
Update gemma_io.cpp
jb621-star Oct 28, 2024
eb1e113
Update lmm.cpp
jb621-star Oct 28, 2024
c18db6d
Update mvlmm.h
jb621-star Oct 28, 2024
94927da
Update gemma.cpp
jb621-star Oct 28, 2024
b2ae33e
Update gemma.cpp
jb621-star Oct 28, 2024
dfd99b3
Update lmm.cpp
jb621-star Oct 28, 2024
2d6afc2
Update mvlmm.cpp
jb621-star Oct 28, 2024
bb4db72
Update mvlmm.h
jb621-star Oct 28, 2024
d94f38b
Update gemma_io.cpp
jb621-star Oct 28, 2024
33bc7d3
Update mvlmm.cpp
jb621-star Oct 28, 2024
7347f95
Update mvlmm.cpp
jb621-star Oct 28, 2024
6c284c4
Update mvlmm.cpp
jb621-star Oct 28, 2024
ed2db02
Update mvlmm.cpp
jb621-star Oct 28, 2024
8b43a6b
Update mvlmm.cpp
jb621-star Oct 28, 2024
95dace7
Update mvlmm.cpp
jb621-star Nov 4, 2024
d397275
Update param.cpp
jb621-star Nov 4, 2024
e3768cb
Update mvlmm.cpp
jb621-star Nov 4, 2024
32f4188
Update mvlmm.cpp
jb621-star Nov 4, 2024
baff409
Update mvlmm.h
jb621-star Nov 4, 2024
ad18a8d
Update mvlmm.cpp
jb621-star Nov 4, 2024
b4a4641
Update gemma.cpp
jb621-star Nov 25, 2024
b198ae6
Update gemma.cpp
jb621-star Nov 25, 2024
56fc6c5
Update gemma.cpp
jb621-star Nov 25, 2024
f8764ae
Update param.h
jb621-star Nov 25, 2024
dd20afe
Update mvlmm.cpp
jb621-star Nov 25, 2024
c75b7d2
Update param.cpp
jb621-star Nov 25, 2024
882c9cd
Update gemma.cpp
jb621-star Nov 27, 2024
f6e9299
Update mvlmm.cpp
jb621-star Nov 27, 2024
73412d2
Update mvlmm.h
jb621-star Nov 27, 2024
4bfc619
Update param.h
jb621-star Nov 27, 2024
2516635
Update param.cpp
jb621-star Nov 27, 2024
0876bf0
Update gemma_io.h
jb621-star Dec 2, 2024
66a5a37
Update gemma_io.h
jb621-star Dec 3, 2024
b6bfb79
Update gemma_io.cpp
jb621-star Dec 3, 2024
c89c219
Update gemma_io.cpp
jb621-star Dec 3, 2024
ea678d7
Update gemma_io.cpp
jb621-star Dec 4, 2024
40b31bc
Update gemma.cpp
jb621-star Dec 23, 2024
bbb458b
Update param.cpp
jb621-star Jan 1, 2025
84cfda9
Update gemma_io.cpp
jb621-star Jan 2, 2025
65dab2e
Update gemma_io.cpp
jb621-star Jan 3, 2025
a96087b
Update mvlmm.cpp
jb621-star Apr 24, 2025
d26bec6
Update param.cpp
jb621-star Apr 24, 2025
a8c75e0
Update mvlmm.cpp
jb621-star Apr 24, 2025
1aa998f
Update param.cpp
jb621-star Apr 24, 2025
93641f1
Update param.cpp
jb621-star Apr 24, 2025
1d49d03
Update gemma_io.cpp
jb621-star Apr 24, 2025
528859d
Update mvlmm.cpp
jb621-star Apr 28, 2025
d8a1684
Update param.cpp
jb621-star Apr 28, 2025
3c93381
Update param.h
jb621-star Apr 28, 2025
79e04f1
Update mvlmm.cpp
jb621-star Apr 28, 2025
a5e6474
Update param.cpp
jb621-star Apr 28, 2025
3840627
Update param.h
jb621-star Apr 28, 2025
0839d6f
Update param.cpp
jb621-star Apr 28, 2025
45ae373
Update mvlmm.cpp
jb621-star Apr 28, 2025
48ac836
Update param.cpp
jb621-star Apr 28, 2025
d9daaba
Update param.h
jb621-star Apr 28, 2025
9e5f10f
Update param.h
jb621-star Apr 28, 2025
1945cee
Update param.cpp
jb621-star Apr 28, 2025
d08496d
Update param.cpp
jb621-star Apr 28, 2025
1592bce
Update param.cpp
jb621-star Apr 28, 2025
a1e2445
Update param.cpp
jb621-star Apr 28, 2025
c19d26c
Update param.cpp
jb621-star Apr 28, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions src/gemma.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,16 @@ void GEMMA::PrintHelp(size_t option) {
cout << " variable for individual 2" << endl;
cout << " ..." << endl;
cout << " missing value: NA" << endl;
cout << " -resid [filename] "
<< " residual variance file contains a diagonal matrix of positive values to be used "
<< "directly as for the residual variance---each diagonal element corresponds to an "
<< "individual, in which each value is the empirical residual variance (sigmasq) based"
<< "on a trait value calculated from multiple replicates for this individual"
<< endl;
cout << " format: variable for individual 1" << endl;
cout << " variable for individual 2" << endl;
cout << " ..." << endl;
cout << " missing value: NA" << endl;
cout << " -k [filename] "
<< " specify input kinship/relatedness matrix file name" << endl;
cout << " -mk [filename] "
Expand Down Expand Up @@ -824,6 +834,14 @@ void GEMMA::Assign(int argc, char **argv, PARAM &cPar) {
str.clear();
str.assign(argv[i]);
cPar.file_weight = str;
} else if (strcmp(argv[i], "-resid") == 0) {
if (argv[i + 1] == NULL || argv[i + 1][0] == '-') {
continue;
}
++i;
str.clear();
str.assign(argv[i]);
cPar.file_resid = str;
Comment thread
jb621-star marked this conversation as resolved.
} else if (strcmp(argv[i], "-wsnp") == 0) {
if (argv[i + 1] == NULL || argv[i + 1][0] == '-') {
continue;
Expand Down Expand Up @@ -1732,6 +1750,7 @@ void GEMMA::BatchRun(PARAM &cPar) {
gsl_matrix *Y = gsl_matrix_safe_alloc(cPar.ni_test, cPar.n_ph);
gsl_matrix *W = gsl_matrix_safe_alloc(Y->size1, cPar.n_cvt);
gsl_matrix *G = gsl_matrix_safe_alloc(Y->size1, Y->size1);
gsl_matrix *sigmasq = gsl_matrix_safe_alloc(cPar.n_resid, cPar.n_resid);
gsl_matrix *U = gsl_matrix_safe_alloc(Y->size1, Y->size1);
gsl_matrix *UtW = gsl_matrix_safe_alloc(Y->size1, W->size2);
gsl_matrix *UtY = gsl_matrix_safe_alloc(Y->size1, Y->size2);
Expand Down Expand Up @@ -1821,7 +1840,8 @@ void GEMMA::BatchRun(PARAM &cPar) {
gsl_matrix *se_B = gsl_matrix_safe_alloc(cPar.n_ph, W->size2);

// obtain estimates
CalcMvLmmVgVeBeta(eval, UtW, UtY, cPar.em_iter, cPar.nr_iter,
MVLMM cMvlmm;
cMvlmm.CalcMvLmmVgVeBeta(eval, U, sigmasq, UtW, UtY, cPar.em_iter, cPar.nr_iter,
cPar.em_prec, cPar.nr_prec, cPar.l_min, cPar.l_max,
cPar.n_region, Vg, Ve, B, se_B);

Expand Down Expand Up @@ -2564,6 +2584,7 @@ void GEMMA::BatchRun(PARAM &cPar) {
gsl_matrix *UtW = gsl_matrix_calloc(Y->size1, W->size2);
gsl_matrix *UtY = gsl_matrix_calloc(Y->size1, Y->size2);
gsl_vector *eval = gsl_vector_calloc(Y->size1);
gsl_matrix *sigmasq = gsl_matrix_calloc(cPar.n_resid, cPar.n_resid);
gsl_vector *env = gsl_vector_safe_alloc(Y->size1);
gsl_vector *weight = gsl_vector_safe_alloc(Y->size1);
debug_msg("Started on LMM");
Expand Down Expand Up @@ -2611,6 +2632,8 @@ void GEMMA::BatchRun(PARAM &cPar) {
}
}
}
// Need some way of reading the residual variance file and then
// assigning it to what would have been the ..+ 1 part of the computation

// eigen-decomposition and calculate trace_G - main track
cout << "Start Eigen-Decomposition..." << endl;
Expand Down Expand Up @@ -2827,15 +2850,15 @@ void GEMMA::BatchRun(PARAM &cPar) {

if (!cPar.file_bfile.empty()) {
if (cPar.file_gxe.empty()) {
cMvlmm.AnalyzePlink(U, eval, UtW, UtY);
cMvlmm.AnalyzePlink(U, eval, sigmasq, UtW, UtY);
} else {
cMvlmm.AnalyzePlinkGXE(U, eval, UtW, UtY, env);
cMvlmm.AnalyzePlinkGXE(U, eval, sigmasq, UtW, UtY, env);
}
} else {
if (cPar.file_gxe.empty()) {
cMvlmm.AnalyzeBimbam(U, eval, UtW, UtY);
cMvlmm.AnalyzeBimbam(U, eval, sigmasq, UtW, UtY);
} else {
cMvlmm.AnalyzeBimbamGXE(U, eval, UtW, UtY, env);
cMvlmm.AnalyzeBimbamGXE(U, eval, sigmasq, UtW, UtY, env);
}
}

Expand Down
141 changes: 133 additions & 8 deletions src/gemma_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <stdlib.h>
#include <string>
#include <vector>
#include <limits>

#include "gsl/gsl_blas.h"
#include "gsl/gsl_cdf.h"
Expand Down Expand Up @@ -150,7 +151,7 @@ std::istream &safeGetline(std::istream &is, std::string &t) {

// Read SNP file. A single column of SNP names.
bool ReadFile_snps(const string file_snps, set<string> &setSnps) {
debug_msg("enter ReadFile_snps");
debug_msg("entered");
setSnps.clear();

igzstream infile(file_snps.c_str(), igzstream::in);
Expand Down Expand Up @@ -329,9 +330,6 @@ bool ReadFile_anno(const string &file_anno, map<string, string> &mapRS2chr,
mapRS2bp[rs] = b_pos;
mapRS2cM[rs] = cM;
}
// for (auto& [key, value] : mapRS2bp) {
// cerr << key << endl;
//}

infile.close();
infile.clear();
Expand Down Expand Up @@ -509,6 +507,84 @@ bool ReadFile_cvt(const string &file_cvt, vector<int> &indicator_cvt,
return true;
}

bool ReadFile_resid(const std::string &file_resid, std::vector<int> &indicator_resid,
gsl_matrix *resid, size_t &n_resid) {
debug_msg("entered");
indicator_resid.clear();

std::ifstream infile(file_resid.c_str(), std::ifstream::in);
if (!infile) {
std::cout << "error! fail to open residual variance file: " << file_resid << std::endl;
return false;
}

std::string line;
char *ch_ptr;
double d;
int flag_na = 0;
size_t row = 0;

// Read and parse each line
while (!safeGetline(infile, line).eof()) {
size_t col = 0;
flag_na = 0;

ch_ptr = strtok((char *)line.c_str(), " ,\t");
while (ch_ptr != NULL) {
if (strcmp(ch_ptr, "NA") == 0) {
flag_na = 1;
d = -9; // Handle missing value
} else {
d = atof(ch_ptr);
}

// Set value in gsl_matrix
gsl_matrix_set(resid, row, col, d);
col++;

ch_ptr = strtok(NULL, " ,\t");
}

if (flag_na == 0) {
indicator_resid.push_back(1); // No missing values
} else {
indicator_resid.push_back(0); // Contains missing values
}

if (row == 0) {
n_resid = col; // Set the number of residuals per row
} else if (n_resid != col) {
std::cout << "error! number of residuals in row " << row
<< " does not match other rows." << std::endl;
return false;
}

row++;
}

if (indicator_resid.empty()) {
n_resid = 0;
}

// Debugging step: Print the residual variance matrix
std::cout << "DEBUG: n_resid = " << n_resid << std::endl;
if (resid != nullptr) {
for (size_t i = 0; i < n_resid; ++i) {
for (size_t j = 0; j < n_resid; ++j) {
std::cout << gsl_matrix_get(resid, i, j) << " ";
}
std::cout << std::endl;
}
} else {
std::cout << "ERROR: Residual variance matrix not initialized." << std::endl;
}

infile.close();
infile.clear();

return true;
}

// Read .bim file.
bool ReadFile_bim(const string &file_bim, vector<SNPINFO> &snpInfo) {
debug_msg("entered");
Expand Down Expand Up @@ -684,9 +760,10 @@ bool ReadFile_geno(const string &file_geno, const set<string> &setSnps,
double maf, geno, geno_old;
size_t n_miss;
size_t n_0, n_1, n_2;

double min_g = std::numeric_limits<float>::max();
double max_g = std::numeric_limits<float>::min();

int flag_poly;

int ni_total = indicator_idv.size();
Expand All @@ -699,9 +776,6 @@ bool ReadFile_geno(const string &file_geno, const set<string> &setSnps,
file_pos = 0;
auto count_warnings = 0;
auto infilen = file_geno.c_str();
// for (auto& [key, value] : mapRS2bp) {
// cerr << key << endl;
// }
while (!safe_get_line(infile, line).eof()) {
ch_ptr = strtok_safe2((char *)line.c_str(), " ,\t",infilen);
rs = ch_ptr;
Expand Down Expand Up @@ -1412,6 +1486,57 @@ void ReadFile_eigenD(const string &file_kd, bool &error, gsl_vector *eval) {
return;
}

//This is the second mention of this function...not sure why it's here or if we need it?
//void ReadFile_resid(const string &file_resid, bool &error, gsl_matrix *resid) {
// debug_msg("entered");
// igzstream infile(file_resid.c_str(), igzstream::in);
// if (!infile) {
// cout << "error! fail to open the residual variance file: " << file_resid << endl;
// error = true;
// return;
// }

// size_t n_row = resid->size1, n_col = resid->size2, i_row = 0, i_col = 0;

// gsl_matrix_set_zero(resid); //change this so that resid = V_e somehow

// string line;
// char *ch_ptr;
// double d;

// while (getline(infile, line)) {
// if (i_row == n_row) {
// cout << "error! number of rows in the residual variance file is larger "
// << "than expected." << endl;
// error = true;
// }

// i_col = 0;
// ch_ptr = strtok((char *)line.c_str(), " ,\t");
// while (ch_ptr != NULL) {
// if (i_col == n_col) {
// cout << "error! number of columns in the residual variance file "
// << "is larger than expected, for row = " << i_row << endl;
// error = true;
// }

// d = atof(ch_ptr);
// gsl_matrix_set(resid, i_row, i_col, d);
// i_col++;

// ch_ptr = strtok(NULL, " ,\t");
// }

// i_row++;
// }

// infile.close();
// infile.clear();

// return;
//}


// Read bimbam mean genotype file and calculate kinship matrix.
bool BimbamKin(const string file_geno, const set<string> ksnps,
vector<int> &indicator_snp, const int k_mode,
Expand Down
2 changes: 2 additions & 0 deletions src/gemma_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ void ReadFile_mk(const string &file_mk, vector<int> &indicator_idv,
gsl_matrix *G);
void ReadFile_eigenU(const string &file_u, bool &error, gsl_matrix *U);
void ReadFile_eigenD(const string &file_d, bool &error, gsl_vector *eval);
bool ReadFile_resid(const string &file_resid, vector<int> &indicator_resid,
gsl_matrix *resid, size_t &n_resid);

bool BimbamKin(const string file_geno, const set<string> ksnps,
vector<int> &indicator_snp, const int k_mode,
Expand Down
10 changes: 3 additions & 7 deletions src/lmm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@
#include "lmm.h"
#include "mathfunc.h"

#define P_YY_MIN 0.00000001

using namespace std;

void LMM::CopyFromParam(PARAM &cPar) {
Expand Down Expand Up @@ -524,8 +522,7 @@ double LogL_f(double l, void *params) {
index_yy = GetabIndex(n_cvt + 2, n_cvt + 2, n_cvt);
double P_yy = gsl_matrix_safe_get(Pab, nc_total, index_yy);

if (P_yy >= 0.0 && (P_yy < P_YY_MIN)) P_yy = P_YY_MIN; // control potential round-off

if (P_yy == 0.0) P_yy = 0.00000001; // control potential round-off
if (is_check_mode() || is_debug_mode()) {
// cerr << "P_yy is" << P_yy << endl;
assert(!is_nan(P_yy));
Expand Down Expand Up @@ -851,7 +848,7 @@ double LogRL_f(double l, void *params) {
index_ww = GetabIndex(n_cvt + 2, n_cvt + 2, n_cvt);
double P_yy = gsl_matrix_safe_get(Pab, nc_total, index_ww);
// P_yy is positive and may get zeroed printf("P_yy=%f",P_yy);
if (P_yy >= 0.0 && (P_yy < P_YY_MIN)) P_yy = P_YY_MIN; // control potential round-off
if (P_yy == 0.0) P_yy = 0.00000001; // control potential round-off

double c = 0.5 * df * (safe_log(df) - safe_log(2 * M_PI) - 1.0);
f = c - 0.5 * logdet_h - 0.5 * logdet_hiw - 0.5 * df * safe_log(P_yy);
Expand Down Expand Up @@ -2027,14 +2024,13 @@ void CalcLambda(const char func_name, FUNC_PARAM &params, const double l_min,
for (vector<double>::size_type i = 0; i < lambda_lh.size(); ++i) {
lambda_l = lambda_lh[i].first;
lambda_h = lambda_lh[i].second;
// printf("%f,%f\n",lambda_l,lambda_h);
auto handler = gsl_set_error_handler_off();
gsl_root_fsolver_set((gsl_root_fsolver*)s_f, &F, lambda_l, lambda_h);

int status = GSL_FAILURE;
uint iter = 0;
const auto max_iter = 100;

auto handler = gsl_set_error_handler_off();
do {
iter++;
status = gsl_root_fsolver_iterate((gsl_root_fsolver*)s_f);
Expand Down
Loading