c---------------------------------------------------------------------
c                  PROGRAM CHIMERASPHERE             ECMeng  1/8/08
c  --interactive
c  --multicluster sphgen output -> multimodel PDB, radius assignmt file
c---------------------------------------------------------------------
c
      character*80 sphfil, pdbout, radout, sphlin
      integer minsiz, imod, iclust, nsph, isph, start
      character*5 cimod, ciclust, cisph
      real x,y,z,rad
c
      write (6, *) 'enter name of sphere file:'
      read (5, 1000) sphfil
 1000 format (A80)
      write (6, 1000) sphfil
      write (6, *) 'minimum size to include?'
      read (5, *) minsiz
      write (6,*) minsiz
      write (6, *) 'enter name of output PDB:'
      read (5, 1000) pdbout
      write (6, 1000) pdbout
      write (6, *) 'enter name of radius assignment file:'
      read (5, 1000) radout
      write (6, 1000) radout
c     write (6, *) 'model number to use?'
c     read (5, *) imod
c     write (cimod, '(i5)') imod
c     write (6, '(a)') cimod(start(cimod):5)
c
      open (unit=1, file=sphfil, status='old')
      open (unit=2, file=pdbout, status='unknown')
      open (unit=3, file=radout, status='unknown')
c
      write (3,'(a)') 'attribute: radius'
      write (3,'(a)') 'match mode: 1-to-1'
      write (3,'(a)') 'recipient: atoms'
c
   50 read (1, 1000, end=500) sphlin
      if (sphlin(1:7).eq.'cluster') then
        read (sphlin,'(8x,i5,32x,i6)') iclust, nsph
c only include the clusters the user wants
        if (nsph.lt.minsiz) go to 500
c following line omits cluster 0
        if (iclust.eq.0) go to 500
        write (6, 1002) 'cluster # ',iclust,' contains ',nsph,' spheres'
 1002   format (a,i5,a,i5,a)
        write (2,'(a5,1x,i8)') 'MODEL', iclust
c not bug! TER at beginning of model needed to prevent connecting hetatms
        write (2,'(a3)') 'TER'
        do 60 i=1,nsph 
          read (1, 1000, end=500) sphlin
          read (sphlin, 1003) isph,x,y,z,rad
 1003     format (i5,3f10.5,f8.3)
          write (2, 1004) 'HETATM',isph,' C   SPH',isph,x,y,z
 1004     format (A6,I5,1X,A8,2X,I4,4X,3F8.3)
          write (ciclust, '(i5)') iclust
          write (cisph, '(i5)') isph
c         write (3, 1005) '#',cimod(start(cimod):5),'.',
c    &    ciclust(start(ciclust):5),':',cisph(start(cisph):5),
c    &    '.het@c',rad
c1005     format ('	',7a,'	',f8.3)
          write (3, 1006) '#.',
     &    ciclust(start(ciclust):5),':',cisph(start(cisph):5),
     &    '.het@c',rad
 1006     format ('	',5a,'	',f8.3)
   60   continue
        write (2,'(a6)') 'ENDMDL'
        go to 50
      else
        go to 50
      endif
  500 continue
      close (1)
      close (2)
      close (3)
      end
c---------------------------------------------------------------------
      integer function start(string)
c
      character*5 string
c
      do 10 i=1,5
        start=i
        if(string(i:i).ne.' ')go to 20
   10 continue
   20 continue
      return
      end
c------------------------------------------------------------------------
