#!/usr/bin/env python
#################################################
'''
check_rmsd.py - test whether a ligand has replicated a pose 50% of the time
'''
#################################################
import sys
import os
#################################################
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
n = max([int(i) for i in reduce(flat, clusters, [])])
return max(lens) >= n/2.0
def process_rmsds(l, subdir, thresh=2.0, need=.5):
'''Check for a threshold and return True or False if desired proportion is hit'''
tf = process_best(l)
# The following 4 lines will remove the subdirectory if the test is False
print
#if not tf:
#for file in os.listdir(subdir):
#os.remove(subdir + '/' + file)
#os.rmdir(subdir)
if not tf:
parent = os.pardir('..')
os.chdir('parent')
os.chdir('output')
os.getcwd()
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])