c
c
c     ###################################################
c     ##  COPYRIGHT (C)  1994  by  Jay William Ponder  ##
c     ###################################################
c
c     ################################################################
c     ##                                                            ##
c     ##  program blaster  --  get mutation list from BLAST output  ##
c     ##                                                            ##
c     ################################################################
c
c
      program blaster
      implicit none
      include 'iounit.i'
      integer i,iblast,imut
      integer leng,length
      integer first,last
      integer start,site
      integer freeunit,trimtext
      character*1 letter,seqi,muti
      character*8 keyword
      character*60 filename
      character*60 blastfile
      character*60 mutantfile
      character*60 seq,mutant
      logical exist
c
c
c     open the BLAST output file containing sequence alignments
c
      call initial
      blastfile = 'blast.out'
   10 continue
      inquire (file=blastfile,exist=exist)
      if (exist) then
         iblast = freeunit ()
         open (unit=iblast,file=blastfile,status='old')
      else
         write (iout,20)
   20    format (/,' Enter the Name of the BLAST Output File :  ',$)
         read (input,30)  blastfile
   30    format (a60)
         goto 10
      end if
c
c     remove any extension from the filename
c
      leng = trimtext (blastfile)
      last = leng
      do i = 1, leng
         letter = blastfile(i:i)
         if (letter .eq. '/')  last = leng
         if (letter .eq. ']')  last = leng
         if (letter .eq. ':')  last = leng
         if (letter .eq. '.')  last = i - 1
      end do
      leng = min(leng,last)
c
c     remove any directory prefix from the filename
c
      first = 1
      do i = 1, leng
         letter = blastfile(i:i)
         if (letter .eq. '/')  first = i + 1
         if (letter .eq. ']')  first = i + 1
         if (letter .eq. ':')  first = i + 1
      end do
      length = leng - first + 1
      filename(1:length) = blastfile(first:leng)
c
c     open the output file for the list of mutation sites
c
      mutantfile = filename(1:length)//'.mut'
      call version (mutantfile,'new')
      imut = freeunit ()
      open (unit=imut,file=mutantfile,status='new')
c
c     read in the summary of each protein structure
c
      dowhile (.true.)
   40    continue
         read (iblast,50,err=40,end=100)  keyword,start,seq
   50    format (a8,i5,1x,a60)
         if (keyword .ne. '>Query: ')  goto 40
         read (iblast,60)
   60    format ()
         read (iblast,70)  mutant
   70    format (14x,a60)
         do i = 1, 60
            site = start + i - 1
            seqi = seq(i:i)
            if (seqi.ge.'A' .and. seqi.le.'Z') then
               muti = mutant(i:i)
               if (seqi .ne. muti) then
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c
c     ideal generic case where the BLAST search string is the
c     same as the residue sequence in the SLEUTH abstract file
c
                  write (imut,80)  site,muti
   80             format (i6,7x,a1)
c
c     Lipscomb's porcine fructose-bis-phosphatase structure
c
c                 if (site.ge.7 .and. site.le.56) then
c                    site = site - 6
c                    write (imut,80)  site,muti
c  80                format (i6,7x,a1)
c                 else if (site.ge.73 .and. site.le.336) then
c                    site = site - 22
c                    write (imut,80)  site,muti
c                 end if
c
c     Bone's human monophosphatase structure
c
c                 if (site.ge.5 .and. site.le.276) then
c                    site = site - 4
c                    write (imut,80)  site,muti
c  80                format (i6,7x,a1)
c                 end if
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
               end if
            else
               goto 90
            end if
         end do
   90    continue
      end do
  100 continue
      close (unit=iblast)
      close (unit=imut)
      end
