#!/usr/bin/env pyXplor
#
# download a number of entries from the PDB.
# load with psfGen.pdbToPSF
# and protocol.initCoords
#
# check that the number of atoms and number of known atoms is correct
# also test the center of mass
#
#
#

import sys

pdbLocation="ftp://ftp.rcsb.org/pub/pdb/data/structures/divided/pdb/"

# download pdb entry to file
# use uncompress on file

def testEntry(entry,
              numAtoms,
              numKnown,
              cm):
    """
    download and test the given entry against previously computed values.
    """

    ok=1

    import protocol
    protocol.initStruct() #erase any existing structure info

    print 'entry: %s ' % entry,
    sys.stdout.flush()

    contents = protocol.downloadPDB(entry)

    print ' [downloaded]',
    sys.stdout.flush()
    
    import psfGen
    psfGen.pdbToPSF(contents)

    print ' [psf]',
    sys.stdout.flush()
    sim = xplor.simulation
    if sim.numAtoms() != numAtoms:
        print '\tnumAtoms: %d != %d (recorded value)' %(sim.numAtoms(),
                                                        numAtoms)
        ok=0
        pass

    import protocol
    protocol.initCoords(string=contents)

    print ' [coords]',
    sys.stdout.flush()
    lenKnown=len(AtomSel('known'))
    if lenKnown != numKnown:
        print '\tnumKnown: %d != %d (recorded value)' %(lenKnown,
                                                        numKnown)
        ok=0
        pass

    from vec3 import Vec3
    center=Vec3(0,0,0)
    sel=AtomSel('known')
    for atom in sel:
        center += atom.mass() * atom.pos()
        pass
    if len(sel):
        center /= len(sel)
        pass

    print ' [cm]',
    sys.stdout.flush()
    tol=0.01
    from vec3 import norm
    if norm(center- cm)>tol:
        print '\tcenter of mass:', center,
        print '!=', cm, '(recorded value)' 
        ok=0
        pass

    if ok: 
	print ' ok'
    else:
        print ' FAIL'
        pass
	

    return ok

    
exit=0
for (entry,numAtoms,numKnown,cm) in (
    ('1bmf', 46056, 22722, [1451.529,  980.474,  989.056]),
    ('1bph', 777  ,   396, [ 276.744,  521.623,  386.037]),
    ('1chg', 3367 , 1643 , [ 171.954,  342.705,  172.004]),
    ('1gal', 8718 , 4452 , [ 504.091,   89.948,  697.444]),
    ('1gbv', 8796 , 4380 , [ 132.195,  174.195,  523.280]),
    ('1gox', 5460 , 2696 , [ 743.665,  651.605,  322.137]),
    ('1hbp', 2759 , 1411 , [ 341.969,  327.496,  609.195]),
    ('1one', 13208, 6584 , [ 390.809,  -69.776,  315.588]),
    ('1pkm', 8044 , 3976 , [ 466.841,  568.866,  500.242]),
    ('1rop', 905  , 447  , [ 651.353,  113.028,  395.699]),
    ('1rsc', 72416, 36496, [1111.273,  280.172,   -1.408]),
    ('1stm', 10550, 5305 , [  -0.000,  435.481,  704.623]),
    ('1thb', 8804 , 4384 , [  50.693,   18.286,  201.377]),
    ('256b', 3308 , 1652 , [ -55.703,  -12.443,   58.982]),
    ('2ctb', 4799 , 2442 , [-107.403,  269.845, -137.438]),
    ('2fua', 3269 , 1625 , [-281.853,  688.809,  372.648] ),
    ('2gyi', 11864, 6036 , [ 136.221,   -0.086, -307.922]),
    ('2lhb', 2300 , 2299 , [ 103.074,  249.157,  -62.105]),
    ('2mhr', 1926 , 1926 , [ 135.592,  352.943,   95.409]),
    ('2ohx', 11292, 5570 , [ 152.483,  129.850,  -11.044]),
    ('2sim', 5847 , 2954 , [ 508.242,  540.651,  626.926]),
    ('2trm', 3211 , 1627 , [ 123.009,  569.662,  634.056]),
    ('2ycc', 1708 , 848  , [  59.974,  261.720,  100.885]),
    ('3fua', 3205 , 1593 , [1096.481,  617.099,  940.219]),
    ('3sdp', 5644 , 2908 , [1246.695,  668.477,  384.833]),
#    ('4cac', 4101 , 1807 , [-125.155,  -20.581,  209.678]),
    ('4gcr', 2847 , 1474 , [ 132.549,  223.689,  339.813]),
    ('4tim', 7616 , 3766 , [ 506.833,  307.929,   52.689]),
    ('6abp', 4677 , 2316 , [ 174.524,  768.430,  690.161]),
    ('8cpp', 6381 , 3208 , [ 688.335,  569.295,  165.257]),
    ('3f6q', 3764 , 1926 , [ 269.859,  100.668,  -52.907]),
    ):

    if not testEntry(entry,numAtoms,numKnown,cm):
        exit=1
        pass
    pass

sys.exit(exit)
