-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #54 from pachterlab/devel
Version 4.2.2
- Loading branch information
Showing
19 changed files
with
1,454 additions
and
359 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,93 @@ | ||
#include "Bootstrap.h" | ||
#include "weights.h" | ||
#include "EMAlgorithm.h" | ||
// #include "weights.h" | ||
// #include "EMAlgorithm.h" | ||
|
||
EMAlgorithm Bootstrap::run_em(const EMAlgorithm& em_start) { | ||
EMAlgorithm Bootstrap::run_em() { | ||
auto counts = mult_.sample(); | ||
auto weights = calc_weights(counts, ecmap_, eff_lens_); | ||
EMAlgorithm em(ecmap_, counts, target_names_, eff_lens_, weights); | ||
EMAlgorithm em(counts, index_, tc_, mean_fl); | ||
|
||
//em.set_start(em_start); | ||
em.run(10000, 20, false); | ||
em.run(10000, 50, false, false); | ||
/* em.compute_rho(); */ | ||
|
||
return em; | ||
} | ||
|
||
BootstrapThreadPool::BootstrapThreadPool( | ||
size_t n_threads, | ||
std::vector<size_t> seeds, | ||
const std::vector<int>& true_counts, | ||
const KmerIndex& index, | ||
const MinCollector& tc, | ||
const std::vector<double>& eff_lens, | ||
double mean, | ||
const ProgramOptions& p_opts, | ||
H5Writer& h5writer | ||
) : | ||
n_threads_(n_threads), | ||
seeds_(seeds), | ||
n_complete_(0), | ||
true_counts_(true_counts), | ||
index_(index), | ||
tc_(tc), | ||
eff_lens_(eff_lens), | ||
mean_fl_(mean), | ||
opt_(p_opts), | ||
writer_(h5writer) | ||
{ | ||
for (size_t i = 0; i < n_threads_; ++i) { | ||
threads_.push_back( std::thread(BootstrapWorker(*this, i)) ); | ||
} | ||
} | ||
|
||
BootstrapThreadPool::~BootstrapThreadPool() { | ||
for (size_t i = 0; i < n_threads_; ++i) { | ||
threads_[i].join(); | ||
} | ||
} | ||
|
||
void BootstrapWorker::operator() (){ | ||
while (true) { | ||
size_t cur_seed; | ||
size_t cur_id; | ||
|
||
// acquire a seed | ||
{ | ||
std::unique_lock<std::mutex> lock(pool_.seeds_mutex_); | ||
|
||
if (pool_.seeds_.empty()) { | ||
// no more bootstraps to perform, this thread is done | ||
return; | ||
} | ||
|
||
cur_id = pool_.seeds_.size() - 1; | ||
cur_seed = pool_.seeds_.back(); | ||
pool_.seeds_.pop_back(); | ||
// std::cout << "cur seed from thread (" << thread_id_ << "): " << | ||
// cur_seed << " id: " << cur_id << std::endl; | ||
} // release lock | ||
|
||
Bootstrap bs(pool_.true_counts_, | ||
pool_.index_, | ||
pool_.tc_, | ||
pool_.eff_lens_, | ||
pool_.mean_fl_, | ||
cur_seed); | ||
|
||
auto res = bs.run_em(); | ||
|
||
if (!pool_.opt_.plaintext) { | ||
std::unique_lock<std::mutex> lock(pool_.write_lock_); | ||
++pool_.n_complete_; | ||
std::cerr << "[bstrp] number of EM bootstraps complete: " << pool_.n_complete_ << "\r"; | ||
pool_.writer_.write_bootstrap(res, cur_id); | ||
// release write lock | ||
} else { | ||
// can write out plaintext in parallel | ||
plaintext_writer(pool_.opt_.output + "/bs_abundance_" + | ||
std::to_string(cur_id) + ".tsv", | ||
pool_.index_.target_names_, res.alpha_, | ||
pool_.eff_lens_, pool_.index_.target_lens_); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,102 @@ | ||
#ifndef KALLISTO_BOOTSTRAP_H | ||
#define KALLISTO_BOOTSTRAP_H | ||
|
||
#include <mutex> | ||
#include <thread> | ||
|
||
#include "KmerIndex.h" | ||
#include "MinCollector.h" | ||
#include "weights.h" | ||
#include "EMAlgorithm.h" | ||
#include "Multinomial.hpp" | ||
#include "H5Writer.h" | ||
|
||
class Bootstrap { | ||
// needs: | ||
// - "true" counts | ||
// - ecmap | ||
// - target_names | ||
// - eff_lens | ||
public: | ||
Bootstrap(const std::vector<int>& true_counts, | ||
const EcMap& ecmap, | ||
const std::vector<std::string>& target_names, | ||
const std::vector<double>& eff_lens, | ||
size_t seed) : | ||
ecmap_(ecmap), | ||
target_names_(target_names), | ||
eff_lens_(eff_lens), | ||
seed_(seed), | ||
mult_(true_counts, seed_) | ||
{} | ||
|
||
// EM Algorithm generates a sample from the Multinomial, then returns | ||
// an "EMAlgorithm" that has already run the EM as well as compute the | ||
public: | ||
Bootstrap(const std::vector<int>& true_counts, | ||
const KmerIndex& index, | ||
const MinCollector& tc, | ||
const std::vector<double>& eff_lens, | ||
double mean, | ||
size_t seed) : | ||
index_(index), | ||
tc_(tc), | ||
eff_lens_(eff_lens), | ||
mean_fl(mean), | ||
seed_(seed), | ||
mult_(true_counts, seed_) | ||
{} | ||
|
||
// EM Algorithm generates a sample from the Multinomial, then returns | ||
// an "EMAlgorithm" that has already run the EM as well as compute the | ||
// rho values | ||
EMAlgorithm run_em(const EMAlgorithm& em_start); | ||
|
||
private: | ||
const EcMap& ecmap_; | ||
const std::vector<std::string>& target_names_; | ||
const std::vector<double>& eff_lens_; | ||
size_t seed_; | ||
Multinomial mult_; | ||
EMAlgorithm run_em(); | ||
|
||
private: | ||
const KmerIndex& index_; | ||
const MinCollector& tc_; | ||
const std::vector<double>& eff_lens_; | ||
double mean_fl; | ||
size_t seed_; | ||
Multinomial mult_; | ||
}; | ||
|
||
class BootstrapThreadPool { | ||
friend class BootstrapWorker; | ||
|
||
public: | ||
BootstrapThreadPool( | ||
size_t n_threads, | ||
std::vector<size_t> seeds, | ||
const std::vector<int>& true_counts, | ||
const KmerIndex& index, | ||
const MinCollector& tc, | ||
const std::vector<double>& eff_lens, | ||
double mean, | ||
const ProgramOptions& p_opts, | ||
H5Writer& h5writer | ||
); | ||
|
||
size_t num_threads() {return n_threads_;} | ||
|
||
~BootstrapThreadPool(); | ||
private: | ||
std::vector<size_t> seeds_; | ||
size_t n_threads_; | ||
|
||
std::vector<std::thread> threads_; | ||
std::mutex seeds_mutex_; | ||
std::mutex write_lock_; | ||
|
||
size_t n_complete_; | ||
|
||
// things to run bootstrap | ||
const std::vector<int> true_counts_; | ||
const KmerIndex& index_; | ||
const MinCollector& tc_; | ||
const std::vector<double>& eff_lens_; | ||
double mean_fl_; | ||
const ProgramOptions& opt_; | ||
H5Writer& writer_; | ||
}; | ||
|
||
class BootstrapWorker { | ||
public: | ||
BootstrapWorker(BootstrapThreadPool& pool, size_t thread_id) : | ||
pool_(pool), | ||
thread_id_(thread_id) | ||
{} | ||
|
||
void operator()(); | ||
|
||
private: | ||
BootstrapThreadPool& pool_; | ||
size_t thread_id_; | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.