Browse code

Revert "Unsuccessful attempt at a t2 using just family level data."

This reverts commit 90efadf9666af4d935b1228247107898e55902f5.

mnodzenski authored on 09/11/2021 20:39:30
Showing 6 changed files

... ...
@@ -121,12 +121,8 @@ chrom_fitness_score <- function(case_genetic_data_in, complement_genetic_data_in
121 121
     .Call('_epistasisGAGE_chrom_fitness_score', PACKAGE = 'epistasisGAGE', case_genetic_data_in, complement_genetic_data_in, target_snps_in, ld_block_vec, weight_lookup, n_different_snps_weight, n_both_one_weight, recessive_ref_prop, recode_test_stat, epi_test, GxE, both_one_snps_in)
122 122
 }
123 123
 
124
-compute_weighted_mean_cov <- function(x, weight_vec, ld_block_vec, target_snps_in) {
125
-    .Call('_epistasisGAGE_compute_weighted_mean_cov', PACKAGE = 'epistasisGAGE', x, weight_vec, ld_block_vec, target_snps_in)
126
-}
127
-
128
-GxE_fitness_score_parents_only <- function(case_genetic_data_list, complement_genetic_data_list, target_snps, weight_lookup, ld_block_vec, n_different_snps_weight = 1L, n_both_one_weight = 0L) {
129
-    .Call('_epistasisGAGE_GxE_fitness_score_parents_only', PACKAGE = 'epistasisGAGE', case_genetic_data_list, complement_genetic_data_list, target_snps, weight_lookup, ld_block_vec, n_different_snps_weight, n_both_one_weight)
124
+GxE_fitness_score_parents_only <- function(case_genetic_data_list, complement_genetic_data_list, target_snps, weight_lookup, n_different_snps_weight = 1L, n_both_one_weight = 0L) {
125
+    .Call('_epistasisGAGE_GxE_fitness_score_parents_only', PACKAGE = 'epistasisGAGE', case_genetic_data_list, complement_genetic_data_list, target_snps, weight_lookup, n_different_snps_weight, n_both_one_weight)
130 126
 }
131 127
 
132 128
 GxE_fitness_score <- function(case_genetic_data_list, complement_genetic_data_list, target_snps, ld_block_vec, weight_lookup, exposure_levels, exposure_risk_levels, n_different_snps_weight = 2L, n_both_one_weight = 1L, recessive_ref_prop = 0.75, recode_test_stat = 1.64, epi_test = FALSE, check_risk = TRUE, use_parents = 1L) {
... ...
@@ -412,23 +412,9 @@ BEGIN_RCPP
412 412
     return rcpp_result_gen;
413 413
 END_RCPP
414 414
 }
415
-// compute_weighted_mean_cov
416
-List compute_weighted_mean_cov(arma::mat x, arma::vec weight_vec, IntegerVector ld_block_vec, IntegerVector target_snps_in);
417
-RcppExport SEXP _epistasisGAGE_compute_weighted_mean_cov(SEXP xSEXP, SEXP weight_vecSEXP, SEXP ld_block_vecSEXP, SEXP target_snps_inSEXP) {
418
-BEGIN_RCPP
419
-    Rcpp::RObject rcpp_result_gen;
420
-    Rcpp::RNGScope rcpp_rngScope_gen;
421
-    Rcpp::traits::input_parameter< arma::mat >::type x(xSEXP);
422
-    Rcpp::traits::input_parameter< arma::vec >::type weight_vec(weight_vecSEXP);
423
-    Rcpp::traits::input_parameter< IntegerVector >::type ld_block_vec(ld_block_vecSEXP);
424
-    Rcpp::traits::input_parameter< IntegerVector >::type target_snps_in(target_snps_inSEXP);
425
-    rcpp_result_gen = Rcpp::wrap(compute_weighted_mean_cov(x, weight_vec, ld_block_vec, target_snps_in));
426
-    return rcpp_result_gen;
427
-END_RCPP
428
-}
429 415
 // GxE_fitness_score_parents_only
430
-List GxE_fitness_score_parents_only(ListOf<IntegerMatrix> case_genetic_data_list, ListOf<IntegerMatrix> complement_genetic_data_list, IntegerVector target_snps, IntegerVector weight_lookup, IntegerVector ld_block_vec, int n_different_snps_weight, int n_both_one_weight);
431
-RcppExport SEXP _epistasisGAGE_GxE_fitness_score_parents_only(SEXP case_genetic_data_listSEXP, SEXP complement_genetic_data_listSEXP, SEXP target_snpsSEXP, SEXP weight_lookupSEXP, SEXP ld_block_vecSEXP, SEXP n_different_snps_weightSEXP, SEXP n_both_one_weightSEXP) {
416
+List GxE_fitness_score_parents_only(ListOf<IntegerMatrix> case_genetic_data_list, ListOf<IntegerMatrix> complement_genetic_data_list, IntegerVector target_snps, IntegerVector weight_lookup, int n_different_snps_weight, int n_both_one_weight);
417
+RcppExport SEXP _epistasisGAGE_GxE_fitness_score_parents_only(SEXP case_genetic_data_listSEXP, SEXP complement_genetic_data_listSEXP, SEXP target_snpsSEXP, SEXP weight_lookupSEXP, SEXP n_different_snps_weightSEXP, SEXP n_both_one_weightSEXP) {
432 418
 BEGIN_RCPP
433 419
     Rcpp::RObject rcpp_result_gen;
434 420
     Rcpp::RNGScope rcpp_rngScope_gen;
... ...
@@ -436,10 +422,9 @@ BEGIN_RCPP
436 422
     Rcpp::traits::input_parameter< ListOf<IntegerMatrix> >::type complement_genetic_data_list(complement_genetic_data_listSEXP);
437 423
     Rcpp::traits::input_parameter< IntegerVector >::type target_snps(target_snpsSEXP);
438 424
     Rcpp::traits::input_parameter< IntegerVector >::type weight_lookup(weight_lookupSEXP);
439
-    Rcpp::traits::input_parameter< IntegerVector >::type ld_block_vec(ld_block_vecSEXP);
440 425
     Rcpp::traits::input_parameter< int >::type n_different_snps_weight(n_different_snps_weightSEXP);
441 426
     Rcpp::traits::input_parameter< int >::type n_both_one_weight(n_both_one_weightSEXP);
442
-    rcpp_result_gen = Rcpp::wrap(GxE_fitness_score_parents_only(case_genetic_data_list, complement_genetic_data_list, target_snps, weight_lookup, ld_block_vec, n_different_snps_weight, n_both_one_weight));
427
+    rcpp_result_gen = Rcpp::wrap(GxE_fitness_score_parents_only(case_genetic_data_list, complement_genetic_data_list, target_snps, weight_lookup, n_different_snps_weight, n_both_one_weight));
443 428
     return rcpp_result_gen;
444 429
 END_RCPP
445 430
 }
... ...
@@ -767,8 +752,7 @@ static const R_CallMethodDef CallEntries[] = {
767 752
     {"_epistasisGAGE_compute_dif_vecs", (DL_FUNC) &_epistasisGAGE_compute_dif_vecs, 7},
768 753
     {"_epistasisGAGE_compute_parent_weights", (DL_FUNC) &_epistasisGAGE_compute_parent_weights, 6},
769 754
     {"_epistasisGAGE_chrom_fitness_score", (DL_FUNC) &_epistasisGAGE_chrom_fitness_score, 12},
770
-    {"_epistasisGAGE_compute_weighted_mean_cov", (DL_FUNC) &_epistasisGAGE_compute_weighted_mean_cov, 4},
771
-    {"_epistasisGAGE_GxE_fitness_score_parents_only", (DL_FUNC) &_epistasisGAGE_GxE_fitness_score_parents_only, 7},
755
+    {"_epistasisGAGE_GxE_fitness_score_parents_only", (DL_FUNC) &_epistasisGAGE_GxE_fitness_score_parents_only, 6},
772 756
     {"_epistasisGAGE_GxE_fitness_score", (DL_FUNC) &_epistasisGAGE_GxE_fitness_score, 14},
773 757
     {"_epistasisGAGE_chrom_fitness_list", (DL_FUNC) &_epistasisGAGE_chrom_fitness_list, 10},
774 758
     {"_epistasisGAGE_GxE_fitness_list", (DL_FUNC) &_epistasisGAGE_GxE_fitness_list, 14},
775 759
Binary files a/src/RcppExports.o and b/src/RcppExports.o differ
... ...
@@ -1164,61 +1164,9 @@ List chrom_fitness_score(IntegerMatrix case_genetic_data_in, IntegerMatrix compl
1164 1164
 // fitness score for gene by environment interactions
1165 1165
 /////////////////////////////////////////////////////////
1166 1166
 
1167
-
1168
-// [[Rcpp::export]]
1169
-List compute_weighted_mean_cov(arma::mat x, arma::vec weight_vec, IntegerVector ld_block_vec,
1170
-                               IntegerVector target_snps_in){
1171
-
1172
-  double sum_weights = arma::sum(weight_vec);
1173
-  arma::rowvec mu_hat_vec= arma::sum(x.each_col() % weight_vec) / sum_weights;
1174
-  arma::mat x_minus_mu_hat = x.each_row() - mu_hat_vec;
1175
-  arma::mat weighted_x_minus_mu_hat = x_minus_mu_hat.each_col() % weight_vec;
1176
-  arma::mat cov_mat = (1 / sum_weights) * trans(weighted_x_minus_mu_hat) * x_minus_mu_hat;
1177
-
1178
-  // set cov elements to zero if SNPs are not in same ld block
1179
-  if (ld_block_vec.length() > 1){
1180
-
1181
-    IntegerVector target_snps_block = get_target_snps_ld_blocks(target_snps_in, ld_block_vec);
1182
-    IntegerVector uni_target_blocks = unique(target_snps_block);
1183
-
1184
-    if (uni_target_blocks.length() > 1){
1185
-
1186
-      IntegerVector target_block_pos = seq_along(target_snps_block);
1187
-
1188
-      for (unsigned int i = 0; i < uni_target_blocks.length(); i++){
1189
-
1190
-        unsigned int block_i = uni_target_blocks[i];
1191
-        LogicalVector these_pos_l = target_snps_block == block_i;
1192
-        IntegerVector these_pos = target_block_pos[these_pos_l];
1193
-        IntegerVector not_these_pos = setdiff(target_block_pos, these_pos);
1194
-        for (unsigned int j = 0; j < these_pos.length(); j++){
1195
-
1196
-          unsigned int these_pos_j = these_pos[j];
1197
-
1198
-          for (unsigned int k = 0; k < not_these_pos.length(); k++){
1199
-
1200
-            unsigned int not_these_pos_k = not_these_pos[k];
1201
-            cov_mat(not_these_pos_k - 1, these_pos_j - 1) = 0;
1202
-
1203
-          }
1204
-
1205
-        }
1206
-
1207
-      }
1208
-
1209
-    }
1210
-
1211
-  }
1212
-  List res = List::create(Named("xbar") = mu_hat_vec,
1213
-                          Named("sigma") = cov_mat,
1214
-                          Named("w") = sum_weights);
1215
-  return(res);
1216
-
1217
-}
1218
-
1219 1167
 // [[Rcpp::export]]
1220 1168
 List GxE_fitness_score_parents_only(ListOf<IntegerMatrix> case_genetic_data_list, ListOf<IntegerMatrix> complement_genetic_data_list,
1221
-                                    IntegerVector target_snps, IntegerVector weight_lookup, IntegerVector ld_block_vec,
1169
+                                    IntegerVector target_snps, IntegerVector weight_lookup,
1222 1170
                                     int n_different_snps_weight = 1, int n_both_one_weight = 0){
1223 1171
 
1224 1172
   // divide the input data based on exposure and get components for fitness score //
... ...
@@ -1267,88 +1215,43 @@ List GxE_fitness_score_parents_only(ListOf<IntegerMatrix> case_genetic_data_list
1267 1215
         arma::mat informativeness_mat_exp1 = family_weights_list_i["difference_mat"];
1268 1216
         arma::mat informativeness_mat_exp2 = family_weights_list_j["difference_mat"];
1269 1217
 
1270
-        // compute components of t2
1271
-        List exp1_list = compute_weighted_mean_cov(informativeness_mat_exp1, weights_exp1, ld_block_vec,
1272
-                                                        target_snps);
1273
-        List exp2_list = compute_weighted_mean_cov(informativeness_mat_exp2, weights_exp2, ld_block_vec,
1274
-                                                        target_snps);
1275
-
1276
-        // mean difference vector //
1277
-        arma::rowvec xbar1 = exp1_list["xbar"];
1278
-        arma::rowvec xbar2 = exp2_list["xbar"];
1279
-        arma::rowvec xbar_diff = xbar1 - xbar2;
1280
-
1281
-        // cov mat //
1282
-        double w1 = exp1_list["w"];
1283
-        arma::mat sigma1 = exp1_list["sigma"];
1284
-        double w2 = exp2_list["w"];
1285
-        arma::mat sigma2 = exp2_list["sigma"];
1286
-        //arma::mat sigma_hat = (1/w1)*sigma1 + (1/w2)*sigma2;
1287
-        arma::mat sigma_hat = (w1*sigma1 + w2*sigma2)/(w1 + w2);
1288
-
1289
-        // two sample modified hotelling stat //
1290
-        double weight_scalar = (w1*w2)/(w1 + w2);
1291
-        s = (weight_scalar / 1000) * as_scalar(xbar_diff * arma::pinv(sigma_hat) * xbar_diff.t());
1292
-        // arma::mat sigma_hat_inv = arma::pinv(sigma_hat);
1293
-        // s = as_scalar(xbar_diff * sigma_hat_inv * xbar_diff.t());
1294
-        // s = s / 1000;
1295
-
1296
-        // if the fitness score is zero or undefined (either due to zero variance or mean), reset to small number
1297
-        if ( (s <= 0) | R_isnancpp(s) | !arma::is_finite(s) ){
1298
-          s = pow(10, -10);
1299
-        }
1300
-
1301
-        // prepare results
1302
-        NumericVector se = wrap(sqrt(sigma_hat.diag()));
1303
-        NumericVector xbar_diff_n = wrap(xbar_diff);
1304
-        NumericVector std_diff_vecs = xbar_diff_n/se;
1305
-        std_diff_vecs[se == 0] = pow(10, -10);
1306
-
1307
-        // store in list
1308
-        pair_scores_list[pos] = List::create(Named("fitness_score") = s,
1309
-                                             Named("sum_dif_vecs") = std_diff_vecs);
1310
-
1311
-        // // get overall mean weight
1312
-        // double exp1_weights_sum = arma::as_scalar(sum(weights_exp1));
1313
-        // double exp2_weights_sum = arma::as_scalar(sum(weights_exp2));
1314
-        // arma::uvec exp1_inf_fams = arma::find(weights_exp1 > 0);
1315
-        // arma::uvec exp2_inf_fams = arma::find(weights_exp2 > 0);
1316
-        // int n_inf_exp1 = exp1_inf_fams.n_elem;
1317
-        // int n_inf_exp2 = exp2_inf_fams.n_elem;
1318
-        // double n_total = n_inf_exp1 + n_inf_exp2;
1319
-        // double mean_weight = (exp1_weights_sum + exp2_weights_sum)/n_total;
1320
-        // double exp1_mean_weight = exp1_weights_sum / weights_exp1.n_elem;
1321
-        // double exp2_mean_weight = exp2_weights_sum / weights_exp2.n_elem;
1218
+        // get overall mean weight
1219
+        double exp1_weights_sum = arma::as_scalar(sum(weights_exp1));
1220
+        double exp2_weights_sum = arma::as_scalar(sum(weights_exp2));
1221
+        arma::uvec exp1_inf_fams = arma::find(weights_exp1 > 0);
1222
+        arma::uvec exp2_inf_fams = arma::find(weights_exp2 > 0);
1223
+        int n_inf_exp1 = exp1_inf_fams.n_elem;
1224
+        int n_inf_exp2 = exp2_inf_fams.n_elem;
1225
+        double n_total = n_inf_exp1 + n_inf_exp2;
1226
+        double mean_weight = (exp1_weights_sum + exp2_weights_sum)/n_total;
1227
+        double exp1_mean_weight = exp1_weights_sum / n_inf_exp1;
1228
+        double exp2_mean_weight = exp2_weights_sum / n_inf_exp2;
1322 1229
 
1323 1230
         // get mse for each exposure for informative families
1324
-        // arma::vec exp1_sq_errors = arma::pow(weights_exp1 - mean_weight, 2);
1325
-        // arma::vec exp1_non_zero_weight = zeros<vec>(exp1_sq_errors.n_elem);
1326
-        // exp1_non_zero_weight.elem(exp1_inf_fams).ones();
1327
-        // arma::vec exp2_sq_errors = arma::pow(weights_exp2 - mean_weight, 2);
1328
-        // arma::vec exp2_non_zero_weight = zeros<vec>(exp2_sq_errors.n_elem);
1329
-        // exp2_non_zero_weight.elem(exp2_inf_fams).ones();
1330
-        // double exp1_mse = arma::as_scalar(sum(exp1_sq_errors.each_col() % exp1_non_zero_weight)) / (n_inf_exp1 - 1);
1331
-        // double exp2_mse = arma::as_scalar(sum(exp2_sq_errors.each_col() % exp2_non_zero_weight)) / (n_inf_exp2 - 1);
1332
-        // double s = abs(exp1_mean_weight - exp2_mean_weight) * abs(exp1_mse - exp2_mse);
1333
-        //double s = abs(exp1_mean_weight - exp2_mean_weight);
1231
+        arma::vec exp1_sq_errors = arma::pow(weights_exp1 - mean_weight, 2);
1232
+        arma::vec exp1_non_zero_weight = zeros<vec>(exp1_sq_errors.n_elem);
1233
+        exp1_non_zero_weight.elem(exp1_inf_fams).ones();
1234
+        arma::vec exp2_sq_errors = arma::pow(weights_exp2 - mean_weight, 2);
1235
+        arma::vec exp2_non_zero_weight = zeros<vec>(exp2_sq_errors.n_elem);
1236
+        exp2_non_zero_weight.elem(exp2_inf_fams).ones();
1237
+        double exp1_mse = arma::as_scalar(sum(exp1_sq_errors.each_col() % exp1_non_zero_weight)) / (n_inf_exp1 - 1);
1238
+        double exp2_mse = arma::as_scalar(sum(exp2_sq_errors.each_col() % exp2_non_zero_weight)) / (n_inf_exp2 - 1);
1239
+        double s = abs(exp1_mean_weight - exp2_mean_weight) * abs(exp1_mse - exp2_mse);
1334 1240
 
1335 1241
         // now look at 'marginal' equivalents
1336
-        // arma::rowvec exp1_weighted_inf = sum(informativeness_mat_exp1.each_col() % weights_exp1, 0);
1337
-        // arma::rowvec exp2_weighted_inf = sum(informativeness_mat_exp2.each_col() % weights_exp2, 0);
1338
-        // double sum_weights = exp1_weights_sum + exp2_weights_sum;
1339
-        // arma::rowvec weighted_mean_inf = (exp1_weighted_inf + exp2_weighted_inf) / sum_weights;
1340
-        // arma::mat exp1_sq_errors_marginal = arma::pow(informativeness_mat_exp1.each_row() - weighted_mean_inf, 2);
1341
-        // arma::rowvec exp1_mse_marginal = sum(exp1_sq_errors_marginal.each_col() % weights_exp1, 0) / (exp1_weights_sum - 1);
1342
-        // arma::mat exp2_sq_errors_marginal = arma::pow(informativeness_mat_exp2.each_row() - weighted_mean_inf, 2);
1343
-        // arma::rowvec exp2_mse_marginal = sum(exp2_sq_errors_marginal.each_col() % weights_exp2, 0) / (exp2_weights_sum - 1);
1344
-        // arma::rowvec mse_diff = arma::abs(exp1_mse_marginal - exp2_mse_marginal);
1345
-        // arma::rowvec exp1_weighted_marginal_mean = exp1_weighted_inf / exp1_weights_sum;
1346
-        // arma::rowvec exp2_weighted_marginal_mean = exp2_weighted_inf / exp2_weights_sum;
1347
-        // arma::rowvec weighted_marginal_diff = arma::abs(exp1_weighted_marginal_mean - exp2_weighted_marginal_mean);
1242
+        arma::rowvec exp1_weighted_inf = sum(informativeness_mat_exp1.each_col() % weights_exp1, 0);
1243
+        arma::rowvec exp2_weighted_inf = sum(informativeness_mat_exp2.each_col() % weights_exp2, 0);
1244
+        double sum_weights = exp1_weights_sum + exp2_weights_sum;
1245
+        arma::rowvec weighted_mean_inf = (exp1_weighted_inf + exp2_weighted_inf) / sum_weights;
1246
+        arma::mat exp1_sq_errors_marginal = arma::pow(informativeness_mat_exp1.each_row() - weighted_mean_inf, 2);
1247
+        arma::rowvec exp1_mse_marginal = sum(exp1_sq_errors_marginal.each_col() % weights_exp1, 0) / (exp1_weights_sum - 1);
1248
+        arma::mat exp2_sq_errors_marginal = arma::pow(informativeness_mat_exp2.each_row() - weighted_mean_inf, 2);
1249
+        arma::rowvec exp2_mse_marginal = sum(exp2_sq_errors_marginal.each_col() % weights_exp2, 0) / (exp2_weights_sum - 1);
1250
+        arma::rowvec mse_diff = arma::abs(exp1_mse_marginal - exp2_mse_marginal);
1348 1251
 
1349 1252
         // store in list
1350
-        // pair_scores_list[pos] = List::create(Named("fitness_score") = s,
1351
-        //                                      Named("sum_dif_vecs") = weighted_marginal_diff);
1253
+        pair_scores_list[pos] = List::create(Named("fitness_score") = s,
1254
+                                             Named("sum_dif_vecs") = mse_diff);
1352 1255
 
1353 1256
         // arma::vec y_exp1(weights_exp1.n_elem, fill::zeros);
1354 1257
         // arma::vec y_exp2(weights_exp2.n_elem, fill::ones);
... ...
@@ -1706,7 +1609,7 @@ List GxE_fitness_list(List case_genetic_data_list, List complement_genetic_data_
1706 1609
     } else {
1707 1610
 
1708 1611
       scores[i] = GxE_fitness_score_parents_only(case_genetic_data_list, complement_genetic_data_list, target_snps,
1709
-                                                 weight_lookup, ld_block_vec, n_different_snps_weight, n_both_one_weight);
1612
+                                                 weight_lookup, n_different_snps_weight, n_both_one_weight);
1710 1613
 
1711 1614
     }
1712 1615
 
1713 1616
Binary files a/src/epistasisGA.o and b/src/epistasisGA.o differ
1714 1617
Binary files a/src/epistasisGAGE.so and b/src/epistasisGAGE.so differ