bpp-popgen  2.2.0
DarwinVarSingle.cpp
Go to the documentation of this file.
1 //
2 // File DarwinVarSingle.cpp
3 // Authors : Sylvain Gaillard
4 // Last modification : April 7, 2008
5 //
6 
7 /*
8  Copyright or © or Copr. Bio++ Development Team, (April 7, 2008)
9 
10  This software is a computer program whose purpose is to provide classes
11  for population genetics analysis.
12 
13  This software is governed by the CeCILL license under French law and
14  abiding by the rules of distribution of free software. You can use,
15  modify and/ or redistribute the software under the terms of the CeCILL
16  license as circulated by CEA, CNRS and INRIA at the following URL
17  "http://www.cecill.info".
18 
19  As a counterpart to the access to the source code and rights to copy,
20  modify and redistribute granted by the license, users are provided only
21  with a limited warranty and the software's author, the holder of the
22  economic rights, and the successive licensors have only limited
23  liability.
24 
25  In this respect, the user's attention is drawn to the risks associated
26  with loading, using, modifying and/or developing or reproducing the
27  software by the user in light of its specific status of free software,
28  that may mean that it is complicated to manipulate, and that also
29  therefore means that it is reserved for developers and experienced
30  professionals having in-depth computer knowledge. Users are therefore
31  encouraged to load and test the software's suitability as regards their
32  requirements in conditions enabling the security of their systems and/or
33  data to be ensured and, more generally, to use and operate it in the
34  same conditions as regards security.
35 
36  The fact that you are presently reading this means that you have had
37  knowledge of the CeCILL license and that you accept its terms.
38  */
39 
40 #include "DarwinVarSingle.h"
41 
42 using namespace bpp;
43 using namespace std;
44 
45 DarwinVarSingle::DarwinVarSingle(size_t missingData) : missingData_(missingData) {}
46 
48 
49 void DarwinVarSingle::write(ostream& os, const DataSet& data_set) const throw (Exception)
50 {
51  if (!os)
52  throw IOException("DarwinVarSingle::write: fail to open stream.");
53  StlOutputStreamWrapper out(&os);
54  (out << "@DARwin 5.0 - SINGLE").endLine();
55  size_t ind_nbr = 0;
56  for (size_t i = 0; i < data_set.getNumberOfGroups(); i++)
57  {
58  ind_nbr += data_set.getNumberOfIndividualsInGroup(i);
59  }
60  vector<string> header;
61  header.push_back("Unit");
62  for (size_t i = 0; i < data_set.getNumberOfLoci(); i++)
63  {
64  const LocusInfo& li = data_set.getLocusInfoAtPosition(i);
65  for (size_t j = 0; j < li.getNumberOfAlleles(); j++)
66  {
67  header.push_back(li.getName() + "." + li.getAlleleInfoByKey(j).getId());
68  }
69  }
70  size_t var_nbr = header.size() - 1;
71  // header.push_back("Name");
72  (out << ind_nbr << "\t" << var_nbr).endLine();
73  VectorTools::print(header, out, "\t");
74  // size_t ind_index = 0;
75  const AnalyzedLoci* al = data_set.getAnalyzedLoci();
76  for (size_t i = 0; i < data_set.getNumberOfGroups(); i++)
77  {
78  size_t ind_nbr_ig = data_set.getNumberOfIndividualsInGroup(i);
79  for (size_t j = 0; j < ind_nbr_ig; j++)
80  {
81  vector<size_t> var;
82  const MultilocusGenotype& geno = data_set.getIndividualAtPositionFromGroup(i, j)->getGenotype();
83  for (size_t k = 0; k < geno.size(); k++)
84  {
85  const MonolocusGenotype& mg = geno.getMonolocusGenotype(k);
86  if (geno.isMonolocusGenotypeMissing(k))
87  {
88  for (size_t l = 0; l < al->getNumberOfAlleles()[k]; l++)
89  {
90  var.push_back(missingData_);
91  }
92  }
93  else
94  {
95  for (size_t l = 0; l < al->getNumberOfAlleles()[k]; l++)
96  {
97  size_t flag = 0;
98  if (VectorTools::contains(mg.getAlleleIndex(), l))
99  flag = 1;
100  var.push_back(flag);
101  }
102  }
103  // var.push_back((mg->getAlleleIndex()).size());
104  }
105  (out << j + (i * ind_nbr_ig) + 1 << "\t" << VectorTools::paste(var, "\t")).endLine();
106  }
107  }
108 }
109 
110 void DarwinVarSingle::write(const string& path, const DataSet& data_set, bool overwrite) const throw (Exception)
111 {
112  AbstractODataSet::write(path, data_set, overwrite);
113 }
114 
const MonolocusGenotype & getMonolocusGenotype(size_t locus_position) const
Get a MonolocusGenotype.
STL namespace.
const AlleleInfo & getAlleleInfoByKey(size_t key) const
Retrieve an AlleleInfo object of the LocusInfo.
Definition: LocusInfo.cpp:104
The MultilocusGenotype class.
bool isMonolocusGenotypeMissing(size_t locus_position) const
Tell if a MonolocusGenotype is a missing data.
std::vector< size_t > getNumberOfAlleles() const
Get the number of alleles at each locus.
const std::string & getName() const
Get the name of the locus.
Definition: LocusInfo.h:101
size_t getNumberOfAlleles() const
Get the number of alleles at this locus.
Definition: LocusInfo.cpp:122
virtual const std::string & getId() const =0
Get the identitier of the allele.
The AnalyzedLoci class.
Definition: AnalyzedLoci.h:64
size_t size() const
Count the number of loci.
DarwinVarSingle(size_t missingData=999)
The MonolocusGenotype virtual class.
The LocusInfo class.
Definition: LocusInfo.h:63
void write(std::ostream &os, const DataSet &data_set) const
Write a DataSet on ostream.
virtual void write(std::ostream &os, const DataSet &data_set) const =0
Write a DataSet on ostream.
virtual std::vector< size_t > getAlleleIndex() const =0
Get the alleles&#39; index.
The DataSet class.
Definition: DataSet.h:73