-
Notifications
You must be signed in to change notification settings - Fork 0
/
mol2parser.py
65 lines (51 loc) · 1.95 KB
/
mol2parser.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
# 10/03/2022 6:47PM
# Parsing Tripos MOL2 files generated from MOE Dock (io_trps.svl 2020.11)
from mol2molclass import Atom, Bond
from auxclass import MolGraph
class Pose:
def __init__(self, mol2block):
self.datablock = mol2block
self.atomblock = mol2block[mol2block.index('@<TRIPOS>ATOM\n') + 1: \
mol2block.index('@<TRIPOS>BOND\n')]
self.atoms = []
for line in self.atomblock:
self.atoms.append(Atom(line))
atom_dic = {}
for atom in self.atoms:
atom_dic[atom.AtInd] = atom
self.atomdic = atom_dic
self.bondblock = mol2block[mol2block.index('@<TRIPOS>BOND\n') + 1: \
mol2block.index('@<TRIPOS>SUBSTRUCTURE\n')]
self.bonds = []
for line in self.bondblock:
self.bonds.append(Bond(line))
self.propertyblock = mol2block[mol2block.index('@<TRIPOS>PROPERTY_DATA\n') + 1:]
prop_dic = {}
for line in self.propertyblock:
if '\t|\t' in line:
items = line.split('\t|\t')
if len(items) > 1:
propval = items[1].replace('\n','')
try:
propval = float(propval)
except:
propval = str(propval)
prop_dic[items[0]] = propval
else:
prop_dic[items[0]] = ''
self.propertydic = prop_dic
self.molgraph = MolGraph(self)
def splitmol2(mol2file):
with open(mol2file, 'r') as f:
datalines = f.readlines()
poselist = []
singleblock = []
for line in datalines:
if line == '@<TRIPOS>MOLECULE\n':
if len(singleblock) > 0:
poselist.append(Pose(singleblock))
singleblock = []
singleblock.append(line)
if len(singleblock) > 0:
poselist.append(Pose(singleblock))
return poselist