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

Go to the SVN repository for this file.

1 /*
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: Andrei Shkeda
27  *
28  * File Description:
29  *
30  */
31 
32 #include <ncbi_pch.hpp>
33 
35 
37 #include <gui/objutils/utils.hpp>
38 
39 // for graph job
40 #include <objmgr/seq_table_ci.hpp>
41 #include <objmgr/graph_ci.hpp>
42 #include <objmgr/seqdesc_ci.hpp>
43 #include <objmgr/table_field.hpp>
48 
53 
54 
55 #include <gui/opengl/glpoint.hpp>
56 
59 
61  { "Phrap Quality", CSGTraceData::eA - 1 },
62  { "Phred Quality", CSGTraceData::eA - 1 },
63  { "A-channel Trace Chromatogram", CSGTraceData::eA },
64  { "C-channel Trace Chromatogram", CSGTraceData::eC },
65  { "T-channel Trace Chromatogram", CSGTraceData::eT },
66  { "G-channel Trace Chromatogram", CSGTraceData::eG }
67 };
68 
69 
70 CSGTraceGraphDS::CSGTraceGraphDS(objects::CScope& scope, const objects::CSeq_id& id)
71  : CSGGenBankDS(scope, id)
72 {
73 }
74 
75 
76 
78 {
79  switch (m_Status) {
80  case eNoData:
81  return false;
82  case eHasData:
83  return true;
84  case eUnknown: {
85  CConstRef<CSeq_id> id = m_Handle.GetSeqId();
86  if (id && id->IsGeneral() && id->GetGeneral().GetTag().IsId() &&
87  (id->GetGeneral().GetDb() == "ti"
88  || id->GetGeneral().GetDb() == "TRACE")) {
90  return true;
91  }
92  m_Status = eNoData;
93  break;
94  }}
95  return false;
96 }
97 
99 {
100  if (m_Data)
101  return;
102 
103  CRef<CSGTraceData> p_data;
104 
105  // first - look for CSeq_graph-s with familiar titles
106  const CSeq_graph* raw_graphs[5] = { 0, 0, 0, 0, 0 };
107 
108  CBioseq_Handle chgr_handle; // empty for now
109 
110  CConstRef<CSeq_id> id = m_Handle.GetSeqId();
111  if (id && id->IsGeneral() && id->GetGeneral().GetTag().IsId() &&
112  (id->GetGeneral().GetDb() == "ti" || id->GetGeneral().GetDb() == "TRACE")) {
113  string sid = string("gnl|TRACE_CHGR|") + NStr::IntToString(id->GetGeneral().GetTag().GetId());
114  CSeq_id trace_chgr_id(sid);
115 
116  // load satellite sequence with chromatograms into Object Manager
117  chgr_handle = m_Handle.GetScope().GetBioseqHandle(trace_chgr_id);
118  }
119 
120  // now using CGraph_CI(handle) we are able to iterate quality and chromatogram graphs
121  if (chgr_handle) {
122  CGraph_CI graph_iter(m_Handle);
123  while (graph_iter) {
124  const CSeq_graph& graph = graph_iter->GetOriginalGraph();
125  if (graph.CanGetTitle() && graph.CanGetLoc()) {
126  string sTitle = graph.GetTitle();
127 
128  auto it = sm_TitleToType.find(sTitle);
129  if (it != sm_TitleToType.end()) {
131 
132  if (sm_TitleToType.find(sTitle) != sm_TitleToType.end()) {
133  int type = it->second;
134  raw_graphs[type + 1] = &graph; // store pointer to graph
135  }
136  }
137  }
138  ++graph_iter;
139  }
140  }
141 
142  if (chgr_handle) {
144  sel.SetSearchExternal(chgr_handle);
145  for (CGraph_CI graph_iter(chgr_handle, sel); graph_iter; ++graph_iter) {
146  const CSeq_graph& graph = graph_iter->GetOriginalGraph();
147  if (graph.CanGetTitle() && graph.CanGetLoc()) {
148  string sTitle = graph.GetTitle();
149 
150  auto it = sm_TitleToType.find(sTitle);
151  if (it != sm_TitleToType.end()) {
153  //_TRACE("\nTitle: "<< sTitle);
154 
155  if (sm_TitleToType.find(sTitle) != sm_TitleToType.end()) {
156  int type = it->second;
157  raw_graphs[type + 1] = &graph; // store pointer to graph
158  }
159  }
160  }
161  //++graph_iter;
162  }
163  }
164 
165  // now check what we've got
166  int len = 0, sig_len = 0;
167 
168  bool b_conf = raw_graphs[0] != NULL;
169  if (b_conf) { // if we have confidence graph len = conf len
170  const CSeq_graph::C_Graph& gr = raw_graphs[0]->GetGraph();
171  const CByte_graph::TValues& values = gr.GetByte().GetValues();
172  len = (int)values.size();
173  }
174 
175  int i = 1;
176  for (; i < 5 && raw_graphs[i] == NULL; i++) {};
177  bool b_ch = (i < 5);
178  if (b_ch) { // if we have chromatograms
179  double A = raw_graphs[i]->GetA();
180  const CSeq_graph::C_Graph& gr = raw_graphs[i]->GetGraph();
181  const CByte_graph::TValues& values = gr.GetByte().GetValues();
182  sig_len = (int)values.size();
183  if (len == 0) {
184  len = (int)(sig_len / A); // seq length of graph
185  }
186  }
187 
188  if (len > 0) { // we have at least one graph
189  p_data.Reset(new CSGTraceData());
190  p_data->Init(0, len - 1, sig_len);
191 
192  if (b_conf) { // copy confidence values to CSGTraceData
193  const CSeq_graph::C_Graph& gr = raw_graphs[0]->GetGraph();
194  const CByte_graph::TValues& values = gr.GetByte().GetValues();
195  for (i = 0; i < len; i++) {
196  p_data->SetConfidence(i, values[i]);
197  }
198  }
199  for (i = 1; i < 5 && b_ch; i++) {
200  bool b_calc_pos = false;
201  if (raw_graphs[i]) {
202  const CSeq_graph::C_Graph& gr = raw_graphs[i]->GetGraph();
203  const CByte_graph::TValues& values = gr.GetByte().GetValues();
205  CSGTraceData::TSignalValue K = A / 255;
206 
207  // calculate positions on sequnce for chromatogram samples
208  if (!b_calc_pos) {
209  CSGTraceData::TPositions& positions = p_data->GetPositions();
211  ((CSGTraceData::TFloatSeqPos) len) / sig_len;
212  for (int j = 0; j < sig_len; j++) {
213  positions[j] = K_pos * j;
214  }
215  b_calc_pos = true;
216  }
217  // copy chromatogram data
218  CSGTraceData::TValues& data_values = p_data->GetValues((CSGTraceData::EChannel) (i - 1));
219  for (int j = 0; j < sig_len; j++) {
220  data_values[j] = K * ((unsigned char)values[j]);
221  }
222  }
223  }
224  CSeqdesc_CI it(chgr_handle, CSeqdesc::e_Title);
225  if (it)
226  p_data->SetTitle(it->GetTitle());
227  }
228  m_Data = p_data;
229 }
230 
231 
232 ///////////////////////////////////////////////////////////////////////////////
233 /// CSGGraphDSType
234 
236 {
237  const CSeq_id& id = dynamic_cast<const CSeq_id&>(object.object.GetObject());
238  return new CSGTraceGraphDS(object.scope.GetObject(), id);
239 }
240 
241 
243 {
244  static string sid("seqgraphic_trace_graph_ds_type");
245  return sid;
246 }
247 
248 
250 {
251  static string slabel("Graphical View Trace Graph Data Source Type");
252  return slabel;
253 }
254 
255 
257 {
258  return false;
259 }
260 
261 
262 ////////////////////////////////////////////////////////////////////////////////
263 /// CSGTraceData
264 
265 void CSGTraceData::Init(TSignedSeqPos from, TSignedSeqPos to, int samples)
266 {
267  _ASSERT(to >= from && samples >= 0);
268  m_From = from;
269  m_To = to;
270  m_Confs.resize(GetSeqLength());
271 
272  m_Positions.resize(samples);
273  m_ASig.resize(samples);
274  m_CSig.resize(samples);
275  m_TSig.resize(samples);
276  m_GSig.resize(samples);
277 
278  m_bNegative = false;
279 }
280 
282 {
283  switch (channel) {
284  case eA: return m_ASig;
285  case eC: return m_CSig;
286  case eT: return m_TSig;
287  case eG: return m_GSig;
288  }
289  _ASSERT(false);
290  NCBI_THROW(CException, eUnknown, "unhandled channel in CSGTraceData");
291 }
292 
294 {
295  switch (channel) {
296  case eA: return m_ASig;
297  case eC: return m_CSig;
298  case eT: return m_TSig;
299  case eG: return m_GSig;
300  }
301  _ASSERT(false);
302  NCBI_THROW(CException, eUnknown, "unhandled channel in CSGTraceData");
303 }
304 
306 {
307  if (m_Confs.size()) {
308  vector<TConfidence>::const_iterator it = max_element(m_Confs.begin(), m_Confs.end());
309  m_MaxConfidence = *it;
310  } else m_MaxConfidence = 0.0f;
311 
312  if (m_ASig.size()) {
313  m_MaxA = *max_element(m_ASig.begin(), m_ASig.end());
314  m_MaxC = *max_element(m_CSig.begin(), m_CSig.end());
315  m_MaxT = *max_element(m_TSig.begin(), m_TSig.end());
316  m_MaxG = *max_element(m_GSig.begin(), m_GSig.end());
317  } else {
318  m_MaxA = m_MaxC = m_MaxT = m_MaxG = 0.0f;
319  }
320 }
321 
323 {
324  return m_MaxConfidence;
325 }
326 
328 {
329  switch (channel) {
330  case eA: return m_MaxA;
331  case eC: return m_MaxC;
332  case eT: return m_MaxT;
333  case eG: return m_MaxG;
334  }
335  _ASSERT(false);
336  NCBI_THROW(CException, eUnknown, "unhandled channel in CSGTraceData");
337 }
338 
339 
340 
User-defined methods of the data storage class.
User-defined methods of the data storage class.
User-defined methods of the data storage class.
CBioseq_Handle –.
CGraph_CI –.
Definition: graph_ci.hpp:234
objects::CBioseq_Handle m_Handle
CSGTraceData.
TValues & GetValues(EChannel signal)
vector< TSignalValue > TValues
TSignedSeqPos m_From
vector< TFloatSeqPos > TPositions
TConfidence GetMaxConfidence() const
TSignalValue m_MaxT
TPositions & GetPositions()
TSignalValue m_MaxA
TSignalValue GetMax(EChannel signal) const
void SetConfidence(TSignedSeqPos pos, TConfidence conf)
vector< TConfidence > m_Confs
TSignalValue m_MaxC
TSignalValue m_MaxG
void Init(TSignedSeqPos from, TSignedSeqPos to, int samples)
CSGTraceData.
TConfidence m_MaxConfidence
void SetTitle(const string &title)
TSignedSeqPos m_To
TSignedSeqPos GetSeqLength() const
TPositions m_Positions
virtual bool IsSharable() const
check if the data source can be shared.
virtual ISGDataSource * CreateDS(SConstScopedObject &object) const
create an instance of the layout track type using default settings.
virtual string GetExtensionIdentifier() const
returns the unique human-readable identifier for the extension the id should use lowercase letters se...
virtual string GetExtensionLabel() const
returns a displayable label for this extension ( please capitalize the key words - "My Extension" )
CSGTraceGraphDS.
CSGTraceGraphDS(objects::CScope &scope, const objects::CSeq_id &id)
bool HasData() const
CRef< CSGTraceData > m_Data
CSeqdesc_CI –.
Definition: seqdesc_ci.hpp:65
File Description:
const_iterator end() const
Definition: map.hpp:152
const_iterator find(const key_type &key) const
Definition: map.hpp:153
int TSignedSeqPos
Type for signed sequence position.
Definition: ncbimisc.hpp:887
string
Definition: cgiapp.hpp:690
#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:168
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
void Reset(void)
Reset reference object.
Definition: ncbiobj.hpp:773
#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:5078
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.
const TTitle & GetTitle(void) const
Get the variant data.
Definition: Seqdesc_.hpp:1032
@ e_Title
a title for this sequence
Definition: Seqdesc_.hpp:115
unsigned int
A callback function used to compare two keys in a database.
Definition: types.hpp:1210
int i
int len
#define A
SAnnotSelector –.
Definition: type.c:6
#define _ASSERT
USING_SCOPE(objects)
static map< string, int > sm_TitleToType
Modified on Fri Sep 20 14:57:43 2024 by modify_doxy.py rev. 669887