#!/usr/bin/env pyXplor

#
# script to test pdb2psf script, and reading of resulting psf + input pdb
#
#
# it makes sure that the numbers of atoms, bonds, angles, dihe, and impr
# are correct
# and it make sure that the correct number of atoms are read in using the
# resulting psf and protocol.initCoords


expectedData={}

#                           natom nbond nangle ndihe nimpr
expectedData["1bph.pdb"] = (777,  788,   1412,  82,   429, 396)
expectedData["1gbv.pdb"] = (8796, 8912, 16172, 884,  4716, 4380)
expectedData["1rop.pdb"] = (905,  909,   1638,  116,  459,  447)

fieldTitles = ['!NATOM', '!NBOND', '!NTHETA', '!NPHI', '!NIMPHI']


cmd = "pdb2psf"

keys = expectedData.keys()

ok=1
import os
for pdb in keys:
    os.system("%s %s" % (cmd,pdb))
    psf = "%s.psf" % pdb[:-4]

    psfFile = open(psf)

    line = psfFile.readline()
    for field in range(len(fieldTitles)):
        while not fieldTitles[field] in line:
            line = psfFile.readline()
            pass
        val = int(line.split()[0])
        if val != expectedData[pdb][field]:
            ok=0
            print '%s: bad %s: %d != %d' % (pdb, fieldTitles[field],
                                            val, expectedData[pdb][field])
            pass
        pass
    import protocol
    protocol.initStruct(psf)
    protocol.initCoords(pdb)
    if len(AtomSel("known")) != expectedData[pdb][-1]:
        ok=0
        print '%s: bad # of read atoms: %d != %d' % (pdb,
                                                     len(AtomSel("known")),
                                                     expectedData[pdb][-1])
        pass
        
    pass

okFile="testPDB2PSF.ok"
if ok:
    print "all ok"
    open(okFile,"w").write("ok")
else:
    try:
        os.unlink(okFile)
    except:
        pass
    pass
