Class: Chem::CDX::CDX

Inherits:
Object
  • Object
show all
Includes:
Molecule
Defined in:
lib/chem/db/cdx.rb

Constant Summary

Constants included from Molecule

Molecule::DESCRIPTORNAME, Molecule::ELEMNUM, Molecule::EpsHeader, Molecule::MDLCountLineFormat, Molecule::MDLHeaderLine2Format

Instance Attribute Summary

Attributes included from Molecule

#cdk2atom, #cdk_mol, #name, #ob_mol, #source

Attributes included from Graph

#adjacencies, #edges, #nodes

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Molecule

#-, #adjacent_index, #assign_2d_geometry, #bit_mat, #box_size, #breadth_first_search, #canonical_ring, #cdk_BCUT, #cdk_CPSA, #cdk_RotatableBondsCount, #cdk_calc_descriptor, #cdk_calc_descriptors, #cdk_find_all_rings, #cdk_fingerprint, #cdk_gasteiger_marsili_partial_charges, #cdk_generate_2D, #cdk_generate_randomly, #cdk_generate_vicinity, #cdk_hose_code, #cdk_hueckel, #cdk_mcs, #cdk_properties, #cdk_rule_of_file, #cdk_save_as, #cdk_setup, #cdk_sssr, #cdk_wiener_numbers, #cdk_xlogp, #composition, #connected?, #deep_dup, #delete, #delete_bond, #depth_first_search, #divide, #f_dfs, #find_smallest_ring, #find_sssr, #fingerprint, #generate_pubchem_subskey, #hilight, #induced_sub, #match, #match_by_ullmann, #method_missing, #molecular_weight, #n_hydrogen, #ob_export_as, #ob_save_as, #oxidation_number, #pubchem_subskeys, #remove_hydrogens!, #save, #save_as_mdl, #save_as_pdf, #subset_in_composition?, #to_cansmi, #to_eps, #to_inchi, #to_sybyl, #typ_str, #use_open_babel

Methods included from Graph

#adjacent_to, #clustering_coefficient, #morgan, #terminal_nodes

Constructor Details

#initializeCDX

Returns a new instance of CDX.



1328
1329
1330
1331
1332
1333
# File 'lib/chem/db/cdx.rb', line 1328

def initialize
  @mols = Array.new
  @end_object = [0x0000].pack('s')

  @object_pool = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Chem::Molecule

Class Method Details

.cdx_create_object(object_type, str) ⇒ Object



1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
# File 'lib/chem/db/cdx.rb', line 1231

def self.cdx_create_object object_type, str
  case object_type
  when :CDXCoordinate
    return CDXCoordinate.new(str.unpack('l')[0])
  when :CDXPoint2D
    xy = str.unpack("V2")
    return CDXPoint2D.new(xy[1], xy[0])
  when :CDXPoint3D#fix me
    return 'fix me'
  when :CDXRectangle
    bound = str.unpack("V4")
    return CDXRectangle.new(bound[1], bound[0], bound[3], bound[2])
  when :CDXBoolean
    return CDXBoolean.new(str[0] == 0 ? false : true)
  when :CDXBooleanImplied
    return CDXBooleanImplied.new(str[0] == 0 ? false : true)
  when :CDXColorTable # fix me
    ct = CDXColorTable.new
    kaisu = str.unpack("v")[0]
    start = 2
    1.upto(kaisu) do |n|
      rgb = str[start, 6].unpack("v3")
      ct.push(rgb[0], rgb[1], rgb[2])
      start += 6
    end
    return ct
  when :CDXCurvePoints #fix me
    return 'fix me'
  when :CDXCurvePoints3D #fix me
    return 'fix me'
  when :CDXElementList #fix me
    return 'fix me'
  when :CDXFontTable #fix me
    font_table = CDXFontTable.new
    font_table.platform = str[0..1].unpack('v')[0]
    n = str[2..3].unpack('v')[0]
    start = 4
    n.times do |n|
      font = Font_class.new
      font.font_id = str[start, 2].unpack('v')[0]
      font.charset = str[start+2, 2].unpack('v')[0]
      font.length = str[start+4, 2].unpack('v')[0]
      font.name = str[start+6, font.length].unpack('a*')[0]
      #          p font.name
      start += 6 + font.length
      font_table.push_font(font)
    end
    return font_table
  when :CDXFormula #fix me
    return 'fix me'
  when :INT8
    return Int8.new(str.unpack('c')[0])
  when :UINT32
    return Uint32.new(str.unpack("V")[0])
  when :INT32
    return Int32.new(str.unpack("V")[0])
  when :UINT16
    return Uint16.new(str.unpack("v")[0])
  when :INT16
    return Int16.new(str.unpack("s")[0])
  when :UINT8
    return Uint8.new(str.unpack('C')[0])
  when :FLOAT64
    return Float64.new(str.unpack('d')[0])# fix me
  when :CDXObjectIDArray
    return CDXObjectIDArray.new(str.unpack('V*'))
  when :CDXObjectIDArrayWithCounts #fix me
    return 'fix me'
  when :CDXObjectID
    return CDXObjectID.new(str.unpack('L')[0])
  when :CDXRepresentsProperty #fix me
    return 'fix me'
  when :CDXString
    cdxstring = CDXString.new
    n = str[0..1].unpack('v')[0]
    n.times do |nn|
      start = 2 + 10 * nn
      cdxstring.push_style(str[start, 10].unpack('v5'))
    end
    cdxstring.push_string(str[(2+10*n)..-1].unpack('a*')[0])
    return cdxstring
  when :CDXFontStyle# fix me
    return CDXFontStyle.new(str[0, 10].unpack('v4'))
  when :CDXDate# fix me
    return 'fix me'
  when :CDXLineStarts# fix me
    return 'fix me'
  when :INT16ListWithCounts# fix me
    return 'fix me'
  when :Unformatted
    return Unformatted.new(str.unpack('H*')[0].upcase)
  else
    p object_type
    return str
  end
end

.open(filename) ⇒ Object



1215
1216
1217
# File 'lib/chem/db/cdx.rb', line 1215

def self.open filename
  CDX.new filename
end

Instance Method Details

#cdxml_create_object(object_type, att) ⇒ Object



1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
# File 'lib/chem/db/cdx.rb', line 1384

def cdxml_create_object object_type, att
  case object_type
  when :CDXStyle
    p att
  when :CDXObject
    p att
  when :CDXCoordinate
    return CDXCoordinate.new(att.to_i)
  when :CDXPoint2D
    return CDXPoint2D.new(att.split[0].to_i * 65536, att.split[1].to_i * 65536)
  when :CDXPoint3D#fix me
    return 'fix me'
  when :CDXRectangle
    bound = att.split
    return CDXRectangle.new(bound[0].to_i * 65536, bound[0].to_i * 65536, bound[3].to_i * 65536, bound[2].to_i * 65536)
  when :CDXBoolean
    return CDXBoolean.new(att == "yes")
  when :CDXBooleanImplied
    return CDXBooleanImplied.new(att == "yes")
    #       when :CDXColorTable # fix me
    #         ct = CDXColorTable.new
    #         p ct
    #         1.upto(kaisu) do |n|
    #           rgb = str[start, 6].unpack("v3")
    #           ct.push(rgb[0], rgb[1], rgb[2])
    #           start += 6
    #         end
    #         return ct
    #       when :CDXCurvePoints #fix me
    #         return 'fix me'
    #       when :CDXCurvePoints3D #fix me
    #         return 'fix me'
    #       when :CDXElementList #fix me
    #         return 'fix me'
    #       when :CDXFontTable #fix me
    #         font_table = CDXFontTable.new
    #         font_table.platform = str[0..1].unpack('v')[0]
    #         n = str[2..3].unpack('v')[0]
    #         start = 4
    #         font_class = Struct.new("Font", :font_id, :charset, :length, :name)
    #         n.times do |n|
    #           font = font_class.new
    #           font.font_id = str[start, 2].unpack('v')[0]
    #           font.charset = str[start+2, 2].unpack('v')[0]
    #           font.length = str[start+4, 2].unpack('v')[0]
    #           font.name = str[start+6, font.length].unpack('a*')[0]
    # #          p font.name
    #           start += 6 + font.length
    #           font_table.push_font(font)
    #         end
    #         return font_table
    #       when :CDXFormula #fix me
    #         return 'fix me'
  when :INT8
    return Int8.new(att.to_i)
  when :UINT32
    return Uint32.new(att.to_i)
  when :INT32
    return Int32.new(att.to_i)
  when :UINT16
    return Uint16.new(att.to_i)
  when :INT16
    return Int16.new(att.to_i)
  when :UINT8
    return Uint8.new(att.to_i)
  when :FLOAT64
    return Float64.new(att.to_i)
  when :CDXObjectIDArray
    return CDXObjectIDArray.new(att.split.collect{|i| i.to_i})
    #       when :CDXObjectIDArrayWithCounts #fix me
    #         return 'fix me'
  when :CDXObjectID
    return CDXObjectID.new(att.to_i)
    #       when :CDXRepresentsProperty #fix me
    #         return 'fix me'
  when :CDXString# may be fix me
    cdxstring = CDXString.new
    cdxstring.push_string(att)
    return cdxstring
    #         n = str[0..1].unpack('v')[0]
    #         n.times do |nn|
    #           start = 2 + 10 * nn
    #           cdxstring.push_style(str[start, 10].unpack('v5'))
    #         end
    #         cdxstring.push_string(str[(2+10*n)..-1].unpack('a*')[0])
    #         return cdxstring
    #       when :CDXFontStyle# fix me
    #         return CDXFontStyle.new(str[0, 10].unpack('v4'))
    #       when :CDXDate# fix me
    #         return 'fix me'
    #       when :CDXLineStarts# fix me
    #         return 'fix me'
    #       when :INT16ListWithCounts# fix me
    #         return 'fix me'
  when :Unformatted
    return Unformatted.new(att)
  else
    p object_type
    return "cdxml_create_object not supported data type! please fix me!"
  end
end

#cdxml_open(filename) ⇒ Object



1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
# File 'lib/chem/db/cdx.rb', line 1335

def cdxml_open(filename)
  require 'rexml/document'

  @object_id = 0
  @objects = parent = CDXObject.new('CDXML', @object_id)
  @object_id += 1

  file = File.new(filename, "r")
  doc = REXML::Document.new(file)
  doc.elements.each do |element|
    parent.objects[@object_id] = recurse_cdxml(element, parent)
  end
  #      p parent
end

#eachObject



1350
1351
1352
# File 'lib/chem/db/cdx.rb', line 1350

def each
  # must return each mol
end

#make_pObject



1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
# File 'lib/chem/db/cdx.rb', line 1184

def make_p
  @objects = CDXObject.new('CDXML', 0)
  object_id = 1
  cdxstring = CDXString.new
  cdxstring.push_string("test.cdxml")
  @objects.properties["Name"] = cdxstring
  page = CDXObject.new('page', object_id)
  @objects.objects[object_id] = page
  object_id += 1
  atomlist = {}
  @mols.last.atoms.each do |atom|
    #      1.upto(10) do |number|
    n = CDXObject.new('n', object_id)
    atomlist[atom] = n
    n.properties['Z'] = Int16.new(object_id)
    n.properties['p'] = CDXPoint2D.new((atom.x + 30) * 10 * 65536, (atom.y + 30) * 10 * 65536)
    n.properties['AS'] = Int16.new(1)
    n.properties['Element'] = Int16.new(atom.atomic_number)
    page.objects[object_id] = n
    object_id += 1
  end
  @mols.last.bonds.each do |bond|
    b = CDXObject.new('b', object_id)
    b.properties['E'] = CDXObjectID.new(atomlist[bond.e].object_id)
    b.properties['B'] = CDXObjectID.new(atomlist[bond.b].object_id)
    b.properties['Order'] = Int16.new(bond.v)
    page.objects[object_id] = b
    object_id += 1
  end
end

#objectsObject



1223
1224
1225
# File 'lib/chem/db/cdx.rb', line 1223

def objects
  @objects
end

#open(filename) ⇒ Object



1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
# File 'lib/chem/db/cdx.rb', line 1486

def open(filename)
  input = File.open(filename, 'r')
  input.read(8)
  input.read(4)
  input.read(16)

  @objects = parent = CDXObject.new('CDXML', 0)
  parent.parent = parent

  sp = 0
  while !input.eof? do
    tag = input.read(2).unpack("v")[0]
    if tag == 0
      sp -= 1
      parent = parent.parent
    elsif (tag & 0x8000) == 0 # Property
      num = input.read(2).unpack("v")[0]
      bytes = input.read(num)
      puts "null number %04x " % tag if Cdx_value2name[tag] == nil
      STDOUT.flush
      parent.properties[Cdx_value2name[tag][0]] =
        CDX.cdx_create_object(Cdx_value2name[tag][1], bytes)
    elsif tag == nil
      next
    else                   # Object
      id = input.read(4).unpack('V')[0]
      object = CDXObject.new(Cdx_value2name[tag][0], id)
      object.name = Cdx_value2name[tag][0]
      @object_pool[id] = object
      parent.objects[id] = object
      object.parent = parent
      parent = object
    end
  end
  self
end

#poolObject



1227
1228
1229
# File 'lib/chem/db/cdx.rb', line 1227

def pool
  @object_pool
end

#push_mol(mol) ⇒ Object



1168
1169
1170
# File 'lib/chem/db/cdx.rb', line 1168

def push_mol mol
  @mols.push(mol)
end

#recurse_cdxml(element, parent) ⇒ Object



1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
# File 'lib/chem/db/cdx.rb', line 1354

def recurse_cdxml element, parent
  obj = CDXObject.new(element.name, 0)
  case element.name
  when 'color'#fix me
  when 's'#fix me
    obj.properties['']
  when 'font'#fix me
  else
    element.attributes.each do |key, att|
      if 'id' == key
        obj.object_id = att.to_i
      else
        #            puts "%s %s" % [key, $cdx_name2value[key].inspect]
        obj.properties[key] = cdxml_create_object(Cdx_value2name[Cdx_name2value[key]][1], att)
      end
    end
  end
  element.elements.each do |el|
    if el.attributes['id']
      object_id = el.attributes['id']# fix me
    else
      object_id =  @object_id
      @object_id += 1
    end
    obj.objects[@object_id] = recurse_cdxml(el, obj)
    @object_id += 1
  end
  obj
end

#set_id(id) ⇒ Object



1180
1181
1182
# File 'lib/chem/db/cdx.rb', line 1180

def set_id id
  return [id].pack('V')
end

#to_binObject



1172
1173
1174
# File 'lib/chem/db/cdx.rb', line 1172

def to_bin
  return Bin_header + @objects.to_bin
end

#to_xmlObject



1176
1177
1178
# File 'lib/chem/db/cdx.rb', line 1176

def to_xml
  return Xml_header + @objects.to_xml
end