forked from dkoes/celpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
celpp2.py
290 lines (254 loc) · 10.4 KB
/
celpp2.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#!/usr/bin/env python
from FileTransfer import FtpFileTransfer
import os
import shutil
import prody
class _main_():
def fetchData():
global wd
wd = str(os.getcwd())
print('All Files will go into the celpp folder')
cred = (wd + '/credentials.txt')
try: #attempts to connect to file required to connect to ftp
print('Trying to open credentials.txt')
fo = open(cred, 'r')
fo.close()
except: #writes file required to connect to ftp if not already made
print('Writing credentials.txt file')
fo = open(cred, 'w')
fo.write('host ftp.box.com\nuser nlr23@pitt.edu\npass #hail2pitt1\npath\ncontestantid 33824\nchallengepath /challengedata\nsubmissionpath /33824')
fo.close()
if(os.path.isdir(wd + '/challengedata')==False):#creates challengedata folder if it doesn't exist
os.mkdir(wd + '/challengedata')
os.chdir(wd + '/challengedata')
else: #changes to challengedata folder if it exists
os.chdir(wd + '/challengedata')
ftp = FtpFileTransfer(cred)
print('Connecting to ftp.box.com')
ftp.connect()
print('Connected to ftp')
ftp_files = ftp.list_files('challengedata')#creates list of files from box
count = 0 #keep track number of files added to local folder
for x in (ftp_files):
split = os.path.splitext(x)
dir = os.path.splitext(split[0])
if(str(split[1]) == '.gz'):
if os.path.isfile(x) == True:#if it finds the zip file in local folder, unzips and deletes zippped file
print('Unzipping folder ' + str(dir[0]))
os.system('tar -xzf ' + x)
print('Deleting zip file: ' + x)
os.system('rm ' + x)
elif os.path.isdir(str(dir[0])) == True:#if it finds the unzipped directory in local folders
pass
else: #if it can't find the week in any format; downloads, unzips, and removes zipped file
ftp.download_file('challengedata/'+ x, wd + '/challengedata/' + x)
print('Unzipping folder ' + str(dir[0]))
os.system('tar -xzf ' + x)
print('Deleting zip file: ' + x)
os.system('rm ' + x)
count = count + 1
print(str(dir[0]) + ' was just added to the challengedata folder')
else:
ftp.download_file('challengedata/'+ x, wd + '/challengedata/' + x)
print('challengedata has been updated. ' + str(count) + ' week(s) was/were just added.')
print('Disconnecting from ftp')
ftp.disconnect()
def align():
global wd
ans = wd +'/challengedata/answers'
if os.path.isdir(ans)==False: #if the answers directory isnt formed make it
os.mkdir(wd+'/challengedata/answers')
rddir = wd+'/challengedata/rdkit-scripts'
if os.path.isdir(rddir)==False:
a='git clone https://github.com/dkoes/rdkit-scripts'
os.system(a)
data = os.listdir(wd+'/challengedata')
for x in (data):#for each weeks data
if x=="readme.txt" or x=="latest.txt" or x=="answers" or x=="rdkit-scripts" or x=='PDBfiles' or x=='visual.txt':
pass
else:
toDir = wd +'/challengedata/answers/' + x
if os.path.isdir(toDir)==False: #if the path to answers dir doesnt exist
os.mkdir(toDir) #make directory
dock=os.listdir(wd+'/challengedata/'+x)
for y in (dock):
a = str(os.getcwd()+'/answers/'+x+'/'+y+'/lmcss_docked.sdf')
if y=='readme.txt' or y=='new_release_structure_sequence_canonical.tsv' or y == 'new_release_structure_nonpolymer.tsv' or y=='new_release_crystallization_pH.tsv' or y=='new_release_structure_sequence.tsv':
pass
elif(os.path.isfile(a)==True):
pass
else:
input = os.listdir(wd+'/challengedata/'+x+'/'+y)
for z in (input):
if z.startswith("LMCSS") and z.endswith(".pdb"):
if(z.endswith("lig.pdb")):
pass
else:
id=z.strip('.pdb')
sts = str("grep ATOM "+ z+" > lmcss_rec.pdb")#creates receptor .pdb file
cd = wd+'/challengedata'
os.chdir(cd+'/'+x+'/'+y)#change directory to week/ligand
os.system(sts)#runs and creates receptor .pbd file
os.chdir(cd)#back to challenge directory
input = os.listdir(cd+'/'+x+'/'+y)#lists files inside ligand in certain week
for z in (input):
if z.endswith(".smi"):# changes .smi -> lig.sdf
cd = str(os.getcwd())
sts = str(" "+cd+'/'+x+'/'+y+'/'+z +" lig.sdf --maxconfs 1")
os.chdir(cd+'/'+x+'/'+y)
os.system(cd+'/rdkit-scripts/rdconf.py'+ sts)
os.chdir(cd)
for z in (input): # runs smina
if z.endswith("lig.pdb"):
sts=str("smina -r lmcss_rec.pdb -l lig.sdf --autobox_ligand "+z+" -o "+id+"_docked.sdf")
cd=str(os.getcwd())#lignad directory
os.chdir(cd+'/'+x+'/'+y)
#os.system(sts)
sts=str("smina -r lmcss_rec.pdb -l lig.sdf --autobox_ligand "+z+" -o lmcss_docked.sdf")
cd=str(os.getcwd())#lignad directory
os.chdir(cd+'/'+x+'/'+y)
os.system(sts)
os.chdir(cd)
cur = str(os.getcwd()+'/answers/'+x+'/'+y)
if (os.path.isdir(cur)==True):
os.chdir(cd+'/'+x+'/'+y)
os.getcwd()##
input = os.listdir(cd+'/'+x+'/'+y)
for i in (input):
if i.endswith(".txt") and i!="center.txt" and i!="visual.txt":
f=open(i)
lines=f.readlines()
ligand= lines[2].strip('ligand, ')
ligand=ligand.replace('\n','')
ligand=str(ligand)
#gets the ligand from txt file
if i.endswith("lig.pdb"):
#see if pdb exists
prody.fetchPDB(y)
proteinPDB=prody.parsePDB(y)
ourPDB=prody.parsePDB('lmcss_rec.pdb')
a,b, seqid, overlap= prody.matchChains(proteinPDB, ourPDB)[0]
b, protein_sp = prody.superpose(b, a, weights=None)
b.select(ligand+'_ligand.pdb')
sts=str("obrms -f "+i+' '+id+"_docked.sdf")
#run obrms
# parse results and output to the visualization txt file
os.system(sts)
f=open('visual.txt', 'ab+')
f.write(x+' smina '+y+'\n')
f.close
curdir = str(cd+'/'+x+'/'+y+'/'+id+'_docked.sdf')
print(input)##
for i in (input):
if i.endswith("lig.pdb"):
#see if pdb exists
protein = prody.fetchPDB(y)
#NEED NUMPY ARRAY
prody.writeArray('lmcss_docked_array.sdf', array)
prody.superpose('lmcss_docked.sdf', protein, weights=None)
sts=str("obrms -f "+i+" lmcss_docked.sdf")
#run obrms
# parse results and output to the visualization txt file
os.system(sts)
os.chdir(wd+'/challengedata/')
f=open('visual.txt', 'ab+')
f.write(x+' smina '+y+'\n')
f.close
curdir = str(cd+'/'+x+'/'+y+'/lmcss_docked.sdf')
todir = str(cd+'/answers/'+x+'/'+y+'/')
shutil.copy(curdir, todir)
print(curdir)
break
os.chdir(wd)
else:
os.mkdir(cur)
os.chdir(cd+'/'+x+'/'+y)
input = os.listdir(cd+'/'+x+'/'+y)
for i in (input):
if i.endswith(".txt") and i!="center.txt" and i!="visual.txt":
f=open(i)
lines=f.readlines()
ligand= lines[2].strip("ligand, ")
ligand=ligand.replace('\n','')
ligand=str(ligand)
#gets ligand from txt file
if i.endswith("lig.pdb"):
prody.fetchPDB(y)
proteinPDB=prody.parsePDB(y)
ourPDB=prody.parsePDB('lmcss_rec.pdb')
prody.matchChains(proteinPDB, ourPDB)
protein_sp = prody.superpose(ourPDB, proteinPDB, weights=None)
protein_sp.select(ligand+'_ligand.pdb')
sts=str("obrms -f "+i+' '+id+"_docked.sdf")
os.system(sts)
f=open('visual.txt', 'ab+')
f.write(x+' smina '+y+'\n')
f.close
curdir = str(cd+'/'+x+'/'+y+'/'+id+'_docked.sdf')
if i.endswith("lig.pdb"):
protein = prody.fetchPDB(y)
prody.writeArray('lmcss_docked_array.sdf', array)
prody.superpose('lmcss_docked.sdf', protein, weights=None)
sts=str("obrms -f "+i+" lmcss_docked.sdf")
os.system(sts)
os.chdir(wd+'/challengedata/')
f=open('visual.txt', 'ab+')
f.write(x+' smina '+y+'\n')
f.close
curdir = str(cd+'/'+x+'/'+y+'/lmcss_docked.sdf')
todir = str(cd+'/answers/'+x+'/'+y+'/')
shutil.copy(curdir, todir)
print(curdir)
break
os.chdir(wd)
def compare():
###get PDB files from databank that are associated with each protein for later use
##change directory
#create a folder that contains all pdb files from the PDB if it does not exist
prody.pathPDBFolder(wd + '/challengedata/PDBfiles')
#list of proteins that need to be downloaded
weeks = []
for(_, dirnames, _) in os.walk(wd + '/challengedata'):
if (dirnames=='latest.txt' or dirnames=='answers' or dirnames =='rdkit-scripts'):
pass
elif (dirnames not in weeks):
weeks.extend(dirnames)
proteins = [x for x in weeks if 'celpp' not in x]
#download pdb using prody
for x in proteins:
if x=='rdkit-scripts' or x=='PDBfiles' or x=='answers':
pass
else:
protein = prody.fetchPDB(x)
#prody.superpose()
def uploadData():#uploads zip files containing docking predictions to contestant folder specified in credentials.txt
print('Uploading files to box contestant folder')
cred = wd + '/credentials.txt'
ftp = FtpFileTransfer(cred)
print('Connecting to ftp.box.com')
ftp.connect()
print('Connected to ftp')
ftp_files = ftp.list_files(ftp.get_contestant_id())#creates list of files from box
d = []
for(_, dirnames, _) in os.walk(wd + '/protocols'):
d.extend(dirnames)
break
for dir in (d):#NOT A FAN OF THE NESTED FOR LOOPS, WILL TRY TO FIND BETTER WAY TO IMPLEMENT THIS
for(_,_,filenames) in os.walk(wd + '/protocols/' + dir):
f = []
f.extend(filenames)
print('Uploading files for ' + dir)
for x in (f):
if((x in ftp_files) == False):
file = wd + '/protocols/' + dir + '/' + x
remote_dir = ftp.get_remote_submission_dir()
remote_file_name = os.path.basename(file)
ftp.upload_file_direct(file, remote_dir, remote_file_name)
else:
pass
print('All the files have been uploaded. Disconnecting from ftp')
ftp.disconnect()
fetchData()
align()
#uploadData()
#uploadData()