Nemo  2.4.2
Simulate forward-in-time genetic evolution in a spatially explicit, individual-based stochastic simulator
FileHandler Class Referenceabstract

Interface to handle file input/output for any SimComponent. More...

#include <filehandler.h>

+ Inheritance diagram for FileHandler:
+ Collaboration diagram for FileHandler:

Public Member Functions

 FileHandler (const char *ext)
 
virtual ~FileHandler ()
 
virtual void init ()
 Called by notifier during simulation setup, performs file checking. More...
 
virtual vector< string > ifExist ()
 Checks if any file associated with the current file name already exists on disk. More...
 
virtual void set (bool rpl_per, bool gen_per, int rpl_occ, int gen_occ, int rank, string path)
 Sets the hanlder parameters. More...
 
virtual void set_multi (bool rpl_per, bool gen_per, int rpl_occ, TMatrix *Occ, string path)
 
virtual void FHwrite ()=0
 Default behavior of the class, called by Handler::update(). More...
 
virtual void FHread (string &filename)=0
 Default input function. More...
 
virtual void update ()
 Updates the inner replicate and generation counters and calls FHwrite if needed by the the periodicity of the file. More...
 
Accessors
Metapopget_pop_ptr ()
 Returns the pointer to the current metapop through the FileServices interface. More...
 
void set_pop_ptr (Metapop *pop_ptr)
 
FileServicesget_service ()
 Returns pointer to the FileServices. More...
 
void set_service (FileServices *srv)
 
std::string & get_path ()
 
void set_path ()
 
std::string & get_extension ()
 
void set_extension (const char *ext)
 
std::string & get_filename ()
 Builds and returns the current file name depending on the periodicity of the file. More...
 
bool get_isInputHandler ()
 
void set_isInputHandler (bool val)
 
bool get_isReplicatePeriodic ()
 
void set_isReplicatePeriodic (bool val)
 
unsigned int get_ReplicateOccurrence ()
 
void set_ReplicateOccurrence (unsigned int val)
 
bool get_isGenerationPeriodic ()
 
void set_isGenerationPeriodic (bool val)
 
unsigned int get_GenerationOccurrence ()
 
void set_GenerationOccurrence (unsigned int val)
 
unsigned int get_ExecRank ()
 unused yet... More...
 
void set_ExecRank (int val)
 
TMatrixget_OccMatrix ()
 
void set_OccMatrix (TMatrix *occ)
 
bool get_isMasterExec ()
 
void set_isMasterExec (bool is)
 
- Public Member Functions inherited from Handler
virtual ~Handler ()
 

Protected Attributes

Metapop_pop
 Pointer to the current metapop, set during initialization within the init function. More...
 

Private Attributes

FileServices_service
 Link to the files manager. More...
 
bool _isMasterExec
 Flag telling if the file must be written by the master or the worker node. More...
 
bool _isInputHandler
 Writing mode flag. More...
 
bool _isReplicatePeriodic
 Flag telling if the file should be written each replicate. More...
 
bool _isGenerationPeriodic
 Flag setting the per generation periodicity behaviour of the file. More...
 
unsigned int _ReplicateOccurrence
 Tells every what replicate the file should be written. More...
 
unsigned int _GenerationOccurrence
 Tells every what generation the file should be written. More...
 
unsigned int _current_replicate
 The current replicate number of the simulation. More...
 
unsigned int _current_generation
 The current generation number of the simulation. More...
 
TMatrix _multipleOccurences
 Multiple occurences. More...
 
list< int >::const_iterator _genITER
 
list< int > _generations
 
unsigned int _ExecRank
 unused... More...
 
std::string _path
 File path as set during initialization. More...
 
std::string _extension
 File extension, should be specific to the implementation. More...
 
std::string _current_filename
 The current filename as set by FileHandler::get_filename(). More...
 

Detailed Description

Interface to handle file input/output for any SimComponent.

Stores the periodicity parameters and the file path and extension. The replicate file name is given by the FileServices. A file handler might be set to write output at a specific generation of a specific replicate or at some periodic time during the simulation. The default writing mode of a FileHandler is in output and is added as a 'writer' to the File Services as such using the FileServices::attach mode. The _isInputHandler flag is thus set to FALSE by default. To set a FileHandler as a reader instead of a writer, the method FileHandler::set_isInputHandler(TRUE) must be called prior to attaching it to the FileServices using the FileServices::attach_reader method. Furthermore, a single instance of a FileHandler can be attached as both a writer and a reader by setting the _isInputHandler flag to TRUE and then using the FileServices::attach method to add it to the list of both the reader and writers. Note on memory management: the FileServices class only deals with its lists of FileHandlers pointers and will never try to delete any one of those. All memory management must thus be taken care of by the user class (typically the TraitPrototype...).

Constructor & Destructor Documentation

◆ FileHandler()

FileHandler::FileHandler ( const char *  ext)
inline
106  : _service(0),
107 #ifdef USE_MPI
108  _isMasterExec(false),
109 #else
110  _isMasterExec(true),
111 #endif
112  _isInputHandler(0),
116  _path(), _extension(ext), _current_filename(), _pop(0)
117  {
118  // initialize the generation list to avoid having a non-allocated list
119  _generations.assign(1,0);
120  _genITER = _generations.begin();
121  }
FileServices * _service
Link to the files manager.
Definition: filehandler.h:55
bool _isGenerationPeriodic
Flag setting the per generation periodicity behaviour of the file.
Definition: filehandler.h:69
list< int > _generations
Definition: filehandler.h:83
bool _isInputHandler
Writing mode flag.
Definition: filehandler.h:65
unsigned int _ExecRank
unused...
Definition: filehandler.h:86
std::string _path
File path as set during initialization.
Definition: filehandler.h:88
std::string _extension
File extension, should be specific to the implementation.
Definition: filehandler.h:91
list< int >::const_iterator _genITER
Definition: filehandler.h:82
unsigned int _GenerationOccurrence
Tells every what generation the file should be written.
Definition: filehandler.h:73
bool _isMasterExec
Flag telling if the file must be written by the master or the worker node.
Definition: filehandler.h:63
std::string _current_filename
The current filename as set by FileHandler::get_filename().
Definition: filehandler.h:97
Metapop * _pop
Pointer to the current metapop, set during initialization within the init function.
Definition: filehandler.h:101
unsigned int _ReplicateOccurrence
Tells every what replicate the file should be written.
Definition: filehandler.h:71
unsigned int _current_generation
The current generation number of the simulation.
Definition: filehandler.h:77
unsigned int _current_replicate
The current replicate number of the simulation.
Definition: filehandler.h:75
bool _isReplicatePeriodic
Flag telling if the file should be written each replicate.
Definition: filehandler.h:67

References _generations, and _genITER.

◆ ~FileHandler()

virtual FileHandler::~FileHandler ( )
inlinevirtual
123 { }

Member Function Documentation

◆ FHread()

virtual void FileHandler::FHread ( string &  filename)
pure virtual

Default input function.

Loads a pop from the genotypes read from the input file.

Implemented in EventFileHandler< LCE >, TraitFileHandler< TP >, TTWolbachiaFH, TTQOhtaStats, TTQFreqExtractor, TTQuantiFH, TTNOhtaStats, TTNeutralGenesFH, TTDeletMutBitstrFH, TTBDMI_FH, LCE_StatFH, GenotyperFH, MPFileHandler, LCE_SelectionFH, FHPedigreeReader, FHLogWriter, and BinaryDataSaver.

Referenced by Metapop::loadPopFromTraitFile().

+ Here is the caller graph for this function:

◆ FHwrite()

◆ get_ExecRank()

unsigned int FileHandler::get_ExecRank ( )
inline

unused yet...

167 {return _ExecRank;}

References _ExecRank.

◆ get_extension()

◆ get_filename()

string & FileHandler::get_filename ( )

Builds and returns the current file name depending on the periodicity of the file.

150 {
154  else
156  else
158 
159  return _current_filename;
160 }
string & getReplicateFileName()
Accessor to the current replicate file name.
Definition: fileservices.cc:456
string getGenerationReplicateFileName()
Accessor to the current file name with generation and replicate counters added.
Definition: fileservices.cc:465
string & getBaseFileName()
Accessor to the base file name of the simulation.
Definition: fileservices.cc:473

Referenced by LCE_SelectionFH::FHwrite(), MPFileHandler::FHwrite(), GenotyperFH::FHwrite(), TTBDMI_FH::FHwrite(), TTNOhtaStats::FHwrite(), TTQOhtaStats::FHwrite(), TTWolbachiaFH::FHwrite(), BinaryDataSaver::finish(), BinaryDataSaver::printHeader(), BinaryDataSaver::setFileDescriptor(), and TTQuantiFH::write_TABLE().

+ Here is the caller graph for this function:

◆ get_GenerationOccurrence()

unsigned int FileHandler::get_GenerationOccurrence ( )
inline
164 {return _GenerationOccurrence;}

References _GenerationOccurrence.

Referenced by BinaryDataSaver::ifExist().

+ Here is the caller graph for this function:

◆ get_isGenerationPeriodic()

bool FileHandler::get_isGenerationPeriodic ( )
inline
161 {return _isGenerationPeriodic;}

References _isGenerationPeriodic.

◆ get_isInputHandler()

bool FileHandler::get_isInputHandler ( )
inline
152 {return _isInputHandler;}

References _isInputHandler.

Referenced by FileServices::attach().

+ Here is the caller graph for this function:

◆ get_isMasterExec()

bool FileHandler::get_isMasterExec ( )
inline
180 {return _isMasterExec;}

References _isMasterExec.

◆ get_isReplicatePeriodic()

bool FileHandler::get_isReplicatePeriodic ( )
inline
155 {return _isReplicatePeriodic;}

References _isReplicatePeriodic.

◆ get_OccMatrix()

TMatrix* FileHandler::get_OccMatrix ( )
inline
170 {return &_multipleOccurences;}
TMatrix _multipleOccurences
Multiple occurences.
Definition: filehandler.h:80

References _multipleOccurences.

◆ get_path()

◆ get_pop_ptr()

Metapop* FileHandler::get_pop_ptr ( )
inline

Returns the pointer to the current metapop through the FileServices interface.

133 {return _pop;}

References _pop.

Referenced by TTDeletMutBitstrFH::FHwrite(), and TTNOhtaStats::FHwrite().

+ Here is the caller graph for this function:

◆ get_ReplicateOccurrence()

unsigned int FileHandler::get_ReplicateOccurrence ( )
inline
158 {return _ReplicateOccurrence;}

References _ReplicateOccurrence.

Referenced by BinaryDataSaver::ifExist().

+ Here is the caller graph for this function:

◆ get_service()

◆ ifExist()

vector< string > FileHandler::ifExist ( )
virtual

Checks if any file associated with the current file name already exists on disk.

Only checks wether the first replicate file is present.

Returns
false if filename already exists on disk, true otherwise.

Reimplemented in LCE_StatFH, and BinaryDataSaver.

69 {
70  //check if the basefilename is already used on disk:
71  string filename = _path + _service->getBaseFileName();
72 
73  vector< string > list;
74 
75  ostringstream rpl, gen;
76 
77  gen.fill('0');
78  gen.width( (int)log10((double)_pop->getGenerations()) + 1);
80 
81  rpl.fill('0');
82  rpl.width( (int)log10((double)_pop->getReplicates()) + 1);
84 
86  filename += _extension;
87  else if(!_isGenerationPeriodic)
88  filename += "_" + rpl.str() + _extension;
89  else
90  filename += "_" + gen.str() + "_" + rpl.str() + _extension;
91 
92  ifstream ifXist;
93  ifXist.setstate(ios::failbit);
94 
95  ifXist.open(filename.c_str(),ios::in);
96 
97  if(ifXist.is_open()) {
98 // warning("filename \"%s\" used by \"%s\"\n",_service->getBaseFileName().c_str(),filename.c_str());
99  ifXist.close();
100  list.push_back( filename );
101 // return false;
102  }
103  ifXist.close();
104 
105  return list;
106 }
unsigned int getGenerations()
Definition: metapop.h:271
unsigned int getReplicates()
Definition: metapop.h:273

◆ init()

void FileHandler::init ( )
virtual

Called by notifier during simulation setup, performs file checking.

Implements Handler.

50 {
51  // this is called after set() is called
52  // so, nothing should be done here that can overwrite user's settings
53 
55  //check if the occurrences exceed the pop parameters:
58 
61 
62  set_path();
63 
64 }
void set_path()
Definition: filehandler.cc:110
virtual Metapop * get_pop_ptr()
Accessor to the pointer to the main population.
Definition: fileservices.h:111

◆ set()

virtual void FileHandler::set ( bool  rpl_per,
bool  gen_per,
int  rpl_occ,
int  gen_occ,
int  rank,
string  path 
)
inlinevirtual

Sets the hanlder parameters.

Parameters
rpl_perreplicate periodicity
gen_pergeneration periodicity
rpl_occreplicate occurence
gen_occgeneration occurence
rankthe rank in the life cycle, actualy unused...
paththe file path
195  {
197  set_GenerationOccurrence(gen_occ); set_ExecRank(rank); _path = path;}
void set_ExecRank(int val)
Definition: filehandler.h:168
void set_ReplicateOccurrence(unsigned int val)
Definition: filehandler.h:159
void set_GenerationOccurrence(unsigned int val)
Definition: filehandler.h:165
void set_isGenerationPeriodic(bool val)
Definition: filehandler.h:162
void set_isReplicatePeriodic(bool val)
Definition: filehandler.h:156

References _path, set_ExecRank(), set_GenerationOccurrence(), set_isGenerationPeriodic(), set_isReplicatePeriodic(), and set_ReplicateOccurrence().

Referenced by FileServices::init(), LCE_StatFH::LCE_StatFH(), Metapop::loadFileServices(), EventFileHandler< LCE >::set(), TraitFileHandler< TP >::set(), BinaryDataSaver::setParameters(), and LCE_StatServiceNotifier::setParameters().

+ Here is the caller graph for this function:

◆ set_ExecRank()

void FileHandler::set_ExecRank ( int  val)
inline
168 {_ExecRank = val;}

References _ExecRank.

Referenced by set().

+ Here is the caller graph for this function:

◆ set_extension()

void FileHandler::set_extension ( const char *  ext)
inline
147 {_extension = ext;}

References _extension.

Referenced by TProtoNeutralGenes::loadFileServices(), and LCE_FileServicesNotifier::setGenotyperParameters().

+ Here is the caller graph for this function:

◆ set_GenerationOccurrence()

void FileHandler::set_GenerationOccurrence ( unsigned int  val)
inline
165 {_GenerationOccurrence = val;}

References _GenerationOccurrence.

Referenced by set(), and set_multi().

+ Here is the caller graph for this function:

◆ set_isGenerationPeriodic()

void FileHandler::set_isGenerationPeriodic ( bool  val)
inline
162 {_isGenerationPeriodic = val;}

References _isGenerationPeriodic.

Referenced by set(), and set_multi().

+ Here is the caller graph for this function:

◆ set_isInputHandler()

void FileHandler::set_isInputHandler ( bool  val)
inline

◆ set_isMasterExec()

void FileHandler::set_isMasterExec ( bool  is)
inline
184 {_isMasterExec = true;}

References _isMasterExec.

Referenced by LCE_StatFH::LCE_StatFH().

+ Here is the caller graph for this function:

◆ set_isReplicatePeriodic()

void FileHandler::set_isReplicatePeriodic ( bool  val)
inline
156 {_isReplicatePeriodic = val;}

References _isReplicatePeriodic.

Referenced by set(), and set_multi().

+ Here is the caller graph for this function:

◆ set_multi()

virtual void FileHandler::set_multi ( bool  rpl_per,
bool  gen_per,
int  rpl_occ,
TMatrix Occ,
string  path 
)
inlinevirtual

◆ set_OccMatrix()

void FileHandler::set_OccMatrix ( TMatrix occ)
inline
172  {
173  _multipleOccurences.reset(occ->nrows(), occ->ncols(), occ->getValArray());
174  _generations.clear();
175  for(unsigned int i = 0; i < _multipleOccurences.ncols(); i++)
176  _generations.push_back((int)_multipleOccurences.get(0, i));
177  _genITER = _generations.begin();
178  }
void reset(unsigned int rows, unsigned int cols)
Re-allocate the existing matrix with assigned rows and cols dimensions and all elements to 0.
Definition: tmatrix.h:159
double * getValArray() const
Definition: tmatrix.h:200
unsigned int ncols() const
Definition: tmatrix.h:214
double get(unsigned int i, unsigned int j) const
Accessor to element at row i and column j.
Definition: tmatrix.h:191
unsigned int nrows() const
Definition: tmatrix.h:211

References _generations, _genITER, _multipleOccurences, TMatrix::get(), TMatrix::getValArray(), TMatrix::ncols(), TMatrix::nrows(), and TMatrix::reset().

Referenced by set_multi().

+ Here is the caller graph for this function:

◆ set_path()

void FileHandler::set_path ( )
111 {
112  string root_dir;
113 
114  root_dir = _service->getRootDir();
115 
116  if(_path.size() != 0 && _path[_path.length()-1] != '/')
117  _path += "/";
118 
119  if(root_dir.size() != 0)
120  _path = root_dir + _path;
121 
122  if(_path.size() != 0) {
123 
124  DIR *dir = opendir(_path.c_str());
125 
126  //check if we have to create the directory:
127  if(_path.size() != 0 && dir == 0) {
128 
129 #ifdef _WINDOWS_
130  mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
131  if((mkdir(_path.c_str(), mode)) == -1)
132 #else
133  string cmd = "mkdir -p " + _path;
134  if ( system( cmd.c_str() ) < 0 )
135 #endif
136  {
137  error("could not create directory \"%s\", saving in current directory.\n",_path.c_str());
138  _path = "";
139  }
140  } else {
141  if(closedir(dir) == -1)
142  warning("FileHandler::set_path::closedir: %s\n",strerror(errno));
143  }
144  }
145 }
string & getRootDir()
Accessor to the name of the simulation's root output directory.
Definition: fileservices.cc:480
int error(const char *str,...)
Definition: output.cc:77
void warning(const char *str,...)
Definition: output.cc:56

References error(), and warning().

◆ set_pop_ptr()

void FileHandler::set_pop_ptr ( Metapop pop_ptr)
inline
135 { _pop = pop_ptr;}

References _pop.

Referenced by Metapop::loadPopFromTraitFile().

+ Here is the caller graph for this function:

◆ set_ReplicateOccurrence()

void FileHandler::set_ReplicateOccurrence ( unsigned int  val)
inline
159 {_ReplicateOccurrence = val;}

References _ReplicateOccurrence.

Referenced by set(), and set_multi().

+ Here is the caller graph for this function:

◆ set_service()

void FileHandler::set_service ( FileServices srv)
inline
139 {_service = srv;}

References _service.

Referenced by FileServices::attach(), and FileServices::attach_reader().

+ Here is the caller graph for this function:

◆ update()

void FileHandler::update ( )
virtual

Updates the inner replicate and generation counters and calls FHwrite if needed by the the periodicity of the file.

Implements Handler.

165 {
167 
169 
170  // reset the generation iterator to the first in the list for new replicates
171  if(_genITER == _generations.end() && _current_generation == 1)
172  _genITER = _generations.begin();
173 
176 
177  if(_isGenerationPeriodic) { // multiple occurrences are to be expected (at least one)
178 
179  if(_GenerationOccurrence != 0) { // periodicity is set
180 
182  FHwrite();
183 
184  } else // periodicity is not set, check the list of generations at which the function is called
185  if(_current_generation == (unsigned)(*_genITER) && _myenv->isMaster() == _isMasterExec) {
186 
187  FHwrite();
188  _genITER++;
189 
190  }
191  } else // the writer prints data only at one possible generation, no periodicity
193  FHwrite();
194  }
195 }
virtual void FHwrite()=0
Default behavior of the class, called by Handler::update().
bool isMaster() const
Definition: MPImanager.h:126
unsigned int getCurrentReplicate()
Definition: metapop.h:293
unsigned int getCurrentGeneration()
Definition: metapop.h:294
MPIenv * _myenv
Definition: MPImanager.cc:34

References _myenv, and MPIenv::isMaster().

Member Data Documentation

◆ _current_filename

std::string FileHandler::_current_filename
private

The current filename as set by FileHandler::get_filename().

Is composed by putting together the following strings: FileHandler::_path, FileServices::_basename (+ replicate and generation counters if needed), and FileHandler::_extension.

◆ _current_generation

unsigned int FileHandler::_current_generation
private

The current generation number of the simulation.

Set by FileHandler::update().

◆ _current_replicate

unsigned int FileHandler::_current_replicate
private

The current replicate number of the simulation.

Set by FileHandler::update().

◆ _ExecRank

unsigned int FileHandler::_ExecRank
private

unused...

yet

Referenced by get_ExecRank(), and set_ExecRank().

◆ _extension

std::string FileHandler::_extension
private

File extension, should be specific to the implementation.

Is set during construction.

Referenced by get_extension(), and set_extension().

◆ _GenerationOccurrence

unsigned int FileHandler::_GenerationOccurrence
private

Tells every what generation the file should be written.

Set by the set() method below.

Referenced by get_GenerationOccurrence(), and set_GenerationOccurrence().

◆ _generations

list< int > FileHandler::_generations
private

Referenced by FileHandler(), and set_OccMatrix().

◆ _genITER

list< int >::const_iterator FileHandler::_genITER
private

Referenced by FileHandler(), and set_OccMatrix().

◆ _isGenerationPeriodic

bool FileHandler::_isGenerationPeriodic
private

Flag setting the per generation periodicity behaviour of the file.

Referenced by get_isGenerationPeriodic(), and set_isGenerationPeriodic().

◆ _isInputHandler

bool FileHandler::_isInputHandler
private

Writing mode flag.

Must be true

Referenced by get_isInputHandler(), and set_isInputHandler().

◆ _isMasterExec

bool FileHandler::_isMasterExec
private

Flag telling if the file must be written by the master or the worker node.

An example of master-only file is the stats files (.txt and _bygen.txt) that aggregate the stats from each node. Binary files or FSTAT files are written by the worker nodes, each file beeing written for one replicate only. The default behaviour is to be TRUE in the non-MPI version and FALSE in the MPI version.

Referenced by get_isMasterExec(), and set_isMasterExec().

◆ _isReplicatePeriodic

bool FileHandler::_isReplicatePeriodic
private

Flag telling if the file should be written each replicate.

Referenced by get_isReplicatePeriodic(), and set_isReplicatePeriodic().

◆ _multipleOccurences

TMatrix FileHandler::_multipleOccurences
private

Multiple occurences.

Referenced by get_OccMatrix(), and set_OccMatrix().

◆ _path

std::string FileHandler::_path
private

File path as set during initialization.

Usually user-defined.

Referenced by get_path(), set(), and set_multi().

◆ _pop

◆ _ReplicateOccurrence

unsigned int FileHandler::_ReplicateOccurrence
private

Tells every what replicate the file should be written.

Set by the set() method below.

Referenced by get_ReplicateOccurrence(), and set_ReplicateOccurrence().

◆ _service

FileServices* FileHandler::_service
private

Link to the files manager.

Referenced by get_service(), and set_service().


The documentation for this class was generated from the following files:

Generated for Nemo v2.4.2 by  doxygen 1.9.1

Catalogued on GSR