bpp-popgen3  3.0.0
DarwinVarSingle.cpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #include "DarwinVarSingle.h"
6 
7 using namespace bpp;
8 using namespace std;
9 
10 DarwinVarSingle::DarwinVarSingle(size_t missingData) : missingData_(missingData) {}
11 
13 
14 void DarwinVarSingle::write(ostream& os, const DataSet& dataset) const
15 {
16  if (!os)
17  throw IOException("DarwinVarSingle::write: fail to open stream.");
18  StlOutputStreamWrapper out(&os);
19  (out << "@DARwin 5.0 - SINGLE").endLine();
20  size_t ind_nbr = 0;
21  for (size_t i = 0; i < dataset.getNumberOfGroups(); i++)
22  {
23  ind_nbr += dataset.getNumberOfIndividualsInGroup(i);
24  }
25  vector<string> header;
26  header.push_back("Unit");
27  for (size_t i = 0; i < dataset.getNumberOfLoci(); ++i)
28  {
29  const LocusInfo& li = dataset.getLocusInfoAtPosition(i);
30  for (size_t j = 0; j < li.getNumberOfAlleles(); ++j)
31  {
32  header.push_back(li.getName() + "." + li.getAlleleInfoByKey(j).getId());
33  }
34  }
35  size_t var_nbr = header.size() - 1;
36  // header.push_back("Name");
37  (out << ind_nbr << "\t" << var_nbr).endLine();
38  VectorTools::print(header, out, "\t");
39  // size_t ind_index = 0;
40  const auto& al = dataset.analyzedLoci();
41  for (size_t i = 0; i < dataset.getNumberOfGroups(); ++i)
42  {
43  size_t ind_nbr_ig = dataset.getNumberOfIndividualsInGroup(i);
44  for (size_t j = 0; j < ind_nbr_ig; ++j)
45  {
46  vector<size_t> var;
47  const auto& geno = dataset.getIndividualAtPositionFromGroup(i, j).getGenotype();
48  for (size_t k = 0; k < geno.size(); ++k)
49  {
50  const auto& mg = geno.monolocusGenotype(k);
51  if (geno.isMonolocusGenotypeMissing(k))
52  {
53  for (size_t l = 0; l < al.getNumberOfAlleles()[k]; ++l)
54  {
55  var.push_back(missingData_);
56  }
57  }
58  else
59  {
60  for (size_t l = 0; l < al.getNumberOfAlleles()[k]; ++l)
61  {
62  size_t flag = 0;
63  if (VectorTools::contains(mg.getAlleleIndex(), l))
64  flag = 1;
65  var.push_back(flag);
66  }
67  }
68  // var.push_back((mg->getAlleleIndex()).size());
69  }
70  (out << j + (i * ind_nbr_ig) + 1 << "\t" << VectorTools::paste(var, "\t")).endLine();
71  }
72  }
73 }
74 
75 void DarwinVarSingle::write(const string& path, const DataSet& dataset, bool overwrite) const
76 {
77  AbstractODataSet::write(path, dataset, overwrite);
78 }
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.
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:37
size_t getNumberOfGroups() const
Get the number of Groups.
Definition: DataSet.cpp:251
const Individual & getIndividualAtPositionFromGroup(size_t groupPosition, size_t individualPosition) const
Get an Individual from a Group.
Definition: DataSet.cpp:415
const AnalyzedLoci & analyzedLoci() const
Get the AnalyzedLoci if there is one.
Definition: DataSet.h:621
size_t getNumberOfIndividualsInGroup(size_t groupPosition) const
Get the number of Individuals in a Group.
Definition: DataSet.cpp:390
const LocusInfo & getLocusInfoAtPosition(size_t locus_position) const
Get a LocusInfo by its position.
Definition: DataSet.cpp:1030
size_t getNumberOfLoci() const
Get the number of loci.
Definition: DataSet.cpp:1090
const MultilocusGenotype & getGenotype() const
Get the genotype.
Definition: Individual.cpp:441
The LocusInfo class.
Definition: LocusInfo.h:31
size_t getNumberOfAlleles() const
Get the number of alleles at this locus.
Definition: LocusInfo.h:134
const AlleleInfo & getAlleleInfoByKey(size_t key) const
Retrieve an AlleleInfo object of the LocusInfo.
Definition: LocusInfo.cpp:42
const std::string & getName() const
Get the name of the locus.
Definition: LocusInfo.h:94
const MonolocusGenotypeInterface & monolocusGenotype(size_t locusPosition) const
Get a MonolocusGenotype.
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=" ")