示例#1
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;
}
示例#2
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;
}