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

# generates helices to view using pymol
# uses helices of rmsd ranges 2.45-2.5, 2.5-2.55...
# uses pdbcut2

import os

pdbdir = '../../dat/helixbrk-pdb/'
outdir = '../../dat/helixbrk-helices/'
rangefiledir = '../dat/stat/'
#rangefiles = ['2.45-2.50', '2.50-2.55', '2.55-2.60', '2.60-2.65']
#rangefiles += ['2.65-2.70', '2.70-2.75', '2.75-2.80']
rangefiles = ['bad-helix']
pdbcut2 = '../../../bin/pdbcut2 '

for rangefile in rangefiles:
    fh = file(rangefiledir+rangefile)
    lines = fh.readlines()
    fh.close()
    pmlbuffer = []
    ctr = 0
    rctr = 0
    for line in lines:
        line = line[:-1]
        fields = line.split()
        infile = fields[0]
        outfile = fields[0]+'_'+fields[2]+'_'+fields[3]
        _range = fields[1]+':'+fields[2]+'.'+fields[1]+':'+fields[3]
        rmsd = fields[4]
        angle = fields[5]
        if not os.path.isfile(outdir+outfile+'.pdb'):
            command = pdbcut2
            command+= '-i '+pdbdir+infile+'.pdb '
            command+= '-o '+outdir+outfile+'.pdb '
            command+= '-r '+_range
            os.system(command)
        pmlline = 'load '+outfile+'.pdb\n'
        pmlline += 'set_title '+outfile+', 1, '+str(rmsd)+' '+str(angle)+'\n'
        pmlline += 'hide lines, '+outfile+'\n'
        pmlline += 'show ribbon, '+outfile+'\n'
        pmlline += 'hide ribbon, '+outfile+'\n'
        pmlbuffer += [pmlline]
        if (ctr >=50):
            fh=file(outdir+rangefile+'_'+str(rctr)+'.pml', 'w')
            fh.writelines(pmlbuffer)
            fh.close()
            pmlbuffer = []
            rctr += 1
            ctr = 0
        ctr += 1
    fh=file(outdir+rangefile+'_'+str(rctr)+'.pml', 'w')
    fh.writelines(pmlbuffer)
    fh.close()
