-
Notifications
You must be signed in to change notification settings - Fork 3
/
simba3d_make_pdb.py
executable file
·119 lines (108 loc) · 3.92 KB
/
simba3d_make_pdb.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
'''
stand alone script for converting simba3d results into a pdb format which can be read by chimera.
'''
import sys
import numpy as np
import os
from scipy.io import loadmat
try: import simplejson as json # try to import simplejson
except ImportError: import json #otherwise import json
def make_pdb_file_00(outputfilepath,X):
'''
taks a 3xn array and converts it into a pdb format which can be read by chimera.
'''
smallest=abs(X).min()
largest=abs(X).max()
X*=100.0/largest
with open(outputfilepath, 'w') as result:
result.write("Structure Inference from Multiscale BAyes in 3 Dimensions (SIMBA3D)\n")
for ii in range(len(X[0])):
result.write('ATOM ')
result.write('{: 5d}'.format(ii+1))
result.write(' CA MET A'+str(ii+1).ljust(8))
result.write('{: 8.3f}'.format(X[0,ii]))
result.write('{: 8.3f}'.format(X[1,ii]))
result.write('{: 8.3f}'.format(X[2,ii]))
result.write(' 0.20 10.00\n')
for ii in range(len(X[0])-1):
result.write('CONECT')
result.write('{: 5d}'.format(ii+1))
result.write('{: 5d}'.format(ii+2)+'\n')
def printhelp_00():
"""
print the help text for the convertion tool
"""
print('Convert simba3d report output from npz to .mat, or .json')
print('simba3d-make-pdb <list of files>')
def load_result_00(outputfile):
"""
This will load a simba3d result
"""
results_dir='.'
extension = os.path.splitext(outputfile)[1]
summary={}
try:
if extension == '.npy':
data=np.load( os.path.join(results_dir,str(outputfile)))
if 'summary' in data:
summary=data['summary'].item()
else:
summary={'X_evol':[data]}
if extension == '.npz':
data=np.load( os.path.join(results_dir,str(outputfile)))
summary=data['summary'].item()
if extension == '.mat':
summary=loadmat(os.path.join(results_dir,str(outputfile)))
if extension == '.json':
with open(os.path.join(results_dir,str(outputfile)),'r') as result:
summary=dict(json.load(result))
summary['X_evol']=[np.array(X) for X in summary['X_evol']]
summary['initialized_curve']=np.array(summary['initialized_curve'])
except:
summary={}
return summary
def save_data_00(outputfilepath,summary):
"""
This will save a simba3d result
Summary is the result of the experiment
"""
ext=os.path.splitext(outputfilepath)[-1].lower()
if ext=='.pdb':
make_pdb_file_00(outputfilepath,np.array(summary['X_evol'][-1]))
def convert_00(filename,ext_out=".json"):
"""
convert .npz simba3d result into some other supported format
"""
summary=[]
try:
ext_in = os.path.splitext(filename)[1]
if (ext_in == ext_out):
print("\tError input extention matches output extention")
else:
summary=load_result_00(filename)
except:
print("\tError loading "+filename)
if summary:
try:
print("\t saving :"+os.path.splitext(filename)[0]+ext_out)
save_data_00(os.path.splitext(filename)[0]+ext_out,summary)
except:
print("\tError converting "+filename+" to "+ext_out)
else:
print("\tCould not load "+filename)
def main(args=None):
"""
main function executing the convertion tool
"""
if args is None:
args=sys.argv[:]
if ('--help' in args) | ('-h'in args): # check if the help option was called
printhelp_00()
return 0; # do not continue if the help was called
ext_out='.pdb' #
del args[0] # the first arg is just the function call
for filename in args: # loop through all the arguments and convert them
print("Converting "+filename+" to "+ext_out)
convert_00(filename,ext_out)
if __name__ == "__main__":
main()