#include <filehandler.h>


Public Member Functions | |
| FileHandler (const char *ext) | |
| virtual | ~FileHandler () |
| virtual void | init () |
| Called by notifier during simulation setup, performs file checking. | |
| virtual bool | ifExist () |
| Checks if any file associated with the current file name already exists on disk. | |
| virtual void | set (bool rpl_per, bool gen_per, int rpl_occ, int gen_occ, int rank, string path) |
| Sets the hanlder parameters. | |
| virtual void | FHwrite ()=0 |
| Default behavior of the class, called by Handler::update(). | |
| virtual void | FHread (string &filename)=0 |
| Default input function. | |
| virtual void | update () |
| Updates the inner replicate and generation counters and calls FHwrite if needed by the the periodicity of the file. | |
Accessors | |
| Metapop * | get_pop_ptr () |
| Returns the pointer to the current metapop through the FileServices interface. | |
| void | set_pop_ptr (Metapop *pop_ptr) |
| FileServices * | get_service () |
| Returns pointer to the FileServices. | |
| 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. | |
| 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. | |
| void | set_ExecRank (int val) |
| bool | get_isMasterExec () |
| void | set_isMasterExec (bool is) |
Protected Attributes | |
| Metapop * | _pop |
| Pointer to the current metapop, set during initialization within the init function. | |
Private Attributes | |
| FileServices * | _service |
| Link to the files manager. | |
| bool | _isMasterExec |
| Flag telling if the file must be written by the master or the slave node. | |
| bool | _isInputHandler |
| Writing mode flag. | |
| bool | _isReplicatePeriodic |
| Flag telling if the file should be written each replicate. | |
| bool | _isGenerationPeriodic |
| Flag setting the per generation periodicity behaviour of the file. | |
| unsigned int | _ReplicateOccurrence |
| Tells every what replicate the file should be written. | |
| unsigned int | _GenerationOccurrence |
| Tells every what generation the file should be written. | |
| unsigned int | _current_replicate |
| The current replicate number of the simulation. | |
| unsigned int | _current_generation |
| The current generation number of the simulation. | |
| unsigned int | _ExecRank |
| unused. | |
| std::string | _path |
| File path as set during initialization. | |
| std::string | _extension |
| File extension, should be specific to the implementation. | |
| std::string | _current_filename |
| The current filename as set by FileHandler::get_filename(). | |
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...).
Definition at line 52 of file filehandler.h.
| FileHandler::FileHandler | ( | const char * | ext | ) | [inline] |
Definition at line 99 of file filehandler.h.
00100 : _service(0), 00101 #ifdef USE_MPI 00102 _isMasterExec(false), 00103 #else 00104 _isMasterExec(true), 00105 #endif 00106 _isInputHandler(0), 00107 _isReplicatePeriodic(0), _isGenerationPeriodic(0), 00108 _ReplicateOccurrence(0), _GenerationOccurrence(0), 00109 _current_replicate(0), _current_generation(0), _ExecRank(0), 00110 _path(), _extension(ext), _current_filename(), _pop(0) 00111 {}
| virtual FileHandler::~FileHandler | ( | ) | [inline, virtual] |
| virtual void FileHandler::FHread | ( | string & | filename | ) | [pure virtual] |
Default input function.
Loads a pop from the genotypes read from the input file.
Implemented in BinaryDataSaver, TraitFileHandler< TP >, EventFileHandler< LCE >, FHLogWriter, LCE_StatFH, TTDeletMutBitstrFH, TTNeutralGenesFH, TraitFileHandler< TProtoNeutralGenes >, and TraitFileHandler< TProtoDeletMutations_bitstring >.
Referenced by Metapop::loadPopFromTraitFile().
| virtual void FileHandler::FHwrite | ( | ) | [pure virtual] |
Default behavior of the class, called by Handler::update().
Implemented in BinaryDataSaver, TraitFileHandler< TP >, EventFileHandler< LCE >, FHLogWriter, LCE_StatFH, TTDeletMutBitstrFH, TTNeutralGenesFH, TraitFileHandler< TProtoNeutralGenes >, and TraitFileHandler< TProtoDeletMutations_bitstring >.
Referenced by update().
| unsigned int FileHandler::get_ExecRank | ( | ) | [inline] |
| std::string& FileHandler::get_extension | ( | ) | [inline] |
Definition at line 135 of file filehandler.h.
Referenced by TTDeletMutBitstrFH::FHwrite(), LCE_StatFH::FHwrite(), TTNeutralGenesFH::write_freq(), TTNeutralGenesFH::write_Fst_i(), TTNeutralGenesFH::write_FSTAT(), and TTNeutralGenesFH::write_varcompWC().
00135 {return _extension;}
| string & FileHandler::get_filename | ( | ) |
Builds and returns the current file name depending on the periodicity of the file.
Definition at line 138 of file filehandler.cc.
Referenced by BinaryDataSaver::finish(), BinaryDataSaver::printData(), BinaryDataSaver::printHeader(), and FHLogWriter::save_simparams().
00139 { 00140 if(_isReplicatePeriodic) 00141 if(_isGenerationPeriodic) 00142 _current_filename = _path + _service->getGenerationReplicateFileName() + _extension; 00143 else 00144 _current_filename = _path + _service->getReplicateFileName() + _extension; 00145 else 00146 _current_filename = _path + _service->getBaseFileName() + _extension; 00147 00148 return _current_filename; 00149 }
| unsigned int FileHandler::get_GenerationOccurrence | ( | ) | [inline] |
| bool FileHandler::get_isGenerationPeriodic | ( | ) | [inline] |
| bool FileHandler::get_isInputHandler | ( | ) | [inline] |
Definition at line 142 of file filehandler.h.
Referenced by FileServices::attach().
00142 {return _isInputHandler;}
| bool FileHandler::get_isMasterExec | ( | ) | [inline] |
| bool FileHandler::get_isReplicatePeriodic | ( | ) | [inline] |
| std::string& FileHandler::get_path | ( | ) | [inline] |
Definition at line 131 of file filehandler.h.
Referenced by TTDeletMutBitstrFH::FHwrite(), LCE_StatFH::FHwrite(), BinaryDataSaver::finish(), LCE_StatFH::ifExist(), LCE_StatFH::PrintStat_byGen(), TTNeutralGenesFH::write_freq(), TTNeutralGenesFH::write_Fst_i(), TTNeutralGenesFH::write_FSTAT(), and TTNeutralGenesFH::write_varcompWC().
00131 {return _path;}
| Metapop* FileHandler::get_pop_ptr | ( | ) | [inline] |
Returns the pointer to the current metapop through the FileServices interface.
Definition at line 123 of file filehandler.h.
Referenced by TTDeletMutBitstrFH::FHwrite().
00123 {return _pop;}
| unsigned int FileHandler::get_ReplicateOccurrence | ( | ) | [inline] |
| FileServices* FileHandler::get_service | ( | ) | [inline] |
Returns pointer to the FileServices.
Definition at line 127 of file filehandler.h.
Referenced by TTDeletMutBitstrFH::FHwrite(), LCE_StatFH::FHwrite(), BinaryDataSaver::finish(), LCE_StatFH::ifExist(), BinaryDataSaver::printHeader(), LCE_StatFH::PrintStat_byGen(), TTNeutralGenesFH::write_freq(), TTNeutralGenesFH::write_Fst_i(), TTNeutralGenesFH::write_FSTAT(), and TTNeutralGenesFH::write_varcompWC().
00127 {return _service;}
| bool 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.
Reimplemented in LCE_StatFH.
Definition at line 69 of file filehandler.cc.
00070 { 00071 //check if the basefilename is already used on disk: 00072 string filename = _path + _service->getBaseFileName(); 00073 00074 ostringstream rpl, gen; 00075 00076 gen.fill('0'); 00077 gen.width( (int)log10((double)_pop->getGenerations()) + 1); 00078 gen<<_GenerationOccurrence; 00079 00080 rpl.fill('0'); 00081 rpl.width( (int)log10((double)_pop->getReplicates()) + 1); 00082 rpl<<_ReplicateOccurrence; 00083 00084 if(!_isReplicatePeriodic) 00085 filename += _extension; 00086 else if(!_isGenerationPeriodic) 00087 filename += "_" + rpl.str() + _extension; 00088 else 00089 filename += "_" + gen.str() + "_" + rpl.str() + _extension; 00090 00091 ifstream ifXist; 00092 ifXist.setstate(ios::failbit); 00093 00094 ifXist.open(filename.c_str(),ios::in); 00095 00096 if(ifXist.is_open()) { 00097 warning("filename \"%s\" used by \"%s\"\n",_service->getBaseFileName().c_str(),filename.c_str()); 00098 ifXist.close(); 00099 return false; 00100 } 00101 ifXist.close(); 00102 00103 return true; 00104 }
| void FileHandler::init | ( | ) | [virtual] |
Called by notifier during simulation setup, performs file checking.
Implements Handler.
Definition at line 51 of file filehandler.cc.
00052 { 00053 _pop = _service->get_pop_ptr(); 00054 //check if the occurrences exceed the pop parameters: 00055 if(_GenerationOccurrence > _pop->getGenerations()) 00056 _GenerationOccurrence = _pop->getGenerations(); 00057 00058 if(_ReplicateOccurrence > _pop->getReplicates()) 00059 _ReplicateOccurrence = _pop->getReplicates(); 00060 00061 if ( _myenv->isMaster() != _isMasterExec ) return; 00062 //@todo check FileHandler::init with MPI, set_path() has moved from set() to init()! 00063 set_path(); 00064 // message(" %s*%s\n",get_path().c_str(),get_extension().c_str()); 00065 }
| virtual void FileHandler::set | ( | bool | rpl_per, | |
| bool | gen_per, | |||
| int | rpl_occ, | |||
| int | gen_occ, | |||
| int | rank, | |||
| string | path | |||
| ) | [inline, virtual] |
Sets the hanlder parameters.
| rpl_per | replicate periodicity | |
| gen_per | generation periodicity | |
| rpl_occ | replicate occurence | |
| gen_occ | generation occurence | |
| rank | the rank in the life cycle, actualy unused... | |
| path | the file path |
Definition at line 175 of file filehandler.h.
Referenced by FileServices::init(), LCE_StatFH::LCE_StatFH(), LCE_StatServiceNotifier::setParameters(), and BinaryDataSaver::setParameters().
00175 { 00176 set_isReplicatePeriodic(rpl_per); set_isGenerationPeriodic(gen_per); set_ReplicateOccurrence(rpl_occ); 00177 set_GenerationOccurrence(gen_occ); set_ExecRank(rank); _path = path;}
| void FileHandler::set_ExecRank | ( | int | val | ) | [inline] |
| void FileHandler::set_extension | ( | const char * | ext | ) | [inline] |
Definition at line 137 of file filehandler.h.
Referenced by TProtoNeutralGenes::loadFileServices().
00137 {_extension = ext;}
| void FileHandler::set_GenerationOccurrence | ( | unsigned int | val | ) | [inline] |
| void FileHandler::set_isGenerationPeriodic | ( | bool | val | ) | [inline] |
| void FileHandler::set_isInputHandler | ( | bool | val | ) | [inline] |
Definition at line 143 of file filehandler.h.
Referenced by TProtoNeutralGenes::loadFileServices(), and TProtoDeletMutations_bitstring::loadFileServices().
00143 {_isInputHandler = val;}
| void FileHandler::set_isMasterExec | ( | bool | is | ) | [inline] |
Definition at line 164 of file filehandler.h.
Referenced by LCE_StatFH::LCE_StatFH().
00164 {_isMasterExec = true;}
| void FileHandler::set_isReplicatePeriodic | ( | bool | val | ) | [inline] |
| void FileHandler::set_path | ( | ) |
Definition at line 108 of file filehandler.cc.
Referenced by init().
00109 { 00110 string root_dir; 00111 00112 root_dir = _service->getRootDir(); 00113 00114 if(_path.size() != 0 && _path[_path.length()-1] != '/') 00115 _path += "/"; 00116 00117 if(root_dir.size() != 0) 00118 _path = root_dir + _path; 00119 00120 //check if we have to create the directory: 00121 if(_path.size() != 0 && opendir(_path.c_str()) == 0) { 00122 #ifdef _WINDOWS_ 00123 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; 00124 if((mkdir(_path.c_str(), mode)) == -1){ 00125 #else 00126 string cmd = "mkdir -p " + _path; 00127 if ( system( cmd.c_str() ) < 0 ) 00128 { 00129 #endif 00130 error("could not create directory \"%s\", saving in wd.\n",_path.c_str()); 00131 _path = ""; 00132 } 00133 } 00134 }
| void FileHandler::set_pop_ptr | ( | Metapop * | pop_ptr | ) | [inline] |
Definition at line 125 of file filehandler.h.
Referenced by Metapop::loadPopFromTraitFile().
00125 { _pop = pop_ptr;}
| void FileHandler::set_ReplicateOccurrence | ( | unsigned int | val | ) | [inline] |
| void FileHandler::set_service | ( | FileServices * | srv | ) | [inline] |
Definition at line 129 of file filehandler.h.
Referenced by FileServices::attach(), and FileServices::attach_reader().
00129 {_service = srv;}
| 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.
Definition at line 153 of file filehandler.cc.
00154 { 00155 _current_replicate = _pop->getCurrentReplicate(); 00156 00157 _current_generation = _pop->getCurrentGeneration(); 00158 00159 if ( ( (_isReplicatePeriodic && !(_current_replicate % _ReplicateOccurrence)) 00160 || (_current_replicate == _ReplicateOccurrence) ) 00161 && 00162 ( (_isGenerationPeriodic && !(_current_generation % _GenerationOccurrence)) 00163 || (_current_generation == _GenerationOccurrence) ) ) 00164 if( _myenv->isMaster() == _isMasterExec) 00165 FHwrite(); 00166 }
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.
Definition at line 91 of file filehandler.h.
Referenced by get_filename().
unsigned int FileHandler::_current_generation [private] |
The current generation number of the simulation.
Set by FileHandler::update().
Definition at line 78 of file filehandler.h.
Referenced by update().
unsigned int FileHandler::_current_replicate [private] |
The current replicate number of the simulation.
Set by FileHandler::update().
Definition at line 76 of file filehandler.h.
Referenced by update().
unsigned int FileHandler::_ExecRank [private] |
unused.
.. yet
Definition at line 80 of file filehandler.h.
Referenced by get_ExecRank(), and set_ExecRank().
std::string FileHandler::_extension [private] |
File extension, should be specific to the implementation.
Is set during construction.
Definition at line 85 of file filehandler.h.
Referenced by get_extension(), get_filename(), ifExist(), and set_extension().
unsigned int FileHandler::_GenerationOccurrence [private] |
Tells every what generation the file should be written.
Set by the set() method below.
Definition at line 74 of file filehandler.h.
Referenced by get_GenerationOccurrence(), ifExist(), init(), set_GenerationOccurrence(), and update().
bool FileHandler::_isGenerationPeriodic [private] |
Flag setting the per generation periodicity behaviour of the file.
Definition at line 70 of file filehandler.h.
Referenced by get_filename(), get_isGenerationPeriodic(), ifExist(), set_isGenerationPeriodic(), and update().
bool FileHandler::_isInputHandler [private] |
Writing mode flag.
Must be true
Definition at line 66 of file filehandler.h.
Referenced by get_isInputHandler(), and set_isInputHandler().
bool FileHandler::_isMasterExec [private] |
Flag telling if the file must be written by the master or the slave 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 slave 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.
Definition at line 64 of file filehandler.h.
Referenced by get_isMasterExec(), init(), set_isMasterExec(), and update().
bool FileHandler::_isReplicatePeriodic [private] |
Flag telling if the file should be written each replicate.
Definition at line 68 of file filehandler.h.
Referenced by get_filename(), get_isReplicatePeriodic(), ifExist(), set_isReplicatePeriodic(), and update().
std::string FileHandler::_path [private] |
File path as set during initialization.
Usually user-defined.
Definition at line 82 of file filehandler.h.
Referenced by get_filename(), get_path(), ifExist(), and set_path().
Metapop* FileHandler::_pop [protected] |
Pointer to the current metapop, set during initialization within the init function.
Definition at line 95 of file filehandler.h.
Referenced by TTNeutralGenesFH::FHread(), TTDeletMutBitstrFH::FHread(), get_pop_ptr(), ifExist(), init(), set_pop_ptr(), update(), TTNeutralGenesFH::write_freq(), TTNeutralGenesFH::write_Fst_i(), TTNeutralGenesFH::write_FSTAT(), and TTNeutralGenesFH::write_varcompWC().
unsigned int FileHandler::_ReplicateOccurrence [private] |
Tells every what replicate the file should be written.
Set by the set() method below.
Definition at line 72 of file filehandler.h.
Referenced by get_ReplicateOccurrence(), ifExist(), init(), set_ReplicateOccurrence(), and update().
FileServices* FileHandler::_service [private] |
Link to the files manager.
Definition at line 56 of file filehandler.h.
Referenced by get_filename(), get_service(), ifExist(), init(), set_path(), and set_service().
1.5.8 -- Nemo is hosted by