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

A file handler to save the neutral markers genotypes in the FSTAT format (extended). More...

#include <ttneutralgenes.h>

+ Inheritance diagram for TTNeutralGenesFH:
+ Collaboration diagram for TTNeutralGenesFH:

Public Member Functions

 TTNeutralGenesFH (TProtoNeutralGenes *TP)
 
virtual ~TTNeutralGenesFH ()
 
virtual void FHwrite ()
 
virtual void FHread (string &filename)
 
void write_TAB ()
 
void write_patch_TAB (Patch *patch, sex_t SEX, age_idx AGE, ofstream &FH)
 
void write_PLINK ()
 
void print_PLINK_PED (ofstream &FH, age_idx Ax, Patch *patch)
 
void write_PLINK_BED (ofstream &BED)
 
void write_GENEPOP ()
 
void write_patch_GENEPOP (Patch *patch, sex_t SEX, age_idx AGE, ofstream &FH, unsigned int digits)
 
void write_FSTAT ()
 
void write_patch_FSTAT (Patch *patch, sex_t SEX, age_idx AGE, ofstream &FH, unsigned int digits)
 
void write_Fst_i ()
 
void write_varcompWC ()
 
void setOutputOption (string opt)
 
void set_write_fct (void(TTNeutralGenesFH::*fct_ptr)())
 
- Public Member Functions inherited from TraitFileHandler< TProtoNeutralGenes >
 TraitFileHandler (TProtoNeutralGenes *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, TProtoNeutralGenes *trait_proto)
 
virtual void set_multi (bool rpl_per, bool gen_per, int rpl_occ, TMatrix *Occ, string path, TProtoNeutralGenes *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
 
void(TTNeutralGenesFH::* write_fct )()
 

Additional Inherited Members

- Protected Attributes inherited from TraitFileHandler< TProtoNeutralGenes >
TProtoNeutralGenes_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

A file handler to save the neutral markers genotypes in the FSTAT format (extended).

By default, the file extension is ".dat" for the genotype file. It is changed to ".fsti" for the per-locus/per-patch Weir& Hill Fst's and ".freq" for the per-locus/per-patch allele frequencies. Also implements the FileHandler::FHread method to load a population's genotypes from an FSTAT file (using the 'source_pop' population parameter).

Constructor & Destructor Documentation

◆ TTNeutralGenesFH()

TTNeutralGenesFH::TTNeutralGenesFH ( TProtoNeutralGenes TP)
inline
217  { }
void(TTNeutralGenesFH::* write_fct)()
Definition: ttneutralgenes.h:211

◆ ~TTNeutralGenesFH()

virtual TTNeutralGenesFH::~TTNeutralGenesFH ( )
inlinevirtual
219 { }

Member Function Documentation

◆ FHread()

void TTNeutralGenesFH::FHread ( string &  filename)
virtual

reads in only FSTAT format

Implements FileHandler.

1245 {
1247  // make sure we are using the whole pop, not a sub-sampled one:
1248 // Metapop *pop = get_service()->get_pop_ptr();
1249 
1250  unsigned int digit, nloci_infile, nall_infile, npatch_infile;
1251  // unsigned int ploidy = _FHLinkedTrait->get_ploidy();
1252  unsigned int nb_all = _FHLinkedTrait->get_allele_num();
1253  unsigned int nb_locus = _FHLinkedTrait->get_locus_num();
1254  unsigned int patchNbr = _pop->getPatchNbr();
1255  unsigned int extended_num_colums = 0;
1256 
1257  bool is_extended = false;
1258 
1259  ifstream FILE(filename.c_str(),ios::in);
1260 
1261  if(!FILE) fatal("could not open FSTAT input file \"%s\"\n", filename.c_str());
1262 
1263  FILE >> npatch_infile >> nloci_infile >> nall_infile >> digit;
1264 
1265  if(npatch_infile != patchNbr) fatal("number of patch in FSTAT file (%i) differs from simulation settings (%i)\n", npatch_infile, patchNbr);
1266 
1267  if (nloci_infile > nb_locus) {
1268 
1269  is_extended = true;
1270 
1271  extended_num_colums = nloci_infile - nb_locus;
1272 
1273  if(extended_num_colums != FSTAT_EXTRA_INFO_LOCI) {
1274  error("FSTAT input genotype file \"%s\" has a non-regular number of extra info loci of %i\n", filename.c_str(), extended_num_colums);
1275  error("Nemo expects %i extra info loci in the extented FSTAT format (age, sex, ped, origin)\n", FSTAT_EXTRA_INFO_LOCI);
1276  error("Extra info loci will be ignored\n");
1277  }
1278 
1279  } else if (nloci_infile == nb_locus){
1280 
1281  is_extended = false;
1282 
1283  } else {
1284 
1285  error("when reading the first line of the input FSTAT file \"%s\" \n", filename.c_str());
1286  fatal("please specify either (num locus) or (num locus + %i) as the second number on the first line of the input file.\n", FSTAT_EXTRA_INFO_LOCI);
1287 
1288  }
1289 
1290 
1291 
1292  if(nall_infile != nb_all) fatal("number of alleles in FSTAT file differs from simulation settings\n");
1293 
1294  digit = (int) pow(10.0,(double)digit);
1295 
1296  string str;
1297 
1298 
1299 #ifdef _DEBUG_
1300  message(">>>> -- FSTAT%s file: \n", (is_extended ? " extended" : ""));
1301  message(">>>> %i patch, %i loci, %i alleles, %i digits \n", npatch_infile, nloci_infile, nall_infile, digit);
1302 #endif
1303 
1304  // read the locus names
1305  for(unsigned int i = 0; i < nloci_infile; ++i) {
1306 
1307  FILE >> str;
1308 
1309  }
1310 
1311  unsigned int patch, genot, all0, all1, age, sex, ped, origin;
1312  age_idx agex;
1313  Individual *ind;
1314  unsigned char* seq[2];
1315  int lnbr = nloci_infile +2; //line counter
1316  seq[0] = new unsigned char[nb_locus];
1317  seq[1] = new unsigned char[nb_locus];
1318 
1319  while(FILE>>patch) {
1320 
1321  if(FILE.bad()) {
1322  error("Reading input genotypes from \"%s\" failed at line %i.\n",filename.c_str(), lnbr);
1323  FILE.clear();
1324  FILE >> str;
1325  fatal("Expecting population number as first element on the line, but received this: %s \n", str.c_str());
1326  }
1327 
1328  //check patch value
1329  if(patch > patchNbr) fatal("found an illegal patch identifier in FSTAT file (%i) at line %i",patch, lnbr);
1330 
1331  for(unsigned int i = 0; i < nb_locus; ++i) {
1332  FILE>>genot;
1333 
1334  all0 = genot/digit;
1335  all1 = genot%digit;
1336 
1337  if( all0 > nb_all ) {
1338 
1339  error("in FSTAT input file at line %i, locus %i : \
1340 first allele value %d is greater than the max value specified (%i)!\n", lnbr, i+1, all0, nb_all);
1341 
1342  fatal("Please check the input file.\n");
1343 
1344  } else if ( all0 > 0 ) {
1345 
1346  seq[0][i] = all0 - 1;
1347 
1348  } else {
1349 
1350  fatal("in FSTAT input file at line %i, locus %i first allele value: \
1351 allele value 0 is not allowed!\n*** Please check the input file.\n", lnbr, i+1, all0, nb_all);
1352 
1353  }
1354 
1355  if( all1 > nb_all ){
1356 
1357  error("in FSTAT input file at line %i, locus %i : \
1358 second allele value %i is greater than the max value specified (%i)!\n", lnbr, i+1, all1, nb_all);
1359 
1360  fatal("Please check the input file.\n");
1361 
1362  } else if (all1 > 0) {
1363 
1364  seq[1][i] = all1 - 1;
1365 
1366  } else {
1367 
1368  fatal("in FSTAT input file at line %i, locus %i second allele value: \
1369 allele value 0 is not allowed!\n*** Please check the input file.\n", lnbr, i+1, all0, nb_all);
1370 
1371  }
1372  }
1373 
1374  if(is_extended) {
1375 
1376  if(extended_num_colums == FSTAT_EXTRA_INFO_LOCI) {
1377 
1378  FILE >> age >> sex >> ped >> origin;
1379 
1380  //age index now saved in the FSTAT file
1381  agex = (static_cast<age_idx> (age) == ADLTx ? ADLTx : OFFSx);
1382 
1383  if(sex > 1)
1384  fatal("in FSTAT input file at line %i, extra column \"sex\" has irregular value %i, must be 0 or 1\n", lnbr, sex);
1385 
1386  } else { //irregular file
1387  getline(FILE, str); // we ignore the extra fields, and use default values
1388  agex = ADLTx;
1389  sex = FEM;
1390  ped = 0;
1391  origin = 1;
1392  }
1393 
1394  } else {
1395  // by default, individuals are adult females
1396  agex = ADLTx;
1397  sex = FEM;
1398  ped = 0;
1399  origin = 1;
1400 
1401  }
1402 
1403 
1404  ind = _pop->makeNewIndividual(0, 0, sex_t(sex), origin - 1);
1405  ind->setPedigreeClass((unsigned char)ped);
1406  ind->getTrait(_FHLinkedTraitIndex)->set_sequence((void**)seq);
1407 
1408  _pop->getPatch(patch-1)->add(sex_t(sex), agex, ind);
1409 
1410  lnbr++;
1411 
1412  }
1413 
1414  FILE.close();
1415 
1416 #ifdef _DEBUG_
1417  message(">>>> read FSTAT file with %i lines, %i individuals in pop \n", lnbr, _pop->size());
1418 #endif
1419 
1420  delete [] seq[0];
1421  delete [] seq[1];
1422 }
Metapop * _pop
Pointer to the current metapop, set during initialization within the init function.
Definition: filehandler.h:103
Individual * makeNewIndividual(Individual *mother, Individual *father, sex_t sex, unsigned short homepatch)
Creates an individual with pointers to parents, sex and home ID set but no genetic data.
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
unsigned int size()
Get the total number of individuals present in the population, all sex and age classes together.
Definition: metapop.h:312
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
unsigned int get_locus_num()
Definition: ttneutralgenes.h:159
unsigned int get_allele_num()
Definition: ttneutralgenes.h:160
virtual void set_sequence(void **seq)=0
Called to set the sequence pointer to an existing trait.
int _FHLinkedTraitIndex
Definition: filehandler.h:220
TProtoNeutralGenes * _FHLinkedTrait
Definition: filehandler.h:219
void fatal(const char *str,...)
Definition: output.cc:96
int error(const char *str,...)
Definition: output.cc:77
void message(const char *message,...)
Definition: output.cc:40
#define FSTAT_EXTRA_INFO_LOCI
Definition: ttneutralgenes.cc:43
sex_t
Sex types, males are always 0 and females 1!!
Definition: types.h:36
@ FEM
Definition: types.h:37
age_idx
Array index of the age classes in the patch sizes and containers arrays.
Definition: types.h:41
@ OFFSx
Definition: types.h:42
@ ADLTx
Definition: types.h:42

References TraitFileHandler< TProtoNeutralGenes >::_FHLinkedTrait, TraitFileHandler< TProtoNeutralGenes >::_FHLinkedTraitIndex, FileHandler::_pop, Patch::add(), ADLTx, error(), fatal(), FEM, FSTAT_EXTRA_INFO_LOCI, TProtoNeutralGenes::get_allele_num(), TProtoNeutralGenes::get_locus_num(), Metapop::getPatch(), Metapop::getPatchNbr(), Individual::getTrait(), IndFactory::makeNewIndividual(), message(), OFFSx, TTrait::set_sequence(), Individual::setPedigreeClass(), and Metapop::size().

◆ FHwrite()

void TTNeutralGenesFH::FHwrite ( )
virtual

Implements TraitFileHandler< TProtoNeutralGenes >.

808 {
809  if(!_pop->isAlive()) return;
810 
811  // reset the pop ptr from the file services, will be the main metapop without sub sampling
812  // or a sub sampled pop otherwise:
813 
815 
816  (this->*write_fct)();
817 
818  // reset the pop ptr to the main pop
820 
821 }
FileServices * get_service()
Returns pointer to the FileServices.
Definition: filehandler.h:135
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:111
bool isAlive()
Checks if the population still contains at least one individual in any sex or age class.
Definition: metapop.h:309

References FileHandler::_pop, FileServices::get_pop_ptr(), FileHandler::get_service(), FileServices::getSampledPop(), Metapop::isAlive(), and write_fct.

◆ print_PLINK_PED()

void TTNeutralGenesFH::print_PLINK_PED ( ofstream &  FH,
age_idx  Ax,
Patch patch 
)
1000 {
1001  Individual *ind;
1002  unsigned char** seq;
1003  char BASE[2] = {'A','G'};
1004  unsigned int loc;
1005  unsigned int ploidy = _FHLinkedTrait->get_ploidy();
1006  unsigned int nb_locus = _FHLinkedTrait->get_locus_num();
1007 
1008  // SPECIFICATION FOR THE .fam FILE = 6 first values in .ped files:
1009 // .fam (PLINK sample information file)
1010 //
1011 // Sample information file accompanying a .bed binary genotype table.
1012 // Also generated by "--recode lgen" and "--recode rlist".
1013 //
1014 // A text file with no header line, and one line per sample with the following six fields:
1015 //
1016 // 1. Family ID ('FID')
1017 // 2. Within-family ID ('IID'; cannot be '0')
1018 // 3. Within-family ID of father ('0' if father isn't in dataset)
1019 // 4. Within-family ID of mother ('0' if mother isn't in dataset)
1020 // 5. Sex code ('1' = male, '2' = female, '0' = unknown)
1021 // 6. Phenotype value ('1' = control, '2' = case, '-9'/'0'/non-numeric = missing data if case/control)
1022 //
1023 // 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.
1024 
1025 
1026  for (unsigned int j = 0; j < patch->size(FEM, Ax); ++j) {
1027 
1028  ind = patch->get(FEM, Ax, j);
1029 
1030  FH<<"fam"<<ind->getHome()+1
1031  <<" "<<ind->getID(); // don't add +1 to keep IDs consistent across files
1032 
1033  if(Ax == OFFSx)
1034  FH<<" "<<ind->getFatherID()<<" "<<ind->getMotherID(); //parents may be in file for offspring, although not guaranteed
1035  else
1036  FH<<" 0 0"; //parents not in file for adults
1037 
1038  FH<<" 2 -9";
1039 
1040  seq = (unsigned char**)ind->getTrait(_FHLinkedTraitIndex)->get_sequence();
1041 
1042  for(unsigned int k = 0; k < nb_locus; ++k) {
1043  FH<<" "<< BASE[ seq[FEM][k] ]<<" "<< BASE[ seq[MAL][k] ]; // the maternally inherited allele comes first
1044  }
1045 
1046  FH <<std::endl;
1047 
1048  }
1049 
1050  for (unsigned int j = 0; j < patch->size(MAL, Ax); ++j) {
1051 
1052  ind = patch->get(MAL, Ax, j);
1053 
1054  FH<<"fam"<<ind->getHome()+1
1055  <<" "<<ind->getID();
1056 
1057  if(Ax == OFFSx)
1058  FH<<" "<<ind->getFatherID()<<" "<<ind->getMotherID(); //parents may be in file for offspring, although not guaranteed
1059  else
1060  FH<<" 0 0"; //parents not in file for adults
1061 
1062  FH<<" 1 -9";
1063 
1064  seq = (unsigned char**)ind->getTrait(_FHLinkedTraitIndex)->get_sequence();
1065 
1066  for(unsigned int k = 0; k < nb_locus; ++k) {
1067  FH<<" "<< BASE[ seq[FEM][k] ]<<" "<< BASE[ seq[MAL][k] ]; // the maternally inherited allele comes first
1068  }
1069 
1070  FH<<std::endl;
1071 
1072  }
1073 }
unsigned long getID()
Definition: individual.h:122
unsigned short getHome()
Definition: individual.h:128
unsigned long getMotherID()
Definition: individual.h:125
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 get_ploidy()
Definition: ttneutralgenes.h:158
virtual void ** get_sequence() const =0
sequence accessor.
@ MAL
Definition: types.h:37

References TraitFileHandler< TProtoNeutralGenes >::_FHLinkedTrait, TraitFileHandler< TProtoNeutralGenes >::_FHLinkedTraitIndex, FEM, Patch::get(), TProtoNeutralGenes::get_locus_num(), TProtoNeutralGenes::get_ploidy(), TTrait::get_sequence(), Individual::getFatherID(), Individual::getHome(), Individual::getID(), Individual::getMotherID(), Individual::getTrait(), MAL, OFFSx, and Patch::size().

Referenced by write_PLINK().

◆ set_write_fct()

void TTNeutralGenesFH::set_write_fct ( void(TTNeutralGenesFH::*)()  fct_ptr)
inline
238 {write_fct = fct_ptr;}

References write_fct.

Referenced by TProtoNeutralGenes::loadFileServices().

◆ setOutputOption()

void TTNeutralGenesFH::setOutputOption ( string  opt)
797 {
798 
799  if(opt != "1") //it means that the param received an argument value
800  _output_option = opt;
801  else
802  _output_option = "locus"; //default
803 }
string _output_option
Definition: ttneutralgenes.h:210

References _output_option.

Referenced by TProtoNeutralGenes::loadFileServices().

◆ write_Fst_i()

void TTNeutralGenesFH::write_Fst_i ( )
1427 {
1428  // make sure we are using the whole pop, not a sub-sampled one:
1429  Metapop *pop = get_service()->get_pop_ptr();
1430 
1431  if(pop->size(ADULTS) == 0) {
1432  warning("No adults in pop, not writing the Fst distribution to file.\n");
1433  return;
1434  }
1435 
1436  unsigned int nb_locus = _FHLinkedTrait->get_locus_num();
1437  unsigned int patchNbr = pop->getPatchNbr();
1438  double **fst_i;
1439  bool added_stater = false;
1440 
1442 
1443  if(stater == NULL) {
1444  stater = new TTNeutralGenesSH(_FHLinkedTrait);
1446  added_stater = true;
1447  }
1448 
1449  fst_i = new double* [patchNbr];
1450  for(unsigned int i = 0; i < patchNbr; i++)
1451  fst_i[i] = new double [nb_locus];
1452 
1453  stater->setFst_li(patchNbr, nb_locus, fst_i);
1454 
1455  std::string filename = get_path() + this->get_service()->getGenerationReplicateFileName() + get_extension();
1456 
1457 #ifdef _DEBUG_
1458  message("TTNeutralGenesFH::FHwrite (%s)\n",filename.c_str());
1459 #endif
1460 
1461  ofstream FILE (filename.c_str(), ios::out);
1462 
1463  if(!FILE) fatal("could not open Fst_i output file!!\n");
1464 
1465  for(unsigned int i = 0; i < patchNbr; ++i)
1466  FILE<<"patch"<<i+1<<" ";
1467 
1468  FILE<<endl;
1469 
1470  for(unsigned int j = 0; j < nb_locus; ++j) {
1471 
1472  for(unsigned int i = 0; i < patchNbr; i++)
1473  FILE<<fst_i[i][j]<<" ";
1474 
1475  FILE<<endl;
1476  }
1477 
1478  FILE.close();
1479 
1480  if(added_stater) delete stater;
1481 
1482  for(unsigned int i = 0; i < patchNbr; i++)
1483  delete [] fst_i[i];
1484 
1485  delete [] fst_i;
1486 
1487 }
std::string & get_path()
Definition: filehandler.h:139
std::string & get_extension()
Definition: filehandler.h:143
string getGenerationReplicateFileName()
Accessor to the current file name with generation and replicate counters added.
Definition: fileservices.cc:438
Top class of the metapopulation structure, contains the patches.
Definition: metapop.h:80
TTNeutralGenesSH * get_stater()
Definition: ttneutralgenes.h:166
The stat handler for neutral markers.
Definition: ttneutralgenes.h:267
void allocateTables(unsigned int loci, unsigned int all)
Definition: stats_fstat.cc:43
void setFst_li(unsigned int N, unsigned int L, double **array)
Computes the per-locus per-patch Fst values using Weir&Hill 2002 approach.
Definition: stats_fstat.cc:1057
void warning(const char *str,...)
Definition: output.cc:58
#define ADULTS
Adults age class flag (breeders).
Definition: types.h:54

References TraitFileHandler< TProtoNeutralGenes >::_FHLinkedTrait, ADULTS, TTNeutralGenesSH::allocateTables(), fatal(), TProtoNeutralGenes::get_allele_num(), FileHandler::get_extension(), TProtoNeutralGenes::get_locus_num(), FileHandler::get_path(), FileServices::get_pop_ptr(), FileHandler::get_service(), TProtoNeutralGenes::get_stater(), FileServices::getGenerationReplicateFileName(), Metapop::getPatchNbr(), message(), TTNeutralGenesSH::setFst_li(), Metapop::size(), and warning().

Referenced by TProtoNeutralGenes::loadFileServices().

◆ write_FSTAT()

void TTNeutralGenesFH::write_FSTAT ( )

The file format is FSTAT-like, with extra info about the individual added (age, sex, pedigree, origin). The file extension is ".dat".

1078 {
1081  unsigned int position;
1082  unsigned int ploidy = _FHLinkedTrait->get_ploidy();
1083  unsigned int nb_all = _FHLinkedTrait->get_allele_num();
1084  unsigned int nb_locus = _FHLinkedTrait->get_locus_num();
1085  unsigned int patchNbr = _pop->getPatchNbr();
1086  unsigned char** seq;
1087  Patch* current_patch;
1088  Individual *ind;
1089 
1090  position = nb_all > 99 ? 3 : nb_all > 9 ? 2 : 1; //assumes nb_all not sup. to 999
1091 
1092  std::string filename = get_path() + this->get_service()->getGenerationReplicateFileName()
1093  + get_extension();
1094 
1095 #ifdef _DEBUG_
1096  message("TTNeutralGenesFH::FHwrite (%s)\n",filename.c_str());
1097 #endif
1098 
1099  ofstream FILE (filename.c_str(), ios::out);
1100 
1101  if(!FILE) fatal("could not open FSTAT output file!!\n");
1102 
1103  FILE<<patchNbr<<" "<< nb_locus + 4 <<" "<<nb_all<<" "<<position<<"\n";
1104 
1105  for (unsigned int i = 0; i < nb_locus; ++i)
1106  FILE<<"loc"<<i+1<<"\n";
1107 
1108  //add names for the three last fields:
1109  FILE<<"age\n"<<"sex\n"<<"ped\n"<<"origin\n";
1110 
1111  for (unsigned int i = 0; i < patchNbr; ++i) {
1112 
1113  current_patch = _pop->getPatch(i);
1114 
1115  write_patch_FSTAT(current_patch, FEM, OFFSx, FILE, position);
1116  write_patch_FSTAT(current_patch, MAL, OFFSx, FILE, position);
1117  write_patch_FSTAT(current_patch, FEM, ADLTx, FILE, position);
1118  write_patch_FSTAT(current_patch, MAL, ADLTx, FILE, position);
1119 
1120  }
1121 
1122  FILE.close();
1123 
1124 }
Second class in the metapopulation design structure, between the Metapop and Individual classes.
Definition: metapop.h:432
void write_patch_FSTAT(Patch *patch, sex_t SEX, age_idx AGE, ofstream &FH, unsigned int digits)
Definition: ttneutralgenes.cc:1128

References TraitFileHandler< TProtoNeutralGenes >::_FHLinkedTrait, FileHandler::_pop, ADLTx, fatal(), FEM, TProtoNeutralGenes::get_allele_num(), FileHandler::get_extension(), TProtoNeutralGenes::get_locus_num(), FileHandler::get_path(), TProtoNeutralGenes::get_ploidy(), FileHandler::get_service(), FileServices::getGenerationReplicateFileName(), Metapop::getPatch(), Metapop::getPatchNbr(), MAL, message(), OFFSx, and write_patch_FSTAT().

Referenced by TProtoNeutralGenes::loadFileServices().

◆ write_GENEPOP()

void TTNeutralGenesFH::write_GENEPOP ( )

The file format is GENEPOP-like, with extra info about the individual added (age, sex, pedigree, origin). The file extension is ".txt".

1159 {
1162  unsigned int position;
1163  unsigned int ploidy = _FHLinkedTrait->get_ploidy();
1164  unsigned int nb_all = _FHLinkedTrait->get_allele_num();
1165  unsigned int nb_locus = _FHLinkedTrait->get_locus_num();
1166  unsigned int patchNbr = _pop->getPatchNbr();
1167  unsigned char** seq;
1168  Patch* current_patch;
1169  Individual *ind;
1170 
1171  position = nb_all > 99 ? 3 : 2; //assumes nb_all not sup. to 999
1172 
1173  if(ploidy == 1) position = 1;
1174 
1175  std::string filename = get_path() + this->get_service()->getGenerationReplicateFileName()
1176  + get_extension();
1177 
1178 #ifdef _DEBUG_
1179  message("TTNeutralGenesFH::FHwrite (%s)\n",filename.c_str());
1180 #endif
1181 
1182  ofstream FILE (filename.c_str(), ios::out);
1183 
1184  if(!FILE) fatal("could not open FSTAT output file!!\n");
1185 
1186  FILE<<"Title line: "<<patchNbr<<" patches, "<<nb_locus<<" loci with "<<nb_all<<" alleles\n";
1187 
1188  for (unsigned int i = 0; i < nb_locus; ++i)
1189  FILE<<"loc"<<i+1<<", ";
1190 
1191  //add names for the three last fields:
1192  FILE<<"age, sex, ped, origin\n";
1193 
1194  for (unsigned int i = 0; i < patchNbr; ++i) {
1195 
1196  current_patch = _pop->getPatch(i);
1197 
1198  FILE<<"POP\n";
1199 
1200  write_patch_GENEPOP(current_patch, FEM, OFFSx, FILE, position);
1201  write_patch_GENEPOP(current_patch, MAL, OFFSx, FILE, position);
1202  write_patch_GENEPOP(current_patch, FEM, ADLTx, FILE, position);
1203  write_patch_GENEPOP(current_patch, MAL, ADLTx, FILE, position);
1204 
1205 
1206  }
1207 
1208  FILE.close();
1209 
1210 }
void write_patch_GENEPOP(Patch *patch, sex_t SEX, age_idx AGE, ofstream &FH, unsigned int digits)
Definition: ttneutralgenes.cc:1214

References TraitFileHandler< TProtoNeutralGenes >::_FHLinkedTrait, FileHandler::_pop, ADLTx, fatal(), FEM, TProtoNeutralGenes::get_allele_num(), FileHandler::get_extension(), TProtoNeutralGenes::get_locus_num(), FileHandler::get_path(), TProtoNeutralGenes::get_ploidy(), FileHandler::get_service(), FileServices::getGenerationReplicateFileName(), Metapop::getPatch(), Metapop::getPatchNbr(), MAL, message(), OFFSx, and write_patch_GENEPOP().

Referenced by TProtoNeutralGenes::loadFileServices().

◆ write_patch_FSTAT()

void TTNeutralGenesFH::write_patch_FSTAT ( Patch patch,
sex_t  SEX,
age_idx  AGE,
ofstream &  FH,
unsigned int  digits 
)
1129 {
1130  unsigned char** seq;
1131  Individual *ind;
1132 
1133  for (unsigned int j = 0; j < patch->size(SEX, AGE); ++j) {
1134 
1135  FH<<patch->getID() + 1<<" ";
1136  ind = patch->get(SEX, AGE, j);
1137  seq = (unsigned char**)ind->getTrait(_FHLinkedTraitIndex)->get_sequence();
1138 
1139  for(unsigned int k = 0; k < _FHLinkedTrait->get_locus_num(); ++k) {
1140  for (unsigned int l = 0; l < _FHLinkedTrait->get_ploidy(); ++l) {
1141  FH.fill('0');
1142  FH.width(digits);
1143  FH<<(unsigned int)(seq[l][k]+1);
1144  }
1145  FH<<" ";
1146  }
1147 
1148  FH << AGE <<" "
1149  <<SEX<<" "
1150  <<ind->getPedigreeClass()<<" "
1151  <<ind->getHome()+1<<std::endl;
1152  }
1153 
1154 }
unsigned int getPedigreeClass()
Returns the pedigree class of the individual, as set during offspring creation.
Definition: individual.h:179
unsigned int getID()
Definition: metapop.h:481

References TraitFileHandler< TProtoNeutralGenes >::_FHLinkedTrait, TraitFileHandler< TProtoNeutralGenes >::_FHLinkedTraitIndex, Patch::get(), TProtoNeutralGenes::get_locus_num(), TProtoNeutralGenes::get_ploidy(), TTrait::get_sequence(), Individual::getHome(), Patch::getID(), Individual::getPedigreeClass(), Individual::getTrait(), and Patch::size().

Referenced by write_FSTAT().

◆ write_patch_GENEPOP()

void TTNeutralGenesFH::write_patch_GENEPOP ( Patch patch,
sex_t  SEX,
age_idx  AGE,
ofstream &  FH,
unsigned int  digits 
)
1215 {
1216  unsigned char** seq;
1217  Individual *ind;
1218 
1219  for (unsigned int j = 0; j < patch->size(SEX, AGE); ++j) {
1220 
1221  FH<<patch->getID() + 1<<", ";
1222  ind = patch->get(SEX, AGE, j);
1223  seq = (unsigned char**)ind->getTrait(_FHLinkedTraitIndex)->get_sequence();
1224 
1225  for(unsigned int k = 0; k < _FHLinkedTrait->get_locus_num(); ++k) {
1226  for (unsigned int l = 0; l < _FHLinkedTrait->get_ploidy(); ++l) {
1227  FH.fill('0');
1228  FH.width(digits);
1229  FH<<(unsigned int)(seq[l][k]+1);
1230  }
1231  FH<<" ";
1232  }
1233 
1234  FH << AGE <<" "
1235  <<SEX<<" "
1236  <<ind->getPedigreeClass()<<" "
1237  <<ind->getHome()+1<<std::endl;
1238  }
1239 
1240 }

References TraitFileHandler< TProtoNeutralGenes >::_FHLinkedTrait, TraitFileHandler< TProtoNeutralGenes >::_FHLinkedTraitIndex, Patch::get(), TProtoNeutralGenes::get_locus_num(), TProtoNeutralGenes::get_ploidy(), TTrait::get_sequence(), Individual::getHome(), Patch::getID(), Individual::getPedigreeClass(), Individual::getTrait(), and Patch::size().

Referenced by write_GENEPOP().

◆ write_patch_TAB()

void TTNeutralGenesFH::write_patch_TAB ( Patch patch,
sex_t  SEX,
age_idx  AGE,
ofstream &  FH 
)
881 {
882  unsigned char** seq;
883  Individual *ind;
884 
885  for (unsigned int j = 0; j < patch->size(SEX, AGE); ++j) {
886 
887  FH<<patch->getID() + 1<<" ";
888 
889  ind = patch->get(SEX, AGE, j);
890  seq = (unsigned char**)ind->getTrait(_FHLinkedTraitIndex)->get_sequence();
891 
892  if(_output_option == "snp") {
893 
894  for(unsigned int k = 0; k < _FHLinkedTrait->get_locus_num(); ++k)
895  FH<<(unsigned int)(seq[0][k])+(unsigned int)(seq[1][k])<<" ";
896 
897  } else {
898 
899  for(unsigned int k = 0; k < _FHLinkedTrait->get_locus_num(); ++k) {
900  for (unsigned int l = 0; l < _FHLinkedTrait->get_ploidy(); ++l) {
901  FH<<(unsigned int)(seq[l][k]+1)<<" ";
902  }
903  }
904  }
905  FH << AGE <<" "
906  <<SEX<<" "
907  <<ind->getHome()+1<<" "
908  <<ind->getPedigreeClass()<<" "
909  << (ind->getFather() && ind->getMother() ?
910  (ind->getFather()->getHome()!=patch->getID()) + (ind->getMother()->getHome()!= patch->getID()) : 0)
911  <<" "
912  <<ind->getFatherID()<<" "
913  <<ind->getMotherID()<<" "
914  <<ind->getID()<<std::endl;
915 
916  }
917 
918 }
Individual * getMother()
Definition: individual.h:127
Individual * getFather()
Definition: individual.h:126

References TraitFileHandler< TProtoNeutralGenes >::_FHLinkedTrait, TraitFileHandler< TProtoNeutralGenes >::_FHLinkedTraitIndex, _output_option, Patch::get(), TProtoNeutralGenes::get_locus_num(), TProtoNeutralGenes::get_ploidy(), TTrait::get_sequence(), Individual::getFather(), Individual::getFatherID(), Individual::getHome(), Individual::getID(), Patch::getID(), Individual::getMother(), Individual::getMotherID(), Individual::getPedigreeClass(), Individual::getTrait(), and Patch::size().

Referenced by write_TAB().

◆ write_PLINK()

void TTNeutralGenesFH::write_PLINK ( )
923 {
924 
925  if(_FHLinkedTrait->get_allele_num() > 2)
926  fatal("PLINK file output for the neutral loci is only possible for di-allelic loci (for now)\n");
927 
928 
929  unsigned int ploidy = _FHLinkedTrait->get_ploidy();
930  unsigned int nb_locus = _FHLinkedTrait->get_locus_num();
931  unsigned int patchNbr = _pop->getPatchNbr();
932  Patch* current_patch;
933  age_t pop_age = _pop->getCurrentAge(); //flag telling which age class should contain individuals
934 
935  std::string filename = get_path() + this->get_service()->getGenerationReplicateFileName() + "-ntrl.ped";
936 
937 #ifdef _DEBUG_
938  message("TTNeutralGenesFH::write_PLINK (%s)\n",filename.c_str());
939 #endif
940 
941  // the PED file -------------------------------------------------------------------------
942  ofstream PED (filename.c_str(), ios::out);
943 
944  if(!PED) fatal("could not open plink .ped output file!!\n");
945 
946  for (unsigned int i = 0; i < patchNbr; ++i) {
947 
948  current_patch = _pop->getPatch(i);
949 
950  if( pop_age & OFFSPRG )
951  print_PLINK_PED(PED, OFFSx, current_patch);
952 
953  if( pop_age & ADULTS )
954  print_PLINK_PED(PED, ADLTx, current_patch);
955 
956  }
957 
958 
959  PED.close();
960 
961  // the MAP file -------------------------------------------------------------------------
962  filename = get_path() + this->get_service()->getGenerationReplicateFileName() + "-ntrl.map";
963 
964  ofstream MAP (filename.c_str(), ios::out);
965 
966  if(!MAP) fatal("could not open plink .map output file!!\n");
967 
968  double **map;
969  map = new double* [nb_locus];
970  for(unsigned int k = 0; k < nb_locus; ++k) map[k] = new double[2];
971 
972  bool found = _FHLinkedTrait->_map.getGeneticMap(_FHLinkedTrait->get_type(), map, nb_locus);
973 
974  if( found ) {
975 
976  // MAP FORMAT (PLINK1.9): chrmsm ID; Loc ID; position (cM); bp ID
977  for(unsigned int k = 0; k < nb_locus; ++k) {
978  MAP<<map[k][0]+1<<" "<<"loc"<<k+1<<" "<<map[k][1]<<" "<<k+1<<endl;
979  }
980  } else { // trait didn't register a genetic map, loci are unlinked (free recombination)
981 
982  warning("PLINK .map file: we assume ntrl loci are unlinked, separated by 50M and on a single chromosome\n");
983 
984  // we're gonna set all loci on a single chrmsme, but 50M apart
985  for(unsigned int k = 0; k < nb_locus; ++k) {
986  MAP<<"1 "<<"loc"<<k+1<<" "<< k*5000.0 + 1.0<<" "<<k+1<<endl;
987  }
988  }
989 
990  MAP.close();
991 
992  for(unsigned int k = 0; k < nb_locus; ++k) delete [] map[k];
993  delete [] map;
994 }
bool getGeneticMap(trait_t trait, double **table, unsigned int table_length)
Definition: ttrait_with_map.cc:889
age_t getCurrentAge()
Definition: metapop.h:299
virtual trait_t get_type() const
Definition: ttneutralgenes.h:180
void print_PLINK_PED(ofstream &FH, age_idx Ax, Patch *patch)
Definition: ttneutralgenes.cc:999
static GeneticMap _map
Definition: ttrait_with_map.h:209
unsigned int age_t
Age class flags.
Definition: types.h:46
#define OFFSPRG
Offspring age class flag.
Definition: types.h:50

References TraitFileHandler< TProtoNeutralGenes >::_FHLinkedTrait, TTProtoWithMap::_map, FileHandler::_pop, ADLTx, ADULTS, fatal(), TProtoNeutralGenes::get_allele_num(), TProtoNeutralGenes::get_locus_num(), FileHandler::get_path(), TProtoNeutralGenes::get_ploidy(), FileHandler::get_service(), TProtoNeutralGenes::get_type(), Metapop::getCurrentAge(), FileServices::getGenerationReplicateFileName(), GeneticMap::getGeneticMap(), Metapop::getPatch(), Metapop::getPatchNbr(), message(), OFFSPRG, OFFSx, print_PLINK_PED(), and warning().

Referenced by TProtoNeutralGenes::loadFileServices().

◆ write_PLINK_BED()

void TTNeutralGenesFH::write_PLINK_BED ( ofstream &  BED)

◆ write_TAB()

void TTNeutralGenesFH::write_TAB ( )

The file extension is ".txt".

827 {
829  unsigned int ploidy = _FHLinkedTrait->get_ploidy();
830  unsigned int nb_locus = _FHLinkedTrait->get_locus_num();
831  unsigned int patchNbr = _pop->getPatchNbr();
832  unsigned char** seq;
833  Patch* current_patch;
834  Individual *ind;
835 
836  std::string filename = get_path() + this->get_service()->getGenerationReplicateFileName()
837  + get_extension();
838 
839 #ifdef _DEBUG_
840  message("TTNeutralGenesFH::write_TAB (%s)\n",filename.c_str());
841 #endif
842 
843  ofstream FILE (filename.c_str(), ios::out);
844 
845  if(!FILE) fatal("could not open TAB output file!!\n");
846 
847  FILE<<"pop";
848 
849  for (unsigned int i = 0; i < nb_locus; ++i) {
850 
851  if( _output_option == "snp") {
852 
853  FILE<<" l"<<i+1;
854 
855  } else {
856  for (unsigned int j = 0; j < ploidy; ++j)
857  FILE<<" l"<<i+1<<1<<j+1;
858  }
859  }
860  //add names for the three last fields:
861  FILE<<" age sex home ped isMigrant father mother ID\n";
862 
863  for (unsigned int i = 0; i < patchNbr; ++i) {
864 
865  current_patch = _pop->getPatch(i);
866 
867  write_patch_TAB(current_patch, FEM, OFFSx, FILE);
868  write_patch_TAB(current_patch, MAL, OFFSx, FILE);
869  write_patch_TAB(current_patch, FEM, ADLTx, FILE);
870  write_patch_TAB(current_patch, MAL, ADLTx, FILE);
871 
872  }
873 
874  FILE.close();
875 
876 }
void write_patch_TAB(Patch *patch, sex_t SEX, age_idx AGE, ofstream &FH)
Definition: ttneutralgenes.cc:880

References TraitFileHandler< TProtoNeutralGenes >::_FHLinkedTrait, _output_option, FileHandler::_pop, ADLTx, fatal(), FEM, FileHandler::get_extension(), TProtoNeutralGenes::get_locus_num(), FileHandler::get_path(), TProtoNeutralGenes::get_ploidy(), FileHandler::get_service(), FileServices::getGenerationReplicateFileName(), Metapop::getPatch(), Metapop::getPatchNbr(), MAL, message(), OFFSx, and write_patch_TAB().

Referenced by TProtoNeutralGenes::loadFileServices().

◆ write_varcompWC()

void TTNeutralGenesFH::write_varcompWC ( )
1493 {
1494  // make sure we are using the whole pop, not a sub-sampled one:
1495  Metapop *pop = get_service()->get_pop_ptr();
1496 
1497  unsigned int nb_allele = _FHLinkedTrait->get_allele_num();
1498  unsigned int nb_locus = _FHLinkedTrait->get_locus_num();
1499  unsigned int patchNbr = pop->getPatchNbr();
1500  bool added_stater = false;
1501 
1502  age_t AGE = pop->getCurrentAge();
1503 
1504  if(AGE == ALL) {
1505 // warning("saving only offspring neutral allele stats\n");
1506  AGE = OFFSPRG;
1507  }
1508 
1509  age_idx age = (AGE == OFFSPRG ? OFFSx : ADLTx);
1510 
1511 // cout << "----------TTNeutralGenesFH::write_varcompWC----------\n";
1512 
1513 // string pad = " ";
1514 
1516 
1517  if(stater == NULL) {
1518  stater = new TTNeutralGenesSH(_FHLinkedTrait);
1520  added_stater = true;
1521  }
1522 
1523  stater->setAlleleTables(AGE);
1524  stater->setHeteroTable(AGE);
1525 
1526  TMatrix *globalFreq = stater->getGlobalFreqs();
1527  DataTable< double > *freqTable = stater->getAlleleFreqTable();
1528  DataTable< double > *heteroTable = stater->getHeteroTable();
1529  DataTable< unsigned int > *alleleCountTable = stater->getAlleleCountTable();
1530 
1531  std::string filename = get_path() + this->get_service()->getGenerationReplicateFileName() + get_extension();
1532 
1533 #ifdef _DEBUG_
1534  message("TTNeutralGenesFH::write_varcompWC (%s)\n",filename.c_str());
1535 #endif
1536 
1537  //init
1538  double* pop_sizes = new double [patchNbr];
1539  double tot_size;
1540  double sum_weights = 0;
1541  double nc;
1542  unsigned int extantPs = 0;
1543 
1544  tot_size = pop->size(AGE);
1545 
1546  for(unsigned int i = 0; i < patchNbr; i++) {
1547  pop_sizes[i] = pop->size(AGE, i);
1548  if(pop_sizes[i]) {
1549  extantPs++;
1550  sum_weights += (pop_sizes[i] * pop_sizes[i] / tot_size);
1551  }
1552  }
1553 
1554  nc = (tot_size - sum_weights)/(extantPs-1);
1555 
1556  // unsigned int np = extantPs;
1557  unsigned int npl = extantPs; //all loci typed in all patches
1558 
1559  //p = _alleleFreqTable
1560  //pb = _globalAlleleFreq
1561 
1562  //counting num alleles per locus
1563  unsigned int *alploc = new unsigned int [nb_locus];
1564 
1565  unsigned int **alploc_table = new unsigned int* [nb_locus];
1566 
1567  for(unsigned int i = 0; i < nb_locus; ++i)
1568  alploc_table[i] = new unsigned int[nb_allele];
1569 
1570  unsigned int tot_num_allele = 0;
1571 
1572  for(unsigned int l = 0; l < nb_locus; ++l){
1573 
1574  alploc[l] = 0;
1575 
1576  for(unsigned int cnt, a = 0; a < nb_allele; ++a) {
1577 
1578  cnt=0;
1579 
1580  for(unsigned int i = 0; i < patchNbr; i++) {
1581 
1582  cnt += alleleCountTable->get(i,l,a);
1583 
1584  }
1585  alploc_table[l][a] = (cnt != 0);
1586  alploc[l] += (cnt != 0);
1587  }
1588 
1589  tot_num_allele += alploc[l];
1590  }
1591 
1592  //correspondance with hierfstat implementation:
1593  //n, and nal are given by pop_sizes, same num ind typed at all loci in each patch
1594  //nc is the same for each locus
1595  //nt is given by tot_size, same tot num of ind typed at all loci
1596 
1597  //SSG = het/2 for each allele
1598  double *SSG = new double[tot_num_allele];
1599  double *SSP = new double[tot_num_allele];
1600  double *SSi = new double[tot_num_allele];
1601  double *loc_id = new double[tot_num_allele];
1602  double *al_id = new double [tot_num_allele];
1603 
1604  unsigned int all_cntr = 0;
1605 
1606  double het, freq, var;
1607 
1608  //computing sum of squares
1609  for(unsigned int l = 0; l < nb_locus; ++l) {
1610 
1611  for(unsigned int a = 0; a < nb_allele & all_cntr < tot_num_allele; ++a) {
1612 
1613  if(alploc_table[l][a] == 0) continue; //do not consider alleles not present in the whole pop
1614 
1615  //store locus and all identifiers for output
1616  loc_id[all_cntr] = l+1;
1617  al_id[all_cntr] = a+1;
1618 
1619  SSG[all_cntr] = 0;
1620  SSi[all_cntr] = 0;
1621  SSP[all_cntr] = 0;
1622 
1623  for(unsigned int p = 0; p < patchNbr; ++p){
1624 
1625  if(!pop->size(AGE, p)) continue; //skip empty patches
1626 
1627  het = heteroTable->get(p, l, a);
1628 
1629  freq = freqTable->get(p, l, a);
1630 
1631  var = freq - globalFreq->get(l, a); //(p_liu - pbar_u)^2
1632 
1633  var *= var;
1634 
1635  SSG[all_cntr] += het;
1636 
1637  SSi[all_cntr] += 2*pop_sizes[p]*freq*(1-freq) - het/2;
1638 
1639  SSP[all_cntr] += 2*pop_sizes[p]*var;
1640  }
1641 
1642  all_cntr++;
1643  }
1644 
1645  }
1646 
1647  assert(all_cntr == tot_num_allele);
1648 
1649  // open the file
1650 
1651  ofstream FILE (filename.c_str(), ios::out);
1652 
1653  if(!FILE) fatal("could not open neutral vcomp output file!!\n");
1654 
1655  // print column names
1656 
1657 
1658  if(_output_option == "allele") {
1659 
1660  FILE<<"locus allele pbar het siga sigb sigw Fst Fis";
1661 
1662  for (unsigned int p = 1; p <= patchNbr; ++p)
1663  FILE<<" het.p"<<p;
1664 
1665  for (unsigned int p = 1; p <= patchNbr; ++p)
1666  FILE<<" freq.p"<<p;
1667 
1668  FILE<<endl;
1669 
1670  } else {
1671 
1672  FILE<<"locus maj.al pbar.maj.al het siga sigb sigw Fst Fis";
1673 
1674  for (unsigned int p = 1; p <= patchNbr; ++p)
1675  FILE<<" het.p"<<p;
1676 
1677  //allele frequencies in each patch
1678  for (unsigned int p = 1; p <= patchNbr; ++p)
1679  // for(unsigned int u = 0; u < nb_allele-1; ++u) //skip last allele, can be deduced...
1680  FILE<<" freq.maj."<<"p"<< p;
1681 
1682  FILE<<endl;
1683  }
1684 
1685  //-----------------------------------------------------------------------------------------
1686  // allele specific stats:
1687 
1688  double *MSG = new double[tot_num_allele];
1689  double *MSP = new double[tot_num_allele];
1690  double *MSI = new double[tot_num_allele];
1691  // double *sigw = new double[tot_num_allele];
1692  double *siga = new double[tot_num_allele];
1693  double *sigb = new double[tot_num_allele];
1694 
1695  for(unsigned int i = 0; i < tot_num_allele; ++i){
1696 
1697  MSG[i] = SSG[i] / (2 * tot_size);
1698  // sigw[i] = MSG[i]; //wasted!
1699 
1700  MSP[i] = SSP[i] / (npl-1);
1701 
1702  MSI[i] = SSi[i]/ (tot_size - npl);
1703 
1704  sigb[i] = 0.5*(MSI[i] - MSG[i]);
1705 
1706  siga[i] = (MSP[i] - MSI[i])/(2*nc);
1707 
1708  if(_output_option == "allele") {
1709 
1710  FILE<< loc_id[i] << " " << al_id[i] << " "
1711  //global allele frequency
1712  << globalFreq->get(loc_id[i] - 1, al_id[i] - 1) << " "
1713  //average heterozygosity:
1714  << SSG[i]/tot_size << " "
1715  //variance components:
1716  << siga[i] << " " << sigb[i] << " " << MSG[i] << " "
1717  //Fst
1718  << siga[i]/(siga[i]+sigb[i]+MSG[i]) << " "
1719  //Fis
1720  << sigb[i]/(sigb[i]+MSG[i]) << " ";
1721  //per patch heterozygosity:
1722  for (unsigned int p = 0; p < patchNbr; ++p) {
1723  FILE << heteroTable->get(p, loc_id[i]-1, al_id[i]-1)/pop_sizes[p] << " ";
1724  }
1725  //per patch allele frequency:
1726  for (unsigned int p = 0; p < patchNbr; ++p) {
1727  FILE << freqTable->get(p, loc_id[i]-1, al_id[i]-1) << " ";
1728  }
1729 
1730  FILE<< endl;
1731  }
1732  }
1733 
1734  //-----------------------------------------------------------------------------------------
1735  // locus specific stats:
1736 
1737  if(_output_option == "locus") {
1738 
1739  double lsiga, lsigb, lsigw, max_all_frq;
1740  unsigned int maj_al;
1741 
1742  all_cntr = 0;
1743 
1744  deque <double> loc_het = stater->setHo2(age);
1745 
1746  for(unsigned int i = 0; i < nb_locus; ++i) {
1747 
1748  lsiga = 0;
1749  lsigb = 0;
1750  lsigw = 0;
1751 
1752  max_all_frq = 0;
1753 
1754  for(unsigned int l = 0; l < alploc[i]; ++l) {
1755 
1756  lsiga += siga[all_cntr];
1757  lsigb += sigb[all_cntr];
1758  lsigw += MSG[all_cntr];
1759 
1760  if(max_all_frq < globalFreq->get(i, al_id[all_cntr]-1 ) ) {
1761  max_all_frq = globalFreq->get(i, al_id[all_cntr]-1 );
1762  maj_al = al_id[all_cntr];
1763  }
1764 
1765  all_cntr++;
1766 
1767  }
1768 
1769  FILE << i+1 <<" "<< maj_al <<" "<< max_all_frq <<" ";
1770  FILE<< loc_het[i] <<" "<< lsiga << " " << lsigb <<" ";
1771  FILE<< lsigw << " "<< lsiga /(lsiga + lsigb + lsigw) <<" ";
1772  FILE<< lsigb /(lsigb + lsigw)<<fflush;
1773  // patch heterozygosities:
1774 
1775  for(unsigned int p = 0; p < patchNbr; ++p) {
1776  het = 0;
1777  for(unsigned int a = 0; a < nb_allele; ++a){
1778  het += heteroTable->get(p, i, a);
1779  }
1780  FILE << " " << het/(2.0*pop_sizes[p]);
1781  }
1782  // patch allele freq?
1783  for(unsigned int p = 0; p < patchNbr; ++p) {
1784  FILE<< " " << freqTable->get(p, i, maj_al-1);
1785  }
1786  FILE << endl;
1787  }//END for locus
1788 
1789  } //END per locus output
1790 
1791 
1792  FILE.close();
1793 
1794  delete[]pop_sizes;
1795  delete[]alploc;
1796  for(unsigned int i = 0; i < nb_locus; ++i)
1797  delete[]alploc_table[i];
1798  delete[]alploc_table;
1799  delete[]loc_id;
1800  delete[]al_id;
1801  delete[]SSG;
1802  delete[]SSi;
1803  delete[]SSP;
1804  delete[]MSG;
1805  delete[]MSI;
1806  delete[]MSP;
1807  // delete[]sigw;
1808  delete[]siga;
1809  delete[]sigb;
1810 
1811  if(added_stater) delete stater;
1812 }
T get(unsigned int group, unsigned int Class, unsigned int elmnt)
Returns value stored of the element 'elmnt' of the class 'Class' in the group 'group'.
Definition: datatable.h:228
A class to handle matrix in params, coerces matrix into a vector of same total size.
Definition: tmatrix.h:50
double get(unsigned int i, unsigned int j) const
Accessor to element at row i and column j.
Definition: tmatrix.h:193
deque< double > setHo2(age_idx age_pos)
Definition: stats_fstat.cc:545
void setAlleleTables(age_t AGE)
Definition: stats_fstat.cc:74
DataTable< double > * getAlleleFreqTable()
Accessor to the table of allele frequencies, per patch.
Definition: ttneutralgenes.h:360
DataTable< unsigned int > * getAlleleCountTable()
Definition: ttneutralgenes.h:362
void setHeteroTable(age_t AGE)
Definition: stats_fstat.cc:166
DataTable< double > * getHeteroTable()
Definition: ttneutralgenes.h:364
TMatrix * getGlobalFreqs()
Accessor to the table of allele frequencies in the whole population.
Definition: ttneutralgenes.h:367
#define ALL
All ages age class flag.
Definition: types.h:56

References TraitFileHandler< TProtoNeutralGenes >::_FHLinkedTrait, _output_option, ADLTx, ALL, TTNeutralGenesSH::allocateTables(), fatal(), DataTable< T >::get(), TMatrix::get(), TProtoNeutralGenes::get_allele_num(), FileHandler::get_extension(), TProtoNeutralGenes::get_locus_num(), FileHandler::get_path(), FileServices::get_pop_ptr(), FileHandler::get_service(), TProtoNeutralGenes::get_stater(), TTNeutralGenesSH::getAlleleCountTable(), TTNeutralGenesSH::getAlleleFreqTable(), Metapop::getCurrentAge(), FileServices::getGenerationReplicateFileName(), TTNeutralGenesSH::getGlobalFreqs(), TTNeutralGenesSH::getHeteroTable(), Metapop::getPatchNbr(), message(), OFFSPRG, OFFSx, TTNeutralGenesSH::setAlleleTables(), TTNeutralGenesSH::setHeteroTable(), TTNeutralGenesSH::setHo2(), and Metapop::size().

Referenced by TProtoNeutralGenes::loadFileServices().

Member Data Documentation

◆ _output_option

string TTNeutralGenesFH::_output_option
private

◆ write_fct

void(TTNeutralGenesFH::* TTNeutralGenesFH::write_fct) ()
private

Referenced by FHwrite(), and set_write_fct().


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