/
MDLV2000Reader.java
939 lines (867 loc) · 40.2 KB
/
MDLV2000Reader.java
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
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
/* Copyright (C) 1997-2007 Christoph Steinbeck <steinbeck@users.sourceforge.net>
* 2010 Egon Willighagen <egonw@users.sourceforge.net>
*
* Contact: cdk-devel@lists.sourceforge.net
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1
* of the License, or (at your option) any later version.
* All we ask is that proper credit is given for our work, which includes
* - but is not limited to - adding the above copyright notice to the beginning
* of your source code files, and to any copyright notice that you may distribute
* with programs based on this work.
*
* 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package org.openscience.cdk.io;
import org.openscience.cdk.CDKConstants;
import org.openscience.cdk.annotations.TestClass;
import org.openscience.cdk.annotations.TestMethod;
import org.openscience.cdk.config.IsotopeFactory;
import org.openscience.cdk.exception.CDKException;
import org.openscience.cdk.interfaces.IAtom;
import org.openscience.cdk.interfaces.IBond;
import org.openscience.cdk.interfaces.IChemFile;
import org.openscience.cdk.interfaces.IChemModel;
import org.openscience.cdk.interfaces.IChemObject;
import org.openscience.cdk.interfaces.IChemSequence;
import org.openscience.cdk.interfaces.IIsotope;
import org.openscience.cdk.interfaces.IMolecule;
import org.openscience.cdk.interfaces.IMoleculeSet;
import org.openscience.cdk.interfaces.IPseudoAtom;
import org.openscience.cdk.interfaces.ISingleElectron;
import org.openscience.cdk.io.formats.IResourceFormat;
import org.openscience.cdk.io.formats.MDLV2000Format;
import org.openscience.cdk.io.setting.BooleanIOSetting;
import org.openscience.cdk.io.setting.IOSetting;
import org.openscience.cdk.tools.ILoggingTool;
import org.openscience.cdk.tools.LoggingToolFactory;
import org.openscience.cdk.tools.manipulator.AtomContainerManipulator;
import javax.vecmath.Point2d;
import javax.vecmath.Point3d;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
/**
* Reads a molecule from an MDL MOL or SDF file {@cdk.cite DAL92}. An SD files
* is read into a {@link IChemSequence} of {@link IChemModel}'s. Each IChemModel will contain one
* Molecule.
*
* <p>From the Atom block it reads atomic coordinates, element types and
* formal charges. From the Bond block it reads the bonds and the orders.
* Additionally, it reads 'M CHG', 'G ', 'M RAD' and 'M ISO' lines from the
* property block.
*
* <p>If all z coordinates are 0.0, then the xy coordinates are taken as
* 2D, otherwise the coordinates are read as 3D.
*
* <p>The title of the MOL file is read and can be retrieved with:
* <pre>
* molecule.getProperty(CDKConstants.TITLE);
* </pre>
*
* <p>RGroups which are saved in the MDL molfile as R#, are renamed according to their appearance,
* e.g. the first R# is named R1. With PseudAtom.getLabel() "R1" is returned (instead of R#).
* This is introduced due to the SAR table generation procedure of Scitegics PipelinePilot.
*
* @cdk.module io
* @cdk.githash
*
* @author steinbeck
* @author Egon Willighagen
* @cdk.created 2000-10-02
* @cdk.keyword file format, MDL molfile
* @cdk.keyword file format, SDF
* @cdk.bug 1587283
*/
@TestClass("org.openscience.cdk.io.MDLV2000ReaderTest")
public class MDLV2000Reader extends DefaultChemObjectReader {
BufferedReader input = null;
private static ILoggingTool logger =
LoggingToolFactory.createLoggingTool(MDLV2000Reader.class);
private BooleanIOSetting forceReadAs3DCoords;
private BooleanIOSetting interpretHydrogenIsotopes;
//Keep track of atoms and the lines they were on in the atom block.
private List<IAtom> atomsByLinePosition;
public MDLV2000Reader() {
this(new StringReader(""));
}
/**
* Constructs a new MDLReader that can read Molecule from a given InputStream.
*
*@param in The InputStream to read from
*/
public MDLV2000Reader(InputStream in) {
this(new InputStreamReader(in));
}
public MDLV2000Reader(InputStream in, Mode mode) {
this(new InputStreamReader(in), mode);
}
/**
* Contructs a new MDLReader that can read Molecule from a given Reader.
*
*@param in The Reader to read from
*/
public MDLV2000Reader(Reader in) {
this(in, Mode.RELAXED);
}
public MDLV2000Reader(Reader in, Mode mode) {
input = new BufferedReader(in);
initIOSettings();
super.mode = mode;
}
@TestMethod("testGetFormat")
public IResourceFormat getFormat() {
return MDLV2000Format.getInstance();
}
@TestMethod("testSetReader_Reader")
public void setReader(Reader input) throws CDKException {
if (input instanceof BufferedReader) {
this.input = (BufferedReader)input;
} else {
this.input = new BufferedReader(input);
}
}
@TestMethod("testSetReader_InputStream")
public void setReader(InputStream input) throws CDKException {
setReader(new InputStreamReader(input));
}
@TestMethod("testAccepts")
public boolean accepts(Class<? extends IChemObject> classObject) {
Class<?>[] interfaces = classObject.getInterfaces();
for (int i=0; i<interfaces.length; i++) {
if (IChemFile.class.equals(interfaces[i])) return true;
if (IChemModel.class.equals(interfaces[i])) return true;
if (IMolecule.class.equals(interfaces[i])) return true;
}
Class superClass = classObject.getSuperclass();
if (superClass != null) return this.accepts(superClass);
return false;
}
/**
* Takes an object which subclasses IChemObject, e.g. Molecule, and will read
* this (from file, database, internet etc). If the specific implementation
* does not support a specific IChemObject it will throw an Exception.
*
*@param object The object that subclasses
* IChemObject
*@return The IChemObject read
*@exception CDKException
*/
public <T extends IChemObject> T read(T object) throws CDKException {
if (object instanceof IChemFile) {
return (T)readChemFile((IChemFile)object);
} else if (object instanceof IChemModel) {
return (T)readChemModel((IChemModel)object);
} else if (object instanceof IMolecule) {
return (T)readMolecule((IMolecule)object);
} else {
throw new CDKException("Only supported are ChemFile and Molecule.");
}
}
private IChemModel readChemModel(IChemModel chemModel) throws CDKException {
IMoleculeSet setOfMolecules = chemModel.getMoleculeSet();
if (setOfMolecules == null) {
setOfMolecules = chemModel.getBuilder().newInstance(IMoleculeSet.class);
}
IMolecule m = readMolecule(chemModel.getBuilder().newInstance(IMolecule.class));
if (m != null) {
setOfMolecules.addMolecule(m);
}
chemModel.setMoleculeSet(setOfMolecules);
return chemModel;
}
/**
* Read a ChemFile from a file in MDL SDF format.
*
* @return The ChemFile that was read from the MDL file.
*/
private IChemFile readChemFile(IChemFile chemFile) throws CDKException {
IChemSequence chemSequence = chemFile.getBuilder().newInstance(IChemSequence.class);
IChemModel chemModel = chemFile.getBuilder().newInstance(IChemModel.class);
IMoleculeSet setOfMolecules = chemFile.getBuilder().newInstance(IMoleculeSet.class);
IMolecule m = readMolecule(chemFile.getBuilder().newInstance(IMolecule.class));
if (m != null) {
setOfMolecules.addMolecule(m);
}
chemModel.setMoleculeSet(setOfMolecules);
chemSequence.addChemModel(chemModel);
setOfMolecules = chemFile.getBuilder().newInstance(IMoleculeSet.class);
chemModel = chemFile.getBuilder().newInstance(IChemModel.class);
String str;
try {
String line;
while ((line = input.readLine()) != null) {
logger.debug("line: ", line);
// apparently, this is a SDF file, continue with
// reading mol files
str = new String(line);
if (str.equals("$$$$")) {
m = readMolecule(chemFile.getBuilder().newInstance(IMolecule.class));
if (m != null) {
setOfMolecules.addMolecule(m);
chemModel.setMoleculeSet(setOfMolecules);
chemSequence.addChemModel(chemModel);
setOfMolecules = chemFile.getBuilder().newInstance(IMoleculeSet.class);
chemModel = chemFile.getBuilder().newInstance(IChemModel.class);
}
} else {
// here the stuff between 'M END' and '$$$$'
if (m != null) {
// ok, the first lines should start with '>'
String fieldName = null;
if (str.startsWith("> ")) {
// ok, should extract the field name
str.substring(2); // String content =
int index = str.indexOf("<");
if (index != -1) {
int index2 = str.substring(index).indexOf(">");
if (index2 != -1) {
fieldName = str.substring(
index+1,
index+index2
);
}
}
}
if (line == null) {
throw new CDKException("Expecting data line here, but found null!");
}
StringBuilder data = new StringBuilder();
int dataLineCount = 0;
boolean lineIsContinued = false;
while ((line = input.readLine()) != null) {
if (line.equals(" ") && dataLineCount == 0) {
// apparently a file can have a field whose value is a single space. Moronic
// we check for it *before* trimming it. ideally we should check for any length
// of whitespace
// In adition some SD files have the blank line after the value line contain
// a space, rather than being a true blank line. So we only store a blank value
// line if it's the first line after the key line
data.append(line);
lineIsContinued = false;
dataLineCount++;
if (!lineIsContinued && dataLineCount > 1)
data.append(System.getProperty("line.separator"));
continue;
}
line = line.trim();
if (line.length() == 0) break;
if (line.equals("$$$$")) {
logger.error("Expecting data line here, but found end of molecule: ", line);
break;
}
logger.debug("data line: ", line);
lineIsContinued = false; // reset property
dataLineCount++;
// preserve newlines, unless the line is exactly 80 chars;
// in that case it is assumed to continue on the next line.
// See MDL documentation.
if (!lineIsContinued && dataLineCount > 1)
data.append(System.getProperty("line.separator"));
// add the data line
data.append(line);
// check if the line will be continued on the next line
if (line.length() == 80) lineIsContinued = true;
}
if (fieldName != null) {
logger.info("fieldName, data: ", fieldName, ", ", data);
m.setProperty(fieldName, data.toString());
}
}
}
}
} catch (CDKException cdkexc) {
throw cdkexc;
} catch (Exception exception) {
String error = "Error while parsing SDF";
logger.error(error);
logger.debug(exception);
throw new CDKException(error, exception);
}
try {
input.close();
} catch (Exception exc) {
String error = "Error while closing file: " + exc.getMessage();
logger.error(error);
throw new CDKException(error, exc);
}
chemFile.addChemSequence(chemSequence);
return chemFile;
}
/**
* Read a Molecule from a file in MDL sd format
*
*@return The Molecule that was read from the MDL file.
*/
private IMolecule readMolecule(IMolecule molecule) throws CDKException {
logger.debug("Reading new molecule");
int linecount = 0;
int atoms = 0;
int bonds = 0;
int atom1 = 0;
int atom2 = 0;
int order = 0;
IBond.Stereo stereo = (IBond.Stereo)CDKConstants.UNSET;
int RGroupCounter=1;
int Rnumber=0;
String [] rGroup=null;
double x = 0.0;
double y = 0.0;
double z = 0.0;
double totalX = 0.0;
double totalY = 0.0;
double totalZ = 0.0;
//int[][] conMat = new int[0][0];
//String help;
IAtom atom;
String line = "";
//A map to keep track of R# atoms so that RGP line can be parsed
Map<Integer,IPseudoAtom> rAtoms = new HashMap<Integer,IPseudoAtom>();
try {
IsotopeFactory isotopeFactory = IsotopeFactory.getInstance(molecule.getBuilder());
logger.info("Reading header");
line = input.readLine(); linecount++;
if (line == null) {
return null;
}
logger.debug("Line " + linecount + ": " + line);
if (line.startsWith("$$$$")) {
logger.debug("File is empty, returning empty molecule");
return molecule;
}
if (line.length() > 0) {
molecule.setProperty(CDKConstants.TITLE, line);
}
line = input.readLine(); linecount++;
logger.debug("Line " + linecount + ": " + line);
line = input.readLine(); linecount++;
logger.debug("Line " + linecount + ": " + line);
if (line.length() > 0) {
molecule.setProperty(CDKConstants.REMARK, line);
}
logger.info("Reading rest of file");
line = input.readLine(); linecount++;
logger.debug("Line " + linecount + ": " + line);
// if the line is empty we hav a problem - either a malformed
// molecule entry or just extra new lines at the end of the file
if (line.length() == 0) {
// read till the next $$$$ or EOF
while (true) {
line = input.readLine(); linecount++;
if (line == null) {
return null;
}
if (line.startsWith("$$$$")) {
return molecule; // an empty molecule
}
}
}
// check the CT block version
if (line.contains("V3000") || line.contains("v3000")) {
handleError("This file must be read with the MDLV3000Reader.");
} else if (!line.contains("V2000") && !line.contains("v2000")) {
handleError("This file must be read with the MDLReader.");
}
atoms = Integer.parseInt(line.substring(0, 3).trim());
logger.debug("Atomcount: " + atoms);
bonds = Integer.parseInt(line.substring(3, 6).trim());
logger.debug("Bondcount: " + bonds);
// read ATOM block
logger.info("Reading atom block");
atomsByLinePosition = new ArrayList<IAtom>();
atomsByLinePosition.add(null); // 0 is not a valid position
int atomBlockLineNumber=0;
for (int f = 0; f < atoms; f++) {
line = input.readLine(); linecount++; atomBlockLineNumber++;
x = Double.parseDouble(line.substring(0, 10).trim());
y = Double.parseDouble(line.substring(10, 20).trim());
z = Double.parseDouble(line.substring(20, 30).trim());
// *all* values should be zero, not just the sum
totalX += Math.abs(x);
totalY += Math.abs(y);
totalZ += Math.abs(z);
logger.debug("Coordinates: " + x + "; " + y + "; " + z);
String element = line.substring(31,34).trim();
logger.debug("Atom type: ", element);
if (isotopeFactory.isElement(element)) {
atom = isotopeFactory.configure(molecule.getBuilder().newInstance(IAtom.class,element));
} else if ("A".equals(element)) {
atom = molecule.getBuilder().newInstance(IPseudoAtom.class,element);
} else if ("Q".equals(element)) {
atom = molecule.getBuilder().newInstance(IPseudoAtom.class,element);
} else if ("*".equals(element)) {
atom = molecule.getBuilder().newInstance(IPseudoAtom.class,element);
} else if ("LP".equals(element)) {
atom = molecule.getBuilder().newInstance(IPseudoAtom.class,element);
} else if ("L".equals(element)) {
atom = molecule.getBuilder().newInstance(IPseudoAtom.class,element);
} else if ( element.equals("R") ||
(element.length() > 0 && element.charAt(0) == 'R')){
logger.debug("Atom ", element, " is not an regular element. Creating a PseudoAtom.");
//check if the element is R
rGroup=element.split("^R");
atom=null;
if (rGroup.length >1){
try{
Rnumber= Integer.valueOf(rGroup[(rGroup.length - 1)]);
RGroupCounter=Rnumber;
element="R"+Rnumber;
atom = molecule.getBuilder().newInstance(IPseudoAtom.class, element);
}catch(Exception ex){
// This happens for atoms labeled "R#".
// The Rnumber may be set later on, using RGP line
atom = molecule.getBuilder().newInstance(IPseudoAtom.class, "R");
rAtoms.put(atomBlockLineNumber,(IPseudoAtom)atom);
}
}
else {
atom = molecule.getBuilder().newInstance(IPseudoAtom.class,"R");
}
} else {
handleError(
"Invalid element type. Must be an existing " +
"element, or one in: A, Q, L, LP, *.",
linecount, 32, 35
);
atom = molecule.getBuilder().newInstance(IPseudoAtom.class,element);
}
// store as 3D for now, convert to 2D (if totalZ == 0.0) later
atom.setPoint3d(new Point3d(x, y, z));
// parse further fields
String massDiffString = line.substring(34,36).trim();
logger.debug("Mass difference: ", massDiffString);
if (!(atom instanceof IPseudoAtom)) {
try {
int massDiff = Integer.parseInt(massDiffString);
if (massDiff != 0) {
IIsotope major = IsotopeFactory.getInstance(molecule.getBuilder()).getMajorIsotope(element);
atom.setMassNumber(major.getMassNumber() + massDiff);
}
} catch (Exception exception) {
handleError(
"Could not parse mass difference field.",
linecount, 35, 37,
exception
);
}
} else {
logger.error("Cannot set mass difference for a non-element!");
}
if (line.length() >= 51) {
String valenceString = removeNonDigits(line.substring(48,51));
logger.debug("Valence: ", valenceString);
if (!(atom instanceof IPseudoAtom)) {
try {
int valence = Integer.parseInt(valenceString);
if (valence != 0) {
//15 is defined as 0 in mol files
if(valence==15)
atom.setValency(0);
else
atom.setValency(valence);
}
} catch (Exception exception) {
handleError(
"Could not parse valence information field",
linecount, 49, 52,
exception
);
}
} else {
logger.error("Cannot set valence information for a non-element!");
}
}
String chargeCodeString = line.substring(36,39).trim();
logger.debug("Atom charge code: ", chargeCodeString);
int chargeCode = Integer.parseInt(chargeCodeString);
if (chargeCode == 0) {
// uncharged species
} else if (chargeCode == 1) {
atom.setFormalCharge(+3);
} else if (chargeCode == 2) {
atom.setFormalCharge(+2);
} else if (chargeCode == 3) {
atom.setFormalCharge(+1);
} else if (chargeCode == 4) {
} else if (chargeCode == 5) {
atom.setFormalCharge(-1);
} else if (chargeCode == 6) {
atom.setFormalCharge(-2);
} else if (chargeCode == 7) {
atom.setFormalCharge(-3);
}
try {
// read the mmm field as position 61-63
String reactionAtomIDString = line.substring(60,63).trim();
logger.debug("Parsing mapping id: ", reactionAtomIDString);
try {
int reactionAtomID = Integer.parseInt(reactionAtomIDString);
if (reactionAtomID != 0) {
atom.setProperty(CDKConstants.ATOM_ATOM_MAPPING, reactionAtomID);
}
} catch (Exception exception) {
logger.error("Mapping number ", reactionAtomIDString, " is not an integer.");
logger.debug(exception);
}
} catch (Exception exception) {
// older mol files don't have all these fields...
logger.warn("A few fields are missing. Older MDL MOL file?");
}
//shk3: This reads shifts from after the molecule. I don't think this is an official format, but I saw it frequently 80=>78 for alk
if(line.length()>=78){
double shift=Double.parseDouble(line.substring(69,80).trim());
atom.setProperty("first shift", shift);
}
if(line.length()>=87){
double shift=Double.parseDouble(line.substring(79,87).trim());
atom.setProperty("second shift", shift);
}
molecule.addAtom(atom);
atomsByLinePosition.add(atom);
}
// convert to 2D, if totalZ == 0
if (totalX == 0.0 && totalY == 0.0 && totalZ == 0.0) {
logger.info("All coordinates are 0.0");
if (molecule.getAtomCount()==1){
molecule.getAtom(0).setPoint2d(new Point2d(x,y));
}else{
for (IAtom atomToUpdate : molecule.atoms()) {
atomToUpdate.setPoint3d(null);
}
}
} else if (totalZ == 0.0 && !forceReadAs3DCoords.isSet()) {
logger.info("Total 3D Z is 0.0, interpreting it as a 2D structure");
Iterator<IAtom> atomsToUpdate = molecule.atoms().iterator();
while (atomsToUpdate.hasNext()) {
IAtom atomToUpdate = atomsToUpdate.next();
Point3d p3d = atomToUpdate.getPoint3d();
if (p3d != null) {
atomToUpdate.setPoint2d(new Point2d(p3d.x, p3d.y));
atomToUpdate.setPoint3d(null);
}
}
}
// read BOND block
logger.info("Reading bond block");
for (int f = 0; f < bonds; f++) {
line = input.readLine(); linecount++;
atom1 = Integer.parseInt(line.substring(0, 3).trim());
atom2 = Integer.parseInt(line.substring(3, 6).trim());
order = Integer.parseInt(line.substring(6, 9).trim());
if (line.length() >= 12) {
int mdlStereo = line.length() > 12
? Integer.parseInt(line.substring(9, 12).trim())
: Integer.parseInt(line.substring(9).trim());
if (mdlStereo == 1) {
// MDL up bond
stereo = IBond.Stereo.UP;
} else if (mdlStereo == 6) {
// MDL down bond
stereo = IBond.Stereo.DOWN;
} else if (mdlStereo == 0) {
if (order == 2) {
// double bond stereo defined by coordinates
stereo = IBond.Stereo.E_Z_BY_COORDINATES;
} else {
// bond has no stereochemistry
stereo = IBond.Stereo.NONE;
}
} else if (mdlStereo == 3 && order == 2) {
// unknown E/Z stereochemistry
stereo = IBond.Stereo.E_OR_Z;
} else if (mdlStereo == 4) {
//MDL bond undefined
stereo = IBond.Stereo.UP_OR_DOWN;
}
} else {
handleError(
"Missing expected stereo field at line: ",
linecount, 10, 12
);
}
if (logger.isDebugEnabled()) {
logger.debug("Bond: " + atom1 + " - " + atom2 + "; order " + order);
}
// interpret CTfile's special bond orders
IAtom a1 = molecule.getAtom(atom1 - 1);
IAtom a2 = molecule.getAtom(atom2 - 1);
IBond newBond = null;
if (order >= 1 && order <= 3) {
IBond.Order cdkOrder = IBond.Order.SINGLE;
if (order == 2) cdkOrder = IBond.Order.DOUBLE;
if (order == 3) cdkOrder = IBond.Order.TRIPLE;
if (stereo != null) {
newBond = molecule.getBuilder().newInstance(IBond.class,a1, a2, cdkOrder, stereo);
} else {
newBond = molecule.getBuilder().newInstance(IBond.class,a1, a2, cdkOrder);
}
} else if (order == 4) {
// aromatic bond
if (stereo != null) {
newBond = molecule.getBuilder().newInstance(IBond.class,a1, a2, IBond.Order.SINGLE, stereo);
} else {
newBond = molecule.getBuilder().newInstance(IBond.class,a1, a2, IBond.Order.SINGLE);
}
// mark both atoms and the bond as aromatic
newBond.setFlag(CDKConstants.ISAROMATIC, true);
a1.setFlag(CDKConstants.ISAROMATIC, true);
a2.setFlag(CDKConstants.ISAROMATIC, true);
}
else {
throw new CDKException
("Detected 'query bond type ' (value="+order +")."+
" Could not create regular molecule.");
}
molecule.addBond(newBond);
}
// read PROPERTY block
logger.info("Reading property block");
while (true) {
line = input.readLine(); linecount++;
if (line == null) {
handleError(
"The expected property block is missing!",
linecount, 0, 0
);
}
if (line.startsWith("M END")) break;
boolean lineRead = false;
if (line.startsWith("M CHG")) {
// FIXME: if this is encountered for the first time, all
// atom charges should be set to zero first!
int infoCount = Integer.parseInt(line.substring(6,9).trim());
StringTokenizer st = new StringTokenizer(line.substring(9));
for (int i=1; i <= infoCount; i++) {
String token = st.nextToken();
int atomNumber = Integer.parseInt(token.trim());
token = st.nextToken();
int charge = Integer.parseInt(token.trim());
molecule.getAtom(atomNumber - 1).setFormalCharge(charge);
}
} else if (line.matches("^A \\d+")) {
// Reads the pseudo atom property from the mol file
// The atom number of the to replaced atom
int aliasAtomNumber = Integer.parseInt(line.replaceFirst("^A ", "")) - RGroupCounter;
line = input.readLine(); linecount++;
String[] aliasArray = line.split("\\\\");
// name of the alias atom like R1 odr R2 etc.
String alias = "";
for (int i = 0; i < aliasArray.length; i++) {
alias += aliasArray[i];
}
IAtom aliasAtom = molecule.getAtom(aliasAtomNumber);
IAtom newPseudoAtom = molecule.getBuilder().newInstance(IPseudoAtom.class,alias);
if(aliasAtom.getPoint2d() != null) {
newPseudoAtom.setPoint2d(aliasAtom.getPoint2d());
}
if(aliasAtom.getPoint3d() != null) {
newPseudoAtom.setPoint3d(aliasAtom.getPoint3d());
}
molecule.addAtom(newPseudoAtom);
List<IBond> bondsOfAliasAtom = molecule.getConnectedBondsList(aliasAtom);
for (int i = 0; i < bondsOfAliasAtom.size(); i++) {
IBond bondOfAliasAtom = bondsOfAliasAtom.get(i);
IAtom connectedToAliasAtom = bondOfAliasAtom.getConnectedAtom(aliasAtom);
IBond newBond = bondOfAliasAtom.getBuilder().newInstance(IBond.class);
newBond.setAtoms(new IAtom[] {connectedToAliasAtom, newPseudoAtom});
newBond.setOrder(bondOfAliasAtom.getOrder());
molecule.addBond(newBond);
molecule.removeBond(aliasAtom, connectedToAliasAtom);
}
molecule.removeAtom(aliasAtom);
RGroupCounter++;
} else if (line.startsWith("M ISO")) {
try {
String countString = line.substring(6,10).trim();
int infoCount = Integer.parseInt(countString);
StringTokenizer st = new StringTokenizer(line.substring(10));
for (int i=1; i <= infoCount; i++) {
int atomNumber = Integer.parseInt(st.nextToken().trim());
int absMass = Integer.parseInt(st.nextToken().trim());
if (absMass != 0) {
IAtom isotope = molecule.getAtom(atomNumber - 1);
isotope.setMassNumber(absMass);
}
}
} catch (NumberFormatException exception) {
String error = "Error (" + exception.getMessage() + ") while parsing line "
+ linecount + ": " + line + " in property block.";
logger.error(error);
handleError(
"NumberFormatException in isotope information.",
linecount, 7, 11,
exception
);
}
} else if (line.startsWith("M RAD")) {
try {
String countString = line.substring(6,9).trim();
int infoCount = Integer.parseInt(countString);
StringTokenizer st = new StringTokenizer(line.substring(9));
for (int i=1; i <= infoCount; i++) {
int atomNumber = Integer.parseInt(st.nextToken().trim());
int spinMultiplicity = Integer.parseInt(st.nextToken().trim());
if (spinMultiplicity > 1) {
IAtom radical = molecule.getAtom(atomNumber - 1);
for (int j=2; j <= spinMultiplicity; j++) {
// 2 means doublet -> one unpaired electron
// 3 means triplet -> two unpaired electron
molecule.addSingleElectron(molecule.getBuilder().newInstance(ISingleElectron.class,radical));
}
}
}
} catch (NumberFormatException exception) {
String error = "Error (" + exception.getMessage() + ") while parsing line "
+ linecount + ": " + line + " in property block.";
logger.error(error);
handleError(
"NumberFormatException in radical information",
linecount, 7, 10,
exception
);
}
} else if (line.startsWith("G ")) {
try {
String atomNumberString = line.substring(3,6).trim();
int atomNumber = Integer.parseInt(atomNumberString);
//String whatIsThisString = line.substring(6,9).trim();
String atomName = input.readLine();
// convert Atom into a PseudoAtom
IAtom prevAtom = molecule.getAtom(atomNumber - 1);
IPseudoAtom pseudoAtom = molecule.getBuilder().newInstance(IPseudoAtom.class,atomName);
if (prevAtom.getPoint2d() != null) {
pseudoAtom.setPoint2d(prevAtom.getPoint2d());
}
if (prevAtom.getPoint3d() != null) {
pseudoAtom.setPoint3d(prevAtom.getPoint3d());
}
AtomContainerManipulator.replaceAtomByAtom(molecule, prevAtom, pseudoAtom);
} catch (NumberFormatException exception) {
String error = "Error (" + exception.toString() + ") while parsing line "
+ linecount + ": " + line + " in property block.";
logger.error(error);
handleError(
"NumberFormatException in group information",
linecount, 4, 7,
exception
);
}
} else if (line.startsWith("M RGP")) {
StringTokenizer st = new StringTokenizer(line);
//Ignore first 3 tokens (overhead).
st.nextToken(); st.nextToken(); st.nextToken();
//Process the R group numbers as defined in RGP line.
while (st.hasMoreTokens()) {
Integer position = new Integer(st.nextToken());
Rnumber = new Integer(st.nextToken());
IPseudoAtom pseudoAtom = rAtoms.get(position);
if (pseudoAtom!=null) {
pseudoAtom.setLabel("R"+Rnumber);
}
}
}
if (line.startsWith("V ")) {
Integer atomNumber = new Integer(line.substring(3,6).trim());
IAtom atomWithComment = molecule.getAtom(atomNumber - 1);
atomWithComment.setProperty(CDKConstants.COMMENT, line.substring(7));
}
if (!lineRead) {
logger.warn("Skipping line in property block: ", line);
}
}
if (interpretHydrogenIsotopes.isSet()) {
fixHydrogenIsotopes(molecule, isotopeFactory);
}
} catch (CDKException exception) {
String error = "Error while parsing line " + linecount + ": " + line + " -> " + exception.getMessage();
logger.error(error);
logger.debug(exception);
throw exception;
} catch (Exception exception) {
exception.printStackTrace();
String error = "Error while parsing line " + linecount + ": " + line + " -> " + exception.getMessage();
logger.error(error);
logger.debug(exception);
handleError(
"Error while parsing line: " + line,
linecount, 0, 0,
exception
);
}
return molecule;
}
private void fixHydrogenIsotopes(IMolecule molecule,IsotopeFactory isotopeFactory) {
Iterator<IAtom> atoms = molecule.atoms().iterator();
while (atoms.hasNext()) {
IAtom atom = atoms.next();
if (atom instanceof IPseudoAtom) {
IPseudoAtom pseudo = (IPseudoAtom)atom;
if ("D".equals(pseudo.getLabel())) {
IAtom newAtom = molecule.getBuilder().newInstance(IAtom.class,atom);
newAtom.setSymbol("H");
IIsotope isotope = molecule.getBuilder().newInstance(IIsotope.class,"H", 2);
isotopeFactory.configure(newAtom, isotope);
AtomContainerManipulator.replaceAtomByAtom(molecule, atom, newAtom);
} else if ("T".equals(pseudo.getLabel())) {
IAtom newAtom = molecule.getBuilder().newInstance(IAtom.class,atom);
newAtom.setSymbol("H");
IIsotope isotope = molecule.getBuilder().newInstance(IIsotope.class,"H", 3);
isotopeFactory.configure(newAtom, isotope);
AtomContainerManipulator.replaceAtomByAtom(molecule, atom, newAtom);
}
}
}
}
@TestMethod("testClose")
public void close() throws IOException {
input.close();
}
private void initIOSettings() {
forceReadAs3DCoords = new BooleanIOSetting("ForceReadAs3DCoordinates", IOSetting.LOW,
"Should coordinates always be read as 3D?",
"false");
interpretHydrogenIsotopes = new BooleanIOSetting("InterpretHydrogenIsotopes", IOSetting.LOW,
"Should D and T be interpreted as hydrogen isotopes?",
"true");
}
public void customizeJob() {
fireIOSettingQuestion(forceReadAs3DCoords);
fireIOSettingQuestion(interpretHydrogenIsotopes);
}
public IOSetting[] getIOSettings() {
IOSetting[] settings = new IOSetting[2];
settings[0] = forceReadAs3DCoords;
settings[1] = interpretHydrogenIsotopes;
return settings;
}
public List<IAtom> getAtomsByLinePosition() {
return atomsByLinePosition;
}
private String removeNonDigits(String input) {
StringBuffer buffer = new StringBuffer();
for (int i=0; i<input.length(); i++) {
char inputChar = input.charAt(i);
if (Character.isDigit(inputChar))
buffer.append(inputChar);
}
return buffer.toString();
}
}