#!/usr/bin/env python
#################################################
'''
check_rmsd.py - test whether a ligand has replicated a pose 50% of the time
'''
#################################################
import sys
import os
import shutil
#################################################
def get_rmsds(fname, thresh=2.0):
'''fish out the rmsds from a log file - return the last line where
distance < threshold (2.0)'''
f = open(fname)
start = False
ret = []
dist = 0.0
best = ''
for l in f:
flds = l.split()
if len(flds) < 2:
continue
if flds[0] == 'Distance' and flds[2] == 'Clusters':
start = True
elif flds[0] == 'Finished':
start = False
elif start:
# find the line with largest distance less then 2.0
d = float(flds[0])
if d < thresh:
best = l
else:
f.close()
return best
return best # if no line is worse than threshold
def process_best(best):
'''return true if largest cluster is more than 50% of n_docks'''
flds = best.split('|')[1:-1]
clusters = [f.split() for f in flds]
lens = map(len, clusters)
def flat(x, y):
x.extend(y)
return x
try:
n = max([int(i) for i in reduce(flat, clusters, [])])
except ValueError:
pass
try:
return max(lens) >= n/2.0
except ValueError:
pass
def process_rmsds(l, subdir, thresh=2.0, need=.5):
'''Check for a threshold and return True or False if desired proportion is hit'''
oldcwd = os.getcwd()
try:
tf = process_best(l)
except ValueError:
tf = False
# The following 4 lines will remove the subdirectory if the test is False
if not tf:
shutil.rmtree(subdir)
os.chdir(os.pardir)
os.chdir('Output')
int_line = 0
list_files = [i for i in open('bestranking.lst', 'r+')]
for file in list_files:
file[1:len(subdir)]
if file.find(subdir) >= 0:
del(list_files[int_line])
int_line += 1
#Write the new file
replacement = open('bestranking.lst', 'w')
replacement.write(''.join(list_files))
replacement.close()
os.chdir(oldcwd)
return tf
#################################################
def main(parent_dir):
print parent_dir
os.chdir(parent_dir)
sub_dirs = os.listdir(parent_dir)
for fname in sub_dirs:
log_file = parent_dir + '/' + fname + '/' + 'gold_' + fname + '.log'
print process_rmsds(get_rmsds(log_file), fname), log_file
def usage():
print 'Usage: %s results_parent_directory' % sys.argv[0]
print '''prints True or False value and the name of the log file which
produced it. The value is whether more than 50% of solutions had an rmsd of
less than 2.0. If the test fails, the directory containing the log file is deleted'''
if __name__ == "__main__":
if len(sys.argv) < 2 or '-h' in sys.argv:
usage()
else:
main(sys.argv[1])