Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhanced molecular properties dialog #1754

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 2 additions & 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 Expand Up @@ -293,6 +294,7 @@ FileFormatManager::FileFormatManager()
addFormat(new OutcarFormat);
addFormat(new PdbFormat);
addFormat(new PoscarFormat);
addFormat(new SdfFormat);
addFormat(new TrrFormat);
addFormat(new TurbomoleFormat);
addFormat(new XyzFormat);
Expand Down
42 changes: 37 additions & 5 deletions avogadro/io/mdlformat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,25 @@ MdlFormat::MdlFormat() {}

MdlFormat::~MdlFormat() {}

void handlePartialCharges(Core::Molecule& mol, std::string data)
{
// the string starts with the number of charges
// then atom index charge
MatrixX charges(mol.atomCount(), 1);
std::istringstream iss(data);
size_t numCharges;
iss >> numCharges;
for (size_t i = 0; i < numCharges; ++i) {
size_t index;
Real charge;
iss >> index >> charge;
// prints with atom index 1, not zero
charges(index - 1, 0) = charge;
}

mol.setPartialCharges("MMFF94", charges);
}

bool MdlFormat::read(std::istream& in, Core::Molecule& mol)
{
string buffer;
Expand Down Expand Up @@ -182,7 +201,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 All @@ -208,7 +227,11 @@ bool MdlFormat::read(std::istream& in, Core::Molecule& mol)
return true;
if (inValue) {
if (buffer.empty() && dataName.length() > 0) {
mol.setData(dataName, dataValue);
// check for partial charges
if (dataName == "PUBCHEM_MMFF94_PARTIAL_CHARGES")
handlePartialCharges(mol, dataValue);
else
mol.setData(dataName, dataValue);
dataName.clear();
dataValue.clear();
inValue = false;
Expand Down Expand Up @@ -260,13 +283,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 +311,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 +321,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)

Check notice on line 17 in avogadro/io/sdfformat.cpp

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

avogadro/io/sdfformat.cpp#L17

Check buffer boundaries if used in a loop including recursive loops (CWE-120, CWE-20).
{
return MdlFormat::read(in, mol);

Check notice on line 19 in avogadro/io/sdfformat.cpp

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

avogadro/io/sdfformat.cpp#L19

Check buffer boundaries if used in a loop including recursive loops (CWE-120, CWE-20).
}

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
64 changes: 64 additions & 0 deletions avogadro/io/sdfformat.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/******************************************************************************
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;

Check notice on line 57 in avogadro/io/sdfformat.h

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

avogadro/io/sdfformat.h#L57

Check buffer boundaries if used in a loop including recursive loops (CWE-120, CWE-20).
bool write(std::ostream& out, const Core::Molecule& molecule) override;
};

} // namespace Io
} // namespace Avogadro

#endif // AVOGADRO_IO_MDLFORMAT_H
35 changes: 4 additions & 31 deletions avogadro/qtgui/richtextdelegate.cpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
/******************************************************************************

This source file is part of the Avogadro project.

Copyright 2015 Marcus Johansson <[email protected]>

This source code is released under the New BSD License, (the "License").

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

This source code is released under the 3-Clause BSD License, (see "LICENSE").
******************************************************************************/

#include "richtextdelegate.h"

#include <cmath>

namespace Avogadro::QtGui {

// See for example
Expand Down Expand Up @@ -44,39 +35,21 @@ QSize RichTextDelegate::sizeHint(const QStyleOptionViewItem& o,
doc.setHtml(ov.text);
doc.setTextWidth(ov.rect.width());
doc.setDefaultFont(ov.font);
doc.setDocumentMargin(1);

return QSize(doc.idealWidth(), doc.size().height());
return QSize(std::ceil(doc.idealWidth()), std::ceil(doc.size().height()));
}

void RichTextDelegate::paint(QPainter* p, const QStyleOptionViewItem& o,
const QModelIndex& index) const
{
if (o.text.isEmpty()) {
// no need to do anything if the text is empty
QStyledItemDelegate::paint(p, o, index);

return;
}

QStyleOptionViewItem ov = o;
initStyleOption(&ov, index);

p->save();

QTextDocument doc;
doc.setHtml(ov.text);

QTextOption textOption;
textOption.setWrapMode(ov.features & QStyleOptionViewItem::WrapText
? QTextOption::WordWrap
: QTextOption::ManualWrap);
textOption.setTextDirection(ov.direction);
doc.setDefaultTextOption(textOption);
doc.setDefaultFont(ov.font);
doc.setDocumentMargin(1);
doc.setTextWidth(ov.rect.width());
doc.adjustSize();

ov.text = "";
ov.widget->style()->drawControl(QStyle::CE_ItemViewItem, &ov, p);
Expand Down
13 changes: 1 addition & 12 deletions avogadro/qtgui/richtextdelegate.h
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
/******************************************************************************

This source file is part of the Avogadro project.

Copyright 2015 Marcus Johansson <[email protected]>

This source code is released under the New BSD License, (the "License").

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

This source code is released under the 3-Clause BSD License, (see "LICENSE").
******************************************************************************/

#ifndef AVOGADRO_QTPLUGINS_RICHTEXTDELEGATE_H
Expand Down
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
Loading
Loading