NCBI C++ ToolKit
trace_data.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: trace_data.cpp 18995 2009-03-18 19:28:03Z wuliangs $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Authors: Andrey Yazhuk
27  *
28  * File Description:
29  *
30  */
31 #include <ncbi_pch.hpp>
32 #include <corelib/ncbistd.hpp>
33 #include <algorithm>
34 
35 #include <gui/objutils/utils.hpp>
37 
40 #include <objmgr/scope.hpp>
41 #include <objmgr/graph_ci.hpp>
43 
44 
47 
48 ////////////////////////////////////////////////////////////////////////////////
49 /// CTraceDataProxy
50 
51 CTraceDataProxy::CTraceDataProxy(const CBioseq_Handle& handle, bool b_neg_strand)
52 : m_Handle(handle),
53  m_bNegativeStrand(b_neg_strand),
54  m_Status(eUnknown)
55 {
56  if(m_TitleToType.empty()) {
57  m_TitleToType["Phrap Quality"] = CTraceData::eA - 1;
58  m_TitleToType["Phred 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 
67 {
68  switch(m_Status) {
69  case eNoData: return false;
70  case eHasData: return true;
71  case eUnknown: {
72  CConstRef<CSeq_id> id = m_Handle.GetSeqId();
73  if(id && id->IsGeneral() && id->GetGeneral().GetTag().IsId() &&
74  (id->GetGeneral().GetDb() == "ti"
75  || id->GetGeneral().GetDb() == "TRACE")) {
77  return true;
78  }
79  m_Status = eNoData;
80  }; break; // case eUnknown
81  };
82  return false;
83 }
84 
86 {
87  CTraceData* p_data = NULL;
88 
89  // first - look for CSeq_graph-s with familiar titles
90  const CSeq_graph* raw_graphs[5] = { 0, 0, 0, 0, 0 };
91 
92  CBioseq_Handle chgr_handle; // empty for now
93 
94  CConstRef<CSeq_id> id = m_Handle.GetSeqId();
95  if (id && id->IsGeneral() && id->GetGeneral().GetTag().IsId() &&
96  (id->GetGeneral().GetDb() == "ti" || id->GetGeneral().GetDb() == "TRACE")) {
97  string sid = string("gnl|TRACE_CHGR|") + NStr::IntToString(id->GetGeneral().GetTag().GetId());
98  CSeq_id trace_chgr_id(sid);
99 
100  // load satellite sequence with chromatograms into Object Manager
101  chgr_handle = m_Handle.GetScope().GetBioseqHandle(trace_chgr_id);
102  }
103 
104  // now using CGraph_CI(handle) we are able to iterate quality and chromatogram graphs
105  if(chgr_handle) {
106  CGraph_CI graph_iter(m_Handle);
107  while(graph_iter) {
108  const CSeq_graph& graph = graph_iter->GetOriginalGraph();
109  if(graph.CanGetTitle() && graph.CanGetLoc()) {
110  string sTitle = graph.GetTitle();
111 
113  if(it != m_TitleToType.end()) {
115 
116  if(m_TitleToType.find(sTitle) != m_TitleToType.end()) {
117  int type = it->second;
118  raw_graphs[type + 1] = &graph; // store pointer to graph
119  }
120  }
121  }
122  ++graph_iter;
123  }
124  }
125 
126  if(chgr_handle) {
128  sel.SetSearchExternal(chgr_handle);
129  for ( CGraph_CI graph_iter(chgr_handle, sel); graph_iter; ++graph_iter ) {
130  const CSeq_graph& graph = graph_iter->GetOriginalGraph();
131  if(graph.CanGetTitle() && graph.CanGetLoc()) {
132  string sTitle = graph.GetTitle();
133 
135  if(it != m_TitleToType.end()) {
137  //_TRACE("\nTitle: "<< sTitle);
138 
139  if(m_TitleToType.find(sTitle) != m_TitleToType.end()) {
140  int type = it->second;
141  raw_graphs[type + 1] = &graph; // store pointer to graph
142  }
143  }
144  }
145  //++graph_iter;
146  }
147  }
148 
149  // now check what we've got
150  int len = 0, sig_len = 0;
151 
152  bool b_conf = raw_graphs[0] != NULL;
153  if(b_conf) { // if we have confidence graph len = conf len
154  const CSeq_graph::C_Graph& gr = raw_graphs[0]->GetGraph();
155  const CByte_graph::TValues& values = gr.GetByte().GetValues();
156  len = (int) values.size();
157  }
158 
159  int i = 1;
160  for( ; i < 5 && raw_graphs[i] == NULL; i++ ) {};
161  bool b_ch = (i < 5);
162  if(b_ch) { // if we have chromatograms
163  double A = raw_graphs[i]->GetA();
164  const CSeq_graph::C_Graph& gr = raw_graphs[i]->GetGraph();
165  const CByte_graph::TValues& values = gr.GetByte().GetValues();
166  sig_len = (int) values.size();
167  if(len == 0) {
168  len = (int) (sig_len / A); // seq length of graph
169  }
170  }
171 
172  if(len > 0) { // we have at least one graph
173  p_data = new CTraceData();
174  p_data->Init(0, len - 1, sig_len, m_bNegativeStrand);
175 
176  if(b_conf) { // copy confidence values to CTraceData
177  const CSeq_graph::C_Graph& gr = raw_graphs[0]->GetGraph();
178  const CByte_graph::TValues& values = gr.GetByte().GetValues();
179  for( i = 0; i < len; i++ ) {
180  p_data->SetConfidence(i, values[i]);
181  }
182  }
183  for( i = 1 ; i < 5 && b_ch; i++) {
184  bool b_calc_pos = false;
185  if(raw_graphs[i]) {
186  const CSeq_graph::C_Graph& gr = raw_graphs[i]->GetGraph();
187  const CByte_graph::TValues& values = gr.GetByte().GetValues();
188  CTraceData::TSignalValue A = (CTraceData::TSignalValue) raw_graphs[i]->GetA();
189  CTraceData::TSignalValue K = A / 255;
190 
191  // calculate positions on sequnce for chromatogram samples
192  if(! b_calc_pos) {
193  CTraceData::TPositions& positions = p_data->GetPositions();
195  ((CTraceData::TFloatSeqPos) len) / sig_len;
196  for( int j = 0; j < sig_len; j++ ) {
197  positions[j] = K_pos * j;
198  }
199  b_calc_pos = true;
200  }
201  // copy chromatogram data
202  CTraceData::TValues& data_values = p_data->GetValues((CTraceData::EChannel) (i - 1));
203  for( int j = 0; j < sig_len; j++ ) {
204  data_values[j] = K * ((unsigned char) values[j]);
205  }
206  }
207  }
208  }
209  return p_data;
210 }
211 
212 ////////////////////////////////////////////////////////////////////////////////
213 /// CTraceData
214 
215 void CTraceData::Init(TSignedSeqPos from, TSignedSeqPos to, int samples, bool negative)
216 {
217  _ASSERT(to >= from && samples >= 0);
218  m_From = from;
219  m_To = to;
220  m_Confs.resize(GetSeqLength());
221 
222  m_Positions.resize(samples);
223  m_ASig.resize(samples);
224  m_CSig.resize(samples);
225  m_TSig.resize(samples);
226  m_GSig.resize(samples);
227 
229 }
230 
232 {
233  switch(channel) {
234  case eA: return m_ASig;
235  case eC: return m_CSig;
236  case eT: return m_TSig;
237  case eG: return m_GSig;
238  }
239  _ASSERT(false);
240  NCBI_THROW(CException, eUnknown, "unhandled channel in CTraceData");
241 }
242 
244 {
245  switch(channel) {
246  case eA: return m_ASig;
247  case eC: return m_CSig;
248  case eT: return m_TSig;
249  case eG: return m_GSig;
250  }
251  _ASSERT(false);
252  NCBI_THROW(CException, eUnknown, "unhandled channel in CTraceData");
253 }
254 
256 {
257  if(m_Confs.size()) {
258  vector<TConfidence>::const_iterator it = max_element(m_Confs.begin(), m_Confs.end());
259  m_MaxConfidence = *it;
260  } else m_MaxConfidence = 0.0f;
261 
262  if(m_ASig.size()) {
263  m_MaxA = * max_element(m_ASig.begin(), m_ASig.end());
264  m_MaxC = * max_element(m_CSig.begin(), m_CSig.end());
265  m_MaxT = * max_element(m_TSig.begin(), m_TSig.end());
266  m_MaxG = * max_element(m_GSig.begin(), m_GSig.end());
267  } else {
268  m_MaxA = m_MaxC = m_MaxT = m_MaxG = 0.0f;
269  }
270 }
271 
273 {
274  return m_MaxConfidence;
275 }
276 
278 {
279  switch(channel) {
280  case eA: return m_MaxA;
281  case eC: return m_MaxC;
282  case eT: return m_MaxT;
283  case eG: return m_MaxG;
284  }
285  _ASSERT(false);
286  NCBI_THROW(CException, eUnknown, "unhandled channel in CTraceData");
287 }
288 
User-defined methods of the data storage class.
CBioseq_Handle –.
CGraph_CI –.
Definition: graph_ci.hpp:234
EStatus m_Status
Definition: trace_data.hpp:63
CTraceData * LoadData()
Definition: trace_data.cpp:85
CTraceDataProxy(const objects::CBioseq_Handle &handle, bool b_neg_strand)
CTraceDataProxy.
Definition: trace_data.cpp:51
const objects::CBioseq_Handle & m_Handle
Definition: trace_data.hpp:61
bool m_bNegativeStrand
handle to Bioseq with traces
Definition: trace_data.hpp:62
TTitleToType m_TitleToType
Definition: trace_data.hpp:65
bool HasData() const
Definition: trace_data.cpp:66
CTraceData.
Definition: trace_data.hpp:72
TSignalValue m_MaxA
Definition: trace_data.hpp:138
TSignalValue m_MaxG
Definition: trace_data.hpp:141
double TFloatSeqPos
Definition: trace_data.hpp:75
float TConfidence
Definition: trace_data.hpp:74
TValues m_CSig
Definition: trace_data.hpp:133
TSignalValue m_MaxC
Definition: trace_data.hpp:139
void Init(TSignedSeqPos from, TSignedSeqPos to, int samples, bool negative)
CTraceData.
Definition: trace_data.cpp:215
TSignedSeqPos m_To
Definition: trace_data.hpp:127
float TSignalValue
Definition: trace_data.hpp:76
TSignalValue GetMax(EChannel signal) const
Definition: trace_data.cpp:277
TValues m_ASig
Definition: trace_data.hpp:132
TConfidence m_MaxConfidence
Definition: trace_data.hpp:137
TValues m_TSig
Definition: trace_data.hpp:134
TValues & GetValues(EChannel signal)
Definition: trace_data.cpp:243
TConfidence GetMaxConfidence() const
Definition: trace_data.cpp:272
vector< TConfidence > m_Confs
Definition: trace_data.hpp:130
TSignedSeqPos m_From
Definition: trace_data.hpp:126
TPositions & GetPositions()
Definition: trace_data.hpp:179
TValues m_GSig
Definition: trace_data.hpp:135
vector< TFloatSeqPos > TPositions
Definition: trace_data.hpp:78
void CalculateMax()
Definition: trace_data.cpp:255
vector< TSignalValue > TValues
Definition: trace_data.hpp:79
bool m_bNegative
Definition: trace_data.hpp:128
TSignedSeqPos GetSeqLength() const
Definition: trace_data.hpp:155
TPositions m_Positions
Definition: trace_data.hpp:131
TSignalValue m_MaxT
Definition: trace_data.hpp:140
void SetConfidence(TSignedSeqPos pos, TConfidence conf)
Definition: trace_data.hpp:167
const_iterator end() const
Definition: map.hpp:152
bool empty() const
Definition: map.hpp:149
const_iterator find(const key_type &key) const
Definition: map.hpp:153
Include a standard set of the NCBI C++ Toolkit most basic headers.
#define A(i)
Definition: ecp_curves.c:948
int TSignedSeqPos
Type for signed sequence position.
Definition: ncbimisc.hpp:887
string
Definition: cgiapp.hpp:687
#define NULL
Definition: ncbistd.hpp:225
#define NCBI_THROW(exception_class, err_code, message)
Generic macro to throw an exception, given the exception class, error code and message string.
Definition: ncbiexpt.hpp:704
static objects::SAnnotSelector GetAnnotSelector(TAnnotFlags flags=0)
request an annotation selector for a given type
Definition: utils.cpp:167
@ eUnknown
Definition: app_popup.hpp:72
SAnnotSelector & SetSearchExternal(const CTSE_Handle &tse)
Set all flags for searching standard GenBank external annotations.
const CSeq_graph & GetOriginalGraph(void) const
Get original graph with unmapped location/product.
Definition: graph_ci.hpp:70
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
static string IntToString(int value, TNumToStringFlags flags=0, int base=10)
Convert int to string.
Definition: ncbistr.hpp:5083
const TTag & GetTag(void) const
Get the Tag member data.
Definition: Dbtag_.hpp:267
bool IsId(void) const
Check if variant Id is selected.
Definition: Object_id_.hpp:264
const TDb & GetDb(void) const
Get the Db member data.
Definition: Dbtag_.hpp:220
TId GetId(void) const
Get the variant data.
Definition: Object_id_.hpp:270
bool IsGeneral(void) const
Check if variant General is selected.
Definition: Seq_id_.hpp:877
const TGeneral & GetGeneral(void) const
Get the variant data.
Definition: Seq_id_.cpp:369
vector< char > TValues
Definition: Byte_graph_.hpp:89
const TGraph & GetGraph(void) const
Get the Graph member data.
const TTitle & GetTitle(void) const
Get the Title member data.
Definition: Seq_graph_.hpp:775
bool CanGetTitle(void) const
Check if it is safe to call GetTitle method.
Definition: Seq_graph_.hpp:769
const TByte & GetByte(void) const
Get the variant data.
Definition: Seq_graph_.cpp:153
bool CanGetLoc(void) const
Check if it is safe to call GetLoc method.
Definition: Seq_graph_.hpp:863
const TValues & GetValues(void) const
Get the Values member data.
E_Choice Which(void) const
Which variant is currently selected.
Definition: Seq_graph_.hpp:716
TA GetA(void) const
Get the A member data.
unsigned int
A callback function used to compare two keys in a database.
Definition: types.hpp:1210
int i
int len
T negative(T x_)
#define K
SAnnotSelector –.
Definition: type.c:6
#define _ASSERT
USING_SCOPE(objects)
Modified on Sat Dec 09 04:47:57 2023 by modify_doxy.py rev. 669887