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

Go to the SVN repository for this file.

1 /* $Id: alignment_quality_score.cpp 43687 2019-08-14 17:03:01Z katargir $
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: Liangshou Wu
27  *
28  * File Description:
29  *
30  */
31 
32 #include <ncbi_pch.hpp>
33 #include <objmgr/graph_ci.hpp>
38 
40 #include <gui/objutils/utils.hpp>
41 
44 
45 ///////////////////////////////////////////////////////////////////////////////
46 /// CAlignOhterScore
47 ///
48 
50 {
51  if (m_AlnInfo) {
52  // try aligned sequence first
53  x_RetrieveQualityMap(1 - m_AlnInfo->m_Anchor, true);
54 
55  if ( !HasScores() ) {
56  // maybe the anchor is the short read sequence
57  x_RetrieveQualityMap(m_AlnInfo->m_Anchor, false);
58  }
59 
60  // We don't need it anymore, release it.
61  m_AlnInfo.Reset();
62  }
63 }
64 
65 
67 {
68  return !m_ScoreColl.empty();
69 }
70 
71 
73 {
75 }
76 
77 
80 {
82  int idx = (int)(score / 5);
83  switch(idx) {
84  case 0:
85  case 1:
86  return CRgbaColor(0.855f, 0.482f, 0.141f);
87  case 2:
88  case 3:
89  return CRgbaColor(0.855f, 0.659f, 0.141f);
90  case 4:
91  return CRgbaColor(0.855f, 0.835f, 0.141f);
92  case 5:
93  return CRgbaColor(0.514f, 0.855f, 0.141f);
94  case 6:
95  return CRgbaColor(0.16f, 0.855f, 0.141f);
96  case 7:
97  return CRgbaColor(0.141f, 0.855f, 0.482f);
98  default:
99  // score >= 40
100  return CRgbaColor(0.141f, 0.855f, 0.788f);
101  }
102  }
103 
104  _ASSERT(false); // shouldn't get to here
105  return CRgbaColor(0.0f, 0.0f, 0.0f);
106 }
107 
108 
110 {
111  return true;
112 }
113 
114 
116 {
118 }
119 
120 
122 {
123  return true;
124 }
125 
126 
128 {
129  return GetScoreName();
130 }
131 
132 
134 {
135  static const string kScoreName = "Quality score";
136  return kScoreName;
137 }
138 
139 
140 
141 template <typename ValType>
143  const TSeqRange& range,
144  const CSeq_graph& gr,
145  const vector<ValType>& vec)
146 {
147  if (vec.empty()) return;
148 
149  float a = (float)(gr.IsSetA() ? gr.GetA() : 1.0);
150  float b = (float)(gr.IsSetB() ? gr.GetB() : 0.0);
151 
152  TSeqPos comp = gr.IsSetComp() ? gr.GetComp() : 1;
153  if (comp == 0) {
154  return;
155  }
156 
158  for (CSeq_loc_CI iter(gr.GetLoc()); iter; ++iter) {
159  ranges += iter.GetRange();
160  }
161 
162  typename vector<ValType>::const_iterator iter_gr = vec.begin();
163  TSeqPos pre_pos = -1;
164  score.SetFrom(ranges.GetFrom());
165  ITERATE (CRangeCollection<TSeqPos>, r_iter, ranges) {
166  const TSeqRange& r = *r_iter;
167  TSeqPos pos = r.GetFrom();
168  if (pos > pre_pos) {
169  // fill the gap
170  score.push_back(0, pos - pre_pos);
171  }
172 
173  // if the data type is char, we need to convert them into unsigned char
174  bool unsigned_char = false;
175  if (sizeof(ValType) == 1) unsigned_char = true;
176  unsigned char val;
177  while (iter_gr != vec.end() && pos < r.GetToOpen()) {
178  if (unsigned_char) {
179  val = (unsigned char)((*iter_gr) * a + b);
180  } else {
181  int val_i = (int)((*iter_gr) * a + b);
182  if (val_i > 255) val = 255;
183  else val = (unsigned char)val_i;
184  }
185  score.push_back(val, comp);
186  pos += comp;
187  ++iter_gr;
188  }
189  pre_pos = pos;
190  }
191 }
192 
193 
195 {
196  CScope& scope = *m_AlnInfo->m_Scope;
197  const CSeq_align& aln = *m_AlnInfo->m_Align;
198  CBioseq_Handle handle = scope.GetBioseqHandle(aln.GetSeq_id(row));
199  if ( !handle ) {
200  return;
201  }
202 
204  handle.GetRangeSeq_loc(aln.GetSeqStart(row), aln.GetSeqStop(row)));
206  sel.AddUnnamedAnnots();
207  CGraph_CI graph_iter(scope, *loc, sel);
208  if (graph_iter && graph_iter.GetSize() == 1) {
209  const CMappedGraph& mapped_gr = *graph_iter;
210  CConstRef<CSeq_graph> seq_graph(&mapped_gr.GetMappedGraph());
211 
212  // remap the graph if needed
213  bool reverse = false;
214  if (remap) {
215  try {
216  CSeq_loc_Mapper mapper(aln, aln.GetSeq_id(1 - row));
217  reverse = aln.GetSeqStrand(0) != aln.GetSeqStrand(1);
218  seq_graph = mapper.Map(*seq_graph);
219  } catch (CAnnotMapperException&) {
220  return ;
221  }
222  }
223 
224  if (seq_graph) {
225  TSeqRange range = aln.GetSeqRange(m_AlnInfo->m_Anchor);
226  const CSeq_graph::TGraph& graph = seq_graph->GetGraph();
227  switch (graph.Which())
228  {
230  {{
231  const CByte_graph& b_graph = graph.GetByte();
232  if (reverse) {
233  CByte_graph::TValues values = b_graph.GetValues();
234  std::reverse(values.begin(), values.end());
235  s_AddGraphToMap(m_ScoreColl, range, *seq_graph, values);
236  } else {
237  s_AddGraphToMap(m_ScoreColl, range, *seq_graph, b_graph.GetValues());
238  }
239  break;
240  }}
242  {{
243  const CInt_graph& i_graph = graph.GetInt();
244  if (reverse) {
245  CInt_graph::TValues values = i_graph.GetValues();
246  std::reverse(values.begin(), values.end());
247  s_AddGraphToMap(m_ScoreColl, range, *seq_graph, values);
248  } else {
249  s_AddGraphToMap(m_ScoreColl, range, *seq_graph, i_graph.GetValues());
250  }
251  break;
252  }}
254  {{
255  const CReal_graph& r_graph = graph.GetReal();
256  if (reverse) {
257  CReal_graph::TValues values = r_graph.GetValues();
258  std::reverse(values.begin(), values.end());
259  s_AddGraphToMap(m_ScoreColl, range, *seq_graph, values);
260  } else {
261  s_AddGraphToMap(m_ScoreColl, range, *seq_graph, r_graph.GetValues());
262  }
263  break;
264  }}
265  default:
266  break;
267  }
268  }
269  }
270 }
271 
User-defined methods of the data storage class.
User-defined methods of the data storage class.
User-defined methods of the data storage class.
USING_SCOPE(objects)
void s_AddGraphToMap(CSGAlnQualityScore::TScoreColl &score, const TSeqRange &range, const CSeq_graph &gr, const vector< ValType > &vec)
Seq-loc and seq-align mapper exceptions.
size_t GetSize(void) const
CBioseq_Handle –.
CByte_graph –.
Definition: Byte_graph.hpp:66
CGraph_CI –.
Definition: graph_ci.hpp:234
CInt_graph –.
Definition: Int_graph.hpp:66
CMappedGraph –.
Definition: graph_ci.hpp:61
const_iterator begin() const
Definition: range_coll.hpp:82
position_type GetFrom() const
Definition: range_coll.hpp:120
CReal_graph –.
Definition: Real_graph.hpp:66
class CRgbaColor provides a simple abstraction for managing colors.
Definition: rgba_color.hpp:58
virtual const string & GetScoringMethod() const
virtual IAlnScoreIterator * GetScoreIterator(int row) const
void x_RetrieveQualityMap(IAlnExplorer::TNumrow row, bool remap)
virtual int GetSupportedColorTypes() const
virtual bool IsCacheable() const
static const string & GetScoreName()
virtual bool IsAverageable() const
virtual bool HasScores() const
virtual CRgbaColor GetColorForScore(float score, IScoringMethod::EColorType type) const
virtual void CalculateScores()
CAlignOhterScore.
CScope –.
Definition: scope.hpp:92
CRange< TSeqPos > GetSeqRange(TDim row) const
GetSeqRange NB: On a Spliced-seg, in case the product-type is protein, these only return the amin par...
Definition: Seq_align.cpp:153
TSeqPos GetSeqStop(TDim row) const
Definition: Seq_align.cpp:273
const CSeq_id & GetSeq_id(TDim row) const
Get seq-id (the first one if segments have different ids).
Definition: Seq_align.cpp:317
TSeqPos GetSeqStart(TDim row) const
Definition: Seq_align.cpp:252
ENa_strand GetSeqStrand(TDim row) const
Get strand (the first one if segments have different strands).
Definition: Seq_align.cpp:294
Seq-loc iterator class – iterates all intervals from a seq-loc in the correct order.
Definition: Seq_loc.hpp:453
CSeq_loc_Mapper –.
unsigned int TSeqPos
Type for sequence locations and lengths.
Definition: ncbimisc.hpp:875
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
Definition: ncbimisc.hpp:815
static objects::SAnnotSelector GetAnnotSelector(TAnnotFlags flags=0)
request an annotation selector for a given type
Definition: utils.cpp:167
void SetFrom(TSeqPos start)
void push_back(const attr_type &attr)
CRef< CSeq_loc > Map(const CSeq_loc &src_loc)
Map seq-loc.
CBioseq_Handle GetBioseqHandle(const CSeq_id &id)
Get bioseq handle by seq-id.
Definition: scope.cpp:95
CRef< CSeq_loc > GetRangeSeq_loc(TSeqPos start, TSeqPos stop, ENa_strand strand=eNa_strand_unknown) const
Return CSeq_loc referencing the given range and strand on the bioseq If start == 0,...
const CSeq_graph & GetMappedGraph(void) const
Graph mapped to the master sequence.
Definition: graph_ci.hpp:100
SAnnotSelector & AddUnnamedAnnots(void)
Add unnamed annots to set of annots names to look for.
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
bool IsSetComp(void) const
compression (residues/value) Check if a value has been assigned to Comp data member.
Definition: Seq_graph_.hpp:981
bool IsSetB(void) const
display = (a x value) + b Check if a value has been assigned to B data member.
vector< char > TValues
Definition: Byte_graph_.hpp:89
const TInt & GetInt(void) const
Get the variant data.
Definition: Seq_graph_.cpp:131
bool IsSetA(void) const
for scaling values Check if a value has been assigned to A data member.
const TGraph & GetGraph(void) const
Get the Graph member data.
const TValues & GetValues(void) const
Get the Values member data.
Definition: Int_graph_.hpp:425
TB GetB(void) const
Get the B member data.
const TByte & GetByte(void) const
Get the variant data.
Definition: Seq_graph_.cpp:153
vector< double > TValues
Definition: Real_graph_.hpp:88
const TReal & GetReal(void) const
Get the variant data.
Definition: Seq_graph_.cpp:109
vector< int > TValues
Definition: Int_graph_.hpp:88
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
const TValues & GetValues(void) const
Get the Values member data.
const TLoc & GetLoc(void) const
Get the Loc member data.
Definition: Seq_graph_.hpp:869
TA GetA(void) const
Get the A member data.
TComp GetComp(void) const
Get the Comp member data.
unsigned int
A callback function used to compare two keys in a database.
Definition: types.hpp:1210
range(_Ty, _Ty) -> range< _Ty >
unsigned int a
Definition: ncbi_localip.c:102
double r(size_t dimension_, const Int4 *score_, const double *prob_, double theta_)
SAnnotSelector –.
Definition: type.c:6
#define _ASSERT
Modified on Sat Dec 02 09:20:02 2023 by modify_doxy.py rev. 669887