!  ANM_Hessian_Generator.f90 
!
!  FUNCTIONS:   Generate the Hessian Matrix according to the ANM Model 
!  ANM_Hessian_Generator      - Entry point of console application.
!

!****************************************************************************
!
!  PROGRAM: ANM_Hessian
!
!  Author: Zheng Yang, proteinyang@gmail.com 
!
!  Date:  11/16/2006
!
!****************************************************************************

        program ANM_Hessian_Generator

        implicit none

! ----- Variables : ------------------------------------------------------- Parameter
        Integer     PDBmax
        Parameter   (PDBmax=500) ! PDB List Possiable Maximum number
        
! ----- Variables : @ * @ * @ * @ * @ * @ * @ * @ * Part 1.0 : Read in [PDBList.inp]     
        
        Character   CharTemp*80
        Character   PDBList(1:PDBmax)*4
        
        Integer     Status
        Integer     ct
        
        Integer     mPDB    ! The total number of PDB id(s) in the PDB List

! ----- Variables : =_=_=_=_=_=_=_=_=_=_=_=_=_=_=_=_=_=_=_=_=_=_=_=_=_=_=_=_=_=_=_=_ Read in [runANM.inp]                
       
        Real        rcut   ! cutoff distance (in A)
        Real        gama   ! the force constant, gama
        
! ----- Variables : H-H H-H H-H H-H H-H H-H H-H H-H Part 2.0 : Generating the Hessian
        
        Character   PDBid*4
        
        Logical     XYZexist 
        Logical     PDBexist 
        Logical     shpexist 


! ----- Variables : -----------------------------------------------------------------------!
        Integer     i

! ===== Body of ANM_Hessian_Generator ===================================== ! 
        
! @ * @ * @ * @ * @ * @ * @ * @ * @ * @ * @ * @ * @ * @ * @ * Part 1.0 : Read in [PDBList.inp]        
        
        Open(08, File='PDBList.inp')
        
        status=1
        ct=0
        Do While(status==1)
            
            Read(08,'(A)') CharTemp
            
            If( CharTemp(1:4) .NE. '====' ) Then
                ct=ct+1
                PDBlist(ct)=CharTemp(1:4)
                If (ct>=PDBmax) Stop 'Error! PDB list too big! '
            Else
                status=0
            EndIf
        
        Enddo ! While(status==1)
        
        mPDB=ct
        
        Close(08) ! File='PDBList.inp'
        
! =_=_=_=_=_=_=_=_=_=_=_=_=_=_=_=_=_=_=_=_=_=_=_=_=_=_=_=_=_=_=_=_=_=_=_=_=_ Read in [runANM.inp]        
        
        Open (07, File='runANM.inp')
        
        Read(07,*) rcut
        Read(07,*) gama
        
        Close (07) ! File='runANM.inp'
        
        
! H-H H-H H-H H-H H-H H-H H-H H-H H-H H-H H-H H-H H-H H-H Part 2.0 : Generating the Hessian
        
        Do i=1, mPDB
        
            PDBid=PDBlist(i)
            
            INQUIRE ( FILE = PDBid(1:4)//'.pdb', EXIST = PDBexist )
            INQUIRE ( FILE = PDBid(1:4)//'.xyz', EXIST = XYZexist ) 
            INQUIRE ( FILE = 'spherical_pertubation.inp', EXIST = shpexist )
            
            If ( PDBexist==.TRUE. )  Then
            
                If ( shpexist == .TRUE. ) Then
                     Call  Hessian_sphere ( PDBid, rcut, gama )
                Else 
                     Call Hessian ( PDBid, rcut, gama )
                EndIf
                                
            Else If (  (PDBexist==.FALSE.) .AND. (XYZexist==.TRUE.) ) Then
                Call  Hessian_XYZ ( PDBid, rcut, gama )
            
            Else If (  (PDBexist==.FALSE.) .AND. (XYZexist==.FALSE.) ) Then
                Write(*,'(A,A)') 'Error: PDB and XYZ file does not exists! [PDBid] : ', PDBid
            
            EndIf    
                                    
        Enddo
        

               
! ===== E = N = D ========================================================= !
 
        end program ANM_Hessian_Generator

