28#include <cmaple_config.h> 
   50#include "operatingsystem.h" 
   56#define ASSERT(EXPRESSION) ((void)0) 
   58#if defined(__GNUC__) || defined(__clang__) 
   59#define ASSERT(EXPRESSION)                                             \ 
   60  ((EXPRESSION) ? (void)0                                              \ 
   61                : cmaple::_my_assert(#EXPRESSION, __PRETTY_FUNCTION__, \ 
   64#define ASSERT(EXPRESSION) \ 
   67       : cmaple::_my_assert(#EXPRESSION, __func__, __FILE__, __LINE__)) 
   73#if defined(__GNUC__) && !defined(GCC_VERSION) 
   75  (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) 
   82#define __func__ __FUNCTION__ 
   85#if defined(USE_HASH_MAP) 
   90#include <unordered_map> 
   91#include <unordered_set> 
   92#elif defined(__clang__) 
   95#if __has_include(<unordered_map>)   
   96#include <unordered_map> 
   97#include <unordered_set> 
   99#include <tr1/unordered_map> 
  100#include <tr1/unordered_set> 
  101using namespace std::tr1;
 
  103#elif !defined(__GNUC__) 
  106using namespace stdext;
 
  107#elif GCC_VERSION < 40300 
  108#include <ext/hash_map> 
  109#include <ext/hash_set> 
  110using namespace __gnu_cxx;
 
  111#define unordered_map hash_map 
  112#define unordered_set hash_set 
  121#elif GCC_VERSION < 40800 
  122#include <tr1/unordered_map> 
  123#include <tr1/unordered_set> 
  124using namespace std::tr1;
 
  126#include <unordered_map> 
  127#include <unordered_set> 
  135#if defined(USE_HASH_MAP) && GCC_VERSION < 40300 && !defined(_MSC_VER) && \ 
  140#if !defined(__GNUC__) 
  148  size_t operator()(
string str)
 const {
 
  149    hash<const char*> hash_str;
 
  150    return hash_str(str.c_str());
 
  158inline void _my_assert(
const char* expression,
 
  162  char* sfile = 
const_cast<char*
>(strrchr(file, 
'/'));
 
  164    sfile = 
const_cast<char*
>(file);
 
  167  std::cerr << sfile << 
":" << line << 
": " << func << 
": Assertion `" 
  168            << expression << 
"' failed." << std::endl;
 
  175typedef uint16_t StateType;
 
  180typedef int32_t PositionType;
 
  185typedef uint32_t NumSeqsType;
 
  190typedef int16_t LengthType;
 
  194typedef int32_t LengthTypeLarge;
 
  199typedef double RealNumType;
 
  204typedef std::vector<RealNumType> RealNumberVector;
 
  209typedef std::vector<int> IntList;
 
  214typedef std::vector<int> IntVector;
 
  219typedef std::vector<bool> BoolVector;
 
  224typedef std::vector<char> CharVector;
 
  229typedef std::vector<std::string> StrVector;
 
  234typedef unsigned int UINT;
 
  239enum RegionType : StateType {
 
  250enum VerboseMode { VB_QUIET, VB_MIN, VB_MED, VB_MAX, VB_DEBUG };
 
  255extern VerboseMode verbose_mode;
 
  260enum MiniIndex : NumSeqsType {
 
  278    Index() : vector_index_(0), mini_index_(UNDEFINED){};
 
  283  Index(NumSeqsType vec_index, MiniIndex mini_index)
 
  284      : vector_index_(vec_index), mini_index_(mini_index){};
 
  289  MiniIndex getMiniIndex()
 const { 
return mini_index_; }
 
  295  MiniIndex getFlipMiniIndex()
 const { 
return MiniIndex(3 - mini_index_); }
 
  300  void setMiniIndex(MiniIndex mini_index) { mini_index_ = mini_index; }
 
  305  NumSeqsType getVectorIndex()
 const { 
return vector_index_; }
 
  310  void setVectorIndex(NumSeqsType vec_index) { vector_index_ = vec_index; }
 
  315  bool operator==(
const Index& rhs)
 const {
 
  316    return (vector_index_ == rhs.getVectorIndex() &&
 
  317            mini_index_ == rhs.getMiniIndex());
 
  321  NumSeqsType vector_index_ : 30;
 
  322  MiniIndex mini_index_ : 2;
 
  329const char REF_NAME[] = 
"REF";
 
  330const int MIN_NUM_TAXA = 3;
 
  331const RealNumType MIN_NEGATIVE =
 
  332    std::numeric_limits<RealNumType>::lowest();  
 
  333const RealNumType MIN_POSITIVE =
 
  334    (std::numeric_limits<RealNumType>::min)();  
 
  337const RealNumType MAX_POSITIVE = (std::numeric_limits<RealNumType>::max)();
 
  338const RealNumType LOG_MAX_POSITIVE = log(MAX_POSITIVE);
 
  341constexpr T getMinCarryOver();
 
  343constexpr double getMinCarryOver<double>() {
 
  347constexpr float getMinCarryOver<float>() {
 
  351const RealNumType MIN_CARRY_OVER = getMinCarryOver<RealNumType>();
 
  352const RealNumType MEAN_SUBS_PER_SITE = 0.02;
 
  353const RealNumType MAX_SUBS_PER_SITE = 0.067;
 
  369  friend class ParamsBuilder;
 
  375  std::string aln_path;
 
  380  std::string ref_path;
 
  385  std::string ref_seqname;
 
  390  std::string aln_format_str;
 
  395  std::string sub_model_str;
 
  400  bool overwrite_output;
 
  411  RealNumType threshold_prob;
 
  416  RealNumType threshold_prob2;
 
  421  int failure_limit_sample;
 
  426  int failure_limit_subtree;
 
  432  int failure_limit_subtree_short_search;
 
  438  bool strict_stop_seeking_placement_sample;
 
  443  bool strict_stop_seeking_placement_subtree;
 
  449  bool strict_stop_seeking_placement_subtree_short_search;
 
  455  RealNumType thresh_log_lh_sample;
 
  461  RealNumType thresh_log_lh_subtree;
 
  467  RealNumType thresh_log_lh_subtree_short_search;
 
  472  RealNumType thresh_log_lh_failure;
 
  478  RealNumType fixed_min_blength;
 
  486  RealNumType min_blength_factor;
 
  493  RealNumType max_blength_factor;
 
  498  RealNumType min_blength_mid_factor;
 
  504  RealNumType thresh_diff_update;
 
  510  RealNumType thresh_diff_fold_update;
 
  516  PositionType mutation_update_period;
 
  521  std::string output_aln;
 
  526  std::string output_aln_format_str;
 
  531  std::string input_treefile;
 
  537  int32_t num_tree_improvement;
 
  544  RealNumType thresh_placement_cost;
 
  551  RealNumType thresh_entire_tree_improvement;
 
  557  RealNumType thresh_placement_cost_short_search;
 
  562  std::string tree_format_str;
 
  567  bool shallow_tree_search;
 
  572  char* output_testing;
 
  577  bool compute_aLRT_SH;
 
  582  PositionType aLRT_SH_replicates;
 
  587  RealNumType aLRT_SH_half_epsilon;
 
  592  uint32_t num_threads;
 
  604  std::string output_prefix;
 
  610  bool allow_replace_input_tree;
 
  615  std::string seq_type_str;
 
  620  std::string tree_search_type_str;
 
  625  bool make_consistent;
 
  630  RealNumType max_subs_per_site;
 
  635  RealNumType mean_subs_per_site;
 
  714      const int32_t& mutation_update_period);
 
  747  std::unique_ptr<cmaple::Params> 
build();
 
  753  std::unique_ptr<Params> params_ptr{};
 
  762void outError(
const char* error, 
bool quit = 
true);
 
  767void outError(
const std::string& error, 
bool quit = 
true);
 
  775void outError(
const char* error, 
const char* msg, 
bool quit = 
true);
 
  780void outError(
const char* error, 
const std::string& msg, 
bool quit = 
true);
 
  786void outWarning(
const char* warn);
 
  787void outWarning(
const std::string& warn);
 
  790std::istream& safeGetline(std::istream& is, std::string& t);
 
  795inline bool controlchar(
char& ch) {
 
  799inline bool is_newick_token(
char& ch) {
 
  800  return ch == 
':' || ch == 
';' || ch == 
',' || ch == 
')' || ch == 
'(' ||
 
  801         ch == 
'[' || ch == 
']';
 
  809bool renameString(std::string& name);
 
  816const char ERR_NO_TAXON[] = 
"Find no taxon with name ";
 
  817const char ERR_NO_AREA[] = 
"Find no area with name ";
 
  818const char ERR_NO_MEMORY[] = 
"Not enough memory!";
 
  819const char ERR_NEG_BRANCH[] = 
"Negative branch length not allowed.";
 
  820const char ERR_READ_INPUT[] =
 
  821    "File not found or incorrect input, pls check it again.";
 
  822const char ERR_READ_ANY[] =
 
  823    "Unidentified error while reading file, pls check it carefully again.";
 
  833std::string convertPosTypeToString(PositionType number);
 
  834std::string convertIntToString(
int number);
 
  835std::string convertInt64ToString(int64_t number);
 
  837std::string convertDoubleToString(RealNumType number);
 
  839std::string convertDoubleToString(RealNumType number, uint8_t precision);
 
  845bool iEquals(
const std::string& a, 
const std::string& b);
 
  853bool copyFile(
const char SRC[], 
const char DEST[]);
 
  860bool fileExists(
const std::string& strFilename);
 
  865int isDirectory(
const char* path);
 
  873int convert_int(
const char* str);
 
  881int64_t convert_int64(
const char* str);
 
  890int convert_int(
const char* str, 
int& end_pos);
 
  898void convert_int_vec(
const char* str, IntVector& vec);
 
  906int64_t convert_int64(
const char* str);
 
  915int64_t convert_int64(
const char* str, 
int& end_pos);
 
  923RealNumType convert_real_number(
const char* str);
 
  932RealNumType convert_real_number(
const char* str, 
int& end_pos);
 
  940void convert_real_numbers(RealNumType*& arr, std::string input_str);
 
  949void convert_real_number_vec(
const char* str,
 
  950                             RealNumberVector& vec,
 
  951                             char separator = 
',');
 
  960void normalize_frequencies_from_index(RealNumType* freqs,
 
  970inline void normalize_arr(RealNumType* 
const entries,
 
  971                          const int num_entries,
 
  972                          RealNumType sum_entries) {
 
  973  assert(num_entries > 0);
 
  980  sum_entries = 1.0 / sum_entries;
 
  981  for (
int i = 0; i < num_entries; ++i)
 
  982    entries[i] *= sum_entries;
 
  991inline void normalize_arr(RealNumType* 
const entries, 
const int num_entries) {
 
  992  RealNumType sum_entries = 0;
 
  993  for (
int i = 0; i < num_entries; ++i)
 
  994    sum_entries += entries[i];
 
  995  normalize_arr(entries, num_entries, sum_entries);
 
 1002std::string convert_time(
const RealNumType sec);
 
 1012void convert_range(
const char* str, 
int& lower, 
int& upper, 
int& step_size);
 
 1022void convert_range(
const char* str,
 
 1025                   RealNumType& step_size);
 
 1034void reinitDoubleArr(RealNumType*& arr,
 
 1036                     bool delete_first = 
true,
 
 1037                     bool set_zero = 
true);
 
 1042void convert_string_vec(
const char* str,
 
 1044                        char separator = 
',');
 
 1052PositionType convert_positiontype(
const char* str);
 
 1058bool is_number(
const std::string& s);
 
 1066void parseArg(
int argc, 
char* argv[], Params& params);
 
 1071void quickStartGuide();
 
 1082void trimString(std::string& str);
 
 1087int countPhysicalCPUCores();
 
 1096template <
class T1, 
class T2>
 
 1097void quicksort(T1* arr, 
int left, 
int right, T2* arr2 = NULL) {
 
 1100  assert(left <= right);
 
 1101  int i = left, j = right;
 
 1102  T1 pivot = arr[(left + right) / 2];
 
 1106    while (arr[i] < pivot)
 
 1108    while (arr[j] > pivot)
 
 1126    quicksort(arr, left, j, arr2);
 
 1128    quicksort(arr, i, right, arr2);
 
 1137void usage_cmaple(
char* argv[], 
bool full_command);
 
 1142void printCopyright(std::ostream& out);
 
 1149void setNumThreads(
const int num_threads);
 
 1154template <
typename T, 
typename... Args>
 
 1155std::unique_ptr<T> make_unique(Args&&... args) {
 
 1156  return std::unique_ptr<T>(
new T(std::forward<Args>(args)...));
 
 1163void resetStream(std::istream& instream);
 
ParamsBuilder & withFixedMinBlength(const double &fixed_min_blength)
Specify a minimum value of the branch lengths. Default: the minimum branch length is computed from 'm...
 
ParamsBuilder & withRandomSeed(const uint64_t &seed)
Specify a seed number for random generators. Default: the clock of the PC. Be careful!...
 
std::unique_ptr< cmaple::Params > build()
Build the Params object after initializing parameters.
 
ParamsBuilder & withMinBlengthFactor(const double &min_blength_factor)
Specify a factor to compute the minimum branch length (if fixed_min_blength is specified,...
 
ParamsBuilder & withNumTreeTraversal(const int32_t &num_tree_traversal)
Specify the number of times we traverse the tree looking for topological improvements (applying SPR m...
 
ParamsBuilder & withStopTreeSearchThresh(const double &stop_search_thresh)
Specify a threshold to stop the tree search. If the total log likelihood improvement obtained by an i...
 
ParamsBuilder & withMaxBlengthFactor(const double &max_blength_factor)
Specify a factor to compute the maximum branch length. Default: 40   <Maximum branch length> = <max_b...
 
ParamsBuilder()
Default constructor - Initializing a builder using the default values.
 
ParamsBuilder & withMutationUpdatePeriod(const int32_t &mutation_update_period)
Specify the period (in term of the number of sample placements) to update the substitution rate matri...
 
ParamsBuilder & withThreshProb(const double &thresh_prob)
Specify a relative probability threshold, which is used to ignore possible states with very low proba...
 
ParamsBuilder & withSPRThresh(const double &SPR_thresh)
Specify a threshold that avoids CMAPLE trying to apply SPR moves on nodes that have the placement cos...