!      %***************************************************************************************%
!      *          Subroutine of Main, locating the common residues of structure A & B          *
!      %***************************************************************************************%

       Subroutine CommonResidue( lmtA, lmtB, lmtC, vd_A, vd_B, AANm_A, AANm_B, ATM_A, ATM_B, vd, SITE, SeqIndex, Seq )
       
       Implicit None       

! ---- Variables : ---------------------------------------------------------------------------------- !       
       Integer     lmtA(4,2)   ! Range of Structure A
       Integer     lmtB(4,2)   ! Range of Structure B
       Integer     lmtC(4,2)   ! Range of Common 
       
       Integer     vd_A( lmtA(1,1):lmtA(1,2), lmtA(2,1):lmtA(2,2), lmtA(3,1):lmtA(3,2), lmtA(4,1):lmtA(4,2) )
       Integer     vd_B( lmtB(1,1):lmtB(1,2), lmtB(2,1):lmtB(2,2), lmtB(3,1):lmtB(3,2), lmtB(4,1):lmtB(4,2) )
       
       Character   AANm_A( lmtA(1,1):lmtA(1,2), lmtA(2,1):lmtA(2,2), lmtA(3,1):lmtA(3,2), lmtA(4,1):lmtA(4,2) )*3    ! The Name of Residue
       Character   AANm_B( lmtB(1,1):lmtB(1,2), lmtB(2,1):lmtB(2,2), lmtB(3,1):lmtB(3,2), lmtB(4,1):lmtB(4,2) )*3    ! The Name of Residue

       Character   ATM_A( lmtA(1,1):lmtA(1,2), lmtA(2,1):lmtA(2,2), lmtA(3,1):lmtA(3,2), lmtA(4,1):lmtA(4,2) )*4    ! The Name of atom
       Character   ATM_B( lmtB(1,1):lmtB(1,2), lmtB(2,1):lmtB(2,2), lmtB(3,1):lmtB(3,2), lmtB(4,1):lmtB(4,2) )*4    ! The Name of atom
        
       Integer     vd( lmtC(1,1):lmtC(1,2), lmtC(2,1):lmtC(2,2), lmtC(3,1):lmtC(3,2), lmtC(4,1):lmtC(4,2) )
       
       Integer     SITE
       Integer     SeqIndex(1:SITE, 1:4)
       Integer     Seq( lmtC(1,1):lmtC(1,2), lmtC(2,1):lmtC(2,2), lmtC(3,1):lmtC(3,2), lmtC(4,1):lmtC(4,2) )
       
! ---- Variables : ---------------------------------------------------------------------------------- !       

       Logical     NodeExist
       Character   ATNAME*4
       Character   NodeAtom(100)*4
       Integer     MaxNode
       

       Integer     MinModel, MinChain, MinResidue, MinAtom  ! The initial Min value is set to an extreme big value
       Integer     MaxModel, MaxChain, MaxResidue, MaxAtom  ! The initial Max value is set to an extreme small value
       
       
! ---- Variables : ---------------------------------------------------------------------------------- !       
       Integer     k, j, i, s, ct, n, m
       
       
! ==== Body of Subroutine CommonRange =============================================================== !

! ------------------------------------------------------- Read in the 'PDB Nodes Atoms.inp' File
       INQUIRE (FILE='PDB Nodes Atoms.inp', EXIST=NodeExist) 
       If(NodeExist==.False.) Then
            Write(*,*)  '  Error! Can not find the [PDB Nodes Atoms.inp] file. '
            Stop
       EndIf
       
       Open (121, FILE='PDB Nodes Atoms.inp')
       
       m=0
       Read(121,'(A4)')  ATNAME
       Do While(ATNAME.NE.'====')
          m=m+1
          NodeAtom(m) = ATNAME
          Read(121,'(A4)')  ATNAME
       Enddo
       MaxNode=m
       
       Close(121) ! FILE='PDB Nodes Atoms.inp'

! #-=-# #-=-# #-=-# #-=-# #-=-# #-=-# #-=-# #-=-# #-=-# #-=-# #-=-# #-=-# #-=-# #-=-# #-=-# 
     
       MinModel      =  lmtC(1,1)
       MaxModel      =  lmtC(1,2)
       MinChain      =  lmtC(2,1)
       MaxChain      =  lmtC(2,2)
       MinResidue    =  lmtC(3,1)
       MaxResidue    =  lmtC(3,2)
       MinAtom       =  lmtC(4,1)
       MaxAtom       =  lmtC(4,2)                                               
       

       ct=0
       
       Do k=MinModel, MaxModel 
          Do j=MinChain, MaxChain
            Do i=MinResidue, MaxResidue
              Do s=MinAtom, MaxAtom
                 
                 vd(k,j,i,s)=0
                 
                 IF( (vd_A(k,j,i,s)>0).AND.(vd_B(k,j,i,s)>0) )Then
                    
                    If( AANm_A(k,j,i,s)==AANm_B(k,j,i,s) .AND. vd_A(k,j,i,s)==vd_B(k,j,i,s) ) Then

                          vd(k,j,i,s)=vd_A(k,j,i,s)
                          
                          Do m=1, MaxNode
                             If( ATM_A(k,j,i,s)==NodeAtom(m) ) Then
                                ct=ct+1
                                SeqIndex(ct,1)=k
                                SeqIndex(ct,2)=j
                                SeqIndex(ct,3)=i
                                SeqIndex(ct,4)=s
                                Seq(k,j,i,s)=ct
                             EndIF
                          Enddo
                            
                    EndIF 
                 EndIF
                 
              Enddo  ! s
            Enddo  ! i
          Enddo  ! j
       Enddo ! k
          
       SITE=ct
        
       Write(*,*) '  '
       Write(*,*) 'Total common site :  ', SITE
       Write(*,*) '  '
       Write(*,*) '= - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - ='

       
       
       
! =====E==N===D====================================================================================== !
       Return
       End                   