bpp-popgen  3.0.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
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;
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
111 {
112  AbstractODataSet::write(path, data_set, overwrite);
113 }
114 
virtual void write(std::ostream &os, const DataSet &data_set) const =0
Write a DataSet on ostream.
virtual const std::string & getId() const =0
Get the identitier of the allele.
The AnalyzedLoci class.
Definition: AnalyzedLoci.h:65
std::vector< size_t > getNumberOfAlleles() const
Get the number of alleles at each locus.
void write(std::ostream &os, const DataSet &data_set) const
Write a DataSet on ostream.
DarwinVarSingle(size_t missingData=999)
The DataSet class.
Definition: DataSet.h:73
size_t getNumberOfGroups() const
Get the number of Groups.
Definition: DataSet.cpp:300
size_t getNumberOfIndividualsInGroup(size_t group_position) const
Get the number of Individuals in a Group.
Definition: DataSet.cpp:439
const LocusInfo & getLocusInfoAtPosition(size_t locus_position) const
Get a LocusInfo by its position.
Definition: DataSet.cpp:1142
size_t getNumberOfLoci() const
Get the number of loci.
Definition: DataSet.cpp:1202
const AnalyzedLoci * getAnalyzedLoci() const
Get the AnalyzedLoci if there is one.
Definition: DataSet.cpp:1093
const Individual * getIndividualAtPositionFromGroup(size_t group_position, size_t individual_position) const
Get an Individual from a Group.
Definition: DataSet.cpp:464
const MultilocusGenotype & getGenotype() const
Get the genotype.
Definition: Individual.cpp:508
The LocusInfo class.
Definition: LocusInfo.h:64
size_t getNumberOfAlleles() const
Get the number of alleles at this locus.
Definition: LocusInfo.cpp:121
const AlleleInfo & getAlleleInfoByKey(size_t key) const
Retrieve an AlleleInfo object of the LocusInfo.
Definition: LocusInfo.cpp:104
const std::string & getName() const
Get the name of the locus.
Definition: LocusInfo.h:101
The MonolocusGenotype virtual class.
virtual std::vector< size_t > getAlleleIndex() const =0
Get the alleles' index.
The MultilocusGenotype class.
const MonolocusGenotype & getMonolocusGenotype(size_t locus_position) const
Get a MonolocusGenotype.
bool isMonolocusGenotypeMissing(size_t locus_position) const
Tell if a MonolocusGenotype is a missing data.
size_t size() const
Count the number of loci.
static bool contains(const std::vector< T > &vec, T el)
static std::string paste(const std::vector< T > &v, const std::string &delim=" ")
static void print(const std::vector< T > &v1, OutputStream &out=*ApplicationTools::message, const std::string &delim=" ")