Class: ChemScanner::ChemDraw::FragmentNode

Inherits:
BaseNode
  • Object
show all
Defined in:
lib/chem_scanner/chem_draw/node/fragment_node.rb

Overview

CDX Node parser

Constant Summary

Constants included from BaseValue

BaseValue::ARROW_NOGO_CROSS, BaseValue::CDXML_ARROW_TYPE, BaseValue::CDXML_ATOM_EXTERNAL_CONNECTION_TYPE, BaseValue::CDXML_CDX_POINT, BaseValue::CDXML_GRAPHIC_TYPE, BaseValue::CDXML_LINE_TYPE, BaseValue::CDXML_NODE_TYPE, BaseValue::CDXML_ORBITAL_TYPE, BaseValue::CDXML_OVAL_TYPE, BaseValue::TEXT_ATTRIBUTES

Instance Attribute Summary collapse

Attributes inherited from BaseNode

#id, #parser, #parser_type, #polygon

Instance Method Summary collapse

Methods inherited from BaseNode

#assign_center, #bounding_box, #cdx_read, #cdxml_read, #center_x, #center_y, #get_tempid, #pre_parse_node, #read, #set_cdx, #set_cdxml

Methods included from BaseValue

#binary_chunks, #cdx_styles, #cdx_text, #cdxml_text, #do_unhandled, #point_2d, #point_3d, #polygon_from_bb, #read_bounding_box, #read_ids, #read_int, #read_type, #read_value

Constructor Details

#initialize(parser, parser_type, id) ⇒ FragmentNode



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/chem_scanner/chem_draw/node/fragment_node.rb', line 17

def initialize(parser, parser_type, id)
  super(parser, parser_type, id)

  @num_hydrogens = -1
  @atnum = -1
  @spin = 0
  @charge = 0
  @iso = 0
  @color = 0
  @type = -1
  @ext_type = -1
  @alias_text = ""
  @warning = false
  @warning_data = ""
  @is_alias = false

  @nested_fragment = {}
  @nested_text = {}
  @expanded = false
  @is_polymer = false
end

Instance Attribute Details

#alias_textObject

Returns the value of attribute alias_text.



12
13
14
# File 'lib/chem_scanner/chem_draw/node/fragment_node.rb', line 12

def alias_text
  @alias_text
end

#atnumObject

Returns the value of attribute atnum.



12
13
14
# File 'lib/chem_scanner/chem_draw/node/fragment_node.rb', line 12

def atnum
  @atnum
end

#chargeObject

Returns the value of attribute charge.



12
13
14
# File 'lib/chem_scanner/chem_draw/node/fragment_node.rb', line 12

def charge
  @charge
end

#colorObject

Returns the value of attribute color.



12
13
14
# File 'lib/chem_scanner/chem_draw/node/fragment_node.rb', line 12

def color
  @color
end

#expandedObject

Returns the value of attribute expanded.



12
13
14
# File 'lib/chem_scanner/chem_draw/node/fragment_node.rb', line 12

def expanded
  @expanded
end

#ext_typeObject

Returns the value of attribute ext_type.



12
13
14
# File 'lib/chem_scanner/chem_draw/node/fragment_node.rb', line 12

def ext_type
  @ext_type
end

#fragmentObject

Returns the value of attribute fragment.



12
13
14
# File 'lib/chem_scanner/chem_draw/node/fragment_node.rb', line 12

def fragment
  @fragment
end

#is_aliasObject

Returns the value of attribute is_alias.



12
13
14
# File 'lib/chem_scanner/chem_draw/node/fragment_node.rb', line 12

def is_alias
  @is_alias
end

#is_polymerObject

Returns the value of attribute is_polymer.



12
13
14
# File 'lib/chem_scanner/chem_draw/node/fragment_node.rb', line 12

def is_polymer
  @is_polymer
end

#isoObject

Returns the value of attribute iso.



12
13
14
# File 'lib/chem_scanner/chem_draw/node/fragment_node.rb', line 12

def iso
  @iso
end

#nested_fragmentObject

Returns the value of attribute nested_fragment.



12
13
14
# File 'lib/chem_scanner/chem_draw/node/fragment_node.rb', line 12

def nested_fragment
  @nested_fragment
end

#nested_textObject

Returns the value of attribute nested_text.



12
13
14
# File 'lib/chem_scanner/chem_draw/node/fragment_node.rb', line 12

def nested_text
  @nested_text
end

#num_hydrogensObject

Returns the value of attribute num_hydrogens.



12
13
14
# File 'lib/chem_scanner/chem_draw/node/fragment_node.rb', line 12

def num_hydrogens
  @num_hydrogens
end

#pointObject

Returns the value of attribute point.



12
13
14
# File 'lib/chem_scanner/chem_draw/node/fragment_node.rb', line 12

def point
  @point
end

#spinObject

Returns the value of attribute spin.



12
13
14
# File 'lib/chem_scanner/chem_draw/node/fragment_node.rb', line 12

def spin
  @spin
end

#typeObject

Returns the value of attribute type.



12
13
14
# File 'lib/chem_scanner/chem_draw/node/fragment_node.rb', line 12

def type
  @type
end

#warningObject

Returns the value of attribute warning.



12
13
14
# File 'lib/chem_scanner/chem_draw/node/fragment_node.rb', line 12

def warning
  @warning
end

#warning_dataObject

Returns the value of attribute warning_data.



12
13
14
# File 'lib/chem_scanner/chem_draw/node/fragment_node.rb', line 12

def warning_data
  @warning_data
end

#xObject

Returns the value of attribute x.



12
13
14
# File 'lib/chem_scanner/chem_draw/node/fragment_node.rb', line 12

def x
  @x
end

#yObject

Returns the value of attribute y.



12
13
14
# File 'lib/chem_scanner/chem_draw/node/fragment_node.rb', line 12

def y
  @y
end

Instance Method Details

#cloneObject



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/chem_scanner/chem_draw/node/fragment_node.rb', line 121

def clone
  cloned = self.class.new(@parser, @parser_type, nil)
  cloned.num_hydrogens = @num_hydrogens
  cloned.atnum = @atnum
  cloned.spin = @spin
  cloned.charge = @charge
  cloned.iso = @iso
  cloned.color = @color
  cloned.type = @type
  cloned.alias_text = @alias_text
  cloned.warning = @warning
  cloned.warning_data = @warning_data
  cloned.is_alias = @is_alias
  cloned.expanded = @expanded

  cloned.nested_fragment = {}
  cloned.nested_text = {}
  @nested_fragment.each { |k, v| cloned.nested_fragment[k] = v }
  @nested_text.each { |k, v| cloned.nested_text[k] = v }

  cloned
end

#has_nil_coord?Boolean



104
105
106
# File 'lib/chem_scanner/chem_draw/node/fragment_node.rb', line 104

def has_nil_coord?
  (@x.nil? || @y.nil?) && @polygon.nil?
end

#leftbottomObject



96
97
98
# File 'lib/chem_scanner/chem_draw/node/fragment_node.rb', line 96

def leftbottom
  @polygon.nil? ? point : @polygon.bounding_box.leftbottom
end

#parse_node(tag, nid, data) ⇒ Object

rubocop:disable Methods/PerceivedComplexity



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
72
73
74
75
76
# File 'lib/chem_scanner/chem_draw/node/fragment_node.rb', line 40

def parse_node(tag, nid, data)
  ref = @props_ref[tag]
  ref = ref.nil? ? @obj_ref[tag] : ref

  case ref
  when "Node_Element" then @atnum = read_value(tag, data)
  when "Atom_Radical" then @spin = read_value(tag, data)
  when "Atom_Isotope" then @iso = read_value(tag, data)
  when "Fragment"
    frag = Fragment.new(@parser, @parser_type, nid)
    frag.read
    @nested_fragment[nid] = frag
  when "Atom_GenericNickname"
    nickname = send("#{@parser_type}_text", data)
    @generic_nickname = nickname.first[:text] unless nickname.empty?
  when "Node_Type"
    @type = read_type(tag, data, CDXML_NODE_TYPE)
    @is_alias = ALIAS_VALUES.include?(@type)
  when "2DPosition" then @x, @y = read_value(tag, data)
  when "Atom_Charge" then @charge = read_value(tag, data)
  when "Text"
    @text = Text.new(@parser, @parser_type, nid, true)
    # NOTE: MUST read first in order to maintain CDX reader
    @text.read
    @polygon = @text.polygon

    @nested_text[@text.id] = @text
  when "ChemicalWarning"
    @warning = true
    @warning_data = @parser_type == "cdxml" ? data.text : data
  when "Atom_NumHydrogens" then @num_hydrogens = read_value(tag, data)
  when "ForegroundColor" then @color = read_value(tag, data)
  when "Atom_ExternalConnectionType"
    @ext_type = read_type(tag, data, CDXML_ATOM_EXTERNAL_CONNECTION_TYPE)
  else do_unhandled(tag)
  end
end

#post_parse_nodeObject

rubocop:enable Methods/PerceivedComplexity



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/chem_scanner/chem_draw/node/fragment_node.rb', line 79

def post_parse_node
  @point = Geometry::Point.new(@x, @y)

  if !@text.nil? && !@text.value.empty?
    @alias_text = @text.value
    return
  end

  interpreter = ChemScanner::Interpreter
  if !@generic_nickname.nil? &&
      interpreter.rgroup_atom?(@generic_nickname)
    @is_alias = true
    @type = 7
    @alias_text = @generic_nickname
  end
end

#righttopObject



100
101
102
# File 'lib/chem_scanner/chem_draw/node/fragment_node.rb', line 100

def righttop
  @polygon.nil? ? point : @polygon.bounding_box.righttop
end

#set_expandedObject



112
113
114
# File 'lib/chem_scanner/chem_draw/node/fragment_node.rb', line 112

def set_expanded
  @expanded = true
end

#set_is_polymerObject



116
117
118
119
# File 'lib/chem_scanner/chem_draw/node/fragment_node.rb', line 116

def set_is_polymer
  @is_alias = true
  @is_polymer = true
end

#set_type(type) ⇒ Object



108
109
110
# File 'lib/chem_scanner/chem_draw/node/fragment_node.rb', line 108

def set_type(type)
  @type = type
end