int CTestCompartApplication::Run(void)
{
    // Get arguments
    const CArgs& args = GetArgs();

    CRef<CObjectManager> om(CObjectManager::GetInstance());
    CGBDataLoader::RegisterInObjectManager(*om);
    CRef<CScope> scope(new CScope(*om));
    scope->AddDefaults();

    list< CRef<CSeq_align> > aligns;
    {{
         CNcbiIstream& istr = args["i"].AsInputFile();
         auto_ptr<CObjectIStream> is
             (CObjectIStream::Open(eSerial_AsnText, istr));
         while ( !is->EndOfData() ) {
             CRef<CSeq_align> align(new CSeq_align);
             *is >> *align;
             aligns.push_back(align);
         }
     }}

    CNcbiOstream& ostr = args["o"].AsOutputFile();

    TCompartOptions opts = fCompart_Defaults;
    if (args["allow-intersect"]) {
        opts |= fCompart_AllowIntersections;
    }
    if (args["allow-intersect-query"]) {
        opts |= fCompart_AllowIntersectionsQuery;
    }
    if (args["allow-intersect-subject"]) {
        opts |= fCompart_AllowIntersectionsSubject;
    }
    if (args["allow-intersect-both"]) {
        opts |= fCompart_AllowIntersectionsBoth;
    }

    list< CRef<CSeq_align_set> > compartments;
    FindCompartments(aligns, compartments, opts);

    LOG_POST(Error << "input alignments: " << aligns.size());
    LOG_POST(Error << "output compartments: " << compartments.size());

    size_t count = 0;
    NON_CONST_ITERATE (list< CRef<CSeq_align_set> >, i, compartments) {
        ++count;
        CSeq_align align;
        align.SetSegs().SetDisc(**i);

        TSeqRange r1 = align.GetSeqRange(0);
        TSeqRange r2 = align.GetSeqRange(1);
        CSeq_id_Handle id1 = CSeq_id_Handle::GetHandle(align.GetSeq_id(0));
        CSeq_id_Handle id2 = CSeq_id_Handle::GetHandle(align.GetSeq_id(1));

        LOG_POST(Error << "compartment " << count << ":");
        LOG_POST(Error << "  query   = " << id1 << " (" << r1.GetFrom() + 1 << ".." << r1.GetTo() + 1 << ")");
        LOG_POST(Error << "  subject = " << id2 << " (" << r2.GetFrom() + 1 << ".." << r2.GetTo() + 1 << ")");
        LOG_POST(Error << "  alignments = " << (*i)->Get().size());

        string title("Compartment ");
        title += NStr::NumericToString(count);
        CSeq_annot annot;
        annot.SetNameDesc(title);
        annot.SetTitleDesc(title);
        annot.SetData().SetAlign() = (*i)->Get();
        ostr << MSerial_AsnText << annot;
    }
Example #2
0
CAlignSort::SSortKey
CAlignSort::SAlignExtractor::operator()(const CSeq_align& align)
{
    SSortKey key;
    ITERATE (vector<string>, iter, key_toks) {
        SSortKey::TItem item;

        if (NStr::EqualNocase(*iter, "query")) {
            CSeq_id_Handle idh =
                CSeq_id_Handle::GetHandle(align.GetSeq_id(0));
            idh = sequence::GetId(idh, *scope,
                                  sequence::eGetId_Canonical);
            item.first = idh.GetSeqId()->AsFastaString();
        }
        else if (NStr::EqualNocase(*iter, "subject")) {
            CSeq_id_Handle idh =
                CSeq_id_Handle::GetHandle(align.GetSeq_id(1));
            idh = sequence::GetId(idh, *scope,
                                  sequence::eGetId_Canonical);
            item.first = idh.GetSeqId()->AsFastaString();
        }

        else if (NStr::EqualNocase(*iter, "query_start")) {
            item.second = align.GetSeqStart(0);
        }
        else if (NStr::EqualNocase(*iter, "subject_start")) {
            item.second = align.GetSeqStart(1);
        }

        else if (NStr::EqualNocase(*iter, "query_end")) {
            item.second = align.GetSeqStop(0);
        }
        else if (NStr::EqualNocase(*iter, "subject_end")) {
            item.second = align.GetSeqStop(1);
        }

        else if (NStr::EqualNocase(*iter, "query_strand")) {
            item.second = align.GetSeqStrand(0);
        }
        else if (NStr::EqualNocase(*iter, "subject_strand")) {
            item.second = align.GetSeqStrand(1);
        }

        else if (NStr::EqualNocase(*iter, "query_align_len")) {
            item.second = align.GetSeqRange(0).GetLength();
        }
        else if (NStr::EqualNocase(*iter, "subject_align_len")) {
            item.second = align.GetSeqRange(1).GetLength();
        }
        else if (NStr::EqualNocase(*iter, "query_traceback")) {
            CScoreBuilder builder;
            item.first = builder.GetTraceback(*scope, align, 0);
        }
        else if (NStr::EqualNocase(*iter, "subject_traceback")) {
            CScoreBuilder builder;
            item.first = builder.GetTraceback(*scope, align, 1);
        }

        else {
            /// assume it is a score
            CScoreLookup lookup;
            lookup.SetScope(*scope);
            item.second = lookup.GetScore(align, *iter);
        }
        key.items.push_back(item);
    }