trace_data.cpp
上传用户:yhdzpy8989
上传日期:2007-06-13
资源大小:13604k
文件大小:10k
源码类别:

生物技术

开发平台:

C/C++

  1. /*
  2.  * ===========================================================================
  3.  * PRODUCTION $Log: trace_data.cpp,v $
  4.  * PRODUCTION Revision 1000.1  2004/06/01 21:07:27  gouriano
  5.  * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.6
  6.  * PRODUCTION
  7.  * ===========================================================================
  8.  */
  9. /*  $Id: trace_data.cpp,v 1000.1 2004/06/01 21:07:27 gouriano Exp $
  10.  * ===========================================================================
  11.  *
  12.  *                            PUBLIC DOMAIN NOTICE
  13.  *               National Center for Biotechnology Information
  14.  *
  15.  *  This software/database is a "United States Government Work" under the
  16.  *  terms of the United States Copyright Act.  It was written as part of
  17.  *  the author's official duties as a United States Government employee and
  18.  *  thus cannot be copyrighted.  This software/database is freely available
  19.  *  to the public for use. The National Library of Medicine and the U.S.
  20.  *  Government have not placed any restriction on its use or reproduction.
  21.  *
  22.  *  Although all reasonable efforts have been taken to ensure the accuracy
  23.  *  and reliability of the software and data, the NLM and the U.S.
  24.  *  Government do not and cannot warrant the performance or results that
  25.  *  may be obtained by using this software or data. The NLM and the U.S.
  26.  *  Government disclaim all warranties, express or implied, including
  27.  *  warranties of performance, merchantability or fitness for any particular
  28.  *  purpose.
  29.  *
  30.  *  Please cite the author in any work or product based on this material.
  31.  *
  32.  * ===========================================================================
  33.  *
  34.  * Authors:  Andrey Yazhuk
  35.  *
  36.  * File Description:
  37.  *
  38.  */
  39. #include <ncbi_pch.hpp>
  40. #include <corelib/ncbistd.hpp>
  41. #include <algorithm>
  42. #include <gui/widgets/aln_multiple/trace_data.hpp>
  43. #include <objects/general/Dbtag.hpp>
  44. #include <objects/general/Object_id.hpp>
  45. #include <objmgr/scope.hpp>
  46. #include <objmgr/graph_ci.hpp>
  47. #include <objects/seqres/Byte_graph.hpp>
  48. BEGIN_NCBI_SCOPE 
  49. USING_SCOPE(objects);
  50. ////////////////////////////////////////////////////////////////////////////////
  51. /// CTraceDataProxy
  52. CTraceDataProxy::CTraceDataProxy(const CBioseq_Handle& handle, bool b_neg_strand)
  53. :   m_Handle(handle),
  54.     m_bNegativeStrand(b_neg_strand),
  55.     m_Status(eUnknown)    
  56. {
  57.     if(m_TitleToType.empty()) {
  58.         m_TitleToType["Phrap Quality"] = CTraceData::eA - 1;
  59.         m_TitleToType["A-channel Trace Chromatogram"] = CTraceData::eA;
  60.         m_TitleToType["C-channel Trace Chromatogram"] = CTraceData::eC;
  61.         m_TitleToType["T-channel Trace Chromatogram"] = CTraceData::eT;
  62.         m_TitleToType["G-channel Trace Chromatogram"] = CTraceData::eG;    
  63.     }
  64. }
  65. bool    CTraceDataProxy::HasData() const
  66. {
  67.     switch(m_Status)    {
  68.     case eNoData: return false;
  69.     case eHasData: return true;
  70.     case eUnknown:  {
  71.         const CSeq_id* id = m_Handle.GetSeqId();
  72.         if(id  &&  id->IsGeneral()  &&  id->GetGeneral().GetTag().IsId()  &&
  73.             (id->GetGeneral().GetDb() == "ti"  
  74.               ||  id->GetGeneral().GetDb() == "TRACE"))     {
  75.             m_Status = eHasData;
  76.             return true;
  77.         }
  78.         m_Status = eNoData;
  79.     }; break; // case eUnknown
  80.     };
  81.     return false;
  82. }
  83. CTraceData* CTraceDataProxy::LoadData()
  84. {
  85.     CTraceData* p_data = NULL;
  86.     // first - look for CSeq_graph-s with familiar titles
  87.     const CSeq_graph* raw_graphs[5] = { 0, 0, 0, 0, 0 };
  88.     CBioseq_Handle chgr_handle; // empty for now
  89.     const CSeq_id* id = m_Handle.GetSeqId();
  90.     if (id  &&  id->IsGeneral()  &&  id->GetGeneral().GetTag().IsId()  &&
  91.         (id->GetGeneral().GetDb() == "ti"  ||  id->GetGeneral().GetDb() == "TRACE"))    {
  92.         string sid = string("gnl|TRACE_CHGR|") + NStr::IntToString(id->GetGeneral().GetTag().GetId());
  93.         CSeq_id trace_chgr_id(sid);
  94.     
  95.         // load satellite sequence with chromatograms into Object Manager
  96.         chgr_handle = m_Handle.GetScope().GetBioseqHandle(trace_chgr_id);
  97.     }
  98.     // now using CGraph_CI(handle) we are able to iterate quality and chromatogram graphs
  99.     CGraph_CI graph_iter(m_Handle, 0, m_Handle.GetBioseqLength());
  100.     while(graph_iter)   {
  101.         const CSeq_graph& graph = graph_iter->GetOriginalGraph();
  102.         if(graph.CanGetTitle()  &&  graph.CanGetLoc()) {
  103.             string sTitle = graph.GetTitle();
  104.             if(m_TitleToType.find(sTitle) != m_TitleToType.end())  {                                
  105.                 _ASSERT(graph.GetGraph().Which() == CSeq_graph::C_Graph::e_Byte);
  106.                 //_TRACE("nTitle: "<< sTitle);
  107.                 int type = m_TitleToType[sTitle];  
  108.                 if(m_TitleToType.find(sTitle) != m_TitleToType.end())  {               
  109.                     raw_graphs[type + 1] = & graph; // store pointer to graph
  110.                 }
  111.             }                        
  112.         }
  113.         ++graph_iter;
  114.     } 
  115.     // now check what we've got    
  116.     int len = 0, sig_len = 0;
  117.     bool b_conf = raw_graphs[0] != NULL;
  118.     if(b_conf)  {  // if we have confidence graph len = conf len      
  119.         const CSeq_graph::C_Graph& gr = raw_graphs[0]->GetGraph();
  120.         const CByte_graph::TValues& values = gr.GetByte().GetValues();
  121.         len = (int) values.size(); 
  122.     }
  123.     int i = 1;
  124.     for( ; i < 5  &&  raw_graphs[i] == NULL;  i++ )  {};
  125.     bool b_ch = (i < 5);
  126.     if(b_ch)  { // if we have chromatograms         
  127.         double A = raw_graphs[i]->GetA();
  128.         const CSeq_graph::C_Graph& gr = raw_graphs[i]->GetGraph();
  129.         const CByte_graph::TValues& values = gr.GetByte().GetValues();
  130.         sig_len = (int) values.size();
  131.         if(len == 0)    {
  132.             len  = (int) (sig_len / A); // seq length of graph
  133.         }
  134.     }
  135.     if(len > 0) { // we have at least one graph
  136.         p_data = new  CTraceData();
  137.         p_data->Init(0, len - 1, sig_len, m_bNegativeStrand);
  138.         if(b_conf)  { // copy confidence values to CTraceData
  139.             const CSeq_graph::C_Graph& gr = raw_graphs[0]->GetGraph();
  140.             const CByte_graph::TValues& values = gr.GetByte().GetValues();
  141.             for( i = 0; i < len; i++ ) {
  142.                 p_data->SetConfidence(i, values[i]);   
  143.             }
  144.         }
  145.         for( i = 1 ; i < 5  &&  b_ch; i++)  {
  146.             bool b_calc_pos = false;
  147.             if(raw_graphs[i])   {
  148.                 const CSeq_graph::C_Graph& gr = raw_graphs[i]->GetGraph();
  149.                 const CByte_graph::TValues& values = gr.GetByte().GetValues();
  150.                 CTraceData::TSignalValue A = raw_graphs[i]->GetA();
  151.                 CTraceData::TSignalValue K = A / 255;
  152.                 // calculate positions on sequnce for chromatogram samples
  153.                 if(! b_calc_pos)    { 
  154.                     CTraceData::TPositions& positions = p_data->GetPositions();
  155.                     CTraceData::TFloatSeqPos K_pos = 
  156.                                     ((CTraceData::TFloatSeqPos) len) / sig_len;
  157.                     for( int j = 0; j < sig_len; j++ )  {
  158.                         positions[j] = K_pos * j;
  159.                     }
  160.                     b_calc_pos = true;
  161.                 }        
  162.                 // copy chromatogram data
  163.                 CTraceData::TValues& data_values = p_data->GetValues((CTraceData::EChannel) (i - 1));
  164.                 for( int j = 0; j < sig_len; j++ )  {
  165.                     data_values[j] = K * ((unsigned char) values[j]);
  166.                 }
  167.             }
  168.         }
  169.     }
  170.     return p_data;
  171. }
  172. ////////////////////////////////////////////////////////////////////////////////
  173. /// CTraceData
  174. void    CTraceData::Init(TSignedSeqPos from, TSignedSeqPos to, int samples, bool negative)
  175. {
  176.     _ASSERT(to >= from  &&  samples >= 0);
  177.     m_From = from;
  178.     m_To = to;
  179.     m_Confs.resize(GetSeqLength());
  180.     m_Positions.resize(samples);
  181.     m_ASig.resize(samples);
  182.     m_CSig.resize(samples);
  183.     m_TSig.resize(samples);
  184.     m_GSig.resize(samples);
  185.     m_bNegative = negative;
  186. }
  187. const CTraceData::TValues&      CTraceData::GetValues(EChannel channel) const
  188. {
  189.     switch(channel)  {
  190.     case eA: return m_ASig;
  191.     case eC: return m_CSig;
  192.     case eT: return m_TSig;
  193.     case eG: return m_GSig;
  194.     }
  195.     _ASSERT(false);
  196.     NCBI_THROW(CException, eUnknown, "unhandled channel in CTraceData");
  197. }
  198. CTraceData::TValues&      CTraceData::GetValues(EChannel channel)
  199. {
  200.     switch(channel)  {
  201.     case eA: return m_ASig;
  202.     case eC: return m_CSig;
  203.     case eT: return m_TSig;
  204.     case eG: return m_GSig;
  205.     }
  206.     _ASSERT(false);
  207.     NCBI_THROW(CException, eUnknown, "unhandled channel in CTraceData");
  208. }
  209. void    CTraceData::CalculateMax()
  210. {
  211.     if(m_Confs.size())  {
  212.         vector<TConfidence>::const_iterator it = max_element(m_Confs.begin(), m_Confs.end());
  213.         m_MaxConfidence = *it;
  214.     } else m_MaxConfidence = 0.0f;
  215.     if(m_ASig.size())   {
  216.         m_MaxA = * max_element(m_ASig.begin(), m_ASig.end());
  217.         m_MaxC = * max_element(m_CSig.begin(), m_CSig.end());
  218.         m_MaxT = * max_element(m_TSig.begin(), m_TSig.end());
  219.         m_MaxG = * max_element(m_GSig.begin(), m_GSig.end());
  220.     } else {
  221.         m_MaxA = m_MaxC = m_MaxT = m_MaxG = 0.0f;
  222.     }
  223. }
  224. CTraceData::TConfidence     CTraceData::GetMaxConfidence() const
  225. {
  226.     return m_MaxConfidence;
  227. }
  228. CTraceData::TSignalValue    CTraceData::GetMax(EChannel channel) const
  229. {
  230.     switch(channel) {
  231.     case eA: return m_MaxA;
  232.     case eC: return m_MaxC;
  233.     case eT: return m_MaxT;
  234.     case eG: return m_MaxG;
  235.     }
  236.     _ASSERT(false);
  237.     NCBI_THROW(CException, eUnknown, "unhandled channel in CTraceData");
  238. }
  239. END_NCBI_SCOPE
  240. /*
  241.  * ===========================================================================
  242.  * $Log: trace_data.cpp,v $
  243.  * Revision 1000.1  2004/06/01 21:07:27  gouriano
  244.  * PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.6
  245.  *
  246.  * Revision 1.6  2004/05/21 22:27:52  gorelenk
  247.  * Added PCH ncbi_pch.hpp
  248.  *
  249.  * Revision 1.5  2004/05/10 17:46:35  yazhuk
  250.  * Addressed GCC warnings
  251.  *
  252.  * Revision 1.4  2004/04/06 13:37:45  dicuccio
  253.  * Removed spurious cout
  254.  *
  255.  * Revision 1.3  2004/03/29 19:00:30  yazhuk
  256.  * Moved CTraceDataProxy from align_row.cpp
  257.  *
  258.  * Revision 1.2  2004/03/03 18:04:55  dicuccio
  259.  * Throw exception from functions expected to return a value if no value can be
  260.  * returned.
  261.  *
  262.  * Revision 1.1  2004/03/02 15:13:51  yazhuk
  263.  * Initial revision
  264.  *
  265.  * ===========================================================================
  266.  */