~ubuntu-branches/ubuntu/maverick/cdk/maverick

« back to all changes in this revision

Viewing changes to src/org/openscience/cdk/config/TXTBasedAtomTypeConfigurator.java

  • Committer: Bazaar Package Importer
  • Author(s): Paul Cager
  • Date: 2008-04-09 21:17:53 UTC
  • Revision ID: james.westby@ubuntu.com-20080409211753-46lmjw5z8mx5pd8d
Tags: upstream-1.0.2
ImportĀ upstreamĀ versionĀ 1.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $RCSfile$    
 
2
 * $Author: egonw $    
 
3
 * $Date: 2007-01-04 18:46:10 +0100 (Thu, 04 Jan 2007) $    
 
4
 * $Revision: 7636 $
 
5
 *
 
6
 * Copyright (C) 1997-2007  Bradley A. Smith <bradley@baysmith.com>
 
7
 *
 
8
 * Contact: cdk-developers@lists.sourceforge.net
 
9
 *
 
10
 * This program is free software; you can redistribute it and/or
 
11
 * modify it under the terms of the GNU Lesser General Public License
 
12
 * as published by the Free Software Foundation; either version 2.1
 
13
 * of the License, or (at your option) any later version.
 
14
 *
 
15
 * This program 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
 
18
 * GNU Lesser General Public License for more details.
 
19
 *
 
20
 * You should have received a copy of the GNU Lesser General Public License
 
21
 * along with this program; if not, write to the Free Software
 
22
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 
23
 */
 
24
package org.openscience.cdk.config;
 
25
 
 
26
import java.awt.Color;
 
27
import java.io.BufferedReader;
 
28
import java.io.IOException;
 
29
import java.io.InputStream;
 
30
import java.io.InputStreamReader;
 
31
import java.util.ArrayList;
 
32
import java.util.List;
 
33
import java.util.StringTokenizer;
 
34
 
 
35
import org.openscience.cdk.interfaces.IAtomType;
 
36
import org.openscience.cdk.interfaces.IChemObjectBuilder;
 
37
 
 
38
/**
 
39
 * AtomType list configurator that uses the AtomTypes originally
 
40
 * defined in Jmol v5. This class was added to be able to port
 
41
 * Jmol to CDK. The AtomType's themselves seems have a computational
 
42
 * background, but this is not clear. 
 
43
 *
 
44
 * @cdk.module core
 
45
 *
 
46
 * @author     Bradley A. Smith <bradley@baysmith.com>
 
47
 *
 
48
 * @cdk.keyword    atom, type
 
49
 */
 
50
public class TXTBasedAtomTypeConfigurator implements IAtomTypeConfigurator {
 
51
 
 
52
    private String configFile = "org/openscience/cdk/config/data/jmol_atomtypes.txt";
 
53
    private InputStream ins = null;
 
54
    
 
55
        public TXTBasedAtomTypeConfigurator() {
 
56
        }
 
57
 
 
58
    /**
 
59
     * Sets the file containing the config data.
 
60
     */
 
61
    public void setInputStream(InputStream ins) {
 
62
        this.ins = ins;
 
63
    }
 
64
    
 
65
    /**
 
66
     * Reads a text based configuration file.
 
67
     * 
 
68
     * @param builder IChemObjectBuilder used to construct the IAtomType's.
 
69
     * @throws        IOException when a problem occured with reading from the InputStream
 
70
     * @return        A List with read IAtomType's.
 
71
     */
 
72
    public List readAtomTypes(IChemObjectBuilder builder) throws IOException {
 
73
        List atomTypes = new ArrayList();
 
74
 
 
75
        if (ins == null) {
 
76
            // trying the default
 
77
            //logger.debug("readAtomTypes getResourceAsStream:"
 
78
            //                   + configFile);
 
79
            ins = this.getClass().getClassLoader().getResourceAsStream(configFile);
 
80
        }
 
81
        if (ins == null) 
 
82
            throw new IOException("There was a problem getting the default stream: " + configFile);
 
83
 
 
84
        // read the contents from file
 
85
        BufferedReader reader = new BufferedReader(new InputStreamReader(ins), 1024);
 
86
        StringTokenizer tokenizer;
 
87
        String string;
 
88
        
 
89
            while (true) {
 
90
                string = reader.readLine();
 
91
                if (string == null) {
 
92
                    break;
 
93
                }
 
94
                if (!string.startsWith("#")) {
 
95
                    String name = "";
 
96
                    String rootType = "";
 
97
                    int atomicNumber = 0, colorR = 0, colorG = 0, colorB = 0;
 
98
                    double mass = 0.0, vdwaals = 0.0, covalent = 0.0;
 
99
                    tokenizer = new StringTokenizer(string, "\t ,;");
 
100
                    int tokenCount = tokenizer.countTokens();
 
101
                    
 
102
                    if (tokenCount == 9) {
 
103
                        name = tokenizer.nextToken();
 
104
                        rootType = tokenizer.nextToken();
 
105
                        String san = tokenizer.nextToken();
 
106
                        String sam = tokenizer.nextToken();
 
107
                        String svdwaals = tokenizer.nextToken();
 
108
                        String scovalent = tokenizer.nextToken();
 
109
                        String sColorR = tokenizer.nextToken();
 
110
                        String sColorG = tokenizer.nextToken();
 
111
                        String sColorB = tokenizer.nextToken();
 
112
                        
 
113
                        try {
 
114
                            mass = new Double(sam).doubleValue();
 
115
                            vdwaals = new Double(svdwaals).doubleValue();
 
116
                            covalent = new Double(scovalent).doubleValue();
 
117
                            atomicNumber = Integer.parseInt(san);
 
118
                            colorR = Integer.parseInt(sColorR);
 
119
                            colorG = Integer.parseInt(sColorG);
 
120
                            colorB = Integer.parseInt(sColorB);
 
121
                        } catch (NumberFormatException nfe) {
 
122
                            throw new IOException("AtomTypeTable.ReadAtypes: " +
 
123
                            "Malformed Number");
 
124
                        }
 
125
                        
 
126
                        IAtomType atomType = builder.newAtomType(name, rootType);
 
127
                        atomType.setAtomicNumber(atomicNumber);
 
128
                        atomType.setExactMass(mass);
 
129
                        atomType.setVanderwaalsRadius(vdwaals);
 
130
                        atomType.setCovalentRadius(covalent);
 
131
                        Color color = new Color(colorR, colorG, colorB);
 
132
                        atomType.setProperty("org.openscience.cdk.renderer.color", color);
 
133
                        atomTypes.add(atomType);
 
134
                    } else {
 
135
                        throw new IOException("AtomTypeTable.ReadAtypes: " + 
 
136
                        "Wrong Number of fields");
 
137
                    }
 
138
                }
 
139
            }    // end while
 
140
            ins.close();
 
141
        
 
142
        return atomTypes;
 
143
    }
 
144
}