Nemo  2.4.2
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
269  { }
void(TTNeutralGenesFH::* write_fct)()
Definition: ttneutralgenes.h:263

◆ ~TTNeutralGenesFH()

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

Member Function Documentation

◆ FHread()

void TTNeutralGenesFH::FHread ( string &  filename)
virtual

reads in only FSTAT format

Implements FileHandler.

1209 {
1211  // make sure we are using the whole pop, not a sub-sampled one:
1212 // Metapop *pop = get_service()->get_pop_ptr();
1213 
1214  unsigned int digit, nloci_infile, nall_infile, npatch_infile;
1215  // unsigned int ploidy = _FHLinkedTrait->get_ploidy();
1216  unsigned int nb_all = _FHLinkedTrait->get_allele_num();
1217  unsigned int nb_locus = _FHLinkedTrait->get_locus_num();
1218  unsigned int patchNbr = _pop->getPatchNbr();
1219  unsigned int extended_num_colums = 0;
1220 
1221  bool is_extended = false;
1222 
1223  ifstream FILE(filename.c_str(),ios::in);
1224 
1225  if(!FILE) fatal("could not open FSTAT input file \"%s\"\n", filename.c_str());
1226 
1227  FILE >> npatch_infile >> nloci_infile >> nall_infile >> digit;
1228 
1229  if(npatch_infile != patchNbr) fatal("number of patch in FSTAT file (%i) differs from simulation settings (%i)\n", npatch_infile, patchNbr);
1230 
1231  if (nloci_infile > nb_locus) {
1232 
1233  is_extended = true;
1234 
1235  extended_num_colums = nloci_infile - nb_locus;
1236 
1237  if(extended_num_colums != FSTAT_EXTRA_INFO_LOCI) {
1238  error("FSTAT input genotype file \"%s\" has a non-regular number of extra info loci of %i\n", filename.c_str(), extended_num_colums);
1239  error("Nemo expects %i extra info loci in the extented FSTAT format (age, sex, ped, origin)\n", FSTAT_EXTRA_INFO_LOCI);
1240  error("Extra info loci will be ignored\n");
1241  }
1242 
1243  } else if (nloci_infile == nb_locus){
1244 
1245  is_extended = false;
1246 
1247  } else {
1248 
1249  error("when reading the first line of the input FSTAT file \"%s\" \n", filename.c_str());
1250  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);
1251 
1252  }
1253 
1254 
1255 
1256  if(nall_infile != nb_all) fatal("number of alleles in FSTAT file differs from simulation settings\n");
1257 
1258  digit = (int) pow(10.0,(double)digit);
1259 
1260  string str;
1261 
1262 
1263 #ifdef _DEBUG_
1264  message(">>>> -- FSTAT%s file: \n", (is_extended ? " extended" : ""));
1265  message(">>>> %i patch, %i loci, %i alleles, %i digits \n", npatch_infile, nloci_infile, nall_infile, digit);
1266 #endif
1267 
1268  // read the locus names
1269  for(unsigned int i = 0; i < nloci_infile; ++i) {
1270 
1271  FILE >> str;
1272 
1273  }
1274 
1275  unsigned int patch, genot, all0, all1, age, sex, ped, origin;
1276  age_idx agex;
1277  Individual *ind;
1278  TTrait* trait;
1279  int lnbr = nloci_infile +2; //line counter
1280 
1281  unsigned int *loc_all0 = new unsigned int[nb_locus];
1282  unsigned int *loc_all1 = new unsigned int[nb_locus];
1283 
1284  while(FILE>>patch) {
1285 
1286  if(FILE.bad()) {
1287  error("Reading input genotypes from \"%s\" failed at line %i.\n",filename.c_str(), lnbr);
1288  FILE.clear();
1289  FILE >> str;
1290  fatal("Expecting population number as first element on the line, but received this: %s \n", str.c_str());
1291  }
1292 
1293  //check patch value
1294  if(patch > patchNbr) fatal("found an illegal patch identifier in FSTAT file (%i) at line %i",patch, lnbr);
1295 
1296  for(unsigned int i = 0; i < nb_locus; ++i) {
1297  FILE>>genot;
1298 
1299  all0 = genot/digit;
1300  all1 = genot%digit;
1301 
1302  if( all0 > nb_all ) {
1303 
1304  error("in FSTAT input file at line %i, locus %i : \
1305 first allele value %d is greater than the max value specified (%i)!\n", lnbr, i+1, all0, nb_all);
1306 
1307  fatal("Please check the input file.\n");
1308 
1309  } else if ( all0 > 0 ) {
1310 
1311  loc_all0[i] = all0 - 1;
1312 
1313  } else {
1314 
1315  fatal("in FSTAT input file at line %i, locus %i first allele value: \
1316 allele value 0 is not allowed!\n*** Please check the input file.\n", lnbr, i+1, all0, nb_all);
1317 
1318  }
1319 
1320  if( all1 > nb_all ){
1321 
1322  error("in FSTAT input file at line %i, locus %i : \
1323 second allele value %i is greater than the max value specified (%i)!\n", lnbr, i+1, all1, nb_all);
1324 
1325  fatal("Please check the input file.\n");
1326 
1327  } else if (all1 > 0) {
1328 
1329  loc_all1[i] = all1 - 1;
1330 
1331  } else {
1332 
1333  fatal("in FSTAT input file at line %i, locus %i second allele value: \
1334 allele value 0 is not allowed!\n*** Please check the input file.\n", lnbr, i+1, all0, nb_all);
1335 
1336  }
1337  }
1338 
1339  if(is_extended) {
1340 
1341  if(extended_num_colums == FSTAT_EXTRA_INFO_LOCI) {
1342 
1343  FILE >> age >> sex >> ped >> origin;
1344 
1345  //age index now saved in the FSTAT file
1346  agex = (static_cast<age_idx> (age) == ADLTx ? ADLTx : OFFSx);
1347 
1348  if(sex > 1)
1349  fatal("in FSTAT input file at line %i, extra column \"sex\" has irregular value %i, must be 0 or 1\n", lnbr, sex);
1350 
1351  } else { //irregular file
1352  getline(FILE, str); // we ignore the extra fields, and use default values
1353  agex = ADLTx;
1354  sex = FEM;
1355  ped = 0;
1356  origin = 1;
1357  }
1358 
1359  } else {
1360  // by default, individuals are adult females
1361  agex = ADLTx;
1362  sex = FEM;
1363  ped = 0;
1364  origin = 1;
1365 
1366  }
1367 
1368 
1369  ind = _pop->makeNewIndividual(0, 0, sex_t(sex), origin - 1);
1370  ind->setPedigreeClass((unsigned char)ped);
1371  trait = ind->getTrait(_FHLinkedTraitIndex);
1372  for(unsigned int i = 0; i < nb_locus; ++i) {
1373  trait->set_allele_value(i, 0, (double)loc_all0[i]);
1374  trait->set_allele_value(i, 1, (double)loc_all1[i]);
1375  }
1376 
1377  _pop->getPatch(patch-1)->add(sex_t(sex), agex, ind);
1378 
1379  lnbr++;
1380 
1381  }
1382 
1383  delete [] loc_all0;
1384  delete [] loc_all1;
1385 
1386  FILE.close();
1387 
1388 #ifdef _DEBUG_
1389  message(">>>> read FSTAT file with %i lines, %i individuals in pop \n", lnbr, _pop->size());
1390 #endif
1391 }
Metapop * _pop
Pointer to the current metapop, set during initialization within the init function.
Definition: filehandler.h:101
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:150
This class contains traits along with other individual information (sex, pedigree,...
Definition: individual.h:47
void setPedigreeClass(Individual *mother, Individual *father)
Definition: individual.h:113
TTrait * getTrait(IDX T)
Trait accessor.
Definition: individual.h:275
unsigned int size()
Get the total number of individuals present in the population, all sex and age classes together.
Definition: metapop.h:310
unsigned int getPatchNbr()
Definition: metapop.h:274
Patch * getPatch(unsigned int i)
Patch accessor, return the ith+1 patch in the metapop.
Definition: metapop.h:255
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:549
unsigned int get_locus_num()
Definition: ttneutralgenes.h:198
unsigned int get_allele_num()
Definition: ttneutralgenes.h:199
Interface for all trait types, declares all basic trait operations.
Definition: ttrait.h:44
virtual void set_allele_value(unsigned int locus, unsigned int allele, double value)=0
Called to change the allelic value at a particular locus.
int _FHLinkedTraitIndex
Definition: filehandler.h:222
TProtoNeutralGenes * _FHLinkedTrait
Definition: filehandler.h:221
void fatal(const char *str,...)
Definition: output.cc:98
int error(const char *str,...)
Definition: output.cc:77
void message(const char *message,...)
Definition: output.cc:38
#define FSTAT_EXTRA_INFO_LOCI
Definition: ttneutralgenes.cc:42
sex_t
Sex types, males are always 0 and females 1!!
Definition: types.h:34
@ FEM
Definition: types.h:35
age_idx
Array index of the age classes in the patch sizes and containers arrays.
Definition: types.h:39
@ OFFSx
Definition: types.h:40
@ ADLTx
Definition: types.h:40

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_allele_value(), Individual::setPedigreeClass(), and Metapop::size().

◆ FHwrite()

void TTNeutralGenesFH::FHwrite ( )
virtual

Implements TraitFileHandler< TProtoNeutralGenes >.

770 {
771  if(!_pop->isAlive()) return;
772 
773  // reset the pop ptr from the file services, will be the main metapop without sub sampling
774  // or a sub sampled pop otherwise:
775 
777 
778  (this->*write_fct)();
779 
780  // reset the pop ptr to the main pop
782 
783 }
FileServices * get_service()
Returns pointer to the FileServices.
Definition: filehandler.h:137
Metapop * getSampledPop()
Sets the down-sampled population and provides accessor to file handlers.
Definition: fileservices.cc:196
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:307

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 
)
965 {
966  Individual *ind;
967  TTrait* trait;
968  char BASE[2] = {'A','G'};
969  unsigned int ploidy = _FHLinkedTrait->get_ploidy();
970  unsigned int nb_locus = _FHLinkedTrait->get_locus_num();
971 
972  // SPECIFICATION FOR THE .fam FILE = 6 first values in .ped files:
973 // .fam (PLINK sample information file)
974 //
975 // Sample information file accompanying a .bed binary genotype table.
976 // Also generated by "--recode lgen" and "--recode rlist".
977 //
978 // A text file with no header line, and one line per sample with the following six fields:
979 //
980 // 1. Family ID ('FID')
981 // 2. Within-family ID ('IID'; cannot be '0')
982 // 3. Within-family ID of father ('0' if father isn't in dataset)
983 // 4. Within-family ID of mother ('0' if mother isn't in dataset)
984 // 5. Sex code ('1' = male, '2' = female, '0' = unknown)
985 // 6. Phenotype value ('1' = control, '2' = case, '-9'/'0'/non-numeric = missing data if case/control)
986 //
987 // 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.
988 
989 
990  for (unsigned int j = 0; j < patch->size(FEM, Ax); ++j) {
991 
992  ind = patch->get(FEM, Ax, j);
993 
994  FH<<"fam"<<ind->getHome()+1
995  <<" "<<ind->getID(); // don't add +1 to keep IDs consistent across files
996 
997  if(Ax == OFFSx)
998  FH<<" "<<ind->getFatherID()<<" "<<ind->getMotherID(); //parents may be in file for offspring, although not guaranteed
999  else
1000  FH<<" 0 0"; //parents not in file for adults
1001 
1002  FH<<" 2 -9";
1003 
1004  trait = ind->getTrait(_FHLinkedTraitIndex);
1005 
1006  for(unsigned int k = 0; k < nb_locus; ++k) {
1007  FH<<" "<< BASE[ trait->get_allele(k, FEM) ]<<" "<< BASE[ trait->get_allele(k, MAL) ]; // the maternally inherited allele comes first
1008  }
1009 
1010  FH <<std::endl;
1011 
1012  }
1013 
1014  for (unsigned int j = 0; j < patch->size(MAL, Ax); ++j) {
1015 
1016  ind = patch->get(MAL, Ax, j);
1017 
1018  FH<<"fam"<<ind->getHome()+1
1019  <<" "<<ind->getID();
1020 
1021  if(Ax == OFFSx)
1022  FH<<" "<<ind->getFatherID()<<" "<<ind->getMotherID(); //parents may be in file for offspring, although not guaranteed
1023  else
1024  FH<<" 0 0"; //parents not in file for adults
1025 
1026  FH<<" 1 -9";
1027 
1028  trait = ind->getTrait(_FHLinkedTraitIndex);
1029 
1030  for(unsigned int k = 0; k < nb_locus; ++k) {
1031  FH<<" "<< BASE[ trait->get_allele(k, FEM) ]<<" "<< BASE[ trait->get_allele(k, MAL) ]; // the maternally inherited allele comes first
1032  }
1033 
1034  FH<<std::endl;
1035 
1036  }
1037 }
unsigned long getID()
Definition: individual.h:120
unsigned short getHome()
Definition: individual.h:126
unsigned long getMotherID()
Definition: individual.h:123
unsigned long getFatherID()
Definition: individual.h:122
unsigned int size(age_t AGE)
Returns the size of the container of the appropriate age class(es) for both sexes.
Definition: metapop.h:496
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:532
unsigned int get_ploidy()
Definition: ttneutralgenes.h:197
virtual unsigned int get_allele(int loc, int all) const =0
Called to read the allele identity at a locus.
@ MAL
Definition: types.h:35

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

Referenced by write_PLINK().

+ Here is the caller graph for this function:

◆ set_write_fct()

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

References write_fct.

Referenced by TProtoNeutralGenes::loadFileServices().

+ Here is the caller graph for this function:

◆ setOutputOption()

void TTNeutralGenesFH::setOutputOption ( string  opt)
759 {
760 
761  if(opt != "1") //it means that the param received an argument value
762  _output_option = opt;
763  else
764  _output_option = "locus"; //default
765 }
string _output_option
Definition: ttneutralgenes.h:262

References _output_option.

Referenced by TProtoNeutralGenes::loadFileServices().

+ Here is the caller graph for this function:

◆ write_Fst_i()

void TTNeutralGenesFH::write_Fst_i ( )
1396 {
1397  // make sure we are using the whole pop, not a sub-sampled one:
1398  Metapop *pop = get_service()->get_pop_ptr();
1399 
1400  if(pop->size(ADULTS) == 0) {
1401  warning("No adults in pop, not writing the Fst distribution to file.\n");
1402  return;
1403  }
1404 
1405  unsigned int nb_locus = _FHLinkedTrait->get_locus_num();
1406  unsigned int patchNbr = pop->getPatchNbr();
1407  double **fst_i;
1408  bool added_stater = false;
1409 
1411 
1412  if(stater == NULL) {
1413  stater = new TTNeutralGenesSH(_FHLinkedTrait);
1415  added_stater = true;
1416  }
1417 
1418  fst_i = new double* [patchNbr];
1419  for(unsigned int i = 0; i < patchNbr; i++)
1420  fst_i[i] = new double [nb_locus];
1421 
1422  stater->setFst_li(patchNbr, nb_locus, fst_i);
1423 
1424  std::string filename = get_path() + this->get_service()->getGenerationReplicateFileName() + get_extension();
1425 
1426 #ifdef _DEBUG_
1427  message("TTNeutralGenesFH::FHwrite (%s)\n",filename.c_str());
1428 #endif
1429 
1430  ofstream FILE (filename.c_str(), ios::out);
1431 
1432  if(!FILE) fatal("could not open Fst_i output file!!\n");
1433 
1434  for(unsigned int i = 0; i < patchNbr; ++i)
1435  FILE<<"patch"<<i+1<<" ";
1436 
1437  FILE<<endl;
1438 
1439  for(unsigned int j = 0; j < nb_locus; ++j) {
1440 
1441  for(unsigned int i = 0; i < patchNbr; i++)
1442  FILE<<fst_i[i][j]<<" ";
1443 
1444  FILE<<endl;
1445  }
1446 
1447  FILE.close();
1448 
1449  if(added_stater) delete stater;
1450 
1451  for(unsigned int i = 0; i < patchNbr; i++)
1452  delete [] fst_i[i];
1453 
1454  delete [] fst_i;
1455 
1456 }
std::string & get_path()
Definition: filehandler.h:141
std::string & get_extension()
Definition: filehandler.h:145
string getGenerationReplicateFileName()
Accessor to the current file name with generation and replicate counters added.
Definition: fileservices.cc:465
Top class of the metapopulation structure, contains the patches.
Definition: metapop.h:78
TTNeutralGenesSH * get_stater()
Definition: ttneutralgenes.h:205
The stat handler for neutral markers.
Definition: ttneutralgenes.h:319
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:1385
void warning(const char *str,...)
Definition: output.cc:56
#define ADULTS
Adults age class flag (breeders).
Definition: types.h:52

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

+ Here is the caller graph for this function:

◆ 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".

1042 {
1045  unsigned int position;
1046  unsigned int ploidy = _FHLinkedTrait->get_ploidy();
1047  unsigned int nb_all = _FHLinkedTrait->get_allele_num();
1048  unsigned int nb_locus = _FHLinkedTrait->get_locus_num();
1049  unsigned int patchNbr = _pop->getPatchNbr();
1050  Patch* current_patch;
1051 
1052  position = nb_all > 99 ? 3 : nb_all > 9 ? 2 : 1; //assumes nb_all not sup. to 999
1053 
1054  std::string filename = get_path() + this->get_service()->getGenerationReplicateFileName()
1055  + get_extension();
1056 
1057 #ifdef _DEBUG_
1058  message("TTNeutralGenesFH::FHwrite (%s)\n",filename.c_str());
1059 #endif
1060 
1061  ofstream FILE (filename.c_str(), ios::out);
1062  std::ios_base::sync_with_stdio(false); // better for writing performances
1063 
1064  if(!FILE) fatal("could not open FSTAT output file!!\n");
1065 
1066  FILE<<patchNbr<<" "<< nb_locus + FSTAT_EXTRA_INFO_LOCI <<" "<<nb_all<<" "<<position<<"\n";
1067 
1068  for (unsigned int i = 0; i < nb_locus; ++i)
1069  FILE<<"loc"<<i+1<<"\n";
1070 
1071  //add names for the three last fields:
1072  FILE<<"age\n"<<"sex\n"<<"ped\n"<<"origin\n";
1073 
1074  for (unsigned int i = 0; i < patchNbr; ++i) {
1075 
1076  current_patch = _pop->getPatch(i);
1077 
1078  write_patch_FSTAT(current_patch, FEM, OFFSx, FILE, position);
1079  write_patch_FSTAT(current_patch, MAL, OFFSx, FILE, position);
1080  write_patch_FSTAT(current_patch, FEM, ADLTx, FILE, position);
1081  write_patch_FSTAT(current_patch, MAL, ADLTx, FILE, position);
1082 
1083  }
1084 
1085  FILE.close();
1086  std::ios_base::sync_with_stdio(true); // reset
1087 
1088 }
Second class in the metapopulation design structure, between the Metapop and Individual classes.
Definition: metapop.h:430
void write_patch_FSTAT(Patch *patch, sex_t SEX, age_idx AGE, ofstream &FH, unsigned int digits)
Definition: ttneutralgenes.cc:1092

References TraitFileHandler< TProtoNeutralGenes >::_FHLinkedTrait, FileHandler::_pop, ADLTx, fatal(), FEM, FSTAT_EXTRA_INFO_LOCI, 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().

+ Here is the caller graph for this function:

◆ 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".

1123 {
1126  unsigned int position;
1127  unsigned int ploidy = _FHLinkedTrait->get_ploidy();
1128  unsigned int nb_all = _FHLinkedTrait->get_allele_num();
1129  unsigned int nb_locus = _FHLinkedTrait->get_locus_num();
1130  unsigned int patchNbr = _pop->getPatchNbr();
1131  Patch* current_patch;
1132 
1133  position = nb_all > 99 ? 3 : 2; //assumes nb_all not sup. to 999
1134 
1135  if(ploidy == 1) position = 1;
1136 
1137  std::string filename = get_path() + this->get_service()->getGenerationReplicateFileName()
1138  + get_extension();
1139 
1140 #ifdef _DEBUG_
1141  message("TTNeutralGenesFH::FHwrite (%s)\n",filename.c_str());
1142 #endif
1143 
1144  ofstream FILE (filename.c_str(), ios::out);
1145  std::ios_base::sync_with_stdio(false); // better for writing performances
1146 
1147  if(!FILE) fatal("could not open FSTAT output file!!\n");
1148 
1149  FILE<<"Title line: "<<patchNbr<<" patches, "<<nb_locus<<" loci with "<<nb_all<<" alleles\n";
1150 
1151  for (unsigned int i = 0; i < nb_locus; ++i)
1152  FILE<<"loc"<<i+1<<", ";
1153 
1154  //add names for the three last fields:
1155  FILE<<"age, sex, ped, origin\n";
1156 
1157  for (unsigned int i = 0; i < patchNbr; ++i) {
1158 
1159  current_patch = _pop->getPatch(i);
1160 
1161  FILE<<"POP\n";
1162 
1163  write_patch_GENEPOP(current_patch, FEM, OFFSx, FILE, position);
1164  write_patch_GENEPOP(current_patch, MAL, OFFSx, FILE, position);
1165  write_patch_GENEPOP(current_patch, FEM, ADLTx, FILE, position);
1166  write_patch_GENEPOP(current_patch, MAL, ADLTx, FILE, position);
1167 
1168 
1169  }
1170 
1171  FILE.close();
1172  std::ios_base::sync_with_stdio(true); // reset
1173 
1174 }
void write_patch_GENEPOP(Patch *patch, sex_t SEX, age_idx AGE, ofstream &FH, unsigned int digits)
Definition: ttneutralgenes.cc:1178

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

+ Here is the caller graph for this function:

◆ write_patch_FSTAT()

void TTNeutralGenesFH::write_patch_FSTAT ( Patch patch,
sex_t  SEX,
age_idx  AGE,
ofstream &  FH,
unsigned int  digits 
)
1093 {
1094  TTrait* trait;
1095  Individual *ind;
1096 
1097  for (unsigned int j = 0; j < patch->size(SEX, AGE); ++j) {
1098 
1099  FH<<patch->getID() + 1<<" ";
1100  ind = patch->get(SEX, AGE, j);
1101  trait = ind->getTrait(_FHLinkedTraitIndex);
1102 
1103  for(unsigned int k = 0; k < _FHLinkedTrait->get_locus_num(); ++k) {
1104  for (unsigned int l = 0; l < _FHLinkedTrait->get_ploidy(); ++l) {
1105  FH.fill('0');
1106  FH.width(digits);
1107  FH<<(trait->get_allele(k, l)+1);
1108  }
1109  FH<<" ";
1110  }
1111 
1112  FH << AGE <<" "
1113  <<SEX<<" "
1114  <<ind->getPedigreeClass()<<" "
1115  <<ind->getHome()+1<<std::endl;
1116  }
1117 
1118 }
unsigned int getPedigreeClass()
Returns the pedigree class of the individual, as set during offspring creation.
Definition: individual.h:177
unsigned int getID()
Definition: metapop.h:479

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

Referenced by write_FSTAT().

+ Here is the caller graph for this function:

◆ write_patch_GENEPOP()

void TTNeutralGenesFH::write_patch_GENEPOP ( Patch patch,
sex_t  SEX,
age_idx  AGE,
ofstream &  FH,
unsigned int  digits 
)
1179 {
1180  TTrait* trait;
1181  Individual *ind;
1182 
1183  for (unsigned int j = 0; j < patch->size(SEX, AGE); ++j) {
1184 
1185  FH<<patch->getID() + 1<<", ";
1186  ind = patch->get(SEX, AGE, j);
1187  trait = ind->getTrait(_FHLinkedTraitIndex);
1188 
1189  for(unsigned int k = 0; k < _FHLinkedTrait->get_locus_num(); ++k) {
1190  for (unsigned int l = 0; l < _FHLinkedTrait->get_ploidy(); ++l) {
1191  FH.fill('0');
1192  FH.width(digits);
1193  FH<<(trait->get_allele(k, l)+1);
1194  }
1195  FH<<" ";
1196  }
1197 
1198  FH << AGE <<" "
1199  <<SEX<<" "
1200  <<ind->getPedigreeClass()<<" "
1201  <<ind->getHome()+1<<std::endl;
1202  }
1203 
1204 }

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

Referenced by write_GENEPOP().

+ Here is the caller graph for this function:

◆ write_patch_TAB()

void TTNeutralGenesFH::write_patch_TAB ( Patch patch,
sex_t  SEX,
age_idx  AGE,
ofstream &  FH 
)
843 {
844  TTrait* trait;
845  Individual *ind;
846 
847  for (unsigned int j = 0; j < patch->size(SEX, AGE); ++j) {
848 
849  FH<<patch->getID() + 1<<" ";
850 
851  ind = patch->get(SEX, AGE, j);
852  trait = ind->getTrait(_FHLinkedTraitIndex);
853 
854  if(_output_option == "snp") {
855 
856  for(unsigned int k = 0; k < _FHLinkedTrait->get_locus_num(); ++k)
857  FH<<trait->get_allele(k, 0)+trait->get_allele(k, 1)<<" ";
858 
859  } else {
860 
861  for(unsigned int k = 0; k < _FHLinkedTrait->get_locus_num(); ++k) {
862  for (unsigned int l = 0; l < _FHLinkedTrait->get_ploidy(); ++l) {
863  FH<<(trait->get_allele(k, l)+1)<<" ";
864  }
865  }
866  }
867  FH << AGE <<" "
868  <<SEX<<" "
869  <<ind->getHome()+1<<" "
870  <<ind->getPedigreeClass()<<" "
871  << (ind->getFather() && ind->getMother() ?
872  (ind->getFather()->getHome()!=patch->getID()) + (ind->getMother()->getHome()!= patch->getID()) : 0)
873  <<" "
874  <<ind->getFatherID()<<" "
875  <<ind->getMotherID()<<" "
876  <<ind->getID()<<std::endl;
877 
878  }
879 
880 }
Individual * getMother()
Definition: individual.h:125
Individual * getFather()
Definition: individual.h:124

References TraitFileHandler< TProtoNeutralGenes >::_FHLinkedTrait, TraitFileHandler< TProtoNeutralGenes >::_FHLinkedTraitIndex, _output_option, Patch::get(), TTrait::get_allele(), TProtoNeutralGenes::get_locus_num(), TProtoNeutralGenes::get_ploidy(), 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().

+ Here is the caller graph for this function:

◆ write_PLINK()

void TTNeutralGenesFH::write_PLINK ( )
885 {
886 
887  if(_FHLinkedTrait->get_allele_num() > 2)
888  fatal("PLINK file output for the neutral loci is only possible for di-allelic loci\n");
889 
890 
891  unsigned int ploidy = _FHLinkedTrait->get_ploidy();
892  unsigned int nb_locus = _FHLinkedTrait->get_locus_num();
893  unsigned int patchNbr = _pop->getPatchNbr();
894  Patch* current_patch;
895  age_t pop_age = _pop->getCurrentAge(); //flag telling which age class should contain individuals
896 
897  std::string filename = get_path() + this->get_service()->getGenerationReplicateFileName() + "-ntrl.ped";
898 
899 #ifdef _DEBUG_
900  message("TTNeutralGenesFH::write_PLINK (%s)\n",filename.c_str());
901 #endif
902 
903  // the PED file -------------------------------------------------------------------------
904  ofstream PED (filename.c_str(), ios::out);
905  std::ios_base::sync_with_stdio(false); // better for writing performances
906 
907  if(!PED) fatal("could not open plink .ped output file!!\n");
908 
909  for (unsigned int i = 0; i < patchNbr; ++i) {
910 
911  current_patch = _pop->getPatch(i);
912 
913  if( pop_age & OFFSPRG )
914  print_PLINK_PED(PED, OFFSx, current_patch);
915 
916  if( pop_age & ADULTS )
917  print_PLINK_PED(PED, ADLTx, current_patch);
918 
919  }
920 
921 
922  PED.close();
923 
924  // the MAP file -------------------------------------------------------------------------
925  filename = get_path() + this->get_service()->getGenerationReplicateFileName() + "-ntrl.map";
926 
927  ofstream MAP (filename.c_str(), ios::out);
928 
929  if(!MAP) fatal("could not open plink .map output file!!\n");
930 
931  double *map[2];
932  map[0] = new double [nb_locus]; // chromosome
933  map[1] = new double [nb_locus]; // position
934 
935  bool found = _FHLinkedTrait->_map.getGeneticMap(_FHLinkedTrait->get_type(), map, nb_locus);
936 
937  if( found ) {
938 
939  // MAP FORMAT (PLINK1.9): chrmsm ID; Loc ID; position (cM); bp ID
940  for(unsigned int k = 0; k < nb_locus; ++k) {
941  MAP<<map[0][k]+1<<" "<<"loc"<<k+1<<" "<<map[1][k]<<" "<<k+1<<endl;
942  }
943  } else { // trait didn't register a genetic map, loci are unlinked (free recombination)
944 
945  warning("PLINK .map file: we assume ntrl loci are unlinked, separated by 50M and on a single chromosome\n");
946 
947  // we're gonna set all loci on a single chrmsme, but 50M apart
948  for(unsigned int k = 0; k < nb_locus; ++k) {
949  MAP<<"1 "<<"loc"<<k+1<<" "<< k*5000.0 + 1.0<<" "<<k+1<<endl;
950  }
951  }
952 
953  MAP.close();
954 
955  std::ios_base::sync_with_stdio(true); // reset
956 
957  delete [] map[0];
958  delete [] map[1];
959 }
bool getGeneticMap(trait_t trait, double **table, unsigned int table_length)
Definition: ttrait_with_map.cc:923
age_t getCurrentAge()
Definition: metapop.h:297
virtual trait_t get_type() const
Definition: ttneutralgenes.h:220
void print_PLINK_PED(ofstream &FH, age_idx Ax, Patch *patch)
Definition: ttneutralgenes.cc:964
static GeneticMap _map
Definition: ttrait_with_map.h:227
unsigned int age_t
Age class flags.
Definition: types.h:44
#define OFFSPRG
Offspring age class flag.
Definition: types.h:48

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

+ Here is the caller graph for this function:

◆ write_PLINK_BED()

void TTNeutralGenesFH::write_PLINK_BED ( ofstream &  BED)

◆ write_TAB()

void TTNeutralGenesFH::write_TAB ( )

The file extension is ".txt".

789 {
791  unsigned int ploidy = _FHLinkedTrait->get_ploidy();
792  unsigned int nb_locus = _FHLinkedTrait->get_locus_num();
793  unsigned int patchNbr = _pop->getPatchNbr();
794  Patch* current_patch;
795 
796  std::string filename = get_path() + this->get_service()->getGenerationReplicateFileName()
797  + get_extension();
798 
799 #ifdef _DEBUG_
800  message("TTNeutralGenesFH::write_TAB (%s)\n",filename.c_str());
801 #endif
802 
803  ofstream FILE (filename.c_str(), ios::out);
804  std::ios_base::sync_with_stdio(false); // better for writing performances
805 
806  if(!FILE) fatal("could not open TAB output file!!\n");
807 
808  FILE<<"pop";
809 
810  for (unsigned int i = 0; i < nb_locus; ++i) {
811 
812  if( _output_option == "snp") {
813 
814  FILE<<" l"<<i+1;
815 
816  } else {
817  for (unsigned int j = 0; j < ploidy; ++j)
818  FILE<<" l"<<i+1<<1<<j+1;
819  }
820  }
821  //add names for the three last fields:
822  FILE<<" age sex home ped isMigrant father mother ID\n";
823 
824  for (unsigned int i = 0; i < patchNbr; ++i) {
825 
826  current_patch = _pop->getPatch(i);
827 
828  write_patch_TAB(current_patch, FEM, OFFSx, FILE);
829  write_patch_TAB(current_patch, MAL, OFFSx, FILE);
830  write_patch_TAB(current_patch, FEM, ADLTx, FILE);
831  write_patch_TAB(current_patch, MAL, ADLTx, FILE);
832 
833  }
834 
835  FILE.close();
836 
837  std::ios_base::sync_with_stdio(true); // reset
838 }
void write_patch_TAB(Patch *patch, sex_t SEX, age_idx AGE, ofstream &FH)
Definition: ttneutralgenes.cc:842

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

+ Here is the caller graph for this function:

◆ write_varcompWC()

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

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

+ Here is the caller graph for this function:

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.2 by  doxygen 1.9.1

Catalogued on GSR