src/c++/lib/genotyping/GraphGenotyper.cpp
/**
 * Add a alignment, depth and summary statistics for a sample
 */
Add a alignment, depth and summary statistics for a sample
Add a alignment depth and summary statistics for a sample
void GraphGenotyper::addAlignment(SampleInfo const& sampleinfo)
{
    const std::string& samplename = sampleinfo.sample_name();
    const Json::Value& alignment = sampleinfo.get_alignment_data();
    const double depth = sampleinfo.autosome_depth();
    const int read_length = sampleinfo.read_length();
    _impl->samplenames.push_back(samplename);
    _impl->samplenameindex[samplename] = _impl->samplenames.size() - 1;
    _impl->breakpoint_maps.push_back(createBreakpointMap(*_impl->graph));
    for (auto& breakpoint : _impl->breakpoint_maps.back())
    {
        breakpoint.second.addCounts(alignment);
    }
    _impl->depths.emplace_back(depth, read_length);
    _impl->depth_sds.emplace_back(sampleinfo.depth_sd());
    _impl->sexes.emplace_back(sampleinfo.sex());
    if (alignment.isMember("eventinfo"))
    {
        if (_impl->basic_info.isMember("eventinfo"))
        {
            assert(alignment["eventinfo"] == _impl->basic_info["eventinfo"]);
        }
        else
        {
            _impl->basic_info["eventinfo"] = alignment["eventinfo"];
        }
    }
    if (!_impl->basic_info.isMember("graphinfo"))
    {
        _impl->basic_info["graphinfo"] = Json::objectValue;
        if (alignment.isMember("ID"))
        {
            _impl->basic_info["graphinfo"]["ID"] = alignment["ID"];
        }
        else if (alignment.isMember("vcf_records"))
        {
            std::string event_id = "";
            for (auto const& rec : alignment["vcf_records"])
            {
                if (rec.isMember("id"))
                {
                    if (!event_id.empty())
                    {
                        event_id += ",";
                    }
                    event_id += rec["id"].asString();
                }
            }
            _impl->basic_info["graphinfo"]["ID"] = event_id;
        }
        if (!_impl->basic_info.isMember("breakpointinfo"))
        {
            _impl->basic_info["breakpointinfo"] = Json::arrayValue;
            const auto& breakpoint_map = _impl->breakpoint_maps.back();
            for (const auto& breakpoint : breakpoint_map)
            {
                Json::Value value = Json::objectValue;
                value["name"] = breakpoint.first;
                value["mapped_alleles"] = Json::objectValue;
                for (const auto& allele : breakpoint.second.allAlleleNames())
                {
                    const auto& canonical_allele = breakpoint.second.getCanonicalAlleleName(allele);
                    if (canonical_allele != allele)
                    {
                        value["mapped_alleles"][allele] = canonical_allele;
                    }
                }
                _impl->basic_info["breakpointinfo"].append(value);
            }
        }
        vector<string> copied_keys = { "target_regions", "sequencenames" };
        for (auto& key : copied_keys)
        {
            _impl->basic_info["graphinfo"][key] = alignment[key];
        }
        _impl->basic_info["graphinfo"]["nodes"] = Json::arrayValue;
        for (auto const& n : alignment["nodes"])
        {
            Json::Value node = Json::objectValue;
            node["name"] = n["name"];
            if (n.isMember("sequences"))
            {
                node["sequences"] = n["sequences"];
            }
            _impl->basic_info["graphinfo"]["nodes"].append(node);
        }
        _impl->basic_info["graphinfo"]["edges"] = Json::arrayValue;
        for (auto const& e : alignment["edges"])
        {
            Json::Value edge = Json::objectValue;
            edge["name"] = e["from"].asString() + "_" + e["to"].asString();
            if (e.isMember("sequences"))
            {
                edge["sequences"] = e["sequences"];
            }
            _impl->basic_info["graphinfo"]["edges"].append(edge);
        }
    }
    if (!_impl->basic_info.isMember("samples"))
    {
        _impl->basic_info["samples"] = Json::Value();
    }
    _impl->basic_info["samples"][samplename] = alignment["alignment_statistics"];
    auto& alignment_stat_json = _impl->basic_info["samples"][samplename];
    for (auto& k : alignment["fragment_statistics"].getMemberNames())
    {
        if (k != "linear_histogram" && k != "graph_histogram") 
        {
            alignment_stat_json[k] = alignment["fragment_statistics"][k];
        }
    }
}
void GraphGenotyper :: addAlignment ( SampleInfo const & sampleinfo ) { const std :: string & samplename = sampleinfo . sample_name ( ) ; const Json :: Value & alignment = sampleinfo . get_alignment_data ( ) ; const double depth = sampleinfo . autosome_depth ( ) ; const int read_length = sampleinfo . read_length ( ) ; _impl -> samplenames . push_back ( samplename ) ; _impl -> samplenameindex [ samplename ] = _impl -> samplenames . size ( ) - 1 ; _impl -> breakpoint_maps . push_back ( createBreakpointMap ( * _impl -> graph ) ) ; for ( auto & breakpoint : _impl -> breakpoint_maps . back ( ) ) { breakpoint . second . addCounts ( alignment ) ; } _impl -> depths . emplace_back ( depth , read_length ) ; _impl -> depth_sds . emplace_back ( sampleinfo . depth_sd ( ) ) ; _impl -> sexes . emplace_back ( sampleinfo . sex ( ) ) ; if ( alignment . isMember ( " " ) ) { if ( _impl -> basic_info . isMember ( " " ) ) { assert ( alignment [ " " ] == _impl -> basic_info [ " " ] ) ; } else { _impl -> basic_info [ " " ] = alignment [ " " ] ; } } if ( ! _impl -> basic_info . isMember ( " " ) ) { _impl -> basic_info [ " " ] = Json :: objectValue ; if ( alignment . isMember ( " " ) ) { _impl -> basic_info [ " " ] [ " " ] = alignment [ " " ] ; } else if ( alignment . isMember ( " " ) ) { std :: string event_id = " " ; for ( auto const & rec : alignment [ " " ] ) { if ( rec . isMember ( " " ) ) { if ( ! event_id . empty ( ) ) { event_id += " " ; } event_id += rec [ " " ] . asString ( ) ; } } _impl -> basic_info [ " " ] [ " " ] = event_id ; } if ( ! _impl -> basic_info . isMember ( " " ) ) { _impl -> basic_info [ " " ] = Json :: arrayValue ; const auto & breakpoint_map = _impl -> breakpoint_maps . back ( ) ; for ( const auto & breakpoint : breakpoint_map ) { Json :: Value value = Json :: objectValue ; value [ " " ] = breakpoint . first ; value [ " " ] = Json :: objectValue ; for ( const auto & allele : breakpoint . second . allAlleleNames ( ) ) { const auto & canonical_allele = breakpoint . second . getCanonicalAlleleName ( allele ) ; if ( canonical_allele != allele ) { value [ " " ] [ allele ] = canonical_allele ; } } _impl -> basic_info [ " " ] . append ( value ) ; } } vector < string > copied_keys = { " " , " " } ; for ( auto & key : copied_keys ) { _impl -> basic_info [ " " ] [ key ] = alignment [ key ] ; } _impl -> basic_info [ " " ] [ " " ] = Json :: arrayValue ; for ( auto const & n : alignment [ " " ] ) { Json :: Value node = Json :: objectValue ; node [ " " ] = n [ " " ] ; if ( n . isMember ( " " ) ) { node [ " " ] = n [ " " ] ; } _impl -> basic_info [ " " ] [ " " ] . append ( node ) ; } _impl -> basic_info [ " " ] [ " " ] = Json :: arrayValue ; for ( auto const & e : alignment [ " " ] ) { Json :: Value edge = Json :: objectValue ; edge [ " " ] = e [ " " ] . asString ( ) + " " + e [ " " ] . asString ( ) ; if ( e . isMember ( " " ) ) { edge [ " " ] = e [ " " ] ; } _impl -> basic_info [ " " ] [ " " ] . append ( edge ) ; } } if ( ! _impl -> basic_info . isMember ( " " ) ) { _impl -> basic_info [ " " ] = Json :: Value ( ) ; } _impl -> basic_info [ " " ] [ samplename ] = alignment [ " " ] ; auto & alignment_stat_json = _impl -> basic_info [ " " ] [ samplename ] ; for ( auto & k : alignment [ " " ] . getMemberNames ( ) ) { if ( k != " " && k != " " ) { alignment_stat_json [ k ] = alignment [ " " ] [ k ] ; } } }
Add a alignment, depth and summary statistics for a sample
Add a alignment depth and summary statistics for a sample
// add breakpoint map and counts // extract extra information and check we have the same event // load event ID // comma separate multiple vcf record ID together // write edge + allele map // load basic json info for output // graph alignment statistics summary // skip histogram output because it is too lengthy