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

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

#include <ttneutralgenes.h>

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

Public Member Functions

 TTNeutralGenesFH (TProtoNeutralGenes *TP)
 
virtual ~TTNeutralGenesFH ()
 
virtual void FHwrite ()
 
virtual void FHread (string &filename)
 
void write_TAB ()
 
void write_patch_TAB (Patch *patch, sex_t SEX, age_idx AGE, ofstream &FH)
 
void write_PLINK ()
 
void print_PLINK_PED (ofstream &FH, age_idx Ax, Patch *patch)
 
void write_PLINK_BED (ofstream &BED)
 
void write_GENEPOP ()
 
void write_patch_GENEPOP (Patch *patch, sex_t SEX, age_idx AGE, ofstream &FH, unsigned int digits)
 
void write_FSTAT ()
 
void write_patch_FSTAT (Patch *patch, sex_t SEX, age_idx AGE, ofstream &FH, unsigned int digits)
 
void write_Fst_i ()
 
void write_varcompWC ()
 
void setOutputOption (string opt)
 
void set_write_fct (void(TTNeutralGenesFH::*fct_ptr)())
 
- Public Member Functions inherited from TraitFileHandler< TProtoNeutralGenes >
 TraitFileHandler (TProtoNeutralGenes *trait_proto, const char *ext)
 
virtual ~TraitFileHandler ()
 
virtual void FHread (string &filename)=0
 
virtual void set (bool rpl_per, bool gen_per, int rpl_occ, int gen_occ, int rank, string path, TProtoNeutralGenes *trait_proto)
 
virtual void set_multi (bool rpl_per, bool gen_per, int rpl_occ, TMatrix *Occ, string path, TProtoNeutralGenes *trait_proto)
 
- Public Member Functions inherited from FileHandler
 FileHandler (const char *ext)
 
virtual ~FileHandler ()
 
virtual void init ()
 Called by notifier during simulation setup, performs file checking. More...
 
virtual vector< string > ifExist ()
 Checks if any file associated with the current file name already exists on disk. More...
 
virtual void set (bool rpl_per, bool gen_per, int rpl_occ, int gen_occ, int rank, string path)
 Sets the hanlder parameters. More...
 
virtual void set_multi (bool rpl_per, bool gen_per, int rpl_occ, TMatrix *Occ, string path)
 
virtual void update ()
 Updates the inner replicate and generation counters and calls FHwrite if needed by the the periodicity of the file. More...
 
Metapopget_pop_ptr ()
 Returns the pointer to the current metapop through the FileServices interface. More...
 
void set_pop_ptr (Metapop *pop_ptr)
 
FileServicesget_service ()
 Returns pointer to the FileServices. More...
 
void set_service (FileServices *srv)
 
std::string & get_path ()
 
void set_path ()
 
std::string & get_extension ()
 
void set_extension (const char *ext)
 
std::string & get_filename ()
 Builds and returns the current file name depending on the periodicity of the file. More...
 
bool get_isInputHandler ()
 
void set_isInputHandler (bool val)
 
bool get_isReplicatePeriodic ()
 
void set_isReplicatePeriodic (bool val)
 
unsigned int get_ReplicateOccurrence ()
 
void set_ReplicateOccurrence (unsigned int val)
 
bool get_isGenerationPeriodic ()
 
void set_isGenerationPeriodic (bool val)
 
unsigned int get_GenerationOccurrence ()
 
void set_GenerationOccurrence (unsigned int val)
 
unsigned int get_ExecRank ()
 unused yet... More...
 
void set_ExecRank (int val)
 
TMatrixget_OccMatrix ()
 
void set_OccMatrix (TMatrix *occ)
 
bool get_isMasterExec ()
 
void set_isMasterExec (bool is)
 
- Public Member Functions inherited from Handler
virtual ~Handler ()
 

Private Attributes

string _output_option
 
void(TTNeutralGenesFH::* write_fct )()
 

Additional Inherited Members

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

Detailed Description

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

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

Constructor & Destructor Documentation

◆ TTNeutralGenesFH()

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

◆ ~TTNeutralGenesFH()

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

Member Function Documentation

◆ FHread()

void TTNeutralGenesFH::FHread ( string &  filename)
virtual

reads in only FSTAT format

Implements FileHandler.

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

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

◆ FHwrite()

void TTNeutralGenesFH::FHwrite ( )
virtual

Implements TraitFileHandler< TProtoNeutralGenes >.

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

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

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

◆ set_write_fct()

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

References write_fct.

Referenced by TProtoNeutralGenes::loadFileServices().

◆ setOutputOption()

void TTNeutralGenesFH::setOutputOption ( string  opt)
763 {
764 
765  if(opt != "1") //it means that the param received an argument value
766  _output_option = opt;
767  else
768  _output_option = "locus"; //default
769 }
string _output_option
Definition: ttneutralgenes.h:264

References _output_option.

Referenced by TProtoNeutralGenes::loadFileServices().

◆ write_Fst_i()

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

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

Referenced by TProtoNeutralGenes::loadFileServices().

◆ write_FSTAT()

void TTNeutralGenesFH::write_FSTAT ( )

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

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

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

Referenced by TProtoNeutralGenes::loadFileServices().

◆ write_GENEPOP()

void TTNeutralGenesFH::write_GENEPOP ( )

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

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

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

Referenced by TProtoNeutralGenes::loadFileServices().

◆ write_patch_FSTAT()

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

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

◆ write_patch_GENEPOP()

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

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

◆ write_patch_TAB()

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

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

◆ write_PLINK()

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

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

Referenced by TProtoNeutralGenes::loadFileServices().

◆ write_PLINK_BED()

void TTNeutralGenesFH::write_PLINK_BED ( ofstream &  BED)

◆ write_TAB()

void TTNeutralGenesFH::write_TAB ( )

The file extension is ".txt".

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

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

Referenced by TProtoNeutralGenes::loadFileServices().

◆ write_varcompWC()

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

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

Referenced by TProtoNeutralGenes::loadFileServices().

Member Data Documentation

◆ _output_option

string TTNeutralGenesFH::_output_option
private

◆ write_fct

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

Referenced by FHwrite(), and set_write_fct().


The documentation for this class was generated from the following files:

Generated for Nemo v2.4.0b by  doxygen 1.9.1 -- Nemo is hosted on  Download Nemo

Locations of visitors to this page
Catalogued on GSR