Nemo  2.4.0b
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 ( )
63  : LifeCycleEvent("save_files",""),
65 {
66 
68 
69  add_parameter("files_sample_patch", MAT, false, false, 0, 0, sampler);
70  add_parameter("files_sample_size", INT, false, false, 0, 0, sampler);
71  add_parameter("files_sample_age", INT, false, false, 0, 0, sampler);
72 
74 
75  add_parameter("files_genotyper_traits", STR, false, false, 0, 0, genotyper);
76  add_parameter("files_genotyper_format", STR, false, false, 0, 0, genotyper);
77  add_parameter("files_genotyper_logtime", INT, false, false, 0, 0, genotyper);
78  add_parameter("files_genotyper_dir", STR, false, false, 0, 0, genotyper);
79  add_parameter("files_genotyper_trim_maf", DBL, false, false, 0, 0, genotyper);
80 
81 }
unsigned int _sample_patch_num
Definition: servicenotifiers.h:77
bool setSamplingParameters()
Definition: servicenotifiers.cc:134
FileServices * _service
Definition: servicenotifiers.h:75
bool setGenotyperParameters()
Definition: servicenotifiers.cc:233
LifeCycleEvent(const char *name, const char *trait_link)
Cstor.
Definition: lifecycleevent.h:98
Implementation of the ParamUpdaterBase interface.
Definition: param.h:363
virtual void add_parameter(Param *param)
Interface to add a parameter to the set.
Definition: simcomponent.h:112
@ DBL
Definition: types.h:78
@ MAT
Definition: types.h:78
@ STR
Definition: types.h:78
@ INT
Definition: types.h:78

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

Referenced by clone().

◆ ~LCE_FileServicesNotifier()

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

References _genotypers.

Member Function Documentation

◆ addAgeClass()

virtual age_t LCE_FileServicesNotifier::addAgeClass ( )
inlinevirtual

Implements LifeCycleEvent.

100 {return 0;}

◆ clone()

virtual LifeCycleEvent* LCE_FileServicesNotifier::clone ( )
inlinevirtual

Implements LifeCycleEvent.

93 {return new LCE_FileServicesNotifier();}
LCE_FileServicesNotifier()
Definition: servicenotifiers.cc:63

References LCE_FileServicesNotifier().

◆ execute()

void LCE_FileServicesNotifier::execute ( )
virtual

Implements LifeCycleEvent.

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

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

◆ loadFileServices()

void LCE_FileServicesNotifier::loadFileServices ( FileServices loader)
virtual

Implements SimComponent.

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

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.

97 {}

◆ removeAgeClass()

virtual age_t LCE_FileServicesNotifier::removeAgeClass ( )
inlinevirtual

Implements LifeCycleEvent.

99 {return 0;}

◆ requiredAgeClass()

virtual age_t LCE_FileServicesNotifier::requiredAgeClass ( )
inlinevirtual

Implements LifeCycleEvent.

101 {return 0;}

◆ resetParameterFromSource()

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

Implements SimComponent.

98 {return false;}

◆ setGenotyperParameters()

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

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().

◆ setParameters()

bool LCE_FileServicesNotifier::setParameters ( )
virtual

Implements SimComponent.

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

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

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().

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.0b by  doxygen 1.9.1 -- Nemo is hosted on  Download Nemo

Locations of visitors to this page
Catalogued on GSR