Nemo  2.4.0
Simulate forward-in-time genetic evolution in a spatially explicit, individual-based stochastic simulator
LCE_FileServicesNotifier Class Reference

Event used to notify all file handlers to update their state through the FileServices::notify() interface. More...

#include <servicenotifiers.h>

+ Inheritance diagram for LCE_FileServicesNotifier:
+ Collaboration diagram for LCE_FileServicesNotifier:

Public Member Functions

 LCE_FileServicesNotifier ()
 
virtual ~LCE_FileServicesNotifier ()
 
virtual void execute ()
 
virtual bool setParameters ()
 
bool setSamplingParameters ()
 
bool setGenotyperParameters ()
 
virtual LifeCycleEventclone ()
 
virtual void loadFileServices (FileServices *loader)
 
virtual void loadStatServices (StatServices *loader)
 
virtual bool resetParameterFromSource (std::string param, SimComponent *cmpt)
 
virtual age_t removeAgeClass ()
 
virtual age_t addAgeClass ()
 
virtual age_t requiredAgeClass ()
 
- Public Member Functions inherited from LifeCycleEvent
 LifeCycleEvent (const char *name, const char *trait_link)
 Cstor. More...
 
virtual ~LifeCycleEvent ()
 
virtual void init (Metapop *popPtr)
 Sets the pointer to the current Metapop and the trait link if applicable. More...
 
virtual bool attach_trait (string trait)
 
virtual void set_paramset (std::string name, bool required, SimComponent *owner)
 
virtual void set_event_name (std::string &name)
 Set the name of the event (name of the ParamSet) and add the corresponding parameter to the set. More...
 
virtual void set_event_name (const char *name)
 
virtual string & get_event_name ()
 Accessor to the LCE's name. More...
 
virtual int get_rank ()
 Accessor to the LCE rank in the life cycle. More...
 
virtual void set_pop_ptr (Metapop *popPtr)
 Accessors for the population pointer. More...
 
virtual Metapopget_pop_ptr ()
 
- Public Member Functions inherited from SimComponent
 SimComponent ()
 
virtual ~SimComponent ()
 
virtual void loadUpdaters (UpdaterServices *loader)
 Loads the parameters and component updater onto the updater manager. More...
 
virtual void set_paramset (ParamSet *paramset)
 Sets the ParamSet member. More...
 
virtual void set_paramsetFromCopy (const ParamSet &PSet)
 Reset the set of parameters from a another set. More...
 
virtual ParamSetget_paramset ()
 ParamSet accessor. More...
 
virtual void add_parameter (Param *param)
 Interface to add a parameter to the set. More...
 
virtual void add_parameter (std::string Name, param_t Type, bool isRequired, bool isBounded, double low_bnd, double up_bnd)
 Interface to add a parameter to the set. More...
 
virtual void add_parameter (std::string Name, param_t Type, bool isRequired, bool isBounded, double low_bnd, double up_bnd, ParamUpdaterBase *updater)
 Interface to add a parameter and its updater to the set. More...
 
virtual Paramget_parameter (std::string name)
 Param getter. More...
 
virtual double get_parameter_value (std::string name)
 Param value getter. More...
 
virtual string get_name ()
 Returnd the name of the ParamSet, i.e. More...
 
virtual bool has_parameter (std::string name)
 Param getter. More...
 

Private Attributes

FileServices_service
 
TMatrix _sample_patch
 
unsigned int _sample_patch_num
 
TMatrix _sample_size
 
TMatrix _sample_age
 
vector< GenotyperFH * > _genotypers
 

Additional Inherited Members

- Protected Attributes inherited from LifeCycleEvent
std::string _event_name
 The param name to be read in the init file. More...
 
Metapop_popPtr
 The ptr to the current Metapop. More...
 
std::string _LCELinkedTraitType
 The name of the linked trait. More...
 
int _LCELinkedTraitIndex
 The index in the individual's trait table of the linked trait. More...
 
- Protected Attributes inherited from SimComponent
ParamSet_paramSet
 The parameters container. More...
 

Detailed Description

Event used to notify all file handlers to update their state through the FileServices::notify() interface.

Constructor & Destructor Documentation

◆ LCE_FileServicesNotifier()

LCE_FileServicesNotifier::LCE_FileServicesNotifier ( )
62  : LifeCycleEvent("save_files",""),
64 {
65 
67 
68  add_parameter("files_sample_patch", MAT, false, false, 0, 0, sampler);
69  add_parameter("files_sample_size", INT, false, false, 0, 0, sampler);
70  add_parameter("files_sample_age", INT, false, false, 0, 0, sampler);
71 
73 
74  add_parameter("files_genotyper_traits", STR, false, false, 0, 0, genotyper);
75  add_parameter("files_genotyper_format", STR, false, false, 0, 0, genotyper);
76  add_parameter("files_genotyper_logtime", INT, false, false, 0, 0, genotyper);
77  add_parameter("files_genotyper_dir", STR, false, false, 0, 0, genotyper);
78  add_parameter("files_genotyper_trim_maf", DBL, false, false, 0, 0, genotyper);
79 
80 }
unsigned int _sample_patch_num
Definition: servicenotifiers.h:76
bool setSamplingParameters()
Definition: servicenotifiers.cc:133
FileServices * _service
Definition: servicenotifiers.h:74
bool setGenotyperParameters()
Definition: servicenotifiers.cc:232
LifeCycleEvent(const char *name, const char *trait_link)
Cstor.
Definition: lifecycleevent.h:97
Implementation of the ParamUpdaterBase interface.
Definition: param.h:362
virtual void add_parameter(Param *param)
Interface to add a parameter to the set.
Definition: simcomponent.h:111
@ DBL
Definition: types.h:77
@ MAT
Definition: types.h:77
@ STR
Definition: types.h:77
@ INT
Definition: types.h:77

References SimComponent::add_parameter(), DBL, INT, MAT, setGenotyperParameters(), setSamplingParameters(), and STR.

Referenced by clone().

+ Here is the caller graph for this function:

◆ ~LCE_FileServicesNotifier()

LCE_FileServicesNotifier::~LCE_FileServicesNotifier ( )
virtual
83 {
84  if(_genotypers.size() != 0) {
85  for(auto g : _genotypers)
86  delete g;
87  _genotypers.clear();
88  }
89 }
vector< GenotyperFH * > _genotypers
Definition: servicenotifiers.h:80

References _genotypers.

Member Function Documentation

◆ addAgeClass()

virtual age_t LCE_FileServicesNotifier::addAgeClass ( )
inlinevirtual

Implements LifeCycleEvent.

99 {return 0;}

◆ clone()

virtual LifeCycleEvent* LCE_FileServicesNotifier::clone ( )
inlinevirtual

Implements LifeCycleEvent.

92 {return new LCE_FileServicesNotifier();}
LCE_FileServicesNotifier()
Definition: servicenotifiers.cc:62

References LCE_FileServicesNotifier().

◆ execute()

void LCE_FileServicesNotifier::execute ( )
virtual

Implements LifeCycleEvent.

367 {
368 #ifdef _DEBUG_
369  message("LCE_FileServicesNotifier::execute (gen: %i rpl: %i)\n",
370  this->get_pop_ptr()->getCurrentGeneration(),
371  this->get_pop_ptr()->getCurrentReplicate());
372 #endif
373 
374  _service->notify();
375 }
virtual void notify()
Definition: fileservices.cc:188
virtual Metapop * get_pop_ptr()
Definition: lifecycleevent.h:173
void message(const char *message,...)
Definition: output.cc:39

References _service, LifeCycleEvent::get_pop_ptr(), message(), and FileServices::notify().

◆ loadFileServices()

void LCE_FileServicesNotifier::loadFileServices ( FileServices loader)
virtual

Implements SimComponent.

349 {
350  _service = loader;
351 
352  // tell the file services to reset the sampled metapop to avoid pbs between chained simulations
354 
356 
357  if( !_genotypers.empty()) {
358  for(auto g : _genotypers)
359  loader->attach(g);
360  }
361 
362 }
void reset_sampled_pop()
Remove the existing sampled metapop, if it has been allocated previously.
Definition: fileservices.cc:385
void set_sampling_params(TMatrix *size, TMatrix *age, TMatrix *patches)
Set the population sampling params.
Definition: fileservices.h:177
virtual void attach(Handler *FH)
Attaches the FileHandler to the current list (_writers) of the FileServices.
Definition: fileservices.cc:60
TMatrix _sample_age
Definition: servicenotifiers.h:78
TMatrix _sample_size
Definition: servicenotifiers.h:77
TMatrix _sample_patch
Definition: servicenotifiers.h:75

References _genotypers, _sample_age, _sample_patch, _sample_size, _service, FileServices::attach(), FileServices::reset_sampled_pop(), and FileServices::set_sampling_params().

◆ loadStatServices()

virtual void LCE_FileServicesNotifier::loadStatServices ( StatServices loader)
inlinevirtual

Implements SimComponent.

96 {}

◆ removeAgeClass()

virtual age_t LCE_FileServicesNotifier::removeAgeClass ( )
inlinevirtual

Implements LifeCycleEvent.

98 {return 0;}

◆ requiredAgeClass()

virtual age_t LCE_FileServicesNotifier::requiredAgeClass ( )
inlinevirtual

Implements LifeCycleEvent.

100 {return 0;}

◆ resetParameterFromSource()

virtual bool LCE_FileServicesNotifier::resetParameterFromSource ( std::string  param,
SimComponent cmpt 
)
inlinevirtual

Implements SimComponent.

97 {return false;}

◆ setGenotyperParameters()

bool LCE_FileServicesNotifier::setGenotyperParameters ( )
233 {
234 
235  if(!get_parameter("files_genotyper_format")->isSet())
236  return error("parameter \"files_genotyper_format\" is missing\n");
237 
238  if(!get_parameter("files_genotyper_logtime")->isSet())
239  return error("parameter \"files_genotyper_logtime\" is missing\n");
240 
241  vector< string > traits = get_parameter("files_genotyper_traits")->getMultiArgs();
242 
243  vector<unsigned int> trait_index;
244 
245  TraitPrototype* Tproto;
246 
247  for(unsigned int i = 0; i < traits.size(); i++) {
248 
249  Tproto = _popPtr->getTraitPrototype(traits[i]);
250 
251  if( !Tproto )
252  return error("Genotyper::cannot find trait \"%s\" among successfully initialized traits.\n", traits[i].c_str());
253 
254  if( !Tproto->is_mappable() )
255  return error("Genotyper::cannot add the non-mappable trait \"%s\" to the joint genotype output file\n", traits[i].c_str());
256 
257  trait_index.push_back(Tproto->get_index());
258  }
259 
260  // FORMAT ---------------------------------------------------------------------------------
261  string format = get_parameter("files_genotyper_format")->getArg();
262 
263  // trim plural form of format string, check if word ends with an "s"
264  if(format.find_last_of("s") == format.size()-1)
265  format = format.substr(0, format.size()-1);
266 
267  if( format != "genotype" && format != "snp_genotype" && format != "snp_id")
268  return error("Genotyper::file format \"%s\" is not a valid option. Check the manual.\n", format.c_str());
269 
270  // check for di-allelic loci compatible with snp-based format:
271  bool is2all = true;
272 
273  for(unsigned int i = 0; i < traits.size(); i++) {
274  // allele number;
275  is2all &= (_popPtr->getTraitPrototype(traits[i])->get_allele_number() < 3);
276 
277  }
278 
279  if( !is2all && format.find("snp") != string::npos) {
280  return error("Genotyper:: not all traits have di-allelic loci as requested by the output format \"%s\"\n", format.c_str());
281  }
282 
283  // create the genotyper:
284  GenotyperFH* genotyper = new GenotyperFH(this);
285 
286  genotyper->setTraits(traits);
287  genotyper->setTraitIndex(trait_index);
288  genotyper->setFormat(format);
289  genotyper->set_isDiallelic(is2all);
290 
291  bool doTrimFixedLoci = get_parameter("files_genotyper_trim_maf")->isSet();
292  double trim_maf = 0;
293 
294  if(doTrimFixedLoci) {
295 
296  if(!is2all) {
297  warning("Genotyper::option \"files_genotyper_trim_maf\" is set but loci are not di-allelic. Not trimming.\n");
298  doTrimFixedLoci = false;
299  }
300 
301  if(format == "genotype") {
302  warning("Genotyper::option \"files_genotyper_trim_maf\" is set but data recorded is allele values (format option \"genotype\"). Not trimming.\n");
303  doTrimFixedLoci = false;
304  }
305 
306  trim_maf = get_parameter_value("files_genotyper_trim_maf");
307 
308  if( trim_maf <= 0) {
309  warning("Genotyper::value %f for option \"files_genotyper_trim_maf\" cannot be used for trimming di-allelic loci.\n",trim_maf);
310  doTrimFixedLoci = false;
311  trim_maf = 0;
312  }
313  }
314 
315  genotyper->set_trimFixedLoci(doTrimFixedLoci, trim_maf);
316 
317  // LOGTIME -------------------------------------------------------------------------------
318  Param* param = get_parameter("files_genotyper_logtime");
319 
320  if(param->isMatrix()) {
321 
322  TMatrix log_mat;
323 
324  param->getMatrix(&log_mat);
325 
326  genotyper->set_multi(true, true, 1, &log_mat, get_parameter("files_genotyper_dir")->getArg());
327 
328  // rpl_per, gen_per, rpl_occ, gen_occ, rank, path, self-ref
329  } else
330  genotyper->set(true, param->isSet(), 1, (param->isSet() ? (int)param->getValue() : 0),
331  0, get_parameter("files_genotyper_dir")->getArg(), this);
332 
333  if (format== "genotype")
334  genotyper->set_extension(".genotype");
335  else if (format=="snp_genotype")
336  genotyper->set_extension(".snp_geno");
337  else if (format== "snp_id")
338  genotyper->set_extension(".snp_id");
339 
340  // add it to the container:
341  _genotypers.push_back(genotyper);
342 
343  return true;
344 }
virtual void set(bool rpl_per, bool gen_per, int rpl_occ, int gen_occ, int rank, string path, LCE *event)
Definition: filehandler.h:275
void set_extension(const char *ext)
Definition: filehandler.h:148
virtual void set_multi(bool rpl_per, bool gen_per, int rpl_occ, TMatrix *Occ, string path)
Definition: filehandler.h:200
FileHandler to write joint genotype files for multiple mappable traits.
Definition: servicenotifiers.h:109
void set_trimFixedLoci(bool test, double maf)
Definition: servicenotifiers.h:137
void setTraits(vector< trait_t > &traits)
Definition: servicenotifiers.cc:384
void setFormat(string format)
Definition: servicenotifiers.h:131
void setTraitIndex(vector< unsigned int > &indices)
Definition: servicenotifiers.cc:391
void set_isDiallelic(bool test)
Definition: servicenotifiers.h:135
TraitPrototype * getTraitPrototype(trait_t type)
Accessor to a TraitPrototype.
Definition: indfactory.cc:139
Metapop * _popPtr
The ptr to the current Metapop.
Definition: lifecycleevent.h:80
This structure stores one parameter, its definition and its string argument.
Definition: param.h:53
double getValue()
Returns the argument value according to its type.
Definition: param.cc:367
bool isMatrix()
Checks if the argument is of matrix type.
Definition: param.h:171
string getArg()
Definition: param.h:137
void getMatrix(TMatrix *mat)
Sets the matrix from the argument string if the parameter is set and of matrix type.
Definition: param.cc:377
vector< string > getMultiArgs()
Definition: param.cc:119
bool isSet()
Definition: param.h:139
virtual double get_parameter_value(std::string name)
Param value getter.
Definition: simcomponent.h:142
virtual Param * get_parameter(std::string name)
Param getter.
Definition: simcomponent.h:138
A class to handle matrix in params, coerces matrix into a vector of same total size.
Definition: tmatrix.h:49
TTrait setter.
Definition: ttrait.h:130
virtual int get_allele_number()=0
Returns the number of allele per locus.
virtual int get_index()
Index getter.
Definition: ttrait.h:151
virtual bool is_mappable()=0
Checks if the trait is mappable, i.e., if the loci can be placed on a genetic map.
int error(const char *str,...)
Definition: output.cc:78
void warning(const char *str,...)
Definition: output.cc:57

References _genotypers, LifeCycleEvent::_popPtr, error(), TraitPrototype::get_allele_number(), TraitPrototype::get_index(), SimComponent::get_parameter(), SimComponent::get_parameter_value(), Param::getArg(), Param::getMatrix(), Param::getMultiArgs(), IndFactory::getTraitPrototype(), Param::getValue(), TraitPrototype::is_mappable(), Param::isMatrix(), Param::isSet(), EventFileHandler< LCE >::set(), FileHandler::set_extension(), GenotyperFH::set_isDiallelic(), FileHandler::set_multi(), GenotyperFH::set_trimFixedLoci(), GenotyperFH::setFormat(), GenotyperFH::setTraitIndex(), GenotyperFH::setTraits(), and warning().

Referenced by LCE_FileServicesNotifier(), and setParameters().

+ Here is the caller graph for this function:

◆ setParameters()

bool LCE_FileServicesNotifier::setParameters ( )
virtual

Implements SimComponent.

94 {
95 
96 
97  // _sample_size holds the number of individuals sampled in each sampled patch and age-class
98  // it is mandatory for any sampling scheme to happen
99  if( get_parameter("files_sample_size")->isSet() ) {
100 
101  if( !setSamplingParameters() ) return false;
102 
103  } else if( get_parameter("files_sample_age")->isSet() || get_parameter("files_sample_patch")->isSet() ) {
104 
105  return error("some patch sampling parameters are set but parameter \"files_sample_size\" is missing.\n");
106 
107  } else { // no sampling parameter present
108  _sample_age.reset();
111  _sample_patch_num = 0;
112  }
113 
114  // GENOTYPER ----------------------------------------------------------------------------
115 
116  if(_genotypers.size() != 0) {
117  for(auto g : _genotypers)
118  delete g;
119  _genotypers.clear();
120  }
121 
122  if(get_parameter("files_genotyper_traits")->isSet()){
123 
124  if( !setGenotyperParameters() ) return false;
125 
126  }
127 
128  return true;
129 }
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:160

References _genotypers, _sample_age, _sample_patch, _sample_patch_num, _sample_size, error(), SimComponent::get_parameter(), TMatrix::reset(), setGenotyperParameters(), and setSamplingParameters().

◆ setSamplingParameters()

bool LCE_FileServicesNotifier::setSamplingParameters ( )
134 {
135 
136  TMatrix tmp_mat;
137 
138  // 1. check which patch needs to be sampled -------------------------------------------
139 
140  // _sample_patch holds the IDs of patches sampled, 1-based in input, 0-based in output
141  if(_paramSet->isSet("files_sample_patch")) {
142 
143  get_parameter("files_sample_patch")->getMatrix(&_sample_patch);
144 
145  if(_sample_patch.nrows() > 1)
146  return error("parameter \"files_sample_patch\" awaits a single row (array) of patch-IDs (1-based) in input.\n");
147 
148 
150 
151  // transform 1-based values to 0-based (we're confident that no 0 was given in input?)
153 
154 
155  } else {
156  // generate a sequence of values 0..patch_number
157  double ID[_popPtr->getPatchNbr()];
158 
159  for(unsigned int i = 0; i < _popPtr->getPatchNbr(); ++i) ID[i] = double(i);
160 
161  _sample_patch.reset(1, _popPtr->getPatchNbr(), &ID[0]);
163 
164  }
165 
166  // 2. check which age class are sampled ----------------------------------------------
167 
168  // _sample_age matrix has at least one array of age-class index in which to sample
169  // can have one array per patch sampled
170  if(get_parameter("files_sample_age")->isSet()) {
171 
172  if(get_parameter("files_sample_age")->isMatrix()) {
173 
174  get_parameter("files_sample_age")->getMatrix(&tmp_mat);
175 
176  if(tmp_mat.nrows() > _sample_patch_num)
177  return error("parameter \"files_sample_age\" has more rows than the number of patches sampled.\n");
178 
179  if(tmp_mat.ncols() > _popPtr->getNumAgeClasses())
180  return error("parameter \"files_sample_age\" has more age-class values than the number of stages in the population.\n");
181 
182  // need to copy-expand!
184  _sample_age.copy_recycle(tmp_mat);
185 
186  } else {
187  // copy a single number to all elements
188  _sample_age.reset(_sample_patch_num, 1, get_parameter_value("files_sample_age"));
189  }
190 
191  } else {
192  // we set the age class flags to all age class by default
193  TMatrix sizes(1,_popPtr->getNumAgeClasses());
194  for(unsigned int a = 0; a < sizes.ncols(); ++a) sizes.set(0, a, double(a));
195  // !!! in nemo, contrary to nemo-age, adults are at position 2, not 1 !!!
196  // specifically for nemo, set adults index to 2
197  sizes.set(0,1,2.0);
199  _sample_age.copy_recycle(sizes); // expand, copy values to all rows
200  }
201 
202 
203  // 3. now read the sampling size provided, and copy to patch and age class if needed --
204 
205  if( _paramSet->isMatrix("files_sample_size")) {
206 
207  get_parameter("files_sample_size")->getMatrix(&tmp_mat);
208 
209  if(tmp_mat.nrows() > _sample_patch_num)
210  return error("parameter \"files_sample_size\" has more rows than the number of sampled patches specified with \"files_sample_patch\" (if any).\n");
211 
212  if(tmp_mat.ncols() > _sample_age.ncols())
213  return error("parameter \"files_sample_size\" has more values per row than the number of sampled stages specified with \"files_sample_age\" (if any).\n");
214 
215  // set the matrix dimensions according to previous parameters
217 
218  // copy input values with expansion if dimensions don't match
219  _sample_size.copy_recycle(tmp_mat);
220 
221  } else {
222  // copy the sample size to all patches and age-classes sampled
223  _sample_size.reset(_sample_patch_num, _sample_age.ncols(), get_parameter("files_sample_size")->getValue());
224 
225  }
226 
227  return true;
228 }
unsigned int getNumAgeClasses()
Definition: metapop.h:289
unsigned int getPatchNbr()
Definition: metapop.h:275
bool isMatrix(string name)
Check if the parameter "name" is of matrix type.
Definition: param.h:295
bool isSet()
Accessor to the status flag.
Definition: param.h:287
ParamSet * _paramSet
The parameters container.
Definition: simcomponent.h:47
unsigned int ncols() const
Definition: tmatrix.h:215
void matrix_increment(double value)
Adds a value to all elements in a matrix.
Definition: tmatrix.h:263
void copy_recycle(const TMatrix &mat)
Copy elements of 'mat', recycling elements of 'mat' if its size is smaller than current matrix.
Definition: tmatrix.h:89
unsigned int nrows() const
Definition: tmatrix.h:212

References SimComponent::_paramSet, LifeCycleEvent::_popPtr, _sample_age, _sample_patch, _sample_patch_num, _sample_size, TMatrix::copy_recycle(), error(), SimComponent::get_parameter(), SimComponent::get_parameter_value(), Param::getMatrix(), Metapop::getNumAgeClasses(), Metapop::getPatchNbr(), ParamSet::isMatrix(), ParamSet::isSet(), TMatrix::matrix_increment(), TMatrix::ncols(), TMatrix::nrows(), TMatrix::reset(), and TMatrix::set().

Referenced by LCE_FileServicesNotifier(), and setParameters().

+ Here is the caller graph for this function:

Member Data Documentation

◆ _genotypers

vector< GenotyperFH* > LCE_FileServicesNotifier::_genotypers
private

◆ _sample_age

TMatrix LCE_FileServicesNotifier::_sample_age
private

◆ _sample_patch

TMatrix LCE_FileServicesNotifier::_sample_patch
private

◆ _sample_patch_num

unsigned int LCE_FileServicesNotifier::_sample_patch_num
private

◆ _sample_size

TMatrix LCE_FileServicesNotifier::_sample_size
private

◆ _service

FileServices* LCE_FileServicesNotifier::_service
private

Referenced by execute(), and loadFileServices().


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

Generated for Nemo v2.4.0 by  doxygen 1.9.1 -- Nemo is hosted on  Download Nemo

Locations of visitors to this page
Catalogued on GSR