Class: ChemScanner::PerkinEln
- Inherits:
-
Object
- Object
- ChemScanner::PerkinEln
- Defined in:
- lib/chem_scanner/perkin_eln.rb
Overview
Read and Parse ChemDraw ELN XML
Instance Attribute Summary collapse
-
#molecules ⇒ Object
Returns the value of attribute molecules.
-
#reactions ⇒ Object
Returns the value of attribute reactions.
-
#scheme_list ⇒ Object
Returns the value of attribute scheme_list.
-
#version ⇒ Object
Returns the value of attribute version.
Instance Method Summary collapse
- #add_details(key, details) ⇒ Object
- #add_reaction_details(group, detail) ⇒ Object
- #do_cdxml(cdxml) ⇒ Object
- #do_object(object) ⇒ Object
- #do_object_detail(object) ⇒ Object
- #do_property_instances(prop_instances) ⇒ Object
- #do_section(section) ⇒ Object
- #do_table_props(cols) ⇒ Object
- #do_table_rows(rows) ⇒ Object
- #do_table_section(table) ⇒ Object
-
#initialize ⇒ PerkinEln
constructor
A new instance of PerkinEln.
- #read(file, is_path = true) ⇒ Object
- #read_value(min, max, val) ⇒ Object
- #refine_data(infos) ⇒ Object
- #to_cml(molecule_only = false) ⇒ Object
- #try_add_molecule_details(key, detail) ⇒ Object
- #try_add_solvent(detail) ⇒ Object
- #try_match_smi_with_mol(detail) ⇒ Object
Constructor Details
#initialize ⇒ PerkinEln
Returns a new instance of PerkinEln.
8 9 10 11 12 13 14 15 16 |
# File 'lib/chem_scanner/perkin_eln.rb', line 8 def initialize super @molecules = [] @reactions = [] @scheme_list = [] @resolved_ids = [] end |
Instance Attribute Details
#molecules ⇒ Object
Returns the value of attribute molecules.
6 7 8 |
# File 'lib/chem_scanner/perkin_eln.rb', line 6 def molecules @molecules end |
#reactions ⇒ Object
Returns the value of attribute reactions.
6 7 8 |
# File 'lib/chem_scanner/perkin_eln.rb', line 6 def reactions @reactions end |
#scheme_list ⇒ Object
Returns the value of attribute scheme_list.
6 7 8 |
# File 'lib/chem_scanner/perkin_eln.rb', line 6 def scheme_list @scheme_list end |
#version ⇒ Object
Returns the value of attribute version.
6 7 8 |
# File 'lib/chem_scanner/perkin_eln.rb', line 6 def version @version end |
Instance Method Details
#add_details(key, details) ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/chem_scanner/perkin_eln.rb', line 177 def add_details(key, details) return if details.nil? || details.empty? if details.class == Array unresolved = [] details.each do |detail| mol = try_add_molecule_details(key, detail) unresolved.push(detail) if mol.nil? end unresolved.each do |detail| mol = try_match_smi_with_mol(detail) add_reaction_details(key, detail) if mol.nil? end else add_reaction_details(key, details) end end |
#add_reaction_details(group, detail) ⇒ Object
273 274 275 276 277 278 279 |
# File 'lib/chem_scanner/perkin_eln.rb', line 273 def add_reaction_details(group, detail) group = "Reaction Description" if %w[Reactants Products].include?(group) reaction = @reactions.last reaction.details[group] = [] if reaction.details[group].nil? reaction.details[group].push(detail) end |
#do_cdxml(cdxml) ⇒ Object
139 140 141 142 143 144 |
# File 'lib/chem_scanner/perkin_eln.rb', line 139 def do_cdxml(cdxml) cp = Cdxml.new cp.read(cdxml, false) cp end |
#do_object(object) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/chem_scanner/perkin_eln.rb', line 53 def do_object(object) obj_type = object.at_xpath("field")["name"] details = do_object_detail(object) obj_details = case obj_type when "Preparation" then details["styledText"] when "Reaction Conditions" then details["propertyInstances"] when "Reaction" then details["chemicalStructure"] else details["tableSection"] end { obj_type => obj_details } end |
#do_object_detail(object) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/chem_scanner/perkin_eln.rb', line 67 def do_object_detail(object) details = {} object.element_children.each do |child| cname = child.name val = case cname when "styledText" then child.at_xpath("./text").text when "chemicalStructure" then do_cdxml(child.content) when "propertyInstances" then do_property_instances(child) when "tableSection" then do_table_section(child) end details[cname] = val unless val.nil? end details end |
#do_property_instances(prop_instances) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/chem_scanner/perkin_eln.rb', line 84 def do_property_instances(prop_instances) props = {} prop_instances.xpath("./propertyInstance").each do |prop| pname = prop.at_xpath("property")["name"] val = read_value(prop["minValue"], prop["maxValue"], prop["value"]) props[pname] = val unless val.nil? end props end |
#do_section(section) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/chem_scanner/perkin_eln.rb', line 33 def do_section(section) section_info = {} section_type = section.at_xpath("sectionType")["name"] section.xpath("./object").each do |child| obj_info = do_object(child) if obj_info.key?("Preparation") prep = obj_info.delete("Preparation") key = "Reaction Conditions" section_info[key] = {} unless section_info.key?(key) section_info[key]["Preparation"] = prep else section_info.merge!(obj_info) end end { "type" => section_type, "details" => section_info } end |
#do_table_props(cols) ⇒ Object
115 116 117 118 119 120 121 122 123 |
# File 'lib/chem_scanner/perkin_eln.rb', line 115 def do_table_props(cols) props = [] cols.each do |col| pname = col.at_xpath("property")["name"] props.push(pname) end props end |
#do_table_rows(rows) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/chem_scanner/perkin_eln.rb', line 125 def do_table_rows(rows) rows_values = [] rows.each do |row| = row.at_xpath("./tags") = { "ID" => nil, "parentID" => nil } if .nil? values = row.xpath("./tableCell").map { |x| read_value(x["minValue"], x["maxValue"], x["value"]) } rows_values.push([["ID"], ["parentID"]].concat(values)) end rows_values end |
#do_table_section(table) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/chem_scanner/perkin_eln.rb', line 96 def do_table_section(table) infos = [] tprops = do_table_props(table.xpath("./tableProperty")) props = %w[ID parentID].concat(tprops) values = do_table_rows(table.xpath("./tableRow")) values.each do |rvalue| info = {} rvalue.each_with_index do |val, idx| info[props[idx]] = val end infos.push(info) end infos end |
#read(file, is_path = true) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/chem_scanner/perkin_eln.rb', line 18 def read(file, is_path = true) fs = is_path ? File.open(file) : file xml = Nokogiri::XML(fs) infos = [] sections = xml.xpath("//section") sections.each do |section| info = do_section(section) infos.push(info) end refine_data(infos) true end |
#read_value(min, max, val) ⇒ Object
146 147 148 149 150 151 152 |
# File 'lib/chem_scanner/perkin_eln.rb', line 146 def read_value(min, max, val) mm = min.nil? && max.nil? return nil if mm && val.nil? return val if mm min == max ? val : `${min} ~ ${max}` end |
#refine_data(infos) ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/chem_scanner/perkin_eln.rb', line 154 def refine_data(infos) infos.each do |section| section_details = section["details"] cp = section_details["Reaction"] oscheme = OpenStruct.new( cdxml: cp, molecules: cp.molecules, reactions: cp.reactions, ) @scheme_list.push(oscheme) @molecules.concat(cp.molecules) @reactions.concat(cp.reactions) section_details.each do |key, details| next if key == "Reaction" add_details(key, details) end end end |
#to_cml(molecule_only = false) ⇒ Object
281 282 283 284 285 |
# File 'lib/chem_scanner/perkin_eln.rb', line 281 def to_cml(molecule_only = false) objs = molecule_only ? @molecules : @reactions cml = ChemScanner::Export::CML.new(objs, molecule_only) cml.process end |
#try_add_molecule_details(key, detail) ⇒ Object
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/chem_scanner/perkin_eln.rb', line 197 def try_add_molecule_details(key, detail) id_str = detail["ID"] if id_str.nil? if key == "Solvents" return try_add_solvent(detail) else return add_reaction_details(key, detail) end end id = id_str.to_i mol = @molecules.detect { |m| m.id == id } return nil if mol.nil? @resolved_ids.push(id) detail.delete("Chemical Structure") mol.details = detail end |
#try_add_solvent(detail) ⇒ Object
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/chem_scanner/perkin_eln.rb', line 217 def try_add_solvent(detail) name = detail["Name"] return if name.nil? || name.empty? smiles = ChemScanner.get_abbreviation(name) return if smiles.empty? rw_mol = RDKitChem::RWMol.mol_from_smiles(smiles) mdl = rw_mol.mol_to_mol_block(true, -1, false) mol = OpenStruct.new( text: "", label: "", mdl: mdl, cano_smiles: smiles, details: detail.merge("Solvent": true), ) @molecules.push(mol) @reactions.first.reagents.push(mol) end |
#try_match_smi_with_mol(detail) ⇒ Object
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/chem_scanner/perkin_eln.rb', line 238 def try_match_smi_with_mol(detail) return nil if !detail.key?("Name") || detail["Name"].empty? name = detail["Name"].gsub(/\[.*\]/, "").strip return nil if name.nil? || name.empty? smiles = ChemScanner.get_abbreviation(name) return if smiles.empty? rw_mol = RDKitChem::RWMol.mol_from_smiles(smiles) cano_smiles = rw_mol.mol_to_smiles(true) smiles_arr = cano_smiles.split(".") fragments = [] @molecules.reject { |m| @resolved_ids.include?(m.id) }.each do |m| next unless smiles_arr.include?(m.cano_smiles) fragments.push(m) end return nil if fragments.empty? || fragments.size != smiles_arr.size mol = fragments.first fragments.each do |frag| next if frag.id == mol.id @molecules.delete_if { |m| frag.id == m.id } @reactions.each { |reaction| reaction.delete_molecule_by_id(frag.id) } mol.add(frag) end mol.update_output_formats mol.details = detail @molecules.push(mol) end |