Class: ODDB::Interaction::FlockhartWriter

Inherits:
NullWriter show all
Defined in:
src/plugin/flockhart.rb,
test/test_plugin/flockhart.rb

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Methods inherited from NullWriter

#new_fonthandler, #new_linkhandler, #send_meta

Constructor Details

- (FlockhartWriter) initialize

A new instance of FlockhartWriter



15
16
17
18
19
20
21
# File 'src/plugin/flockhart.rb', line 15

def initialize
  @tablehandlers = []
  @cytochromes = {}
  @values = {}
  @collected_hashes = []
  @duplicates = []
end

Instance Attribute Details

- (Object) auc_factor

Returns the value of attribute auc_factor



20
21
22
# File 'test/test_plugin/flockhart.rb', line 20

def auc_factor
  @auc_factor
end

- (Object) category

Returns the value of attribute category



18
19
20
# File 'test/test_plugin/flockhart.rb', line 18

def category
  @category
end

- (Object) current_category

Returns the value of attribute current_category



20
21
22
# File 'test/test_plugin/flockhart.rb', line 20

def current_category
  @current_category
end

- (Object) current_table (readonly)

Returns the value of attribute current_table



21
22
23
# File 'test/test_plugin/flockhart.rb', line 21

def current_table
  @current_table
end

- (Object) cytochromes

Returns the value of attribute cytochromes



18
19
20
# File 'test/test_plugin/flockhart.rb', line 18

def cytochromes
  @cytochromes
end

- (Object) duplicates (readonly)

Returns the value of attribute duplicates



22
23
24
# File 'test/test_plugin/flockhart.rb', line 22

def duplicates
  @duplicates
end

- (Object) substances (readonly)

Returns the value of attribute substances



14
15
16
# File 'src/plugin/flockhart.rb', line 14

def substances
  @substances
end

- (Object) tablehandlers (readonly)

Returns the value of attribute tablehandlers



21
22
23
# File 'test/test_plugin/flockhart.rb', line 21

def tablehandlers
  @tablehandlers
end

- (Object) tobecombined

Returns the value of attribute tobecombined



19
20
21
# File 'test/test_plugin/flockhart.rb', line 19

def tobecombined
  @tobecombined
end

- (Object) type

Returns the value of attribute type



19
20
21
# File 'test/test_plugin/flockhart.rb', line 19

def type
  @type
end

Instance Method Details

- (Object) check_string(string)



22
23
24
25
26
27
28
29
30
31
# File 'src/plugin/flockhart.rb', line 22

def check_string(string)
  case string
  when '', /\302\240/u, /^[=>(:]/u, /^[0-9]OH/u, /^[0-9]-OH/u, /^No[rt]/iu,
       /^NAPQI/u, /^[0-9]+\s*$/u, /^Chr\d+\s*$/u,
       /o-desme/iu
    return false
  else
    return true
  end
end

- (Object) clear_string(string)



32
33
34
35
36
37
38
39
# File 'src/plugin/flockhart.rb', line 32

def clear_string(string)
  string.delete!("\302\240")
  string.slice!(/^=/u)
  string.slice!(/^>/u)
  string.slice!(/[0-9]OH/u)
  string.slice!(/[0-9]-OH/u)
  string.strip
end

- (Object) create_update_objects(base_name, data, cyt_id, not_cyt)



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'src/plugin/flockhart.rb', line 40

def create_update_objects(base_name, data, cyt_id, not_cyt)
  case @type
  when 'substrates'
    new_class = ODDB::Interaction::SubstrateConnection
  when 'inhibitors'
    new_class = ODDB::Interaction::InhibitorConnection
  when 'inducers'
    new_class = ODDB::Interaction::InducerConnection
  end
  obj = new_class.new(base_name, 'en')
  data.each { |key, val|
    obj.send("#{key}=", val) if(val)
  }
  @cytochromes.each { |key, value|
    column, base_name = key.split("/")
    if(column==cyt_id.to_s)
      value.add_connection(obj) unless not_cyt==base_name
    end
  }   
end

- (Object) end_category



60
61
62
# File 'src/plugin/flockhart.rb', line 60

def end_category 
  @category = "end" unless @current_category.nil?
end

- (Object) extract_data



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'src/plugin/flockhart.rb', line 63

def  extract_data
  links = ODDB::Interaction::FlockhartPlugin::LINKS
  max = links.size
  @tablehandlers.compact.uniq.each { |th|
    th.each_row { |row|
      (0...max).each { |dig|
        data = row.cdata(dig)
        if(data.is_a?(String))
          if(data.match(/@\/@\/@/u))
            # clean out some javascript.
            data.gsub!(/^.*function[^}]+\}/u, '')
            parse_cyt_string(data, dig)
          elsif(data.size>3)
            @collected_hashes.push(Hash[dig, parse_string(data)])
          end
        end
        if(data.is_a?(Array))
          data.each { |str|
            @collected_hashes.push(Hash[dig, parse_string(str)])
          }
        end
      }
      @collected_hashes.each { |hash|
        hash.each { |cyt_id, hsh|
          if(hsh!=nil)
            hsh.each { |base_name, data|
              not_cyt = nil
              if(base_name.match(/-\/-\/-/u))
                base_name, not_cyt = base_name.split(/-\/-\/-/u)
              end
              create_update_objects(base_name, data, cyt_id, not_cyt)
            }
          end
        }
      }
      @collected_hashes.clear
    }
  }
  @cytochromes
end

- (Object) new_font(font)



103
104
105
106
107
108
109
110
111
# File 'src/plugin/flockhart.rb', line 103

def new_font(font)
  _, _, bold, _ = font
  if bold == 1
    @current_category = nil
    @category = "start"
  else
    @category = nil 
  end
end

- (Object) new_tablehandler(handler)



112
113
114
115
116
# File 'src/plugin/flockhart.rb', line 112

def new_tablehandler(handler)
  @current_table = nil
  @current_tablehandler = handler
  @tablehandlers.push(handler)
end

- (Object) parse_array(array)



117
118
119
120
121
122
123
124
# File 'src/plugin/flockhart.rb', line 117

def parse_array(array)
  hsh = {}
  array.each { |str| 
    name, category, auc = str.split(/\*\/\*\/\*/u)
    hsh.store(name, {:category => category, :auc_factor => auc})
  }
  hsh
end

- (Object) parse_cyt_string(string, cyt_id)



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'src/plugin/flockhart.rb', line 125

def parse_cyt_string(string, cyt_id)
  array = string.split(/@\/@\/@/u)
  cyt = []
  if(FlockhartPlugin::FORMAT_CYT_ID.keys.include?(array[1]))
    FlockhartPlugin::FORMAT_CYT_ID[array[1]].each { |name|
      cyt.push(name)
    }
  else
    cyt = array[1]
  end
  objs = {}
  cyt.each { |name|
    objs.store(cyt_id.to_s+"/"+name, ODDB::Interaction::Cytochrome.new(name))
  }
  objs.each { |id, obj|
    unless(@cytochromes.keys.include?(id))
      @cytochromes.store(id, obj)
    end
  }
  @type = array.first
end

- (Object) parse_string(string)



146
147
148
149
150
151
152
153
154
155
# File 'src/plugin/flockhart.rb', line 146

def parse_string(string)
  array = []
  if(string.match(/-\/-\/-/u))
    array = string.split(/&\/&\/&/u)
    @duplicates.push(array.shift)
  else
    array = string.split(/&\/&\/&/u)
  end
  parse_array(array)
end

- (Object) send_flowing_data(data)



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'src/plugin/flockhart.rb', line 156

def send_flowing_data(data) 
  if(match = /SUBSTRATES|INHIBITORS|INDUCERS/u.match(data))
    @current_table = match.to_s.downcase
  elsif(@current_tablehandler)
    if(@current_table)
      @data = data.strip
      case @category
      when "start"
        @current_category = data.split(/:/u).first.downcase
        @data = 'cat'
      when "end"
        @current_category = nil
        @category = nil
      end
      if /:$/.match(@data)
        @current_category = @data[0...-1].downcase
        @data = 'cat'
      end
      if(@tr_class && @tr_class.match(/lite/u) && @data!='cat')
        data = clear_string(data)
        return unless check_string(data)
        if(data.match(/\(not/u))
          data.slice!(/\(not/u)
          data.slice!(/\)/u)
          data = @previous_data << "-/-/-" + data.strip
        end
        if(@ignore_next && !data.match(/=/u))
          @ignore_next = false
        else
          if(data.match(/=/u))
            data.slice!(/=/u)
            @ignore_next = true
          end
          data_string = write_substance_string(data)
          @previous_data = data
          @current_tablehandler.send_cdata(data_string) if check_string(data_string)
        end
      elsif(@tr_class && @tr_class.match(/green/u) && @data!='cat')
        type = @current_table.dup
        type_cyt = type << "@/@/@" << data.strip
        @current_tablehandler.send_cdata(type_cyt) if check_string(type_cyt)
      end
    end
  end
end

- (Object) send_image(src)



201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'src/plugin/flockhart.rb', line 201

def send_image(src)
  case File.basename(src)
  when "red.jpg"
    @auc_factor = "5"
  when "orange.jpg"
    @auc_factor = "2"
  when "orangeGreen.jpg"
    @auc_factor = "1.75"
  when "green.jpg"
    @auc_factor = "1.25"
  when "blue.jpg"
    @auc_factor = "1"
  end
end

- (Object) send_line_break



215
216
217
218
219
# File 'src/plugin/flockhart.rb', line 215

def send_line_break
  unless(@current_tablehandler.nil?)
    @current_tablehandler.next_line
  end
end

- (Object) start_tr(attributes)



220
221
222
223
224
225
226
227
228
229
# File 'src/plugin/flockhart.rb', line 220

def start_tr(attributes)
  case attributes && attributes.first
  when ["bgcolor", "#CCCCCC"]
    @tr_class = 'green'
  when ["valign", "top"]
    @tr_class = 'lite'
  else
    @tr_class = nil
  end
end

- (Object) write_substance_string(data)



230
231
232
233
234
235
236
237
# File 'src/plugin/flockhart.rb', line 230

def write_substance_string(data)
  cat = @current_category
  name = data.strip
  string = name.dup
  string << "*/*/*#{cat}"
  string << "*/*/*#@auc_factor" 
  string << "&/&/&"
end