Skip to content

Commit 6352706

Browse files
authored
Merge pull request #10 from sastrys1/main
adjusted init.py so calculate_pka function could be imported in code
2 parents fc4ac3c + dc42860 commit 6352706

File tree

4 files changed

+114
-124
lines changed

4 files changed

+114
-124
lines changed

pkaani/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@
99
__all__ = ['ani_descriptors','ase_io_proteindatabank_mod', 'pkaani','run','prep_pdb']
1010
__version__ = "0.1.0"
1111

12+
from pkaani.pkaani import calculate_pka

pkaani/pkaani.py

+26-26
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,34 @@
1414

1515
def calculate_pka(pdbfiles,writefile=None):
1616

17-
18-
# device to run the training
19-
device = torch.device('cpu')
20-
21-
print("Loading pKa-ANI Models and ANI-2x...")
22-
#FEATURES
23-
tyr_features=joblib.load(os.path.join(os.path.dirname(__file__),'models/FTYR.joblib'))
24-
asp_features=joblib.load(os.path.join(os.path.dirname(__file__),'models/FASP.joblib'))
25-
glu_features=joblib.load(os.path.join(os.path.dirname(__file__),'models/FGLU.joblib'))
26-
lys_features=joblib.load(os.path.join(os.path.dirname(__file__),'models/FLYS.joblib'))
27-
his_features=joblib.load(os.path.join(os.path.dirname(__file__),'models/FHIS.joblib'))
28-
29-
#MODELS
30-
asp_model=joblib.load(os.path.join(os.path.dirname(__file__),'models/ASP_ani2x_FINAL_MODEL_F100.joblib'))
31-
glu_model=joblib.load(os.path.join(os.path.dirname(__file__),'models/GLU_ani2x_FINAL_MODEL_F75.joblib'))
32-
his_model=joblib.load(os.path.join(os.path.dirname(__file__),'models/HIS_ani2x_FINAL_MODEL_F100.joblib'))
33-
lys_model=joblib.load(os.path.join(os.path.dirname(__file__),'models/LYS_ani2x_FINAL_MODEL_F25.joblib'))
34-
tyr_model=joblib.load(os.path.join(os.path.dirname(__file__),'models/TYR_ani2x_FINAL_MODEL_F25.joblib'))
35-
36-
#######################################################################
37-
#call ani
17+
if len(pdbfiles) != 0:
18+
# device to run the training
19+
device = torch.device('cpu')
20+
21+
print("Loading pKa-ANI Models and ANI-2x...")
22+
#FEATURES
23+
tyr_features=joblib.load(os.path.join(os.path.dirname(__file__),'models/FTYR.joblib'))
24+
asp_features=joblib.load(os.path.join(os.path.dirname(__file__),'models/FASP.joblib'))
25+
glu_features=joblib.load(os.path.join(os.path.dirname(__file__),'models/FGLU.joblib'))
26+
lys_features=joblib.load(os.path.join(os.path.dirname(__file__),'models/FLYS.joblib'))
27+
his_features=joblib.load(os.path.join(os.path.dirname(__file__),'models/FHIS.joblib'))
28+
29+
#MODELS
30+
asp_model=joblib.load(os.path.join(os.path.dirname(__file__),'models/ASP_ani2x_FINAL_MODEL_F100.joblib'))
31+
glu_model=joblib.load(os.path.join(os.path.dirname(__file__),'models/GLU_ani2x_FINAL_MODEL_F75.joblib'))
32+
his_model=joblib.load(os.path.join(os.path.dirname(__file__),'models/HIS_ani2x_FINAL_MODEL_F100.joblib'))
33+
lys_model=joblib.load(os.path.join(os.path.dirname(__file__),'models/LYS_ani2x_FINAL_MODEL_F25.joblib'))
34+
tyr_model=joblib.load(os.path.join(os.path.dirname(__file__),'models/TYR_ani2x_FINAL_MODEL_F25.joblib'))
35+
36+
#######################################################################
37+
#call ani
3838

39-
ani = torchani.models.ANI2x(periodic_table_index=True)
40-
print('Finished Loading.')
41-
42-
pkaressize=0
43-
pkadict={}
39+
ani = torchani.models.ANI2x(periodic_table_index=True)
40+
print('Finished Loading.')
4441

42+
pkaressize=0
43+
pkadict={}
44+
4545
for fpdb in pdbfiles:
4646
print('Calculating pKa for %s' % fpdb)
4747

pkaani/prep_pdb.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ def make_apo_pdb(pdbin,pdbout):
3333

3434
with open(pdbin, 'r+') as fd:
3535
contents = fd.readlines()
36-
3736
#first find if there is an SSBOND
3837
for line in contents:
3938
if('SSBOND' in line):
@@ -57,7 +56,9 @@ def make_apo_pdb(pdbin,pdbout):
5756

5857
for line in contents:
5958
row=line.strip().split()
60-
59+
if (len(row) == 0):
60+
continue
61+
6162
if row[0]=='MODEL':
6263
model_num = row[-1].strip()
6364
if(model_num=='1'): protein.append(line)

pkaani/run.py

+84-96
Original file line numberDiff line numberDiff line change
@@ -3,130 +3,118 @@
33
import getopt
44
import shutil
55
import numpy as np
6+
import argparse
67

78
from urllib.request import urlopen
89
from io import StringIO
9-
from pkaani.pkaani import calculate_pka
10+
1011
from pkaani.prep_pdb import prep_pdb
1112

13+
class MyParser(argparse.ArgumentParser):
14+
def error(self, message):
15+
sys.stderr.write('error: %s\n' % message)
16+
usage_pkaani()
17+
sys.exit(2)
18+
19+
1220
def usage_pkaani():
13-
"""
14-
Show how to use this program!
15-
"""
16-
print("""
21+
"""
22+
Show how to use this program!
23+
"""
24+
25+
print("""
1726
Example usages:
1827
1928
* If PDB file doesnt exist, it is downloaded and prepared for pKa calculations.
2029
21-
pkaani -i 1BNZ
22-
pkaani -i 1BNZ.pdb
30+
pkaani -i 1BNZ
31+
pkaani -i 1BNZ.pdb
2332
2433
* Multiple files can be given as inputs
2534
26-
pkaani -i 1BNZ,1E8L
35+
pkaani -i 1BNZ,1E8L
2736
2837
* If a specific directory is wanted:
2938
30-
pkaani -i path_to_file/1BNZ
31-
pkaani -i path_to_file/1BNZ,path_to_file/1E8L
39+
pkaani -i path_to_file/1BNZ
40+
pkaani -i path_to_file/1BNZ,path_to_file/1E8L
3241
3342
3443
Arguments: -i: Input files. Inputs can be given with or without
35-
file extension (.pdb). If PDB file is under a
36-
specific directory (or will be downloaded) the path
37-
can also be given as path_to_file/PDBFILE.
38-
Multiple PDB files can be given
39-
by using "," as separator (i.e. pkaani -i 1BNZ,1E8L).
44+
file extension (.pdb). If PDB file is under a
45+
specific directory (or will be downloaded) the path
46+
can also be given as path_to_file/PDBFILE.
47+
Multiple PDB files can be given
48+
by using "," as separator (i.e. pkaani -i 1BNZ,1E8L
49+
or pkaani -i myFile1.pdb,myFile2.pdb).
4050
""")
4151

42-
def handle_arguments_pkaani():
43-
inp_file = None
44-
prep_files = None
52+
def validate_args():
53+
parser = MyParser(description="Given a PDB, find the pKa of the titratable amino acids")
54+
parser.add_argument("-i", "--input_file_list", type=str, help="Comma-separated list of input files", required=True)
4555

46-
try:
47-
opts, args = getopt.getopt(sys.argv[1:], "hi:", ["help","inp="])
48-
except getopt.GetoptError:
49-
usage_pkaani()
50-
sys.exit(-1)
51-
52-
for opt, arg in opts:
53-
56+
args = parser.parse_args()
57+
return args
5458

55-
if opt in ('-h', "--help"):
56-
usage_pkaani()
57-
sys.exit(-1)
58-
59-
elif opt in ("-i", "--inp"):
60-
inp_file=[x.strip() for x in arg.split(',')]
61-
62-
else:
63-
assert False, usage_pkaani()
6459

65-
if inp_file is None:
66-
usage_pkaani()
67-
sys.exit(-1)
68-
# Input PDB file is mandatory!
69-
if len(inp_file)==0: # is None:
70-
print("@> ERROR: A PDB file is mandatory!")
71-
usage_pkaani()
72-
sys.exit(-1)
60+
def main():
61+
args = validate_args()
7362

74-
return inp_file
63+
if len(args.input_file_list) != 0:
64+
input_files = [x.strip() for x in args.input_file_list.split(',')]
7565

76-
def main():
66+
pdbfiles=np.array(input_files)
67+
68+
#first prepare PDB files for pkaani
69+
for inputpdb in pdbfiles:
70+
pdbid=inputpdb.rsplit('.', 1)[0]
71+
pdbfile=pdbid+".pdb"
72+
file_exist=True
73+
if not os.path.exists(pdbfile):
74+
file_exist=False
75+
base=os.path.basename(pdbfile)
76+
dpdbid=base.rsplit('.', 1)[0]
77+
78+
print("File %s is not accessible" % pdbfile)
79+
print("Downloading : http://www.rcsb.org/pdb/files/%s.pdb" % dpdbid)
80+
81+
url = 'http://www.rcsb.org/pdb/files/%s.pdb' % dpdbid
82+
83+
file = urlopen(url)
84+
contents = file.read().decode('utf-8')
85+
file.close()
86+
file = StringIO(contents)
87+
88+
outfile=pdbfile
89+
90+
with open(outfile, 'w') as f2:
91+
for line in contents:
92+
f2.write(line)
93+
94+
prep_pdb(pdbfile)
95+
file_exist=True
96+
97+
#CALCULATER PKA
98+
from pkaani.pkaani import calculate_pka
99+
pkadict=calculate_pka(pdbfiles,writefile=True)
77100

78-
input_files = handle_arguments_pkaani()
79-
pdbfiles=np.array(input_files)
80-
81-
#first prepare PDB files for pkaani
82-
for inputpdb in pdbfiles:
83-
pdbid=inputpdb.rsplit('.', 1)[0]
84-
pdbfile=pdbid+".pdb"
85-
file_exist=True
86-
if not os.path.exists(pdbfile):
87-
file_exist=False
88-
base=os.path.basename(pdbfile)
89-
dpdbid=base.rsplit('.', 1)[0]
90-
91-
print("File %s is not accessible" % pdbfile)
92-
print("Downloading : http://www.rcsb.org/pdb/files/%s.pdb" % dpdbid)
93-
94-
url = 'http://www.rcsb.org/pdb/files/%s.pdb' % dpdbid
95-
96-
file = urlopen(url)
97-
contents = file.read().decode('utf-8')
98-
file.close()
99-
file = StringIO(contents)
100-
101-
outfile=pdbfile
102-
103-
with open(outfile, 'w') as f2:
104-
for line in contents:
105-
f2.write(line)
106-
107-
prep_pdb(pdbfile)
108-
file_exist=True
109-
110-
#CALCULATER PKA
111-
pkadict=calculate_pka(pdbfiles,writefile=True)
112-
113-
#RENAME FILES PROPERLY
114-
for inputpdb in pdbfiles:
115-
pdbid=inputpdb.rsplit('.', 1)[0]
116-
pdbfile=pdbid+".pdb"
117-
118-
if os.path.exists(pdbfile):
119-
oldf=pdbfile
120-
newf=pdbid+"_pkaani.pdb"
121-
os.rename(oldf,newf)
122-
if file_exist:
123-
oldf=pdbid+"_0.pdb"
124-
newf=pdbfile
125-
os.rename(pdbid+"_0.pdb",pdbfile)
101+
#RENAME FILES PROPERLY
102+
for inputpdb in pdbfiles:
103+
pdbid=inputpdb.rsplit('.', 1)[0]
104+
pdbfile=pdbid+".pdb"
105+
106+
if os.path.exists(pdbfile):
107+
oldf=pdbfile
108+
newf=pdbid+"_pkaani.pdb"
109+
os.rename(oldf,newf)
110+
if file_exist:
111+
oldf=pdbid+"_0.pdb"
112+
newf=pdbfile
113+
os.rename(pdbid+"_0.pdb",pdbfile)
126114

127115

128116

129117
if __name__ == "__main__":
130-
main()
131-
132-
118+
main()
119+
120+

0 commit comments

Comments
 (0)