Package Chem
[hide private]
[frames] | no frames]

Source Code for Package Chem

  1  # $Id: __init__.py 608 2008-04-10 08:12:17Z glandrum $ 
  2  # 
  3  #  Copyright (C) 2000-2008  greg Landrum and Rational Discovery LLC 
  4  # 
  5  #   @@ All Rights Reserved  @@ 
  6  # 
  7  """ A module for molecules and stuff 
  8   
  9   see Chem/index.html in the doc tree for documentation 
 10    
 11  """ 
 12  import rdBase 
 13  import RDConfig 
 14  import Numeric 
 15  import DataStructs 
 16  from Geometry import rdGeometry 
 17  import PeriodicTable as pyPeriodicTable 
 18  import rdchem 
 19  _HasSubstructMatchStr=rdchem._HasSubstructMatchStr 
 20  from rdchem import * 
 21  from rdmolfiles import * 
 22  from rdmolops import * 
 23   
24 -def GetSmartsMatchCDXML(mol,patt,maps,which=0,showAllAtoms=0):
25 try: 26 from Chem import CDXMLWriter 27 except: 28 CDXMLWriter = None 29 if CDXMLWriter is None: 30 return '' 31 try: 32 from cStringIO import StringIO 33 except: 34 from StringIO import StringIO 35 if not showAllAtoms and (which < 0 or not len(maps) or len(maps)<which+1): 36 return '' 37 io = StringIO() 38 matchBonds = [] 39 matchAtoms = [] 40 if not showAllAtoms: 41 mapL = maps[which] 42 for bondIdx in range(patt.GetNumBonds()): 43 bnd = patt.GetBondWithIdx(bondIdx) 44 begIdx = bnd.GetBeginAtomIdx() 45 endIdx = bnd.GetEndAtomIdx() 46 molBegIdx = mapL[begIdx] 47 molEndIdx = mapL[endIdx] 48 matchBonds.append(mol.GetBondBetweenAtoms(molBegIdx,molEndIdx).GetIdx()) 49 else: 50 for mapL in maps: 51 for idx in mapL: 52 if idx not in matchAtoms: matchAtoms.append(idx) 53 54 CDXMLWriter.MolToCDXML(mol,io,highlightBonds=matchBonds, 55 highlightAtoms=matchAtoms) 56 cdxml = io.getvalue() 57 cdxml = cdxml.replace('><','>\n<') 58 return cdxml
59
60 -def DisplaySmartsMatch(mol,patt,maps,which=0,showAllAtoms=0):
61 try: 62 from utils import chemdraw 63 except: 64 chemdraw = None 65 cdxml = GetSmartsMatchCDXML(mol,patt,maps,which=which,showAllAtoms=showAllAtoms) 66 if cdxml and chemdraw: 67 chemdraw.CDXDisplay(cdxml) 68 return cdxml
69
70 -def QuickSmartsMatch(smi,sma,unique=1,display=0):
71 m = MolFromSmiles(smi) 72 p = MolFromSmarts(sma) 73 res = m.GetSubstructMatches(p,unique) 74 if display: 75 DisplaySmartsMatch(m,p,res) 76 return res
77
78 -def CanonSmiles(smi,useChiral=1):
79 m = MolFromSmiles(smi) 80 return MolToSmiles(m,useChiral)
81
82 -def SmilesRoundtrip(smi,useChiral=1):
83 m = MolFromSmiles(smi) 84 refSmi = MolToSmiles(m,useChiral) 85 m2 = MolFromSmiles(refSmi) 86 smi = MolToSmiles(m2,useChiral) 87 if smi!=refSmi: 88 print refSmi 89 print smi 90 return refSmi==smi
91
92 -def SupplierFromFilename(fileN,delim='',**kwargs):
93 ext = fileN.split('.')[-1].lower() 94 if ext=='sdf': 95 suppl = SDMolSupplier(fileN,**kwargs) 96 elif ext=='csv': 97 if not delim: 98 delim = ',' 99 suppl = SmilesMolSupplier(fileN,delimiter=delim,**kwargs) 100 elif ext=='txt': 101 if not delim: 102 delim='\t' 103 suppl = SmilesMolSupplier(fileN,delimiter=delim,**kwargs) 104 elif ext=='tdt': 105 suppl = TDTMolSupplier(fileN,delimiter=delim,**kwargs) 106 else: 107 raise ValueError,"unrecognized extension: %s"%ext 108 109 return suppl
110
111 -def FindMolChiralCenters(mol):
112 """ 113 >>> mol = Chem.MolFromSmiles('[C@H](Cl)(F)Br') 114 >>> FindMolChiralCenters(mol) 115 [(0, 'R')] 116 >>> mol = Chem.MolFromSmiles('[C@@H](Cl)(F)Br') 117 >>> FindMolChiralCenters(mol) 118 [(0, 'S')] 119 120 >>> FindMolChiralCenters(Chem.MolFromSmiles('CCC')) 121 [] 122 123 """ 124 AssignAtomChiralCodes(mol) 125 centers = [] 126 for atom in mol.GetAtoms(): 127 if atom.HasProp('_CIPCode'): 128 centers.append((atom.GetIdx(),atom.GetProp('_CIPCode'))) 129 return centers
130