c
c
c     ###################################################
c     ##  COPYRIGHT (C)  1990  by  Jay William Ponder  ##
c     ###################################################
c
c     ###############################################################
c     ##                                                           ##
c     ##  subroutine prtpdb  --  output of Protein Data Bank file  ##
c     ##                                                           ##
c     ###############################################################
c
c
c     "prtpdb" writes out a set of Protein Data Bank
c     coordinates to an external disk file
c
c
      subroutine prtpdb (ipdb)
      implicit none
      include 'sizes.i'
      include 'files.i'
      include 'pdb.i'
      include 'titles.i'
      integer i,k,ipdb
      character*60 pdbfile
      logical opened
c
c
c     open output unit if not already done
c
      inquire (unit=ipdb,opened=opened)
      if (.not. opened) then
         pdbfile = filename(1:leng)//'.pdb'
         call version (pdbfile,'new')
         open (unit=ipdb,file=pdbfile,status='new')
      end if
c
c     write out the header lines and the title
c
      if (ltitle .eq. 0) then
         write (ipdb,10)
   10    format ('HEADER',/,'COMPND',/,'SOURCE')
      else
         write (ipdb,20)  title(1:ltitle)
   20    format ('HEADER',4x,a,/,'COMPND',/,'SOURCE')
      end if
c
c     next, write the coordinates for each PDB atom
c
      do i = 1, npdb
         write (ipdb,30)  pdbtyp(i),i,atmnam(i),resnam(i),
     &                    resnum(i),xpdb(i),ypdb(i),zpdb(i)
   30    format (a6,i5,1x,a4,1x,a3,2x,i4,4x,3f8.3)
      end do
c
c     finally, write any connectivity records for PDB atoms
c
      do i = 1, npdb
         if (npdb12(i) .ne. 0) then
            write (ipdb,40)  i,(ipdb12(k,i),k=1,npdb12(i))
   40       format ('CONECT',5i5)
         end if
      end do
      write (ipdb,50)
   50 format ('END')
c     close (unit=ipdb)
      return
      end
