Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: joelib/algo/morgan/MorganTest.java


1   ///////////////////////////////////////////////////////////////////////////////
2   //  Filename: $RCSfile: MorganTest.java,v $
3   //  Purpose:  Unique molecule numbering test.
4   //  Language: Java
5   //  Compiler: JDK 1.4
6   //  Authors:  Joerg K. Wegner
7   //  Version:  $Revision: 1.11 $
8   //            $Date: 2003/08/22 15:56:15 $
9   //            $Author: wegner $
10  //
11  //  Copyright (c) Dept. Computer Architecture, University of Tuebingen, Germany
12  //
13  //  This program is free software; you can redistribute it and/or modify
14  //  it under the terms of the GNU General Public License as published by
15  //  the Free Software Foundation version 2 of the License.
16  //
17  //  This program is distributed in the hope that it will be useful,
18  //  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  //  GNU General Public License for more details.
21  ///////////////////////////////////////////////////////////////////////////////
22  package joelib.algo.morgan;
23  
24  import joelib.algo.morgan.types.BasicTieResolver;
25  
26  import joelib.io.IOType;
27  import joelib.io.IOTypeHolder;
28  import joelib.io.SimpleReader;
29  
30  import joelib.molecule.JOEAtom;
31  import joelib.molecule.JOEMol;
32  
33  import wsi.ra.tool.ResourceLoader;
34  
35  /*==========================================================================*
36   * IMPORTS
37   *==========================================================================  */
38  import java.io.ByteArrayInputStream;
39  import java.io.IOException;
40  
41  import org.apache.log4j.Category;
42  
43  
44  /*==========================================================================*
45   * CLASS DECLARATION
46   *==========================================================================  */
47  
48  /**
49   * Unique molecule numbering test.
50   *
51   * @author     wegnerj
52   * @license    GPL
53   * @cvsversion    $Revision: 1.11 $, $Date: 2003/08/22 15:56:15 $
54   */
55  public class MorganTest
56  {
57      //~ Static fields/initializers /////////////////////////////////////////////
58  
59      /*-------------------------------------------------------------------------*
60       * private static member variables
61       *-------------------------------------------------------------------------  */
62  
63      // Obtain a suitable logger.
64      private static Category logger = Category.getInstance(
65              "joelib.algo.morgan.MorganTest");
66  
67      //~ Methods ////////////////////////////////////////////////////////////////
68  
69      /*-------------------------------------------------------------------------*
70       * main
71       *-------------------------------------------------------------------------  */
72  
73      /**
74       *  The main program for the TestSmarts class
75       *
76       * @param  args  The command line arguments
77       */
78      public static void main(String[] args)
79      {
80          MorganTest morgan = new MorganTest();
81  
82          if (args.length != 1)
83          {
84              morgan.usage();
85              System.exit(0);
86          }
87          else
88          {
89              //        String molURL = new String("joelib/test/test.mol");
90              morgan.test(args[0], IOTypeHolder.instance().getIOType("SDF"),
91                  IOTypeHolder.instance().getIOType("SDF"));
92          }
93      }
94  
95      /**
96       *  A unit test for JUnit
97       *
98       * @param  molURL   Description of the Parameter
99       * @param  inType   Description of the Parameter
100      * @param  outType  Description of the Parameter
101      */
102     public void test(String molURL, IOType inType, IOType outType)
103     {
104         // get molecules from resource URL
105         byte[] bytes = ResourceLoader.instance().getBytesFromResourceLocation(molURL);
106 
107         if (bytes == null)
108         {
109             logger.error("Molecule can't be loaded at \"" + molURL + "\".");
110             System.exit(1);
111         }
112 
113         ByteArrayInputStream sReader = new ByteArrayInputStream(bytes);
114 
115         // create simple reader
116         SimpleReader reader = null;
117 
118         try
119         {
120             reader = new SimpleReader(sReader, inType);
121         }
122          catch (IOException e)
123         {
124             e.printStackTrace();
125             System.exit(1);
126         }
127 
128         // load molecules and handle test
129         JOEMol mol = new JOEMol(inType, outType);
130         JOEMol renumberedMol;
131         Morgan morgan = new Morgan(new BasicTieResolver());
132 
133         for (;;)
134         {
135             try
136             {
137                 if (!reader.readNext(mol))
138                 {
139                     break;
140                 }
141             }
142              catch (Exception ex)
143             {
144                 ex.printStackTrace();
145                 System.exit(1);
146             }
147 
148             System.out.println("--------------------------------------");
149             mol.deleteHydrogens();
150 
151             //logger.info("Processing (atoms="+mol.numAtoms()+"):" + mol.getTitle());
152             //System.out.println("Hashcode:" + getHashcode(mol));
153             //System.out.println("Molecule before renumbering:");
154             //System.out.println(mol);
155             if (morgan.calculate(mol))
156             {
157                 renumberedMol = morgan.renumber(mol);
158 
159                 //System.out.println("Molecule after renumbering:");
160                 //System.out.println(mol);
161                 String status;
162                 String statusSMILES;
163 
164                 if (morgan.tieResolvingProblem())
165                 {
166                     status = "Unsure";
167                     statusSMILES = "Nearly Unique/Canonical";
168                 }
169                 else
170                 {
171                     status = "Sure";
172                     statusSMILES = "Unique/Canonical";
173                 }
174 
175                 System.out.println(status +
176                     " hashcode(without E/Z and S/R) for " + mol.getTitle() +
177                     ": " + getHashcode(renumberedMol));
178                 System.out.print("Basic SMILES: " +
179                     mol.toString(IOTypeHolder.instance().getIOType("SMILES")));
180                 System.out.print(statusSMILES + " SMILES: " +
181                     renumberedMol.toString(IOTypeHolder.instance().getIOType("SMILES")));
182             }
183             else
184             {
185                 System.out.println("");
186             }
187 
188             //System.out.println(mol.toString(IOTypeHolder.instance().getIOType("SDF")));
189         }
190     }
191 
192     /*-------------------------------------------------------------------------*
193      * public methods
194      *-------------------------------------------------------------------------  */
195 
196     /**
197      *  Description of the Method
198      */
199     public void usage()
200     {
201         StringBuffer sb = new StringBuffer();
202         String programName = this.getClass().getName();
203 
204         sb.append("Usage is : ");
205         sb.append("java -cp . ");
206         sb.append(programName);
207 
208         System.out.println(sb.toString());
209 
210         System.exit(0);
211     }
212 
213     /**
214      * Primitive hashcode method without chirality and cis/trans.
215      *
216      * @param mol
217      * @return int
218      */
219     private int getHashcode(JOEMol mol)
220     {
221         int hash = mol.numRotors();
222         JOEAtom atom;
223 
224         // take number of rings into account
225         hash = (31 * hash) + mol.getSSSR().size();
226 
227         for (int i = 1; i <= mol.numAtoms(); i++)
228         {
229             atom = mol.getAtom(i);
230 
231             hash = (31 * hash) + atom.getIdx();
232             hash = (31 * hash) + atom.getAtomicNum();
233             hash = (31 * hash) + atom.getHvyValence();
234             hash = (31 * hash) + atom.getImplicitValence();
235         }
236 
237         return hash;
238     }
239 }
240 ///////////////////////////////////////////////////////////////////////////////
241 //  END OF FILE.
242 ///////////////////////////////////////////////////////////////////////////////