From c6d1ee3cf4592cbe21870f7eb020e0081532021e Mon Sep 17 00:00:00 2001 From: Geoff Hutchison Date: Tue, 22 Aug 2023 11:50:32 -0400 Subject: [PATCH 1/5] Add a Chem MessagePack format as a subclass of CJSON Signed-off-by: Geoff Hutchison --- avogadro/io/CMakeLists.txt | 2 ++ avogadro/io/cjsonformat.cpp | 5 ++- avogadro/io/cjsonformat.h | 5 ++- avogadro/io/cmsgpackformat.cpp | 30 ++++++++++++++++++ avogadro/io/cmsgpackformat.h | 58 ++++++++++++++++++++++++++++++++++ 5 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 avogadro/io/cmsgpackformat.cpp create mode 100644 avogadro/io/cmsgpackformat.h diff --git a/avogadro/io/CMakeLists.txt b/avogadro/io/CMakeLists.txt index fde03f9c3d..3afa3ec267 100644 --- a/avogadro/io/CMakeLists.txt +++ b/avogadro/io/CMakeLists.txt @@ -3,6 +3,7 @@ add_library(IO) avogadro_headers(IO cjsonformat.h cmlformat.h + cmsgpackformat.h dcdformat.h fileformat.h fileformatmanager.h @@ -19,6 +20,7 @@ avogadro_headers(IO target_sources(IO PRIVATE cjsonformat.cpp cmlformat.cpp + cmsgpackformat.cpp dcdformat.cpp fileformat.cpp fileformatmanager.cpp diff --git a/avogadro/io/cjsonformat.cpp b/avogadro/io/cjsonformat.cpp index 715d19ef9c..a3a5b07ca6 100644 --- a/avogadro/io/cjsonformat.cpp +++ b/avogadro/io/cjsonformat.cpp @@ -1043,7 +1043,10 @@ bool CjsonFormat::write(std::ostream& file, const Molecule& molecule) } // Write out the file, use a two space indent to "pretty print". - file << std::setw(2) << root; + if (m_json) + file << std::setw(2) << root; + else // write msgpack + json::to_msgpack(root, file); return true; } diff --git a/avogadro/io/cjsonformat.h b/avogadro/io/cjsonformat.h index 6fbe5f1cf7..556a78204a 100644 --- a/avogadro/io/cjsonformat.h +++ b/avogadro/io/cjsonformat.h @@ -41,7 +41,7 @@ class AVOGADROIO_EXPORT CjsonFormat : public FileFormat std::string specificationUrl() const override { - return "http://wiki.openchemistry.org/Chemical_JSON"; + return "https://github.com/openchemistry/chemicaljson"; } std::vector fileExtensions() const override; @@ -49,6 +49,9 @@ class AVOGADROIO_EXPORT CjsonFormat : public FileFormat bool read(std::istream& in, Core::Molecule& molecule) override; bool write(std::ostream& out, const Core::Molecule& molecule) override; + + // write JSON or MessagePack + bool m_json = true; }; } // end Io namespace diff --git a/avogadro/io/cmsgpackformat.cpp b/avogadro/io/cmsgpackformat.cpp new file mode 100644 index 0000000000..c4dcb4861f --- /dev/null +++ b/avogadro/io/cmsgpackformat.cpp @@ -0,0 +1,30 @@ +/****************************************************************************** + This source file is part of the Avogadro project. + This source code is released under the 3-Clause BSD License, (see "LICENSE"). +******************************************************************************/ + +#include "cmsgpackformat.h" + +using namespace std; + +namespace Avogadro::Io { + +CMsgPackFormat::CMsgPackFormat() = default; + +CMsgPackFormat::~CMsgPackFormat() = default; + +vector CMsgPackFormat::fileExtensions() const +{ + vector ext; + ext.emplace_back("cmpk"); + return ext; +} + +vector CMsgPackFormat::mimeTypes() const +{ + vector mime; + mime.emplace_back("chemical/x-cmpack"); + return mime; +} + +} // namespace Avogadro::Io diff --git a/avogadro/io/cmsgpackformat.h b/avogadro/io/cmsgpackformat.h new file mode 100644 index 0000000000..ce92f1d672 --- /dev/null +++ b/avogadro/io/cmsgpackformat.h @@ -0,0 +1,58 @@ +/****************************************************************************** + This source file is part of the Avogadro project. + This source code is released under the 3-Clause BSD License, (see "LICENSE"). +******************************************************************************/ + +#ifndef AVOGADRO_IO_CMSGPACKFORMAT_H +#define AVOGADRO_IO_CMSGPACKFORMAT_H + +#include "fileformat.h" +#include "cjsonformat.h" + +namespace Avogadro { +namespace Core { +class GaussianSet; +} +namespace Io { + +/** + * @class CMsgPackFormat cmsgpackformat.h + * @brief Implementation of the Chemical MessagePack format. + */ + +class AVOGADROIO_EXPORT CMsgPackFormat : public CjsonFormat +{ +public: + CMsgPackFormat(); + ~CMsgPackFormat() override; + + Operations supportedOperations() const override + { + return ReadWrite | File | Stream | String; + } + + FileFormat* newInstance() const override { return new CMsgPackFormat; } + std::string identifier() const override { return "Avogadro: CMsgPack"; } + std::string name() const override { return "Chemical MessagePack"; } + std::string description() const override + { + return "CMsgPack format is a lightweight intermediate format used to exchange " + "information between Avogadro and other data parsing applications"; + } + + std::string specificationUrl() const override + { + return "https://github.com/openchemistry/chemicaljson"; + } + + std::vector fileExtensions() const override; + std::vector mimeTypes() const override; + + // write MessagePack + bool m_json = false; +}; + +} // end Io namespace +} // end Avogadro namespace + +#endif // AVOGADRO_IO_CMSGPACKFORMAT_H From a1e27903f9a4f981ec15d360b659bb697e17bbac Mon Sep 17 00:00:00 2001 From: Geoff Hutchison Date: Thu, 31 Aug 2023 15:41:48 -0400 Subject: [PATCH 2/5] Add support for a MessagePack variant to CJSON Signed-off-by: Geoff Hutchison --- avogadro/io/cjsonformat.cpp | 13 +++++++++++-- avogadro/io/cjsonformat.h | 5 +++-- avogadro/io/cmsgpackformat.cpp | 18 +++++++++++++++++- avogadro/io/cmsgpackformat.h | 3 +++ avogadro/io/fileformatmanager.cpp | 2 ++ 5 files changed, 36 insertions(+), 5 deletions(-) diff --git a/avogadro/io/cjsonformat.cpp b/avogadro/io/cjsonformat.cpp index a3a5b07ca6..c5221250e6 100644 --- a/avogadro/io/cjsonformat.cpp +++ b/avogadro/io/cjsonformat.cpp @@ -80,6 +80,11 @@ bool isBooleanArray(json& j) } bool CjsonFormat::read(std::istream& file, Molecule& molecule) +{ + return deserialize(file, molecule, true); +} + +bool CjsonFormat::deserialize(std::istream& file, Molecule& molecule, bool isJson) { json jsonRoot = json::parse(file, nullptr, false); if (jsonRoot.is_discarded()) { @@ -622,6 +627,11 @@ bool CjsonFormat::read(std::istream& file, Molecule& molecule) } bool CjsonFormat::write(std::ostream& file, const Molecule& molecule) +{ + return serialize(file, molecule, true); +} + +bool CjsonFormat::serialize(std::ostream& file, const Molecule& molecule, bool isJson) { json opts; if (!options().empty()) @@ -1042,8 +1052,7 @@ bool CjsonFormat::write(std::ostream& file, const Molecule& molecule) root["layer"]["settings"][settings.first] = setting; } - // Write out the file, use a two space indent to "pretty print". - if (m_json) + if (isJson) file << std::setw(2) << root; else // write msgpack json::to_msgpack(root, file); diff --git a/avogadro/io/cjsonformat.h b/avogadro/io/cjsonformat.h index 556a78204a..e7e62a6528 100644 --- a/avogadro/io/cjsonformat.h +++ b/avogadro/io/cjsonformat.h @@ -50,8 +50,9 @@ class AVOGADROIO_EXPORT CjsonFormat : public FileFormat bool read(std::istream& in, Core::Molecule& molecule) override; bool write(std::ostream& out, const Core::Molecule& molecule) override; - // write JSON or MessagePack - bool m_json = true; + // internal - to allow JSON or MsgPack to be written + bool deserialize(std::istream& in, Core::Molecule& molecule, bool json); + bool serialize(std::ostream& out, const Core::Molecule& molecule, bool json); }; } // end Io namespace diff --git a/avogadro/io/cmsgpackformat.cpp b/avogadro/io/cmsgpackformat.cpp index c4dcb4861f..5474832794 100644 --- a/avogadro/io/cmsgpackformat.cpp +++ b/avogadro/io/cmsgpackformat.cpp @@ -5,11 +5,16 @@ #include "cmsgpackformat.h" +#include + using namespace std; namespace Avogadro::Io { -CMsgPackFormat::CMsgPackFormat() = default; +CMsgPackFormat::CMsgPackFormat(): CjsonFormat() +{ + m_json = false; +} CMsgPackFormat::~CMsgPackFormat() = default; @@ -27,4 +32,15 @@ vector CMsgPackFormat::mimeTypes() const return mime; } + bool CMsgPackFormat::read(std::istream& in, Core::Molecule& molecule) + { + return CjsonFormat::deserialize(in, molecule, false); + } + + bool CMsgPackFormat::write(std::ostream& out, const Core::Molecule& molecule) + { + std::cerr << "CMsgPackFormat::write" << std::endl; + return CjsonFormat::serialize(out, molecule, false); + } + } // namespace Avogadro::Io diff --git a/avogadro/io/cmsgpackformat.h b/avogadro/io/cmsgpackformat.h index ce92f1d672..8a8afca6fd 100644 --- a/avogadro/io/cmsgpackformat.h +++ b/avogadro/io/cmsgpackformat.h @@ -48,6 +48,9 @@ class AVOGADROIO_EXPORT CMsgPackFormat : public CjsonFormat std::vector fileExtensions() const override; std::vector mimeTypes() const override; + bool read(std::istream& in, Core::Molecule& molecule) override; + bool write(std::ostream& out, const Core::Molecule& molecule) override; + // write MessagePack bool m_json = false; }; diff --git a/avogadro/io/fileformatmanager.cpp b/avogadro/io/fileformatmanager.cpp index 1841213ee0..999a9c2c68 100644 --- a/avogadro/io/fileformatmanager.cpp +++ b/avogadro/io/fileformatmanager.cpp @@ -9,6 +9,7 @@ #include "cjsonformat.h" #include "cmlformat.h" +#include "cmsgpackformat.h" #include "dcdformat.h" #include "gromacsformat.h" #include "lammpsformat.h" @@ -284,6 +285,7 @@ FileFormatManager::FileFormatManager() { addFormat(new CmlFormat); addFormat(new CjsonFormat); + addFormat(new CMsgPackFormat); addFormat(new DcdFormat); addFormat(new GromacsFormat); addFormat(new LammpsTrajectoryFormat); From 838c3a9380bab186066f72c533243c864f209405 Mon Sep 17 00:00:00 2001 From: Geoff Hutchison Date: Mon, 13 Nov 2023 14:03:24 -0500 Subject: [PATCH 3/5] Fix everything Signed-off-by: Geoff Hutchison --- avogadro/io/cjsonformat.cpp | 16 ++++++++++++---- avogadro/io/cmsgpackformat.cpp | 1 - 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/avogadro/io/cjsonformat.cpp b/avogadro/io/cjsonformat.cpp index 21d817a768..a312e175b3 100644 --- a/avogadro/io/cjsonformat.cpp +++ b/avogadro/io/cjsonformat.cpp @@ -87,7 +87,12 @@ bool CjsonFormat::read(std::istream& file, Molecule& molecule) bool CjsonFormat::deserialize(std::istream& file, Molecule& molecule, bool isJson) { - json jsonRoot = json::parse(file, nullptr, false); + json jsonRoot; + if (isJson) + jsonRoot = json::parse(file, nullptr, false); + else // msgpack + jsonRoot = json::from_msgpack(file); + if (jsonRoot.is_discarded()) { appendError("Error reading CJSON file."); return false; @@ -1025,7 +1030,8 @@ bool CjsonFormat::serialize(std::ostream& file, const Molecule& molecule, bool i modes.push_back(static_cast(i) + 1); freqs.push_back(molecule.vibrationFrequencies()[i]); inten.push_back(molecule.vibrationIRIntensities()[i]); - raman.push_back(molecule.vibrationRamanIntensities()[i]); + if (molecule.vibrationRamanIntensities().size() > i) + raman.push_back(molecule.vibrationRamanIntensities()[i]); Core::Array atomDisplacements = molecule.vibrationLx(i); json eigenVector; for (auto pos : atomDisplacements) { @@ -1038,7 +1044,8 @@ bool CjsonFormat::serialize(std::ostream& file, const Molecule& molecule, bool i root["vibrations"]["modes"] = modes; root["vibrations"]["frequencies"] = freqs; root["vibrations"]["intensities"] = inten; - root["vibrations"]["ramanIntensities"] = raman; + if (molecule.vibrationRamanIntensities().size() > 0) + root["vibrations"]["ramanIntensities"] = raman; root["vibrations"]["eigenVectors"] = eigenVectors; } @@ -1071,8 +1078,9 @@ bool CjsonFormat::serialize(std::ostream& file, const Molecule& molecule, bool i if (isJson) file << std::setw(2) << root; - else // write msgpack + else { // write msgpack json::to_msgpack(root, file); + } return true; } diff --git a/avogadro/io/cmsgpackformat.cpp b/avogadro/io/cmsgpackformat.cpp index 5474832794..28f45aa5df 100644 --- a/avogadro/io/cmsgpackformat.cpp +++ b/avogadro/io/cmsgpackformat.cpp @@ -39,7 +39,6 @@ vector CMsgPackFormat::mimeTypes() const bool CMsgPackFormat::write(std::ostream& out, const Core::Molecule& molecule) { - std::cerr << "CMsgPackFormat::write" << std::endl; return CjsonFormat::serialize(out, molecule, false); } From bbd5162ca66891a7d5a9719771ae2538f677293d Mon Sep 17 00:00:00 2001 From: Geoff Hutchison Date: Mon, 13 Nov 2023 14:14:55 -0500 Subject: [PATCH 4/5] Fix formatting Signed-off-by: Geoff Hutchison --- avogadro/io/cjsonformat.cpp | 8 +++++--- avogadro/io/cjsonformat.h | 4 ++-- avogadro/io/cmsgpackformat.cpp | 18 +++++++++--------- avogadro/io/cmsgpackformat.h | 9 +++++---- 4 files changed, 21 insertions(+), 18 deletions(-) diff --git a/avogadro/io/cjsonformat.cpp b/avogadro/io/cjsonformat.cpp index a312e175b3..aa73d5e6cb 100644 --- a/avogadro/io/cjsonformat.cpp +++ b/avogadro/io/cjsonformat.cpp @@ -85,14 +85,15 @@ bool CjsonFormat::read(std::istream& file, Molecule& molecule) return deserialize(file, molecule, true); } -bool CjsonFormat::deserialize(std::istream& file, Molecule& molecule, bool isJson) +bool CjsonFormat::deserialize(std::istream& file, Molecule& molecule, + bool isJson) { json jsonRoot; if (isJson) jsonRoot = json::parse(file, nullptr, false); else // msgpack jsonRoot = json::from_msgpack(file); - + if (jsonRoot.is_discarded()) { appendError("Error reading CJSON file."); return false; @@ -648,7 +649,8 @@ bool CjsonFormat::write(std::ostream& file, const Molecule& molecule) return serialize(file, molecule, true); } -bool CjsonFormat::serialize(std::ostream& file, const Molecule& molecule, bool isJson) +bool CjsonFormat::serialize(std::ostream& file, const Molecule& molecule, + bool isJson) { json opts; if (!options().empty()) diff --git a/avogadro/io/cjsonformat.h b/avogadro/io/cjsonformat.h index e7e62a6528..092f0c23ca 100644 --- a/avogadro/io/cjsonformat.h +++ b/avogadro/io/cjsonformat.h @@ -55,7 +55,7 @@ class AVOGADROIO_EXPORT CjsonFormat : public FileFormat bool serialize(std::ostream& out, const Core::Molecule& molecule, bool json); }; -} // end Io namespace -} // end Avogadro namespace +} // namespace Io +} // namespace Avogadro #endif // AVOGADRO_IO_CJSONFORMAT_H diff --git a/avogadro/io/cmsgpackformat.cpp b/avogadro/io/cmsgpackformat.cpp index 28f45aa5df..2cf641fa14 100644 --- a/avogadro/io/cmsgpackformat.cpp +++ b/avogadro/io/cmsgpackformat.cpp @@ -11,7 +11,7 @@ using namespace std; namespace Avogadro::Io { -CMsgPackFormat::CMsgPackFormat(): CjsonFormat() +CMsgPackFormat::CMsgPackFormat() : CjsonFormat() { m_json = false; } @@ -32,14 +32,14 @@ vector CMsgPackFormat::mimeTypes() const return mime; } - bool CMsgPackFormat::read(std::istream& in, Core::Molecule& molecule) - { - return CjsonFormat::deserialize(in, molecule, false); - } +bool CMsgPackFormat::read(std::istream& in, Core::Molecule& molecule) +{ + return CjsonFormat::deserialize(in, molecule, false); +} - bool CMsgPackFormat::write(std::ostream& out, const Core::Molecule& molecule) - { - return CjsonFormat::serialize(out, molecule, false); - } +bool CMsgPackFormat::write(std::ostream& out, const Core::Molecule& molecule) +{ + return CjsonFormat::serialize(out, molecule, false); +} } // namespace Avogadro::Io diff --git a/avogadro/io/cmsgpackformat.h b/avogadro/io/cmsgpackformat.h index 8a8afca6fd..7147e82543 100644 --- a/avogadro/io/cmsgpackformat.h +++ b/avogadro/io/cmsgpackformat.h @@ -6,8 +6,8 @@ #ifndef AVOGADRO_IO_CMSGPACKFORMAT_H #define AVOGADRO_IO_CMSGPACKFORMAT_H -#include "fileformat.h" #include "cjsonformat.h" +#include "fileformat.h" namespace Avogadro { namespace Core { @@ -36,7 +36,8 @@ class AVOGADROIO_EXPORT CMsgPackFormat : public CjsonFormat std::string name() const override { return "Chemical MessagePack"; } std::string description() const override { - return "CMsgPack format is a lightweight intermediate format used to exchange " + return "CMsgPack format is a lightweight intermediate format used to " + "exchange " "information between Avogadro and other data parsing applications"; } @@ -55,7 +56,7 @@ class AVOGADROIO_EXPORT CMsgPackFormat : public CjsonFormat bool m_json = false; }; -} // end Io namespace -} // end Avogadro namespace +} // namespace Io +} // namespace Avogadro #endif // AVOGADRO_IO_CMSGPACKFORMAT_H From 606254ff03b475b57d901f48c1c6bd793572c85f Mon Sep 17 00:00:00 2001 From: Geoff Hutchison Date: Mon, 13 Nov 2023 14:16:37 -0500 Subject: [PATCH 5/5] Fix formatting Signed-off-by: Geoff Hutchison --- avogadro/io/fileformatmanager.cpp | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/avogadro/io/fileformatmanager.cpp b/avogadro/io/fileformatmanager.cpp index 999a9c2c68..ec19127dca 100644 --- a/avogadro/io/fileformatmanager.cpp +++ b/avogadro/io/fileformatmanager.cpp @@ -135,7 +135,7 @@ bool FileFormatManager::addFormat(FileFormat* format) appendError("Format " + format->identifier() + " already loaded."); return false; } - for (auto & m_format : m_formats) { + for (auto& m_format : m_formats) { if (m_format == format) { appendError("The format object was already loaded."); return false; @@ -147,11 +147,11 @@ bool FileFormatManager::addFormat(FileFormat* format) m_formats.push_back(format); m_identifiers[format->identifier()].push_back(index); std::vector mimes = format->mimeTypes(); - for (auto & mime : mimes) { + for (auto& mime : mimes) { m_mimeTypes[mime].push_back(index); } std::vector extensions = format->fileExtensions(); - for (auto & extension : extensions) { + for (auto& extension : extensions) { m_fileExtensions[extension].push_back(index); } @@ -161,7 +161,7 @@ bool FileFormatManager::addFormat(FileFormat* format) namespace { // Lookup each key from "keys" in "map", and remove "val" from the Map's // data value (which is a vector of ValueType) -template +template void removeFromMap(Map& map, const VectorOfKeys& keys, const ValueType& val) { for (auto key = keys.begin(), keyEnd = keys.end(); key != keyEnd; ++key) { @@ -172,13 +172,12 @@ void removeFromMap(Map& map, const VectorOfKeys& keys, const ValueType& val) if (vec.size() <= 1) { map.erase(*key); } else { - auto newEnd = - std::remove(vec.begin(), vec.end(), val); + auto newEnd = std::remove(vec.begin(), vec.end(), val); vec.resize(newEnd - vec.begin()); } } } -} +} // namespace bool FileFormatManager::removeFormat(const std::string& identifier) { @@ -305,7 +304,7 @@ FileFormatManager::FileFormatManager() FileFormatManager::~FileFormatManager() { // Delete the file formats that were loaded. - for (auto & m_format : m_formats) { + for (auto& m_format : m_formats) { delete m_format; } m_formats.clear(); @@ -316,9 +315,9 @@ std::vector FileFormatManager::filteredKeysFromFormatMap( const FileFormatManager::FormatIdMap& fmap) const { std::vector result; - for (const auto & it : fmap) { - for (auto formatIt = it.second.begin(); - formatIt != it.second.end(); ++formatIt) { + for (const auto& it : fmap) { + for (auto formatIt = it.second.begin(); formatIt != it.second.end(); + ++formatIt) { if (filter == FileFormat::None || (m_formats[*formatIt]->supportedOperations() & filter) == filter) { result.push_back(it.first); @@ -385,4 +384,4 @@ void FileFormatManager::appendError(const std::string& errorMessage) m_error += errorMessage + "\n"; } -} // end Avogadro namespace +} // namespace Avogadro::Io