bpp-seq3  3.0.0
BppOAlphabetIndex2Format.cpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
6 #include <Bpp/Text/KeyvalTools.h>
7 #include <memory>
8 #include <string>
9 
10 #include "../Alphabet/AlphabetTools.h"
11 #include "../AlphabetIndex/AAIndex2Entry.h"
12 #include "../AlphabetIndex/AlphabetIndex1.h"
13 #include "../AlphabetIndex/BLOSUM50.h"
14 #include "../AlphabetIndex/CodonFromProteicAlphabetIndex2.h"
15 #include "../AlphabetIndex/GranthamAAChemicalDistance.h"
16 #include "../AlphabetIndex/MiyataAAChemicalDistance.h"
17 #include "../AlphabetIndex/SimpleIndexDistance.h"
20 
21 using namespace bpp;
22 using namespace std;
23 
24 unique_ptr<AlphabetIndex2> BppOAlphabetIndex2Format::read(const std::string& description)
25 {
26  if (description != "None")
27  {
28  string name;
29  map<string, string> args;
30  KeyvalTools::parseProcedure(description, name, args);
31  if (verbose_)
32  ApplicationTools::displayResult(message_, description);
33 
34  if (AlphabetTools::isCodonAlphabet(*alphabet_))
35  {
36  if (!gencode_)
37  throw Exception("BppOAlphabetIndex2Format::read. Missing genetic code for codon alphabet.");
38 
39  auto alphaPtr = gencode_->getTargetAlphabet();
40  BppOAlphabetIndex2Format reader2(alphaPtr, message_, false);
41 
42  shared_ptr<AlphabetIndex2> ai2(reader2.read(description));
43  if (!AlphabetTools::isProteicAlphabet(ai2->alphabet()))
44  throw Exception("BppOAlphabetIndex2Format::read. Not a Proteic Alphabet for CodonAlphabetIndex2.");
45 
46  return make_unique<CodonFromProteicAlphabetIndex2>(gencode_, ai2);
47  }
48 
49  // Currently, only protein indices are supported:
50  if (!AlphabetTools::isProteicAlphabet(*alphabet_))
51  throw Exception("BppOAlphabetIndex2Format::read. This index is only supported with a protein alphabet.");
52 
53  if (name == "Blosum50")
54  {
55  return make_unique<BLOSUM50>();
56  }
57  else if (name == "Grantham")
58  {
59  bool sym = ApplicationTools::getBooleanParameter("symmetrical", args, true, "", true, 1);
60  auto M = make_unique<GranthamAAChemicalDistance>();
61  M->setSymmetric(sym);
62  if (!sym)
63  M->setPC1Sign(true);
64  return M;
65  }
66  else if (name == "Miyata")
67  {
68  bool sym = ApplicationTools::getBooleanParameter("symmetrical", args, true, "", true, 1);
69  auto M = make_unique<MiyataAAChemicalDistance>();
70  M->setSymmetric(sym);
71  return M;
72  }
73  else if (name == "Diff")
74  {
75  string index1Desc = ApplicationTools::getStringParameter("index1", args, "None", "", true, 1);
76  bool sym = ApplicationTools::getBooleanParameter("symmetrical", args, true, "", true);
77  BppOAlphabetIndex1Format index1Reader(alphabet_, "", false);
78  auto index1 = index1Reader.read(index1Desc);
79  if (index1)
80  {
81  auto M = make_unique<SimpleIndexDistance>(std::move(index1));
82  M->setSymmetric(sym);
83  return M;
84  }
85  else
86  {
87  throw Exception("BppOAlphabetIndex2Format::read. Diff: index1 should be provided.");
88  }
89  }
90  else if (name == "User")
91  {
92  bool sym = ApplicationTools::getBooleanParameter("symmetrical", args, true, "", true, 1);
93  string aax2FilePath = ApplicationTools::getAFilePath("file", args, true, true, "", false);
94  ifstream aax2File(aax2FilePath.c_str(), ios::in);
95  auto M = make_unique<AAIndex2Entry>(aax2File, sym);
96  aax2File.close();
97  return M;
98  }
99  else
100  {
101  throw Exception("Invalid index2 '" + name + "'.");
102  }
103  }
104  else
105  {
106  return 0;
107  }
108 }
static bool isProteicAlphabet(const Alphabet &alphabet)
static bool isCodonAlphabet(const Alphabet &alphabet)
static bool getBooleanParameter(const std::string &parameterName, const std::map< std::string, std::string > &params, bool defaultValue, const std::string &suffix="", bool suffixIsOptional=true, int warn=0)
static std::string getStringParameter(const std::string &parameterName, const std::map< std::string, std::string > &params, const std::string &defaultValue, const std::string &suffix="", bool suffixIsOptional=true, int warn=0)
static void displayResult(const std::string &text, const T &result)
static std::string getAFilePath(const std::string &parameter, const std::map< std::string, std::string > &params, bool isRequired=true, bool mustExist=true, const std::string &suffix="", bool suffixIsOptional=false, const std::string &defaultPath="none", int warn=0)
AlphabetIndex1 I/O in BppO format.
std::unique_ptr< AlphabetIndex1 > read(const std::string &description)
Read a AlphabetIndex1 object from a string.
AlphabetIndex2 I/O in BppO format.
std::unique_ptr< AlphabetIndex2 > read(const std::string &description)
Read a AlphabetIndex2 object from a string.
static void parseProcedure(const std::string &desc, std::string &name, std::map< std::string, std::string > &args)
This alphabet is used to deal NumericAlphabet.