Commit cc671b80 by Maarten L. Hekkelman

fixes in numeric conversions

parent 96a67b23
...@@ -608,7 +608,13 @@ class sugar : public residue ...@@ -608,7 +608,13 @@ class sugar : public residue
sugar(sugar &&rhs); sugar(sugar &&rhs);
sugar &operator=(sugar &&rhs); sugar &operator=(sugar &&rhs);
int num() const { return std::stoi(m_auth_seq_id); } int num() const {
int result;
auto r = std::from_chars(m_auth_seq_id.data(), m_auth_seq_id.data() + m_auth_seq_id.length(), result);
if (r.ec != std::errc())
throw std::runtime_error("The auth_seq_id should be a number for a sugar");
return result;
}
std::string name() const; std::string name() const;
/// \brief Return the atom the C1 is linked to /// \brief Return the atom the C1 is linked to
......
...@@ -115,8 +115,8 @@ void file::load_dictionary() ...@@ -115,8 +115,8 @@ void file::load_dictionary()
} }
} }
if (not m_validator) // if (not m_validator)
load_dictionary("mmcif_pdbx.dic"); // TODO: maybe incorrect? Perhaps improve? // load_dictionary("mmcif_pdbx.dic"); // TODO: maybe incorrect? Perhaps improve?
} }
void file::load_dictionary(std::string_view name) void file::load_dictionary(std::string_view name)
......
...@@ -704,16 +704,17 @@ class Ff : public FBase ...@@ -704,16 +704,17 @@ class Ff : public FBase
else else
{ {
std::string s{ text() }; std::string s{ text() };
try
{ double d = 0;
os << std::stod(s); auto r = cif::from_chars(s.data(), s.data() + s.length(), d);
} if (r.ec != std::errc())
catch (const std::exception &ex)
{ {
if (VERBOSE >= 0) if (VERBOSE > 0)
std::cerr << "Failed to write '" << s << "' as a double, this indicates an error in the code for writing PDB files" << std::endl; std::cerr << "Failed to write '" << s << "' as a double, this indicates an error in the code for writing PDB files" << std::endl;
os << s; os << s;
} }
else
os << d;
} }
} }
}; };
......
...@@ -1136,16 +1136,15 @@ void PDBFileParser::PreParseInput(std::istream &is) ...@@ -1136,16 +1136,15 @@ void PDBFileParser::PreParseInput(std::istream &is)
{ {
std::string cs = lookahead.substr(offset, len); std::string cs = lookahead.substr(offset, len);
cif::trim(cs); cif::trim(cs);
int result; int result = 0;
try if (not cs.empty())
{
result = cs.empty() ? 0 : stoi(cs);
}
catch (...)
{ {
throw std::runtime_error("Continuation std::string '" + cs + "' is not valid"); auto r = std::from_chars(cs.data(), cs.data() + cs.length(), result);
if (r.ec != std::errc())
throw std::runtime_error("Continuation std::string '" + cs + "' is not valid");
} }
return result; return result;
}; };
...@@ -1402,7 +1401,12 @@ void PDBFileParser::PreParseInput(std::istream &is) ...@@ -1402,7 +1401,12 @@ void PDBFileParser::PreParseInput(std::istream &is)
link.symOpB = cur->vS(67, 72); // 67 - 72 SymOP sym2 Symmetry operator atom 2. link.symOpB = cur->vS(67, 72); // 67 - 72 SymOP sym2 Symmetry operator atom 2.
if (type == "LINK") // 1 - 6 Record name "LINK " if (type == "LINK") // 1 - 6 Record name "LINK "
link.distance = std::stof(cur->vF(74, 78)); {
auto f = cur->vF(74, 78);
auto r = cif::from_chars(f.data(), f.data() + f.length(), link.distance);
if (r.ec != std::errc() and cif::VERBOSE > 0)
std::cerr << "Error parsing link distance at line " << cur->mLineNr << std::endl;
}
// 74 – 78 Real(5.2) Length Link distance // 74 – 78 Real(5.2) Length Link distance
mLinks.push_back(link); mLinks.push_back(link);
...@@ -5107,11 +5111,10 @@ void PDBFileParser::ParseConnectivtyAnnotation() ...@@ -5107,11 +5111,10 @@ void PDBFileParser::ParseConnectivtyAnnotation()
if (mRec->is("LINK ")) if (mRec->is("LINK "))
{ {
distance = vS(74, 78); distance = vS(74, 78);
try
{ double d;
stod(distance); auto r = cif::from_chars(distance.data(), distance.data() + distance.length(), d);
} if (r.ec != std::errc())
catch (const std::invalid_argument &)
{ {
if (cif::VERBOSE > 0) if (cif::VERBOSE > 0)
std::cerr << "Distance value '" << distance << "' is not a valid float in LINK record" << std::endl; std::cerr << "Distance value '" << distance << "' is not a valid float in LINK record" << std::endl;
...@@ -6211,6 +6214,10 @@ file read(std::istream &is) ...@@ -6211,6 +6214,10 @@ file read(std::istream &is)
result.load(is); result.load(is);
} }
// Must be a PDB like file, right?
if (result.get_validator() == nullptr)
result.load_dictionary("mmcif_pdbx.dic");
return result; return result;
} }
......
...@@ -1207,13 +1207,15 @@ void Remark3Parser::storeCapture(const char *category, std::initializer_list<con ...@@ -1207,13 +1207,15 @@ void Remark3Parser::storeCapture(const char *category, std::initializer_list<con
} }
else if (iequals(category, "pdbx_refine_tls_group")) else if (iequals(category, "pdbx_refine_tls_group"))
{ {
std::string tlsGroupID; std::string tlsID;
if (not mDb["pdbx_refine_tls"].empty()) if (not mDb["pdbx_refine_tls"].empty())
tlsGroupID = mDb["pdbx_refine_tls"].back()["id"].as<std::string>(); tlsID = mDb["pdbx_refine_tls"].back()["id"].as<std::string>();
std::string tlsGroupID = cat.get_unique_id("");
cat.emplace({ { "pdbx_refine_id", mExpMethod }, cat.emplace({
{ "pdbx_refine_id", mExpMethod },
{ "id", tlsGroupID }, { "id", tlsGroupID },
{ "refine_tls_id", tlsGroupID } }); { "refine_tls_id", tlsID } });
} }
else if (iequals(category, "pdbx_refine_tls")) else if (iequals(category, "pdbx_refine_tls"))
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment