void AdmixtureEdgeReplaceResidualsFNPR::storeAdmixtureEventsForLineage(AdmixtureNode* p)
{
    AdmixtureTree& tau = variable->getValue();
    AdmixtureNode* q_it = &p->getParent();
    while (q_it->getNumberOfChildren() == 1)
    {
        AdmixtureNode* qp = q_it;
        
        // adm parent
        if (&q_it->getAdmixtureChild() != NULL)
            ;
        
        // adm child
        else if (&q_it->getAdmixtureParent() != NULL)
            qp = &q_it->getAdmixtureParent();
        
        // divergence
        else
            break;
        
        AdmixtureNode* qc = &qp->getAdmixtureChild();
        AdmixtureNode* qpc = &qp->getChild(0);
        AdmixtureNode* qcc = &qc->getChild(0);
        
        //std::cout << "rem " << qp << " " << qc << " " << qpc << " " << qcc << "\n";
        AdmixtureEdgePosition ap(qp, qc, qpc, qcc, qc->getAge(), qc->getWeight());
        
        storedAdmixtureEdges.push_front(ap);
        
        tau.removeAdmixtureEdge(qp);
        tau.eraseAdmixtureNode(qp);
        tau.eraseAdmixtureNode(qc);
        
        q_it = &p->getParent();
    }
}
void AdmixtureBipartitionSummaryMonitor::updateBipartitions(void)
{

    if (delayTimer->getValue() <= 0)
    {

        // get tree object
        AdmixtureTree* tau = &tree->getValue();
        std::vector<AdmixtureNode*> nodes;
        for (unsigned i = 0; i < tree->getValue().getNumberOfNodes(); i++)
            nodes.push_back(&tau->getNode(i));

        // map key iterator used for find()
        std::map<std::vector<bool>, std::vector<AdmixtureEdgeRecord> >::iterator it;

        // get partitions for all nodes
        bool firstRootHit = false;
        for (size_t i = 0; i < nodes.size(); i++)
        {
            AdmixtureNode* p = nodes[i];
            if (p->isRoot())
                continue;
            else if (&p->getParent() == &tau->getRoot() && firstRootHit)
            {
                firstRootHit = false;
                continue;
            }

            std::vector<bool> p_key = std::vector<bool>(numTaxa,false);
            std::vector<bool> c_key;

            AdmixtureEdgeRecord er(0,false,0,0,0,0);
            findTaxonBipartition(p_key, p);

            if (&p->getAdmixtureParent() != NULL)
            {
                // admixture children are handled by the admixture parent case
                continue;
            }
            else if (&p->getAdmixtureChild() == NULL && &p->getAdmixtureParent() == NULL)
            {
                // skip tips for tree partitions
                if (p->getNumberOfChildren() == 0)
                    continue;

                // topology nodes have complementary disjoint bipartitions
                //flipTaxonBipartitionToMinor(p_key);

                er = AdmixtureEdgeRecord((int)numSamples,false,p->getWeight(),p->getAge(),p->getBranchLength(),branchRates->getValue()[p->getIndex()]);

                // if key exists, push on to existing value as copy of original node
                if (treeBipartitions.find(p_key) != treeBipartitions.end())
                {
                    treeBipartitions[p_key].push_back(er);
                    //std::cout << "\tpush\n";
                }

                // otherwise, create new vector
                else
                {
                    std::vector<AdmixtureEdgeRecord> tmp(1,er);
                    treeBipartitions[p_key] = tmp;
                    //std::cout << "\tinsert\n";
                }
            }

            else if (&p->getAdmixtureChild() != NULL)
            {
                // admixture parent-child pairs have disjoint bipartitions
                AdmixtureNode* c = &p->getAdmixtureChild();
                c_key = std::vector<bool>(numTaxa,false);
                findTaxonBipartition(c_key, c);
                er = AdmixtureEdgeRecord((int)numSamples,true,c->getWeight(),c->getAge(),c->getBranchLength(),branchRates->getValue()[c->getTopologyChild(0).getIndex()]);

                // if key exists, push on to existing value as copy of original node
                if (AdmixtureBipartitionSummarys.find(p_key) != AdmixtureBipartitionSummarys.end() && AdmixtureBipartitionSummarys.at(p_key).find(c_key) != AdmixtureBipartitionSummarys.at(p_key).end())
                {
                    AdmixtureBipartitionSummarys[p_key][c_key].push_back(er);
                }

                // otherwise, create new vector
                else
                {
                    std::vector<AdmixtureEdgeRecord> tmp(1,er);
                    AdmixtureBipartitionSummarys[p_key][c_key] = tmp;
                }
            }
        }
        numSamples++;
    }
}
std::string AdmixtureBipartitionMonitor::buildBipartitionString(void)
{
    // return buffer
    std::stringstream ss;
    
    // get tree object
    AdmixtureTree* tau = &tree->getValue();
    std::vector<AdmixtureNode*> nodes;
    for (unsigned i = 0; i < tree->getValue().getNumberOfNodes(); i++)
        nodes.push_back(&tau->getNode(i));
    
   // bool firstRootHit = false; // necessary?
    for (size_t i = 0; i < nodes.size(); i++)
    {

        AdmixtureNode* p = nodes[i];
                /*
        if (p->isRoot())
            continue;
        else if (&p->getParent() == &tau->getRoot() && firstRootHit)
        {
            firstRootHit = false;
            continue;
        }
         */
        
        std::vector<bool> pbp = std::vector<bool>(numTaxa,false);
        findTaxonBipartition(pbp, p);
        
        // look like s_0,s_1,r_0,r_1,t,w
        
        if (&p->getAdmixtureParent() != NULL)
        {
            // admixture children are handled by the admixture parent case
            continue;
        }
        else if (p->isRoot())
        {
            for (size_t i = 0; i < pbp.size(); i++)
            {
                //std::cout << std::noboolalpha << pbp[i];
                ss << std::noboolalpha << pbp[i];
            }
            //std::cout << "\n";
            ss << ",";
            ss << "," << p->getAge();
            ss << ",";
            ss << ",";
            ss << ",1";
            ss << separator;
        }
        else if (&p->getAdmixtureChild() == NULL && &p->getAdmixtureParent() == NULL)
        {
            // skip tips for divergence partitions
            // actually, keep 'em for pop-size params...
            //if (p->getNumberOfChildren() == 0)
            //    continue;

            //ss << "D";
            // print bitstring for divergence partition
            for (size_t i = 0; i < pbp.size(); i++)
                ss << std::noboolalpha << pbp[i];
            ss << ",";
            ss << "," << p->getAge();
            ss << "," << branchRates->getValue()[p->getIndex()];
            ss << ",";
            ss << ",1";
            ss << separator;
        }
        
        else if (&p->getAdmixtureChild() != NULL)
        {
            AdmixtureNode* c = &p->getAdmixtureChild();
            std::vector<bool> cbp = std::vector<bool>(numTaxa,false);
            findTaxonBipartition(cbp, c);
            
            for (size_t i = 0; i < pbp.size(); i++)
                ss << std::noboolalpha << pbp[i];
            ss << ",";
            for (size_t i = 0; i < cbp.size(); i++)
                ss << std::noboolalpha << cbp[i];
            ss << "," << p->getAge();
            ss << "," << branchRates->getValue()[p->getTopologyChild(0).getIndex()];
            ss << "," << branchRates->getValue()[c->getTopologyChild(0).getIndex()];
            ss << "," << c->getWeight();
            ss << separator;
        }
    }
    return ss.str();
}
/* Build newick string */
std::string ExtendedNewickAdmixtureTreeMonitor::buildExtendedNewick( AdmixtureNode* n ) {
    // create the newick string
    std::stringstream o;
    
    // extended data is only found on admixture nodes
    std::string additionalInfo = "";
    
    // loop over admixture nodes per branch
    std::stringstream admixturestream;
    
    double br = 1.0;
    if (!n->isRoot())
    {
        if (showRates)
            br = branchRates->getValue()[n->getIndex()];
        //std::cout << "hmm\n";
        // [{s=&srcPtr,d=&dstPtr,t=double,w=double},{...},...,{...}]
        
    
        if (showMetadata)
        {
            admixturestream << "[";
            AdmixtureNode* p = &n->getParent();
            while (p->getNumberOfChildren() == 1)
            {
        //        std::cout << "ok\n";

                admixturestream << "{s=";
                if (&p->getAdmixtureParent() == NULL)
                    admixturestream << p;
                else
                    admixturestream << &p->getAdmixtureParent();
                
                admixturestream << ",d=";
                if (&p->getAdmixtureChild() == NULL)
                    admixturestream << p;
                else
                    admixturestream << &p->getAdmixtureChild();
                
                admixturestream << ",t=" << p->getAge();
                admixturestream << ",w=" << p->getWeight();
                admixturestream << "}";
                
                p = &p->getParent();
                
                if (p->getNumberOfChildren() == 1)
                    admixturestream << ",";

            }
            admixturestream << "]";
            additionalInfo = admixturestream.str();
        }
    }
//    std::cout << br << "\n";
    //std::cout << additionalInfo << "\n";
    
    // test whether this is a internal or external node
    if (n->isTip()) {
        //std::cout << "tipnode\t" << additionalInfo << "\n";
        // this is a tip so we just return the name of the node
        o << n->getName() << additionalInfo << ":" << n->getTopologyBranchLength() * br;
    }
    else {
        //std::cout << "intnode\t" << additionalInfo << "\n";
        o << "(";
        for (size_t i=0; i<(n->getNumberOfChildren()-1); i++) {
            o << buildExtendedNewick( &n->getTopologyChild(i) ) << ",";
        }
        o << buildExtendedNewick( &n->getTopologyChild(n->getNumberOfChildren()-1) ) << ")" << additionalInfo << ":" << n->getTopologyBranchLength() * br;
    }
    
    return o.str();
}