rcdk 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -7,12 +7,9 @@ Java, from Ruby.
7
7
 
8
8
  require 'rubygems'
9
9
  require_gem 'rcdk'
10
+ require 'util'
10
11
 
11
- SmilesParser = import('org.openscience.cdk.smiles.SmilesParser')
12
-
13
- parser = SmilesParser.new
14
- smiles = 'c1ccccc1'
15
- mol = parser.parseSmiles(smiles)
12
+ mol = RCDK::Util::Lang.read_smiles('c1ccccc1')
16
13
 
17
14
  puts mol.getAtomCount # =>6
18
15
 
data/Rakefile CHANGED
@@ -26,23 +26,27 @@ require 'rake/testtask'
26
26
  require 'rake/rdoctask'
27
27
  require 'rake/gempackagetask'
28
28
 
29
- PKG_VERSION = "0.1.0"
29
+ PKG_VERSION = "0.2.0"
30
30
 
31
31
  PKG_FILES = FileList[
32
32
  "Rakefile", "README",
33
33
  "lib/**/*.rb",
34
34
  "test/**/*",
35
- "java/*.jar"
35
+ "java/lib/*.jar"
36
36
  ]
37
37
 
38
38
  desc "Default task"
39
39
  task :default => [:test]
40
40
 
41
+
42
+ task :dist => [:rdoc]
43
+
41
44
  desc "Clean up"
42
45
  task :clean do
43
46
  rm_rf "dist"
44
47
  rm_rf "doc"
45
48
  rm_rf "pkg"
49
+ rm_rf "output"
46
50
  end
47
51
 
48
52
  desc "Create the source distribution"
@@ -50,13 +54,17 @@ task :dist do
50
54
  rm_rf "dist"
51
55
 
52
56
  mkdir "dist"
57
+ mkdir "dist/doc"
53
58
  mkdir "dist/lib"
54
59
  mkdir "dist/java"
60
+ mkdir "dist/java/lib"
61
+ mkdir "dist/java/src"
55
62
  mkdir "dist/test"
56
63
 
64
+ mv Dir.glob('doc/*'), 'dist/doc'
57
65
  cp_r Dir.glob('*.rb'), 'dist'
58
66
  cp_r Dir.glob('lib/*.rb'), 'dist/lib'
59
- cp_r Dir.glob('java/*.jar'), 'dist/java'
67
+ cp_r Dir.glob('java/lib/*.jar'), 'dist/java/lib'
60
68
  cp_r Dir.glob('test/*.rb'), 'dist/test'
61
69
  cp 'Rakefile', 'dist'
62
70
  cp 'README', 'dist'
@@ -64,6 +72,10 @@ task :dist do
64
72
  end
65
73
 
66
74
  Rake::TestTask.new do |t|
75
+
76
+ rm_rf "output"
77
+ mkdir "output"
78
+
67
79
  t.libs << "test"
68
80
  t.test_files = FileList['test/test*.rb']
69
81
  t.verbose = true
File without changes
Binary file
data/lib/rcdk.rb CHANGED
@@ -15,20 +15,18 @@
15
15
  # but WITHOUT ANY WARRANTY; without even the implied warranty of
16
16
  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17
17
  # Lesser General Public License for more details.
18
- #
19
- # You should have received a copy of the GNU Lesser General Public
20
- # License along with this library; if not, write to the Free
21
- # Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor
22
- # Boston, MA 02111-1301, USA.
23
18
 
24
19
  require 'java'
25
20
 
26
- RCDK_VERSION = '0.1.0'
21
+ RCDK_VERSION = '0.2.0'
27
22
  CDK_VERSION = '20060714'
23
+ STRUCTURE_CDK_VERSION = '0.1.2'
28
24
 
29
25
  Java::Classpath.add(Gem.dir + File::SEPARATOR +
30
- File.join('gems', 'rcdk-' + RCDK_VERSION, 'java', 'cdk-' +
31
- CDK_VERSION + '.jar'))
32
-
26
+ File.join('gems', 'rcdk-' + RCDK_VERSION, 'java', 'lib', 'cdk-' +
27
+ CDK_VERSION + '.jar'))
33
28
 
29
+ Java::Classpath.add(Gem.dir + File::SEPARATOR +
30
+ File.join('gems', 'rcdk-' + RCDK_VERSION, 'java', 'lib', 'structure-cdk-' +
31
+ STRUCTURE_CDK_VERSION + '.jar'))
34
32
 
data/lib/util.rb ADDED
@@ -0,0 +1,160 @@
1
+ # =============================================
2
+ # RCDK - The Chemistry Development Kit for Ruby
3
+ # =============================================
4
+ #
5
+ # Project Info: http://rcdk.rubyforge.org
6
+ #
7
+ # Copyright (C) 2006 Richard L. Apodaca
8
+ #
9
+ # This library is free software; you can redistribute it and/or
10
+ # modify it under the terms of the GNU Lesser General Public
11
+ # License version 2.1 as published by the Free Software
12
+ # Foundation.
13
+ #
14
+ # This library is distributed in the hope that it will be useful,
15
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17
+ # Lesser General Public License for more details.
18
+
19
+ require 'rcdk'
20
+
21
+ StringReader = import 'java.io.StringReader'
22
+ StringWriter = import 'java.io.StringWriter'
23
+
24
+ Molecule = import 'org.openscience.cdk.Molecule'
25
+ MDLReader = import 'org.openscience.cdk.io.MDLReader'
26
+ MDLWriter = import 'org.openscience.cdk.io.MDLWriter'
27
+ SmilesParser = import 'org.openscience.cdk.smiles.SmilesParser'
28
+ SmilesGenerator = import 'org.openscience.cdk.smiles.SmilesGenerator'
29
+ DefaultChemObjectBuilder = import 'org.openscience.cdk.DefaultChemObjectBuilder'
30
+ StructureDiagramGenerator = import 'org.openscience.cdk.layout.StructureDiagramGenerator'
31
+
32
+ ImageKit = import 'net.sf.structure.cdk.util.ImageKit'
33
+
34
+ # The Ruby Chemistry Development Kit.
35
+ module RCDK
36
+
37
+ # Convenience methods for working with the CDK.
38
+ module Util
39
+
40
+ # Molecular language translation. Currently molfile and SMILES
41
+ # are implemented.
42
+ class Lang
43
+ @@mdl_reader = MDLReader.new
44
+ @@mdl_writer = MDLWriter.new
45
+ @@smiles_parser = SmilesParser.new
46
+ @@smiles_generator = SmilesGenerator.new(DefaultChemObjectBuilder.getInstance)
47
+
48
+ # Returns a CDK <tt>Molecule</tt> given the String-based molfile
49
+ # <tt>molfile</tt>.
50
+ def self.read_molfile(molfile)
51
+ reader =
52
+ StringReader.new_with_sig('Ljava.lang.String;', molfile)
53
+
54
+ @@mdl_reader.setReader(reader)
55
+ @@mdl_reader.read(Molecule.new)
56
+ end
57
+
58
+ # Returns a String-based molfile by parsing the CDK <tt>molecule</tt>.
59
+ def self.get_molfile(molecule)
60
+ writer = StringWriter.new
61
+
62
+ @@mdl_writer.setWriter(writer)
63
+ @@mdl_writer.writeMolecule(molecule)
64
+ @@mdl_writer.close
65
+
66
+ writer.toString
67
+ end
68
+
69
+ # Returns a CDK <tt>Molecule</tt> by parsing <tt>smiles</tt>.
70
+ def self.read_smiles(smiles)
71
+ @@smiles_parser.parseSmiles(smiles)
72
+ end
73
+
74
+ # Returns a SMILES string based on the structure of the indicated
75
+ # CDK <tt>molecule</tt>.
76
+ def self.get_smiles(molecule)
77
+ @@smiles_generator.createSMILES(molecule)
78
+ end
79
+
80
+ # Returns a SMILES string by parsing the <tt>molfile</tt> string.
81
+ def self.molfile_to_smiles(molfile)
82
+ get_smiles(read_molfile(molfile))
83
+ end
84
+
85
+ # Returns a molfiles STRING by parsing <tt>smiles</tt>.
86
+ def self.smiles_to_molfile(smiles)
87
+ get_molfile(read_smiles(smiles))
88
+ end
89
+
90
+ end
91
+
92
+ # 2-D coordinate generation.
93
+ class XY
94
+ @@sdg = StructureDiagramGenerator.new
95
+
96
+ # Assigns 2-D coordinates to the indicated <tt>molfile</tt> string.
97
+ def self.coordinate_molfile(molfile)
98
+ mol = coordinate_molecule(Lang.read_molfile(molfile))
99
+
100
+ Lang.get_molfile(mol)
101
+ end
102
+
103
+ # Assigns 2-D coordinates to the indicated CDK <tt>molecule</tt>.
104
+ def self.coordinate_molecule(molecule)
105
+ @@sdg.setMolecule(molecule)
106
+ @@sdg.generateCoordinates
107
+ @@sdg.getMolecule
108
+ end
109
+ end
110
+
111
+ # Raster and SVG 2-D molecular images.
112
+ class Image
113
+
114
+ # Writes a <tt>width</tt> by <tt>height</tt> PNG image to
115
+ # <tt>path_to_png</tt> using <tt>molfile</tt>.
116
+ def self.molfile_to_png(molfile, path_to_png, width, height)
117
+ ImageKit.writePNG(Lang.read_molfile(molfile), width, height, path_to_png)
118
+ end
119
+
120
+ # Writes a <tt>width</tt> by <tt>height</tt> SVG document to
121
+ # <tt>path_to_svg</tt> using <tt>molfile</tt>.
122
+ def self.molfile_to_svg(molfile, path_to_svg, width, height)
123
+ ImageKit.writeSVG(Lang.read_molfile(molfile), width, height, path_to_svg)
124
+ end
125
+
126
+ # Writes a <tt>width</tt> by <tt>height</tt> JPG image to
127
+ # <tt>path_to_jpg</tt> using <tt>molfile</tt>.
128
+ def self.molfile_to_jpg(molfile, path_to_jpg, width, height)
129
+ ImageKit.writeJPG(Lang.read_molfile(molfile), width, height, path_to_jpg)
130
+ end
131
+
132
+ # Writes a <tt>width</tt> by <tt>height</tt> PNG image to
133
+ # <tt>path_to_png</tt> using <tt>smiles</tt>. Coordinates are automatically
134
+ # assigned.
135
+ def self.smiles_to_png(smiles, path_to_png, width, height)
136
+ mol = XY.coordinate_molecule(Lang.read_smiles(smiles))
137
+
138
+ ImageKit.writePNG(mol, width, height, path_to_png)
139
+ end
140
+
141
+ # Writes a <tt>width</tt> by <tt>height</tt> SVG document to
142
+ # <tt>path_to_svg</tt> using <tt>smiles</tt>. Coordinates are automatically
143
+ # assigned.
144
+ def self.smiles_to_svg(smiles, path_to_svg, width, height)
145
+ mol = XY.coordinate_molecule(Lang.read_smiles(smiles))
146
+
147
+ ImageKit.writeSVG(mol, width, height, path_to_svg)
148
+ end
149
+
150
+ # Writes a <tt>width</tt> by <tt>height</tt> JPG image to
151
+ # <tt>path_to_jpg</tt> using <tt>smiles</tt>. Coordinates are automatically
152
+ # assigned.
153
+ def self.smiles_to_jpg(smiles, path_to_jpg, width, height)
154
+ mol = XY.coordinate_molecule(Lang.read_smiles(smiles))
155
+
156
+ ImageKit.writeJPG(mol, width, height, path_to_jpg)
157
+ end
158
+ end
159
+ end
160
+ end
data/test/test.rb CHANGED
@@ -15,33 +15,115 @@
15
15
  # but WITHOUT ANY WARRANTY; without even the implied warranty of
16
16
  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17
17
  # Lesser General Public License for more details.
18
- #
19
- # You should have received a copy of the GNU Lesser General Public
20
- # License along with this library; if not, write to the Free
21
- # Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor
22
- # Boston, MA 02111-1301, USA.
23
18
 
24
19
  $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
25
20
 
21
+
26
22
  require 'rcdk'
27
23
  require 'test/unit'
28
24
 
29
- Java::Classpath.add(File.join('java', 'cdk-' + '20060714' + '.jar'))
25
+ Java::Classpath.clear!
26
+ Java::Classpath.add(File.join('java', 'lib', 'cdk-' + CDK_VERSION + '.jar'))
27
+ Java::Classpath.add(File.join('java', 'lib', 'structure-cdk-' + STRUCTURE_CDK_VERSION + '.jar'))
28
+
29
+ require 'util'
30
+
31
+ include RCDK::Util
30
32
 
31
- SmilesParser = import('org.openscience.cdk.smiles.SmilesParser')
33
+ MoleculeFactory = import 'org.openscience.cdk.templates.MoleculeFactory'
32
34
 
33
- # A very simple test suite.
35
+ # A very simple test suite. Woefully incomplete. Just exercises the API.
34
36
  class BasicTest < Test::Unit::TestCase
35
37
 
36
- # Loads the SMILES string for benzene and verifies that the resulting
37
- # CDK molecule has six atoms.
38
- def test_parse_smiles
39
- parser = SmilesParser.new
40
- smiles = 'c1ccccc1'
41
-
42
- mol = parser.parseSmiles(smiles)
38
+ def setup
39
+ @benzene =
40
+ "c1ccccc1
41
+ JME 2004.10 Thu Jun 01 18:20:16 EDT 2006
42
+
43
+ 6 6 0 0 0 0 0 0 0 0999 V2000
44
+ 0.0000 0.0000 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
45
+ 0.0000 0.0000 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
46
+ 0.0000 0.0000 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
47
+ 0.0000 0.0000 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
48
+ 0.0000 0.0000 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
49
+ 0.0000 0.0000 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
50
+ 1 2 2 0 0 0 0
51
+ 1 3 1 0 0 0 0
52
+ 2 4 1 0 0 0 0
53
+ 3 5 2 0 0 0 0
54
+ 4 6 2 0 0 0 0
55
+ 5 6 1 0 0 0 0
56
+ M END"
57
+ end
58
+
59
+ def test_read_molfile
60
+ mol = Lang.read_molfile(@benzene)
43
61
 
44
62
  assert_equal(6, mol.getAtomCount)
45
63
  end
64
+
65
+ def test_write_molfile
66
+ molecule = MoleculeFactory.makeIndole
67
+ molfile = Lang.get_molfile(molecule)
68
+
69
+ assert_not_equal(0, molfile.length)
70
+ end
71
+
72
+ def test_read_smiles
73
+ molecule = Lang.read_smiles('c1ccccc1')
74
+
75
+ assert_equal(6, molecule.getAtomCount)
76
+ end
77
+
78
+ def test_write_smiles
79
+ smiles = Lang.get_smiles(MoleculeFactory.makeBenzene)
80
+
81
+ assert_equal('c1ccccc1', smiles)
82
+ end
83
+
84
+ def test_molfile_to_smiles
85
+ smiles = Lang.molfile_to_smiles(@benzene)
86
+
87
+ assert_equal('c1ccccc1', smiles)
88
+ end
89
+
90
+ def test_smiles_to_molfile
91
+ molfile = Lang.smiles_to_molfile('c1ccccc1')
92
+
93
+ assert_not_equal(0, molfile.length)
94
+ end
95
+
96
+ def test_coordinate_molecule
97
+ indole = MoleculeFactory.makeIndole
98
+ XY.coordinate_molecule(indole)
99
+ end
100
+
101
+ def test_coordinate_molfile
102
+ molfile = XY.coordinate_molfile(XY.coordinate_molfile(@benzene))
103
+ end
104
+
105
+ def test_molfile_to_png
106
+ Image.molfile_to_png(XY.coordinate_molfile(@benzene), 'output/benzene.png', 200, 200)
107
+ end
108
+
109
+ def test_molfile_to_svg
110
+ Image.molfile_to_svg(XY.coordinate_molfile(@benzene), 'output/benzene.svg', 200, 200)
111
+ end
112
+
113
+ def test_molfile_to_jpg
114
+ Image.molfile_to_jpg(XY.coordinate_molfile(@benzene), 'output/benzene.jpg', 200, 200)
115
+ end
116
+
117
+ def test_smiles_to_png
118
+ Image.smiles_to_png('Clc1ccccc1', 'output/chlorobenzene.png', 200, 200)
119
+ end
120
+
121
+ def test_smiles_to_svg
122
+ Image.smiles_to_svg('Clc1ccccc1', 'output/chlorobenzene.svg', 200, 200)
123
+ end
124
+
125
+ def test_smiles_to_jpg
126
+ Image.smiles_to_jpg('Clc1ccccc1', 'output/chlrobenzene.jpg', 200, 200)
127
+ end
46
128
  end
47
129
 
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.0
3
3
  specification_version: 1
4
4
  name: rcdk
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.1.0
7
- date: 2006-09-13 00:00:00 -07:00
6
+ version: 0.2.0
7
+ date: 2006-09-24 00:00:00 -07:00
8
8
  summary: A Ruby wrapper for the Chemistry Development Kit
9
9
  require_paths:
10
10
  - lib
@@ -33,8 +33,10 @@ files:
33
33
  - README
34
34
  - lib/rcdk.rb
35
35
  - lib/java.rb
36
+ - lib/util.rb
36
37
  - test/test.rb
37
- - java/cdk-20060714.jar
38
+ - java/lib/cdk-20060714.jar
39
+ - java/lib/structure-cdk-0.1.2.jar
38
40
  test_files: []
39
41
 
40
42
  rdoc_options: []