1 /* $RCSfile$
2  * $Author: egonw $
3  * $Date: 2005-11-10 09:52:44 -0600 (Thu, 10 Nov 2005) $
4  * $Revision: 4255 $
5  *
6  * Copyright (C) 2004-2005  The Jmol Development Team
7  *
8  * Contact: jmol-developers@lists.sf.net
9  *
10  *  This library is free software; you can redistribute it and/or
11  *  modify it under the terms of the GNU Lesser General Public
12  *  License as published by the Free Software Foundation; either
13  *  version 2.1 of the License, or (at your option) any later version.
14  *
15  *  This library is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  *  Lesser General Public License for more details.
19  *
20  *  You should have received a copy of the GNU Lesser General Public
21  *  License along with this library; if not, write to the Free Software
22  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
23  */
24 
25 package org.jmol.adapter.readers.simple;
26 
27 import org.jmol.adapter.smarter.AtomSetCollectionReader;
28 import org.jmol.adapter.smarter.Atom;
29 
30 import org.jmol.api.JmolAdapter;
31 
32 /**
33  * Support for .hin, HyperChem's native file format.
34  * http://www.hyper.com
35  * <p />
36  * Record format is:
37  * <code>
38  * atom 1 - C ** - -0.06040 0.00000 0.00000 0.00000 3 2 a 6 a 38 s
39  * ...
40  * atom 67 - H ** - 0.17710 -7.10260 -3.74840 2.24660 1 34 s
41  * endmol 1
42  * </code>
43  * interesting fields are partialCharge, x, y, z, bondCount<br />
44  * bonds are atom number and s/d/t/a for single/double/triple/aromatic
45  */
46 public class HyperChemReader extends AtomSetCollectionReader {
47 
48   @Override
checkLine()49   protected boolean checkLine() throws Exception {
50     if (line.length() == 0 || line.charAt(0) == ';') // comment
51       return true;
52     if (line.startsWith("mol ")) {
53       // we have reached the start of a molecule
54       if (!doGetModel(++modelNumber, null))
55         return checkLastModel();
56       processMol();
57       return true;
58     }
59     if (!doProcessLines)
60       return true;
61 
62     if (line.startsWith("atom ")) {
63       processAtom();
64       return true;
65     }
66     if (line.startsWith("endmol ")) {
67       applySymmetryAndSetTrajectory();
68       return true;
69     }
70     return true;
71   }
72 
73   private int atomIndex;
74 
processMol()75   private void processMol() throws Exception {
76     asc.newAtomSet();
77     String molName = getMolName();
78     asc.setAtomSetName(molName);
79     atomIndex = 0;
80     baseAtomIndex = asc.ac;
81   }
82 
getMolName()83   private String getMolName() {
84     parseTokenStr(line);
85     parseToken();
86     return parseToken();
87   }
88 
processAtom()89   private void processAtom() throws Exception {
90 
91     int fileAtomNumber = parseIntAt(line, 5);
92     if (fileAtomNumber - 1 != atomIndex) {
93       throw new Exception ("bad atom number sequence ... expected:" +
94         (atomIndex + 1) + " found:" + fileAtomNumber);
95     }
96     Atom atom = asc.addNewAtom();
97     parseToken(); // discard
98     atom.elementSymbol = parseToken();
99     parseToken(); // discard
100     parseToken(); // discard
101     atom.partialCharge = parseFloat();
102     setAtomCoordXYZ(atom, parseFloat(), parseFloat(), parseFloat());
103 
104     int bondCount = parseInt();
105     for (int i = 0; i < bondCount; ++i) {
106       int otherAtomNumber = parseInt();
107       String bondTypeToken = parseToken();
108       if (otherAtomNumber > atomIndex)
109         continue;
110       int bondOrder;
111       switch(bondTypeToken.charAt(0)) {
112       case 's':
113         bondOrder = 1;
114         break;
115       case 'd':
116         bondOrder = 2;
117         break;
118       case 't':
119         bondOrder = 3;
120         break;
121       case 'a':
122         bondOrder = JmolAdapter.ORDER_AROMATIC;
123         break;
124       default:
125         throw new Exception ("unrecognized bond type:" + bondTypeToken +
126           " atom #" + fileAtomNumber);
127       }
128       asc.addNewBondWithOrder(baseAtomIndex + atomIndex,
129                        baseAtomIndex + otherAtomNumber - 1,
130                        bondOrder);
131     }
132     ++atomIndex;
133   }
134 
135 }
136