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

Go to the SVN repository for this file.

1 /* $Id: seq_masker_ostat.cpp 98105 2022-09-29 00:28:26Z morgulis $
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  * Author: Aleksandr Morgulis
27  *
28  * File Description:
29  * Implementation of CSeqMaskerUStat class.
30  *
31  */
32 
33 #include <ncbi_pch.hpp>
34 
36 
37 #include <sstream>
38 
40 
41 #define STAT_ALGO_VER_MAJOR 1
42 #define STAT_ALGO_VER_MINOR 0
43 #define STAT_ALGO_VER_PATCH 0
44 
45 //------------------------------------------------------------------------------
47  "windowmasker-statistics-algorithm-version";
48 
49 
50 //------------------------------------------------------------------------------
52  STAT_ALGO_COMPONENT_NAME,
56 );
57 
58 //------------------------------------------------------------------------------
60 {
61  switch( GetErrCode() )
62  {
63  case eBadState: return "bad state";
64  default: return CException::GetErrCodeString();
65  }
66 }
67 
68 //------------------------------------------------------------------------------
70  std::ostringstream os;
71  Uint4 t_low( pvalues[0] == 0 ? 1 : pvalues[0] ), t_high( pvalues[3] );
72  os << "##parameters:unit=" << (Uint4)unit_size << ' '
73  << "t_low=" << t_low << ' ' << "t_high=" << t_high;
74  return os.str();
75 }
76 
77 //------------------------------------------------------------------------------
78 void CSeqMaskerOstat::WriteBinMetaData( std::ostream & os ) const {
79  Uint4 sz( 0 );
80  string s1( "##" );
81  s1 += GetStatFmtVersion().Print();
82  sz += s1.size() + 1;
83  string s2( "##" );
84  s2 += fmt_gen_algo_ver.Print();
85  sz += s2.size() + 1;
86  string s3( FormatParameters() ),
87  s4;
88  sz += s3.size() + 1;
89 
90  if( !metadata.empty() ) {
91  s4 = string( "##note:" ) + metadata;
92  sz += s4.size() + 1;
93  }
94 
95  std::string th_formatted;
96 
97  if( !count_map.empty() )
98  {
99  std::ostringstream oss;
100  oss << "##pct: " << max_count;
101  for( size_t i( 0 ); i <= max_count; ++i ) oss << ' ' << count_map[i];
102  th_formatted = oss.str();
103  sz += th_formatted.size() + 1;
104  }
105 
106  char zero( 0 );
107  os.write( (char *)&sz, sizeof( sz ) );
108  os.write( s1.c_str(), s1.size() );
109  os.write( &zero, 1 );
110  os.write( s2.c_str(), s2.size() );
111  os.write( &zero, 1 );
112  os.write( s3.c_str(), s3.size() );
113  os.write( &zero, 1 );
114 
115  if( !s4.empty() ) {
116  os.write( s4.c_str(), s4.size() );
117  os.write( &zero, 1 );
118  }
119 
120  if( !th_formatted.empty() )
121  {
122  os.write( th_formatted.c_str(), th_formatted.size() );
123  os.write( &zero, 1 );
124  }
125 }
126 
127 //------------------------------------------------------------------------------
129  std::ostringstream os;
130  os << "##" << GetStatFmtVersion().Print() << endl;
131  os << "##" << fmt_gen_algo_ver.Print() << endl;
132  os << FormatParameters() << endl;
133  if( !metadata.empty() ) os << "##note:" << metadata << endl;
134 
135  if( !count_map.empty() )
136  {
137  for( size_t i( 0 ); i <= max_count; ++i )
138  {
139  os << "##pct: " << i << ' ' << count_map[i] << endl;
140  }
141  }
142 
143  return os.str();
144 }
145 
146 //------------------------------------------------------------------------------
148 {
149  if( state != start )
150  {
151  CNcbiOstrstream ostr;
152  ostr << "can not set unit size in state " << state;
153  string s = CNcbiOstrstreamToString(ostr);
154  NCBI_THROW( CSeqMaskerOstatException, eBadState, s );
155  }
156 
157  doSetUnitSize( us );
158  state = ulen;
159 }
160 
161 //------------------------------------------------------------------------------
163 {
164  if( state != ulen && state != udata )
165  {
166  CNcbiOstrstream ostr;
167  ostr << "can not set unit count data in state " << state;
168  string s = CNcbiOstrstreamToString(ostr);
169  NCBI_THROW( CSeqMaskerOstatException, eBadState, s );
170  }
171 
172  doSetUnitCount( unit, count );
173  state = udata;
174 }
175 
176 //------------------------------------------------------------------------------
177 void CSeqMaskerOstat::setParam( const string & name, Uint4 value )
178 {
179  if( state != udata && state != thres && state != ulen)
180  {
181  CNcbiOstrstream ostr;
182  ostr << "can not set masking parameters in state " << state;
183  string s = CNcbiOstrstreamToString(ostr);
184  NCBI_THROW( CSeqMaskerOstatException, eBadState, s );
185  }
186 
187  doSetParam( name, value );
188  state = thres;
189 }
190 
191 //------------------------------------------------------------------------------
193 {
194  if( state != udata && state != thres )
195  {
196  CNcbiOstrstream ostr;
197  ostr << "can not finalize data structure in state " << state;
198  string s = CNcbiOstrstreamToString(ostr);
199  NCBI_THROW( CSeqMaskerOstatException, eBadState, s );
200  }
201 
202  state = final;
203  doFinalize();
204 }
205 
CNcbiOstrstreamToString class helps convert CNcbiOstrstream to a string Sample usage:
Definition: ncbistre.hpp:802
Exceptions that CSeqMaskerOstat can throw.
virtual const char * GetErrCodeString() const override
Get a description string for this exception.
@ eBadState
Operation can not be performed in the current state.
static CSeqMaskerVersion StatAlgoVersion
Version of the statistics generation algorithm.
static char const * STAT_ALGO_COMPONENT_NAME
virtual CSeqMaskerVersion const & GetStatFmtVersion() const =0
Get actual counts format version.
std::vector< double > count_map
string FormatParameters() const
Format algorithm parameters into a string.
Uint1 unit_size
unit size
enum CSeqMaskerOstat::@33 state
string metadata
metadata string
virtual void doSetUnitSize(Uint4 us)
Uint4 max_count
information about counts and corresponding pct cutoffs
void setUnitCount(Uint4 unit, Uint4 count)
Add count value for a particular unit.
void WriteBinMetaData(std::ostream &os) const
Write metadata in binary format.
CSeqMaskerVersion fmt_gen_algo_ver
version of the algorithm used to generate counts
void finalize()
Perform any final tasks required to generate unit counts in the particular format.
void setParam(const string &name, Uint4 value)
Set a value of a WindowMasker parameter.
virtual void doSetParam(const string &, Uint4)
void setUnitSize(Uint1 us)
Set the unit size value.
string FormatMetaData() const
Combine version data and metadata into a single string.
virtual void doFinalize()
virtual void doSetUnitCount(Uint4, Uint4)=0
virtual std::string Print() const
Print version information.
string
Definition: cgiapp.hpp:687
TErrCode GetErrCode(void) const
Get error code.
Definition: ncbiexpt.cpp:453
#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
virtual const char * GetErrCodeString(void) const
Get error code interpreted as text.
Definition: ncbiexpt.cpp:444
uint8_t Uint1
1-byte (8-bit) unsigned integer
Definition: ncbitype.h:99
uint32_t Uint4
4-byte (32-bit) unsigned integer
Definition: ncbitype.h:103
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
int i
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1227
#define STAT_ALGO_VER_PATCH
#define STAT_ALGO_VER_MAJOR
#define STAT_ALGO_VER_MINOR
Modified on Tue May 21 10:58:55 2024 by modify_doxy.py rev. 669887