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

Go to the SVN repository for this file.

1 /* $Id: dense_hit.cpp 46044 2021-01-21 17:33:26Z grichenk $
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 
32 #include <ncbi_pch.hpp>
33 
35 
38 
41 
42 
43 ///////////////////////////////////////////////////////////////////////////////
44 /// CDenseSegHit
45 
46 bool CDenseSegHit::HasAlignment(const objects::CDense_seg& denseg,
47  size_t q_index, size_t s_index,
48  TDirection dir)
49 {
50  // check whether orientation of the specified rows matches "dir"
51  bool reversed = false; // by default
52  if(denseg.IsSetStrands()) {
53  bool q_minus = denseg.GetSeqStrand((CDense_seg::TDim)q_index) == eNa_strand_minus;
54  bool s_minus = denseg.GetSeqStrand((CDense_seg::TDim)s_index) == eNa_strand_minus;
55  reversed = (q_minus != s_minus);
56  }
57 
58  if((reversed && dir == CAlnUserOptions::eDirect) ||
59  (! reversed && dir == CAlnUserOptions::eReverse)) {
60  return false; // this alignment does not qualify
61  }
62 
63  // check whether do q_index and s_index define a pairwise alignment -
64  // look for a column that has non-negative starts in both Q and S rows
65  CDense_seg::TDim dim = denseg.GetDim();
66  CDense_seg::TNumseg n_seg = denseg.GetNumseg();
67  const CDense_seg::TStarts& starts = denseg.GetStarts();
68 
69  for( CDense_seg::TNumseg seg = 0; seg < n_seg; seg++ ) {
70  CDense_seg::TDim offset = seg * dim;
71  TSignedSeqPos q_start = starts[q_index + offset];
72  TSignedSeqPos s_start = starts[s_index + offset];
73  if(q_start >= 0 && s_start >=0 ) {
74  return true;
75  }
76  }
77  return false;
78 }
79 
80 
81 CDenseSegHit::CDenseSegHit(const objects::CSeq_align& align, int q_index, int s_index)
82 : m_SeqAlign(&align),
83  m_QueryIndex(q_index),
84  m_SubjectIndex(s_index)
85 {
86  _ASSERT(align.GetSegs().IsDenseg());
87  const CDense_seg& denseg = align.GetSegs().GetDenseg();
88 
89  /// using Alignment functions
90  unique_ptr<SAlignedSeq> aln_seq;
92 
93  _ASSERT(aln_seq.get());
94  const SAlignedSeq::TAlignColl& coll = *aln_seq->m_AlignColl;
95 
96  ITERATE(SAlignedSeq::TAlignColl, it, coll) {
97  const SAlignedSeq::TAlignRange& range = *it;
98 
99  TRange q_r(range.GetFirstFrom(), range.GetFirstTo());
100  TRange s_r(range.GetSecondFrom(), range.GetSecondTo());
101  ENa_strand q_strand = eNa_strand_plus;
102  ENa_strand s_strand = range.IsReversed() ? eNa_strand_minus : eNa_strand_plus;
103  CDenseSegHitElement* elem = new CDenseSegHitElement(*this, q_r, s_r, q_strand, s_strand);
104 
105  m_Elements.push_back(elem);
106  }
107 }
108 
109 
111 {
112  for(size_t i = 0; i < m_Elements.size(); i++) {
113  delete m_Elements[i];
114  }
115 }
116 
117 
119 {
120  return m_Elements.size();
121 }
122 
123 
124 const IHitElement& CDenseSegHit::GetElem(TDim elem_index) const
125 {
126  _ASSERT(size_t(elem_index) < m_Elements.size());
127  return *m_Elements[elem_index];
128 }
129 
130 
131 double CDenseSegHit::GetScoreValue(const string& score_name) const
132 {
133  const objects::CSeq_align::TScore& scores = m_SeqAlign->GetScore();
134  ITERATE(objects::CSeq_align::TScore, itS, scores) {
135  const objects::CScore& score = **itS;
136  _ASSERT(score.CanGetId());
137 
138  if(score.GetId().GetStr() == score_name) { // Match
139  const objects::CScore::TValue& val = score.GetValue();
140  switch(val.Which()) {
141  case objects::CScore::TValue::e_Real: return val.GetReal(); break;
142  case objects::CScore::TValue::e_Int: return val.GetInt(); break;
143  default: _ASSERT(false);
144  }
145  }
146  }
147  _ASSERT(false);
148  return -1;
149 }
150 
151 
152 const objects::CSeq_align* CDenseSegHit::GetSeqAlign() const
153 {
154  return m_SeqAlign;
155 }
156 
157 
158 ///////////////////////////////////////////////////////////////////////////////
159 ///
161 : m_Hit(NULL)
162 {
163 }
164 
166 {
167 }
168 
169 
171  const TRange& q_r, const TRange& s_r,
172  ENa_strand q_strand,
173  ENa_strand s_strand)
174 : m_Hit(&hit),
175  m_QueryRange(q_r),
176  m_SubjectRange(s_r),
177  m_QueryStrand(q_strand),
178  m_SubjectStrand(s_strand)
179 {
180 
181 }
182 
183 
185 {
186  return *m_Hit;
187 }
188 
189 
191 {
192  return m_QueryRange.GetFrom();
193 }
194 
195 
197 {
198  return m_SubjectRange.GetFrom();
199 }
200 
201 
203 {
204  return m_QueryRange.GetLength();
205 }
206 
207 
209 {
210  return m_SubjectRange.GetLength();
211 }
212 
213 
215 {
216  return m_QueryStrand;
217 }
218 
219 
221 {
222  return m_SubjectStrand;
223 }
224 
225 
226 
CAlignRange Represents an element of pairwise alignment of two sequences.
Definition: align_range.hpp:63
EDirection
Row direction flags.
@ eDirect
Use only sequences whose strand is the same as that of the anchor.
@ eReverse
Use only sequences whose strand is opposite to that of the anchor.
CDenseSegHitElement - IHitElement implementation for CDense_seg-based alignments.
Definition: dense_hit.hpp:58
virtual TSignedSeqPos GetSubjectStart() const
Definition: dense_hit.cpp:196
virtual objects::ENa_strand GetQueryStrand() const
Definition: dense_hit.cpp:214
objects::ENa_strand m_SubjectStrand
Definition: dense_hit.hpp:83
virtual ~CDenseSegHitElement()
Definition: dense_hit.cpp:165
virtual TSeqPos GetQueryLength() const
Definition: dense_hit.cpp:202
virtual const IHit & GetHit() const
Definition: dense_hit.cpp:184
virtual TSeqPos GetSubjectLength() const
Definition: dense_hit.cpp:208
const CDenseSegHit * m_Hit
Definition: dense_hit.hpp:79
virtual TSignedSeqPos GetQueryStart() const
Definition: dense_hit.cpp:190
virtual objects::ENa_strand GetSubjectStrand() const
Definition: dense_hit.cpp:220
objects::ENa_strand m_QueryStrand
Definition: dense_hit.hpp:82
Wraps a CSeq_align containing CDense_seg and provides a simple API for interpreting it as a pairwise ...
Definition: dense_hit.hpp:93
TDim m_SubjectIndex
Definition: dense_hit.hpp:118
virtual const objects::CSeq_align * GetSeqAlign() const
Definition: dense_hit.cpp:152
virtual ~CDenseSegHit()
Definition: dense_hit.cpp:110
static bool HasAlignment(const objects::CDense_seg &denseg, size_t q_index, size_t s_index, TDirection dir)
CDenseSegHit.
Definition: dense_hit.cpp:46
CDenseSegHit(const objects::CSeq_align &align, int q_index, int s_index)
Definition: dense_hit.cpp:81
virtual TDim GetElemsCount() const
Definition: dense_hit.cpp:118
virtual double GetScoreValue(const string &score_name) const
Definition: dense_hit.cpp:131
virtual const IHitElement & GetElem(TDim elem_index) const
Definition: dense_hit.cpp:124
TElements m_Elements
Definition: dense_hit.hpp:119
const objects::CSeq_align * m_SeqAlign
Definition: dense_hit.hpp:116
TDim m_QueryIndex
Definition: dense_hit.hpp:117
IHitElement.
Definition: hit.hpp:50
IHit.
Definition: hit.hpp:69
IHitElement::TDim TDim
Definition: hit.hpp:71
USING_SCOPE(ncbi::objects)
int offset
Definition: replacements.h:160
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
int TSignedSeqPos
Type for signed sequence position.
Definition: ncbimisc.hpp:887
#define NULL
Definition: ncbistd.hpp:225
position_type GetLength(void) const
Definition: range.hpp:158
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
TFrom GetFrom(void) const
Get the From member data.
Definition: Range_.hpp:222
vector< TSignedSeqPos > TStarts
Definition: Dense_seg_.hpp:107
ENa_strand
strand of nucleic acid
Definition: Na_strand_.hpp:64
@ eNa_strand_plus
Definition: Na_strand_.hpp:66
@ eNa_strand_minus
Definition: Na_strand_.hpp:67
int i
range(_Ty, _Ty) -> range< _Ty >
SAlignedSeq * CreateAlignRow(const objects::CSparse_align &align, bool master_first)
Helper functions used by Converters.
#define _ASSERT
Modified on Fri Sep 20 14:57:54 2024 by modify_doxy.py rev. 669887