|
3 | 3 | import getopt
|
4 | 4 | import shutil
|
5 | 5 | import numpy as np
|
| 6 | +import argparse |
6 | 7 |
|
7 | 8 | from urllib.request import urlopen
|
8 | 9 | from io import StringIO
|
9 |
| -from pkaani.pkaani import calculate_pka |
| 10 | + |
10 | 11 | from pkaani.prep_pdb import prep_pdb
|
11 | 12 |
|
| 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 | + |
12 | 20 | 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(""" |
17 | 26 | Example usages:
|
18 | 27 |
|
19 | 28 | * If PDB file doesnt exist, it is downloaded and prepared for pKa calculations.
|
20 | 29 |
|
21 |
| - pkaani -i 1BNZ |
22 |
| - pkaani -i 1BNZ.pdb |
| 30 | + pkaani -i 1BNZ |
| 31 | + pkaani -i 1BNZ.pdb |
23 | 32 |
|
24 | 33 | * Multiple files can be given as inputs
|
25 | 34 |
|
26 |
| - pkaani -i 1BNZ,1E8L |
| 35 | + pkaani -i 1BNZ,1E8L |
27 | 36 |
|
28 | 37 | * If a specific directory is wanted:
|
29 | 38 |
|
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 |
32 | 41 |
|
33 | 42 |
|
34 | 43 | 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). |
40 | 50 | """)
|
41 | 51 |
|
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) |
45 | 55 |
|
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 |
54 | 58 |
|
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() |
64 | 59 |
|
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() |
73 | 62 |
|
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(',')] |
75 | 65 |
|
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) |
77 | 100 |
|
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) |
126 | 114 |
|
127 | 115 |
|
128 | 116 |
|
129 | 117 | if __name__ == "__main__":
|
130 |
| - main() |
131 |
| - |
132 |
| - |
| 118 | + main() |
| 119 | + |
| 120 | + |
0 commit comments