#!/usr/bin/env python
#
# This script processes the output file generated by PICO, and generates a
# consensus summary of the choices made for each location in the docking
# problem
#

import sys
import re

p = "Variable\((.*)\) = (.*)"
p = re.compile(p)
stat = {}
INPUT = open(sys.argv[1])

for line in INPUT:
  if line[0:8] == 'Variable':
     r = p.match(line.strip())
     s= [eval(r.groups()[0]), eval(r.groups()[1])]
     if s[0] not in stat:
        stat[s[0]] = {}
     if s[1] not in stat[s[0]]:
        stat[s[0]][s[1]] = 0
     stat[s[0]][s[1]] += 1

vars = stat.keys()
vars.sort()

for var in vars:
  print "Variable",var
  vals = stat[var].keys()
  vals.sort()
  total = 0.0
  for val in vals:
    total += stat[var][val]
  for val in vals:
    print "  Value",val,"Percentage",100*stat[var][val]/total
  print ""
