-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtools.py
155 lines (135 loc) · 5.37 KB
/
tools.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""tools
Tools related to loading designs from TOML formatted configuration files. Most
methods require a running OSPREY instance. This instance can be started with:
`osprey.start()`
"""
##################################################
# Version
# 0.0
# Author
# Graham Holt
#
# Contact Info:
# Bruce Donald
# Duke University
# Department of Computer Science
# Levine Science Research Center (LSRC)
# Durham
# NC 27708-0129
# USA
# e-mail: www.cs.duke.edu/brd/
#
# <signature of Bruce Donald>, Mar 1, 2018
# Bruce Donald, Professor of Computer Science
#
##################################################
##################################################
# Imports
##################################################
import osprey
import toml
from os.path import join, dirname, abspath
import multiprocessing
##################################################
# Globals
##################################################
app_path = dirname(abspath(__file__))
PDB = join(app_path, "resources/pdb")
DESIGN = join(app_path, "designs")
TEST = join(app_path, "tests")
##################################################
# Classes / Functions
##################################################
def load_confspaces(f, xtal_rotamers=True, continuous=False, force_wt=True):
"""Loads OSPREY ConfSpace objects from a TOML file.
OPTIONS:
xtal_rotamers: if true, add the rotamers from the input pdb
continuous: if true, set continuous rotamers
force_wt: if true, always add the wild-type amino acid
"""
config = toml.load(f)
return loadd_confspaces(config,
xtal_rotamers=xtal_rotamers,
continuous=continuous,
force_wt=force_wt)
def loadd_confspaces(config, xtal_rotamers=True, continuous=False, force_wt=True):
"""Loads OSPREY ConfSpace objects from a config dictionary.
OPTIONS:
xtal_rotamers: if true, add the rotamers from the input pdb
continuous: if true, set continuous rotamers
force_wt: if true, always add the wild-type amino acid
"""
ffparams = osprey.ForcefieldParams()
mol = osprey.readPdb(join(PDB, config["molecule"]))
template_library = osprey.TemplateLibrary(ffparams.forcefld)
# Make sure we don't have 3 strands (some cfs files do)
if len(config["strand_definitions"]) > 2:
exit()
# Define the protein strand
protein = osprey.Strand(mol,
templateLib=template_library,
residues=config["strand_definitions"]["strand0"]
)
for resi, res_allowed in config["strand_mutations"]["strand0"].items():
# Add the osprey.WILD_TYPE object to the allowed residues if desired
if force_wt:
res_allowed.append(osprey.WILD_TYPE)
# Set the flexibility
protein.flexibility[resi]\
.setLibraryRotamers(*res_allowed)
if xtal_rotamers:
protein.flexibility[resi].addWildTypeRotamers()
if continuous:
protein.flexibility[resi].setContinuous()
# Define the ligand strand
ligand = osprey.Strand(mol,
templateLib=template_library,
residues=config["strand_definitions"]["strand1"]
)
for resi, res_allowed in config["strand_mutations"]["strand1"].items():
# Add the osprey.WILD_TYPE object to the allowed residues if desired
if force_wt:
res_allowed.append(osprey.WILD_TYPE)
# Set the flexibility
ligand.flexibility[resi]\
.setLibraryRotamers(*res_allowed)
if xtal_rotamers:
ligand.flexibility[resi].addWildTypeRotamers()
if continuous:
ligand.flexibility[resi].setContinuous()
# Build spaces
return {'protein' : osprey.ConfSpace(protein),
'ligand' : osprey.ConfSpace(ligand),
'complex': osprey.ConfSpace([protein, ligand]),
'ffparams' : ffparams
}
def make_emat(fi, fo):
"""Save complex energy matrix based on TOML file"""
confspaces = load_confspaces(fi)
parallelism = osprey.Parallelism(cpuCores=multiprocessing.cpu_count())
makeo_emat(confspaces["complex"],
confspaces["ffparams"],
parallelism,
fo)
def maked_emat(config, fo):
"""Save complex energy matrix based on dictionary"""
confspaces = loadd_confspaces(config)
parallelism = osprey.Parallelism(cpuCores=multiprocessing.cpu_count())
makeo_emat(confspaces["complex"],
confspaces["ffparams"],
parallelism,
fo)
def makeo_emat(confspace, ffparams, parallelism, fo, continuous=False):
"""Save energy matrix based on OSPREY objects"""
ecalc = osprey.EnergyCalculator(confspace,
ffparams,
parallelism,
isMinimizing=continuous)
eref = osprey.ReferenceEnergies(confspace, ecalc)
conf_ecalc = osprey.ConfEnergyCalculator(confspace,
ecalc,
referenceEnergies=eref)
emat = osprey.EnergyMatrix(conf_ecalc,
cacheFile=fo)