formula.h
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef GCU_FORMULA_H
00026 #define GCU_FORMULA_H
00027
00028 #include <string>
00029 #include <map>
00030 #include <list>
00031 #include <stdexcept>
00032 #include "isotope.h"
00033 #include "macros.h"
00034 #include "value.h"
00035
00036 namespace gcu
00037 {
00038
00039 typedef enum {
00040 GCU_FORMULA_PARSE_GUESS,
00041 GCU_FORMULA_PARSE_ATOM,
00042 GCU_FORMULA_PARSE_RESIDUE,
00043 GCU_FORMULA_PARSE_ASK,
00044 GCU_FORMULA_PARSE_NO_CASE=8
00045 } FormulaParseMode;
00046
00052 class parse_error: public std::exception
00053 {
00054 public:
00058 explicit
00059 parse_error (const std::string& __arg, int start, int length);
00060
00061 virtual
00062 ~parse_error () throw ();
00063
00067 virtual const char*
00068 what () const throw ();
00072 const char*
00073 what (int& start, int& length) const throw ();
00074
00078 void add_offset (int offset) {m_start += offset;}
00079
00080 private:
00081 std::string m_msg;
00082 int m_start, m_length;
00083
00084 };
00085
00086 class FormulaElt
00087 {
00088 public:
00089 FormulaElt ();
00090 virtual ~FormulaElt ();
00091 virtual std::string Markup ();
00092 virtual void BuildRawFormula (std::map<int, int> &raw) = 0;
00093 virtual int GetValence () = 0;
00094 int stoich;
00095 unsigned start, end;
00096 };
00097
00098 class FormulaAtom: public FormulaElt
00099 {
00100 public:
00101 FormulaAtom (int Z);
00102 virtual ~FormulaAtom ();
00103 std::string Markup ();
00104 void BuildRawFormula (std::map<int, int> &raw);
00105 int GetValence ();
00106 int elt;
00107 };
00108
00109 class FormulaBlock: public FormulaElt
00110 {
00111 public:
00112 FormulaBlock ();
00113 virtual ~FormulaBlock ();
00114 std::string Markup ();
00115 void BuildRawFormula (std::map<int, int> &raw);
00116 std::list<FormulaElt *> children;
00117 int GetValence ();
00118 int parenthesis;
00119 };
00120
00121 class Residue;
00122
00123 class FormulaResidue: public FormulaElt
00124 {
00125 public:
00126 FormulaResidue (Residue const *res, char const *symbol, int Z);
00127 virtual ~FormulaResidue ();
00128 std::string Markup ();
00129 void BuildRawFormula (std::map<int, int> &raw);
00130 int GetValence ();
00131 Residue const *residue;
00132 std::string Symbol;
00133 GCU_RO_PROP (int, Z);
00134 };
00135
00136
00142 class Formula
00143 {
00144 public:
00150 Formula (std::string entry, FormulaParseMode mode = GCU_FORMULA_PARSE_GUESS) throw (parse_error);
00151 virtual ~Formula ();
00152
00156 char const *GetMarkup ();
00160 std::map<int,int> &GetRawFormula ();
00164 char const *GetRawMarkup ();
00171 void SetFormula (std::string entry) throw (parse_error);
00175 void Clear ();
00181 DimensionalValue GetMolecularWeight (bool &artificial);
00187 void CalculateIsotopicPattern (IsotopicPattern &pattern);
00188
00189 std::list<FormulaElt *> const &GetElements () const {return Details;}
00190
00191 private:
00192 bool BuildConnectivity ();
00193 void Parse (std::string &formula, std::list<FormulaElt *>&result) throw (parse_error);
00194 bool AnalString (char *sz, std::list<FormulaElt *> &result, bool &ambiguous, int offset);
00195 bool TryReplace (std::list<FormulaElt *> &result, std::list<FormulaElt *>::iterator it);
00196
00197 private:
00198 std::string Entry, Markup, RawMarkup;
00199 std::map<int,int> Raw;
00200 std::list<FormulaElt *> Details;
00201 DimensionalValue m_Weight;
00202 bool m_WeightCached;
00203 bool m_Artificial;
00204 bool m_ConnectivityCached;
00205 GCU_PROP (FormulaParseMode, ParseMode);
00206 };
00207
00208 }
00209
00210 #endif // GCU_FORMULA_H