-
Notifications
You must be signed in to change notification settings - Fork 5
/
setup.py
195 lines (170 loc) · 5.64 KB
/
setup.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
# This source code is part of the Biotite package and is distributed
# under the 3-Clause BSD License. Please see 'LICENSE.rst' for further
# information.
import fnmatch
import os
import pickle
import warnings
from os.path import abspath, dirname, isfile, join, normpath
import biotite.structure as struc
import biotite.structure.info as info
import msgpack
import numpy as np
from biotite.structure.info.ccd import get_ccd
from Cython.Build import cythonize
from setuptools import Extension, find_packages, setup
# Molecules that appear is most structures
# Hence, there is a high importance to get the hydrogen conformations
# for these molecules right
# Fragments from these molecules will overwrite any existing fragments
# from the standard library
PROMINENT_MOLECULES = [
# Amino acids
"ALA",
"ARG",
"ASN",
"ASP",
"CYS",
"GLN",
"GLU",
"GLY",
"HIS",
"ILE",
"LEU",
"LYS",
"MET",
"PHE",
"PRO",
"SER",
"THR",
"TRP",
"TYR",
"VAL",
# Nucleotides
"A",
"C",
"G",
"T",
"U",
"DA",
"DC",
"DG",
"DT",
"DU",
# Solvent
"HOH",
]
original_wd = os.getcwd()
# Change directory to setup directory to ensure correct file identification
os.chdir(dirname(abspath(__file__)))
# Import is relative to working directory
from src.hydride import AtomNameLibrary, FragmentLibrary, __version__
# Compile Cython into C
try:
cythonize("src/**/*.pyx", include_path=[np.get_include()], language_level=3)
except ValueError:
# This is a source distribution and the directory already contains
# only C files
pass
def get_extensions():
ext_sources = []
for dirpath, _, filenames in os.walk(normpath(join("src", "hydride"))):
for filename in fnmatch.filter(filenames, "*.c"):
ext_sources.append(os.path.join(dirpath, filename))
ext_names = [
source.replace("src" + normpath("/"), "")
.replace(".c", "")
.replace(normpath("/"), ".")
for source in ext_sources
]
ext_modules = [
Extension(ext_names[i], [ext_sources[i]], include_dirs=[np.get_include()])
for i in range(len(ext_sources))
]
return ext_modules
def get_protonation_variants():
with open("prot_variants.msgpack", "rb") as file:
molecule_data = msgpack.unpack(file, use_list=False, raw=False)
molecules = []
for molecule_dict in molecule_data.values():
molecule = struc.AtomArray(len(molecule_dict["res_name"]))
molecule.add_annotation("charge", int)
molecule.res_name = molecule_dict["res_name"]
molecule.atom_name = molecule_dict["atom_name"]
molecule.element = molecule_dict["element"]
molecule.charge = molecule_dict["charge"]
molecule.hetero = molecule_dict["hetero"]
molecule.coord[:, 0] = molecule_dict["coord_x"]
molecule.coord[:, 1] = molecule_dict["coord_y"]
molecule.coord[:, 2] = molecule_dict["coord_z"]
molecule.bonds = struc.BondList(
molecule.array_length(),
bonds=np.stack(
[
molecule_dict["bond_i"],
molecule_dict["bond_j"],
molecule_dict["bond_type"],
]
).T,
)
molecules.append(molecule)
return molecules
def get_mol_names_in_ccd():
ccd = get_ccd()
atom_category = ccd["chem_comp_atom"]
return np.unique(atom_category["comp_id"].as_array()).tolist()
# Compile fragment library
fragment_file_path = join("src", "hydride", "fragments.pickle")
if not isfile(fragment_file_path):
std_fragment_library = FragmentLibrary()
# Add protonation variants at first because the bond lengths
# for neutral fragments might vary slightly
for mol in get_protonation_variants():
std_fragment_library.add_molecule(mol)
mol_names = get_mol_names_in_ccd() + PROMINENT_MOLECULES
with warnings.catch_warnings():
warnings.simplefilter("ignore")
for i, mol_name in enumerate(mol_names):
if not i % 100:
print(f"Compiling fragment library... ({i}/{len(mol_names)})", end="\r")
try:
mol = info.residue(mol_name)
except KeyError:
continue
std_fragment_library.add_molecule(mol)
print("Compiling fragment library... Done" + " " * 20)
with open(fragment_file_path, "wb") as fragments_file:
pickle.dump(std_fragment_library._frag_dict, fragments_file)
# Compile atom name library
names_file_path = join("src", "hydride", "names.pickle")
if not isfile(names_file_path):
std_name_library = AtomNameLibrary()
with warnings.catch_warnings():
warnings.simplefilter("ignore")
# To reduce the size of the atom name library, only atom names
# from prominent molecules are stored
for i, mol_name in enumerate(PROMINENT_MOLECULES):
print(
f"Compiling atom name library... "
f"({i+1}/{len(PROMINENT_MOLECULES)})",
end="\r",
)
try:
mol = info.residue(mol_name)
except KeyError:
continue
std_name_library.add_molecule(mol)
print("Compiling atom name library... Done" + " " * 20)
with open(names_file_path, "wb") as names_file:
pickle.dump(std_name_library._name_dict, names_file)
setup(
version=__version__,
zip_safe=False,
packages=find_packages("src"),
package_dir={"": "src"},
ext_modules=get_extensions(),
# Include fragment and atom name libraries
package_data={"hydride": ["*.pickle"]},
)
# Return to original directory
os.chdir(original_wd)