bpp-seq3  3.0.0
BppOAlphabetIndex1Format.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/AAChargeIndex.h"
12 #include "../AlphabetIndex/AAChenGuHuangHydrophobicityIndex.h"
13 #include "../AlphabetIndex/AAChouFasmanAHelixIndex.h"
14 #include "../AlphabetIndex/AAChouFasmanBSheetIndex.h"
15 #include "../AlphabetIndex/AAChouFasmanTurnIndex.h"
16 #include "../AlphabetIndex/AAIndex1Entry.h"
17 #include "../AlphabetIndex/AAMassIndex.h"
18 #include "../AlphabetIndex/AASEA1030Index.h"
19 #include "../AlphabetIndex/AASEAInf10Index.h"
20 #include "../AlphabetIndex/AASEASup30Index.h"
21 #include "../AlphabetIndex/AASurfaceIndex.h"
22 #include "../AlphabetIndex/AAVolumeIndex.h"
23 #include "../AlphabetIndex/CodonFromProteicAlphabetIndex1.h"
24 #include "../AlphabetIndex/GranthamAAPolarityIndex.h"
25 #include "../AlphabetIndex/GranthamAAVolumeIndex.h"
26 #include "../AlphabetIndex/KD_AAHydropathyIndex.h"
27 #include "../AlphabetIndex/KleinAANetChargeIndex.h"
29 
30 using namespace bpp;
31 using namespace std;
32 
33 unique_ptr<AlphabetIndex1> BppOAlphabetIndex1Format::read(const std::string& description)
34 {
35  if (description != "None")
36  {
37  string name;
38  map<string, string> args;
39  KeyvalTools::parseProcedure(description, name, args);
40  if (verbose_)
41  ApplicationTools::displayResult(message_, description);
42 
43  if (AlphabetTools::isCodonAlphabet(*alphabet_))
44  {
45  if (!gencode_)
46  throw Exception("BppOAlphabetIndex2Format::read. Missing genetic code for codon alphabet.");
47 
48  auto alphaPtr = gencode_->getTargetAlphabet();
49  BppOAlphabetIndex1Format reader1(alphaPtr, message_, false);
50 
51  shared_ptr<AlphabetIndex1> ai2(reader1.read(description));
52  if (!AlphabetTools::isProteicAlphabet(ai2->alphabet()))
53  throw Exception("BppOAlphabetIndex2Format::read. Not a Proteic Alphabet for CodonAlphabetIndex1.");
54 
55  return make_unique<CodonFromProteicAlphabetIndex1>(gencode_, ai2);
56  }
57 
58  // Currently, only protein indices are supported:
59  if (!AlphabetTools::isProteicAlphabet(*alphabet_))
60  throw Exception("BppOAlphabetIndex1Format::read. This index is only supported with a protein alphabet.");
61  if (name == "GranthamPolarity")
62  {
63  return make_unique<GranthamAAPolarityIndex>();
64  }
65  else if (name == "GranthamVolume")
66  {
67  return make_unique<GranthamAAVolumeIndex>();
68  }
69  else if (name == "KleinCharge")
70  {
71  return make_unique<KleinAANetChargeIndex>();
72  }
73  else if (name == "KDHydropathy")
74  {
75  return make_unique<KD_AAHydropathyIndex>();
76  }
77  else if (name == "ChouFasmanAHelix")
78  {
79  return make_unique<AAChouFasmanAHelixIndex>();
80  }
81  else if (name == "ChouFasmanBSheet")
82  {
83  return make_unique<AAChouFasmanBSheetIndex>();
84  }
85  else if (name == "ChouFasmanTurn")
86  {
87  return make_unique<AAChouFasmanTurnIndex>();
88  }
89  else if (name == "ChenGuHuangHydrophobicity")
90  {
91  return make_unique<AAChenGuHuangHydrophobicityIndex>();
92  }
93  else if (name == "Surface")
94  {
95  return make_unique<AASurfaceIndex>();
96  }
97  else if (name == "Mass")
98  {
99  return make_unique<AAMassIndex>();
100  }
101  else if (name == "Volume")
102  {
103  return make_unique<AAVolumeIndex>();
104  }
105  else if (name == "Charge")
106  {
107  return make_unique<AAChargeIndex>();
108  }
109  else if (name == "SEAMedium")
110  {
111  return make_unique<AASEA1030Index>();
112  }
113  else if (name == "SEAHigh")
114  {
115  return make_unique<AASEASup30Index>();
116  }
117  else if (name == "SEALow")
118  {
119  return make_unique<AASEAInf10Index>();
120  }
121  else if (name == "User")
122  {
123  string aax1FilePath = ApplicationTools::getAFilePath("file", args, true, true, "", false);
124  ifstream aax1File(aax1FilePath.c_str(), ios::in);
125  auto I = make_unique<AAIndex1Entry>(aax1File);
126  aax1File.close();
127  return I;
128  }
129  else
130  {
131  throw Exception("Invalid index1 '" + name + "'.");
132  }
133  }
134  else
135  {
136  return nullptr;
137  }
138 }
static bool isProteicAlphabet(const Alphabet &alphabet)
static bool isCodonAlphabet(const Alphabet &alphabet)
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.
static void parseProcedure(const std::string &desc, std::string &name, std::map< std::string, std::string > &args)
This alphabet is used to deal NumericAlphabet.