Class: ChemScanner::Interpreter::MoleculeTextGroup

Inherits:
Object
  • Object
show all
Includes:
BoldGroup
Defined in:
lib/chem_scanner/interpreter/text_group/molecule_text_group.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from BoldGroup

#extract_alphabet_number, #extract_range_number, #group_combinations, #line_bold_groups, #normalize_bold, #normalize_bold_groups, #text_bold_groups

Constructor Details

#initialize(molecule, alias_info, scheme) ⇒ MoleculeTextGroup

Returns a new instance of MoleculeTextGroup.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/chem_scanner/interpreter/text_group/molecule_text_group.rb', line 12

def initialize(molecule, alias_info, scheme)
  @molecule = molecule
  @alias_info = alias_info
  @mol_map = scheme.mol_map
  @text_map = scheme.text_map

  # List of aliases, ex: ["R1", "R2"]
  @alias_groups = []

  # final list if text-groups and label
  @text_groups = []

  retrieve_alias_groups
end

Instance Attribute Details

#alias_groupsObject

Returns the value of attribute alias_groups.



10
11
12
# File 'lib/chem_scanner/interpreter/text_group/molecule_text_group.rb', line 10

def alias_groups
  @alias_groups
end

#bold_groupsObject

Returns the value of attribute bold_groups.



10
11
12
# File 'lib/chem_scanner/interpreter/text_group/molecule_text_group.rb', line 10

def bold_groups
  @bold_groups
end

#plain_groupsObject

Returns the value of attribute plain_groups.



10
11
12
# File 'lib/chem_scanner/interpreter/text_group/molecule_text_group.rb', line 10

def plain_groups
  @plain_groups
end

#text_groupsObject

Returns the value of attribute text_groups.



10
11
12
# File 'lib/chem_scanner/interpreter/text_group/molecule_text_group.rb', line 10

def text_groups
  @text_groups
end

Instance Method Details

#build_molecule_combisObject



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
70
71
# File 'lib/chem_scanner/interpreter/text_group/molecule_text_group.rb', line 40

def build_molecule_combis
  # group list without associated label
  @plain_groups = {}

  # groups with associated label
  @bold_groups = []

  @molecule.text_ids.each do |tid|
    bolds, groups = text_bold_groups(tid)
    bolds.reject!(&:empty?)

    if bolds.empty?
      @plain_groups.merge!(groups) { |_, old, new| old.concat(new) }
    else
      norm_bolds = normalize_bold_groups(bolds, groups)

      @bold_groups.concat(norm_bolds)
    end
  end

  n_atom_groups = plain_groups.reject do |k, _|
    ChemScanner::Interpreter.super_atom?(k)
  end

  combis = if n_atom_groups.empty?
             group_combinations(plain_groups)
           else
             n_atom_combinations(plain_groups)
           end

  @text_groups = @bold_groups + combis.map { |g| { group: g } }
end

#generate_moleculeObject



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
103
104
105
106
107
# File 'lib/chem_scanner/interpreter/text_group/molecule_text_group.rb', line 73

def generate_molecule
  malias = @alias_info[@molecule.id]

  generated_molecules = []
  @text_groups.each do |bg|
    cmol = @molecule.clone
    group_val = bg[:group]

    generated_text = []
    malias.each do |ainfo|
      agroup = ainfo[:group]
      val = group_val[agroup]
      next if val.nil?

      if ainfo[:type] == GENERATE_N_ATOM
        next if (val =~ /\d+/).nil? || val.to_i == 1

        cmol.n_atom_transform(ainfo[:aid], val.to_i)
      else
        cmol.group_transform(ainfo[:aid], agroup, val)
      end

      generated_text << "#{agroup} - #{val}"
    end

    cmol.update_output_formats
    bg[:bold].nil? || cmol.label = bg[:bold]

    cmol.text += ". Generated with: " + generated_text.join("; ") unless generated_text.empty?
    @mol_map[cmol.id] = cmol
    generated_molecules.push(cmol)
  end

  generated_molecules
end

#inspectObject



128
129
130
131
132
133
134
135
# File 'lib/chem_scanner/interpreter/text_group/molecule_text_group.rb', line 128

def inspect
  (
    "#<MoleculeTextGroup: id=#{@molecule.id}, " +
    "alias_groups: #{@alias_groups}, " +
    "plain_groups: #{@plain_groups}, " +
    "bold_groups: #{@bold_groups} >"
  )
end

#interpretObject



36
37
38
# File 'lib/chem_scanner/interpreter/text_group/molecule_text_group.rb', line 36

def interpret
  build_molecule_combis
end

#n_atom_combinations(group) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/chem_scanner/interpreter/text_group/molecule_text_group.rb', line 109

def n_atom_combinations(group)
  pilot_key = group.keys.first
  group.each do |k, v|
    pilot_key = k if v.count > group[pilot_key].count
  end

  combis = []
  group[pilot_key].each_with_index do |_, idx|
    combi = {}
    group.each do |k, v|
      combi[k] = v[idx] unless v[idx].nil?
    end

    combis.push(combi)
  end

  combis
end

#retrieve_alias_groupsObject



27
28
29
30
31
32
33
34
# File 'lib/chem_scanner/interpreter/text_group/molecule_text_group.rb', line 27

def retrieve_alias_groups
  mid = @molecule.id
  if @alias_info.key?(mid)
    @alias_groups = @alias_info[mid].map { |i| i[:group] }.compact
  end

  @alias_groups.reject!(&:empty?)
end