Nemo  2.4.0
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
270  { }
void(TTNeutralGenesFH::* write_fct)()
Definition: ttneutralgenes.h:264

◆ ~TTNeutralGenesFH()

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

Member Function Documentation

◆ FHread()

void TTNeutralGenesFH::FHread ( string &  filename)
virtual

reads in only FSTAT format

Implements FileHandler.

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

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

771 {
772  if(!_pop->isAlive()) return;
773 
774  // reset the pop ptr from the file services, will be the main metapop without sub sampling
775  // or a sub sampled pop otherwise:
776 
778 
779  (this->*write_fct)();
780 
781  // reset the pop ptr to the main pop
783 
784 }
FileServices * get_service()
Returns pointer to the FileServices.
Definition: filehandler.h:138
Metapop * getSampledPop()
Sets the down-sampled population and provides accessor to file handlers.
Definition: fileservices.cc:197
virtual Metapop * get_pop_ptr()
Accessor to the pointer to the main population.
Definition: fileservices.h:112
bool isAlive()
Checks if the population still contains at least one individual in any sex or age class.
Definition: metapop.h:308

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

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
291 {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)
760 {
761 
762  if(opt != "1") //it means that the param received an argument value
763  _output_option = opt;
764  else
765  _output_option = "locus"; //default
766 }
string _output_option
Definition: ttneutralgenes.h:263

References _output_option.

Referenced by TProtoNeutralGenes::loadFileServices().

+ Here is the caller graph for this function:

◆ write_Fst_i()

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

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

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

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

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

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

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

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

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

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

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

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

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

Locations of visitors to this page
Catalogued on GSR