Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge branch 'master' of ssh://cdk.git.sourceforge.net/gitroot/cdk/cdk

  • Loading branch information...
commit eb6defd23f8afd978db3e3f71cdabeffc6813017 2 parents 2896226 + a586d6a
Egon Willighagen authored
32  src/main/org/openscience/cdk/io/MDLWriter.java
@@ -86,6 +86,9 @@
86 86
         LoggingToolFactory.createLoggingTool(MDLWriter.class);
87 87
 
88 88
     private BooleanIOSetting forceWriteAs2DCoords;
  89
+    
  90
+    /* Should aromatic bonds be written as bond type 4? If true, this makes the output a query file. */
  91
+    private BooleanIOSetting writeAromaticBondTypes;
89 92
 
90 93
     private BufferedWriter writer;
91 94
     
@@ -324,7 +327,7 @@ else if(atom.getValency()==0)
324 327
         // write Bond block
325 328
         Iterator<IBond> bonds = container.bonds().iterator();
326 329
         while (bonds.hasNext()) {
327  
-            IBond bond = (IBond) bonds.next();
  330
+            IBond bond = bonds.next();
328 331
 
329 332
         	if (bond.getAtomCount() != 2) {
330 333
         		logger.warn("Skipping bond with more/less than two atoms: " + bond);
@@ -339,7 +342,13 @@ else if(atom.getValency()==0)
339 342
         			line = formatMDLInt(container.getAtomNumber(bond.getAtom(0)) + 1,3);
340 343
         			line += formatMDLInt(container.getAtomNumber(bond.getAtom(1)) + 1,3);
341 344
         		}
342  
-        		line += formatMDLInt((int)bond.getOrder().ordinal()+1,3);
  345
+                        int bondType;
  346
+                        if (writeAromaticBondTypes.isSet() && bond.getFlag(CDKConstants.ISAROMATIC))
  347
+                            bondType=4;
  348
+                        else
  349
+                            bondType=(int)bond.getOrder().ordinal()+1;
  350
+                        line += formatMDLInt(bondType,3);
  351
+                            
343 352
         		line += "  ";
344 353
         		switch(bond.getStereo()){
345 354
         		case UP:
@@ -513,7 +522,12 @@ protected static String formatMDLString(String s, int le) {
513 522
             s += " ";
514 523
         return s;
515 524
     }
516  
-
  525
+    
  526
+    /**
  527
+     * Initializes IO settings.<br>
  528
+     * Please note with regards to "writeAromaticBondTypes": bond type values 4 through 8 are for SSS queries only,
  529
+     * so a 'query file' is created if the container has aromatic bonds and this settings is true.
  530
+     */
517 531
     private void initIOSettings() {
518 532
         forceWriteAs2DCoords = new BooleanIOSetting(
519 533
             "ForceWriteAs2DCoordinates",
@@ -521,18 +535,26 @@ private void initIOSettings() {
521 535
             "Should coordinates always be written as 2D?",
522 536
             "false"
523 537
         );
  538
+        writeAromaticBondTypes = new BooleanIOSetting(
  539
+            "WriteAromaticBondTypes",
  540
+            IOSetting.LOW,
  541
+            "Should aromatic bonds be written as bond type 4?",
  542
+            "false"
  543
+        );
524 544
     }
525 545
 
526 546
     public void customizeJob() {
527 547
         fireIOSettingQuestion(forceWriteAs2DCoords);
  548
+        fireIOSettingQuestion(writeAromaticBondTypes);
  549
+
528 550
     }
529 551
 
530 552
     public IOSetting[] getIOSettings() {
531  
-        IOSetting[] settings = new IOSetting[1];
  553
+        IOSetting[] settings = new IOSetting[2];
532 554
         settings[0] = forceWriteAs2DCoords;
  555
+        settings[1] = writeAromaticBondTypes;
533 556
         return settings;
534 557
     }
535  
-
536 558
 }
537 559
 
538 560
 
34  src/test/org/openscience/cdk/io/MDLWriterTest.java
@@ -24,6 +24,7 @@
24 24
 package org.openscience.cdk.io;
25 25
 
26 26
 import java.io.StringWriter;
  27
+
27 28
 import java.util.Properties;
28 29
 
29 30
 import javax.vecmath.Point2d;
@@ -32,6 +33,7 @@
32 33
 import org.junit.Assert;
33 34
 import org.junit.BeforeClass;
34 35
 import org.junit.Test;
  36
+
35 37
 import org.openscience.cdk.Atom;
36 38
 import org.openscience.cdk.AtomContainer;
37 39
 import org.openscience.cdk.CDKConstants;
@@ -49,8 +51,11 @@
49 51
 import org.openscience.cdk.interfaces.IMoleculeSet;
50 52
 import org.openscience.cdk.interfaces.IPseudoAtom;
51 53
 import org.openscience.cdk.io.listener.PropertiesListener;
  54
+import org.openscience.cdk.nonotify.NoNotificationChemObjectBuilder;
  55
+import org.openscience.cdk.smiles.SmilesParser;
52 56
 import org.openscience.cdk.templates.MoleculeFactory;
53 57
 
  58
+
54 59
 /**
55 60
  * TestCase for the writer MDL mol files using one test file.
56 61
  *
@@ -297,11 +302,38 @@
297 302
         StringWriter writer = new StringWriter();
298 303
         MDLWriter mdlWriter = new MDLWriter(writer);
299 304
         mdlWriter.write(molecule);
300  
-        System.out.println(writer.toString());
301 305
         
302 306
         Assert.assertTrue(writer.toString().indexOf("V    1 Oxygen comment") != -1);
303 307
         Assert.assertTrue(writer.toString().indexOf("V    2 Carbon comment") != -1);
304 308
         
305 309
     }
  310
+    
  311
+    /**
  312
+     * Test option to write aromatic bonds with bond type "4".
  313
+     * Please note: bond type values 4 through 8 are for SSS queries only.
  314
+     * @throws Exception
  315
+     */
  316
+    @Test public void testAromaticBondType4() throws Exception {
  317
+        SmilesParser sp = new SmilesParser(NoNotificationChemObjectBuilder.getInstance());
  318
+        String smiles = "c1ccccc1";
  319
+        IMolecule benzene = sp.parseSmiles(smiles);
  320
+
306 321
 
  322
+        StringWriter writer = new StringWriter();
  323
+        MDLWriter mdlWriter = new MDLWriter(writer);
  324
+        mdlWriter.write(benzene);
  325
+        Assert.assertTrue(writer.toString().indexOf("2  1  1  0  0  0  0") != -1);
  326
+
  327
+
  328
+        writer = new StringWriter();
  329
+        mdlWriter = new MDLWriter(writer);
  330
+        Properties prop = new Properties();
  331
+        prop.setProperty("WriteAromaticBondTypes","true");
  332
+        PropertiesListener listener = new PropertiesListener(prop);
  333
+        mdlWriter.addChemObjectIOListener(listener);
  334
+        mdlWriter.customizeJob();
  335
+        mdlWriter.write(benzene);
  336
+        Assert.assertTrue(writer.toString().indexOf("2  1  4  0  0  0  0") != -1);
  337
+    }
  338
+    
307 339
 }

0 notes on commit eb6defd

Please sign in to comment.
Something went wrong with that request. Please try again.