c
c
c     ###################################################
c     ##  COPYRIGHT (C)  1994  by  Jay William Ponder  ##
c     ###################################################
c
c     ################################################################
c     ##                                                            ##
c     ##  program profile  --  align sequence to Eisenberg profile  ##
c     ##                                                            ##
c     ################################################################
c
c
c     "profile" constructs a 3-D structural profile for a known protein
c     structure using information about secondary structure, buried
c     side chain area, and fractional polar area; other sequences can
c     be aligned to the profile and their alignment significance tested
c
c     literature reference:
c
c     J. U. Bowie, R. Luthy and D. Eisenberg, "A Method to Identify
c     Protein Sequences That Fold into a Known Three-Dimensional
c     Structure", Science, 253, 164-170 (1991)
c
c
      program profile
      implicit none
      include 'sizes.i'
      include 'aminos.i'
      include 'inform.i'
      include 'iounit.i'
      include 'sequen.i'
      integer maxenvir
      parameter (maxenvir=18)
      integer i,j,k,isum,iseq
      integer leng,start,stop
      integer nline,label,first,last
      integer nres,offset,score
      integer freeunit,trimtext
      integer open(maxres),widen(maxres)
      integer table(maxamino,maxenvir)
      integer template(maxamino,maxres)
      integer penalty(3),match(maxres)
      real*8 mean,stddev,zscore
      real*8 area,fraction,cutoff(5)
      real*8 bury(maxres),fpolar(maxres)
      character*1 letter,resnam(maxres)
      character*3 res3(maxres)
      character*3 class(maxenvir),envir(maxres)
      character*3 struct,fold(maxres)
      character*60 sumfile,seqfile
      character*80 record
      logical exist
      data class  /'B1a', 'B1b', 'B1 ', 'B2a', 'B2b', 'B2 ',
     &             'B3a', 'B3b', 'B3 ', 'P1a', 'P1b', 'P1 ',
     &             'P2a', 'P2b', 'P2 ', 'Ea ', 'Eb ', 'E  '/
c
c
c     set up original values for print flag and other variables
c
      call initial
c
c     get a summary file with residue environment assignments
c
      exist = .false.
      dowhile (.not. exist)
         write (iout,10)
   10    format (/,' Enter the Name of the SLEUTH Summary File :  ',$)
         read (input,20)  sumfile
   20    format (a60)
         call suffix (sumfile,'dat')
         call version (sumfile,'old')
         leng = trimtext(sumfile)
         if (leng .ne. 0)  inquire (file=sumfile(1:leng),exist=exist)
      end do
      isum = freeunit ()
      open (unit=isum,file=sumfile(1:leng),status='old')
c
c     get the number of amino acid residues in the protein
c
   30 continue
      read (isum,40)  record
   40 format (a80)
      if (record(1:23) .eq. ' Protein Data Bank File') then
         read (isum,50)  nres
   50    format (///,32x,i12)
      else
         goto 30
      end if
c
c     get the amino acid sequence of the protein
c
   60 continue
      read (isum,70)  record
   70 format (a80)
      if (record(2:20) .eq. 'Amino Acid Sequence') then
         read (isum,80)
   80    format (//)
         do i = 1, ((nres-1)/10)+1
            read (isum,90)  (res3(j),j=10*i-9,min(10*i,nres))
   90       format (11x,9(a3,2x),a3)
         end do
      else
         goto 60
      end if
c
c     translate 3-letter to 1-letter amino acid code
c
      do i = 1, nres
         resnam(i) = ' '
         do j = 1, maxamino
            if (res3(i) .eq. amino(j)) then
               resnam(i) = amino1(j)
               goto 100
            end if
         end do
  100    continue
      end do
c
c     get the Kabsch-Sander secondary structure assignments
c
  110 continue
      read (isum,120)  record
  120 format (a80)
      if (record(1:34) .eq. ' Kabsch-Sander Secondary Structure') then
         read (isum,130)
  130    format (//)
         do i = 1, ((nres-1)/10)+1
            read (isum,140)  (fold(j),j=10*i-9,min(10*i,nres))
  140       format (11x,9(a3,2x),a3)
         end do
      else
         goto 110
      end if
c
c     get the buried surface area and fractional polar area
c
  150 continue
      read (isum,160)  record
  160 format (a80)
      if (record(1:20) .eq. ' Buried Surface Area') then
         read (isum,170)
  170    format (//)
         do i = 1, nres
            read (isum,180)  bury(i),fpolar(i)
  180       format (26x,f6.1,20x,f6.1)
         end do
      else
         goto 150
      end if
      close (unit=isum)
c
c     get the residue-to-environment definitions and scoring table
c
      call gettable (table,cutoff,offset)
c
c     set the residue environment for each position in the sequence
c
      do i = 1, nres
         struct = fold(i)
         call upcase (struct)
         area = bury(i)
         fraction = fpolar(i)
         if (area .lt. cutoff(1)) then
            if (struct .eq. ' H ') then
               envir(i) = 'Ea '
            else if (struct .eq. ' E ') then
               envir(i) = 'Eb '
            else
               envir(i) = 'E  '
            end if
         else if (area .lt. cutoff(2)) then
            if (fraction .lt. cutoff(3)) then
               if (struct .eq. ' H ') then
                  envir(i) = 'P1a'
               else if (struct .eq. ' E ') then
                  envir(i) = 'P1b'
               else
                  envir(i) = 'P1 '
               end if
            else
               if (struct .eq. ' H ') then
                  envir(i) = 'P2a'
               else if (struct .eq. ' E ') then
                  envir(i) = 'P2b'
               else
                  envir(i) = 'P2 '
               end if
            end if
         else
            if (fraction .lt. cutoff(4)) then
               if (struct .eq. ' H ') then
                  envir(i) = 'B1a'
               else if (struct .eq. ' E ') then
                  envir(i) = 'B1b'
               else
                  envir(i) = 'B1 '
               end if
            else if (fraction .lt. cutoff(5)) then
               if (struct .eq. ' H ') then
                  envir(i) = 'B2a'
               else if (struct .eq. ' E ') then
                  envir(i) = 'B2b'
               else
                  envir(i) = 'B2 '
               end if
            else
               if (struct .eq. ' H ') then
                  envir(i) = 'B3a'
               else if (struct .eq. ' E ') then
                  envir(i) = 'B3b'
               else
                  envir(i) = 'B3 '
               end if
            end if
         end if
      end do
c
c     print out the Eisenberg 3-D environment of each residue
c
      write (iout,190)
  190 format (/,' Residue Environments for 3-D Compatibility Search :',
     &        //,12x,'1',4x,'2',4x,'3',4x,'4',4x,'5',
     &            4x,'6',4x,'7',4x,'8',4x,'9',3x,'10',/)
      nline = (nres-1)/10 + 1
      do i = 1, nline
         label = 10 * (i-1)
         first = label + 1
         last = min(label+10,nres)
         write (iout,200)  label,(envir(k),k=first,last)
  200    format (i6,3x,10(2x,a3))
      end do
c
c     build the profile template for the input structure
c
      do i = 1, nres
         do j = 1, maxenvir
            if (envir(i) .eq. class(j)) then
               do k = 1, maxamino
                  template(k,i) = table(k,j)
               end do
            end if
         end do
      end do
c
c     get a protein sequence to be aligned on the template
c
  210 continue
      exist = .false.
      dowhile (.not. exist)
         write (iout,220)
  220    format (/,' Enter the Name of a Protein Sequence File :  ',$)
         read (input,230)  seqfile
  230    format (a60)
         call suffix (seqfile,'seq')
         call version (seqfile,'old')
         leng = trimtext(seqfile)
         if (leng .ne. 0)  inquire (file=seqfile(1:leng),exist=exist)
      end do
      iseq = freeunit ()
      open (unit=iseq,file=seqfile(1:leng),status='old')
      call readseq (iseq)
      close (unit=iseq)
c
c     set the penalties for opening or extending alignment gaps
c
      do i = 1, 3
         penalty(i) = 0
      end do
      write (iout,240)
  240 format (/,' Enter Gap Penalties (Open,Extend,Break',
     &           ' [CR]=450/5/0) :  ',$)
      read (input,250)  (penalty(i),i=1,3)
  250 format (3i10)
      if (penalty(1) .eq. 0) then
         do i = 1, nres
            open(i) = -450
            widen(i) = -5
         end do
      else if (penalty(3) .ne. 0) then
         do i = 1, nres
            struct = fold(i)
            call upcase (struct)
            if (struct.eq.' H ' .or. struct.eq.' E ') then
               open(i) = -abs(penalty(3))
               widen(i) = -abs(penalty(3))
            else
               open(i) = -abs(penalty(1))
               widen(i) = -abs(penalty(2))
            end if
         end do
      else
         do i = 1, nres
            open(i) = -abs(penalty(1))
            widen(i) = -abs(penalty(2))
         end do
      end if
c
c     perform alignment via Needleman-Wunsch or Smith-Waterman
c
      verbose = .true.
      write (iout,260)
  260 format (/,' Choose Needleman-Wunsch or Smith-Waterman',
     &           ' Alignment (N or [S]) :  ',$)
      read (input,270)  letter
  270 format (a1)
      call upcase (letter)
      if (letter .eq. 'N') then
         call wunsch (nres,template,open,widen,score,match)
      else
         call smith (nres,template,open,widen,score,match)
      end if
c
c     compute alignment statistics over many random sequences
c
      verbose = .false.
      call stats (nres,template,open,widen,mean,stddev)
c
c     print the final alignment score and estimated Z-score
c
      zscore = (dble(score) - mean) / stddev
      write (iout,280)  score,zscore
  280 format (/,' Final Optimal Alignment Score :',i10,
     &           9x,'Z-Score :',f8.2,/)
c
c     print the optimal alignment skipping gaps at the ends
c
      start = 0
      stop = 0
      do i = 1, nres
         if (match(i) .ne. 0) then
            if (start .eq. 0) then
               start = max(1,i-3)
               j = match(i)
            end if
            stop = min(i+3,nres)
         end if
      end do
      do i = start, stop
         if (match(i) .eq. 0) then
            write (iout,290)  i,resnam(i),envir(i)
  290       format (i8,3x,a1,3x,a3)
         else if (match(i) .eq. j) then
            write (iout,300)  i,resnam(i),envir(i),j,amino1(seqtyp(j)),
     &                        template(seqtyp(j),i)-offset
  300       format (i8,3x,a1,3x,a3,4x,i8,3x,a1,4x,i8)
            j = j + 1
         else if (match(i) .gt. j) then
            do k = j, match(i)-1
               write (iout,310)  k,amino1(seqtyp(k))
  310          format (22x,i8,3x,a1)
            end do
            j = match(i)
            write (iout,320)  i,resnam(i),envir(i),j,amino1(seqtyp(j)),
     &                        template(seqtyp(j),i)-offset
  320       format (i8,3x,a1,3x,a3,4x,i8,3x,a1,4x,i8)
            j = j + 1
         end if
      end do
c
c     decide whether to process another probe sequence file
c
      write (iout,330)
  330 format (/,' Process another Sequence File (Yes or [No]) :  ',$)
      read (input,340)  letter
  340 format (a1)
      call upcase (letter)
      if (letter .eq. 'Y')  goto 210
      end
c
c
c     ###################################################
c     ##  COPYRIGHT (C)  1994  by  Jay William Ponder  ##
c     ###################################################
c
c     ################################################################
c     ##                                                            ##
c     ##  subroutine gettable  --  read environment scoring matrix  ##
c     ##                                                            ##
c     ################################################################
c
c
c     "gettable" reads the scoring table that translates between
c     the 18 residue environment classes and 20 amino acid types
c
c
      subroutine gettable (table,cutoff,offset)
      implicit none
      include 'sizes.i'
      include 'iounit.i'
      integer maxenvir
      parameter (maxenvir=18)
      integer i,j,itab,offset
      integer leng,freeunit,trimtext
      integer table(maxamino,maxenvir)
      real*8 cutoff(5)
      character*60 tabfile
      logical exist,use_offset
c
c
c     initialize the scoring table and offset value
c
      use_offset = .false.
      offset = 0
      do i = 1, maxenvir
         do j = 1, maxamino
            table(j,i) = 0
         end do
      end do
c
c     get file containing environment definitions and scoring table
c
      exist = .false.
      dowhile (.not. exist)
         write (iout,10)
   10    format (/,' Enter the Name of the Scoring Table',
     &              ' [profile.score] :  ',$)
         read (input,20)  tabfile
   20    format (a60)
         if (tabfile(1:1) .eq. ' ')  tabfile = 'profile'
         leng = trimtext(tabfile)
         if (leng .ne. 0)  inquire (file=tabfile(1:leng),exist=exist)
         if (.not. exist) then
            tabfile = tabfile(1:leng)//'.score'
            leng = trimtext(tabfile)
            inquire (file=tabfile(1:leng),exist=exist)
         end if
      end do
      itab = freeunit ()
      open (unit=itab,file=tabfile(1:leng),status='old')
c
c     read the environment definitions from the scoring file
c
      read (itab,30)
   30 format (/)
      read (itab,40)  (cutoff(i),i=1,5)
   40 format (6x,f6.1,9x,f6.1,10x,f6.1,10x,f6.1,10x,f6.1)
c
c     read the residue-environment values from the scoring file
c
      read (itab,50)
   50 format (//)
      do i = 1, maxenvir
         read (itab,60)  (table(j,i),j=1,20)
   60    format (5x,20i5)
      end do
      close (unit=itab)
c
c     add a constant to the table to make all values positive
c
      if (use_offset) then
         do i = 1, maxenvir
            do j = 1, maxamino
               if (table(j,i) .lt. offset)  offset = table(j,i)
            end do
         end do
         offset = -offset
         do i = 1, maxenvir
            do j = 1, maxamino
               table(j,i) = table(j,i) + offset
            end do
         end do
      end if
      return
      end
c
c
c     ###################################################
c     ##  COPYRIGHT (C)  1994  by  Jay William Ponder  ##
c     ###################################################
c
c     #################################################################
c     ##                                                             ##
c     ##  subroutine stats  --  get mean alignment score and stddev  ##
c     ##                                                             ##
c     #################################################################
c
c
c     "stats" computes the mean alignment score and standard
c     deviation for a set of random or shuffled sequences
c
c
      subroutine stats (nres,template,open,widen,mean,stddev)
      implicit none
      include 'sizes.i'
      include 'iounit.i'
      include 'sequen.i'
      integer i,j,nres,ntrial,score,aatype
      integer oldseq(maxres),key(maxres)
      integer template(maxamino,maxres)
      integer open(maxres),widen(maxres)
      integer match(maxres)
      real*8 mean,stddev,sum,sum2
      real*8 random,list(maxres)
      logical shuffle
c
c
c     store the original protein sequence
c
      do i = 1, nseq
         oldseq(i) = seqtyp(i)
      end do
c
c     set the number of sequences and mode of randomization
c
      shuffle = .false.
      ntrial = 0
      write (iout,10)
   10 format (/,' Enter Number of Random Sequences to Generate',
     &           ' ([CR]=100) :  ',$)
      read (input,20)  ntrial
   20 format (i10)
      if (ntrial .eq. 0)  ntrial = 100
c
c     make random or shuffled sequences and align to the template
c
      sum = 0.0d0
      sum2 = 0.0d0
      do i = 1, ntrial
         if (shuffle) then
            do j = 1, nseq
               list(j) = random()
            end do
            call sort2 (nseq,list,key)
            do j = 1, nseq
               seqtyp(j) = oldseq(key(j))
            end do
         else
            do j = 1, nseq
               aatype = int(20.0d0*random()) + 1
               aatype = max(1,min(20,aatype))
               seqtyp(j) = aatype
            end do
         end if
c        call wunsch (nres,template,open,widen,score,match)
         call smith (nres,template,open,widen,score,match)
         sum = sum + dble(score)
         sum2 = sum2 + dble(score)**2
      end do
c
c     compute the report the mean and standard deviation
c
      mean = sum / dble(ntrial)
      stddev = sqrt((dble(ntrial)*sum2-sum**2)
     &                 /dble(ntrial*(max(1,ntrial-1))))
      if (shuffle) then
         write (iout,30)  nseq,mean,stddev
   30    format (/,' Shuffle Alignment Score for',i5,' Residues :',
     &              f11.2,'  +/-',f8.2)
      else
         write (iout,40)  nseq,mean,stddev
   40    format (/,' Random Alignment Score for',i5,' Residues :',
     &              f12.2,'  +/-',f8.2)
      end if
c
c     restore the original protein sequence
c
      do i = 1, nseq
         seqtyp(i) = oldseq(i)
      end do
      return
      end
c
c
c     ###################################################
c     ##  COPYRIGHT (C)  1994  by  Jay William Ponder  ##
c     ###################################################
c
c     ##################################################################
c     ##                                                              ##
c     ##  subroutine wunsch  --  Needleman-Wunsch sequence alignment  ##
c     ##                                                              ##
c     ##################################################################
c
c
c     "wunsch" finds the optimal global alignment between structural
c     environments and amino acid sequences via a dynamic programming
c     algorithm due to Needleman and Wunsch
c
c     literature reference:
c
c     S. B. Needleman and C. D. Wunsch, "A General Method Applicable
c     to the Search for Similarities in the Amino Acid Sequence of
c     Two Proteins", J. Mol. Biol., 48, 443-453 (1970)
c
c
      subroutine wunsch (nres,template,open,widen,score,match)
      implicit none
      include 'sizes.i'
      include 'inform.i'
      include 'iounit.i'
      include 'sequen.i'
      integer maxmask
      parameter (maxmask=1000)
      integer i,j,k,nres,score
      integer ires,itmp,itrace
      integer jseq,jtmp,jtrace
      integer gap,prior,value
      integer template(maxamino,maxres)
      integer open(maxres),widen(maxres)
      integer mask(0:maxmask,0:maxmask)
      integer trace(2,maxmask,maxmask)
      integer match(maxres)
c
c
c     build alignment mask between template and probe proteins
c
      do i = 1, nres
         do j = 1, nseq
            mask(j,i) = template(seqtyp(j),i)
         end do
      end do
      do i = 1, nres
         mask(0,i) = 0
      end do
      do j = 1, nseq
         mask(j,0) = 0
      end do
c
c     write out the complete alignment mask before summation
c
      if (verbose .and. nseq.le.30) then
         write (iout,10)
   10    format (/,' Alignment Mask before Summation :')
         write (iout,20)  (i,i=1,nseq)
   20    format (/,10x,<nseq>i5,/)
         do i = 1, nres
            write (iout,30)  i,(mask(j,i),j=1,nseq)
   30       format (i5,5x,<nseq>i5)
         end do
      end if
c
c     complete the mask by the successive summation procedure
c
      do i = 1, nres
         do j = 1, nseq
            prior = mask(j-1,i-1)
            jtrace = j - 1
            itrace = i - 1
            do k = 1, j-2
               gap = open(i-1) + (j-1-k)*widen(i-1)
               value = mask(k,i-1) + gap
               if (value .gt. prior) then
                  prior = value
                  jtrace = k
                  itrace = i - 1
               end if
            end do
            do k = 1, i-2
               gap = open(i-1) + (i-1-k)*widen(i-1)
               value = mask(j-1,k) + gap
               if (value .gt. prior) then
                  prior = value
                  jtrace = j - 1
                  itrace = k
               end if
            end do
            mask(j,i) = mask(j,i) + prior
            trace(1,j,i) = jtrace
            trace(2,j,i) = itrace
         end do
      end do
c
c     write out the complete alignment mask after summation
c
      if (verbose .and. nseq.le.30) then
         write (iout,40)
   40    format (/,' Alignment Mask following Summation :')
         write (iout,50)  (i,i=1,nseq)
   50    format (/,10x,<nseq>i5,/)
         do i = 1, nres
            write (iout,60)  i,(mask(j,i),j=1,nseq)
   60       format (i5,5x,<nseq>i5)
         end do
      end if
c
c     find the end of the best alignment in last row or column
c
      score = -1000000
      do i = 1, nres
         if (mask(nseq,i) .gt. score) then
            score = mask(nseq,i)
            ires = i
            jseq = nseq
         end if
      end do
      do j = 1, nseq
         if (mask(j,nres) .gt. score) then
            score = mask(j,nres)
            ires = nres
            jseq = j
         end if
      end do
c
c     backtrace to locate the remainder of the optimal alignment
c
      do i = 1, nres
         match(i) = 0
      end do
      match(ires) = jseq
      dowhile (ires .ne. 0)
         match(ires) = jseq
         jtmp = trace(1,jseq,ires)
         itmp = trace(2,jseq,ires)
         jseq = jtmp
         ires = itmp
      end do
      return
      end
c
c
c     ###################################################
c     ##  COPYRIGHT (C)  1994  by  Jay William Ponder  ##
c     ###################################################
c
c     ###############################################################
c     ##                                                           ##
c     ##  subroutine smith  --  Smith-Waterman sequence alignment  ##
c     ##                                                           ##
c     ###############################################################
c
c
c     "smith" finds the optimal local alignment between structural
c     environments and amino acid sequences via a dynamic programming
c     algorithm due to Smith and Waterman
c
c     literature reference:
c
c     T. F. Smith and M. S. Waterman, "Identification of Common
c     Molecular Subsequences", J. Mol. Biol., 147, 195-197 (1981)
c
c     R. Luthy and D. Eisenberg, Chapter 2 in "Sequence Analysis
c     Primer", edited by M. Gribskov and J. Devereux, Stockton
c     Press, New York, 1991
c
c
      subroutine smith (nres,template,open,widen,score,match)
      implicit none
      include 'sizes.i'
      include 'inform.i'
      include 'iounit.i'
      include 'sequen.i'
      integer maxmask
      parameter (maxmask=1000)
      integer i,j,ires,jseq,nres,score
      integer diag,horiz,vert,large
      integer template(maxamino,maxres)
      integer open(maxres),widen(maxres)
      integer mask(0:maxmask,0:maxmask)
      integer match(maxres)
      character*1 trace(0:maxmask,0:maxmask)
      logical gap(0:maxmask,0:maxmask)
c
c
c     build alignment mask between template and probe proteins
c
      do i = 1, nres
         do j = 1, nseq
            mask(j,i) = template(seqtyp(j),i)
         end do
      end do
      do i = 1, nres
         mask(0,i) = 0
         gap(j,i) = .false.
      end do
      do j = 1, nseq
         mask(j,0) = 0
         gap(j,i) = .false.
      end do
c
c     write out the complete alignment mask before transformation
c
      if (verbose .and. nseq.le.30) then
         write (iout,10)
   10    format (/,' Alignment Mask before Transformation :')
         write (iout,20)  (i,i=1,nseq)
   20    format (/,10x,<nseq>i5,/)
         do i = 1, nres
            write (iout,30)  i,(mask(j,i),j=1,nseq)
   30       format (i5,5x,<nseq>i5)
         end do
      end if
c
c     complete the mask by the transformation procedure
c
      do i = 1, nres
         do j = 1, nseq
            diag = mask(j-1,i-1) + mask(j,i)
            if (gap(j-1,i)) then
               vert = mask(j-1,i) + widen(i)
            else
               vert = mask(j-1,i) + open(i)
            end if
            if (gap(j,i-1)) then
               horiz = mask(j,i-1) + widen(i)
            else
               horiz = mask(j,i-1) + open(i)
            end if
            large = max(diag,vert,horiz,0)
            mask(j,i) = large
            if (diag .eq. large) then
               trace(j,i) = 'D'
               gap(j,i) = .false.
            else if (vert .eq. large) then
               trace(j,i) = 'V'
               gap(j,i) = .true.
            else if (horiz .eq. large) then
               trace(j,i) = 'H'
               gap(j,i) = .true.
            else
               trace(j,i) = 'E'
               gap(j,i) = .true.
            end if
         end do
      end do
c
c     write out the complete alignment mask after transformation
c
      if (verbose .and. nseq.le.30) then
         write (iout,40)
   40    format (/,' Alignment Mask following Transformation :')
         write (iout,50)  (i,i=1,nseq)
   50    format (/,10x,<nseq>i5,/)
         do i = 1, nres
            write (iout,60)  i,(mask(j,i),j=1,nseq)
   60       format (i5,5x,<nseq>i5)
         end do
      end if
c
c     find the end of the best alignment at biggest element
c
      score = -1000000
      do i = 1, nres
         do j = 1, nseq
            if (mask(j,i) .gt. score) then
               score = mask(j,i)
               ires = i
               jseq = j
            end if
         end do
      end do
c
c     backtrace to locate the remainder of the optimal alignment
c
      do i = 1, nres
         match(i) = 0
      end do
      dowhile (ires.ne.0 .and. jseq.ne.0)
         if (trace(jseq,ires) .eq. 'D') then
            match(ires) = jseq
            jseq = jseq - 1
            ires = ires - 1
         else if (trace(jseq,ires) .eq. 'V') then
            jseq = jseq - 1
         else if (trace(jseq,ires) .eq. 'H') then
            ires = ires - 1
         else if (trace(jseq,ires) .eq. 'E') then
            ires = 0
         end if
      end do
      return
      end
