summaryrefslogtreecommitdiff
path: root/cad/plugins/NanoVision-1/src/Plugins/NanorexMMPImportExport/mmp-parser.rl
blob: e682b12f6ccaa7170cf1309a5643fd74a38b886e (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
#include <iostream>
#include <cstring>
#include <fstream>
#include <sstream>
#include <set>
#include <map>
#include <vector>
#include <cassert>
#include <openbabel/mol.h>
#include <Nanorex/Interface/NXMoleculeData.h>
#include "periodic_table.h"
#include "ragelistreamptr.h"

using namespace std;
using namespace OpenBabel;
using namespace Nanorex;

#define CERR(x) cerr << filename << ':' << line << ": " << (x) << endl

%%{
# Ragel fsm
    
    machine mmp_parser;
    
    EOL = '\n' ${/*++line;*/} ;
    EOF = 0xff ;
    
    whole_number = digit+
        >to{intval=0;}
        ${intval = intval*10 + (fc-'0');}
    ; 
    
    integer = ('+'? whole_number ) | ('-' whole_number %{intval=-intval;}) ;
    
    charString = ('_' | alnum) (alnum | [_\-])*
        >to{stringval.clear(); stringval = stringval + fc;}
        ${stringval = stringval + fc; }
    ;
    
    charStringWithSpace = ('_' | alnum) (alnum | [ _\-])*
        >to{stringval.clear(); stringval = stringval + fc;}
        ${stringval = stringval + fc; }
    ;
    
    charStringWithSpace2 = ('_' | alnum) (alnum | [ _\-])*
        >to{stringval2.clear();  stringval2 = stringval2 + fc;}
        ${stringval2 = stringval2 + fc; }
    ;
    
    BLANK_LINE = space* EOL @{++line;}
        %{CERR("skipped blank line");}
    ;
    COMMENT_LINE = space* '#' [^\n]* EOL @{++line;}
        %{CERR("skipped comment");}
    ;
    INCOMPREHENSIBLE_LINE = [^\n]+
        EOL @{++line;}
        %{CERR("skipped incomprehensible");}
    ;

    # Read in atom
    get_atom_id = whole_number %{atomID = intval;} ;
    get_atomic_num = '(' space* whole_number @{atomicNum = intval;} space* ')' ;
    get_coords =
        '('  space*
        (integer % { x=intval;}) space* ',' space*
        (integer % { y=intval;}) space* ',' space*
        (integer % { z=intval;}) space* ')'
        ;
    get_atom_style =
        ( 'def'  %{atomStyle[atomID] = diDEFAULT;} |
          'inv'  %{atomStyle[atomID] = diINVISIBLE;} |
          'vdw'  %{atomStyle[atomID] = diTrueCPK;} |
          'lin'  %{atomStyle[atomID] = diLINES;} |
          'cpk'  %{atomStyle[atomID] = diBALL;} |
          'tub'  %{atomStyle[atomID] = diTUBES;} )
        ;
atom_record :=
        space+
        get_atom_id
        space+
        get_atomic_num
        space+
        get_coords
        space+
        get_atom_style
        space* EOL
        @{
            ++line;
            /*cerr << "after atom, *p = ";
            if(*p=='\n') cerr << "\\n" << endl;
            else cerr << (*p) << endl; */
            if(molPtr != NULL) {
                map<int,OBAtom*>::iterator atomExistsQuery = foundAtomList.find(atomID);
                // guard against duplicates
                // also a hack to protect against Ragel's duplicate parsing when encountering a blank line
                if(atomExistsQuery == foundAtomList.end()) {
                    // atom was not previously encountered, include
                    cerr << filename << ':' << line << ": "
                        << chem::PeriodicTable::GetSymbol(chem::element_t(atomicNum))
                        << " atom with index " << atomID << endl;
                    atomPtr = molPtr->NewAtom();
                    // atomPtr->SetIdx(atomID);
                    NXMoleculeData *atomIDData = new NXMoleculeData;
                    atomIDData->SetIdx(atomID);
                    atomPtr->SetData(atomIDData);
                    atomPtr->SetAtomicNum(atomicNum);
                    atomPtr->SetVector(x,y,z);
                    foundAtomList[atomID] = atomPtr;
                }
            }
            fret;
        }
    ;
    


# Read in bond
    get_bond_order = ('1' %{bond_order=1;} |
                      '2' %{bond_order=2;} |
                      '3' %{bond_order=3;} |
                      'a' %{bond_order=4;} |
                      'g' %{bond_order=5;} |
                      'c' %{bond_order=6;} ) ;
    get_bond_target_atom =
        whole_number
        %{
            int const& targetAtomIdx = intval;
            // cerr << "get_bond_target_atom: " << targetAtomIdx << endl;
            map<int,OBAtom*>::iterator targetAtomExistsQuery = foundAtomList.find(targetAtomIdx);
            if(targetAtomExistsQuery == foundAtomList.end()) {
                ostringstream errMsg;
                errMsg << "**ERROR** attempting to bond to non-existent atomID " << targetAtomIdx;
                CERR(errMsg.str());
            }
            else {
                OBAtom *targetAtomPtr = foundAtomList[targetAtomIdx];
                // guard against duplicates
                // also a hack to protect against Ragel's duplicate parsing when encountering a blank line
                if(molPtr->GetBond(atomPtr, targetAtomPtr) == NULL) {
                    // bond was not previously encountered, include
                    ostringstream msg;
                    msg << "bonding atom #" << atomPtr->GetIdx() << " to atom #" << targetAtomPtr->GetIdx();
                    CERR(msg.str());
                    targetAtomList.push_back(targetAtomPtr);
                }
                else {
                    ostringstream msg;
                    msg << "bond to atom #" << targetAtomIdx << " already exists";
                    CERR(msg.str());
                }
            }
        }
    ;
bond_record :=
#        'bond'
        ( get_bond_order
            %{ /*cerr << "bond_record: *p=" << fc << endl;*/ CERR("clearing targetAtomList"); targetAtomList.clear(); }
        )
        (space+ get_bond_target_atom)+
        space* EOL
#        @{++line;}
        @{
            ++line;
            {
                ostringstream msg;
                msg << bondOrderNameString[bond_order-1] << " bond to " << targetAtomList.size() << " atoms";
                CERR(msg.str());
            }
            if(molPtr != NULL && atomPtr != NULL) {
                vector<OBAtom*>::iterator targetAtomIter;
                for(targetAtomIter  = targetAtomList.begin();
                    targetAtomIter != targetAtomList.end();
                    ++targetAtomIter)
                {
                    bondPtr = molPtr->NewBond();
                    bondPtr->SetBondOrder(bond_order);
                    OBAtom *targetAtomPtr = *targetAtomIter;
                    bondPtr->SetBegin(atomPtr);
                    bondPtr->SetEnd(targetAtomPtr);
                    atomPtr->AddBond(bondPtr);
                    targetAtomPtr->AddBond(bondPtr);
                    // cerr << "bonded atom #" << atomPtr->GetIdx() << " to atom #" << targetAtomPtr->GetIdx() << endl;
                }
            }
            fret;
        }
        ;
    
    

# Read in molecule
    mol_style = ('def' | 'inv' | 'vdw' | 'cpk' | 'lin' | 'tub') ;
#ignore molecule style for now
mol_record := space+ '(' space* charString space* ')' space+ mol_style space* EOL
    @{
        ++line;
        molPtr = createNewMolecule();
        molPtr->SetTitle(stringval.c_str());
        {
            ostringstream msg;
            msg << "molecule " << molPtr->GetTitle();
            CERR(msg.str());
        }
        fret;
     }
     ;

# @todo - spaces allowed in keys and values but there should only be one pair per line?
info_atom_record := ( space+ charStringWithSpace space* '=' space* charStringWithSpace2
                      %{  {
                            ostringstream msg;
                            msg << "atom-property: " << stringval << " = " << stringval2;
                            CERR(msg.str());
                          }
                          applyAtomProperty(atomPtr, stringval, stringval2);
                       }
                     )+
                    space* EOL
                    @{
                        fret;
                    }
                    ;
    
#info_record := space+
#               ( 'atom' @{fcall info_atom_record;} |
#                 'chunk' @{fcall info_chunk_record;} |
#                 'leaf' @{fcall info_leaf_record;} |
#                 'opengroup' @{fcall info_opengroup_record;} |
#               );
    
main := ( space**
          ( 'mol'  @{fcall mol_record;} $(main,10) |
            'atom' @{fcall atom_record;} $(main,10) |
            'bond' @{fcall bond_record;} $(main,10) |
            'info' space+ 'atom' @{++line;fcall info_atom_record;} $(main,10) |
            '#' [^\n]* EOL $(main,10) %{++line; CERR("comment"); } |
            [^\n]+ EOL $(main,1) %{++line; CERR("ignored"); }
          )?
#          EOL @{++line;}
        )*
        $err{success = false;}
        ;
    
}%%


#define SKIP_IF_BLANK_OR_COMMENT_LINE(p) { \
    for(; *p != '\0' && isspace(*p); ++p); \
    if(*p == '\0') { cerr << "Skipping blank line" << endl; continue; } \
    if(*p == '#') { cerr << "skipping blank line" << endl; continue; } \
}

%% write data;


class MMPException {};

class MMPParser {
public:
    MMPParser(string const& theFilename);
    ~MMPParser();
    
    bool parse(void);
    void printMolecules(void);
    
private:
    map<int, char const*> atomStyle;
    static char const *const diDEFAULT;
    static char const *const diINVISIBLE;
    static char const *const diTrueCPK;
    static char const *const diLINES;
    static char const *const diBALL;
    static char const *const diTUBES;
    static char const *const bondOrderString;
    static char const *const bondOrderNameString[];
    
    string filename;
    ifstream mmpfile;
    
    // Ragel + parser state variables
    int cs, stack[1000], top;
    int intval, atomicNum, atomID, bond_order;
    int x, y, z;
    int line;
    OBMol *molPtr;
    OBAtom *atomPtr;
    OBBond *bondPtr;
    map<int,OBAtom*> foundAtomList;
    vector<OBAtom*> targetAtomList;
    string stringval, stringval2;
    
    RagelIstreamPtr p, pe, eof;
    
    vector<OBMol*> moleculeSet;

    OBMol* createNewMolecule(void);
    void printMolecule(OBMol *molPtr);
    
    void applyAtomProperty(OBAtom *atomPtr,
                           string const& keyStr,
                           string const& valueStr);
};

char const *const MMPParser::diDEFAULT("def");
char const *const MMPParser::diINVISIBLE("inv");
char const *const MMPParser::diTrueCPK("vdw");
char const *const MMPParser::diLINES("lin");
char const *const MMPParser::diBALL("cpk");
char const *const MMPParser::diTUBES("tub");

char const *const MMPParser::bondOrderString("123agc");
char const *const MMPParser::bondOrderNameString[] = {
    "single", "double", "triple", "aromatic", "graphitic", "carbomeric"
};

void MMPParser::applyAtomProperty(OBAtom *atomPtr,
                                  string const& keyStr,
                                  string const& valueStr)
{
    if(molPtr != NULL && atomPtr != NULL) {
        if(keyStr == "atomtype") { // hybridization info
            if(valueStr == "sp") atomPtr->SetHyb(1);
            else if(valueStr == "sp2") atomPtr->SetHyb(2);
            else if(valueStr == "sp2_g") atomPtr->SetHyb(2);
            else if(valueStr == "sp3") atomPtr->SetHyb(3);
            else if(valueStr == "sp3d") atomPtr->SetHyb(3);
            // else ignore
        }
    }
}

MMPParser::MMPParser(string const& theFilename)
{
    filename = theFilename;
    mmpfile.open(filename.c_str(), std::ios::in);
    if(!mmpfile) {
        cerr << "Cannot open " << filename << " for input" << endl;
        throw MMPException();
    }
    
    p = RagelIstreamPtr(mmpfile);
    pe = RagelIstreamPtr(mmpfile, 0, ios::end);
    eof = pe;
    
    line = 0;
    
    molPtr = NULL;
    atomPtr = NULL;
    bondPtr = NULL;
    foundAtomList.clear();
    targetAtomList.clear();
    moleculeSet.clear();

    %% write init;
}

MMPParser::~MMPParser()
{
    vector<OBMol*>::iterator molIter;
    for(molIter = moleculeSet.begin(); molIter != moleculeSet.end(); ++molIter) {
        molPtr = *molIter;
        delete molPtr;
    }
}


inline OBMol* MMPParser::createNewMolecule(void)
{ 
    // foundAtomList.clear();
    atomPtr = NULL;
    bondPtr = NULL;
    OBMol *newMolPtr = new OBMol;
    moleculeSet.push_back(newMolPtr);
    return newMolPtr;
}


bool MMPParser::parse()
{
    bool success = true;
    %% write exec;
    return success;
}


void MMPParser::printMolecule(OBMol *molPtr)
{
    set<int> prevAtomIdx;
    set<int> prevBondIdx;
    
    cout << "Molecule (" << molPtr->GetTitle() << ')' << endl;
    
    OBAtomIterator atomIter;
    for(atomPtr = molPtr->BeginAtom(atomIter);
        atomPtr != NULL;
        atomPtr = molPtr->NextAtom(atomIter))
    {
        // int const atomID = atomPtr->GetIdx();
        NXMoleculeData *atomIDData = static_cast<NXMoleculeData*>(atomPtr->GetData(NXMoleculeDataType));
        int atomID = atomIDData->GetIdx();
        map<int,char const*>::const_iterator atomStyleIter = atomStyle.find(atomID);
        assert(atomStyleIter != atomStyle.end());
        char const *const atomStyleString = atomStyleIter->second;
        cout << "atom " << atomID << " (" << atomPtr->GetAtomicNum() << ") "
            << '(' << atomPtr->x() << ',' << atomPtr->y() << ',' << atomPtr->z() << ')'
            << ' ' << atomStyleString << endl;
        
        char const *hybridizationString[8] = {
            "none", "sp", "sp2", "sp3", "X-hyb4", "X-hyb5", "X-hyb6", "X-hyb7"
        };
        if(atomPtr->GetHyb() != 0)
            cout << "info atom atomtype = " << hybridizationString[atomPtr->GetHyb()] << endl;
        
        OBBondIterator bondIter;
        vector<int> bondCategories[6];
        for(bondPtr = atomPtr->BeginBond(bondIter);
            bondPtr != NULL;
            bondPtr = atomPtr->NextBond(bondIter))
        {
            OBAtom *nbrAtomPtr = bondPtr->GetNbrAtom(atomPtr);
            if(prevAtomIdx.find(nbrAtomPtr->GetIdx()) != prevAtomIdx.end()) {
                int bondOrder = bondPtr->GetBondOrder();
                bondCategories[bondOrder-1].push_back(nbrAtomPtr->GetIdx());
                prevBondIdx.insert(bondPtr->GetIdx());
            }
        }
        
        for(int i=0; i<6; ++i) {
                int J = bondCategories[i].size();
                if(J > 0) {
                    cout << "bond" << bondOrderString[i];
                    for(int j=0; j<J; ++j)
                         cout << ' ' << bondCategories[i][j];
                    cout  << endl;
            }
        }
        
        prevAtomIdx.insert(atomID);
    }
    
    cerr << "# atoms check ";
    if(molPtr->NumAtoms() == prevAtomIdx.size())
        cerr << "PASS ("  << molPtr->NumAtoms() << ')' << endl;
    else
        cerr << "FAIL: " << molPtr->NumAtoms() << " != " << prevAtomIdx.size()<< endl;
    
    cerr << "# bonds check ";
    if(molPtr->NumBonds() == prevBondIdx.size())
        cerr << "PASS (" << molPtr->NumBonds() << ')' << endl;
    else
        cerr << "FAIL: " << molPtr->NumBonds() << " != " << prevBondIdx.size()<< endl;
        
    cout << endl;
}


void MMPParser::printMolecules(void)
{
    vector<OBMol*>::iterator molIter;
    for(molIter = moleculeSet.begin();
        molIter != moleculeSet.end();
        ++molIter)
    {
        printMolecule(*molIter);
    }
}


int main(int argc, char *argv[])
{
    if(argc != 2) {
        cerr << "usage: mmp-parser <filename>" << endl;
        return 1;
    }
    MMPParser mmpParser(argv[1]);
    bool parsed = mmpParser.parse();
    if(parsed) mmpParser.printMolecules();
    return 0;
}