Package Chem :: Module CDXMLWriter
[hide private]
[frames] | no frames]

Source Code for Module Chem.CDXMLWriter

  1  # $Id: CDXMLWriter.py 455 2007-12-18 06:37:48Z glandrum $ 
  2  # 
  3  # Copyright (C) 2002-2006 greg Landrum and Rational Discovery LLC 
  4  # 
  5  #   @@ All Rights Reserved  @@ 
  6  # 
  7  """  
  8   
  9  Dumping CDXML from python 
 10   
 11  """ 
 12  from elementtree.SimpleXMLWriter import XMLWriter 
 13  import Chem 
 14   
 15  header = """<?xml version="1.0" encoding="UTF-8" ?> 
 16  <!DOCTYPE CDXML SYSTEM "http://www.camsoft.com/xml/cdxml.dtd" > 
 17  """ 
 18  _fontsStartId=0 
 19   
20 -def DumpAtom(atom,w,highlightAtoms=[], 21 highlightParms={'color':'4','size':'18'}):
22 global _fontsStartId 23 attrs = {'id':str(atom.GetIdx())} 24 if atom.GetAtomicNum()!=6: 25 attrs['Element'] = str(atom.GetAtomicNum()) 26 if atom.GetFormalCharge()!=0: 27 attrs['Charge'] = str(atom.GetFormalCharge()) 28 #if atom.ExplicitHydrogenCount()!=0: 29 # attrs['NumHydrogens'] = str(atom.ExplicitHydrogenCount()) 30 # attrs['ImplicitHydrogens'] = "0" 31 w.start('n',attrs) 32 if highlightAtoms and atom.GetIdx() in highlightAtoms: 33 attrs = {} 34 attrs.update(highlightParms) 35 attrs['font']=attrs.get('font',str(_fontsStartId)) 36 w.start('t') 37 w.start('s',attrs) 38 w.data(atom.GetSymbol()) 39 w.end() 40 w.end() 41 w.end()
42
43 -def DumpBond(bond,w,baseIdx,highlightBonds=[], 44 highlightParms={'color':'4','LineWidth':'2'}):
45 attrs = {'id':str(bond.GetIdx()+baseIdx)} 46 if bond.GetBondType()!=Chem.BondType.AROMATIC: 47 attrs['Order'] = str(int(bond.GetBondType())); 48 else: 49 attrs['Order'] = str(1.5) 50 attrs['Display'] = "Dash" 51 attrs['B'] = str(bond.GetBeginAtomIdx()) 52 attrs['E'] = str(bond.GetEndAtomIdx()) 53 bondDir = bond.GetBondDir() 54 if bondDir==Chem.BondDir.BEGINWEDGE: 55 attrs['Display'] = "WedgeBegin" 56 elif bondDir==Chem.BondDir.ENDWEDGE: 57 attrs['Display'] = "WedgeEnd" 58 if bond.GetIdx() in highlightBonds: 59 attrs.update(highlightParms) 60 w.start('b',attrs) 61 w.end()
62
63 -def DumpFrag(frag,w,extrasId,highlightBonds=[],highlightAtoms=[]):
64 w.start('fragment',id=str(extrasId)) 65 extrasId+=1 66 for at in frag.GetAtoms(): 67 DumpAtom(at,w,highlightAtoms=highlightAtoms) 68 for bond in frag.GetBonds(): 69 DumpBond(bond,w,frag.GetNumAtoms()+1,highlightBonds=highlightBonds) 70 w.end()
71 72
73 -def MolToCDXML(mol,outF,highlightBonds=[],highlightAtoms=[]):
74 global header,_fontsStartId 75 outF.write(header) 76 w = XMLWriter(outF) 77 extrasId = mol.GetNumAtoms()+mol.GetNumBonds()+2 78 cdxml = w.start('CDXML') 79 w.start('colortable') 80 w.element('color',r="1",g="1",b="1") 81 w.element('color',r="0",g="0",b="0") 82 w.element('color',r="1",g="0",b="0") 83 w.element('color',r="0",g="0",b="1") 84 w.element('color',r="1",g="1",b="0") 85 w.element('color',r="1",g="0",b="1") 86 w.end() 87 w.start('fonttable') 88 _fontsStartId=extrasId 89 w.element('font',id=str(extrasId),charset="iso-8859-1",name="Arial") 90 extrasId+=1 91 w.element('font',id=str(extrasId),charset="iso-8859-1",name="Times New Roman") 92 extrasId+=1 93 w.end() 94 w.start('page',id=str(extrasId)) 95 extrasId+=1 96 DumpFrag(mol,w,extrasId,highlightBonds=highlightBonds, 97 highlightAtoms=highlightAtoms) 98 w.end() 99 w.close(cdxml)
100 101 if __name__ == '__main__': 102 import sys,cStringIO 103 try: 104 from utils import chemdraw 105 except: 106 chemdraw = None 107 import Chem 108 for smi in ['c1cnccc1','C1=CN=CC=C1']: 109 print '---------------------------------' 110 m = Chem.MolFromSmiles(smi) 111 io = cStringIO.StringIO() 112 MolToCDXML(m,io,highlightBonds=[1,2,3],highlightAtoms=[3]) 113 txt = io.getvalue() 114 txt = txt.replace('><','>\n<') 115 cdxml = txt 116 if chemdraw: 117 chemdraw.CDXDisplay(cdxml) 118 print cdxml 119