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

TTQuantiFH. More...

#include <ttquanti.h>

+ Inheritance diagram for TTQuantiFH:
+ Collaboration diagram for TTQuantiFH:

Public Member Functions

 TTQuantiFH (TProtoQuanti *T)
 
virtual ~TTQuantiFH ()
 
void setOutputOption (string opt)
 
virtual void FHwrite ()
 
void write_TABLE ()
 
void print (ofstream &FH, Patch *patch, sex_t SEX, age_idx Ax, unsigned int print_gene, bool print_genotype, bool print_additive_genotype)
 
void write_PLINK ()
 
void print_PLINK_PED (ofstream &FH, age_idx Ax, Patch *patch)
 
void print_PLINK_FAM (ofstream &FH, age_idx Ax, Patch *patch)
 
virtual void FHread (string &filename)
 
- Public Member Functions inherited from TraitFileHandler< TProtoQuanti >
 TraitFileHandler (TProtoQuanti *trait_proto, const char *ext)
 
virtual ~TraitFileHandler ()
 
virtual void FHread (string &filename)=0
 
virtual void set (bool rpl_per, bool gen_per, int rpl_occ, int gen_occ, int rank, string path, TProtoQuanti *trait_proto)
 
virtual void set_multi (bool rpl_per, bool gen_per, int rpl_occ, TMatrix *Occ, string path, TProtoQuanti *trait_proto)
 
- Public Member Functions inherited from FileHandler
 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 update ()
 Updates the inner replicate and generation counters and calls FHwrite if needed by the the periodicity of the file. More...
 
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 ()
 

Private Attributes

string _output_option
 
bool _has_genetic_map
 

Additional Inherited Members

- Protected Attributes inherited from TraitFileHandler< TProtoQuanti >
TProtoQuanti_FHLinkedTrait
 
int _FHLinkedTraitIndex
 
- Protected Attributes inherited from FileHandler
Metapop_pop
 Pointer to the current metapop, set during initialization within the init function. More...
 

Detailed Description

Constructor & Destructor Documentation

◆ TTQuantiFH()

TTQuantiFH::TTQuantiFH ( TProtoQuanti T)
inline
816  : TraitFileHandler<TProtoQuanti>(T,".quanti"),
817  _has_genetic_map(0) {}
bool _has_genetic_map
Definition: ttquanti.h:813

◆ ~TTQuantiFH()

virtual TTQuantiFH::~TTQuantiFH ( )
inlinevirtual
818 {}

Member Function Documentation

◆ FHread()

void TTQuantiFH::FHread ( string &  filename)
virtual

Implements FileHandler.

6193 {
6194 // unsigned int nb_locus = _FHLinkedTrait->get_num_locus();
6195  unsigned int nb_trait = _FHLinkedTrait->get_num_traits();
6196  unsigned int seqLength = _FHLinkedTrait->get_seq_length();
6197  bool has_genotype = (!_FHLinkedTrait->get_env_var().empty());
6198  bool has_additive_genotype = (_FHLinkedTrait->get_dominance_model() != 0);
6199 
6200  unsigned int patchNbr = _pop->getPatchNbr();
6201 
6202  TTQuanti_continuous* trait;
6203  TTQuanti* DA_trait;
6204 
6205  ifstream FILE(filename.c_str(),ios::in);
6206 
6207  if(!FILE) fatal("could not open QUANTI input file \"%s\"\n",filename.c_str());
6208 
6209  unsigned int age, sex, ped, origin, dummy;
6210  unsigned int loc;
6211  age_idx stagex;
6212  unsigned int pop;
6213 // age_idx agex;
6214  Individual *ind;
6215  double all0, all1, pheno, geno;
6216  double *seq[2];
6217  seq[0] = new double [seqLength];
6218  seq[1] = new double [seqLength];
6219 
6220  unsigned char *binseq[2];
6221  binseq[0] = new unsigned char [seqLength];
6222  binseq[1] = new unsigned char [seqLength];
6223 
6224  int lnbr = 1;
6225 
6226  // swallow the header
6227  FILE.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
6228 
6229  // swallow the second line if it stores the dominance effects:
6230  if(has_additive_genotype)
6231  FILE.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
6232 
6233  string str;
6234 
6235  while(FILE>>pop) {
6236 
6237  if(FILE.bad()) {
6238  error("Reading input genotypes from \"%s\" failed at line %i.\n",filename.c_str(), lnbr);
6239  FILE.clear();
6240  FILE >> str;
6241  fatal("Expecting population number as first element on the line, but received this: %s \n", str.c_str());
6242  }
6243 
6244  if(pop > patchNbr)
6245  fatal("Patch number found in file exceeds number of patches in the population.\n");
6246 
6247  for(unsigned int k = 0; k < nb_trait; ++k) {
6248  for(unsigned int i = 0; i < _FHLinkedTrait->get_num_locus(k); ++i) {
6249 
6250  loc = _FHLinkedTrait->get_locus_seq_pos(i, k);
6251 
6252  FILE>>all0>>all1;
6253 
6254  if(FILE.bad())
6255  fatal("Reading input genotypes from \"%s\" failed at line %i.\n",filename.c_str(), lnbr);
6256 
6257  seq[0][loc] = all0;
6258  seq[1][loc] = all1;
6259  }
6260  }
6261 
6262 
6263  for(unsigned int k = 0; k < nb_trait; k++) {
6264  FILE >> pheno; //read phenotypic value
6265  if(has_genotype) FILE >> geno; // genotypic values, only present if trait is influenced by env. var.
6266  if(has_additive_genotype) FILE >> geno; // geno unused;
6267  }
6268 
6269  // read extra fields
6270  FILE >> age >> sex >> origin >> ped >> dummy >> dummy >> dummy >> dummy;
6271 
6272  stagex = age_idx(age);
6273 
6274  ind = _pop->makeNewIndividual(0, 0, sex_t(sex), origin - 1);
6275  ind->setPedigreeClass((unsigned char)ped);
6276  ind->setAge(age);
6277 
6278  if(_FHLinkedTrait->get_allele_model() > 2) { // non-diallelic loci
6279 
6280  trait = dynamic_cast<TTQuanti_continuous*> (ind->getTrait(_FHLinkedTraitIndex));
6281  trait->set_sequence((void**)seq);
6282  trait->set_value();
6283 
6284  } else { // diallelic loci
6285 
6286  DA_trait = dynamic_cast<TTQuanti*> (ind->getTrait(_FHLinkedTraitIndex));
6287 
6288  //have to make a binary sequence out of the data read:
6289  for (unsigned int l = 0; l < seqLength; ++l) {
6290 
6291  binseq[0][l] = (seq[0][l] != _FHLinkedTrait->get_seq_diallele_value(l, 0));
6292  binseq[1][l] = (seq[1][l] != _FHLinkedTrait->get_seq_diallele_value(l, 0));
6293 
6294  }
6295 
6296  DA_trait->set_sequence((void**)binseq);
6297  DA_trait->set_value();
6298  }
6299 
6300 
6301  // ind->show_up();
6302 
6303  _pop->getPatch(pop-1)->add(sex_t(sex), stagex, ind);
6304 
6305  lnbr++;
6306  }
6307 
6308 
6309  FILE.close();
6310 
6311  delete seq[0];
6312  delete seq[1];
6313  delete binseq[0];
6314  delete binseq[1];
6315 }
Metapop * _pop
Pointer to the current metapop, set during initialization within the init function.
Definition: filehandler.h:103
Individual * makeNewIndividual(Individual *newind, Individual *mother, Individual *father, sex_t sex, unsigned short homepatch)
Creates an individual from existing pointer with new ID.
Definition: indfactory.cc:152
This class contains traits along with other individual information (sex, pedigree,...
Definition: individual.h:49
void setPedigreeClass(Individual *mother, Individual *father)
Definition: individual.h:115
TTrait * getTrait(IDX T)
Trait accessor.
Definition: individual.h:277
void setAge(unsigned short value)
Definition: individual.h:105
unsigned int getPatchNbr()
Definition: metapop.h:276
Patch * getPatch(unsigned int i)
Patch accessor, return the ith+1 patch in the metapop.
Definition: metapop.h:257
void add(sex_t SEX, age_idx AGE, Individual *ind)
Adds an individual to the appropriate container, increments its size, eventually resizing it.
Definition: metapop.h:551
double get_seq_diallele_value(unsigned int position, unsigned int allele)
Definition: ttquanti.h:438
unsigned int get_allele_model()
Definition: ttquanti.h:434
unsigned int get_locus_seq_pos(unsigned int loc, unsigned int trait)
Definition: ttquanti.h:453
unsigned int get_num_locus()
Definition: ttquanti.h:424
unsigned int get_seq_length()
Definition: ttquanti.h:427
unsigned int get_num_traits()
Definition: ttquanti.h:423
unsigned int get_dominance_model()
Definition: ttquanti.h:478
vector< double > get_env_var()
Definition: ttquanti.h:430
TTQuanti_continuous.
Definition: ttquanti.h:114
virtual void set_sequence(void **seq)
Definition: ttquanti.cc:3300
TTQuanti.
Definition: ttquanti.h:61
virtual void set_value()
Definition: ttquanti.cc:3125
virtual void set_sequence(void **seq)=0
Called to set the sequence pointer to an existing trait.
int _FHLinkedTraitIndex
Definition: filehandler.h:224
TProtoQuanti * _FHLinkedTrait
Definition: filehandler.h:223
void fatal(const char *str,...)
Definition: output.cc:100
int error(const char *str,...)
Definition: output.cc:79
sex_t
Sex types, males are always 0 and females 1!!
Definition: types.h:36
age_idx
Array index of the age classes in the patch sizes and containers arrays.
Definition: types.h:41

References TraitFileHandler< TProtoQuanti >::_FHLinkedTrait, TraitFileHandler< TProtoQuanti >::_FHLinkedTraitIndex, FileHandler::_pop, Patch::add(), error(), fatal(), TProtoQuanti::get_allele_model(), TProtoQuanti::get_dominance_model(), TProtoQuanti::get_env_var(), TProtoQuanti::get_locus_seq_pos(), TProtoQuanti::get_num_locus(), TProtoQuanti::get_num_traits(), TProtoQuanti::get_seq_diallele_value(), TProtoQuanti::get_seq_length(), Metapop::getPatch(), Metapop::getPatchNbr(), Individual::getTrait(), IndFactory::makeNewIndividual(), TTQuanti_continuous::set_sequence(), TTrait::set_sequence(), TTQuanti::set_value(), Individual::setAge(), and Individual::setPedigreeClass().

◆ FHwrite()

void TTQuantiFH::FHwrite ( )
virtual

Implements TraitFileHandler< TProtoQuanti >.

5700 {
5701  if (!_pop->isAlive()) return;
5702 
5703  if(!get_service()) fatal("TTQuantiFH: link to file services not set!!\n");
5704 
5705  // sets the pop ptr to a sub sampled pop, if sub sampling happened
5707 
5708  if(_output_option == "plink")
5709  write_PLINK();
5710  else
5711  write_TABLE();
5712 
5713 
5714  // reset to pop ptr to main pop:
5715  _pop = get_service()->get_pop_ptr();
5716 
5717 }
FileServices * get_service()
Returns pointer to the FileServices.
Definition: filehandler.h:139
Metapop * getSampledPop()
Sets the down-sampled population and provides accessor to file handlers.
Definition: fileservices.cc:198
virtual Metapop * get_pop_ptr()
Accessor to the pointer to the main population.
Definition: fileservices.h:113
bool isAlive()
Checks if the population still contains at least one individual in any sex or age class.
Definition: metapop.h:309
void write_PLINK()
Definition: ttquanti.cc:5885
void write_TABLE()
Definition: ttquanti.cc:5721
string _output_option
Definition: ttquanti.h:812

References _output_option, FileHandler::_pop, fatal(), FileServices::get_pop_ptr(), FileHandler::get_service(), FileServices::getSampledPop(), Metapop::isAlive(), write_PLINK(), and write_TABLE().

◆ print()

void TTQuantiFH::print ( ofstream &  FH,
Patch patch,
sex_t  SEX,
age_idx  Ax,
unsigned int  print_gene,
bool  print_genotype,
bool  print_additive_genotype 
)
5817 {
5818  Individual* ind;
5819  TTQuanti* trait;
5820 
5821  double* Tval;
5822 
5823  unsigned int pos, nb_trait=_FHLinkedTrait->get_num_traits();
5824 
5825  for(unsigned int j = 0, size = patch->size(SEX, Ax); j < size; j++) {
5826 
5827  ind = patch->get(SEX, Ax, j);
5828  trait = dynamic_cast<TTQuanti*> (ind->getTrait(_FHLinkedTraitIndex));
5829 
5830  FH<<patch->getID() +1<<" ";
5831 
5832 
5833  if(print_gene) { // print the genotype values at each locus
5834 
5835  FH.precision(6);
5836 
5837  for(unsigned int k = 0; k < nb_trait; k++) {
5838  for(unsigned int l = 0; l < _FHLinkedTrait->get_num_locus(k); l++) {
5839 
5840  pos = _FHLinkedTrait->get_locus_seq_pos(l, k);
5841 
5842  if(print_gene == 1) { // print the allelic values, not the allelic state
5843 
5844  FH << trait->get_allele_value(pos, FEM) <<" "
5845  << trait->get_allele_value(pos, MAL) <<" ";
5846 
5847  } else { // must be option 2 or more; print the genotype state, 0/1/2
5848 
5849  //SNP genotypes, for di-allelic loci only (checked already)
5850 
5851  if(print_gene == 2) {
5852  // SNP genotypes are 0/1/2 for 00/01/11 genotypes
5853  FH << trait->get_allele_bit(pos, FEM) + trait->get_allele_bit(pos, MAL) << " ";
5854  } else {
5855  // print the allele ID 0/1
5856  FH << trait->get_allele_bit(pos, FEM)<< " "
5857  << trait->get_allele_bit(pos, MAL) << " ";
5858  }
5859  }
5860  }
5861  }
5862  }
5863 
5864  FH.precision(4);
5865 
5866  Tval = (double*)trait->getValue();
5867 
5868  for(unsigned int k = 0; k < nb_trait; k++) {
5869  FH<<Tval[k]<<" ";
5870  if(print_genotype) FH << trait->get_full_genotype(k) << " ";
5871  if(print_additive_genotype) FH << trait->get_additive_genotype(k) << " ";
5872  }
5873 
5874  FH<<Ax<<" "<<ind->getSex()<<" "<<ind->getHome()+1<<" "<<ind->getPedigreeClass()<<" "
5875  << (ind->getFather() && ind->getMother() ?
5876  (ind->getFather()->getHome()!= patch->getID() ) + (ind->getMother()->getHome()!= patch->getID() ) : 0)
5877  <<" "<<ind->getFatherID()<<" "<<ind->getMotherID()<<" "<<ind->getID()<<std::endl;
5878  }
5879 
5880 }
unsigned long getID()
Definition: individual.h:122
unsigned short getHome()
Definition: individual.h:128
Individual * getMother()
Definition: individual.h:127
unsigned long getMotherID()
Definition: individual.h:125
Individual * getFather()
Definition: individual.h:126
sex_t getSex()
Definition: individual.h:129
unsigned int getPedigreeClass()
Returns the pedigree class of the individual, as set during offspring creation.
Definition: individual.h:179
unsigned long getFatherID()
Definition: individual.h:124
unsigned int size(age_t AGE)
Returns the size of the container of the appropriate age class(es) for both sexes.
Definition: metapop.h:498
Individual * get(sex_t SEX, age_idx AGE, unsigned int at)
Returns a pointer to the individual sitting at the index passed.
Definition: metapop.h:534
unsigned int getID()
Definition: metapop.h:481
virtual bool get_allele_bit(unsigned int position, unsigned int allele) const =0
virtual double get_additive_genotype(const unsigned int trait) const =0
virtual void * getValue() const
Definition: ttquanti.h:79
virtual double get_full_genotype(unsigned int trait)=0
virtual double get_allele_value(int loc, int all) const =0
Called to read the value of the allele at a particular locus.
@ FEM
Definition: types.h:37
@ MAL
Definition: types.h:37

References TraitFileHandler< TProtoQuanti >::_FHLinkedTrait, TraitFileHandler< TProtoQuanti >::_FHLinkedTraitIndex, FEM, Patch::get(), TTQuanti::get_additive_genotype(), TTQuanti::get_allele_bit(), TTrait::get_allele_value(), TTQuanti::get_full_genotype(), TProtoQuanti::get_locus_seq_pos(), TProtoQuanti::get_num_locus(), TProtoQuanti::get_num_traits(), Individual::getFather(), Individual::getFatherID(), Individual::getHome(), Individual::getID(), Patch::getID(), Individual::getMother(), Individual::getMotherID(), Individual::getPedigreeClass(), Individual::getSex(), Individual::getTrait(), TTQuanti::getValue(), MAL, and Patch::size().

Referenced by write_TABLE().

◆ print_PLINK_FAM()

void TTQuantiFH::print_PLINK_FAM ( ofstream &  FH,
age_idx  Ax,
Patch patch 
)
6118 {
6119  Individual *ind;
6120  unsigned int nb_trait = _FHLinkedTrait->get_num_traits();
6121 
6122  // SPECIFICATION FOR THE .fam FILE = 6 first values in .ped files:
6123  // .fam (PLINK sample information file)
6124  //
6125  // Sample information file accompanying a .bed binary genotype table.
6126  // Also generated by "--recode lgen" and "--recode rlist".
6127  //
6128  // A text file with no header line, and one line per sample with the following six fields:
6129  //
6130  // 1. Family ID ('FID')
6131  // 2. Within-family ID ('IID'; cannot be '0')
6132  // 3. Within-family ID of father ('0' if father isn't in dataset)
6133  // 4. Within-family ID of mother ('0' if mother isn't in dataset)
6134  // 5. Sex code ('1' = male, '2' = female, '0' = unknown)
6135  // 6. Phenotype value ('1' = control, '2' = case, '-9'/'0'/non-numeric = missing data if case/control)
6136  //
6137  // If there are any numeric phenotype values other than {-9, 0, 1, 2}, the phenotype is interpreted as a
6138  // quantitative trait instead of case/control status. In this case, -9 normally still designates a missing
6139  // phenotype; use --missing-phenotype if this is problematic.
6140 
6141 
6142  // here, we save the phenotypic value of the first trait by default
6143  // the complete set of phenotypic values for all traits is saved in the .fam file
6144 
6145  for (unsigned int j = 0; j < patch->size(FEM, Ax); ++j) {
6146 
6147  ind = patch->get(FEM, Ax, j);
6148 
6149  FH<<"fam"<< ind->getHome()+1
6150  <<" "<< ind->getID();
6151 
6152  if(Ax == OFFSx)
6153  FH<<" "<<ind->getFatherID()<<" "<<ind->getMotherID(); //parents may be in file for offspring, although not guaranteed
6154  else
6155  FH<<" 0 0"; //parents not in file for adults
6156 
6157  FH<<" 2";
6158 
6159  for(unsigned int k = 0; k < nb_trait; ++k) {
6160  FH << " " << ((double*)ind->getTraitValue(_FHLinkedTraitIndex))[k];
6161  }
6162 
6163  FH <<std::endl;
6164 
6165  }
6166 
6167  for (unsigned int j = 0; j < patch->size(MAL, Ax); ++j) {
6168 
6169  ind = patch->get(MAL, Ax, j);
6170 
6171  FH<<"fam"<< ind->getHome()+1
6172  <<" "<< ind->getID();
6173 
6174  if(Ax == OFFSx)
6175  FH<<" "<<ind->getFatherID()<<" "<<ind->getMotherID(); //parents may be in file for offspring, although not guaranteed
6176  else
6177  FH<<" 0 0"; //parents not in file for adults
6178 
6179  FH<<" 1";
6180 
6181  for(unsigned int k = 0; k < nb_trait; ++k) {
6182  FH << " " << ((double*)ind->getTraitValue(_FHLinkedTraitIndex))[k];
6183  }
6184 
6185  FH<<std::endl;
6186 
6187  }
6188 }
void * getTraitValue(IDX T)
Accessor to the value (phenotype) of a particular trait.
Definition: individual.h:271
@ OFFSx
Definition: types.h:42

References TraitFileHandler< TProtoQuanti >::_FHLinkedTrait, TraitFileHandler< TProtoQuanti >::_FHLinkedTraitIndex, FEM, Patch::get(), TProtoQuanti::get_num_traits(), Individual::getFatherID(), Individual::getHome(), Individual::getID(), Individual::getMotherID(), Individual::getTraitValue(), MAL, OFFSx, and Patch::size().

Referenced by write_PLINK().

◆ print_PLINK_PED()

void TTQuantiFH::print_PLINK_PED ( ofstream &  FH,
age_idx  Ax,
Patch patch 
)
6021 {
6022  Individual *ind;
6023 // double** seq;
6024  TTQuanti* trait;
6025  char BASE[2] = {'A','G'};
6026 // const TMatrix& ref_all = _FHLinkedTrait->get_diallele_values();
6027  unsigned int nb_trait = _FHLinkedTrait->get_num_traits(), pos;
6028 
6029  // SPECIFICATION FOR THE .fam FILE = 6 first values in .ped files:
6030  // .fam (PLINK sample information file)
6031  //
6032  // Sample information file accompanying a .bed binary genotype table.
6033  // Also generated by "--recode lgen" and "--recode rlist".
6034  //
6035  // A text file with no header line, and one line per sample with the following six fields:
6036  //
6037  // 1. Family ID ('FID')
6038  // 2. Within-family ID ('IID'; cannot be '0')
6039  // 3. Within-family ID of father ('0' if father isn't in dataset)
6040  // 4. Within-family ID of mother ('0' if mother isn't in dataset)
6041  // 5. Sex code ('1' = male, '2' = female, '0' = unknown)
6042  // 6. Phenotype value ('1' = control, '2' = case, '-9'/'0'/non-numeric = missing data if case/control)
6043  //
6044  // If there are any numeric phenotype values other than {-9, 0, 1, 2}, the phenotype is interpreted as a quantitative trait instead of case/control status. In this case, -9 normally still designates a missing phenotype; use --missing-phenotype if this is problematic.
6045 
6046 
6047  // here, we save the phenotypic value of the first trait by default
6048  // the complete set of phenotypic values for all traits is saved in the .fam file
6049 
6050  for (unsigned int j = 0; j < patch->size(FEM, Ax); ++j) {
6051 
6052  ind = patch->get(FEM, Ax, j);
6053 
6054  FH<<"fam"<<ind->getHome()+1
6055  <<" "<<ind->getID(); // don't add +1 to keep IDs consistent across files
6056 
6057  if(Ax == OFFSx)
6058  FH<<" "<<ind->getFatherID()<<" "<<ind->getMotherID(); //parents may be in file for offspring, although not guaranteed
6059  else
6060  FH<<" 0 0"; //parents not in file for adults
6061 
6062  FH<<" 2 "
6063  <<((double*)ind->getTraitValue(_FHLinkedTraitIndex))[0]<<" ";
6064 
6065  trait = dynamic_cast<TTQuanti*>(ind->getTrait(_FHLinkedTraitIndex));
6066 
6067  for(unsigned int k = 0; k < nb_trait; ++k) {
6068  for (unsigned int l = 0; l < _FHLinkedTrait->get_num_locus(k); ++l) {
6069 
6070  pos = _FHLinkedTrait->get_locus_seq_pos(l, k);
6071 
6072 
6073  FH << BASE[ trait->get_allele_bit(pos, FEM)] << " "
6074  << BASE[ trait->get_allele_bit(pos, MAL)] << " ";
6075 
6076  }
6077  }
6078 
6079  FH <<std::endl;
6080 
6081  }
6082 
6083  for (unsigned int j = 0; j < patch->size(MAL, Ax); ++j) {
6084 
6085  ind = patch->get(MAL, Ax, j);
6086 
6087  FH<<"fam"<<ind->getHome()+1
6088  <<" "<<ind->getID();
6089 
6090  if(Ax == OFFSx)
6091  FH<<" "<<ind->getFatherID()<<" "<<ind->getMotherID(); //parents may be in file for offspring, although not guaranteed
6092  else
6093  FH<<" 0 0"; //parents not in file for adults
6094 
6095  FH<<" 1 "
6096  <<((double*)ind->getTraitValue(_FHLinkedTraitIndex))[0]<<" ";
6097 
6098  trait = dynamic_cast<TTQuanti*>(ind->getTrait(_FHLinkedTraitIndex));
6099 
6100  for(unsigned int k = 0; k < nb_trait; ++k) {
6101  for (unsigned int l = 0; l < _FHLinkedTrait->get_num_locus(k); ++l) {
6102 
6103  pos = _FHLinkedTrait->get_locus_seq_pos(l, k);
6104 
6105  FH << BASE[ trait->get_allele_bit(pos, FEM)] << " "
6106  << BASE[ trait->get_allele_bit(pos, MAL)] << " ";
6107 
6108  }
6109  }
6110  FH<<std::endl;
6111 
6112  }
6113 }

References TraitFileHandler< TProtoQuanti >::_FHLinkedTrait, TraitFileHandler< TProtoQuanti >::_FHLinkedTraitIndex, FEM, Patch::get(), TProtoQuanti::get_locus_seq_pos(), TProtoQuanti::get_num_locus(), TProtoQuanti::get_num_traits(), Individual::getFatherID(), Individual::getHome(), Individual::getID(), Individual::getMotherID(), Individual::getTrait(), Individual::getTraitValue(), MAL, OFFSx, and Patch::size().

Referenced by write_PLINK().

◆ setOutputOption()

void TTQuantiFH::setOutputOption ( string  opt)
5678 {
5679  _output_option = opt;
5680 
5681  std::for_each(_output_option.begin(), _output_option.end(), [](char& c){c = ::tolower(c);});
5682 
5683  if( !(_output_option == "genotypes" || _output_option == "genotype" ||
5684  _output_option == "snp_genotypes" || _output_option == "snp_genotype" ||
5685  _output_option == "snp_id" || _output_option == "plink" ||
5686  _output_option == "1" || _output_option == "0")) {
5687  fatal("option \"%s\" to parameter \"quanti_output\" is not recognized\n", opt.c_str());
5688  }
5689 
5691 
5692  if(_output_option == "plink" && !_has_genetic_map)
5693  warning("PLINK .map file for quant trait: no genetic map found for QTL, we assume they are unlinked, separated by 50M and on a single chromosome.\n");
5694 
5695 }
bool checkRegisteredTrait(trait_t trait)
Returns true if trait 'trait' has registered a genetic map, false otherwise.
Definition: ttrait_with_map.cc:674
virtual trait_t get_type() const
Definition: ttquanti.h:576
static GeneticMap _map
Definition: ttrait_with_map.h:209
void warning(const char *str,...)
Definition: output.cc:58
std::string trait_t
Trait types.
Definition: types.h:63

References TraitFileHandler< TProtoQuanti >::_FHLinkedTrait, _has_genetic_map, TTProtoWithMap::_map, _output_option, GeneticMap::checkRegisteredTrait(), fatal(), TProtoQuanti::get_type(), and warning().

Referenced by TProtoQuanti::loadFileServices().

◆ write_PLINK()

void TTQuantiFH::write_PLINK ( )
5886 {
5887  if(_FHLinkedTrait->get_allele_model() > 2)
5888  fatal("PLINK file output for the quanti trait is only possible for di-allelic loci.\n");
5889 
5890  unsigned int patchNbr = _pop->getPatchNbr();
5891  Patch* current_patch;
5892 
5893  std::string filename = get_path() + this->get_service()->getGenerationReplicateFileName() + "-quanti.ped";
5894 
5895  // the PED file -------------------------------------------------------------------------
5896  ofstream PED (filename.c_str(), ios::out);
5897  std::ios_base::sync_with_stdio(false); // better for writing performances
5898 
5899  if(!PED) fatal("could not open plink .ped output file!!\n");
5900 
5901 #ifdef _DEBUG_
5902  message("TTQuantiFH::write_PLINK (%s)\n",filename.c_str());
5903 #endif
5904 
5905  for (unsigned int i = 0; i < patchNbr; ++i) {
5906 
5907  current_patch = _pop->getPatch(i);
5908 
5909  if( _pop->getCurrentAge() & OFFSPRG)
5910  print_PLINK_PED(PED, OFFSx, current_patch);
5911 
5912  if( _pop->getCurrentAge() & ADULTS) {
5913  for(unsigned int a = 1; a < _pop->getNumAgeClasses(); ++a)
5914  print_PLINK_PED(PED, age_idx(a), current_patch);
5915  }
5916 
5917  }
5918 
5919  PED.close();
5920 
5921  // the FAM file -------------------------------------------------------------------------
5922  // we write the .fam file only when more than one trait; phenotype columns are added here
5923 
5924  if(_FHLinkedTrait->get_num_traits() > 1) {
5925 
5926  filename = get_path() + this->get_service()->getGenerationReplicateFileName() + "-quanti.fam";
5927 
5928  ofstream FAM (filename.c_str(), ios::out);
5929  std::ios_base::sync_with_stdio(false); // better for writing performances
5930 
5931  if(!FAM) fatal("could not open plink .fam output file!!\n");
5932 
5933 #ifdef _DEBUG_
5934  message("TTQuantiFH::write_PLINK (%s)\n",filename.c_str());
5935 #endif
5936 
5937  for (unsigned int i = 0; i < patchNbr; ++i) {
5938 
5939  current_patch = _pop->getPatch(i);
5940 
5941  if( _pop->getCurrentAge() & OFFSPRG)
5942  print_PLINK_FAM(FAM, OFFSx, current_patch);
5943 
5944  if( _pop->getCurrentAge() & ADULTS){
5945  for(unsigned int a = 1; a < _pop->getNumAgeClasses(); ++a)
5946  print_PLINK_FAM(FAM, age_idx(a), current_patch);
5947  }
5948 
5949  }
5950 
5951  FAM.close();
5952  }
5953 
5954  // the MAP file -------------------------------------------------------------------------
5955  // !! careful here with pleiotropy, same locus will be registered for each trait it affects
5956  // output for pleiotropic locis has not be tested with PLINK
5957 
5958  filename = get_path() + this->get_service()->getGenerationReplicateFileName() + "-quanti.map";
5959 
5960  ofstream MAP (filename.c_str(), ios::out);
5961 
5962  if(!MAP) fatal("could not open plink .map output file!!\n");
5963 
5964 #ifdef _DEBUG_
5965  message("TTQuantiFH::write_PLINK (%s)\n",filename.c_str());
5966 #endif
5967 
5968 
5969  if( _has_genetic_map ) {
5970 
5971  unsigned int nb_locus = _FHLinkedTrait->get_num_locus();
5972 
5973  double **map;
5974  map = new double* [nb_locus];
5975  for(unsigned int k = 0; k < nb_locus; ++k)
5976  map[k] = new double[2]; // [0]: chr; [1]: map pos in cM
5977 
5979 
5980  for(unsigned int k = 0; k < _FHLinkedTrait->get_num_traits() ; ++k) {
5981  for (unsigned int l = 0, LOCUS; l < _FHLinkedTrait->get_num_locus(k); ++l) { // this will give the locus ID
5982 
5983  LOCUS = _FHLinkedTrait->get_locus_ID(l, k);
5984 
5985  // MAP FORMAT (PLINK1.9): chrmsm ID; Loc ID; position (cM); bp ID
5986  MAP <<map[ LOCUS ][FEM]+1
5987  <<" T"<< k + 1 <<"loc"<< LOCUS + 1 <<" "
5988  <<map[LOCUS][MAL]<<" "<< LOCUS + 1 <<endl;
5989 
5990  }
5991  }
5992 
5993 
5994  for(unsigned int k = 0; k < nb_locus; ++k) delete [] map[k];
5995  delete [] map;
5996 
5997  } else { // trait didn't register a genetic map, loci are unlinked (free recombination)
5998 
5999  // we're gonna set all loci on a single chrmsme, but 50M apart
6000  for(unsigned int k = 0; k < _FHLinkedTrait->get_num_traits() ; ++k) { //_FHLinkedTrait->get_num_traits()
6001  for (unsigned int l = 0, LOCUS; l < _FHLinkedTrait->get_num_locus(k); ++l) { // this will give the locus ID
6002 
6003  LOCUS = _FHLinkedTrait->get_locus_ID(l, k) + 1;
6004 
6005  // write: chromosome, locus name, map position, locus number
6006  MAP<< 1 <<" T"<<k+1<<"loc"<< LOCUS <<" "<< l*5000.0 + 1.0 <<" "<< LOCUS <<endl;
6007 
6008  }
6009  }
6010 
6011  }
6012  MAP.close();
6013 
6014  std::ios_base::sync_with_stdio(true); // reset
6015 
6016 }
std::string & get_path()
Definition: filehandler.h:143
string getGenerationReplicateFileName()
Accessor to the current file name with generation and replicate counters added.
Definition: fileservices.cc:467
bool getGeneticMap(trait_t trait, double **table, unsigned int table_length)
Definition: ttrait_with_map.cc:915
unsigned int getNumAgeClasses()
Definition: metapop.h:290
age_t getCurrentAge()
Definition: metapop.h:299
Second class in the metapopulation design structure, between the Metapop and Individual classes.
Definition: metapop.h:432
unsigned int get_locus_ID(unsigned int locus, unsigned int trait)
Definition: ttquanti.h:455
void print_PLINK_FAM(ofstream &FH, age_idx Ax, Patch *patch)
Definition: ttquanti.cc:6117
void print_PLINK_PED(ofstream &FH, age_idx Ax, Patch *patch)
Definition: ttquanti.cc:6020
void message(const char *message,...)
Definition: output.cc:40
#define ADULTS
Adults age class flag (breeders).
Definition: types.h:54
#define OFFSPRG
Offspring age class flag.
Definition: types.h:50

References TraitFileHandler< TProtoQuanti >::_FHLinkedTrait, _has_genetic_map, TTProtoWithMap::_map, FileHandler::_pop, ADULTS, fatal(), FEM, TProtoQuanti::get_allele_model(), TProtoQuanti::get_locus_ID(), TProtoQuanti::get_num_locus(), TProtoQuanti::get_num_traits(), FileHandler::get_path(), FileHandler::get_service(), TProtoQuanti::get_type(), Metapop::getCurrentAge(), FileServices::getGenerationReplicateFileName(), GeneticMap::getGeneticMap(), Metapop::getNumAgeClasses(), Metapop::getPatch(), Metapop::getPatchNbr(), MAL, message(), OFFSPRG, OFFSx, print_PLINK_FAM(), and print_PLINK_PED().

Referenced by FHwrite().

◆ write_TABLE()

void TTQuantiFH::write_TABLE ( )
5722 {
5723  std::string filename = get_filename();
5724 
5725  std::ofstream FILE (filename.c_str(), ios::out);
5726  std::ios_base::sync_with_stdio(false); // better for writing performances
5727 
5728 
5729  if(!FILE) fatal("could not open \"%s\" output file!!\n",filename.c_str());
5730 
5731 #ifdef _DEBUG_
5732  message("TTQuantiFH::write_TABLE (%s)\n", filename.c_str());
5733 #endif
5734 
5735  //print the genotype at each locus
5736  unsigned int print_gene = 0;
5737 
5738  if(_output_option == "genotypes" || _output_option == "genotype")
5739  print_gene = 1;
5740  else if(_output_option == "snp_genotypes" || _output_option == "snp_genotype")
5741  print_gene = 2;
5742  else if(_output_option == "snp_id")
5743  print_gene = 3;
5744 
5745 
5746  //print the genotypic value of the individual in sus of the phenotype
5747  bool print_genotype = (!_FHLinkedTrait->get_env_var().empty()) || print_gene != 0;
5748 
5749  bool print_additive_genotype = (_FHLinkedTrait->get_dominance_model() != 0);
5750 
5751  // NOTE: it is not possible to separate total genotypic value from pure additive value in the epistatic case
5752 
5753  // First column is the patch ID:
5754  FILE<<"pop ";
5755 
5756  // print the allelic values?
5757  if(print_gene) {
5758 
5759  // we print allelic values ordered trait:locus, alleles at loci affecting one trait are grouped
5760  for(unsigned int k = 0; k < _FHLinkedTrait->get_num_traits() ; k++) {
5761  for(unsigned int l = 0, LOCUS; l < _FHLinkedTrait->get_num_locus(k) ; l++) {
5762 
5763  LOCUS = _FHLinkedTrait->get_locus_ID(l, k) + 1;
5764 
5765  if(print_gene == 1 || print_gene == 3) { // print two values per locus
5766  // "x" maternally inherited, "y" paternally inherited
5767  FILE<<"t"<<k+1<<"l"<< LOCUS <<"x "<<"t"<<k+1<<"l"<< LOCUS <<"y ";
5768 
5769  } else { // means > 1; print snp_genotype 0/1/2, instead of alleles state/value
5770 
5771  //check allele model:
5772  if(_FHLinkedTrait->get_allele_model() > 2)
5773  fatal("trait quant output::SNP genotypes can only be printed for di-allelic loci.\n");
5774 
5775  // snp-encoded genotypes: 0, 1, 2
5776  FILE<<"t"<<k+1<<"l"<< LOCUS <<" ";
5777  }
5778  }
5779  }
5780  }
5781 
5782  for(unsigned int k = 0; k < _FHLinkedTrait->get_num_traits(); k++) {
5783  FILE<<"P"<<k+1<< " ";
5784  if(print_genotype) FILE<<"G"<<k+1<< " ";
5785  if(print_additive_genotype) FILE<<"A"<<k+1<< " ";
5786  }
5787 
5788  FILE<<"age sex home ped isMigrant father mother ID\n";
5789 
5790 
5791  Patch* current_patch;
5792 
5793  for(unsigned int i = 0; i < _pop->getPatchNbr(); ++i) {
5794 
5795  current_patch = _pop->getPatch(i);
5796 
5797  if( current_patch->size(OFFSx) != 0 ) {
5798  print(FILE, current_patch, FEM, OFFSx, print_gene, print_genotype, print_additive_genotype);
5799  print(FILE, current_patch, MAL, OFFSx, print_gene, print_genotype, print_additive_genotype);
5800  }
5801 
5802  if( current_patch->size(ADLTx) != 0 ) {
5803  print(FILE, current_patch, FEM, ADLTx, print_gene, print_genotype, print_additive_genotype);
5804  print(FILE, current_patch, MAL, ADLTx, print_gene, print_genotype, print_additive_genotype);
5805  }
5806 
5807  }
5808 
5809  FILE.close();
5810  std::ios_base::sync_with_stdio(true); // reset
5811 
5812 }
std::string & get_filename()
Builds and returns the current file name depending on the periodicity of the file.
Definition: filehandler.cc:151
void print(ofstream &FH, Patch *patch, sex_t SEX, age_idx Ax, unsigned int print_gene, bool print_genotype, bool print_additive_genotype)
Definition: ttquanti.cc:5816
@ ADLTx
Definition: types.h:42

References TraitFileHandler< TProtoQuanti >::_FHLinkedTrait, _output_option, FileHandler::_pop, ADLTx, fatal(), FEM, TProtoQuanti::get_allele_model(), TProtoQuanti::get_dominance_model(), TProtoQuanti::get_env_var(), FileHandler::get_filename(), TProtoQuanti::get_locus_ID(), TProtoQuanti::get_num_locus(), TProtoQuanti::get_num_traits(), Metapop::getPatch(), Metapop::getPatchNbr(), MAL, message(), OFFSx, print(), and Patch::size().

Referenced by FHwrite().

Member Data Documentation

◆ _has_genetic_map

bool TTQuantiFH::_has_genetic_map
private

Referenced by setOutputOption(), and write_PLINK().

◆ _output_option

string TTQuantiFH::_output_option
private

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