# $CVSHeader: pal8e/src/stathelix-parse.py,v 1.2 2005/02/15 01:10:26 indraneel Rel $

import re, sys
from Bin import Bin
from Sigma import Sigma

stathelixfile = '../dat/stat/stathelix'

fh = file(stathelixfile)
lines = fh.readlines()
fh.close()

newlines = []
for i in range(len(lines)/4):
    for j in [1,2,3]:
        newlines.append(lines[(i*4)+j][:-1])
origlines = lines
lines = newlines
lengths = []
rmsds = []
angles = []
for (i, line) in enumerate(lines):
    regex = '.+stathelix.+\t\[\[(.+)\]\]'
    line = re.sub(regex, "\g<1>", line)
    pieces = line.split("], [")
    for (j, piece) in enumerate(pieces):
        piece = piece.split(", ")
        for (k, p) in enumerate(piece):
            if p:
                if ((i/3.0) == (i/3)):
                    piece[k] = int(p)
                else:
                    piece[k] = float(p)
        pieces[j] = piece
    line = pieces
    lines[i] = line
    if ((i/3.0) == (i/3)):
        lengths.append(line)
    if (((i-1)/3.0) == ((i-1)/3)):
        rmsds.append(line)
    if (((i-2)/3.0) == ((i-2)/3)):
        angles.append(line)

#result[brks][length]
#result = {}
#for i in range(4):
#    result[i] = {}
#    for j in range(100):
#        result[i][j] = []
#
#for (i, ls) in enumerate(lengths):
#    rs = rmsds[i]
#    as = angles[i]
#    for b in range(len(ls)):
#        if (as[b] == ['']): continue
#        a = as[b]
#        result[b][ls[0][0]] += a

#as = []
#brk = 3
#for j in range(8,100):
#    if not result[brk][j]: continue
#    as += result[brk][j]
#if not as: sys.exit('empty array')
#bin = Bin(as)
#bin.bin(2.0, -0.5, 90.5)
#bin.normalise()
#data = bin.getNumarray()
#for d in data:
#    print str(d[0])+'\t'+str(d[1])


#brk = 1
#ary = range(8,20,2) + range(20,30,5) + range(30,40,10) + range(40,100,59)
#for (ctr,leng) in enumerate(ary[:-1]):
#    as = []
#    for i in range(leng, ary[ctr+1]):
#        as += result[brk][i]
#    a = len(as)
#    as.sort()
#    if (a % 2):
#        median = as[int(a/2)]
#    else:
#        median = (as[(a/2)-1] + as[(a/2)])/2.0
#    bin = Bin(as)
#    bin.bin(5.0, -0.5, 90.5)
#    maxval = max(bin.values())
#    for k in bin.keys():
#        if (bin[k] == maxval):
#            mode = k
#            break
#    print str((leng+ary[ctr+1])/2)+'\t'+str(mode)

#data = []
#for (i, ls) in enumerate(lengths):
#    rs = rmsds[i]
#    as = angles[i]
#    b = 1
#    a = as[b]
#    if ((a[0] < 14.0) or
#        ((a[0] < 22.0) and (ls[0][0] >= 20))):
#        data.append(rs[0][0])
#bin=Bin(data)
#bin.bin(0.1, 1.45, 3.45)
#bin.normalise()
#data = bin.getNumarray()
#for d in data:
#    print str(d[0])+'\t'+str(d[1])

#data = []
#brk = 2
#for (i, ls) in enumerate(lengths):
#    rs = rmsds[i]
#    as = angles[i]
#    if ((len(as) <= brk) or (not as[brk])): continue
#    a = as[1]
#    if ((a[0] < 14.0) or
#        ((a[0] < 22.0) and (ls[0][0] >= 20))):
#        data += as[brk]
#bin=Bin(data)
#bin.bin(2, -0.5, 90.5)
#bin.normalise()
#data = bin.getNumarray()
#for d in data:
#    print str(d[0])+'\t'+str(d[1])

#for (i, ls) in enumerate(lengths):
#    rs = rmsds[i]
#    as = angles[i]
#    b = 1
#    a = as[b]
#    if ((a[0] < 14.0) or
#        ((a[0] < 22.0) and (ls[0][0] >= 20))):
#        print str(ls[0][0])+'\t'+str(rs[0][0])

#data={}
#for (i, ls) in enumerate(lengths):
#    rs = rmsds[i]
#    as = angles[i]
#    b = 1
#    a = as[b]
#    if ((a[0] < 14.0) or
#        ((a[0] < 22.0) and (ls[0][0] >= 20))):
#        if not data.has_key(ls[0][0]): data[ls[0][0]] = []
#        data[ls[0][0]].append(rs[0][0])
#for k in data.keys():
#    if (len(data[k]) < 2): continue
#    sigma = Sigma(data[k])
#    s = sigma.sigma()
#    print str(k)+'\t'+str(s)

#data = []
#for (i, ls) in enumerate(lengths):
#    rs = rmsds[i]
#    as = angles[i]
#    b = 1
#    a = as[b]
#    if ((a[0] < 14.0) or
#        ((a[0] < 22.0) and (ls[0][0] >= 20))):
#        if (2.45 < rs[0][0] < 2.50):
#            line = origlines[i*4][:-1]+' '+str(rs[0][0])
#            regex = '(.+):.+ stathelix ElementType:.+Chain: (.)\s+'
#            regex += '(\d+)\D+<====>\s+(\d+)\D+(\d+)\.(\d+)'
#            subst = '\g<1>\t\g<2>\t\g<3>\t\g<4>\t\g<5>.\g<6>'
#            line = re.sub(regex, subst, line)
#            print line

#data = []
#for (i, ls) in enumerate(lengths):
#    rs = rmsds[i]
#    as = angles[i]
#    b = 1
#    a = as[b]
#    if ((a[0] > 22.0) or
#        ((a[0] > 14.0) and (ls[0][0] < 20))):
#        if (2.45 < rs[0][0] < 2.50):
#            line = origlines[i*4][:-1]+' '+str(rs[0][0])+'\t'+str(a[0])
#            regex = '(.+):.+ stathelix ElementType:.+Chain: (.)\s+'
#            regex += '(\d+)\D+<====>\s+(\d+)\D+(\d+)\.(\d+)'
#            subst = '\g<1>\t\g<2>\t\g<3>\t\g<4>\t\g<5>.\g<6>'
#            line = re.sub(regex, subst, line)
#            print line
