hit_matrix_ds.cpp
上传用户:yhdzpy8989
上传日期:2007-06-13
资源大小:13604k
文件大小:9k
- /*
- * ===========================================================================
- * PRODUCTION $Log: hit_matrix_ds.cpp,v $
- * PRODUCTION Revision 1000.3 2004/06/01 21:10:54 gouriano
- * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.6
- * PRODUCTION
- * ===========================================================================
- */
- /* $Id: hit_matrix_ds.cpp,v 1000.3 2004/06/01 21:10:54 gouriano Exp $
- * ===========================================================================
- *
- * PUBLIC DOMAIN NOTICE
- * National Center for Biotechnology Information
- *
- * This software/database is a "United States Government Work" under the
- * terms of the United States Copyright Act. It was written as part of
- * the author's official duties as a United States Government employee and
- * thus cannot be copyrighted. This software/database is freely available
- * to the public for use. The National Library of Medicine and the U.S.
- * Government have not placed any restriction on its use or reproduction.
- *
- * Although all reasonable efforts have been taken to ensure the accuracy
- * and reliability of the software and data, the NLM and the U.S.
- * Government do not and cannot warrant the performance or results that
- * may be obtained by using this software or data. The NLM and the U.S.
- * Government disclaim all warranties, express or implied, including
- * warranties of performance, merchantability or fitness for any particular
- * purpose.
- *
- * Please cite the author in any work or product based on this material.
- *
- * ===========================================================================
- *
- * Authors: Andrey Yazhuk
- *
- * File Description:
- *
- */
- #include <ncbi_pch.hpp>
- #include <gui/widgets/hit_matrix/hit_matrix_ds.hpp>
- #include <objects/seqalign/Seq_align.hpp>
- #include <objects/seqalign/Seq_align_set.hpp>
- #include <objects/seqalign/Dense_seg.hpp>
- #include <algorithm>
- BEGIN_NCBI_SCOPE
- USING_SCOPE(ncbi::objects);
- struct FEqualSeqId
- {
- FEqualSeqId(const CSeq_id& id) : m_Id(id) {}
- FEqualSeqId(const CRef<CSeq_id>& id) : m_Id(*id) {}
- bool operator()(CConstRef<CSeq_id> id) { return id->Equals(m_Id); }
- bool operator()(CRef<CSeq_id> id) { return id->Equals(m_Id); }
- protected:
- const CSeq_id& m_Id;
- };
- CHitMatrixDataSource::CHitMatrixDataSource(const CSeq_annot& annot, CScope& scope)
- {
- m_Annot = &annot;
- m_Scope = &scope;
- CTypeConstIterator<CSeq_align> it(ConstBegin(*m_Annot));
- x_Init(it);
- }
- CHitMatrixDataSource::CHitMatrixDataSource(const CSeq_align_set& aset,
- CScope& scope)
- {
- m_Set = &aset;
- m_Scope = &scope;
- CTypeConstIterator<CSeq_align> it(ConstBegin(*m_Set));
- x_Init(it);
- }
- CHitMatrixDataSource::~CHitMatrixDataSource()
- {
- }
- void CHitMatrixDataSource::x_Init(CTypeConstIterator<CSeq_align>& it)
- {
- m_QueryHitsRange.Set(0, 0);
- m_SubjectHitsRange.Set(0, 0);
-
- // iterate through all terminal seq-aligns and build list of seq_ids
- for( ; it; ++it) {
- const CSeq_align& align = *it;
- // terminal CSeq_align
- if(align.CanGetSegs() && align.GetSegs().IsDenseg()) {
- const CDense_seg& denseg = align.GetSegs().GetDenseg();
-
- if(m_SeqIds.size() == 0) { // first seq-align - fill m_ScoreIds
- const CSeq_align::TScore& scores = align.GetScore();
- ITERATE(CSeq_align::TScore , itSc, scores) {
- if((*itSc)->CanGetId()) {
- const CObject_id& id = (*itSc)->GetId();
- m_ScoreIds.push_back(&id);
- }
- }
- }
- _ASSERT(denseg.CanGetIds());
- const CDense_seg::TIds& v_ids = denseg.GetIds();
-
- // iterate by IDs in denseg
- ITERATE(CDense_seg::TIds, itID, v_ids) {
- FEqualSeqId EqualID(*itID);
- TIdRefCont::const_iterator itS =
- find_if(m_SeqIds.begin(), m_SeqIds.end(), EqualID);
- if(itS == m_SeqIds.end()) { // not found - this is a new one
- m_SeqIds.push_back(TIdRef(*itID));
- }
- }
- }
- } //for
- }
- const CHitMatrixDataSource::TIdRefCont& CHitMatrixDataSource::GetSeqIDs()
- {
- return m_SeqIds;
- }
- /// selects query and subject from the set of available sequences
- bool CHitMatrixDataSource::SelectIds(const TIdRef& q_id, const TIdRef& s_id)
- {
- cout << "nCHitMatrixDataSource::SelectIds";
- bool b_first = true;
- m_QueryHitsRange.Set(0, 0);
- m_SubjectHitsRange.Set(0, 0);
-
- // check that both ids exist in data
- FEqualSeqId FindQ(q_id.GetObject());
- FEqualSeqId FindS(s_id.GetObject());
-
- TIdRefCont::const_iterator itQuery =
- find_if(m_SeqIds.begin(), m_SeqIds.end(), FindQ);
- TIdRefCont::const_iterator itSubject =
- find_if(m_SeqIds.begin(), m_SeqIds.end(), FindS);
-
- bool b_ok = itQuery != m_SeqIds.end()
- && itSubject != m_SeqIds.end();
- if(b_ok) { // iterate though seq_aligns and create Hit Adapters
- m_QueryID = q_id;
- m_SubjectID = s_id;
- m_HitAdapters.clear();
- CTypeConstIterator<CSeq_align> it = m_Annot.NotEmpty() ? ConstBegin(*m_Annot) : ConstBegin(*m_Set);
- // iterate by all seq_aligns
- for( ; it; ++it) {
- const CSeq_align& align = *it;
- if(align.GetType() == CSeq_align_Base::eType_partial) {
-
- _ASSERT(align.CanGetSegs());
- const CDense_seg& denseg = align.GetSegs().GetDenseg();
-
- // find S and Q Ids in denseseg
- const CDense_seg::TIds& v_ids = denseg.GetIds();
-
- // looking for query Id
- CDense_seg::TIds::const_iterator itQ = find_if(v_ids.begin(), v_ids.end(), FindQ);
- if(itQ != v_ids.end()) { // found Query
- CDense_seg::TIds::const_iterator itS =
- find_if(v_ids.begin(), v_ids.end(), FindS);
- if(itS != v_ids.end()) {
- int q_index = itQ - v_ids.begin();
- int s_index = itS - v_ids.begin();
-
- TSeqRange q_r = denseg.GetSeqRange(q_index);
- if(q_r.GetFrom() > q_r.GetTo()) {
- q_r.Set(q_r.GetTo(), q_r.GetFrom());
- }
- TSeqRange s_r = denseg.GetSeqRange(s_index);
- if(s_r.GetFrom() > s_r.GetTo()) {
- s_r.Set(s_r.GetTo(), s_r.GetFrom());
- }
- if(b_first) {
- m_QueryHitsRange = q_r;
- m_SubjectHitsRange = s_r;
- b_first = false;
- } else {
- m_QueryHitsRange += q_r;
- m_SubjectHitsRange += s_r;
- }
-
- m_HitAdapters.push_back( CHitDataAdapter(align, denseg, q_index, s_index) );
- }
- }
- }
- }
- }
- return b_ok;
- }
- CScope& CHitMatrixDataSource::GetScope()
- {
- return m_Scope.GetObject();
- }
- CHitMatrixDataSource::TIdRef CHitMatrixDataSource::GetQueryId() const
- {
- return m_QueryID;
- }
- CHitMatrixDataSource::TIdRef CHitMatrixDataSource::GetSubjectId() const
- {
- return m_SubjectID;
- }
- CBioseq_Handle CHitMatrixDataSource::GetQueryHandle()
- {
- return m_Scope->GetBioseqHandle(m_QueryID.GetObject());
- }
- CBioseq_Handle CHitMatrixDataSource::GetSubjectHandle()
- {
- return m_Scope->GetBioseqHandle(m_SubjectID.GetObject());
- }
- const CHitMatrixDataSource::THitAdapterCont& CHitMatrixDataSource::GetHits() const
- {
- return m_HitAdapters;
- }
- int CHitMatrixDataSource::GetScoresCount() const
- {
- return m_ScoreIds.size();
- }
- const CObject_id& CHitMatrixDataSource::GetScoreId(size_t index) const
- {
- return *m_ScoreIds[index];
- }
- TSeqRange CHitMatrixDataSource::GetQueryHitsRange()
- {
- return m_QueryHitsRange;
- }
- TSeqRange CHitMatrixDataSource::GetSubjectHitsRange()
- {
- return m_SubjectHitsRange;
- }
- END_NCBI_SCOPE
- /*
- * ===========================================================================
- * $Log: hit_matrix_ds.cpp,v $
- * Revision 1000.3 2004/06/01 21:10:54 gouriano
- * PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.6
- *
- * Revision 1.6 2004/05/21 22:27:54 gorelenk
- * Added PCH ncbi_pch.hpp
- *
- * Revision 1.5 2004/02/12 21:03:28 yazhuk
- * Extended x_Init() to support wider variety of alignments; implemented calculation
- * of hit ranges
- *
- * Revision 1.4 2004/01/20 18:22:10 dicuccio
- * Don't assume that the partial flag is required for proper alignment rendering
- *
- * Revision 1.3 2003/12/05 17:39:49 yazhuk
- * Added construction from CSeq_Annot, changed API to use const refs instead of CRefs
- *
- * Revision 1.2 2003/12/01 17:02:04 yazhuk
- * Seriously redesigned CHitMatrixDataSource
- *
- * Revision 1.1 2003/11/17 20:41:19 yazhuk
- * Initial revision
- *
- * ===========================================================================
- */