src/c++/lib/genotyping/BreakpointStatistics.cpp
/**
 * Create breakpoint statistics on graph
 * @param wgraph graph information
 * @param node_name name of node
 * @param forward true to use successor node/edges, false to use predecessors
 */
Create breakpoint statistics on graph
@param wgraph graph information
@param node_name name of node
@param forward true to use successor node/edges, false to use predecessors
Create breakpoint statistics on graph @param wgraph graph information @param node_name name of node @param forward true to use successor node / edges false to use predecessors
BreakpointStatistics::BreakpointStatistics(Graph const& graph, NodeId node_id, bool forward)
{
    const auto& node_name = graph.nodeName(node_id);
    const auto allele_nodes = forward ? graph.successors(node_id) : graph.predecessors(node_id);
    assert(allele_nodes.size() > 1);
    std::map<std::string, std::set<std::string>> allele_edge_sets;
    for (auto const& an : allele_nodes)
    {
        const auto& an_name = graph.nodeName(an);
        const std::string edge_name = forward ? (node_name + "_" + an_name) : (an_name + "_" + node_name);
        edge_names.push_back(edge_name);
        edge_name_to_index[edge_name] = edge_names.size() - 1;
        const auto& edge_labels = forward ? graph.edgeLabels(node_id, an) : graph.edgeLabels(an, node_id);
        for (const auto& allele_name : edge_labels)
        {
            allele_edge_sets[allele_name].insert(edge_name);
            auto a_it = std::find(all_allele_names.begin(), all_allele_names.end(), allele_name);
            if (a_it == all_allele_names.end())
            {
                all_allele_names.push_back(allele_name);
            }
        }
    }
    std::map<std::string, std::list<std::string>> canonical_allele_to_allele;
    for (const auto& allele : allele_edge_sets)
    {
        const std::string canonical_allele_id = boost::algorithm::join(allele.second, ";");
        canonical_allele_to_allele[canonical_allele_id].push_back(allele.first);
    }
    std::vector<std::pair<std::string, std::string>> ordered_canonical_alleles;
    for (const auto& canonical_allele : canonical_allele_to_allele)
    {
        const auto ref_it = std::find(canonical_allele.second.begin(), canonical_allele.second.end(), "REF");
        auto canonical_allele_name = canonical_allele.second.front();
        if (ref_it != canonical_allele.second.end())
        {
            ordered_canonical_alleles.insert(ordered_canonical_alleles.begin(), std::make_pair("REF", "REF"));
            canonical_allele_name = "REF";
        }
        canonical_allele_names.push_back(canonical_allele_name);
        const auto this_allele_index = canonical_allele_names.size() - 1;
        for (const auto& edge : allele_edge_sets[canonical_allele_name])
        {
            edgename_to_alleles[edge].push_back(this_allele_index);
        }
        for (auto const& noncanonical_allele : canonical_allele.second)
        {
            allele_name_to_index[noncanonical_allele] = this_allele_index;
            allele_name_to_canonical_allele_name[noncanonical_allele] = canonical_allele_name;
            if (noncanonical_allele != "REF")
            {
                ordered_canonical_alleles.emplace_back(std::make_pair(canonical_allele_name, noncanonical_allele));
            }
        }
    }
}
BreakpointStatistics :: BreakpointStatistics ( Graph const & graph , NodeId node_id , bool forward ) { const auto & node_name = graph . nodeName ( node_id ) ; const auto allele_nodes = forward ? graph . successors ( node_id ) : graph . predecessors ( node_id ) ; assert ( allele_nodes . size ( ) > 1 ) ; std :: map < std :: string , std :: set < std :: string > > allele_edge_sets ; for ( auto const & an : allele_nodes ) { const auto & an_name = graph . nodeName ( an ) ; const std :: string edge_name = forward ? ( node_name + " " + an_name ) : ( an_name + " " + node_name ) ; edge_names . push_back ( edge_name ) ; edge_name_to_index [ edge_name ] = edge_names . size ( ) - 1 ; const auto & edge_labels = forward ? graph . edgeLabels ( node_id , an ) : graph . edgeLabels ( an , node_id ) ; for ( const auto & allele_name : edge_labels ) { allele_edge_sets [ allele_name ] . insert ( edge_name ) ; auto a_it = std :: find ( all_allele_names . begin ( ) , all_allele_names . end ( ) , allele_name ) ; if ( a_it == all_allele_names . end ( ) ) { all_allele_names . push_back ( allele_name ) ; } } } std :: map < std :: string , std :: list < std :: string > > canonical_allele_to_allele ; for ( const auto & allele : allele_edge_sets ) { const std :: string canonical_allele_id = boost :: algorithm :: join ( allele . second , " " ) ; canonical_allele_to_allele [ canonical_allele_id ] . push_back ( allele . first ) ; } std :: vector < std :: pair < std :: string , std :: string > > ordered_canonical_alleles ; for ( const auto & canonical_allele : canonical_allele_to_allele ) { const auto ref_it = std :: find ( canonical_allele . second . begin ( ) , canonical_allele . second . end ( ) , " " ) ; auto canonical_allele_name = canonical_allele . second . front ( ) ; if ( ref_it != canonical_allele . second . end ( ) ) { ordered_canonical_alleles . insert ( ordered_canonical_alleles . begin ( ) , std :: make_pair ( " " , " " ) ) ; canonical_allele_name = " " ; } canonical_allele_names . push_back ( canonical_allele_name ) ; const auto this_allele_index = canonical_allele_names . size ( ) - 1 ; for ( const auto & edge : allele_edge_sets [ canonical_allele_name ] ) { edgename_to_alleles [ edge ] . push_back ( this_allele_index ) ; } for ( auto const & noncanonical_allele : canonical_allele . second ) { allele_name_to_index [ noncanonical_allele ] = this_allele_index ; allele_name_to_canonical_allele_name [ noncanonical_allele ] = canonical_allele_name ; if ( noncanonical_allele != " " ) { ordered_canonical_alleles . emplace_back ( std :: make_pair ( canonical_allele_name , noncanonical_allele ) ) ; } } } }
Create breakpoint statistics on graph
 @param wgraph graph information
 @param node_name name of node
 @param forward true to use successor node/edges, false to use predecessors
Create breakpoint statistics on graph @param wgraph graph information @param node_name name of node @param forward true to use successor node / edges false to use predecessors
// note that sequences / sequence labels that have the same name as an edge // will not work well with this. // create index of counts <-> edge names // create canonical alleles // choose representative allele from each equivalence class