// =============================================================== //
//                                                                 //
//   File      : AP_sequence.cxx                                   //
//   Purpose   :                                                   //
//                                                                 //
//   Institute of Microbiology (Technical University Munich)       //
//   http://www.arb-home.de/                                       //
//                                                                 //
// =============================================================== //

#include "AP_sequence.hxx"
#include <arbdbt.h>

long AP_combinableSeq::global_combineCount = 0;

AP_sequence::AP_sequence(const AliView *aliview) :
    ali(aliview),
    gb_sequence(NULp),
    has_sequence(false),
    update(0),
    cached_wbc(-1.0)
{}

GB_ERROR AP_sequence::bind_to_species(GBDATA *gb_species) {
    GB_ERROR error = NULp;

    ap_assert(!gb_sequence); // already bound to species!
    if (!gb_sequence) {
        GB_transaction ta(ali->get_gb_main());
        gb_sequence = GBT_find_sequence(gb_species, ali->get_aliname());
        if (!gb_sequence) {
            error = GBS_global_string("Species '%s' has no data in alignment '%s'",
                                      GBT_get_name_or_description(gb_species), ali->get_aliname());
        }
        unset();
    }
    return error;
}

void AP_sequence::unbind_from_species() {
    ap_assert(gb_sequence);
    gb_sequence = NULp;
    unset();
}

void AP_sequence::do_lazy_load() const {
    ap_assert(gb_sequence);
    ap_assert(!has_sequence);

    GB_transaction ta(gb_sequence);
    const char *seq = GB_read_char_pntr(gb_sequence);
    if (!seq) {
        GB_ERROR  error      = GB_await_error();
        GBDATA   *gb_species = GB_get_grandfather(gb_sequence);

        GB_warningf("Failed to load sequence of '%s'\n(Reason: %s)",
                    GBT_get_name_or_description(gb_species), error);
        seq = "";                                   // fake empty sequence
    }

    // this is no modification, this is lazy initialization
    const_cast<AP_sequence*>(this)->set(seq);
}


