       !************************************************************************
       !           A Subroutine of Main, Output the trajectory file            *
       !************************************************************************
       
       Subroutine TrajectoryWriting ( PDBid_A, PDBid_B, lmtA, SITE, Nstp, PDBRecord, Line, ATM, AANm, X, Y, Z, vd, Seq, SeqIndex, rn, Mstart )
       
       Implicit None
       
! ---- Variables : --------------------------------------------------------------------------------- !
       
       CHARACTER   PDBid_A*4
       CHARACTER   PDBid_B*4
       
       Integer     lmtA(4,2)   ! Range of Structure A
       
       Integer     SITE
       
       Integer     Nstp        ! Total of trajectory steps

       Character   PDBRecord(100,5000)*80 ! Recording the content of each line : PDBRecord(RecordType, Line#) 
       Integer     Line(100)              ! The line number of each RecordType : Line(RecordType)

       Character   ATM(  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   AANm(  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
       
       Real        X(  lmtA(1,1):lmtA(1,2), lmtA(2,1):lmtA(2,2), lmtA(3,1):lmtA(3,2), lmtA(4,1):lmtA(4,2) )         ! Coordinates: X
       Real        Y(  lmtA(1,1):lmtA(1,2), lmtA(2,1):lmtA(2,2), lmtA(3,1):lmtA(3,2), lmtA(4,1):lmtA(4,2) )         ! Coordinates: Y
       Real        Z(  lmtA(1,1):lmtA(1,2), lmtA(2,1):lmtA(2,2), lmtA(3,1):lmtA(3,2), lmtA(4,1):lmtA(4,2) )         ! Coordinates: Z
       
       Integer     vd( lmtA(1,1):lmtA(1,2), lmtA(2,1):lmtA(2,2), lmtA(3,1):lmtA(3,2), lmtA(4,1):lmtA(4,2) )         ! Record the existence of the ATOM :
              
       Integer     Seq( lmtA(1,1):lmtA(1,2), lmtA(2,1):lmtA(2,2), lmtA(3,1):lmtA(3,2), lmtA(4,1):lmtA(4,2) ) ! The sequential number of a certain node at (k,j,i,s)
       Integer     SeqIndex(1:SITE,1:4) ! Array SeqIndex(Site#,1/2/3/4) /Model#/Chain#/Residue#/Atom#/
       
       
       Real        rn(1:Nstp, 1:SITE, 1:3)    ! The coordinates of the strucutre in the transition pathway
       
       Integer        Mstart  ! The starting MODEL number
       
       
! ---- Variables : --------------------------------------------------------------------------------- !

       Integer     MinModel, MinChain, MinResidue, MinAtom  
       Integer     MaxModel, MaxChain, MaxResidue, MaxAtom  
       
       Real        DX(1:SITE, 1:Nstp)
       Real        DY(1:SITE, 1:Nstp)
       Real        DZ(1:SITE, 1:Nstp)

       Real        Deform(1:SITE)
       
       Integer     RecordType ! each section in PDB file is assigned a RecordType

       Character   DUMMY6*6
       Character   ATNAME*4
       Character   DUMMY3*3
       Character   A1*1
       Integer     D1INT, D2INT
       Real        XXX, YYY, ZZZ, BBB
       Real        R1
       
       Character   ChainName*1
       Character   IDModel*1
       
       Character   DUMMY80*80
       
	   Character   ACE*2 ! the string will be stored here 
       Character   SegID*5
       

! ---- Variables : --------------------------------------------------------------------------------- !
       Integer     k, j, i, s, n, m, t, jj, ct
       
! ==== Body of Subroutine TrajectoryWriting ======================================================== !     


       MinModel      =  lmtA(1,1)
       MaxModel      =  lmtA(1,2)
       MinChain      =  lmtA(2,1)
       MaxChain      =  lmtA(2,2)
       MinResidue    =  lmtA(3,1)
       MaxResidue    =  lmtA(3,2)
       MinAtom       =  lmtA(4,1)
       MaxAtom       =  lmtA(4,2)                                               
       
       
! |.| |.| |.| |.| |.| |.| |.| |.| |.| |.| |.| |.| |.| |.| |.| |.| |.| |.| |.|  Preparing the DX(), DY(), DZ()       
       
       Do t=1, Nstp
            Do n=1, SITE
                DX(n,t) = rn(t,n,1) - rn(1,n,1)
                DY(n,t) = rn(t,n,2) - rn(1,n,2)
                DZ(n,t) = rn(t,n,3) - rn(1,n,3)
            Enddo ! n   
       Enddo ! t
       
       Call Bcolor ( SITE, Nstp, DX, DY, DZ, Deform )       

! [M].[M].[M].[M].[M].[M].[M].[M].[M].[M].[M].[M].[M].[M].[M].[M].[M].[M].[M].[M].[M]. Movie Generating Loop 
        
        Open( 110, File='['//PDBid_A//'-'//PDBid_B//']_Transition Trajectory'//'.pdb' )
        
        RecordType=1     !+++++++++++++++++  Title Section
        Do i=1, Line(RecordType)
            Write(110, '(A80)') PDBRecord(RecordType, i)
        Enddo
            
        Do t=1, Nstp ! _________________________________________________________________________ [movie.pdb]
        
            Write(*,'(A,I4)') '-> Now writing the trajectory PDB files | .......Step : ', t                       
            
            Write(110,'(A6,5X,I3)') 'MODEL ', t+Mstart-1
            
            RecordType=5     !+++++++++++++++++  Secondary Structure 
            Do i=1, Line(RecordType)
                Write(110, '(A80)') PDBRecord(RecordType, i)
            Enddo
                                
            Do k=MinModel, MaxModel               
                                    
                ct=0
                Do j=MinChain, MaxChain
                    
                    Do i=MinResidue, MaxResidue
                        Do s=MinAtom, MaxAtom 
            
                            IF(vd(k,j,i,s)>0) Then
                            
                                DUMMY6='ATOM  '
                                ATNAME=ATM(k,j,i,s)
                                DUMMY3=AANm(k,j,i,s)
                                A1=ChainName(j)
                                D2INT=i
                                R1=1.00
                                
                                If( (vd(k,j,i,s)==1).AND.(1<=s).AND.(s<=4) ) Then ! Backbone Atoms
                                    
                                    ct=ct+1
                                    D1INT=ct
                                    
                                    n = seq(k,j,i,2)
                                    
                                    ! atom CA
                                    XXX = X(k,j,i,s) + DX(n,t)
                                    YYY = Y(k,j,i,s) + DY(n,t)
                                    ZZZ = Z(k,j,i,s) + DZ(n,t)
                                    BBB = Deform(n)
                                    
                                    If( (s==1).AND.(1<n).AND.(n<SITE) ) Then  ! atom N
                                        jj=SeqIndex(n-1,2)
                                        If(jj==j) Then
                                            XXX = X(k,j,i,s) + 0.25*DX(n-1,t) + 0.75*DX(n,t)
                                            YYY = Y(k,j,i,s) + 0.25*DY(n-1,t) + 0.75*DY(n,t)
                                            ZZZ = Z(k,j,i,s) + 0.25*DZ(n-1,t) + 0.75*DZ(n,t)
                                            BBB = 0.25*Deform(n-1) + 0.75*Deform(n)
                                        EndIF ! (jj==j)
                                    EndIF ! ( (s==1).AND.(1<n).AND.(n<SITE)   ! atom N
                                    
                                    If( (s==3).AND.(1<n).AND.(n<SITE) ) Then  ! atom C
                                        jj=SeqIndex(n+1,2)
                                        If(jj==j) Then
                                            XXX = X(k,j,i,s) + 0.25*DX(n+1,t) + 0.75*DX(n,t)
                                            YYY = Y(k,j,i,s) + 0.25*DY(n+1,t) + 0.75*DY(n,t)
                                            ZZZ = Z(k,j,i,s) + 0.25*DZ(n+1,t) + 0.75*DZ(n,t)
                                            BBB = 0.25*Deform(n+1) + 0.75*Deform(n)
                                        EndIF ! (jj==j)
                                    EndIF ! ( (s==3).AND.(1<n).AND.(n<SITE)   ! atom C
                                    
                                    If( (s==4).AND.(1<n).AND.(n<SITE) ) Then  ! atom O
                                        jj=SeqIndex(n+1,2)
                                        If(jj==j) Then
                                            XXX = X(k,j,i,s) + 0.50*DX(n+1,t) + 0.50*DX(n,t)
                                            YYY = Y(k,j,i,s) + 0.50*DY(n+1,t) + 0.50*DY(n,t)
                                            ZZZ = Z(k,j,i,s) + 0.50*DZ(n+1,t) + 0.50*DZ(n,t)
                                            BBB = 0.50*Deform(n-1) + 0.50*Deform(n)
                                        EndIF ! (jj==j)
                                    EndIF ! ( (s==4).AND.(1<n).AND.(n<SITE)   ! atom O
                                    
                                    SegID(1:3)='000'
                                    SegID(4:5)=ACE(k,2)
                                    Write(110,66) DUMMY6,D1INT,ATNAME,DUMMY3,A1,D2INT,XXX,YYY,ZZZ,R1,BBB,SegID(2:5),ATNAME(2:2)
                                    
                                EndIf ! ( (vd(k,j,i,s)==1).AND.(1<=s).AND.(s<=4) ) 
                                
                                If(vd(k,j,i,s)==2) Then
                                    ct=ct+1
                                    D1INT=ct
                                    n = seq(k,j,i,40)
                                    XXX = X(k,j,i,s) + DX(n,t)
                                    YYY = Y(k,j,i,s) + DY(n,t)
                                    ZZZ = Z(k,j,i,s) + DZ(n,t)
                                    BBB = Deform(n)
                                    SegID(1:3)='000'
                                    SegID(4:5)=ACE(k,2)
                                    Write(110,66) DUMMY6,D1INT,ATNAME,DUMMY3,A1,D2INT,XXX,YYY,ZZZ,R1, BBB,SegID(2:5),ATNAME(2:2)
                                EndIf                                                    
                                
                            EndIF  ! IF(vd(k,j,i,s)>0)
                            
                        Enddo  ! s
                    Enddo  ! i
                    ct=ct+1
                    Write(110, '(A6,I5,6x,A3,1x,A1,I4)') 'TER   ', ct, DUMMY3, A1, D2INT
                    
                Enddo  ! j
            
            Enddo ! k    
            
            Write(110,'(A6)') 'ENDMDL'        
        Enddo ! t=1, Nstp ! _________________________________________________________________________ [movie.pdb]
        
        Write(110,'(A6)') 'END   '        
        
        Close(110)  ! File='['//PDBid_A//'-'//PDBid_B//']_Transition Trajectory'//'.pdb'

66	   FORMAT(A6,I5,1X,A4,1X,A3,1X,A1,I4,4X,3F8.3,2F6.2,6x,A4,1x,A1)       
       
       Write(*,*) ' '
       Write(*,'(A,I4)') 'The final MODEL number is : ', Nstp+Mstart-1
       Write(*,*) ' '
       
! =====E==N===D====================================================================================== !
       Return
       End                                  