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

Go to the SVN repository for this file.

1 /* $Id: seq_id_chunk_file.cpp 91383 2020-10-21 16:07:12Z 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: Cheinan Marks
27  *
28  * File Description: See chunk_file.hpp
29  */
30 
31 #include <ncbi_pch.hpp>
32 
33 #include <string>
34 #include <fstream>
35 #include <sstream>
36 #include <iomanip>
37 
38 #include <corelib/ncbifile.hpp>
39 #include <corelib/ncbistre.hpp>
40 
41 #include <serial/serial.hpp>
42 #include <serial/objostrasnb.hpp>
43 #include <serial/objistrasnb.hpp>
44 
45 #include <objects/seq/Bioseq.hpp>
46 
50 
53 
54 void CSeqIdChunkFile::OpenForWrite( const string & root_path )
55 {
56  if (!root_path.empty() && m_OpenFileRootPath != root_path) {
57  m_OpenFileRootPath = root_path;
58  if ( m_FileStream.is_open() ) {
59  m_FileStream.close();
60  }
61  string file_path = NASNCacheFileName::GetSeqIdChunk(root_path);
62  Reset( file_path );
63  }
64  if (!m_FileStream.is_open()) {
65  if( Exists() ) {
66  m_FileStream.open( GetPath().c_str(),
67  ios::out | ios::binary | ios::app );
68  } else {
69  m_FileStream.open( GetPath().c_str(), ios::out | ios::binary );
70  ERR_POST( Info << "SeqId chunk file " << GetPath()
71  << " does not exist. Creating." );
72  }
73  }
74 
75  if ( ! m_FileStream ) {
76  int saved_errno = NCBI_ERRNO_CODE_WRAPPER();
77  string error_string = "Unable to open a seqid chunk file for writing at " + GetPath() ;
78  error_string += " (errno = " + NStr::NumericToString( saved_errno ) + ": ";
79  error_string += string( NCBI_ERRNO_STR_WRAPPER( saved_errno ) + string( ")" ) );
80  ERR_POST( Error << error_string );
81  NCBI_THROW( CASNCacheException, eCantOpenChunkFile, error_string );
82  }
83 }
84 
85 
86 void CSeqIdChunkFile::OpenForRead( const string & root_path )
87 {
88  if (!root_path.empty() && m_OpenFileRootPath != root_path) {
89  m_OpenFileRootPath = root_path;
90  if ( m_FileStream.is_open() ) {
91  m_FileStream.close();
92  }
93  string file_path = NASNCacheFileName::GetSeqIdChunk(root_path);
94  Reset( file_path );
95  }
96 
97  if (! m_FileStream.is_open() ) {
98  if( Exists() ) {
99  m_FileStream.open( GetPath().c_str(), ios::in | ios::binary );
100  if ( ! m_FileStream ) {
101  int saved_errno = NCBI_ERRNO_CODE_WRAPPER();
102  string error_string = "Unable to open a seqid chunk file for reading at " + GetPath() ;
103  error_string += " (errno = " + NStr::NumericToString( saved_errno ) + ": ";
104  error_string += string( NCBI_ERRNO_STR_WRAPPER( saved_errno ) + string( ")" ) );
105  ERR_POST( Error << error_string );
106  NCBI_THROW( CASNCacheException, eCantOpenChunkFile, error_string );
107  }
108  } else {
109  string error_string = "Tried to read nonexistant seqid chunk file at "
110  + GetPath();
111  ERR_POST( Error << error_string );
112  NCBI_THROW( CASNCacheException, eCantOpenChunkFile, error_string );
113  }
114  }
115 }
116 
117 
118 void CSeqIdChunkFile::Write( const CBioseq::TId & seq_ids )
119 {
121  ITERATE(CBioseq::TId, it, seq_ids)
122  asn_stream << **it;
123  asn_stream.Flush();
124 }
125 
126 
127 void CSeqIdChunkFile::Write( const vector<CSeq_id_Handle> & seq_ids )
128 {
130  ITERATE(vector<CSeq_id_Handle>, it, seq_ids)
131  asn_stream << *it->GetSeqId();
132  asn_stream.Flush();
133 }
134 
135 
136 void CSeqIdChunkFile::RawWrite( const char * raw_seq_ids, size_t raw_seq_ids_size )
137 {
138  m_FileStream.write( raw_seq_ids, raw_seq_ids_size );
139 }
140 
141 
142 void CSeqIdChunkFile::Read( vector<CSeq_id_Handle> & target, streampos offset, size_t seq_ids_size )
143 {
144  m_Buffer.clear();
145  m_Buffer.resize( seq_ids_size );
146 
147  m_FileStream.seekg( offset );
148  m_FileStream.read( &m_Buffer[0], seq_ids_size );
149 
150  CObjectIStreamAsnBinary asn_stream( &m_Buffer[0], seq_ids_size );
151  CSeq_id id;
152  while((size_t)asn_stream.GetStreamPos() < seq_ids_size){
153  asn_stream >> id;
154  target.push_back(CSeq_id_Handle::GetHandle(id));
155  }
156 }
157 
158 
159 void CSeqIdChunkFile::RawRead( streampos offset, char * raw_seq_ids, size_t raw_seq_ids_size )
160 {
161  if (raw_seq_ids_size > static_cast< size_t >(std::numeric_limits< streamsize >::max())) {
163  "CSeqIdChunkFile::RawRead(): "
164  "requested a larger than supported number of bytes: " +
165  NStr::NumericToString(raw_seq_ids_size));
166  }
167  streamsize size(raw_seq_ids_size);
168  m_FileStream.seekg( offset );
169  m_FileStream.read( raw_seq_ids, size );
170  if ( m_FileStream.gcount() != size ) {
172  "CChunkFile::RawRead(): "
173  "failed to read specified number of bytes: got " +
174  NStr::Int8ToString(m_FileStream.gcount()) + ", expected " +
175  NStr::UInt8ToString(raw_seq_ids_size) +
176  " (offset=" + NStr::UInt8ToString(offset) + ")");
177  }
178 }
179 
180 
181 bool CSeqIdChunkFile::Append( const string & root_path, const CFile & input_seq_id_chunk_file,
182  Uint8 input_offset )
183 {
184  string file_path = NASNCacheFileName::GetSeqIdChunk(root_path);
185  Reset( file_path );
186 
187  m_FileStream.open( file_path.c_str(), ios::app | ios::out | ios::binary );
188  CNcbiIfstream input_seq_id_chunk_stream( input_seq_id_chunk_file.GetPath().c_str(), ios::in | ios::binary );
189  input_seq_id_chunk_stream.seekg( input_offset );
190  if (! NcbiStreamCopy( m_FileStream, input_seq_id_chunk_stream ) ) {
191  ERR_POST( Error << "Append of " << input_seq_id_chunk_file.GetPath() << " to " << file_path
192  << " at offset " << input_offset << " failed." );
193  return false;
194  }
195  return true;
196 }
197 
198 
200 
CFile –.
Definition: ncbifile.hpp:1604
CObjectIStreamAsnBinary –.
Definition: objistrasnb.hpp:59
CObjectOStreamAsnBinary –.
Definition: objostrasnb.hpp:58
CNcbiFstream m_FileStream
CSimpleBufferT< char > m_Buffer
void RawRead(std::streampos offset, char *raw_seq_ids, size_t raw_seq_ids_size)
bool Append(const string &root_path, const CFile &input_seq_id_chunk_file, Uint8 input_offset=0)
void OpenForWrite(const std::string &root_path="")
void OpenForRead(const std::string &root_path="")
void RawWrite(const char *raw_seq_ids, size_t raw_seq_ids_size)
void Read(vector< objects::CSeq_id_Handle > &target, std::streampos offset, size_t seq_ids_size)
void Write(const objects::CBioseq::TId &seq_ids)
std::string m_OpenFileRootPath
void resize(size_type new_size)
std::ofstream out("events_result.xml")
main entry point for tests
int offset
Definition: replacements.h:160
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
Definition: ncbimisc.hpp:815
string
Definition: cgiapp.hpp:687
#define ERR_POST(message)
Error posting with file, line number information but without error codes.
Definition: ncbidiag.hpp:186
void Error(CExceptionArgs_Base &args)
Definition: ncbiexpt.hpp:1197
#define NCBI_ERRNO_CODE_WRAPPER
Definition: ncbiexpt.hpp:1529
#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
#define NCBI_ERRNO_STR_WRAPPER
Definition: ncbiexpt.hpp:1530
void Info(CExceptionArgs_Base &args)
Definition: ncbiexpt.hpp:1185
void Reset(const string &path)
Reset path string.
Definition: ncbifile.cpp:298
const string & GetPath(void) const
Get entry path.
Definition: ncbifile.hpp:3910
virtual bool Exists(void) const
Check existence of file.
Definition: ncbifile.hpp:4038
@ eUnknown
Unknown type.
Definition: ncbifile.hpp:793
static CSeq_id_Handle GetHandle(const CSeq_id &id)
Normal way of getting a handle, works for any seq-id.
CNcbiStreampos GetStreamPos(void) const
Get the current stream position.
Definition: objistr.cpp:790
uint64_t Uint8
8-byte (64-bit) unsigned integer
Definition: ncbitype.h:105
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
IO_PREFIX::ifstream CNcbiIfstream
Portable alias for ifstream.
Definition: ncbistre.hpp:439
bool NcbiStreamCopy(CNcbiOstream &os, CNcbiIstream &is)
Copy the entire contents of stream "is" to stream "os".
Definition: ncbistre.cpp:211
static string Int8ToString(Int8 value, TNumToStringFlags flags=0, int base=10)
Convert Int8 to string.
Definition: ncbistr.hpp:5159
static enable_if< is_arithmetic< TNumeric >::value||is_convertible< TNumeric, Int8 >::value, string >::type NumericToString(TNumeric value, TNumToStringFlags flags=0, int base=10)
Convert numeric value to string.
Definition: ncbistr.hpp:673
static string UInt8ToString(Uint8 value, TNumToStringFlags flags=0, int base=10)
Convert UInt8 to string.
Definition: ncbistr.hpp:5168
list< CRef< CSeq_id > > TId
Definition: Bioseq_.hpp:94
string GetSeqIdChunk()
Definition: file_names.hpp:54
const struct ncbi::grid::netcache::search::fields::SIZE size
Defines classes: CDirEntry, CFile, CDir, CSymLink, CMemoryFile, CFileUtil, CFileLock,...
NCBI C++ stream class wrappers for triggering between "new" and "old" C++ stream libraries.
T max(T x_, T y_)
std::istream & in(std::istream &in_, double &x_)
USING_SCOPE(objects)
Modified on Wed Apr 24 14:18:52 2024 by modify_doxy.py rev. 669887