summaryrefslogtreecommitdiff
path: root/cad/src/experimental/openbabel-changes/src/formats/mmpformat.cpp
blob: 8aaae4726d79b5d97db8fc66e1ccc59c371f3914 (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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
// Copyright 2006-2007 Nanorex, Inc.  See LICENSE file for details. 
/**********************************************************************
 
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation version 2 of the License.
 
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
***********************************************************************/

#include "babelconfig.h"
#include "mol.h"
#include "obconversion.h"
#include "obmolecformat.h"
#include <errno.h>
#include <stdarg.h>

#include <vector>
#include <map>

#include <sstream>

#define UNIMPLEMENTED
#define UNTESTED

// #define DEBUG

#ifdef DEBUG
static FILE *debugfile = NULL;
static void dbgprintf(const char *file, const int linenum, const char *fmt, ...)
{
    va_list argp;
    va_start(argp, fmt);
    if (debugfile == NULL) {
	fprintf(stderr, "OPENING DEBUG FILE babeldebug\n");
	debugfile = fopen("babeldebug", "w");
    }
    fprintf(debugfile, "%s (%d) ", file, linenum);
    vfprintf(debugfile, fmt, argp);
}
#define DBGPRINTF(fmt...)  dbgprintf(__FILE__, __LINE__, ## fmt)
#else
#define DBGPRINTF(fmt...)  ((void *) 0)
#endif

#define HERE()  DBGPRINTF("\n")
#define DD(z)   DBGPRINTF("%s = %d\n", #z, z);
#define XX(z)   DBGPRINTF("%s = %08X\n", #z, z);
#define SS(z)   DBGPRINTF("%s = \"%s\"\n", #z, z);

static int atom_index = 1;
static int bond_index = 1;

#define ERROR(fmt, a...) DBGPRINTF(fmt, ## a)

#define OUCH()  DBGPRINTF("TROUBLE\n")

using namespace std;
namespace OpenBabel
{

    static char __line[2000], *__p;

    struct MMPinfo
    {
	int lineNumber;
	int charPosition;
    };

    // should be preceded by a call to ERROR() giving details
    static void
    mmpParseError(struct MMPinfo *mmpInfo)
    {
	fprintf(stderr, "MMP file line %d, col %d\n", mmpInfo->lineNumber, mmpInfo->charPosition);
    }

    enum hybridization {
	sp,
	sp2,
	sp2_g, // graphitic
	sp3,
	sp3d
    };

    static char tokenBuffer[256];

    // Read the next token from the given stream.  Possible return values
    // are:
    //
    //  NULL    when EOF or an error has been encountered
    //  ""      a zero length string for each '\0' (treated as a delimiter)
    //  delim   a one char length string for each delimiter encountered
    //  token   a string for each sequence of non-delimiter characters
    //
    // See the switch statement for the list of delimiters.
    //
    // Any sequence of more than 255 non-delimiter characters is broken
    // into tokens every 255 characters.
    static char *
    readToken(istream& mmp, struct MMPinfo *mmpInfo)
    {
	char *s = tokenBuffer;

	if (mmp.eof() || mmp.bad()) {
	    return NULL;
	}
	while (!mmp.eof()) {
	    mmp.get((char &)s[0]);
	    mmpInfo->charPosition++;
	    switch (*s) {
	    case ' ':
	    case '(':
	    case ')':
	    case ',':
	    case ';':
	    case '=':
	    case '\r':
	    case '\n':
	    case '\0':
		if (s == tokenBuffer) {
		    // XXX line numbering is off by a factor of 2 for files with
		    // \r\n line termination
		    if (*s == '\n' || *s == '\r') {
			mmpInfo->lineNumber++;
			mmpInfo->charPosition = 1;
		    }
		    s++;
		} else {
		    mmp.unget();
		    mmpInfo->charPosition--;
		}
		*s = '\0';
		return tokenBuffer;
	    default:
		s++;
		if (s >= &tokenBuffer[255]) {
		    *s = '\0';
		    return tokenBuffer;
		}
	    }
	}
	if (s == tokenBuffer) {
	    return NULL;
	}
	*s = '\0';
	return tokenBuffer;
    }

    // Skip zero or more whitespace characters (space or tab).  Newline is
    // not considered whitespace.  The next token will start with the
    // first non-whitespace character found.
    static void
    consumeWhitespace(istream& mmp, struct MMPinfo *mmpInfo)
    {
	char c;

	if (mmp.eof() || mmp.bad()) {
	    return;
	}
	while (!mmp.eof()) {
	    mmp.get(c);
	    mmpInfo->charPosition++;
	    switch (c) {
	    case ' ':
	    case '\t':
		break;
	    default:
		mmp.unget();
		mmpInfo->charPosition--;
		return;
	    }
	}
    }

    // Ignores the remainder of this line.
    static void
    consumeRestOfLine(istream& mmp, struct MMPinfo *mmpInfo)
    {
	char *tok;

	while (1) {
	    tok = readToken(mmp, mmpInfo);
	    if (tok == NULL || *tok == '\n' || *tok == '\r') {
		return;
	    }
	}
    }

    // Complains and dies if the next token is not the expected token.
    // Pass in NULL to expect EOF.
    static int
    expectToken(istream& mmp, struct MMPinfo *mmpInfo, char *expected)
    {
	char *tok;
	int ret;

	tok = readToken(mmp, mmpInfo);
	if (tok == NULL) {
	    ret = expected == NULL;
	} else if (expected == NULL) {
	    ret = tok == NULL;
	} else {
	    ret = !strcmp(tok, expected);
	}
	if (!ret) {
	    ERROR("expected \"%s\", got \"%s\"\n", expected ? expected : "EOF", tok ? tok : "EOF");
	    mmpParseError(mmpInfo);
	}
	return ret;
    }

    // Parse an integer.  Returns 1 if value was successfully filled in
    // with an integer value.  If checkForNewline is true, and a newline
    // was encountered instead of an integer the newline is consumed and 0
    // is returned.
    //
    // Any whitespace before and after the integer is consumed.
    static int
    expectInt(istream& mmp, struct MMPinfo *mmpInfo, int *value, int checkForNewline)
    {
	char *tok;
	char *end;
	long int val;

	consumeWhitespace(mmp, mmpInfo);
	tok = readToken(mmp, mmpInfo);
	if (value == NULL) {
	    ERROR("internal error, value==NULL");
	    mmpParseError(mmpInfo);
	}
	if (tok != NULL) {
	    if ((*tok == '\n' || *tok == '\r') && checkForNewline) {
		return 0;
	    }
	    if (*tok == '\0') {
		ERROR("expected int, got \\0\n");
		mmpParseError(mmpInfo);
	    }
	    errno = 0;
	    val = strtol(tok, &end, 0);
	    if (errno != 0) {
		ERROR("integer value out of range: \"%s\"\n", tok);
		mmpParseError(mmpInfo);
	    }
	    if (*end != '\0') {
		ERROR("expected int, got \"%s\"\n", tok);
		mmpParseError(mmpInfo);
	    }
	    if (val > INT_MAX || val < INT_MIN) {
		ERROR("integer value out of range: \"%s\"\n", tok);
		mmpParseError(mmpInfo);
	    }
	    *value = val;
	    consumeWhitespace(mmp, mmpInfo);
	    return 1;
	}
	ERROR("expected int, got EOF\n");
	mmpParseError(mmpInfo);
	return 0; // not reached
    }

    // Parse a double.  Returns 1 if value was successfully filled in
    // with a double value.  If checkForNewline is true, and a newline
    // was encountered instead of a double the newline is consumed and 0
    // is returned.
    //
    // Any whitespace before and after the double is consumed.
    static int
    expectDouble(istream& mmp, struct MMPinfo *mmpInfo, double *value, int checkForNewline)
    {
	char *tok;
	char *end;
	double val;

	consumeWhitespace(mmp, mmpInfo);
	tok = readToken(mmp, mmpInfo);
	if (value == NULL) {
	    ERROR("internal error, value==NULL");
	    mmpParseError(mmpInfo);
	}
	if (tok != NULL) {
	    if ((*tok == '\n' || *tok == '\r') && checkForNewline) {
		return 0;
	    }
	    if (*tok == '\0') {
		ERROR("expected double, got \\0");
		mmpParseError(mmpInfo);
	    }
	    errno = 0;
	    val = strtod(tok, &end);
	    if (errno != 0) {
		ERROR("double value out of range: %s", tok);
		mmpParseError(mmpInfo);
	    }
	    if (*end != '\0') {
		ERROR("expected double, got %s", tok);
		mmpParseError(mmpInfo);
	    }
	    *value = val;
	    consumeWhitespace(mmp, mmpInfo);
	    return 1;
	}
	ERROR("expected double, got EOF");
	mmpParseError(mmpInfo);
	return 0; // not reached
    }

    // Parses:
    //
    // ( <int>, <int>, <int> )
    //
    // where each space between the parenthesis can be zero or more
    // whitespace characters.  If p is non-null, the three ints are
    // multiplied by 0.1 and placed in the x, y, and z fields.
    //
    // Any whitespace before and after the triple is consumed.
    static vector3
    expectXYZInts(istream& mmp, struct MMPinfo *mmpInfo)
    {
	int x;
	int y;
	int z;

	consumeWhitespace(mmp, mmpInfo);
	expectToken(mmp, mmpInfo, "(");
	expectInt(mmp, mmpInfo, &x, 0);
	expectToken(mmp, mmpInfo, ",");
	expectInt(mmp, mmpInfo, &y, 0);
	expectToken(mmp, mmpInfo, ",");
	expectInt(mmp, mmpInfo, &z, 0);
	expectToken(mmp, mmpInfo, ")");
	consumeWhitespace(mmp, mmpInfo);
	return vector3(x * 0.001, y * 0.001, z * 0.001);
    }

    static char tempBuffer[1000];

    char *
    copy_string(char *s)
    {
	int n = strlen(s)+1;
	char *ret = (char *)malloc(n);
	if (ret == NULL) {
	    fprintf(stderr, "Out of memory\n");
	    exit(1);
	}
	return strcpy(ret, s);
    }

    // Names in mmp files are delimited by ()'s.  () are encoded using
    // %xx, and so are disallowed from appearing in the encoded form that
    // we're reading here.  This reads an arbitrary length name (up to
    // half the size of available memory) and returns an allocated copy of
    // it.  Individual tokens are read and concatenated together to form
    // the resulting name.  A \0 may be included, and will be encoded as
    // %00.  The closing parenthesis must occur before a newline or end of
    // file.
    //
    // Any whitespace before and after the name is consumed.
    static char *
    expectName(istream& mmp, struct MMPinfo *mmpInfo)
    {
	char *tok;
	char *buf;
	int len;

	consumeWhitespace(mmp, mmpInfo);
	expectToken(mmp, mmpInfo, "(");
	len = 0;
	buf = (char *)tempBuffer;
	buf[len] = '\0';
	while ((tok = readToken(mmp, mmpInfo)) != NULL) {
	    if (!strcmp(tok, ")")) {
		consumeWhitespace(mmp, mmpInfo);
		return copy_string(buf);
	    }
	    if (*tok == '\0') {
		tok = "%00";
	    }
	    if (*tok == '\n' || *tok == '\r') {
		ERROR("reading name, expected ), got newline");
		mmpParseError(mmpInfo);
	    }
	    len += strlen(tok);
	    buf = (char *)tempBuffer;
	    strcat(buf, tok);
	}
	ERROR("reading name, expected ), got EOF");
	mmpParseError(mmpInfo);
	return ""; // not reached
    }

    // Parses a list of integers terminated by a newline.  An arbitrary
    // number of integers can be parsed, and the accumulator they are
    // stored in is placed in listPtr, with the length stored in length.  If
    // expectedLength is non-zero, complains and dies if the actual length
    // is not expectedLength.  Also complains and dies for zero length
    // lists.
    static void
    expectIntList(istream& mmp, struct MMPinfo *mmpInfo, int **listPtr, int *length, int expectedLength)
    {
	int *buf;
	int index;
	int len;
	int listElement;

	len = 0;
	buf = (int *)tempBuffer;
	index = 0;
	while (expectInt(mmp, mmpInfo, &listElement, 1)) {
	    len += sizeof(int);
	    buf = (int *)tempBuffer;
	    buf[index++] = listElement;
	}
	if (index == 0) {
	    ERROR("zero length list of atoms");
	    mmpParseError(mmpInfo);
	}
	if (expectedLength != 0 && expectedLength != index) {
	    ERROR("expected exactly %d atoms, got %d", expectedLength, index);
	    mmpParseError(mmpInfo);
	}
	*length = index;
	*listPtr = buf;
    }

    static void
    makeAtom(OBMol& mol, int atomID, int elementType, vector3 v, OBResidue *res)
    {
	OBAtom atom;
	atom.SetVector(v);
	atom.SetAtomicNum(elementType);
	if (res != NULL)
	    atom.SetResidue(res);
	mol.AddAtom(atom);
    }

    static void
    setAtomHybridization(OBMol& mol, int previousAtomID, enum hybridization hybridization)
    {
	OBAtom *atom = mol.GetAtom(previousAtomID);
	switch (hybridization) {
	case sp:
	    atom->SetHyb(1);
	    break;
	case sp2:
	    atom->SetHyb(2);
	    break;
	case sp2_g:
	    atom->SetHyb(2);  UNTESTED;
	    break;
	case sp3:
	    atom->SetHyb(3);
	    break;
	case sp3d:
	    atom->SetHyb(3);  UNTESTED;
	    break;
	default:
	    OUCH();
	    break;
	}
    }

    static void
    makeBond(OBMol& mol, int previousAtomID, int atomID, char bondOrder)
    {
	OBBond bnd;
	OBAtom *atom1, *atom2;
	atom1 = mol.GetAtom(previousAtomID);
	atom2 = mol.GetAtom(atomID);
	if (atom1 == NULL || atom2 == NULL) {
	    // This will happen whenever there are bondpoints in an MMP file
	    // so it's not a big deal.
	    return;
	}

	bnd.Set(bond_index++, atom1, atom2, 1, 0);

	switch (bondOrder) {
	case '1':
	    bnd.SetBO(1);
	    break;
	case '2':
	    bnd.SetBO(2);
	    break;
	case '3':
	    bnd.SetBO(3);
	    break;
	case 'a':  // aromatic, Open Babel handles this
	case 'g':  // graphitic -> same as aromatic??
	case 'c':  // carbomeric -> same as aromatic??
	    bnd.SetBO(5);
	    break;
	default:
	    UNIMPLEMENTED;  // ? ? ? ?
	    OUCH();
	    break;
	}

	mol.AddBond(bnd);
    }

    static void
    makeVanDerWaals(OBMol& p, int previousAtomID, int atomID)
    {
	// I really don't think we need anything here.
    }

    static void
    readMMP(OBMol& mol, istream& mmp)
    {
	//struct part *p;
	//struct mmpStream thisMMPStream, *mmp;
	char *tok;
	static char group_name[200];
	//int group_num = 0;  // If we get groups, we only want the first one. Ignore "View Data" group.
	char bondOrder;
	int elementType;
	int previousAtomID = -1; // ID of atom just defined, so we can back-reference to it in later lines
	vector3 position;
	vector3 center;
	vector3 axis;
	int atomID;
	struct MMPinfo mmpInfo;
	OBResidue *residue = NULL;
	int resnum = 1;
	int atoms_in_this_group = 0;  // we want the first NON-EMPTY group

	mmpInfo.lineNumber = 1;
	mmpInfo.charPosition = 1;
	group_name[0] = '\0';

	while ((tok = readToken(mmp, &mmpInfo)) != NULL) {

	    SS(tok);

	    // atom atomNumber (element) (posx, posy, posz)
	    // Identifies a new atom with the given element type and position.
	    // Position vectors are integers with units of 0.1pm.
	    if (!strcmp(tok, "atom")) {
		expectInt(mmp, &mmpInfo, &atomID, 0);
		expectToken(mmp, &mmpInfo, "(");
		expectInt(mmp, &mmpInfo, &elementType, 0);
		expectToken(mmp, &mmpInfo, ")");
		position = expectXYZInts(mmp, &mmpInfo);
		// hack: change singlets to hydrogen
		if (elementType == 0) elementType=1;
		previousAtomID = atomID;
		makeAtom(mol, atomID, elementType, position, residue);
		atoms_in_this_group++;
		consumeRestOfLine(mmp, &mmpInfo);
	    }

	    // after an atom:
	    //  info atom atomtype = sp2
	    // after an rmotor:
	    //  info leaf initial_speed = 20.0
	    else if (!strcmp(tok, "info")) {
		consumeWhitespace(mmp, &mmpInfo);
		tok = readToken(mmp, &mmpInfo);
		if (!strcmp(tok, "atom")) {
		    consumeWhitespace(mmp, &mmpInfo);
		    tok = readToken(mmp, &mmpInfo);
		    if (!strcmp(tok, "atomtype")) {
			enum hybridization hybridization = sp3;
			
			consumeWhitespace(mmp, &mmpInfo);
			expectToken(mmp, &mmpInfo, "=");
			consumeWhitespace(mmp, &mmpInfo);
			tok = readToken(mmp, &mmpInfo);
			
			if (!strcmp(tok, "sp3d")) {
			    hybridization = sp3d;
			} else if (!strcmp(tok, "sp3")) {
			    hybridization = sp3;
			} else if (!strcmp(tok, "sp2(graphitic)")) {
			    hybridization = sp2_g;
			} else if (!strcmp(tok, "sp2")) {
			    hybridization = sp2;
			} else if (!strcmp(tok, "sp")) {
			    hybridization = sp;
			} else {
			    ERROR("unknown hybridization: %s", tok);
			    mmpParseError(&mmpInfo);
			}
			setAtomHybridization(mol, previousAtomID, hybridization);
		    }
		}
		consumeRestOfLine(mmp, &mmpInfo);
	    }
	    else if (!strncmp(tok, "bond", 4)) {
		bondOrder = tok[4];
		while (expectInt(mmp, &mmpInfo, &atomID, 1)) {
		    makeBond(mol, previousAtomID, atomID, bondOrder);
		}
	    }
	    else if (!strcmp(tok, "mol")) {
		char *name = expectName(mmp, &mmpInfo);
		residue = new OBResidue();
		residue->SetName(name);
		residue->SetNum(resnum++);
		mol.AddResidue(*residue);
		consumeRestOfLine(mmp, &mmpInfo);
	    }
	    else if (!strcmp(tok, "group")) {
		strcpy(group_name, expectName(mmp, &mmpInfo));
		consumeRestOfLine(mmp, &mmpInfo);
		atoms_in_this_group = 0;
	    }
	    else if (!strcmp(tok, "egroup")) {
		consumeRestOfLine(mmp, &mmpInfo);
		if (atoms_in_this_group > 0) {
		    // got a non-empty group, read remains and bail
		    while ((tok = readToken(mmp, &mmpInfo)) != NULL);
		    return;
		}
	    }

	    else if (!strcmp(tok, "end")) {
		consumeRestOfLine(mmp, &mmpInfo);
		return;
	    }
	    else if (tok[0] == '\r' || tok[0] == '\n') {
		// blank line, or second char of \r\n combo
		;
	    } else {
		consumeRestOfLine(mmp, &mmpInfo);
	    }
	}
    }



    class MMPFormat : public OBMoleculeFormat
    {
    public:
	//Register this format type ID
	MMPFormat()
	{
	    OBConversion::RegisterFormat("mmp",this, "chemical/x-mmp");
	    OBConversion::RegisterFormat("ent",this, "chemical/x-mmp");
	}

	virtual const char* Description() //required
	{
	    return
		"Molecular Machine Part format\n \
       Read Options e.g. -as\n\
        s  Output single bonds only\n\
        b  Disable bonding entirely\n\n";
	}

	virtual const char* SpecificationURL()
	{
	    return "http://www.nanoengineer-1.net";
	}

	virtual const char* GetMIMEType()
	{ return "chemical/x-mmp"; };

	//Flags() can return be any the following combined by | or be omitted if none apply
	// NOTREADABLE  READONEONLY  NOTWRITABLE  WRITEONEONLY
	virtual unsigned int Flags()
	{
	    return READONEONLY;
	};

	//*** This section identical for most OBMol conversions ***
	////////////////////////////////////////////////////
	/// The "API" interface functions
	virtual bool ReadMolecule(OBBase* pOb, OBConversion* pConv);
	virtual bool WriteMolecule(OBBase* pOb, OBConversion* pConv);

    };
    //***

    //Make an instance of the format class
    MMPFormat theMMPFormat;

    static char buffer[BUFF_SIZE];
    
    /////////////////////////////////////////////////////////////////
    bool MMPFormat::ReadMolecule(OBBase* pOb, OBConversion* pConv)
    {

	OBMol* pmol = dynamic_cast<OBMol*>(pOb);
	if(pmol==NULL) {
	    DBGPRINTF("ouch\n");
	    return false;
	}

	//Define some references so we can use the old parameter names
	istream &ifs = *pConv->GetInStream();
	OBMol &mol = *pmol;
	const char* title = pConv->GetTitle();

	int chainNum = 1;
	OBBitVec bs;

	mol.SetTitle(title);

	mol.BeginModify();


	readMMP(mol, ifs);

	// these two should be unnecessary for a MMP file, unless it's broken
	HERE();
	mol.ConnectTheDots();
	HERE();
	mol.PerceiveBondOrders();

	// clean out remaining blank lines
	while(ifs.peek() != EOF && ifs.good() &&
	      (ifs.peek() == '\n' || ifs.peek() == '\r'))
	    ifs.getline(buffer,BUFF_SIZE);

	HERE();
	mol.EndModify();

	HERE();
	mol.SetAtomTypesPerceived();
	HERE();
	atomtyper.AssignImplicitValence(mol);

	HERE();
	if (!mol.NumAtoms())
	    return(false);
	HERE();
	return(true);
    }

    /*
     * Open Babel's OBResidue corresponds to NE-1's chunk.
     */

    //////////////////////////////////////////////////////////////////////////////
    bool MMPFormat::WriteMolecule(OBBase* pOb, OBConversion* pConv)
    {
	extern void _mmp_write_atoms(OBMol& mol, ostream &ofs, int resnum);

	OBMol* pmol = dynamic_cast<OBMol*>(pOb);
	if(pmol==NULL) {
	    DBGPRINTF("ouch\n");
	    return false;
	}

	//Define some references so we can use the old parameter names
	ostream &ofs = *pConv->GetOutStream();
	OBMol &mol = *pmol;

	unsigned int i;
	char buffer[BUFF_SIZE];
	char group_name[200];
	int resnum = 1;
	int residueIndex, previousResidueIndex = -1;

	snprintf(buffer, BUFF_SIZE, "mmpformat 050920 required; 060421 preferred");
	ofs << buffer << endl;
	//snprintf(buffer, BUFF_SIZE, "# Generated by OpenBabel %s", BABEL_VERSION);
	//ofs << buffer << endl;
	snprintf(buffer, BUFF_SIZE, "kelvin 300");
	ofs << buffer << endl;

	// Let's set up a coordinate system so the nasty warning goes away.
	ofs << "group (View Data)" << endl;
	ofs << "info opengroup open = True" << endl;
	ofs << "csys (HomeView) (1.000000, 0.000000, 0.000000, 0.000000) (10.000000) (0.000000, 0.000000, 0.000000) (1.000000)" << endl;
	ofs << "csys (LastView) (1.000000, 0.000000, 0.000000, 0.000000) (10.000000) (0.000000, 0.000000, 0.000000) (1.000000)" << endl;
	ofs << "egroup (View Data)" << endl;

	if (strlen(mol.GetTitle()) > 0) {
	    const char *p = mol.GetTitle();
	    std::string fn = p;
	    int pos = fn.find_last_of("\\/");
	    if (pos != string::npos) p += pos + 1;
	    strncpy(group_name, p, 199);
	    char *dot = strrchr(group_name, '.');
	    if (dot != NULL)
		*dot = '\0';
	} else {
	    snprintf(group_name, 200, "Babel-generated file");
	}
	snprintf(buffer, BUFF_SIZE, "group (%s)", group_name);
	ofs << buffer << endl;
	snprintf(buffer, BUFF_SIZE, "info opengroup open = True");
	ofs << buffer << endl;

	HERE();
	if (mol.NumResidues() > 0) {
	    OBResidue *residue;
	    vector<OBResidue*>::iterator r;
	    HERE();
	    for (residue = mol.BeginResidue(r); residue; residue = mol.NextResidue(r)) {
		HERE();
		residueIndex = residue->GetIdx();
		if (residueIndex != previousResidueIndex) {
		    previousResidueIndex = residueIndex;
		    snprintf(buffer, BUFF_SIZE, "mol (Chunk %d)", residueIndex);
		    ofs << buffer << endl;
		}
		_mmp_write_atoms(mol, ofs, residueIndex);
	    }
	} else {
	    HERE();
	    snprintf(buffer, BUFF_SIZE, "mol (Chunk 1)");
	    ofs << buffer << endl;
	    _mmp_write_atoms(mol, ofs, -1);
	}

	snprintf(buffer, BUFF_SIZE, "egroup (%s)", group_name);
	ofs << buffer << endl;

	ofs << "group (Clipboard)" << endl;
	ofs << "info opengroup open = True" << endl;
	ofs << "egroup (Clipboard)" << endl;
	
	ofs << "end molecular machine part" << endl;
	return(true);
    }

    void _mmp_write_atoms(OBMol& mol, ostream &ofs, int resnum)
    {
	for (int i = 1; i <= mol.NumAtoms(); i ++) {
	    HERE();
	    OBAtom *atom = mol.GetAtom(i);
	    DD(resnum);
	    XX(atom);
	    if (atom != NULL) {
		XX(atom->GetResidue());
		if (atom->GetResidue() != NULL) {
		    DD(atom->GetResidue()->GetIdx());
		}
	    }
	    if (resnum == -1 || (atom->GetResidue() != NULL && atom->GetResidue()->GetIdx() == resnum)) {
		DD(atom->GetAtomicNum());
		DD((int) (1000.0 * atom->GetX()));
		DD((int) (1000.0 * atom->GetY()));
		DD((int) (1000.0 * atom->GetZ()));
		snprintf(buffer, BUFF_SIZE, "atom %d (%d) (%d, %d, %d) def",
			 atom->GetIdx(), atom->GetAtomicNum(),
			 (int) (1000.0 * atom->GetX()),
			 (int) (1000.0 * atom->GetY()),
			 (int) (1000.0 * atom->GetZ()));
		ofs << buffer << endl;
		// Now the bonds
		vector<OBEdgeBase*>::iterator j;
		for (int bondOrder = 1; bondOrder <= 5; bondOrder++) {
		    int bondsOfThisOrder = 0;
		    OBBond *bond;
		    HERE();
		    for (bond = atom->BeginBond(j); bond; bond = atom->NextBond(j)) {
			if (bond->GetBO() == bondOrder) {
			    OBAtom *atom2 = bond->GetNbrAtom(atom);
			    if (atom2->GetIdx() < atom->GetIdx())
				bondsOfThisOrder++;
			}
		    }
		    HERE();
		    if (bondsOfThisOrder > 0) {
			DBGPRINTF("Uncharted waters!\n");
			UNTESTED; // test for aromatic, graphitic, carbomeric bonds
			static char bondchars[] = " 123agc";
			snprintf(buffer, BUFF_SIZE, "bond%c", bondchars[bondOrder]);
			ofs << buffer;
			for (bond = atom->BeginBond(j); bond; bond = atom->NextBond(j)) {
			    if (bond->GetBO() == bondOrder) {
				OBAtom *atom2 = bond->GetNbrAtom(atom);
				if (atom2->GetIdx() < atom->GetIdx()) {
				    snprintf(buffer, BUFF_SIZE, " %d", atom2->GetIdx());
				    ofs << buffer;
				}
			    }
			}
			ofs << endl;
		    }
		}
	    }
	}
    }
} //namespace OpenBabel