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

Go to the SVN repository for this file.

1 /* $Id: fasta_exception.cpp 93579 2021-05-01 20:54:52Z stakhovv $
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: Michael Kornbluh, NCBI
27 *
28 * File Description:
29 * Exceptions for CFastaReader.
30 *
31 * ===========================================================================
32 */
33 #include <ncbi_pch.hpp>
34 
35 #include <corelib/ncbistr.hpp>
37 #include <corelib/ncbistre.hpp>
38 #include <functional>
39 #include <algorithm>
41 
42 using namespace std;
43 
46 
47 void CBadResiduesException::ReportExtra(ostream& out) const
48 {
49  if( empty() ) {
50  out << "No Bad Residues";
51  return;
52  }
53 
54  out << "Bad Residues = ";
55  if( m_BadResiduePositions.m_SeqId ) {
56  out << m_BadResiduePositions.m_SeqId->GetSeqIdString(true);
57  } else {
58  out << "Seq-id ::= NULL";
59  }
60  out << ", positions: ";
61  m_BadResiduePositions.ConvertBadIndexesToString( out );
62 }
63 
65 {
66  ITERATE(SBadResiduePositions::TBadIndexMap, new_line_iter, additionalBadIndexMap) {
67  const int lineNum = new_line_iter->first;
68  const vector<TSeqPos> & src_seqpos_vec = new_line_iter->second;
69 
70  if( src_seqpos_vec.empty() ) {
71  continue;
72  }
73 
74  vector<TSeqPos> & dest_seqpos_vec =
75  m_BadIndexMap[lineNum];
76  copy( src_seqpos_vec.begin(), src_seqpos_vec.end(),
77  back_inserter(dest_seqpos_vec) );
78  }
79 }
80 
82  CNcbiOstream & out,
83  unsigned int maxRanges ) const
84 {
85  const char *line_prefix = "";
86  unsigned int iRangesFound = 0;
87  ITERATE( SBadResiduePositions::TBadIndexMap, index_map_iter, m_BadIndexMap ) {
88  const int lineNum = index_map_iter->first;
89  const vector<TSeqPos> & badIndexesOnLine = index_map_iter->second;
90 
91  // assert that badIndexes is sorted in ascending order on every line
92  _ASSERT(adjacent_find(badIndexesOnLine.begin(), badIndexesOnLine.end(),
93  std::greater<int>()) == badIndexesOnLine.end() );
94 
95  typedef pair<TSeqPos, TSeqPos> TRange;
96  typedef vector<TRange> TRangeVec;
97 
98  TRangeVec rangesFound;
99 
100  ITERATE( vector<TSeqPos>, idx_iter, badIndexesOnLine ) {
101  const TSeqPos idx = *idx_iter;
102 
103  // first one
104  if( rangesFound.empty() ) {
105  rangesFound.push_back(TRange(idx, idx));
106  ++iRangesFound;
107  continue;
108  }
109 
110  const TSeqPos last_idx = rangesFound.back().second;
111  if( idx == (last_idx+1) ) {
112  // extend previous range
113  ++rangesFound.back().second;
114  continue;
115  }
116 
117  if( iRangesFound >= maxRanges ) {
118  break;
119  }
120 
121  // create new range
122  rangesFound.push_back(TRange(idx, idx));
123  ++iRangesFound;
124  }
125 
126  // turn the ranges found on this line into a string
127  out << line_prefix << "On line " << lineNum << ": ";
128  line_prefix = ", ";
129 
130  const char *pos_prefix = "";
131  for( unsigned int rng_idx = 0;
132  ( rng_idx < rangesFound.size() );
133  ++rng_idx )
134  {
135  out << pos_prefix;
136  const TRange &range = rangesFound[rng_idx];
137  out << (range.first + 1); // "+1" because 1-based for user
138  if( range.first != range.second ) {
139  out << "-" << (range.second + 1); // "+1" because 1-based for user
140  }
141 
142  pos_prefix = ", ";
143  }
144  if (iRangesFound > maxRanges) {
145  out << ", and more";
146  return;
147  }
148  }
149 }
150 
151 
CLocalRange< TOffset > TRange
define for the fundamental building block of sequence ranges
Definition: base.hpp:115
The NCBI C++ standard methods for dealing with std::string.
std::ofstream out("events_result.xml")
main entry point for tests
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
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define END_SCOPE(ns)
End the previously defined scope.
Definition: ncbistl.hpp:75
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
#define BEGIN_SCOPE(ns)
Define a new scope.
Definition: ncbistl.hpp:72
IO_PREFIX::ostream CNcbiOstream
Portable alias for ostream.
Definition: ncbistre.hpp:149
range(_Ty, _Ty) -> range< _Ty >
constexpr bool empty(list< Ts... >) noexcept
NCBI C++ stream class wrappers for triggering between "new" and "old" C++ stream libraries.
void copy(Njn::Matrix< S > *matrix_, const Njn::Matrix< T > &matrix0_)
Definition: njn_matrix.hpp:613
vector< CRef< CSeq_interval > > TRangeVec
Definition: orf.cpp:50
void AddBadIndexMap(const TBadIndexMap &additionalBadIndexMap)
void ConvertBadIndexesToString(CNcbiOstream &out, unsigned int maxRanges=1000) const
#define _ASSERT
Modified on Tue Apr 23 07:41:05 2024 by modify_doxy.py rev. 669887