...
 
Commits (4)
......@@ -15,7 +15,7 @@
* --------------------------------------------------------------------------------------------
* Convert Metapop files into Genepop files
* ********************************************************************************************/
void convert2gnp ( std::string filename, std::vector<population>& deme ) {
void convert2gnp (std::string filename, std::vector<population>& deme, const allele2D& alleles) {
std::size_t loci (deme[0].getIndividual(0).getLociNumber());
std::ofstream genepop_file;
......@@ -33,31 +33,26 @@ void convert2gnp ( std::string filename, std::vector<population>& deme ) {
std::size_t digits (1);
allele_t max (0);
for (std::size_t p(0); p < deme.size(); ++p) {
if (DEBUG) std::cout << "\r Reading population " << p+1 << " / " << deme.size();
for (std::size_t i(0); i < deme[p].getPopulationSize(); ++i) {
for (std::size_t l(0); l < loci; ++l) {
if ( deme[p].getIndividual(i).getLocus(l).getA(0) > max ) {
max = deme[p].getIndividual(i).getLocus(l).getA(0);
}
if ( deme[p].getIndividual(i).getLocus(l).getA(1) > max ) {
max = deme[p].getIndividual(i).getLocus(l).getA(1);
}
for (std::size_t l(0);l < alleles.size(); ++l) {
if (DEBUG) std::cout << "\r Reading locus " << l+1 << " / " << alleles.size();
for (std::size_t a(0); a < alleles[l].size(); ++a) {
if (alleles[l][a] > max ) {
max = alleles[l][a];
}
}
}
while (max /= 10) ++digits;
if (DEBUG) std::cout << std::endl;
for (std::size_t p (0); p < deme.size(); p++){
for (std::size_t p (0); p < deme.size(); ++p) {
if (DEBUG) std::cout << "\r Writting population " << p+1 << " / " << deme.size();
genepop_file << "Pop" << std::endl;
for (std::size_t i(0); i<deme[p].getPopulationSize(); i++){
for (std::size_t i(0); i<deme[p].getPopulationSize(); ++i) {
individual ind (deme[p].getIndividual(i));
genepop_file << deme[p].getLabel() << p << i << ", ";
for (std::size_t l(0); l< loci; l++){
allele_t a (deme[p].getIndividual(i).getLocus(l).getA(0));
allele_t b (deme[p].getIndividual(i).getLocus(l).getA(1));
allele_t a (ind.getLocus(l).getA(0));
allele_t b (ind.getLocus(l).getA(1));
genepop_file << std::setfill('0') << setw(digits) << a << setw(digits) << b << "\t";
}
genepop_file << std::endl;
......@@ -163,6 +158,7 @@ void convert (const Metapop& input, const MTPConfigFile& config) {
std::string mode (config.get_convert_mode());
bool use_nucleotides (config.get_use_nucleotides());
int ploidy (config.get_ploidy());
allele2D alleles (input.get_alleles_per_locus());
if (mode == "mtp2gp") {
if (use_nucleotides) {
......@@ -171,7 +167,7 @@ void convert (const Metapop& input, const MTPConfigFile& config) {
}
std::string genepop_filename (caso + ".gen");
std::cout << "Converting " << input.get_filename() << " (Metapop format) into " << genepop_filename << " (Genepop format)" << std::endl;
convert2gnp (genepop_filename, deme);
convert2gnp (genepop_filename, deme, alleles);
} else if (mode == "mtp2ped") {
std::string ped_filename (caso + ".ped");
std::cout << "Converting " << input.get_filename() << " (Metapop format) into " << ped_filename << " (Plink PED format)" << std::endl;
......
......@@ -6,10 +6,10 @@
#include "output.h"
// ===== FUNCTION'S PROTOTYPES =====
void convert2gnp ( std::string , const std::vector<population>& );
void convert2ped ( std::string , const std::vector<population>& , bool , int );
void convert_deme2mtp ( std::string , const std::vector<population>& , bool );
void convert ( const Metapop& , const MTPConfigFile& );
void convert2gnp (std::string, const std::vector<population>&, const allele2D&);
void convert2ped (std::string, const std::vector<population>&, bool, int);
void convert_deme2mtp (std::string, const std::vector<population>&, bool);
void convert (const Metapop&, const MTPConfigFile&);
#endif
......@@ -33,7 +33,7 @@ individual::individual (std::string fname, unshort ID, unshort sex, const std::v
* --------------------------------------------------------------------------------------------
* List of methods to return protected attributes
* ********************************************************************************************/
locus individual::getLocus ( std::size_t index) const {
locus individual::getLocus (const std::size_t& index) const {
return loci[index];
}
......
......@@ -20,7 +20,7 @@ public:
individual ( std::string , unshort , unshort , const std::vector<locus>& );
locus getLocus ( std::size_t ) const;
locus getLocus ( const std::size_t& ) const;
std::size_t getLociNumber ();
std::string getLabel ();
int getSex ();
......
......@@ -23,7 +23,7 @@ locus::locus(const allele1D& as) {
this->check();
}
locus::locus(allele_t a1, allele_t a2) {
locus::locus(const allele_t& a1, const allele_t& a2) {
alleles.push_back(a1);
alleles.push_back(a2);
this->check();
......@@ -34,11 +34,11 @@ locus::locus(allele_t a1, allele_t a2) {
* --------------------------------------------------------------------------------------------
* Return information of alleles in the locus.
* ********************************************************************************************/
allele_t locus::getA (std::size_t n) const {
allele_t locus::getA (const std::size_t& n) const {
return alleles[n];
}
allele_t locus::getNAllels (allele_t a) const {
allele_t locus::getNAllels (const allele_t& a) const {
unshort nalel (0);
for (auto ai: alleles) if (a==ai) ++nalel;
return nalel;
......
......@@ -13,10 +13,10 @@ class locus {
public:
locus ( const std::vector<allele_t>& );
locus ( allele_t , allele_t );
allele_t getA ( std::size_t ) const;
locus ( const allele_t& , const allele_t& );
allele_t getA ( const std::size_t& ) const;
allele1D getAllels () const { return alleles; }
allele_t getNAllels ( allele_t ) const;
allele_t getNAllels ( const allele_t& ) const;
bool status () const;
protected:
......
......@@ -50,15 +50,17 @@ Metapop::Metapop (std::string file_name, const MTPConfigFile& config) {
if (config.get_datafile_sformat()=="pedigree") read_pedigree = true;
// Read and format input file
bool compute_coancestry (true);
if (config.get_convert_mode() != "none") compute_coancestry = false;
use_nucleotides = config.get_use_nucleotides();
std::string data ((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
file.close();
clean_datafile(data);
filename = file_name;
if (input_format == GNP) input_genepop (data);
else if (input_format == PED) input_pedfile (data, read_pedigree, config.get_rm_file());
else this->update (data); // input_metapop
if (input_format == GNP) input_genepop (data, compute_coancestry);
else if (input_format == PED) input_pedfile (data, read_pedigree, config.get_rm_file(), compute_coancestry);
else this->update (data, compute_coancestry); // input_metapop
if (for_simulation) use_nucleotides = false;
// Read management file
......@@ -117,8 +119,8 @@ Metapop::Metapop ( std::string file_name, const MTPConfigFile& config, std::stri
filename = file_name;
input_format = config.get_datafile_format();
if (mode == "gp2mtp") input_genepop (data);
else if (mode == "ped2mtp") input_pedfile (data, false,"");
if (mode == "gp2mtp") input_genepop (data, false);
else if (mode == "ped2mtp") input_pedfile (data, false,"", false);
else {
std::cerr << "Unknown conversion mode. Select one of 'none', 'mtp2gp' or 'mtp2ped'." << std::endl;
exit(-1);
......@@ -178,7 +180,7 @@ Metapop::Metapop ( const Metapop& original) { // Copy constructor for bootstrap
* --------------------------------------------------------------------------------------------
* Read a string with the content of the datafile, and update its attributes.
* ********************************************************************************************/
void Metapop::update (std::string file) {
void Metapop::update (std::string file, bool compute_coancestry) {
// Initialize variables
n_subpops = 0;
......@@ -199,6 +201,7 @@ void Metapop::update (std::string file) {
// Read metapop file
this->input_metapop(file);
if (!compute_coancestry) return;
// Calculate coancestry matrix from molecular markers (if it hasn't been inputed)
// should this be part of metapop class?
......@@ -1054,7 +1057,7 @@ void Metapop::input_metapop (std::string file) {
* --------------------------------------------------------------------------------------------
* This function creates an intern and simplified metapop input file from a genepop input file
* ********************************************************************************************/
void Metapop::input_genepop (std::string str) {
void Metapop::input_genepop (std::string str, bool compute_coancestry) {
use_molecular_markers = true;
......@@ -1162,6 +1165,7 @@ void Metapop::input_genepop (std::string str) {
}
// Calculate coancestry
if (!compute_coancestry) return;
this->coancestry_matrix();
}
......@@ -1171,7 +1175,7 @@ void Metapop::input_genepop (std::string str) {
* --------------------------------------------------------------------------------------------
* This function creates an intern and simplified metapop input file from a plink PED file
* ********************************************************************************************/
void Metapop::input_pedfile (std::string str, bool calc_pedigree, std::string rmatrix_filename) {
void Metapop::input_pedfile (std::string str, bool calc_pedigree, std::string rmatrix_filename, bool compute_coancestry) {
if (!calc_pedigree) use_molecular_markers = true;
else {
......@@ -1300,6 +1304,7 @@ void Metapop::input_pedfile (std::string str, bool calc_pedigree, std::string rm
}
// Test pedigree
if (!compute_coancestry) return;
if (calc_pedigree) test_pedigree(pedigree);
// Calculate coancestry
......
......@@ -148,7 +148,7 @@ public:
Metapop ( std::string , const MTPConfigFile& );
Metapop ( std::string , const MTPConfigFile& , std::string ); // user for conversion to metapop format
Metapop ( const Metapop& ); // copy constructor for bootstrap
void update ( std::string );
void update ( std::string , bool );
void check ( const MTPConfigFile& );
~Metapop ();
......@@ -209,8 +209,8 @@ protected:
// Input methods
void input_metapop ( std::string );
void input_genepop ( std::string );
void input_pedfile ( std::string, bool, std::string);
void input_genepop ( std::string , bool );
void input_pedfile ( std::string, bool, std::string , bool );
void default_mng ();
void input_management ( std::string );
void end_body ( std::size_t& , std::size_t& , std::size_t& , std::string& , std::vector<individual>& );
......
......@@ -778,7 +778,7 @@ void printSolution ( const MTPConfigFile& config, Metapop& metapop, const Calcul
}
//Establish the input for the new generation
if (simMode) metapop.update (dat.str());
if (simMode) metapop.update (dat.str(), true);
fr.close();
/* SAVE SIMULATION RESULTS */
......