-
Notifications
You must be signed in to change notification settings - Fork 0
/
pdbqt2pdb_converter.py
142 lines (118 loc) · 5.04 KB
/
pdbqt2pdb_converter.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
###########################################
##### PDBQT -> PDB file converter #####
##### Neoventures Biotechnology Inc. #####
##### Jesse Bourret-Gheysen #####
##### May 6th 2019 #####
###########################################
import re
import os
import time
from os.path import isfile, join
print('''########################################################################
########################################################################
#### _ _ _ ####
#### | | | | | ####
#### _ __ __| | |__ __ _| |_ NeoVentures ####
#### | '_ \ / _` | '_ \ / _` | __| Biotechnology Inc. ####
#### | |_) | (_| | |_) | (_| | |_ 2019 ####
#### | .__/ \__,_|_.__/ \__, |\__| ####
#### | | | | Jesse Bourret-Gheysen ####
#### |_|_ _ _ |_| ####
#### |__ \ | | | ####
#### ) | _ __ __| | |__ ####
#### / / | '_ \ / _` | '_ \ ####
#### / /_ | |_) | (_| | |_) | ####
#### |____| | .__/ \__,_|_.__/ _ ####
#### | | | | ####
#### ___ _|_| _ ____ _____ _ __| |_ ___ _ __ ####
#### / __/ _ \| '_ \ \ / / _ | '__| __/ _ | '__| ####
#### | (_| (_) | | | \ V | __| | | || __| | ####
#### \___\___/|_| |_|\_/ \___|_| \__\___|_| ####
#### ####
########################################################################
########################################################################''')
time.sleep(2)
print('''This program converts pdbqt files into pdb files by splitting
the individual models into seperate files, while also adding in the
receptor model to these files so the receptor and ligand can be seen
together in their respective docking conformations.
To use this program, make sure that the executable is in the same directory as
both the receptor and output pdbqt files that you wish to use. ''')
time.sleep(1)
print(".")
time.sleep(1)
print("..")
time.sleep(1)
print("...")
#pdb file writing function
def pdb_writer(pdb, pdb_num):
'''function which pulls a pdb from the pdb dictionary
and writes it to an independantly labeled file'''
pdb_name = pdbqt_file_name[:-6] + ".0" + str(pdb_num) + ".pdb"
pdb_file = open(pdb_name, "a+")
for line in pdb:
pdb_file.write(line)
pdb_file.write(" \n")
pdb_file.close()
return pdb_name
#Current working directory
cwd = os.getcwd()
#list pdbqt files in cwd
list_of_pdbqts = []
for f in os.listdir(cwd):
if isfile(join(cwd, f)) and bool(re.search('pdbqt$|PDBQT$', f)):
list_of_pdbqts.append(f)
print(list_of_pdbqts)
print(" ")
#enter the name of the desired file (pdbqt to split up)
pdbqt_file_name = str(input("please enter the pdbqt file name(no extension)"))
print(" ")
#list pdb files in cwd, so that the receptor can be selected
list_of_pdbs = []
for f in os.listdir(cwd):
if isfile(join(cwd, f)) and bool(re.search('pdb$|PDB$', f)):
list_of_pdbs.append(f)
print(list_of_pdbs)
print(" ")
#enter the name of the desired file (pdb receptor)
print("""Please enter the name of the pdb file listed above corresponding
to the receptor for the current pdbqt file""")
receptor_file_name = str(input("pdb receptor name (no extension): "))
print(" ")
#appends the file extension if its missing for the pdbqt
if pdbqt_file_name[-6:] != ".pdbqt" or pdbqt_file_name[-6:] != ".PDBQT":
pdbqt_file_name += ".PDBQT"
#appends the file extension if its missing for the pdb
if receptor_file_name[-4:] != ".pdbqt" or receptor_file_name[-4:] != ".PDBQT":
receptor_file_name += ".PDB"
#open file into this var
pdbqt_file = open(pdbqt_file_name, "r")
#a list, a dict, and a var
pdb_list = []
count = 0
pdb_dictionary = {}
#pump and and dump pdb's
for line in pdbqt_file:
if not re.match("ENDMDL", line):
pdb_list.append(line)
else:
count += 1
pdb_dictionary[str(count)] = pdb_list
pdb_list = []
pdbqt_file.close()
#open the receptor file
#receptor_file = open(receptor_file_name, "r")
list_of_pdb_names = []
for k, v in pdb_dictionary.items():
list_of_pdb_names.append(pdb_writer(v, k))
#concatenate the output pdb's with the receptor pdb file
#the receptor file is read and then written into the pdb out file
for pdb_file in list_of_pdb_names:
pdb_file_new = "recp_" + pdb_file
with open(pdb_file_new, 'w') as outfile:
with open(pdb_file) as infile:
outfile.write(infile.read())
with open(receptor_file_name) as infile:
outfile.write(infile.read())
os.remove(pdb_file)
print('Program complete, thank you.')