-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandchoosemols.py
executable file
·222 lines (192 loc) · 6.07 KB
/
randchoosemols.py
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#!/usr/bin/env python3
try:
# version 3
from openbabel import openbabel
except ImportError:
# version 2
import openbabel
import os
import sys
import time
import random
import argparse
FEATURES = [
'version 0.1.0 : Randomly choose molecules based on given database',
'version 0.2.0 : add support for DEFLATE compressed files, gz & tgz',
'version 0.3.0 : add option fileinfo',
]
VERSION = FEATURES[-1].split(':')[0].replace('version',' ').strip()
_fms = openbabel.OBConversion().GetSupportedInputFormat()
SUPPORTED_FORMATS = [i.split()[0].lower() for i in _fms]
class ReadFileOBMols:
"""read data from input file
Attributes:
filename(list): input file name
fileobmols(List[openbabel.OBMol, ]):
"""
def __init__(self,filename=None,fileinfo=None,*args,**kwargs):
self.filename = filename
self.fileinfo = True if fileinfo is True else False
def run(self,filename=None):
print(f'Note: input file: {self.filename}')
if self.fileinfo:
print('Note: processing file info')
curtime = time.time()
if filename is None: filename = self.filename
if self.fileinfo:
self.fileobmols = []
self.atomnum = self.readfile(filename,True)
else:
self.fileobmols = self.readfile(filename)
self.atomnum = len(self.fileobmols)
self.runtime = time.time() - curtime
def readfile(self,file,fileinfo=None):
"""read data from file based on extension"""
if file.endswith('.tgz'):
sfile = file.strip('.tgz')
elif file.endswith('.tar.gz'):
sfile = file.strip('.tar.gz')
elif file.endswith('.gz'):
sfile = file.strip('.gz')
else:
sfile = file
bo = False
if '.' in sfile:
ext = sfile.split('.')[-1].lower()
if ext not in SUPPORTED_FORMATS: bo = True
else:
bo = True
if bo:
print('Warning: file not support: ',file)
return []
obconv = openbabel.OBConversion()
obconv.SetInFormat(ext)
if fileinfo:
atomnum = 0
obmol = openbabel.OBMol()
bo = obconv.ReadFile(obmol,file)
while bo:
atomnum += 1
bo = obconv.Read(obmol)
return atomnum
obmollist = []
obmol = openbabel.OBMol()
bo = obconv.ReadFile(obmol,file)
while bo:
obmollist.append(obmol)
# continue reading
obmol = openbabel.OBMol()
bo = obconv.Read(obmol)
return obmollist
class RandChooseMols(ReadFileOBMols):
def __init__(self,n=None,*args,**kwargs):
super().__init__(*args,**kwargs)
self.n = n
def run(self):
super().run()
self.obmols = []
print(f'Note: number of total molecules: {self.atomnum}')
print('Note: ReadFile runtime cost: {:.2f}s'.format(self.runtime))
if not self.fileobmols: return
if self.n > len(self.fileobmols):
print(f'Warning: too many choices: ({self.n} > {len(self.fileobmols)})')
return
self.obmols = random.sample(self.fileobmols,k=self.n)
class SaveFile:
"""save final results"""
def __init__(self,obmols=None,ftype=None,fname=None,*args,**kwargs):
if ftype:
self.ftype = ftype.lower()
if self.ftype not in SUPPORTED_FORMATS:
print('Fatal: currently file format not support: ',self.ftype)
self.ftype = 'sdf'
else:
self.ftype = 'sdf'
if isinstance(obmols,list):
self.obmols = obmols
else:
print('Fatal: not data input')
self.obmols = None
self.fname = fname if fname else 'artchoices'
if self.obmols: self.saveobmols()
def saveobmols(self):
# check existing files to avoid overwriting
fnew = self._get_fnew(self.fname, self.ftype)
fnew += '.' + self.ftype
print('Note: result file is saved to: '+fnew)
obconv = openbabel.OBConversion()
obconv.SetOutFormat(self.ftype)
obconv.WriteFile(self.obmols[0],fnew)
for obmol in self.obmols[1:]:
obconv.Write(obmol)
obconv.CloseOutFile()
def _get_fnew(self,fname,ftype):
i = 0
while True:
i += 1
if not os.path.isfile(fname+'-'+str(i)+'.'+ftype):
break
return fname+'-'+str(i)
def parsecmd():
parser = argparse.ArgumentParser(
description='Random Choose Number of Molecules on Given Database',
allow_abbrev=False,
)
parser.add_argument(
'-v','--version',
action='version',
version=VERSION
)
parser.add_argument(
'-f','--filename',
help='input filename, database'
)
parser.add_argument(
'-n',
type=int,
help='number of choose'
)
parser.add_argument(
'--fileinfo',
action='store_true',
help='show necessary info of input and exit'
)
parser.add_argument(
'--fname',
help='output file basename'
)
parser.add_argument(
'--ftype',
help='output file type, check by option: --show_supported_ftypes',
)
parser.add_argument(
'--show_supported_ftypes',
action='store_true',
help='show supported file output types',
)
parser.add_argument(
'--features',
action='store_true',
help='show development features'
)
if len(sys.argv) == 1:
parser.print_help()
return {}
args = parser.parse_args(sys.argv[1:])
if args.show_supported_ftypes:
print('Supported output file types:')
print('; '.join(SUPPORTED_FORMATS))
return {}
if args.features:
for i in FEATURES: print(i)
return {}
return vars(args)
def main():
args = parsecmd()
if not len(args): return
t = RandChooseMols(**args)
t.run()
args['obmols'] = t.obmols
SaveFile(**args)
if __name__ == '__main__':
main()