Class: ChemScanner::Interpreter::TextGroupInterpreter

Inherits:
Object
  • Object
show all
Includes:
Passthrough
Defined in:
lib/chem_scanner/interpreter/text_group/retrieve_alias_info.rb,
lib/chem_scanner/interpreter/text_group/retrieve_n_atoms.rb,
lib/chem_scanner/interpreter/text_group/text_group_interpreter.rb

Overview

Text and group interpreter

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Passthrough

#passthrough

Constructor Details

#initialize(scheme) ⇒ TextGroupInterpreter

Returns a new instance of TextGroupInterpreter.



15
16
17
18
19
20
# File 'lib/chem_scanner/interpreter/text_group/text_group_interpreter.rb', line 15

def initialize(scheme)
  @scheme = scheme
  passthrough(scheme)

  @alias_info = {}
end

Instance Attribute Details

#n_atomsObject (readonly)

Returns the value of attribute n_atoms.



8
9
10
# File 'lib/chem_scanner/interpreter/text_group/text_group_interpreter.rb', line 8

def n_atoms
  @n_atoms
end

Instance Method Details

#add_n_atoms_info(mid, info) ⇒ Object



100
101
102
103
# File 'lib/chem_scanner/interpreter/text_group/retrieve_n_atoms.rb', line 100

def add_n_atoms_info(mid, info)
  cur_info = @n_atoms[mid] || []
  @n_atoms[mid] = cur_info.push(info)
end

#bracket_as_text(molecule, tid, atom_group) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/chem_scanner/interpreter/text_group/retrieve_n_atoms.rb', line 55

def bracket_as_text(molecule, tid, atom_group)
  text = @text_map[tid]
  box = text.polygon.bounding_box

  aids = []
  molecule.atom_map.each_value do |atom|
    next unless box.contains_point?(atom.point)

    aids.push(atom.id)
  end

  return unless aids.count == 1

  info = { aid: aids.first, group: atom_group }
  molecule.text_ids.delete(tid)
  add_n_atoms_info(molecule.id, info)
end

#bracket_node(molecule, bids) ⇒ Object



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
# File 'lib/chem_scanner/interpreter/text_group/retrieve_n_atoms.rb', line 73

def bracket_node(molecule, bids)
  bracketed_atom_ids = bids & molecule.atom_map.keys
  return if bracketed_atom_ids.empty?

  text_ids = molecule.text_ids.select do |id|
    tval = @text_map[id].value.strip
    /\A *[nm]\z/.match?(tval)
  end

  bracketed_atom_ids.each do |id|
    apoint = molecule.atom_map[id].point

    dist_map = text_ids.map do |tid|
      dist = @text_map[tid].polygon.distance_to_point(apoint)
      { dist: dist, id: tid }
    end
    min_dist_text = dist_map.min_by { |text| text[:dist] }
    next if min_dist_text[:dist] > 0.5

    tid = min_dist_text[:id]
    text = @text_map[tid].value
    info = { aid: id, group: text }
    molecule.text_ids.delete(tid)
    add_n_atoms_info(molecule.id, info)
  end
end

#generate_elementsObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/chem_scanner/interpreter/text_group/text_group_interpreter.rb', line 22

def generate_elements
  generate_indenpendent_molecules

  rtg_map = {}

  @reactions.each do |r|
    rtg = ReactionTextGroup.new(r, @alias_info, @scheme)
    rtg.interpret

    rtg_map[r.arrow_id] = rtg
  end

  # Bringup text-groups information from other reaction if possible
  no_group_rids = rtg_map.select do |_, v|
    !v.alias_groups.empty? && v.text_groups.empty?
  end

  no_group_rids.each do |k, v|
    other_rids = rtg_map.reject do |ok, ov|
      ok == k || ov.text_groups.empty?
    end

    other_rids.each do |_, ov|
      ogroups = ov.text_groups.select { |tg| tg[:bold].nil? }
      ogroups.each do |plain_group|
        v.text_groups.push(plain_group)
      end
    end

    v.text_groups.uniq!(&:keys)
  end

  genr_arr = rtg_map.reduce([]) do |arr, (_, v)|
    arr.concat(v.generate_reaction)
  end

  @reactions.concat(genr_arr)
  @reactions.each do |r|
    %w[reactant reagent product].each do |group|
      group_molecules = r.send("#{group}s")
      group_molecules.each do |m|
        @mol_map[m.id] = m unless @mol_map.key?(m.id)
      end

      r.send("#{group}_ids=", group_molecules.map(&:id))
    end
  end
end

#generate_indenpendent_moleculesObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/chem_scanner/interpreter/text_group/text_group_interpreter.rb', line 71

def generate_indenpendent_molecules
  independent_ids = @reactions.reduce([]) do |arr, r|
    arr.concat(r.all_ids)
  end

  molecules = @mol_map.reject do |k, _|
    independent_ids.include?(k) || !@alias_info.key?(k)
  end

  molecules.each_value do |m|
    mtg = MoleculeTextGroup.new(m, @alias_info, @scheme)
    mtg.interpret

    genm_arr = mtg.generate_molecule
    genm_arr.each do |genm|
      @mol_map[genm.id] = genm
    end
  end
end

#retrieve_alias_infoObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/chem_scanner/interpreter/text_group/retrieve_alias_info.rb', line 8

def retrieve_alias_info
  @alias_info = {}

  interpreter = ChemScanner::Interpreter

  @mol_map.each_value do |molecule|
    atoms = molecule.atom_map.values.select(&:is_alias)
    mid = molecule.id

    atoms.each do |atom|
      text = atom.alias_text
      is_alias_group = interpreter.alias_group?(text)

      next unless is_alias_group || interpreter.rgroup_atom?(text)

      type = is_alias_group ? GENERATE_ALIAS_GROUP : GENERATE_RGROUP
      # Extract only R-group, NOT "OR2", "SR1" ...
      group = is_alias_group ? text : text.scan(/(R\d+|R *)/).last.last

      info = { group: group, aid: atom.id, type: type }
      cur_info = @alias_info[mid] || []
      @alias_info[mid] = cur_info.push(info)
    end
  end

  @n_atoms.each do |mid, n_atom_info|
    info = n_atom_info.map { |ainfo| ainfo.merge(type: GENERATE_N_ATOM) }
    cur_info = @alias_info[mid] || []
    @alias_info[mid] = cur_info.concat(info)
  end
end

#retrieve_n_atoms_infoObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/chem_scanner/interpreter/text_group/retrieve_n_atoms.rb', line 14

def retrieve_n_atoms_info
  @n_atoms = {}

  bracketed_ids = []
  @bracket_map.each_value do |bracket|
    next unless bracket.object_ids.size == 1

    # Bigger bracket could wrap molecule(s) and/or more elements
    # Only process bracket which wrap 1 element id (1 atom)
    bracketed_ids.push(bracket.object_ids.first)
    bracket.attachments.each do |attachment|
      gid = attachment.graphic_id
      next unless @graphic_map.key?(gid)

      graphic = @graphic_map[gid]
      cd_graphic = ChemScanner::ChemDraw::Graphic
      next unless graphic.type == cd_graphic::GRAPHIC_BRACKET_TYPE

      @graphic_map.delete(gid)
    end
  end

  @mol_map.values.each do |m|
    m.text_ids.each do |id|
      text = @text_map[id].value

      N_ATOMS_REGEXES.each do |regex|
        matched = text.match(regex)
        next if matched.nil?

        atom_groups = matched.captures
        next unless atom_groups.count == 1

        bracket_as_text(m, id, atom_groups.first)
      end
    end

    bracket_node(m, bracketed_ids)
  end
end