Skip to content

Commit

Permalink
Add an explicit sdf format which writes properties
Browse files Browse the repository at this point in the history
Signed-off-by: Geoff Hutchison <[email protected]>
  • Loading branch information
ghutchis committed Oct 28, 2024
1 parent 75abcf4 commit b3681cf
Show file tree
Hide file tree
Showing 12 changed files with 164 additions and 21 deletions.
14 changes: 8 additions & 6 deletions avogadro/io/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ avogadro_headers(IO
fileformatmanager.h
gromacsformat.h
mdlformat.h
vaspformat.h
lammpsformat.h
pdbformat.h
xyzformat.h
sdfformat.h
trrformat.h
turbomoleformat.h
lammpsformat.h
vaspformat.h
xyzformat.h
)

target_sources(IO PRIVATE
Expand All @@ -25,13 +26,14 @@ target_sources(IO PRIVATE
fileformat.cpp
fileformatmanager.cpp
gromacsformat.cpp
lammpsformat.cpp
mdlformat.cpp
vaspformat.cpp
pdbformat.cpp
xyzformat.cpp
sdfformat.cpp
trrformat.cpp
turbomoleformat.cpp
lammpsformat.cpp
vaspformat.cpp
xyzformat.cpp
)

if(USE_HDF5)
Expand Down
1 change: 1 addition & 0 deletions avogadro/io/fileformatmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "lammpsformat.h"
#include "mdlformat.h"
#include "pdbformat.h"
#include "sdfformat.h"
#include "trrformat.h"
#include "turbomoleformat.h"
#include "vaspformat.h"
Expand Down
17 changes: 13 additions & 4 deletions avogadro/io/mdlformat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ bool MdlFormat::read(std::istream& in, Core::Molecule& mol)
}

// Apply charges.
for (auto & i : chargeList) {
for (auto& i : chargeList) {
size_t index = i.first;
signed int charge = i.second;
mol.setFormalCharge(index, charge);
Expand Down Expand Up @@ -260,13 +260,23 @@ bool MdlFormat::write(std::ostream& out, const Core::Molecule& mol)
<< " 0 0 0 0\n";
}
// Properties block.
for (auto & i : chargeList) {
for (auto& i : chargeList) {
Index atomIndex = i.first;
signed int atomCharge = i.second;
out << "M CHG 1 " << setw(3) << std::right << atomIndex + 1 << " "
<< setw(3) << atomCharge << "\n";
}
// TODO: isotopes, etc.
out << "M END\n";
// Data block
if (m_writeProperties) {
const auto dataMap = mol.dataMap();
for (const auto& key : dataMap.names()) {
out << "> <" << key << ">\n";
out << dataMap.value(key).toString() << "\n";
out << "\n"; // empty line between data blocks
}
}

if (isMode(FileFormat::MultiMolecule))
out << "$$$$\n";
Expand All @@ -278,7 +288,6 @@ std::vector<std::string> MdlFormat::fileExtensions() const
{
std::vector<std::string> ext;
ext.emplace_back("mol");
ext.emplace_back("sdf");
return ext;
}

Expand All @@ -289,4 +298,4 @@ std::vector<std::string> MdlFormat::mimeTypes() const
return mime;
}

} // namespace Avogadro
} // namespace Avogadro::Io
10 changes: 7 additions & 3 deletions avogadro/io/mdlformat.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ class AVOGADROIO_EXPORT MdlFormat : public FileFormat

std::string specificationUrl() const override
{
return "http://help.accelrysonline.com/ulm/onelab/1.0/content/ulm_pdfs/direct/"
return "http://help.accelrysonline.com/ulm/onelab/1.0/content/ulm_pdfs/"
"direct/"
"reference/ctfileformats2016.pdf";
/* for previous (2011) version, see:
https://web.archive.org/web/20180329184712/http://download.accelrys.com/freeware/ctfile-formats/ctfile-formats.zip
Expand All @@ -52,9 +53,12 @@ class AVOGADROIO_EXPORT MdlFormat : public FileFormat

bool read(std::istream& in, Core::Molecule& molecule) override;
bool write(std::ostream& out, const Core::Molecule& molecule) override;

protected:
bool m_writeProperties = false;
};

} // end Io namespace
} // end Avogadro namespace
} // namespace Io
} // namespace Avogadro

#endif // AVOGADRO_IO_MDLFORMAT_H
41 changes: 41 additions & 0 deletions avogadro/io/sdfformat.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/******************************************************************************
This source file is part of the Avogadro project.
This source code is released under the 3-Clause BSD License, (see "LICENSE").
******************************************************************************/

#include "sdfformat.h"

namespace Avogadro::Io {

SdfFormat::SdfFormat() : MdlFormat()
{
m_writeProperties = true;
}

SdfFormat::~SdfFormat() {}

bool SdfFormat::read(std::istream& in, Core::Molecule& mol)
{
return MdlFormat::read(in, mol);
}

bool SdfFormat::write(std::ostream& out, const Core::Molecule& mol)
{
return MdlFormat::write(out, mol);
}

std::vector<std::string> SdfFormat::fileExtensions() const
{
std::vector<std::string> ext;
ext.emplace_back("sdf");
return ext;
}

std::vector<std::string> SdfFormat::mimeTypes() const
{
std::vector<std::string> mime;
mime.emplace_back("chemical/x-mdl-molfile");
return mime;
}

} // namespace Avogadro::Io
67 changes: 67 additions & 0 deletions avogadro/io/sdfformat.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/******************************************************************************
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_SDFFORMAT_H
#define AVOGADRO_IO_SDFFORMAT_H

#include "fileformat.h"
#include "mdlformat.h"

namespace Avogadro {
namespace Io {

/**
* @class SdfFormat sdfformat.h <avogadro/io/sdfformat.h>
* @brief Implementation of the generic SDF format.
* @author Marcus D. Hanwell
*
* Differs from the MDL / Mol format in that it includes properties
*
* Currently just supports V2000 of the format.
*/

class AVOGADROIO_EXPORT SdfFormat : public MdlFormat
{
public:
SdfFormat();
~SdfFormat() override;

Operations supportedOperations() const override
{
return ReadWrite | MultiMolecule | File | Stream | String;
}

FileFormat* newInstance() const override { return new SdfFormat; }
std::string identifier() const override { return "Avogadro: SDF"; }
std::string name() const override { return "SDF"; }
std::string description() const override
{
return "Generic format that contains atoms, bonds, positions.";
}

std::string specificationUrl() const override
{
return "http://help.accelrysonline.com/ulm/onelab/1.0/content/ulm_pdfs/"
"direct/"
"reference/ctfileformats2016.pdf";
/* for previous (2011) version, see:
https://web.archive.org/web/20180329184712/http://download.accelrys.com/freeware/ctfile-formats/ctfile-formats.zip
*/
}

std::vector<std::string> fileExtensions() const override;
std::vector<std::string> mimeTypes() const override;

bool read(std::istream& in, Core::Molecule& molecule) override;
bool write(std::ostream& out, const Core::Molecule& molecule) override;

protected:
bool m_writeProperties = true;
};

} // namespace Io
} // namespace Avogadro

#endif // AVOGADRO_IO_MDLFORMAT_H
7 changes: 6 additions & 1 deletion avogadro/qtplugins/forcefield/scriptenergy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <avogadro/io/cmlformat.h>
#include <avogadro/io/mdlformat.h>
#include <avogadro/io/pdbformat.h>
#include <avogadro/io/sdfformat.h>
#include <avogadro/io/xyzformat.h>

#include <QtCore/QDebug>
Expand Down Expand Up @@ -191,10 +192,12 @@ ScriptEnergy::Format ScriptEnergy::stringToFormat(const std::string& str)
return Cjson;
else if (str == "cml")
return Cml;
else if (str == "mdl" || str == "mol" || str == "sdf" || str == "sd")
else if (str == "mdl" || str == "mol")
return Mdl;
else if (str == "pdb")
return Pdb;
else if (str == "sdf")
return Sdf;
else if (str == "xyz")
return Xyz;
return NotUsed;
Expand All @@ -211,6 +214,8 @@ Io::FileFormat* ScriptEnergy::createFileFormat(ScriptEnergy::Format fmt)
return new Io::MdlFormat;
case Pdb:
return new Io::PdbFormat;
case Sdf:
return new Io::SdfFormat;
case Xyz:
return new Io::XyzFormat;
default:
Expand Down
3 changes: 2 additions & 1 deletion avogadro/qtplugins/forcefield/scriptenergy.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ class ScriptEnergy : public Avogadro::Calc::EnergyCalculator
NotUsed,
Cjson,
Cml,
Mdl, // sdf
Mdl,
Pdb,
Sdf,
Xyz
};

Expand Down
7 changes: 6 additions & 1 deletion avogadro/qtplugins/scriptcharges/scriptchargemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <avogadro/io/cmlformat.h>
#include <avogadro/io/mdlformat.h>
#include <avogadro/io/pdbformat.h>
#include <avogadro/io/sdfformat.h>
#include <avogadro/io/xyzformat.h>

#include <QtCore/QDebug>
Expand Down Expand Up @@ -199,8 +200,10 @@ ScriptChargeModel::Format ScriptChargeModel::stringToFormat(
return Cjson;
else if (str == "cml")
return Cml;
else if (str == "mdl" || str == "mol" || str == "sdf" || str == "sd")
else if (str == "mdl" || str == "mol")
return Mdl;
else if (str == "sdf")
return Sdf;
else if (str == "pdb")
return Pdb;
else if (str == "xyz")
Expand All @@ -220,6 +223,8 @@ Io::FileFormat* ScriptChargeModel::createFileFormat(
return new Io::MdlFormat;
case Pdb:
return new Io::PdbFormat;
case Sdf:
return new Io::SdfFormat;
case Xyz:
return new Io::XyzFormat;
default:
Expand Down
3 changes: 2 additions & 1 deletion avogadro/qtplugins/scriptcharges/scriptchargemodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ class ScriptChargeModel : public Avogadro::Calc::ChargeModel
NotUsed,
Cjson,
Cml,
Mdl, // sdf
Mdl,
Pdb,
Sdf,
Xyz
};

Expand Down
11 changes: 8 additions & 3 deletions avogadro/qtplugins/scriptfileformats/fileformatscript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <avogadro/io/cmlformat.h>
#include <avogadro/io/mdlformat.h>
#include <avogadro/io/pdbformat.h>
#include <avogadro/io/sdfformat.h>
#include <avogadro/io/xyzformat.h>

#include <QtCore/QDebug>
Expand Down Expand Up @@ -130,10 +131,12 @@ FileFormatScript::Format FileFormatScript::stringToFormat(
return Cjson;
else if (str == "cml")
return Cml;
else if (str == "mdl" || str == "mol" || str == "sdf" || str == "sd")
else if (str == "mdl" || str == "mol")
return Mdl;
else if (str == "pdb")
return Pdb;
else if (str == "sdf")
return Sdf;
else if (str == "xyz")
return Xyz;
return NotUsed;
Expand All @@ -150,6 +153,8 @@ Io::FileFormat* FileFormatScript::createFileFormat(FileFormatScript::Format fmt)
return new Io::MdlFormat;
case Pdb:
return new Io::PdbFormat;
case Sdf:
return new Io::SdfFormat;
case Xyz:
return new Io::XyzFormat;
default:
Expand Down Expand Up @@ -217,7 +222,7 @@ void FileFormatScript::readMetaData()

// validate operations:
Operations operationsTmp = Io::FileFormat::None;
for (auto & it : opStringsTmp) {
for (auto& it : opStringsTmp) {
if (it == "read")
operationsTmp |= Io::FileFormat::Read;
else if (it == "write")
Expand Down Expand Up @@ -357,4 +362,4 @@ bool FileFormatScript::parseStringArray(const QJsonObject& ob,
return !array.empty();
}

} // namespace Avogadro
} // namespace Avogadro::QtPlugins
4 changes: 3 additions & 1 deletion avogadro/qtplugins/scriptfileformats/fileformatscript.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ namespace QtPlugins {
* `"cjson"`, or `"xyz"`. See the `--write` documentation for more detail.
* - `outputFormat` indicates the format that the script can convert to from the
* implemented format by the `--read` command. Allowed values are `"cml"`,
* `"cjson"`, `"sdf"`, `"pdb"` or `"xyz"`. See the `--read` documentation for more detail.
* `"cjson"`, `"sdf"`, `"pdb"` or `"xyz"`. See the `--read` documentation for
more detail.
* - `operations` specifies the scripts capabilities. The array should contain
* `"read"` if the script implements the `--read` option, and/or `"write"` if
* `--write` is available.
Expand Down Expand Up @@ -120,6 +121,7 @@ class FileFormatScript : public Avogadro::Io::FileFormat
Cml,
Mdl,
Pdb,
Sdf,
Xyz
};

Expand Down

0 comments on commit b3681cf

Please sign in to comment.