def transform_atom( self, a, doc): out = doc.createElement( 'atom') try: atom = self.CML_atom( cml=a) except cml_exception, detail: # problems converting cml to atom raise plugin.import_exception( detail)
def transform_bond( self, b, doc): out = doc.createElement( 'bond') try: bond = self.CML_bond( cml=b) except cml_exception, detail: # problems converting cml to bond raise plugin.import_exception( str( detail))
def transform_CML_bond( self, bond, doc): """called by transform_bond in order to do the transform from CML_bond instance to CDML""" if bond.not_enough_data(): # bond does not have sufficient data to be safely converted to cdml raise plugin.import_exception( "missing "+str( bond.not_enough_data())+" in bond specification") out = doc.createElement( 'bond') type = (bond.stereo.lower() or 'n') + str( bond.order) dom_ext.setAttributes( out, (('type', type), ('start', bond.atom1), ('end', bond.atom2))) return out
class CML_importer( plugin.importer): """Imports a CML (Chemical Markup Language) document, uses version 1.0 of the CML standard.""" doc_string = _("Imports a CML (Chemical Markup Language) document, uses version 1.0 of the CML standard.") def __init__( self): # this makes implementing CML2 much easier - just supply different CML_atom and CML_bond self.CML_atom = CML_atom self.CML_bond = CML_bond self.xs = [] self.ys = [] def on_begin( self): self.xs = [] self.ys = [] self.atoms = [] return 1 def get_cdml_dom( self, file_name): tree = dom.parse( file_name) out = dom.Document() root = dom_ext.elementUnder( out, 'cdml', (('version','0.11'),)) for m in tree.getElementsByTagName( 'molecule'): out_m = self.transform_molecule( m, out) if out_m: root.appendChild( out_m) view = self.on_end_set_viewport() if view: viewport = out.createElement( 'viewport') viewport.setAttribute( 'viewport', '%f %f %f %f' % view) root.insertBefore( viewport, root.firstChild) return root def transform_molecule( self, mol, doc): #atoms out = doc.createElement( 'molecule') for a in mol.getElementsByTagName( 'atom'): out_a = self.transform_atom( a, doc) if out_a: out.appendChild( out_a) # bonds ar = mol.getElementsByTagName( 'bondArray') if ar: ar = ar[0] for b in ar.getElementsByTagName( 'bond'): out_b = self.transform_bond( b, doc) if out_b: out.appendChild( out_b) else: for b in self.add_nonexisting_bonds(): out.appendChild( b) return out def transform_atom( self, a, doc): out = doc.createElement( 'atom') try: atom = self.CML_atom( cml=a) except cml_exception, detail: # problems converting cml to atom raise plugin.import_exception( detail) if atom.not_enough_data(): # atom does not have sufficient data to be safely converted to cdml raise plugin.import_exception( "missing "+str( atom.not_enough_data())+" in atom specification") self.atoms.append( atom) dom_ext.setAttributes( out, (('name', atom.symbol), ('id', atom.id))) pnt = dom_ext.elementUnder( out, 'point', (('x', str( atom.x)), ('y', str( atom.y)))) if atom.z != None: pnt.setAttribute( 'z', str( atom.z)) self.xs.append( atom.x) self.ys.append( atom.y) return out