forked from mattasmith/SCHEMA-RASPP
/
schemaenergy.py
231 lines (197 loc) · 7.93 KB
/
schemaenergy.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
221
222
223
224
225
226
227
228
229
230
231
#! /usr/local/bin/python
"""Script for calculating SCHEMA energies.
******************************************************************
Copyright (C) 2005 Allan Drummond, California Institute of Technology
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*******************************************************************
SCHEMA was developed in the laboratory of Frances H. Arnold at the California Institute of Technology.
References:
Voigt, C. et al., "Protein building blocks preserved by recombination," Nature Structural Biology 9(7):553-558 (2002).
Meyer, M. et al., "Library analysis of SCHEMA-guided recombination," Protein Science 12:1686-1693 (2003).
Otey, C. et al., "Functional evolution and structural conservation in chimeric cytochromes P450: Calibrating a structure-guided approach," Chemistry & Biology 11:1-20 (2004)
Silberg, J. et al., "SCHEMA-guided protein recombination," Methods in Enzymology 388:35-42 (2004).
Endelman, J. et al., "Site-directed protein recombination as a shortest-path problem," Protein Engineering, Design & Selection 17(7):589-594 (2005).
"""
import sys, string, os
import pdb, schema
ARG_PRINT_E = 'E'
ARG_PRINT_M = 'm'
ARG_PDB_ALIGNMENT_FILE = 'pdbal'
ARG_PARENT_INDEX = 'p'
ARG_CHIMERAS = 'chim'
ARG_CROSSOVER_FILE = 'xo'
ARG_MULTIPLE_SEQUENCE_ALIGNMENT_FILE = 'msa'
ARG_CONTACT_FILE = 'con'
ARG_RANDOM_SEED = 'seed'
ARG_OUTPUT_FILE = 'o'
ARG_HELP = 'help'
def parse_arguments(args):
# Turn linear arguments into a dictionary of (option, [values,...]) pairs
arg_dict = {}
key = None
for arg in args[1:]:
if arg[0] == '-':
key = arg[1:]
arg_dict[key] = None
else:
if arg_dict.has_key(key):
if arg_dict[key]:
if type(arg_dict[key]) is list:
arg_dict[key] = arg_dict[key]+[arg]
else:
arg_dict[key] = [arg_dict[key],arg]
else:
arg_dict[key] = arg
else:
arg_dict[key] = arg
return arg_dict
def print_usage(args):
print 'Usage: python', args[0].split(os.path.sep)[-1], ' [options]'
print 'Options:\n', \
'\t-%s <alignment file>\n' % ARG_MULTIPLE_SEQUENCE_ALIGNMENT_FILE, \
'\t-%s <contact file>\n' % ARG_CONTACT_FILE, \
'\t-%s <crossover file>\n' % ARG_CROSSOVER_FILE, \
'\t[-%s <chimera list>]\n' % ARG_CHIMERAS, \
'\t[-%s]\n' % ARG_PRINT_E, \
'\t[-%s]\n' % ARG_PRINT_M, \
'\t[-%s <output file>]' % ARG_OUTPUT_FILE
def confirm_arguments(arg_dict):
# Are arguments okay?
res = True
arg_keys = arg_dict.keys()
try:
if len(arg_keys) == 0:
res = False
return
if not ARG_MULTIPLE_SEQUENCE_ALIGNMENT_FILE in arg_keys:
print " You must provide a library file (-%s <file>)" % ARG_MULTIPLE_SEQUENCE_ALIGNMENT_FILE
res = False
elif not os.path.isfile(arg_dict[ARG_MULTIPLE_SEQUENCE_ALIGNMENT_FILE]):
print " Can't find library file %s" % arg_dict[ARG_MULTIPLE_SEQUENCE_ALIGNMENT_FILE]
res = False
if not ARG_CROSSOVER_FILE in arg_keys:
print " You must provide a crossover file (-%s <file>)" % ARG_CROSSOVER_FILE
res = False
elif not os.path.isfile(arg_dict[ARG_CROSSOVER_FILE]):
print " Can't find crossover file %s" % arg_dict[ARG_CROSSOVER_FILE]
res = False
if not ARG_CONTACT_FILE in arg_keys:
print " You must provide a contact file (-%s <file>)" % ARG_CONTACT_FILE
res = False
elif not os.path.isfile(arg_dict[ARG_CONTACT_FILE]):
print " Can't find contact file %s" % arg_dict[ARG_CONTACT_FILE]
res = False
if not (arg_dict.has_key(ARG_PRINT_E) or arg_dict.has_key(ARG_PRINT_M)):
print " No output specified; use -E to print disruption and/or -m to print mutation"
res = False
except Exception, e:
raise e
res = False
return res
def outputEnergies(chimera_blocks, contacts, fragments, parents, output_file, output_string, print_E, print_m):
if not schema.checkChimera(chimera_blocks, fragments, parents):
output_file.write("# %s is not a valid chimera\n" % chimera_blocks)
return
output_vars = [chimera_blocks]
E = None
m = None
if print_E:
E = schema.getChimeraDisruption(chimera_blocks, contacts, fragments, parents)
output_vars = output_vars + [E]
if print_m:
m = schema.getChimeraShortestDistance(chimera_blocks, fragments, parents)
output_vars = output_vars + [m]
#print output_vars
output_file.write(output_string % tuple(output_vars))
return (E,m)
def main(args):
arg_dict = parse_arguments(args)
if not confirm_arguments(arg_dict):
if args[0].split(os.path.sep)[-1] == "schemaenergy.py":
print_usage(args)
return
# Flags and values
print_E = False
print_m = False
output_file = sys.stdout
# Inputs:
# The alignment/fragment file name.
msa_file = arg_dict[ARG_MULTIPLE_SEQUENCE_ALIGNMENT_FILE]
if arg_dict.has_key(ARG_PRINT_E):
print_E = True
if arg_dict.has_key(ARG_PRINT_M):
print_m = True
# Read the alignment file to create a list of parents.
# The parents will appear in the list in the order in which they appear in the file.
parent_list = schema.readMultipleSequenceAlignmentFile(file(msa_file, 'r'))
parents = [p for (k,p) in parent_list]
crossovers = schema.readCrossoverFile(file(arg_dict[ARG_CROSSOVER_FILE], 'r'))
fragments = schema.getFragments(crossovers, parents[0])
# Get the contacts
pdb_contacts = schema.readContactFile(file(arg_dict[ARG_CONTACT_FILE], 'r'))
contacts = schema.getSCHEMAContactsWithCrossovers(pdb_contacts, parents, crossovers)
if arg_dict.has_key(ARG_OUTPUT_FILE):
output_file = file(arg_dict[ARG_OUTPUT_FILE], 'w')
# Now, what does the user want?
output_string = '%s'
output_file.write('# chimera')
if print_E:
output_string += '\t%d'
output_file.write('\tE')
if print_m:
output_string += '\t%d'
output_file.write('\tm')
output_string += '\n'
output_file.write('\n')
if arg_dict.has_key(ARG_CHIMERAS): # Print values for chimeras
chimeras = arg_dict[ARG_CHIMERAS]
# Could be a) a chimera, b) a list of chimeras, or c) a file of chimeras.
if type(chimeras) is list:
# It's a list of chimeras
for chimera_blocks in chimeras:
outputEnergies(chimera_blocks, contacts, fragments, parents, output_file, output_string, print_E, print_m)
elif os.path.isfile(chimeras):
# It's a file of chimeras
for line in file(chimeras,'r').readlines():
chimera_blocks = line.strip()
outputEnergies(chimera_blocks, contacts, fragments, parents, output_file, output_string, print_E, print_m)
else:
# It's a single chimera sequence
chimera_blocks = chimeras
outputEnergies(chimera_blocks, contacts, fragments, parents, output_file, output_string, print_E, print_m)
else:
# Enumerates all possible chimeras and their disruption and mutation values.
p = len(parents)
n = len(fragments)
Es = []
ms = []
for i in xrange(len(parents)**len(fragments)):
# The next two lines turn i into a chimera block pattern
# (e.g., 0 -> '11111111', 1 -> '11111112', 2 -> '11111113'...)
n2c = schema.base(i,p)
chimera_blocks = ''.join(['1']*(n-len(n2c))+['%d'%(int(x)+1,) for x in n2c])
(E, m) = outputEnergies(chimera_blocks, contacts, fragments, parents, output_file, output_string, print_E, print_m)
if (print_E):
Es.append(E)
if (print_m):
ms.append(m)
if (print_E):
mean_str = "# Average disruption <E> = %1.4f\n" % schema.mean(Es)
output_file.write(mean_str)
if (print_m):
mean_str = "# Average mutation <m> = %1.4f\n" % schema.mean(ms)
output_file.write(mean_str)
if arg_dict.has_key(ARG_OUTPUT_FILE):
output_file.close()
def main_wrapper():
main(sys.argv)
main_wrapper()