Example #1
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;
}