Beispiel #1
0
void TreeTools::computeBootstrapValues(Tree& tree, const vector<Tree*>& vecTr, bool verbose, int format)
{
    vector<int> index;
    BipartitionList bpTree(tree, true, &index);
    vector<size_t> occurences;
    BipartitionList* bpList = bipartitionOccurrences(vecTr, occurences);

    vector< Number<double> > bootstrapValues(bpTree.getNumberOfBipartitions());

    for (size_t i = 0; i < bpTree.getNumberOfBipartitions(); i++)
    {
        if (verbose)
            ApplicationTools::displayGauge(i, bpTree.getNumberOfBipartitions() - 1, '=');
        for (size_t j = 0; j < bpList->getNumberOfBipartitions(); j++)
        {
            if (BipartitionTools::areIdentical(bpTree, i, *bpList, j))
            {
                bootstrapValues[i] = format >= 0 ? round(static_cast<double>(occurences[j]) * pow(10., 2 + format) / static_cast<double>(vecTr.size())) / pow(10., format) : static_cast<double>(occurences[j]);
                break;
            }
        }
    }

    for (size_t i = 0; i < index.size(); i++)
    {
        if (!tree.isLeaf(index[i]))
            tree.setBranchProperty(index[i], BOOTSTRAP, bootstrapValues[i]);
    }

    delete bpList;
}
Beispiel #2
0
bool BipartitionTools::areCompatible(
  const BipartitionList& bipartL1, size_t i1,
  const BipartitionList& bipartL2, size_t i2,
  bool checkElements)
{
  BipartitionList* twoBipL = buildBipartitionPair(bipartL1, i1, bipartL2, i2, checkElements);
  bool test = twoBipL->areCompatible(0, 1);
  delete twoBipL;
  return test;
}
Beispiel #3
0
bool BipartitionTools::areIdentical(
    const BipartitionList & bipartL1, unsigned int i1,
    const BipartitionList & bipartL2, unsigned int i2,
    bool checkElements)
{
  BipartitionList *twoBipL = buildBipartitionPair(bipartL1, i1, bipartL2, i2, checkElements);
  bool test = twoBipL->areIdentical(0,1);
  delete twoBipL;
  return test;
}
Beispiel #4
0
BipartitionList::BipartitionList(const BipartitionList& bipL) :
  bitBipartitionList_(),
  elements_(bipL.elements_),
  sorted_(bipL.sorted_)
{
  size_t lword  = static_cast<size_t>(BipartitionTools::LWORD);
  size_t nbword = (bipL.getNumberOfElements() + lword - 1) / lword;
  size_t nbint  = nbword * lword / (CHAR_BIT * sizeof(int));

  bitBipartitionList_.resize(bipL.getNumberOfBipartitions());
  vector<int*> bitBipL = bipL.getBitBipartitionList();
  for (size_t i = 0; i < bipL.getNumberOfBipartitions(); i++)
  {
    bitBipartitionList_[i] = new int[nbint];
    for (size_t j = 0; j < nbint; j++)
    {
      bitBipartitionList_[i][j] = bitBipL[i][j];
    }
  }
}
Beispiel #5
0
BipartitionList::BipartitionList(const BipartitionList & bipL)
{

  unsigned int lword  = BipartitionTools::LWORD;
  unsigned int nbword = (bipL.getNumberOfElements() + lword - 1) / lword;
  unsigned int nbint  = nbword * lword / (CHAR_BIT * sizeof(int));

  _bitBipartitionList.resize(bipL.getNumberOfBipartitions());
  vector<int*> bitBipL = bipL.getBitBipartitionList();
  for(unsigned int i = 0; i < bipL.getNumberOfBipartitions(); i++)
  {
    _bitBipartitionList[i] = new int[nbint];
    for(unsigned int j = 0; j < nbint; j++)
    {
      _bitBipartitionList[i][j] = bitBipL[i][j];
    }
  }

  _elements = bipL._elements;
  _sorted = bipL._sorted;
}
Beispiel #6
0
TreeTemplate<Node>* TreeTools::thresholdConsensus(const vector<Tree*>& vecTr, double threshold, bool checkNames) throw (Exception)
{
    vector<size_t> bipScore;
    vector<string> tr0leaves;
    BipartitionList* bipL;
    double score;

    if (vecTr.size() == 0)
        throw Exception("TreeTools::thresholdConsensus. Empty vector passed");

    /* check names */
    if (checkNames)
    {
        tr0leaves = vecTr[0]->getLeavesNames();
        for (size_t i = 1; i < vecTr.size(); i++)
        {
            if (!VectorTools::haveSameElements(vecTr[i]->getLeavesNames(), tr0leaves))
                throw Exception("TreeTools::thresholdConsensus. Distinct leaf sets between trees");
        }
    }

    bipL = bipartitionOccurrences(vecTr, bipScore);

    for (size_t i = bipL->getNumberOfBipartitions(); i > 0; i--)
    {
        if (bipL->getPartitionSize(i - 1) == 1)
            continue;
        score = static_cast<int>(bipScore[i - 1]) / static_cast<double>(vecTr.size());
        if (score <= threshold && score != 1.)
        {
            bipL->deleteBipartition(i - 1);
            continue;
        }
        if (score > 0.5)
            continue;
        for (size_t j = bipL->getNumberOfBipartitions(); j > i; j--)
        {
            if (!bipL->areCompatible(i - 1, j - 1))
            {
                bipL->deleteBipartition(i - 1);
                break;
            }
        }
    }

    TreeTemplate<Node>* tr = bipL->toTree();
    delete bipL;
    return tr;
}
Beispiel #7
0
TreeTemplate<Node>* BipartitionList::toTree() const throw (Exception)
{
  BipartitionList* sortedBipL;
  vector<int*> sortedBitBipL;
  int* bip;
  vector<Node*> vecNd, sonNd;
  vector<bool> alive;
  size_t lword, nbword, nbint, ii;

  /* check, copy and prepare bipartition list */

  if (!BipartitionList::areAllCompatible())
    throw Exception("Trying to build a tree from incompatible bipartitions");

  sortedBipL = dynamic_cast<BipartitionList*>(clone());
  for (size_t i = 0; i < sortedBipL->getNumberOfBipartitions(); i++)
  {
    if (sortedBipL->getPartitionSize(i) > sortedBipL->getNumberOfElements() / 2)
      sortedBipL->flip(i);
  }
  sortedBipL->sortByPartitionSize();
  sortedBipL->removeRedundantBipartitions();
  sortedBitBipL = sortedBipL->getBitBipartitionList();

  for (size_t i = 0; i < sortedBipL->getNumberOfBipartitions(); i++)
  {
    alive.push_back(true);
  }
  vecNd.resize(sortedBipL->getNumberOfBipartitions() + 1);
  lword  = static_cast<size_t>(BipartitionTools::LWORD);
  nbword = (elements_.size() + lword - 1) / lword;
  nbint  = nbword * lword / (CHAR_BIT * sizeof(int));
  bip    = new int[1]; bip[0] = 0;

  /* main loop: create one node per bipartition */
  for (size_t i = 0; i < sortedBipL->getNumberOfBipartitions(); i++)
  {
    if (sortedBipL->getPartitionSize(i) == 1)
    { // terminal
      for (size_t j = 0; j < sortedBipL->getNumberOfElements(); j++)
      {
        if (BipartitionTools::testBit(sortedBitBipL[i], static_cast<int>(j)))
        {
          vecNd[i] = new Node(elements_[j]);
          break;
        }
      }
    }
    else
    { // internal
      sonNd.clear();
      for (size_t j = 0; j < i; j++)
      {
        if (alive[j])
        {
          for (ii = 0; ii < nbint; ii++)
          {
            BipartitionTools::bitOr(bip, sortedBitBipL[j] + ii, sortedBitBipL[i] + ii, 1);
            if (bip[0] != sortedBitBipL[i][ii])
              break;
          }
          if (ii == nbint)
          {
            sonNd.push_back(vecNd[j]);
            alive[j] = false;
          }
        }
      }
      vecNd[i] = new Node();
      for (size_t k = 0; k < sonNd.size(); k++)
      {
        vecNd[i]->addSon(sonNd[k]);
      }
    }
  }

  /* create last node, which joins alive bipartitions = fatherless nodes */
  Node* rootNd = new Node();
  for (size_t i = 0; i < sortedBipL->getNumberOfBipartitions(); i++)
  {
    if (alive[i])
      rootNd->addSon(vecNd[i]);
  }

  /* construct tree and return */
  TreeTemplate<Node>* tr = new TreeTemplate<Node>(rootNd);
  tr->resetNodesId();
  delete sortedBipL;
  return tr;
}
Beispiel #8
0
BipartitionList* TreeTools::bipartitionOccurrences(const vector<Tree*>& vecTr, vector<size_t>& bipScore)
{
    vector<BipartitionList*> vecBipL;
    BipartitionList* mergedBipL;
    vector<size_t> bipSize;
    size_t nbBip;

    /*  build and merge bipartitions */
    for (size_t i = 0; i < vecTr.size(); i++)
    {
        vecBipL.push_back(new BipartitionList(*vecTr[i]));
    }
    mergedBipL = BipartitionTools::mergeBipartitionLists(vecBipL);
    for (size_t i = 0; i < vecTr.size(); i++)
    {
        delete vecBipL[i];
    }

    mergedBipL->removeTrivialBipartitions();
    nbBip = mergedBipL->getNumberOfBipartitions();
    bipScore.clear();
    for (size_t i = 0; i < nbBip; i++)
    {
        bipSize.push_back(mergedBipL->getPartitionSize(i));
        bipScore.push_back(1);
    }

    /* compare bipartitions */
    for (size_t i = nbBip; i > 0; i--)
    {
        if (bipScore[i - 1] == 0)
            continue;
        for (size_t j = i - 1; j > 0; j--)
        {
            if (bipScore[j - 1] && bipSize[i - 1] == bipSize[j - 1] && mergedBipL->areIdentical(i - 1, j - 1))
            {
                bipScore[i - 1]++;
                bipScore[j - 1] = 0;
            }
        }
    }

    /* keep only distinct bipartitions */
    for (size_t i = nbBip; i > 0; i--)
    {
        if (bipScore[i - 1] == 0)
        {
            bipScore.erase(bipScore.begin() + static_cast<ptrdiff_t>(i - 1));
            mergedBipL->deleteBipartition(i - 1);
        }
    }

    /* add terminal branches */
    mergedBipL->addTrivialBipartitions(false);
    for (size_t i = 0; i < mergedBipL->getNumberOfElements(); i++)
    {
        bipScore.push_back(vecTr.size());
    }

    return mergedBipL;
}