bpp-popgen  3.0.0
Genetix.cpp
Go to the documentation of this file.
1 //
2 // File Genetix.cpp
3 // Authors : Sylvain Gaillard
4 // Khalid Belkhir
5 // Last modification : Monday August 02 2004
6 //
7 
8 /*
9  Copyright or © or Copr. CNRS, (November 17, 2004)
10 
11  This software is a computer program whose purpose is to provide classes
12  for population genetics analysis.
13 
14  This software is governed by the CeCILL license under French law and
15  abiding by the rules of distribution of free software. You can use,
16  modify and/ or redistribute the software under the terms of the CeCILL
17  license as circulated by CEA, CNRS and INRIA at the following URL
18  "http://www.cecill.info".
19 
20  As a counterpart to the access to the source code and rights to copy,
21  modify and redistribute granted by the license, users are provided only
22  with a limited warranty and the software's author, the holder of the
23  economic rights, and the successive licensors have only limited
24  liability.
25 
26  In this respect, the user's attention is drawn to the risks associated
27  with loading, using, modifying and/or developing or reproducing the
28  software by the user in light of its specific status of free software,
29  that may mean that it is complicated to manipulate, and that also
30  therefore means that it is reserved for developers and experienced
31  professionals having in-depth computer knowledge. Users are therefore
32  encouraged to load and test the software's suitability as regards their
33  requirements in conditions enabling the security of their systems and/or
34  data to be ensured and, more generally, to use and operate it in the
35  same conditions as regards security.
36 
37  The fact that you are presently reading this means that you have had
38  knowledge of the CeCILL license and that you accept its terms.
39  */
40 
41 #include "Genetix.h"
42 
43 using namespace bpp;
44 using namespace std;
45 
47 
49 
50 void Genetix::read(istream& is, DataSet& data_set)
51 {
52  if (!is)
53  throw IOException("Genetix::read: fail to open stream.");
54  // Loci number
55  string temp = FileTools::getNextLine(is);
56  unsigned int loc_nbr;
57  stringstream(temp) >> loc_nbr;
58  data_set.initAnalyzedLoci(loc_nbr);
59 
60  // Groups number
61  temp = FileTools::getNextLine(is);
62  unsigned int grp_nbr;
63  stringstream(temp) >> grp_nbr;
64 
65  // Loci data
66  for (unsigned int i = 0; i < loc_nbr; i++)
67  {
68  // Locus name
69  string name = FileTools::getNextLine(is);
71  LocusInfo tmp_loc(name);
72  // Alleles
73  stringstream values(FileTools::getNextLine(is));
74  unsigned int nbr_al;
75  values >> nbr_al;
76  for (unsigned int j = 0; j < nbr_al; j++)
77  {
78  string al_id;
79  values >> al_id;
80  BasicAlleleInfo tmp_al(al_id);
81  tmp_loc.addAlleleInfo(tmp_al);
82  }
83  data_set.setLocusInfo(i, tmp_loc);
84  }
85 
86  // Groups
87  for (unsigned int i = 0; i < grp_nbr; i++)
88  {
89  data_set.addEmptyGroup(i);
90  // Group name ... Now used khalid
91  temp = FileTools::getNextLine(is);
92  data_set.setGroupName(i, temp);
93 
94  // Number of individuals
95  unsigned int ind_nbr;
96  temp = FileTools::getNextLine(is);
97  stringstream tmp(temp);
98  tmp >> ind_nbr;
99  for (unsigned int j = 0; j < ind_nbr; j++)
100  {
101  temp = FileTools::getNextLine(is);
102  string ind_name(temp.begin(), temp.begin() + 11);
103  temp = string(temp.begin() + 11, temp.end());
104  data_set.addEmptyIndividualToGroup(i, TextTools::removeSurroundingWhiteSpaces(ind_name) + string("_") + TextTools::toString(i + 1) + string("_") + TextTools::toString(j + 1));
105  data_set.initIndividualGenotypeInGroup(i, j);
106  StringTokenizer alleles(temp, string(" "));
107  // cout << alleles.numberOfRemainingTokens() << endl;
108  for (unsigned int k = 0; k < loc_nbr; k++)
109  {
110  string tmp_string = alleles.nextToken();
111  vector<string> tmp_alleles;
112  tmp_alleles.push_back(string(tmp_string.begin(), tmp_string.begin() + 3));
113  tmp_alleles.push_back(string(tmp_string.begin() + 3, tmp_string.begin() + 6));
114  if (tmp_alleles[0] != string("000") && tmp_alleles[1] != string("000"))
115  data_set.setIndividualMonolocusGenotypeByAlleleIdInGroup(i, j, k, tmp_alleles);
116  }
117  }
118  }
119 }
120 
121 void Genetix::read(const string& path, DataSet& data_set)
122 {
123  AbstractIDataSet::read(path, data_set);
124 }
125 
126 DataSet* Genetix::read(istream& is)
127 {
128  return AbstractIDataSet::read(is);
129 }
130 
131 DataSet* Genetix::read(const string& path)
132 {
133  return AbstractIDataSet::read(path);
134 }
135 
virtual void read(std::istream &is, DataSet &data_set)=0
Read a DataSet on istream.
The BasicAlleleInfo class.
The DataSet class.
Definition: DataSet.h:73
void initIndividualGenotypeInGroup(size_t group_position, size_t individual_position)
Initialyze the genotype of an Individual in a Group.
Definition: DataSet.cpp:877
void initAnalyzedLoci(size_t number_of_loci)
Initialize the AnalyzedLoci for number of loci.
Definition: DataSet.cpp:1084
void setGroupName(size_t group_id, const std::string &group_name) const
set the name of a Group.
Definition: DataSet.cpp:254
void setIndividualMonolocusGenotypeByAlleleIdInGroup(size_t group_position, size_t individual_position, size_t locus_position, const std::vector< std::string > allele_id)
Set a MonolocusGenotype of an Individual from a group.
Definition: DataSet.cpp:973
void setLocusInfo(size_t locus_position, const LocusInfo &locus)
Set a LocusInfo.
Definition: DataSet.cpp:1110
void addEmptyIndividualToGroup(size_t group_position, const std::string &individual_id)
Add an empty Individual to a Group.
Definition: DataSet.cpp:423
void addEmptyGroup(size_t group_id)
Add an empty Group to the DataSet.
Definition: DataSet.cpp:217
static std::string getNextLine(std::istream &in)
void read(std::istream &is, DataSet &data_set)
Read a DataSet on istream.
Definition: Genetix.cpp:50
The LocusInfo class.
Definition: LocusInfo.h:64
void addAlleleInfo(const AlleleInfo &allele)
Add an AlleleInfo to the LocusInfo.
Definition: LocusInfo.cpp:83
const std::string & nextToken()
std::string removeSurroundingWhiteSpaces(const std::string &s)
std::string toString(T t)