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