/usr/lib/python2.7/dist-packages/chempy/sdf.py is in pymol 1.7.0.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | #A* -------------------------------------------------------------------
#B* This file contains source code for the PyMOL computer program
#C* copyright 1998-2000 by Warren Lyford Delano of DeLano Scientific.
#D* -------------------------------------------------------------------
#E* It is unlawful to modify or remove this copyright notice.
#F* -------------------------------------------------------------------
#G* Please see the accompanying LICENSE file for further information.
#H* -------------------------------------------------------------------
#I* Additional authors of this source file include:
#-*
#-*
#-*
#Z* -------------------------------------------------------------------
import string
import re
import copy
from chempy import io
class SDFRec:
def __init__(self,sdflist):
getkee = re.compile("^>\s+<([^>]*)>")
gettag = re.compile("^>\s+<[^>]*>\s+\((.*)\)")
ll = len(sdflist)
if ll<4:
print " SDFRec: invalid SDF record format #1"
raise RuntimeError
self.kees = ['MOL'] # separate key list to preserve order
self.data = {}
self.ref_code = {}
self.data['MOL'] = []
mol = self.data['MOL']
l = 0
while l<ll:
mol.append(sdflist[l])
if (sdflist[l][0:6]=='M END') or (sdflist[l][0:5]=='M END'):
break;
if sdflist[l][0:1]=='>':
mol[len(mol)-1]='M END\n'
sdflist.insert(l,'M END\n')
ll=len(sdflist)
break;
l = l + 1
if l>=ll:
print " SDFRec: invalid SDF record format #2"
raise RuntimeError
while l<ll:
if sdflist[l][0]=='>':
sl = sdflist[l]
kee_match = getkee.match(sl)
if not kee_match:
print " SDFRec: invalid SDF record format #3"
raise RuntimeError
kee = kee_match.group(1)
self.kees.append(kee)
ref_code_match = gettag.match(sl)
if ref_code_match:
self.ref_code[kee] = ref_code_match.group(1)
else:
self.ref_code[kee] = ''
self.data[kee] = []
sd = self.data[kee]
l = l + 1
while l<ll:
if len(string.strip(sdflist[l]))!=0:
sd.append(sdflist[l])
l = l + 1
else:
break;
else:
l = l + 1
def toList(self):
r = []
for k in self.kees:
if k!='MOL':
if self.ref_code[k]!='':
r.append("> <"+k+"> ("+self.ref_code[k]+")\n")
else:
r.append("> <"+k+">\n")
for a in self.data[k]:
r.append(a)
if k!='MOL':
r.append("\n")
return r
def get(self,kee):
if self.data.has_key(kee):
return self.data[kee]
else:
return None
def get_single(self,kee): # automatic stripping
if self.data.has_key(kee):
sdk = self.data[kee]
if len(sdk):
return string.strip(sdk[0])
else:
return None
else:
return None
def set_single(self,kee,data,ref_code=None): # adds LF
self.set(kee,[data+'\n'],ref_code)
def get_model(self):
return io.mol.fromList(self.get('MOL'))
def set_model(self,model):
self.set('MOL',io.mol.toList(model))
def set(self,kee,data,ref_code=None):
if kee not in self.kees:
self.kees.append(kee)
self.ref_code[kee]=''
if ref_code!=None:
self.ref_code[kee]=ref_code
self.data[kee] = copy.deepcopy(data)
def delete(self,kee):
self.kees.remove(kee)
del self.data[kee]
class SDF:
def __init__(*args):
mode = 'r'
if len(args)<2:
raise ValueError
self = args[0]
fname = args[1]
if len(args)==3:
mode = args[2]
self.mode = mode
self.at_eof = 0
if mode not in ('w','r','wa','pf','url'):
print " SDF: bad mode"
return None
if mode=='pf': # pseudofile
self.file = fname
elif (mode[0:1]=='r') and (string.find(fname,':')>1):
# does this look like a URL? (but not a DOS path)
from urllib import urlopen
self.file = urlopen(fname)
else:
self.file = open(fname,mode)
def write(self,rec):
lst = rec.toList()
for a in lst:
self.file.write(a)
self.file.write('$$$$\n')
def read(self): # returns SDFRec or None at end of file
cur = []
while 1:
s = self.file.readline()
if not s:
return None
elif s[0:4]==r'$$$$':
return SDFRec(cur)
else:
cur.append(s)
def close(self):
self.file.close()
|