|
11 | 11 | #include <Rcpp.h> |
12 | 12 | #include <cli/progress.h> |
13 | 13 | #include "utils.h" |
| 14 | +#include <random> |
14 | 15 |
|
15 | 16 | #define MISMATCHBAM_MODE_READ 1 |
16 | 17 | #define MISMATCHBAM_MODE_STATE 3 |
@@ -524,6 +525,13 @@ Rcpp::List read_mismatchbam_cpp(std::string inname_str, |
524 | 525 |
|
525 | 526 | // ... return value for mode 3 |
526 | 527 | Rcpp::NumericMatrix pair_counts; |
| 528 | + // random number generation |
| 529 | + // first generate a single random number from R |
| 530 | + // to link the C++ RNG to the R session's current seed state |
| 531 | + // dis() can then be used to draw uniformly distributed random numbers |
| 532 | + double r_seed = R::runif(0, 1000000); |
| 533 | + std::mt19937 gen((unsigned int)r_seed); |
| 534 | + std::uniform_real_distribution<double> dis(0.0, 1.0); |
527 | 535 |
|
528 | 536 | // prepare bam file for reading |
529 | 537 | if (verbose) { |
@@ -603,7 +611,7 @@ Rcpp::List read_mismatchbam_cpp(std::string inname_str, |
603 | 611 | (calculate_aligned_bases(bamdata) >= minAlignedLength)) { |
604 | 612 |
|
605 | 613 | if (!(bamdata->core.flag & BAM_FPAIRED) && |
606 | | - ((n_alns_to_sample == 0) || (R::runif(0, 1) < keep_aln_fraction))) { |
| 614 | + ((n_alns_to_sample == 0) || (dis(gen) < keep_aln_fraction))) { |
607 | 615 | // single-end - process directly |
608 | 616 | success = process_mismatch_bam_record( |
609 | 617 | MISMATCHBAM_MODE_STATE, // run mode |
@@ -663,7 +671,7 @@ Rcpp::List read_mismatchbam_cpp(std::string inname_str, |
663 | 671 | // mate seen - get it from the map and process the pair |
664 | 672 | bamdata2 = curr_records_it->second; |
665 | 673 |
|
666 | | - if ((n_alns_to_sample == 0) || (R::runif(0, 1) < keep_aln_fraction)) { |
| 674 | + if ((n_alns_to_sample == 0) || (dis(gen) < keep_aln_fraction)) { |
667 | 675 | success = process_mismatch_bam_record_pair( |
668 | 676 | MISMATCHBAM_MODE_STATE, // run mode |
669 | 677 | bamdata2, // bam record |
@@ -712,7 +720,7 @@ Rcpp::List read_mismatchbam_cpp(std::string inname_str, |
712 | 720 | curr_records_it++) { |
713 | 721 | bamdata2 = curr_records_it->second; |
714 | 722 |
|
715 | | - if ((n_alns_to_sample == 0) || (R::runif(0, 1) < keep_aln_fraction)) { |
| 723 | + if ((n_alns_to_sample == 0) || (dis(gen) < keep_aln_fraction)) { |
716 | 724 | success = process_mismatch_bam_record( |
717 | 725 | MISMATCHBAM_MODE_STATE, // run mode |
718 | 726 | bamdata2, // bam record |
@@ -802,7 +810,7 @@ Rcpp::List read_mismatchbam_cpp(std::string inname_str, |
802 | 810 | // read overlapping alignments using iterator |
803 | 811 | while ((c = sam_itr_next(infile, iter, bamdata)) >= 0) { |
804 | 812 | if (!(bamdata->core.flag & (BAM_FUNMAP | BAM_FSECONDARY | BAM_FSUPPLEMENTARY)) && |
805 | | - ((n_alns_to_sample == 0) || (R::runif(0, 1) < keep_aln_fraction))) { |
| 813 | + ((n_alns_to_sample == 0) || (dis(gen) < keep_aln_fraction))) { |
806 | 814 | success = process_mismatch_bam_record( |
807 | 815 | MISMATCHBAM_MODE_READ, // run mode |
808 | 816 | bamdata, // bam record |
|
0 commit comments