diff options
author | Brian Helfrich <helfrich9000@gmail.com> | 2008-01-31 15:24:33 +0000 |
---|---|---|
committer | Brian Helfrich <helfrich9000@gmail.com> | 2008-01-31 15:24:33 +0000 |
commit | cae4d0e62d02e24e1ea373b3ca8fb7a84bded765 (patch) | |
tree | 927be05117aada9d98d72bb8ee6c857705b4f3f7 | |
parent | fb2bbd612f1a17c0fa3e8bd3ffb865eb70629c9e (diff) | |
download | nanoengineer-theirix-cae4d0e62d02e24e1ea373b3ca8fb7a84bded765.tar.gz nanoengineer-theirix-cae4d0e62d02e24e1ea373b3ca8fb7a84bded765.zip |
Initial revision of OpenBabelImportExport plugin.
11 files changed, 346 insertions, 10 deletions
diff --git a/cad/plugins/NanoVision-1/include/Nanorex/Interface/NXDataStoreInfo.h b/cad/plugins/NanoVision-1/include/Nanorex/Interface/NXDataStoreInfo.h index 326735adf..d0ee1639b 100644 --- a/cad/plugins/NanoVision-1/include/Nanorex/Interface/NXDataStoreInfo.h +++ b/cad/plugins/NanoVision-1/include/Nanorex/Interface/NXDataStoreInfo.h @@ -23,6 +23,12 @@ class NXDataStoreInfo { const string& getFilename() { return _filename; } void setFilename(const string& filename) { _filename = filename; } + // TODO: rename "id" in the following to "frameSetId" + + void addInputStructure(string name, int id) { + _inputStructures[name] = id; + } + void addTrajectory(string name, int id) { _trajectories[name] = id; } int getTrajectoryId(string name) { return _trajectories[name]; } //vector<string> getTrajectoryNames(); @@ -43,6 +49,9 @@ class NXDataStoreInfo { private: string _filename; + // Maps input structure name to its frame set id. + map<string, int> _inputStructures; + // Maps trajectory name to its frame set id. map<string, int> _trajectories; diff --git a/cad/plugins/NanoVision-1/include/Nanorex/Interface/NXEntityManager.h b/cad/plugins/NanoVision-1/include/Nanorex/Interface/NXEntityManager.h index a4056c925..4e1003af9 100644 --- a/cad/plugins/NanoVision-1/include/Nanorex/Interface/NXEntityManager.h +++ b/cad/plugins/NanoVision-1/include/Nanorex/Interface/NXEntityManager.h @@ -51,7 +51,10 @@ class NXEntityManager { int addFrameSet() { vector<NXMoleculeSet*> moleculeSetVector; moleculeSets.push_back(moleculeSetVector); - return moleculeSets.size() - 1; + int frameSetId = moleculeSets.size() - 1; + dataStoreInfo->setLastFrame(frameSetId, true); + dataStoreInfo->setStoreComplete(frameSetId, true); + return frameSetId; } int addFrame(int frameSetId) { NXMoleculeSet* moleculeSet = new NXMoleculeSet(); diff --git a/cad/plugins/NanoVision-1/src/Interface/NXEntityManager.cpp b/cad/plugins/NanoVision-1/src/Interface/NXEntityManager.cpp index 50be0737b..03e072c58 100644 --- a/cad/plugins/NanoVision-1/src/Interface/NXEntityManager.cpp +++ b/cad/plugins/NanoVision-1/src/Interface/NXEntityManager.cpp @@ -122,7 +122,6 @@ void NXEntityManager::loadDataImportExportPlugins(NXProperties* properties) { NXCommandResult* NXEntityManager::importFromFile(const string& filename, int frameSetId, int frameIndex) { - NXCommandResult* result; //PR_Lock(importExportPluginsMutex); diff --git a/cad/plugins/NanoVision-1/src/KDevelop/Plugins/Plugins.pro b/cad/plugins/NanoVision-1/src/KDevelop/Plugins/Plugins.pro index c81ba9c51..458c68d09 100644 --- a/cad/plugins/NanoVision-1/src/KDevelop/Plugins/Plugins.pro +++ b/cad/plugins/NanoVision-1/src/KDevelop/Plugins/Plugins.pro @@ -2,7 +2,8 @@ TEMPLATE = subdirs SUBDIRS += HDF5_SimResultsImportExport \ - RenderingEngines + RenderingEngines \ + OpenBabelImportExport CONFIG += ordered diff --git a/cad/plugins/NanoVision-1/src/KDevelop/Testing/CppUnit/CppUnit.pro b/cad/plugins/NanoVision-1/src/KDevelop/Testing/CppUnit/CppUnit.pro index 92471ef55..ee46d9e48 100644 --- a/cad/plugins/NanoVision-1/src/KDevelop/Testing/CppUnit/CppUnit.pro +++ b/cad/plugins/NanoVision-1/src/KDevelop/Testing/CppUnit/CppUnit.pro @@ -5,7 +5,8 @@ SOURCES += ../../../Testing/CppUnit/CppUnit.cpp \ ../../../Utility/NXLoggerTest.cpp \ ../../../Utility/NXStringTokenizerTest.cpp \ ../../../Utility/NXUtilityTest.cpp \ - ../../../Plugins/HDF5_SimResultsImportExport/HDF5_SimResultsImportExportTest.cpp + ../../../Plugins/HDF5_SimResultsImportExport/HDF5_SimResultsImportExportTest.cpp \ + ../../../Plugins/OpenBabelImportExport/OpenBabelImportExportTest.cpp TEMPLATE = app @@ -27,7 +28,8 @@ HEADERS += ../../../Utility/NXCommandResultTest.h \ ../../../Utility/NXUtilityTest.h \ ../../../Interface/NXEntityManagerTest.h \ ../../../Interface/NXNumbersTest.h \ -../../../Plugins/HDF5_SimResultsImportExport/HDF5_SimResultsImportExportTest.h +../../../Plugins/HDF5_SimResultsImportExport/HDF5_SimResultsImportExportTest.h \ + ../../../Plugins/OpenBabelImportExport/OpenBabelImportExportTest.h TARGETDEPS += ../../../../lib/libNanorexUtility.so \ ../../../../lib/libNanorexInterface.so \ diff --git a/cad/plugins/NanoVision-1/src/Plugins/HDF5_SimResultsImportExport/HDF5_SimResultsImportExportTest.cpp b/cad/plugins/NanoVision-1/src/Plugins/HDF5_SimResultsImportExport/HDF5_SimResultsImportExportTest.cpp index 26de2e64a..3e6e94f2f 100644 --- a/cad/plugins/NanoVision-1/src/Plugins/HDF5_SimResultsImportExport/HDF5_SimResultsImportExportTest.cpp +++ b/cad/plugins/NanoVision-1/src/Plugins/HDF5_SimResultsImportExport/HDF5_SimResultsImportExportTest.cpp @@ -14,8 +14,12 @@ void HDF5_SimResultsImportExportTest::setUp() { entityManager = new NXEntityManager(); NXProperties* properties = new NXProperties(); - properties->setProperty("NXEntityManager.importExport.0.plugin", + properties->setProperty("ImportExport.0.plugin", "libHDF5_SimResultsImportExport"); + properties->setProperty("ImportExport.0.exportFormats", + "HDF5 Simulation Results (*.h5 *.nh5)"); + properties->setProperty("ImportExport.0.importFormats", + "HDF5 Simulation Results (*.h5 *.nh5)"); entityManager->loadDataImportExportPlugins(properties); delete properties; } @@ -74,12 +78,10 @@ void HDF5_SimResultsImportExportTest::basicExportTest() { bond = molecule->NewBond(); bond->SetBegin(atomO); bond->SetEnd(atomH2); -printf("\n> 3\n"); // Write it with the HDF5_SimResultsImportExport plugin NXCommandResult* commandResult = entityManager->exportToFile("testHDF5store.nh5", frameSetId, -1); -printf("> 4\n"); if (commandResult->getResult() != NX_CMD_SUCCESS) printf("\n%s\n", qPrintable(GetNV1ResultCodeString(commandResult))); CPPUNIT_ASSERT(commandResult->getResult() == NX_CMD_SUCCESS); diff --git a/cad/plugins/NanoVision-1/src/Plugins/HDF5_SimResultsImportExport/HDF5_SimResultsImportExportTest.h b/cad/plugins/NanoVision-1/src/Plugins/HDF5_SimResultsImportExport/HDF5_SimResultsImportExportTest.h index ba901eba0..7b507ee8a 100644 --- a/cad/plugins/NanoVision-1/src/Plugins/HDF5_SimResultsImportExport/HDF5_SimResultsImportExportTest.h +++ b/cad/plugins/NanoVision-1/src/Plugins/HDF5_SimResultsImportExport/HDF5_SimResultsImportExportTest.h @@ -1,7 +1,7 @@ // Copyright 2008 Nanorex, Inc. See LICENSE file for details. -#ifndef HDF5_SIMRESULTSIMPORTEXPORT_H -#define HDF5_SIMRESULTSIMPORTEXPORT_H +#ifndef HDF5_SIMRESULTSIMPORTEXPORTTEST_H +#define HDF5_SIMRESULTSIMPORTEXPORTTEST_H #include <cppunit/extensions/HelperMacros.h> diff --git a/cad/plugins/NanoVision-1/src/Plugins/OpenBabelImportExport/OpenBabelImportExport.cpp b/cad/plugins/NanoVision-1/src/Plugins/OpenBabelImportExport/OpenBabelImportExport.cpp new file mode 100644 index 000000000..b77b4b186 --- /dev/null +++ b/cad/plugins/NanoVision-1/src/Plugins/OpenBabelImportExport/OpenBabelImportExport.cpp @@ -0,0 +1,128 @@ +// Copyright 2008 Nanorex, Inc. See LICENSE file for details. + +#include "OpenBabelImportExport.h" + + +/* FUNCTION: instantiate */ +DLLEXPORT NXPlugin* instantiate() { + return new OpenBabelImportExport(); +} + + + +/* CONSTRUCTOR */ +OpenBabelImportExport::OpenBabelImportExport() { +} + + +/* DESTRUCTOR */ +OpenBabelImportExport::~OpenBabelImportExport() { +} + + +/* FUNCTION: importFromFile */ +NXCommandResult* OpenBabelImportExport::importFromFile + (NXMoleculeSet* moleculeSet, NXDataStoreInfo* dataStoreInfo, + const string& filename, int frameSetId, int frameIndex) { + + bool success = true; + NXCommandResult* result = new NXCommandResult(); + result->setResult(NX_CMD_SUCCESS); + + // Check input file format + OBConversion conv; + OBFormat* inFormat = conv.FormatFromExt(filename.c_str()); + if (inFormat) { + conv.SetInFormat(inFormat); + + } else { + populateCommandResult(result, + (string("Can't read file type: ") + filename) + .c_str()); + success = false; + } + + // Read file into a new OpenBabel molecule + if (success) { + ifstream inFileStream; + inFileStream.open(filename.c_str()); + if (!inFileStream) { + populateCommandResult(result, + (string("File not found: ") + filename) + .c_str()); + success = false; + + } else { + conv.SetInStream(&inFileStream); + + OBMol* obMolecule = moleculeSet->newMolecule(); + conv.Read(obMolecule); + inFileStream.close(); + } + } + + // Retrieve and set the meta information about the data store. + if (success) { + QFileInfo fileInfo(filename.c_str()); + dataStoreInfo->addInputStructure(qPrintable(fileInfo.fileName()), + frameSetId); + } + + return result; +} + + +/* FUNCTION: exportToFile */ +NXCommandResult* OpenBabelImportExport::exportToFile + (NXMoleculeSet* moleculeSet, NXDataStoreInfo* dataStoreInfo, + const string& filename, int frameSetId, int frameIndex) { + + bool success = true; + NXCommandResult* result = new NXCommandResult(); + result->setResult(NX_CMD_SUCCESS); + + // Make sure we can handle the output type. + OBConversion conv; + OBFormat* outFormat = conv.FormatFromExt(filename.c_str()); + if (outFormat) { + conv.SetOutFormat(outFormat); + + } else { + populateCommandResult(result, + (string("Can't write file type: ") + filename) + .c_str()); + success = false; + } + + // Write the molecule to file + if (success) { + ofstream outFileStream; + outFileStream.open(filename.c_str()); + if (!outFileStream) { + populateCommandResult(result, + (string("Couldn't open file: ") + filename) + .c_str()); + success = false; + + } else { + conv.SetOutStream(&outFileStream); + + OBMolIterator moleculeIter = moleculeSet->moleculesBegin(); + OBMol* obMolecule = *moleculeIter; + conv.Write(obMolecule); + outFileStream.close(); + } + } + return result; +} + + +/* FUNCTION: populateCommandResult */ +void OpenBabelImportExport::populateCommandResult + (NXCommandResult* result, const string& message) { + result->setResult(NX_PLUGIN_REPORTS_ERROR); + vector<QString> resultVector; + resultVector.push_back("OpenBabelImportExport"); + resultVector.push_back(message.c_str()); + result->setParamVector(resultVector); +} diff --git a/cad/plugins/NanoVision-1/src/Plugins/OpenBabelImportExport/OpenBabelImportExport.h b/cad/plugins/NanoVision-1/src/Plugins/OpenBabelImportExport/OpenBabelImportExport.h new file mode 100644 index 000000000..18c327b48 --- /dev/null +++ b/cad/plugins/NanoVision-1/src/Plugins/OpenBabelImportExport/OpenBabelImportExport.h @@ -0,0 +1,72 @@ +// Copyright 2008 Nanorex, Inc. See LICENSE file for details. + +#ifndef NX_OPENBABELIMPORTEXPORT_H +#define NX_OPENBABELIMPORTEXPORT_H + +#ifdef WIN32 +# ifdef _MSC_VER +# pragma warning(disable:4786) +# endif +#endif + +#include <QDir> +#include <QFile> +#include <QString> + +#include <openbabel/obconversion.h> + +#include "Nanorex/Utility/NXLogger.h" +#include "Nanorex/Utility/NXCommandResult.h" +#include "Nanorex/Interface/NXNumbers.h" +#include "Nanorex/Interface/NXNanoVisionResultCodes.h" +//#include "Nanorex/Interface/NXEntityManager.h" +#include "Nanorex/Interface/NXDataImportExportPlugin.h" +using namespace Nanorex; + +//#include <iostream> +#include <vector> +//#include <string> +//#include <stack> +//#include <list> +//#include <map> +using namespace std; + +#ifdef WIN32 +# if _MSC_VER > 1000 +# pragma once +# endif // _MSC_VER > 1000 + +// Exclude rarely-used stuff from Windows headers +# define WIN32_LEAN_AND_MEAN +# include <windows.h> + +// DLL-specific +# define DLLEXPORT __declspec(dllexport) +#else +# define DLLEXPORT +#endif + +extern "C" DLLEXPORT NXPlugin* instantiate(); + + +/* CLASS: OpenBabelImportExport */ +class OpenBabelImportExport : public NXDataImportExportPlugin { + public: + OpenBabelImportExport(); + ~OpenBabelImportExport(); + + // NXDataImportExportPlugin implementation + NXCommandResult* importFromFile(NXMoleculeSet* moleculeSet, + NXDataStoreInfo* dataStoreInfo, + const std::string& filename, + int frameSetId, int frameIndex); + NXCommandResult* exportToFile(NXMoleculeSet* moleculeSet, + NXDataStoreInfo* dataStoreInfo, + const std::string& filename, + int frameSetId, int frameIndex); + private: + void populateCommandResult(NXCommandResult* result, + const string& message); +}; + +#endif diff --git a/cad/plugins/NanoVision-1/src/Plugins/OpenBabelImportExport/OpenBabelImportExportTest.cpp b/cad/plugins/NanoVision-1/src/Plugins/OpenBabelImportExport/OpenBabelImportExportTest.cpp new file mode 100644 index 000000000..637dfa287 --- /dev/null +++ b/cad/plugins/NanoVision-1/src/Plugins/OpenBabelImportExport/OpenBabelImportExportTest.cpp @@ -0,0 +1,87 @@ +// Copyright 2008 Nanorex, Inc. See LICENSE file for details. + +#include "OpenBabelImportExportTest.h" + + +CPPUNIT_TEST_SUITE_REGISTRATION(OpenBabelImportExportTest); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(OpenBabelImportExportTest, "OpenBabelImportExportTestSuite"); + + +/* FUNCTION: setUp */ +void OpenBabelImportExportTest::setUp() { + logger = new NXLogger(); + //logger->addHandler(new NXConsoleLogHandler(NXLogLevel_Info)); + entityManager = new NXEntityManager(); + + NXProperties* properties = new NXProperties(); + properties->setProperty("ImportExport.0.plugin", + "libOpenBabelImportExport"); + properties->setProperty("ImportExport.0.exportFormats", + "Protein Data Bank (*.pdb)"); + properties->setProperty("ImportExport.0.importFormats", + "Protein Data Bank (*.pdb)"); + entityManager->loadDataImportExportPlugins(properties); + delete properties; +} + + +/* FUNCTION: tearDown */ +void OpenBabelImportExportTest::tearDown() { + delete entityManager; + delete logger; +} + + +/* FUNCTION: basicExportTest */ +void OpenBabelImportExportTest::basicExportTest() { + + // Create a water molecule for frame 0 + // + int frameSetId = entityManager->addFrameSet(); + int frameIndex = entityManager->addFrame(frameSetId); + NXMoleculeSet* rootMoleculeSet = + entityManager->getRootMoleculeSet(frameSetId, frameIndex); + OBMol* molecule = rootMoleculeSet->newMolecule(); + OBAtom* atomO = molecule->NewAtom(); + atomO->SetAtomicNum(etab.GetAtomicNum("O")); // Oxygen + atomO->SetVector(0.00000000, 0.00000000, 0.37000000); // Angstroms + OBAtom* atomH1 = molecule->NewAtom(); + atomH1->SetAtomicNum(etab.GetAtomicNum("H")); // Hydrogen + atomH1->SetVector(0.78000000, 0.00000000, -0.18000000); + OBAtom* atomH2 = molecule->NewAtom(); + atomH2->SetAtomicNum(etab.GetAtomicNum("H")); // Hydrogen + atomH2->SetVector(-0.78000000, 0.00000000, -0.18000000); + OBBond* bond = molecule->NewBond(); + bond->SetBegin(atomO); + bond->SetEnd(atomH1); + bond = molecule->NewBond(); + bond->SetBegin(atomO); + bond->SetEnd(atomH2); + + // Write it with the OpenBabelImportExport plugin + NXCommandResult* commandResult = + entityManager->exportToFile("testOpenBabel.pdb", frameSetId, -1); + if (commandResult->getResult() != NX_CMD_SUCCESS) + printf("\n%s\n", qPrintable(GetNV1ResultCodeString(commandResult))); + CPPUNIT_ASSERT(commandResult->getResult() == NX_CMD_SUCCESS); +} + + +/* FUNCTION: basicImportTest */ +void OpenBabelImportExportTest::basicImportTest() { + + // Read with the OpenBabelImportExport plugin + NXCommandResult* commandResult = + entityManager->importFromFile("testOpenBabel.pdb"); + if (commandResult->getResult() != NX_CMD_SUCCESS) + printf("\n%s\n", qPrintable(GetNV1ResultCodeString(commandResult))); + CPPUNIT_ASSERT(commandResult->getResult() == NX_CMD_SUCCESS); + + NXMoleculeSet* rootMoleculeSet = entityManager->getRootMoleculeSet(0, 0); + CPPUNIT_ASSERT(rootMoleculeSet != 0); + OBMolIterator moleculeIter = rootMoleculeSet->moleculesBegin(); + CPPUNIT_ASSERT((*moleculeIter)->GetAtom(1)->GetAtomicNum() == 8); + CPPUNIT_ASSERT_DOUBLES_EQUAL(0.37, (*moleculeIter)->GetAtom(1)->GetZ(), + 0.001); + CPPUNIT_ASSERT((*moleculeIter)->NumBonds() == 2); +} diff --git a/cad/plugins/NanoVision-1/src/Plugins/OpenBabelImportExport/OpenBabelImportExportTest.h b/cad/plugins/NanoVision-1/src/Plugins/OpenBabelImportExport/OpenBabelImportExportTest.h new file mode 100644 index 000000000..e9b42d585 --- /dev/null +++ b/cad/plugins/NanoVision-1/src/Plugins/OpenBabelImportExport/OpenBabelImportExportTest.h @@ -0,0 +1,33 @@ +// Copyright 2008 Nanorex, Inc. See LICENSE file for details. + +#ifndef OPENBABELIMPORTEXPORTTEST_H +#define OPENBABELIMPORTEXPORTTEST_H + +#include <cppunit/extensions/HelperMacros.h> + +#include "Nanorex/Interface/NXEntityManager.h" + +using namespace Nanorex; + + +/* CLASS: OpenBabelImportExportTest */ +class OpenBabelImportExportTest : public CPPUNIT_NS::TestFixture { + + CPPUNIT_TEST_SUITE(OpenBabelImportExportTest); + CPPUNIT_TEST(basicExportTest); + CPPUNIT_TEST(basicImportTest); + CPPUNIT_TEST_SUITE_END(); + + public: + void setUp(); + void tearDown(); + + void basicExportTest(); + void basicImportTest(); + + private: + NXEntityManager* entityManager; + NXLogger* logger; +}; + +#endif |