-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcrawl_ADV.py
1248 lines (996 loc) · 34.5 KB
/
crawl_ADV.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
''' crawl_ADV: utilities to crawl ADVina FAAH files
was part of get_ADInfo
@version 1.0
@date on 30 Aug 14
@author: [email protected]
'''
import sys
# import getopt
import re
import glob
import os
import csv
import tempfile
import tarfile
import shutil
import json
import argparse
import datetime
from string import strip
import numpy as np
import pybel
ob = pybel.ob
from CADD.Raccoon2.HelperFunctionsN3P import pathToList
import config
import FAAHA
def getLines(filename, doStrip = False, removeEmpty=False):
""" """
f = open(filename, 'r')
lines = f.readlines()
f.close()
if doStrip:
lines = map(strip,lines)
if removeEmpty:
#lines = removeEmptyLines(lines)
lines = [ l for l in lines if l.strip() ]
return lines
# defaults
mode = "1" # ONLY pose for Vina
ebest = -999. # energy
eworst = -3.
cworst = 1. # cluster poses
cbest = 999.
pworst = 1. # cluster %
pbest = 100.
lworst = 0. # ligand efficiency
lbest = -99
DEBUG = False
pattern = "_VS.pdbqt" # default pattern for searching result files
#pattern = ".VS.pdbqt" # default pattern for searching result files
do_filter = False
recursive = False
## Shared with AutoDock
# AADict = {'ASP':'D', 'GLU':'E', 'LYS':'K', 'HIS':'H', 'ARG':'R',
# 'GLN':'Q', 'ASN':'N', 'SER':'S', 'ASX':'B', 'GLX':'Z',
# 'PHE':'F', 'TRP':'W', 'TYR':'Y',
# 'GLY':'G', 'ALA':'A', 'ILE':'I', 'LEU':'L', 'CYS':'C',
# 'MET':'M', 'THR':'T', 'VAL':'V', 'PRO':'P' }
# updated Dec'14 with additional codes
AADict = {'ASP':'D', 'GLU':'E', 'LYS':'K', 'HIS':'H', 'ARG':'R',
'GLN':'Q', 'ASN':'N', 'SER':'S', 'ASX':'B', 'GLX':'Z',
'PHE':'F', 'TRP':'W', 'TYR':'Y',
'GLY':'G', 'ALA':'A', 'ILE':'I', 'LEU':'L', 'CYS':'C',
'MET':'M', 'THR':'T', 'VAL':'V', 'PRO':'P',
'HID':'H', 'HIE':'H', 'HIP':'H',
'ASH':'D', 'GLH':'E',
'LYN':'K', 'ARN':'R',
'HOH':'U', 'CL': 'J' }
ADbatchRE = r'FAHV_(x?)(.+)_([0-9]+)_processed.tgz'
ADbatchREPat = re.compile(ADbatchRE)
InterTypes = ('hba', 'hbd', 'mtl','ppi','tpi','vdw')
InterRE = r'(.*):.+():([A-Za-z]+[0-9]*)~~(.*):(.+):(.+)'
InterREPat = re.compile(InterRE)
# vdw are different
# :CYS65:SG
# InterVDWRE = r'(.*):([A-Z]+[0-9]+):([A-Z]+)'
InterVDWRE = r'(.*):(.+):(.+)'
InterVDWREPat = re.compile(InterVDWRE)
# ppi/tpi are different
# B:HIS114~~(-4.295,-13.390,-20.427:-3.408,-10.290,-18.368)
# cf. piStackingAndRingDetection.findLigRecPiStack()
# pstack.append([res, rec_centroid, lig_centroid])
InterPiRE = r'(.*):(.+)~~\(([-.,0-9]+):([-.,0-9]+)\)'
InterPiREPat = re.compile(InterPiRE)
def reducePlusInterDict(ligData):
'''create sparse version of interaction dict, with only ligands atom type (not its index)
the syntax for the InterPattern seems a PDB convention?
'''
rinterDict = {}
for itype in InterTypes:
if len(ligData[itype]) > 0:
rinterDict[itype] = []
if itype=='ppi' or itype=='tpi':
for inter in ligData[itype]:
# d:<0>:O3~~B:ARG57:N
m = InterPiREPat.match(inter)
try:
(rchain,raa,rcenter,ligcenter) = m.groups()
rinterDict[itype].append( (rchain,raa,rcenter,ligcenter) )
except:
print 'reducePlusInterDict: bad ppi/tpi string?!',inter
else:
# 151110: new processing now includes vdw ligand atoms
for inter in ligData[itype]:
# d:<0>:O3~~B:ARG57:N
m = InterREPat.match(inter)
try:
# (ligIdx,liname,rchain,raa,ratom) = m.groups()
(lchain,lres,latom,rchain,raa,ratom) = m.groups()
rinterDict[itype].append( (rchain,raa,ratom,latom) )
except:
print 'reducePlusInterDict: bad %s string?! %s' % (itype,inter)
return rinterDict
#############################################################################
# from rabbit
# Author: Stefano FORLI
#
# Copyright: Stefano Forli, TSRI 2011
#
# v.0.4
#############################################################################
def checkVSresult_ADV(lines):
"parses and validates a PDBQT+ file (lines)"
if (lines[0].startswith("USER ADVS_Vina_result>")):
return True
else:
return False
def setKwMode_ADV(mode = "1"):
# USER ADVina_pose1> -10.200, -0.309
# mode = "1", "2", "..."
if mode == "any":
kw = "ADVina_pose."
else:
kw = "ADVina_pose"+mode
return kw
def getResultCount_ADV(lines):
return int(lines[4].split("ADVina_results>")[1].strip())
def getRawEnergyData_ADV(lines, mode = "1"):
# mode = "1", "2", "...", "any" (POSES)
# format: { "e" : [ float(e)] , "leff" : [float(leff)] }
kw = setKwMode_ADV(mode)+">"
result = { "e" : [],
"leff" : [] }
for l in lines:
if re.search(kw, l):
l = l.split(">", 1)[1]
e, leff = l.split(",")
result['e'].append(float(e))
result["leff"].append(float(leff))
if not mode == "any":
break
return result
def getLigInteractions_ADV(lines, mode = "1"):
kw = setKwMode_ADV(mode) #
kw += "_" # TODO test this!
interactions = {"vdw" : [], # these keys must match the
"hba" : [], # tags used in writing the PDBQT+
"hbd" : [],
"ppi" : [],
"tpi" : [],
"mtl" : [],
}
for l in lines:
if re.search(kw, l):
for itype in interactions.keys():
if (itype == "ppi") or (itype == "tpi"):
sep = ";"
else:
sep = ","
if itype in l:
l = l.split(itype+">", 1)[1]
l = l.split(sep)
for i in l:
interactions[itype].append(i.strip())
return interactions
def getLigSource_ADV(lines):
srcPrefix = 'USER ADVina_pose'
src = ''
## 160531: grabs FIRST pose number in PDBQT?!
for l in lines:
if l.startswith(srcPrefix):
gtpos = l.find('>')
src = l[gtpos+1:].strip()
return src
return src
def getGenericData_ADV(lines):
receptNamePrefix = 'USER ADVina_rec> '
nresultPrefix = 'USER ADVina_results> '
rname = ''
nresult = 0
for l in lines:
if l.startswith(receptNamePrefix):
rname = l[len(receptNamePrefix):].strip()
if l.find(nresultPrefix) != -1:
nresult = int(l[len(nresultPrefix):].strip())
return (rname,nresult)
#############################################################################
# eo rabbit
def parseADPDBQT_ADV(f):
'''uses raccoon's routines to extract
{'recept': '.../structure/x3NF6_B_IN_LEDGF.pdbqt',
'e': -6.9,
'leff': -0.216,
'nresult': 1,
'src': 's> 1',
'hba': [('A', 'GLU170', 'N', 'O33'), ...],
'vdw': [('A', 'GLU170', 'HN', 'C25'), ... ]
}
'''
ligand = getLines(f)
if not checkVSresult_ADV(ligand):
return None
ligData = {}
rname,nresult = getGenericData_ADV(ligand)
if rname=='':
print 'parseADPDBQT_ADV: missing receptor name?!',f
ligData['recept'] = ''
else:
ligData['recept'] = rname
if nresult==0:
print 'parseADPDBQT_ADV: missing results?!',f
ligData['nresult'] = 0
return None
else:
ligData['nresult'] = nresult
## ADVina has one
src = getLigSource_ADV(ligand)
if src=='':
print 'parseADPDBQT_ADV: missing src?!',f
ligData['nresult'] = 0
else:
ligData['src'] = src
ligdataRaw = getRawEnergyData_ADV(ligand)
# dict: {'c_pc': [53.45], 'e': [-6.19], 'leff': [-0.413], 'c_size': [93]}
for k,v in ligdataRaw.items():
ligData[k] = v[0]
liginteract = getLigInteractions_ADV(ligand)
reducedInterDict = reducePlusInterDict(liginteract)
ligData.update(reducedInterDict)
# do additional pdbqt access via OB
# to convert tpi, ppi ligandCenter to closest atom
if 'ppi' in ligData or 'tpi' in ligData:
allMol = pybel.readfile('pdbqt', f)
# print 'len(allMol)',len(allMol)
ligMol = allMol.next() # ASSUME only one mol PDBQT
obmol = ligMol.OBMol
atomCoord = {}
for res in ob.OBResidueIter(obmol):
for obatom in ob.OBResidueAtomIter(res):
pbatom = pybel.Atom(obatom)
idx = pbatom.idx
laName = res.GetAtomID(obatom).strip()
laIdx = idx
laCoord = pbatom.coords
laType = pbatom.type
atomCoord[idx] = [laName,laType,laCoord]
for itype in ['tpi' ,'ppi']:
if itype not in ligData:
continue
newInterList = []
for inter in ligData[itype]:
#NB: ratom is really receptorCenter for tpi/ppi
(rchain,raa,rcenter,lcenter) = inter
minLA = None
minDist = 1.0e6
laTuple = tuple(eval(lcenter)) # NB: latomFull is a string
obLAnp = np.array(laTuple)
for laIdx in atomCoord:
laName,laType,laCoord = atomCoord[laIdx]
latype = FAAHA.getAtomType(laName)
# 151109: only consider ligatoms *OTHER THAN* H, C
if latype in config.VDWExcludedLigAtoms:
continue
rlifLAnp = np.array(laCoord)
l2norm = np.linalg.norm(obLAnp - rlifLAnp)
# print laIdx,atName,l2norm
if l2norm < minDist:
minLA = laIdx
minDist = l2norm
ratom = ''
latomFull = atomCoord[minLA][0]
newInterList.append( (rchain,raa,ratom,latomFull) )
ligData[itype] = newInterList
return ligData
# simplified, here, means that this should work for all batches after ??? Exp.# ???
# because naming was simplified (by dns)
# ADVsimple = r'fahv.x([a-zA-Z0-9]+)(_[A-Z0-9]*)*_(ZINC[0-9]+)(_[0-9]*)*_([0-9]+)_out_Vina_VS.pdbqt'
# fahv.x3KF0_prASw0c0_ZINC00147966_544588015_out_Vina_VS.pdbqt
# 160110: ligand capturing part of qualified receptor?
# fahv.x3KF0_prASw0c0_ZINC00147966_544588015_out_Vina_VS.pdbqt
# ADVsimple = r'fahv.x([a-zA-Z0-9]+)_(.+)_([0-9]+)_out_Vina_VS.pdbqt'
ADVsimple1 = r'fahv.x([a-zA-Z0-9_]+)_(.+)_([0-9]+)_out_Vina_VS.pdbqt'
ADVsimplePat1 = re.compile(ADVsimple1, re.IGNORECASE)
# 160216: consuming SForli's rerun of DUDE
# ZINC63694490_out_Vina_VS.pdbqt
# activated by config.procVersion = 0.2
ADVsimple2 = r'([A-Z0-9_]+)_out_Vina_VS.pdbqt'
ADVsimplePat2 = re.compile(ADVsimple2, re.IGNORECASE)
def visit_ADV_tgz(tgzPath,exptname,recon,verbose):
dataTbl = {}
# 2do: Py2.7 allows WITH context management! TODO
# with tarfile.open(tgzPath) as subTar:
# with tarfile.open(subTar) as dataDir:
tmpDir = tempfile.mkdtemp()
allTar = tarfile.open(tgzPath)
allTar.extractall(tmpDir)
# ASSUME: _VS "bar" style processed file names for ADV
# fahv.x4I7G_RT_NNRTIadj_wNNRTI_ZINC58421065_1_649284996_out_Vina_VS.pdbqt
# Exp79/Results_x3kf0A/FAHV_x3kf0A_0124403_processed.tgz example:
# FAHV_x3kf0A_0124403_processed/ # (untarred dir)
# fahv.x3kf0A_ZINC01569654_1113915765_out_Vina_VS.pdbqt
procList = glob.glob(tmpDir+'/FAHV*/fahv.*_out_Vina_VS.pdbqt')
if verbose:
print 'visit_ADV_tgz: NTGZ=',len(procList)
ndup =0
ninvFile=0
for isd,procPath in enumerate(procList):
procBits = os.path.split(procPath)
#
dirBits = procBits[0].split('/')
currReceptor = dirBits[-2]
# batchNo = int(dirBits[-1].split('_')[1])
procf = procBits[1]
# lbpos = procf.find('_')
# rbpos = procf.rfind('_')
# assert (lbpos != -1 and rbpos != -1 and rbpos > lbpos), 'visit_ADV_tgz: bad procf?! %s' % (procf)
# ligand = procf[lbpos+1:rbpos]
if config.procVersion == '0.2':
mpath = ADVsimplePat2.match(procf)
ligand = mpath.groups()[0]
receptor = currReceptor
else:
mpath = ADVsimplePat1.match(procf)
(receptor,ligand,workNo) = mpath.groups()
dk = (exptname,receptor,ligand)
if dk in dataTbl:
print 'visit_ADV_tgz: dup dataKey?!',dk
ndup += 1
continue
###-------
ligData = parseADPDBQT_ADV(procPath)
###-------
if not(ligData):
print 'visit_ADV_tgz: invalid ADV file?!',procf, tgzPath
ninvFile += 1
continue
dataTbl[dk] = ligData
if verbose and isd % 100 == 0:
print 'visit_ADV_tgz: nproc=', isd
shutil.rmtree(tmpDir)
print 'visit_ADV_tgz: done. NLig=%d NDup=%d NInvalidFile=%d' % \
(len(dataTbl),ndup,ninvFile)
return dataTbl
def visit_ADV(procPath,exptname,recon,verbose):
'''assume path already has processed files (aot/ tgzPath), as produced by process
'''
dataTbl = {}
# 2do: Py2.7 allows WITH context management! TODO
# with tarfile.open(tgzPath) as subTar:
# with tarfile.open(subTar) as dataDir:
# ASSUME: _VS "bar" style processed file names for ADV
# fahv.x4I7G_RT_NNRTIadj_wNNRTI_ZINC58421065_1_649284996_out_Vina_VS.pdbqt
# Exp79/Results_x3kf0A/FAHV_x3kf0A_0124403_processed.tgz example:
# FAHV_x3kf0A_0124403_processed/ # (untarred dir)
# fahv.x3kf0A_ZINC01569654_1113915765_out_Vina_VS.pdbqt
procList = glob.glob(procPath+'/*_out_Vina_VS.pdbqt')
if verbose:
print 'visit_ADV: NProcFiles=',len(procList)
ndup =0
ninvFile=0
for isd,procPath in enumerate(procList):
# fahv.x3kf0A_ZINC00145439_2057149382_out_Vina_VS.pdbqt
procBits = os.path.split(procPath)
#
dirBits = procBits[0].split('/')
currReceptor = dirBits[-2]
# batchNo = int(dirBits[-1].split('_')[1])
procf = procBits[1]
# 160110: ligand capturing part of qualified receptor?
# fahv.x3KF0_prASw0c0_ZINC00147966_544588015_out_Vina_VS.pdbqt
# fahv.x3kf0A_ZINC00145439_2057149382_out_Vina_VS.pdbqt
# ADVsimple = r'fahv.x([a-zA-Z0-9]+)_(.+)_([0-9]+)_out_Vina_VS.pdbqt'
# lbpos = procf.find('_')
# rbpos = procf.rfind('_')
# assert (lbpos != -1 and rbpos != -1 and rbpos > lbpos), 'visit_ADV_tgz: bad procf?! %s' % (procf)
# ligand = procf[lbpos+1:rbpos]
if config.procVersion == '0.2':
mpath = ADVsimplePat2.match(procf)
ligand = mpath.groups()[0]
receptor = currReceptor
else:
mpath = ADVsimplePat1.match(procf)
(receptor,ligand,workNo) = mpath.groups()
dk = (exptname,receptor,ligand)
if dk in dataTbl:
print 'visit_ADV: dup dataKey isd=%d %s?!' % (isd,dk)
ndup += 1
continue
###-------
ligData = parseADPDBQT_ADV(procPath)
###-------
if not(ligData):
print 'visit_ADV: invalid ADV file?!',procf, procPath
ninvFile += 1
continue
dataTbl[dk] = ligData
# if verbose and isd % 1000 == 0:
# print 'visit_ADV: nproc=', isd
print 'visit_ADV: procPath=%s done. NLig=%d NDup=%d NInvalidFile=%d' % \
(procPath,len(dataTbl),ndup,ninvFile)
return dataTbl
def rptData_ADV(dataTbl,summf,interf,exptname,batchNo):
'''V1: produce condensed JSON inter file
[ [Expt,BatchNo,Recept,Lig, [IType,[InterEnum] ] ] ]
ala ["Exp96", 197339, "x3ZSW_B_IN_Y3", "Y3_ZINC00626007", [[0, [["B", "R199", "NH2", "O1"], ["B", "K188", "NZ", "O2"]]], [5, [["B", "G82", "CA"], ..., ["B", "I141", "O"]]]]]
'''
summs = open(summf,'w')
summs.write('Expt,Batch,Recept,Ligand,E,Eff,Nvdw,Ninter\n')
allInter = []
for dk in dataTbl:
(exptname,receptor,lig) = dk
ligData = dataTbl[dk]
ninter = 0
nvdw = 0
for itype in InterTypes:
if itype in ligData:
if itype=='vdw':
nvdw = len(ligData['vdw'])
else:
ninter += len(ligData[itype])
summs.write('%s,%d,%s,%s,%s,%s,%d,%d\n' % \
(exptname,batchNo,ligData['recept'],lig,\
ligData['e'],ligData['leff'],nvdw,ninter))
interInfo = [exptname, batchNo, ligData['recept'], lig]
interList = []
for itype in InterTypes:
if itype in ligData:
# additional compression by combining all inter of same type
itlist = []
# convert itype string to its index in InterTypes
itypeIdx = InterTypes.index(itype)
for interTuple in ligData[itype]:
inter = list(interTuple)
# convert 3-letter receptor AA to single char
raa = inter[1]
raalet3 = raa[:3]
pos = raa[3:]
if raalet3 in AADict:
raaLet = AADict[raalet3]
else:
raaLet = 'X'
inter[1] = raaLet+pos
itlist.append(inter)
interList.append( [itypeIdx,itlist] )
interInfo.append(interList)
allInter.append(interInfo)
summs.close()
inters = open(interf,'w')
json.dump(allInter,inters)
inters.close()
def visitRpt_ADV_tgz(tgzPath,outPath,recon,batchTbl,tocs,exptname,batchNo,verbose):
''' get info, place in this table
info is extracted from enhanced pdbqt files inside tarball
'''
dataTbl = visit_ADV_tgz(tgzPath,exptname,recon,verbose)
if recon:
print 'visitRpt_ADV_tgz: Recon-only; no reporting'
return len(dataTbl)
summf = outPath +'/summ/ADV_summ_%07d.csv' % (batchNo)
interf = outPath +'/inter/ADV_inter_%07d.json' % (batchNo)
rptData_ADV(dataTbl,summf,interf,exptname,batchNo)
#tocStr = '%s,%05d,%d,%s' % (exptname,ntgz,len(dataTbl),tgzPath)
tocStr = '%s,%d,%d,%s' % (exptname,batchNo,len(dataTbl),tgzPath)
if verbose:
print 'visitRpt_ADV_tgz toc:',tocStr
tocs.write(tocStr+'\n')
# fun2watch! toc
tocs.flush(); os.fsync(tocs.fileno())
return len(dataTbl)
def visitRpt_ADV(tgzPath,outPath,recon,batchTbl,tocs,exptname,batchNo,verbose):
''' get info, place in this table
info is extracted from enhanced pdbqt files inside tarball
assume path already has processed files (aot/ tgzPath), as produced by process
'''
dataTbl = visit_ADV(tgzPath,exptname,recon,verbose)
if recon:
print 'visitRpt_ADV: Recon-only; no reporting'
return len(dataTbl)
summf = outPath +'summ/ADV_summ_%07d.csv' % (batchNo)
interf = outPath +'inter/ADV_inter_%07d.json' % (batchNo)
rptData_ADV(dataTbl,summf,interf,exptname,batchNo)
#tocStr = '%s,%05d,%d,%s' % (exptname,ntgz,len(dataTbl),tgzPath)
tocStr = '%s,%d,%d,%s' % (exptname,batchNo,len(dataTbl),tgzPath)
if verbose:
print 'visitRpt_ADV toc:',tocStr
tocs.write(tocStr+'\n')
# fun2watch! toc
tocs.flush(); os.fsync(tocs.fileno())
return len(dataTbl)
def mglTop_visit_ADV(tgzProc=True,exptList=None,recon=False,verbose=False):
'recon stops after opening, parsing one file in first tgz'
if exptList:
print 'mglTop_visit_ADV: Explicit experiment list %s' % (str(exptList))
else:
print 'mglTop_visit_ADV: Full crawl of %s' % (ProcDir)
dirbits = ProcDir.split('/')
# ASSUME ADV_topDir = .../ exptName / receptor /
# ala .../DUDE-HIVPR/x1XL2/
pexpt = dirbits[-3]
# exptList = [os.path.split(exptPath)[1] for exptPath in glob.glob(crawlPat) ]
# 160218: Empty string causes exptPath = ADV_topDIr, outPath = outDir
exptList = [pexpt]
print 'mglTop_visit_ADV: NExperiments=',len(exptList)
if verbose:
print 'mglTop_visit_ADV: **Verbose output'
if recon:
print 'mglTop_visit_ADV: **Reconnaissance sniffing only!'
totParse = 0
for ie, exptname in enumerate(exptList):
startTime = datetime.datetime.now()
print 'mglTop_visit_ADV: %s starting %s' % (exptname,startTime.strftime("%y%m%d %H:%M:%S"))
# only create exptname subdir if exptname does NOT come from project experiment, above
if ProcDir.find(exptname) == -1:
exptPath = ProcDir+exptname+'/'
outPath = CrawlDir+exptname+'/'
else:
exptPath = ProcDir
outPath = CrawlDir
if verbose:
print ' *',ie,exptPath
exptReceptList = glob.glob(exptPath+'*')
exptReceptList.sort()
for receptPath in exptReceptList:
recept = receptPath.split('/')[-1]
exptReceptPath = exptPath + recept + '/'
outReceptPath = outPath + recept + '/'
if verbose:
print ' **',ie,recept,exptReceptPath
if not os.path.isdir(outReceptPath):
print 'mglTop_visit_ADV: creating ExptOutput directory',outReceptPath
os.makedirs(outReceptPath)
tocf = outReceptPath+'ADV_toc.csv'
tocs = open(tocf,'w')
#tocs.write('NTGZ,Data,Path\n')
tocs.write('Experiment, Batch, Data, Path\n')
summDir = outReceptPath+'summ/'
if not os.path.isdir(summDir):
print 'mglTop_visit_ADV: creating summDir ',summDir
os.makedirs(summDir)
interDir = outReceptPath+'inter/'
if not os.path.isdir(interDir):
print 'mglTop_visit_ADV: creating interDir ',interDir
os.makedirs(interDir)
batchTbl = {}
if tgzProc:
exptSubList = glob.glob(exptReceptPath+'batch_*.tgz')
else:
exptSubList = glob.glob(exptReceptPath+'batch_*')
print 'mglTop_visit_ADV: Recept=%s NBatches=%d' % (recept,len(exptSubList))
#NB: glob() returns matches in arbitrary order
exptSubList.sort()
for ise, exptSubPath in enumerate(exptSubList):
# exptSubPath = /Data/sharedData/coevol-HIV/WCG/process2/Exp120/Results_x3KF0_prASw0c0/batch_0550340
if verbose:
print ' ***',ie,ise,recept,exptSubPath
if tgzProc:
tgzList = glob.glob(exptSubPath+'/*.tgz')
print 'mglTop_visit_ADV: NTGZ=',len(tgzList)
tgzList.sort()
for jt,tgzPath in enumerate(tgzList):
if recon and jt > 0:
print 'mglTop_visit_ADV: Recon-only; break'
break
tgznow = os.path.split(tgzPath)[1]
mpath = ADbatchREPat.match(tgznow)
if mpath:
(x, vinarec, vinabatch) = mpath.groups()
else:
print 'mglTop_visit_ADV: bad match?!',tgznow
continue
batchNo = int(vinabatch)
if verbose:
print 'mglTop_visit_ADV: Attempting to analyze',tgznow
nparse = visitRpt_ADV_tgz(tgzPath,outReceptPath,recon,batchTbl,tocs,exptname,batchNo,verbose)
else:
# exptSubPath = /Data/sharedData/coevol-HIV/WCG/process2/Exp120/batch_0550340
bpos = exptSubPath.rfind('_')
bnos = exptSubPath[bpos+1:]
batchNo = int(bnos)
nparse = visitRpt_ADV(exptSubPath,outReceptPath,recon,batchTbl,tocs,exptname,batchNo,verbose)
#
totParse += nparse
endTime = datetime.datetime.now()
elapTime = endTime-startTime
print 'mglTop_visit_ADV: Expt=%s Recept=%s done. TotParse=%d NSec=%s' % (exptname,recept,totParse,elapTime.total_seconds())
tocs.close() # for each experiment directory
print 'mglTop_visit_ADV: TotParse=',totParse
def Local_Top_visit_ADV2(ADV_topDir,outdir,exptName,receptor,procPattern = '*_out_Vina_VS.pdbqt',tgzProc=False,verbose=False):
'crawl local (non-FAAH) processed files'
tocf = outdir+'ADV_toc.csv'
tocs = open(tocf,'w')
#tocs.write('NTGZ,Data,Path\n')
tocs.write('Experiment, Batch, Data, Path\n')
summDir = outdir+'summ/'
if not os.path.isdir(summDir):
print 'mglTop_visit_ADV: creating summDir ',summDir
os.makedirs(summDir)
interDir = outdir+'inter/'
if not os.path.isdir(interDir):
print 'mglTop_visit_ADV: creating interDir ',interDir
os.makedirs(interDir)
batchTbl = {}
if tgzProc:
batchList = glob.glob(ADV_topDir+'batch_*.tgz')
else:
batchList = glob.glob(ADV_topDir+'batch_*')
print 'Local_Top_visit_ADV2: NBatches=',len(batchList)
#NB: glob() returns matches in arbitrary order
batchList.sort()
totParse = 0
recon = False
for ib, batchPath in enumerate(batchList):
bpos = batchPath.rfind('_')
bnos = batchPath[bpos+1:]
batchNo = int(bnos)
nparse = visitRpt_ADV(batchPath,recon,batchTbl,outdir,tocs,exptName,batchNo,verbose)
#
totParse += nparse
# procList = pathToList(ADV_topDir,recursive=True, pattern=procPattern)
#
# totParse = 0
# dataTbl = {}
# print 'Local_Top_visit_ADV2: NProc=',len(procList)
# for isd,procPath in enumerate(procList):
#
# procf = os.path.split(procPath)[1]
# fbits = procf.split('_')
# ligand = fbits[0]
#
# ###-------
# ligData = parseADPDBQT_ADV(procPath)
# ###-------
#
# if not(ligData):
# print 'Local_Top_visit_ADV2: invalid ADV file?!',procPath
# continue
#
# dk = (exptName,receptor,ligand)
#
# if dk in dataTbl:
# print 'Local_Top_visit_ADV2: dup dataKey?!',dk
# continue
#
# dataTbl[dk] = ligData
#
# totParse += 1
#
# summf = outdir+'/ADV_summ.csv'
# interf = outdir+'/ADV_inter.json'
# rptLocalData_ADV(dataTbl,summf,interf)
print 'Local_Top_visit_ADV2: TotParse=',totParse
# 141028 rikPR253
# .../processed/rikPR253/x1A8K_PRAS/PDB0Q4_out_Vina_VS.pdbqt
Local_PR253_RE = r'(.+)_out_Vina_VS.pdbqt'
Local_PR253_Pat = re.compile(Local_PR253_RE)
def Local_PR253_Top_visit_ADV(ADV_topDir,outdir,verbose=False):
'crawl local (non-FAAH) processed files'
# .../processed/rikPR253/x1A8K_PRAS/PDB0Q4_out_Vina_VS.pdbqt
totParse = 0
dataTbl = {}
pdbDirList = glob.glob(ADV_topDir+'/x*_PRAS')
print 'Local_PR253_Top_visit_ADV: NSubExperiments=',len(pdbDirList)
for ise, exptSubPath in enumerate(pdbDirList):
pathBits = exptSubPath.split('/')
receptor = exptSubPath.split('/')[-1]
assert receptor.endswith('_PRAS')
assert receptor[0]=='x'
receptor = receptor[1:]
receptor = receptor[:-5]
procList = glob.glob(exptSubPath+'/*_out_Vina_VS.pdbqt')
for isd,procPath in enumerate(procList):
procf = os.path.split(procPath)[1]
mpath = Local_PR253_Pat.match(procf)
if mpath:
ligand = mpath.groups()[0]
else:
print 'Local_PR253_Top_visit_ADV: bad match?!',procPath
continue
###-------
ligData = parseADPDBQT_ADV(procPath)
###-------
if not(ligData):
print 'Local_PR253_Top_visit_ADV: invalid ADV file?!',procPath
continue
dk = (receptor,ligand)
if dk in dataTbl:
print 'Local_PR253_Top_visit_ADV: dup dataKey?!',dk
continue
dataTbl[dk] = ligData
totParse += 1
print 'Local_PR253_Top_visit_ADV: TotParse=',totParse
summf = outdir+'/ADV_summ.csv'
interf = outdir+'/ADV_inter.json'
rptLocalData_ADV(dataTbl,summf,interf)
# 141016 rikPR19 dockings: 19 inhib X
# .../wcg/processed/rikPR19/processed/Results_x1A30-AS/PDB017_x1A30-AS_vina-out_Vina_VS.pdbqt
# ...Results_x1FB7-1w0n0-AAsh25/PDB017_x1FB7-1w0n0-AAsh25_vina-out_Vina_VS.pdbqt
#
LocalRE_PR19 = r'(.+)_(.+)_vina-out_Vina_VS.pdbqt'
LocalRE_PR19_Pat = re.compile(LocalRE_PR19)
def PR19_Top_visit_ADV(ADV_topDir,outdir,verbose=False):
'crawl local (non-FAAH) processed files'
# 'Results_%s/%s_%s_vina-out_VS.pdbqt' % (recept,lig,recept)
totParse = 0
dataTbl = {}
exptSubList = glob.glob(ADV_topDir+'/Results_*')
print 'PR19_Top_visit_ADV: NSubExperiments=',len(exptSubList)
for ise, exptSubPath in enumerate(exptSubList):
if verbose:
print ' **',ise,exptSubPath
procList = glob.glob(exptSubPath+'/*_vina-out_Vina_VS.pdbqt')
for isd,procPath in enumerate(procList):
# fahv.x3kf0A_ZINC00145439_2057149382_out_Vina_VS.pdbqt
procf = os.path.split(procPath)[1]
mpath = LocalRE_PR19_Pat.match(procf)
if mpath:
(ligand,receptor) = mpath.groups()
else:
print 'PR19_Top_visit_ADV: bad match?!',procPath
continue
###-------
ligData = parseADPDBQT_ADV(procPath)
###-------
if not(ligData):
print 'PR19_Top_visit_ADV: invalid ADV file?!',procPath
continue
dk = (receptor,ligand)
if dk in dataTbl:
print 'PR19_Top_visit_ADV: dup dataKey?!',dk
continue
dataTbl[dk] = ligData
totParse += 1
print 'PR19_Top_visit_ADV: TotParse=',totParse
summf = outdir+'/ADV_summ.csv'
interf = outdir+'/ADV_inter.json'
rptLocalData_ADV(dataTbl,summf,interf)
def rptLocalData_ADV(dataTbl,summf,interf):
'''V1: produce condensed JSON inter file
[ [Expt,BatchNo,Recept,Lig, [IType,[InterEnum] ] ] ]
ala ["Exp96", 197339, "x3ZSW_B_IN_Y3", "Y3_ZINC00626007", [[0, [["B", "R199", "NH2", "O1"], ["B", "K188", "NZ", "O2"]]], [5, [["B", "G82", "CA"], ..., ["B", "I141", "O"]]]]]
'''
summs = open(summf,'w')
summs.write('Recept,Ligand,E,Eff,Nvdw,Ninter\n')
allInter = []
for dk in dataTbl:
(exptname,receptor,lig) = dk
ligData = dataTbl[dk]
ninter = 0
nvdw = 0
for itype in InterTypes:
if itype in ligData:
if itype=='vdw':
nvdw = len(ligData['vdw'])
else:
ninter += len(ligData[itype])
summs.write('%s,%s,%s,%s,%d,%d\n' % \
(receptor,lig,\
ligData['e'],ligData['leff'],nvdw,ninter))
interInfo = [ receptor, lig]
interList = []
for itype in InterTypes:
if itype in ligData:
# additional compression by combining all inter of same type
itlist = []
# convert itype string to its index in InterTypes
itypeIdx = InterTypes.index(itype)
for interTuple in ligData[itype]:
inter = list(interTuple)
# convert 3-letter receptor AA to single char
raa = inter[1]
raalet3 = raa[:3]
pos = raa[3:]
if raalet3 in AADict:
raaLet = AADict[raalet3]
else:
raaLet = 'X'
inter[1] = raaLet+pos
itlist.append(inter)
interList.append( [itypeIdx,itlist] )
interInfo.append(interList)
allInter.append(interInfo)
summs.close()
inters = open(interf,'w')
json.dump(allInter,inters)
inters.close()
def Local_focusedLib_Top_visit_ADV(fldir,outdir,verbose=False):
'crawl Stefanos "focused library" of ILINI compounds'
# .../processed/focusedLib/lib_v2.1__R1/chlorine/0/libR1_clorinated_5/libR1_clorinated_5_out_Vina_VS.pdbqt