Exemplo n.º 1
0
int RateGamma::computePatternRates(DoubleVector &pattern_rates, IntVector &pattern_cat) {
	//cout << "Computing Gamma site rates by empirical Bayes..." << endl;

	phylo_tree->computePatternLhCat(WSL_RATECAT);

	int npattern = phylo_tree->aln->getNPattern();
	pattern_rates.resize(npattern);
	pattern_cat.resize(npattern);

	double *lh_cat = phylo_tree->_pattern_lh_cat;
	for (int i = 0; i < npattern; i++) {
		double sum_rate = 0.0, sum_lh = 0.0;
		int best = 0;
		for (int c = 0; c < ncategory; c++) {
			sum_rate += rates[c] * lh_cat[c];
			sum_lh += lh_cat[c];
			if (lh_cat[c] > lh_cat[best] || (lh_cat[c] == lh_cat[best] && random_double()<0.5))  // break tie at random
                best = c;
		}
		pattern_rates[i] = sum_rate / sum_lh;
		pattern_cat[i] = best;
		lh_cat += ncategory;
	}
    return ncategory;

//	pattern_rates.clear();
//	pattern_rates.insert(pattern_rates.begin(), ptn_rates, ptn_rates + npattern);
//	pattern_cat.resize(npattern, 0);
//	for (int i = 0; i < npattern; i++)
//		for (int j = 1; j < ncategory; j++)
//			if (fabs(rates[j] - ptn_rates[i]) < fabs(rates[pattern_cat[i]] - ptn_rates[i]))
//				pattern_cat[i] = j;
//	delete [] ptn_rates;
}
Exemplo n.º 2
0
void ZFunction::calculateAndWriteToVector(
            String string, IntVector &stringFunction) {
    stringFunction.resize(string.size());
    stringFunction[0] = 0;
    int mostRightBound = -1, mostRightBoundBegin = -1;
    for (int currentPosition = 1; currentPosition < (int)string.size();
                currentPosition++) {
        if (currentPosition < mostRightBound) {
            stringFunction[currentPosition] =
                std::min(stringFunction[currentPosition - mostRightBoundBegin],
                mostRightBound - currentPosition);
        } else {
            stringFunction[currentPosition] = 0;
        }
        while (currentPosition + stringFunction[currentPosition]
                < (int)string.size()
                && string[currentPosition + stringFunction[currentPosition]]
                == string[stringFunction[currentPosition]]) {
            stringFunction[currentPosition]++;
        }
        if (currentPosition + stringFunction[currentPosition]
                > mostRightBound) {
            mostRightBound = currentPosition
                + stringFunction[currentPosition];
            mostRightBoundBegin = currentPosition;
        }
    }
}
Exemplo n.º 3
0
void Ioss::ParallelUtils::global_count(const IntVector &local_counts, IntVector &global_counts) const
{
  // Vector 'local_counts' contains the number of objects
  // local to this processor.  On exit, global_counts
  // contains the total number of objects on all processors.
  // Assumes that ordering is the same on all processors
  global_counts.resize(local_counts.size());
#ifdef HAVE_MPI
  if (local_counts.size() > 0 && parallel_size() > 1) {
    if (Ioss::SerializeIO::isEnabled() && Ioss::SerializeIO::inBarrier()) {
      std::ostringstream errmsg;
      errmsg << "Attempting mpi while in barrier owned by " << Ioss::SerializeIO::getOwner();
      IOSS_ERROR(errmsg);
    }
    const int success = MPI_Allreduce((void*)&local_counts[0], &global_counts[0],
				      static_cast<int>(local_counts.size()),
				      MPI_INT, MPI_SUM, communicator_);
    if (success !=  MPI_SUCCESS) {
      std::ostringstream errmsg;
      errmsg  << "Ioss::ParallelUtils::global_count - MPI_Allreduce failed";
      IOSS_ERROR(errmsg);
    }
  } else {
    // Serial run, just copy local to global...
    std::copy(local_counts.begin(), local_counts.end(), global_counts.begin());
  }
#else
  std::copy(local_counts.begin(), local_counts.end(), global_counts.begin());
#endif
}
Exemplo n.º 4
0
int RateGammaInvar::computePatternRates(DoubleVector &pattern_rates, IntVector &pattern_cat) {
	//cout << "Computing Gamma site rates by empirical Bayes..." << endl;

	phylo_tree->computePatternLhCat(WSL_RATECAT);

	int npattern = phylo_tree->aln->getNPattern();
	pattern_rates.resize(npattern);
	pattern_cat.resize(npattern);

	double *lh_cat = phylo_tree->_pattern_lh_cat;
	for (int i = 0; i < npattern; i++) {
		double sum_rate = 0.0, sum_lh = phylo_tree->ptn_invar[i];
		int best = 0;
        double best_lh = phylo_tree->ptn_invar[i];
		for (int c = 0; c < ncategory; c++) {
			sum_rate += rates[c] * lh_cat[c];
			sum_lh += lh_cat[c];
			if (lh_cat[c] > best_lh || (lh_cat[c] == best_lh && random_double()<0.5)) { // break tie at random
                best = c+1;
                best_lh = lh_cat[c];
            }
		}
		pattern_rates[i] = sum_rate / sum_lh;
		pattern_cat[i] = best;
		lh_cat += ncategory;
	}
    return ncategory+1;
}
Exemplo n.º 5
0
	void initializeRandomly(int totalNodes)
	{
		int targetCount = std::parallel_uniform_random<int>(0, totalNodes);
		
		targets.resize(targetCount);
		
		std::parallel_launch({targetCount}, initializeTargetsRandomly, targets,
			totalNodes);
	}
Exemplo n.º 6
0
void readIntVector( ContainerNode &node,
                    const string &array_name,
                    IntVector &v) throw(Error)
{
    ContainerNode array_node = node.readArray(array_name);
    v.resize(0);
    while (array_node.hasUnread()) {
	v.push_back((int)array_node.readNumber());
    }
}
Exemplo n.º 7
0
void testResize(IntVector& test)
{
	cout << "Resize to 5 " << endl;
	test.resize(5);
	testValues(test);
	
	cout << "Resize to 10 " << endl;
	test.resize(10);
	testValues(test);
	
	cout << "Resize to 16 " << endl;
	test.resize(16);
	testValues(test);
	
	cout << "Resize to 5, fill 1 " << endl;
	test.resize(5, 1);
	testValues(test);
	
	cout << "Resize to 10, fill 1 " << endl;
	test.resize(10, 1);
	testValues(test);
	
	cout << "Resize to 17, fill 1 " << endl;
	test.resize(17, 1);
	testValues(test);
}
Exemplo n.º 8
0
void PrefixFunction::calculateAndWriteToVector(
            String string, IntVector &stringFunction) {
    stringFunction.resize(string.size());
    stringFunction[0] = 0;
    for (int currentPosition = 1; currentPosition < (int)string.size();
                currentPosition++) {
        int currentSuffixLength = stringFunction[currentPosition - 1];
        while (currentSuffixLength > 0 && string[currentPosition]
                != string[currentSuffixLength]) {
            currentSuffixLength = stringFunction[currentSuffixLength - 1];
        }
        stringFunction[currentPosition] = currentSuffixLength
            + (string[currentPosition] == string[currentSuffixLength]);
    }
}
Exemplo n.º 9
0
// function to populate long prefix suffix vector
void PopulateLPS(const string& pattern, IntVector& lps) {

  // lps vector should hold values for each character in pattern
  lps.resize(pattern.size() + 1);

  // first character don't have any lps
  lps[0] = 0;

  // iterator for going over pattern
  int pIter = 1;

  // len of the lps
  int pLen = 0;

  while(pIter < pattern.size()) {

    // got a match, update lps
    if( pattern[pIter] == pattern[pLen] ) {
      lps[pIter] = pLen + 1;
      pIter++;
      pLen++;
    }
    else {
      if(pLen == 0) {
        // no match, move to the next character
        lps[pIter ] = 0;
        pIter++;
      }
      else {
        // tricky, lps is used inside lps :)
        // no match, go back to previous lps and check from there
        // pIter is not incremented, since we need to check for same character again 
        pLen = lps[pLen-1];
      }
    }
  }
}
Exemplo n.º 10
0
void PalindromeFunction::calculateAndWriteToVector(
            String string, IntVector &stringFunction) {
    stringFunction.resize(string.size() * 2 + 1);
    String specialString;
    specialString.push_back(specialSymbol);
    for (int currentPosition = 0; currentPosition < (int)string.size();
                currentPosition++) {
        specialString.push_back(string[currentPosition]);
        specialString.push_back(specialSymbol);
    }
    int mostRightBound = -1, mostRightBoundBegin = -1;
    for (int currentPosition = 0; currentPosition < (int)specialString.size();
                currentPosition++) {
        if (currentPosition < mostRightBound) {
            stringFunction[currentPosition] =
                std::min(stringFunction[2 * mostRightBoundBegin
                - currentPosition], mostRightBound - currentPosition);
        } else {
            stringFunction[currentPosition] = 0;
        }
        while (currentPosition + stringFunction[currentPosition]
                < (int)specialString.size()
                && currentPosition - stringFunction[currentPosition] >= 0
                && specialString[currentPosition
                    + stringFunction[currentPosition]]
                == specialString[currentPosition
                    - stringFunction[currentPosition]]) {
            stringFunction[currentPosition]++;
        }
        if (currentPosition + stringFunction[currentPosition]
                > mostRightBound) {
            mostRightBound = currentPosition
                + stringFunction[currentPosition];
            mostRightBoundBegin = currentPosition;
        }
    }
}