Permalink
Browse files

working to get sdf reading going

  • Loading branch information...
1 parent 702f239 commit 40dab671aeba14dfe71eba7e53aa20cad4d077ef @jtprince jtprince committed Apr 18, 2012
Showing with 125 additions and 0 deletions.
  1. +27 −0 lib/mspire/atom.rb
  2. +21 −0 lib/mspire/molecule.rb
  3. +57 −0 lib/mspire/sdf.rb
  4. +20 −0 spec/molecule_spec.rb
View
@@ -0,0 +1,27 @@
+
+module Mspire
+ class Atom
+ COMMON_VALENCE = {
+ :c => 4,
+ :o => 2,
+ :h => 1,
+ :n => 3,
+ :p => 5, # 3 ??
+ }
+
+ attr_accessor :element
+ attr_accessor :valence
+ attr_accessor :bonds
+
+ def initialize(element, bonds=[], valence=nil)
+ @element = element.downcase.to_sym
+ @bonds = bonds
+ @valence =
+ if valence
+ valence
+ else
+ COMMON_VALENCE[element]
+ end
+ end
+ end
+end
View
@@ -0,0 +1,21 @@
+require 'mspire/atom'
+require 'strscan'
+
+module Mspire
+ class Molecule < Array
+
+
+ "LMFA01010012| LIPDMAPS01251212252D|| 14 13 0 0 0 0 0 0 0 0999 V2000| 6.4289 5.0000 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0| 7.8578 5.0000 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0| 7.1433 5.4125 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0| 8.5723 5.4125 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0| 9.2868 5.0000 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0| 10.0013 5.4125 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0| 10.7158 5.0000 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0| 11.4302 5.4125 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0| 12.1448 5.0000 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0| 12.8592 5.4125 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0| 13.5737 5.0000 0.0000 O 0 0 0 0 0 0 0 0 0 0 0 0| 12.8592 6.2375 0.0000 O 0 0 0 0 0 0 0 0 0 0 0 0| 5.7144 5.4125 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0| 5.0000 5.0000 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0| 2 3 1 0 0 0 0| 3 1 1 0 0 0 0| 4 2 1 0 0 0 0| 5 4 1 0 0 0 0| 6 5 1 0 0 0 0| 7 6 1 0 0 0 0| 8 7 1 0 0 0 0| 9 8 1 0 0 0 0| 10 9 1 0 0 0 0| 11 10 1 0 0 0 0| 10 12 2 0 0 0 0| 13 1 1 0 0 0 0| 14 13 1 0 0 0 0|M END"
+
+ def self.from_sdf_string(sdf_string)
+ pieces = sdf_string.split(/\s*\|\s*/)
+ puts "HIAY"
+ puts pieces
+ abort 'here'
+ end
+
+ def mass
+ end
+
+ end
+end
View
@@ -0,0 +1,57 @@
+
+module Mspire
+ class SDF
+ Atom = Struct.new
+
+ # (4th line) an array of count data
+ attr_accessor :count_data
+ # an array: the first 3 lines
+ attr_accessor :header
+
+ # the string for one compound
+ def initialize(string=nil)
+ parse(string) if string
+ end
+
+ def parse(string)
+ string.chomp!
+ lines = string.split(/\r?\n/)
+ @header = lines[0,3]
+ count_line = lines[3]
+ @count_data = count_line.split(/\s+/)
+ @coordinates = lines[4,num_atoms]
+
+ end
+
+ def num_atoms
+ @count_data[0].to_i
+ end
+
+ def num_bonds
+ @count_data[1].to_i
+ end
+
+ end
+end
+
+=begin
+ benzene
+ ACD/Labs0812062058
+
+ 6 6 0 0 0 0 0 0 0 0 1 V2000
+ 1.9050 -0.7932 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
+ 1.9050 -2.1232 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
+ 0.7531 -0.1282 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
+ 0.7531 -2.7882 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
+ -0.3987 -0.7932 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
+ -0.3987 -2.1232 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
+ 2 1 1 0 0 0 0
+ 3 1 2 0 0 0 0
+ 4 2 2 0 0 0 0
+ 5 3 1 0 0 0 0
+ 6 4 1 0 0 0 0
+ 6 5 2 0 0 0 0
+ M END
+=end
+
+## note $$$$ is the split for multiple compounds
View
@@ -0,0 +1,20 @@
+require 'spec_helper'
+
+require 'mspire/molecule'
+
+
+
+
+
+
+describe Mspire::Molecule do
+
+ before(:each) do
+
+ @sdf_string = "LMFA01010012| LIPDMAPS01251212252D|| 14 13 0 0 0 0 0 0 0 0999 V2000| 6.4289 5.0000 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0| 7.8578 5.0000 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0| 7.1433 5.4125 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0| 8.5723 5.4125 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0| 9.2868 5.0000 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0| 10.0013 5.4125 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0| 10.7158 5.0000 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0| 11.4302 5.4125 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0| 12.1448 5.0000 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0| 12.8592 5.4125 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0| 13.5737 5.0000 0.0000 O 0 0 0 0 0 0 0 0 0 0 0 0| 12.8592 6.2375 0.0000 O 0 0 0 0 0 0 0 0 0 0 0 0| 5.7144 5.4125 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0| 5.0000 5.0000 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0| 2 3 1 0 0 0 0| 3 1 1 0 0 0 0| 4 2 1 0 0 0 0| 5 4 1 0 0 0 0| 6 5 1 0 0 0 0| 7 6 1 0 0 0 0| 8 7 1 0 0 0 0| 9 8 1 0 0 0 0| 10 9 1 0 0 0 0| 11 10 1 0 0 0 0| 10 12 2 0 0 0 0| 13 1 1 0 0 0 0| 14 13 1 0 0 0 0|M END"
+ end
+ it 'converts from sdf to molecule' do
+ molecule = Mspire::Molecule.from_sdf_string(@sdf_string)
+ p molecule
+ end
+end

0 comments on commit 40dab67

Please sign in to comment.