bpp-seq3  3.0.0
BppSequenceApplication.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 // From the STL:
6 #include <iostream>
7 #include <iomanip>
8 #include <limits>
9 
10 #include "BppSequenceApplication.h"
11 
12 #include "../Alphabet/AlphabetTools.h"
13 #include "../Container/SiteContainerTools.h"
14 
15 using namespace std;
16 using namespace bpp;
17 
18 /******************************************************************************/
19 
20 shared_ptr<Alphabet> BppSequenceApplication::getAlphabet(
21  const string& suffix,
22  bool suffixIsOptional,
23  bool allowGeneric) const
24 {
25  return SequenceApplicationTools::getAlphabet(params_, suffix, suffixIsOptional, verbose_, allowGeneric, warn_);
26 }
27 
28 shared_ptr<GeneticCode> BppSequenceApplication::getGeneticCode(
29  shared_ptr<const Alphabet>& alphabet,
30  const string& suffix,
31  bool suffixIsOptional) const
32 {
33  shared_ptr<const CodonAlphabet> codonAlphabet = dynamic_pointer_cast<const CodonAlphabet>(alphabet);
34 
35  if (!codonAlphabet)
36  {
37  auto allAlp = dynamic_pointer_cast<const AllelicAlphabet>(alphabet);
38  if (allAlp)
39  codonAlphabet = dynamic_pointer_cast<const CodonAlphabet>(allAlp->getStateAlphabet());
40  }
41 
42  if (codonAlphabet)
43  {
44  string codeDesc = ApplicationTools::getStringParameter("genetic_code", params_, "Standard", suffix, suffixIsOptional, warn_);
45  if (verbose_)
46  ApplicationTools::displayResult("Genetic Code", codeDesc);
47  auto alphaPtr = codonAlphabet->getNucleicAlphabet();
48  return SequenceApplicationTools::getGeneticCode(alphaPtr, codeDesc);
49  }
50  else
51  return nullptr;
52 }
53 
54 map<size_t, unique_ptr<AlignmentDataInterface>>
55 BppSequenceApplication::getAlignmentsMap(
56  shared_ptr<const Alphabet>& alphabet,
57  bool changeGapsToUnknownCharacters,
58  bool optionalData,
59  const std::string& prefix,
60  const std::string& suffix,
61  bool suffixIsOptional) const
62 {
63  try
64  {
65  auto mSites = SequenceApplicationTools::getProbabilisticSiteContainers(alphabet, params_, prefix, suffix, suffixIsOptional, verbose_, warn_);
66 
67  if (!optionalData && mSites.size() == 0)
68  throw Exception("Missing data input.sequence.file option");
69 
70  if (changeGapsToUnknownCharacters)
71  {
72  for (auto& sites : mSites)
73  {
74  SiteContainerTools::changeGapsToUnknownCharacters(*sites.second);
75  }
76  }
77 
78  for (auto& sites : mSites)
79  {
80  if (sites.second->getNumberOfSites() == 0)
81  throw Exception("Empty alignment number " + TextTools::toString(sites.first));
82  }
83 
84  map<size_t, unique_ptr<AlignmentDataInterface>> mSites2;
85  for (auto& sites : mSites)
86  {
87  mSites2.emplace(sites.first, unique_ptr<AlignmentDataInterface>(sites.second.release()));
88  }
89 
90  return mSites2;
91  }
92  catch (IOException& e)
93  {
94  auto mSites = SequenceApplicationTools::getSiteContainers(alphabet, params_, prefix, suffix, suffixIsOptional, verbose_, warn_);
95 
96  if (!optionalData && mSites.size() == 0)
97  throw Exception("Missing data input.sequence.file option");
98 
99  if (changeGapsToUnknownCharacters)
100  {
101  for (auto& itc : mSites)
102  {
103  SiteContainerTools::changeGapsToUnknownCharacters(*itc.second);
104  }
105  }
106 
107  for (auto& sites:mSites)
108  {
109  if (sites.second->getNumberOfSites() == 0)
110  throw Exception("Empty alignment number " + TextTools::toString(sites.first));
111  }
112 
113  map<size_t, unique_ptr<AlignmentDataInterface>> mSites2;
114  for (auto& sites : mSites)
115  {
116  mSites2.emplace(sites.first, unique_ptr<AlignmentDataInterface>(sites.second.release()));
117  }
118 
119  return mSites2;
120  }
121 }
122 
123 map<size_t, unique_ptr<const AlignmentDataInterface>>
124 BppSequenceApplication::getConstAlignmentsMap(
125  shared_ptr<const Alphabet>& alphabet,
126  bool changeGapsToUnknownCharacters,
127  bool optionalData,
128  const std::string& prefix,
129  const std::string& suffix,
130  bool suffixIsOptional) const
131 {
132  auto mSites = getAlignmentsMap(alphabet, changeGapsToUnknownCharacters, optionalData, prefix, suffix, suffixIsOptional);
133 
134  map<size_t, unique_ptr<const AlignmentDataInterface>> mSitesConst;
135  for (auto& sites : mSites)
136  {
137  mSitesConst[sites.first] = std::move(sites.second);
138  }
139 
140  return mSitesConst;
141 }
This alphabet is used to deal NumericAlphabet.