summaryrefslogtreecommitdiff
path: root/cad/plugins/NanoVision-1/src/Plugins/NanorexMMPImportExport/NanorexMMPImportExport.h
blob: 94264924ddc54b854fd8dc742346edaa9558e557 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Copyright 2008 Nanorex, Inc.  See LICENSE file for details.

#ifndef NANOREXMMPIMPORTEXPORT_H
#define NANOREXMMPIMPORTEXPORT_H

#include <openbabel/mol.h>
#include "Nanorex/Utility/NXLogger.h"
#include "Nanorex/Utility/NXCommandResult.h"
#include "Nanorex/Interface/NXNanoVisionResultCodes.h"
#include "Nanorex/Interface/NXDataImportExportPlugin.h"
#include <Nanorex/Interface/NXAtomData.h>
#include <Nanorex/Interface/NXMoleculeSet.h>
#include <Nanorex/Interface/NXNamedView.h>

#include "RagelIstreamPtr.h"
#include <fstream>
#include <vector>
#include <sstream>
#include <set>
#include <map>
#include <cmath>
// #include <stack>

using namespace std;
using namespace Nanorex;


/* CLASS: NanorexMMPImportExport */
class NanorexMMPImportExport : public QObject, public NXDataImportExportPlugin
{
	Q_OBJECT;
	Q_INTERFACES(Nanorex::NXDataImportExportPlugin);
        
public:
        
    NanorexMMPImportExport();
    ~NanorexMMPImportExport();
    
    // NXDataImportExportPlugin implementation
    NXCommandResult* importFromFile(NXMoleculeSet* moleculeSet,
                                    NXDataStoreInfo* dataStoreInfo,
                                    const std::string& theFilename,
                                    int frameSetId, int frameIndex);
    NXCommandResult* exportToFile(NXMoleculeSet* moleculeSet,
                                  NXDataStoreInfo* dataStoreInfo,
                                  const std::string& theFilename,
                                  int frameSetId, int frameIndex);

	string const& getRequiredVersion(void) const { return requiredVersion; }
	string const getPreferredVersion(void) const { return preferredVersion; }
	
	double const& getTemperature(void) const { return kelvinTemp; }
	
	
private:
    
	string inputFilename;
	NXDataStoreInfo *dataStoreInfo;
	
	// extracted data that can be queried later
	string requiredVersion;
	string preferredVersion;
	double kelvinTemp;
	
	// state variables
	int lineNum;
	
// scratch variables to write parsed values to
	OBMol *molPtr;   ///< current molecule
	OBAtom *atomPtr; ///< current atom
	OBBond *bondPtr; ///< current bond

	NXMoleculeSet *molSetPtr;
	///< current molecule-set. This is initialized by the input in
	///< importFromFile() which points to the molecule-structure of interest
	///< to the user. When inside the Clipboard group, this is NULL at the top
	///< level in between groups. Each top-level node will have to be allocated
	///< and assigned to this.
	
	bool insideViewDataGroup;
	NXMoleculeSet *clipboardGroup;
	bool insideClipboardGroup;
		

	int atomicNum, atomId, bondOrder;
	string atomStyle;
	map<int,OBAtom*> foundAtomList;
	vector<OBAtom*> targetAtomList;
	string stringVal, stringVal2;
	int intVal, intVal2;
	double doubleVal;
	// named-view temporaries
	std::string csysViewName;
	double csysQw, csysQx, csysQy, csysQz;
	double csysScale, csysZoomFactor;
	double csysPovX, csysPovY, csysPovZ;
	
    // molecule-set 'stack' to help with recursive 'group' specification
	NXMoleculeSet *rootMoleculeSetPtr;
	std::vector<NXMoleculeSet*> molSetPtrStack;
	std::string molStyle;
	// int molStructGroupLevel; ///< level in recursive specification
	// std::vector<std::string> defaultAtomStyleStack; // track with recursion into groups

	// Ragel + parser state variables
    int cs, top, act;
    int x, y, z;
    int stackSize;
	std::vector<int> stack;
	
    // Ragel pointers to input stream
    RagelIstreamPtr p, pe, eof, ts, te;
	RagelIstreamPtr charStringWithSpaceStart, charStringWithSpaceStop;
	RagelIstreamPtr lineStart;
	
	NXCommandResult commandResult;
	
    // helper functions
	
	void reset(void);
    bool readMMP(istream& instream, NXMoleculeSet *rootMoleculeSetPtr);
	// void createNewMolecule(void);
	// void createNewMoleculeSet(void);
	// void closeMoleculeSet(void);
    
	// void applyAtomType(string const& keyStr, string const& valueStr);
    
	
	// "slots" called for matched patterns
	
	void newAtom(int id, int atomicNum, int x, int y, int z,
	             string const& style);
	void newBond(string const& type, int targetAtomId);
	void newBondDirection(int atomId1, int atomId2);
	void newMolecule(string const& name, string const& style);
	void newViewDataGroup();
	void newNamedView(std::string const& name,
	                  double const& qw, double const& qx, double const& qy,
	                  double const& qz, double const& scale,
	                  double const& povX, double const& povY,
	                  double const& povZ, double const& zoomFactor);
	void newMolStructGroup(std::string const& name,
	                       std::string const& classification);
	void endMolStructGroup(std::string const& name);
	void newClipboardGroup();
	void endGroup(std::string const& groupName);
	
	void newAtomInfo(std::string const& key, std::string const& value);
	void newOpenGroupInfo(std::string const& key, std::string const& value);
	void newChunkInfo(std::string const& key, std::string const& value);
	
	void end1();
	
    // Static data and function members
    
    static int const NUM_BOND_TYPES = 6;
    static char const _s_bondOrderString[NUM_BOND_TYPES];
    static char const _s_bondOrderNameString[NUM_BOND_TYPES][16];
    static char const _s_hybridizationName[8][8];
    
    static void PrintMoleculeSet(ostream& o,
                                 NXMoleculeSet *const molSetPtr);
    static void PrintMolecule(ostream& o, OBMol *const molPtr);
    
	static int GetBondOrderFromType(string const& type);
	static int GetAtomID(OBAtom *atomPtr);
	// static char const *const GetAtomRenderStyleName(OBAtom *const atomPtr);
	static string const& GetAtomRenderStyleCode(OBAtom *const atomPtr);

    static void populateCommandResult(NXCommandResult* result,
                                      const string& message);
	
	static void SetResult(NXCommandResult& cmdResult,
	                      int errCode, std::string const& errMsg);
	static void ClearResult(NXCommandResult& cmdResult);
	
    

	friend class NanorexMMPImportExportTest;
	
};


#endif // NANOREXMMPIMPORTEXPORT_H