Class: ChemScanner::Interpreter::Scheme

Inherits:
Object
  • Object
show all
Includes:
PostProcess, PreProcess, ReactionDetection
Defined in:
lib/chem_scanner/interpreter/scheme.rb,
lib/chem_scanner/interpreter/post_process/assemble.rb

Overview

General scheme, contains all graphics (molecules, text, arrows …)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from PostProcess

#extract_product_yield, #extract_reaction_info, #extract_temperature, #extract_time_info, #extract_yield_info, #merge_chemdraw_with_predefined, #name_to_struct, #process_reaction_info, #range_number_regex, #refine_reagents_label, #refine_text_as_molecule, #refine_text_label, #replace_label_by_molecule, #split_text, #text_regex, #time_duration_range_regex

Methods included from ReactionDetection

#assign_molecule_group, #assign_text, #assign_to_reaction, #check_position, #check_reaction_orderring, #detect_position, #detect_reaction_step, #distance_molecule_group, #molecules_intersects_with_segment, #multi_line_chain_reaction, #nearest_arrow, #nearest_molecule, #process_reactions_step, #refine_duplicate_reagents, #remove_separated_mol, #sort_arrow_map, #text_around_arrow?, #try_detect_label_position

Methods included from SchemeBase

#add_molecule_substitution_info, #add_reaction_substitution_info, #assemble_molecule_text, #auto_fit_arrow_polygons

Methods included from PreProcess

#assemble_ionic_molecule, #detect_line_fragment, #extract_fragment_graphic, #find_fragment_inside_rectangle, #fragment_to_molecules, #populate_molecule_info, #process_orbital_as_polymer, #refine_arrow, #refine_molecules, #try_check_cross, #try_extend_split, #try_extend_tail

Constructor Details

#initialize(parser) ⇒ Scheme

Returns a new instance of Scheme.



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
# File 'lib/chem_scanner/interpreter/scheme.rb', line 19

def initialize(parser)
  fragment_map = parser.fragment_map.map { |k, v| [k, Fragment.new(v)] }
  @fragment_map = fragment_map.to_h
  @fragment_group_map = parser.fragment_group_map

  @geometry_map = parser.geometry_map
  @graphic_map = parser.graphic_map

  @text_map = parser.text_map
  @bracket_map = parser.bracket_map

  @mol_map = ElementMap.new
  @mol_group_map = ElementMap.new

  @arrow_map = ElementMap.new
  # Segment or headless arrow
  @segment_map = ElementMap.new

  @mol_substitutes = {}
  @reaction_substitutes = {}

  @fragment_as_line = 0

  @reactions = []
end

Instance Attribute Details

#bracket_mapObject (readonly)

Returns the value of attribute bracket_map.



12
13
14
# File 'lib/chem_scanner/interpreter/scheme.rb', line 12

def bracket_map
  @bracket_map
end

#fragment_as_lineObject (readonly)

Returns the value of attribute fragment_as_line.



12
13
14
# File 'lib/chem_scanner/interpreter/scheme.rb', line 12

def fragment_as_line
  @fragment_as_line
end

#mol_mapObject (readonly)

Returns the value of attribute mol_map.



12
13
14
# File 'lib/chem_scanner/interpreter/scheme.rb', line 12

def mol_map
  @mol_map
end

#n_atomsObject (readonly)

Returns the value of attribute n_atoms.



12
13
14
# File 'lib/chem_scanner/interpreter/scheme.rb', line 12

def n_atoms
  @n_atoms
end

#reactionsObject (readonly)

Returns the value of attribute reactions.



12
13
14
# File 'lib/chem_scanner/interpreter/scheme.rb', line 12

def reactions
  @reactions
end

#text_mapObject (readonly)

Returns the value of attribute text_map.



12
13
14
# File 'lib/chem_scanner/interpreter/scheme.rb', line 12

def text_map
  @text_map
end

Instance Method Details

#assemble_reactionObject



7
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
# File 'lib/chem_scanner/interpreter/post_process/assemble.rb', line 7

def assemble_reaction
  @reactions.each do |r|
    %w[reactant reagent product].each do |group|
      group_ids = r.send("#{group}_ids")
      groups = r.send("#{group}s")

      group_ids.each do |id|
        if @text_map.key?(id)
          r.text_ids.push(id)
          next
        end

        if @mol_map.key?(id)
          groups.push(@mol_map[id])
          next
        end

        @mol_group_map.select do |_, mgroup|
          mgroup.molecule_ids.include?(id)
        end.each do |_, mgroup|
          groups.push(mgroup.molecules.first)
        end
      end
    end

    r.arrow = @arrow_map[r.arrow_id]
    r.text_ids.concat(r.arrow.text_arr).uniq!
  end
end

#interpretObject



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
70
71
72
73
74
75
76
77
78
79
# File 'lib/chem_scanner/interpreter/scheme.rb', line 45

def interpret
  pre_process
  reaction_detection
  post_process

  tgi = TextGroupInterpreter.new(self)
  # Detect if molecule has any n-atom, save those infos
  tgi.retrieve_n_atoms_info

  @n_atoms = tgi.n_atoms

  # Retrieve rgroups, alias-groups of molecules
  tgi.retrieve_alias_info

  # - Find R-groups ("R1", "R2", "R", ...)
  # - Find alias-groups ("X", "Y", "Ar", "M")
  # - Detect label set ("2a,b" "3-6" ...)
  # tgi.retrieve_labels_and_groups

  # - Combine corresponding addition info detected molecule/reaction text
  #   e.g., "3: m = 1, R = H"
  # - Interpret previouse retrieved data
  # - Save those infos to generate molecules/reactions later
  # interpret_labels_and_groups

  # Try generate new molecules/reactions
  #   based on R-groups, alias-groups, n-atoms ...
  tgi.generate_elements

  @mol_group_map.each do |_, mgroup|
    mgroup.molecules.each do |m|
      @mol_map[m.id] = m unless @mol_map.key?(m.id)
    end
  end
end

#moleculesObject



81
82
83
# File 'lib/chem_scanner/interpreter/scheme.rb', line 81

def molecules
  @mol_map.values
end