C   SIP (similarity investigation program)
C   copyright Rodger Staden 
C  23-7-92 Added facility to list exact matches
C  16-7-92 changed equivalencing of arrays and upped maxseq to 100000 and
C          maxcom to 50000. Also allowed quickscan  and identities plot to 
C          ignore main diagonal.
C   Jan 25th 1990 changes to allow switches between protein and dna sequences
C   8-2-90 Changed to give xhair access when alignm chosen with D
C   16-3-90 removed escape after bpause
C    5-4-90 Changed getdev to redir and all that implies (see redir)
C   12-06-90 Changed all occurrences of lh to lf
C   21-06-90 Added calls to showfu
C    6-07-90 If no seq read forced retry (used to exit)
C    9-7-90 renamed menu routine
C   11-12-90 Changed handling of pir libraries. New string and paramter
C   25-4-91  Fixed bug in cfsq 
C            statment
C     13-04-91 removed initial CLEARV
C  3-7-91 set namlen = 60
C  25-2-92 changed call to rdseq
C         AUTHOR RODGER STADEN MRC LMB, CAMBRIDGE
C   DIMENSION VARIABLES USED:
C             MXSPAN= MAXIMUM SPAN
C             MAXSEQ= MAXIMUM SEQUENCE LENGTH
C             MAXCOM= MAXIMUM LENGTH OF SEQUENCE THAT CAN BE COMPARED IN ONE
C             MXCOMP= MAXIMUM SPAN + MAXCOM
C             MXSPSP= MAXIMUM SPAN + MAXIMUM SEQUENCE - 1
C             MSPO2= MAXIMUM SPAN / 2
C             MSPO21= MAXIMUM SPAN / 2 +1
C             IDIMV = ACTUAL SEQUENCE LENGTH (VERTICAL)
C             IDIMVA= ACTIVE SEQUENCE
C             IDIMVP= ACTIVE SEQUENCE LENGTH + SPAN
C             IDPOLY= DIMENSION OF POLYNOMIAL, PERC, SCORE ARRAYS
C             ISXMAX= MAXIMUM SCREEN DIMENSION IN X
C   POINTERS
C             ISV1, ISV2, IDIMVA = START, END, LENGTH OF ACTIVE REGION VERTICAL
C             ISH1, ISH2, IDIMHA = START, END, LENGTH OF ACTIVE REGION HORIZ
C             IDIMVP=IDIMVA+SPAN
C             IDIMHP=IDIMHA+SPAN
C             IW1, IW2, IDIML1 = START, END, LENGTH FOR WRITING BOTH SEQ (VERT)
C             IW3, IW4, IDIML  = START, END, LENGTH FOR WRITING BOTH SEQ (HORIZ)
C             IHX,IHY XHAIR POSITION IN SEQ UNITS
C             IWX3, IWX4, = IHX+-SPAN/2 FOR SAVING XHAIR POSITION
C             IWX1, IWX2, = IHY+-SPAN/2 FOR SAVING XHAIR POSITION
C
C
      SUBROUTINE FMAIN()
      PARAMETER (MAXDEV=9)
      PARAMETER (NAMLEN = 60)
      CHARACTER*(NAMLEN) HELPF,POINTF,FILOUT,FILEH,FILEV,FILEM,FILEP
      CHARACTER*(NAMLEN) FILNAM
      CHARACTER*(NAMLEN) LIBLF
      CHARACTER*(NAMLEN*2) CURFIL
      PARAMETER (MAXPRM = 4)
      CHARACTER PROMPT(2)*(MAXPRM)
      INTEGER BOTOPT,TOPOPT,DEVNOS(MAXDEV)
      PARAMETER (MXSPAN=401,
     +           MAXSEQ=100000,
     +           MXSPSP=MXSPAN-1+MAXSEQ,
     +           MAXCOM=50000,
     +           MXCOMP=MAXCOM+MXSPAN-1,
     +           MSPO2=MXSPAN/2,
     +           MSPO21=MSPO2+1,
     +           IDPOLY=4000,
     +           BOTOPT=0,
     +           TOPOPT=34,
     +           MAXOPT=34,
     +           MINMEN=-4,
     +           HELPF='SIPHELP',
     +           POINTF='SIPHPNT',
     +           FILEM='SIPMARG',
     +           FILEP='PROTMAT',
     +           LIBLF='SEQUENCELIBRARIES')
C   WARNING TO RS: MAX CONTINUATION LINES USED HERE!
      PARAMETER (LCONSM = 75)
C  LCONSM SET FOR MAX PROTEIN WORD LENGTH OF 3 (I.E. 3*(IDM-1))
      PARAMETER (IDMM = 26)
      PARAMETER (MAXWRD = 16384)
      INTEGER SEQV(MXCOMP),SEQH(MXCOMP),OPT
      CHARACTER SEQVC(MAXSEQ),SEQHC(MAXSEQ)
      CHARACTER NOROO,NOROE,VORH
      CHARACTER CHRSET(IDMM),CHRDNA(5)
      INTEGER LINEB(MXCOMP),LINEC(MXCOMP),LINEE(MXCOMP)
      INTEGER MATRIX(IDMM,IDMM),MATDNA(25)
      REAL PERC(IDPOLY),POLYA(IDPOLY),POLYB(IDPOLY)
      INTEGER SCORE(IDPOLY)
      INTEGER HELPS(BOTOPT:TOPOPT),HELPE(BOTOPT:TOPOPT)
      INTEGER MARG1(1),MARG2(1)
C   NOTE THE MARG ARRAYS ARE FOR COMPATIBILITY WITH GETMRG
      INTEGER WORDP(MAXWRD),CONSTS(0:LCONSM)
C   ALIGNMENT PARAMETERS AND VARIABLES
       INTEGER STKREC
       PARAMETER (MAXSA = 5000,
     +            STKREC = 11,
     +            MAXSTK = MAXSA*3*STKREC)
C   THE STACK SIZE ASSUMES A MAXIMUM SEQUENCE SIZE OF MAXSA
C   AND THAT THE ESTIMATE OF 2.5 * SEQUENCE LENGTH FOR THE STACK
C   IS CORRECT. 
      INTEGER STACK(0:MAXSTK)
      INTEGER WTS(0:IDMM,0:IDMM)
      CHARACTER SEQ1A(MAXSA),SEQ2A(MAXSA)
      INTEGER CC(0:MAXSA+1),DD(0:MAXSA+1)
      INTEGER RR(0:MAXSA+1),SS(0:MAXSA+1)
      INTEGER SOP(0:2*MAXSA)
      INTEGER MAIND(IDMM),SEQS(MAXCOM),HIST(-MAXCOM:MAXCOM)
      INTEGER POSN(MAXCOM)
      PARAMETER (LENEQ = MAXCOM*4 + MXCOMP*3 + MAXWRD + 1)
      INTEGER EQ1(LENEQ)
      EQUIVALENCE (EQ1,HIST)
      EQUIVALENCE (EQ1(2+2*MAXCOM),SEQS)
      EQUIVALENCE (EQ1(2+3*MAXCOM),POSN)
      EQUIVALENCE (EQ1(2+4*MAXCOM),LINEB)
      EQUIVALENCE (EQ1(2+4*MAXCOM+MXCOMP),LINEC)
      EQUIVALENCE (EQ1(2+4*MAXCOM+2*MXCOMP),LINEE)
      EQUIVALENCE (EQ1(2+4*MAXCOM+3*MXCOMP),WORDP)
      EQUIVALENCE (EQ1,STACK)
      EQUIVALENCE (CHRSET,CHRDNA)
C      EQUIVALENCE (STACK,HIST)
C      EQUIVALENCE (STACK(1+2*MAXCOM),SEQS)
C      EQUIVALENCE (STACK(1+3*MAXCOM),POSN)
C      EQUIVALENCE (STACK(1+4*MAXCOM),LINEB)
C      EQUIVALENCE (STACK(1+4*MAXCOM+MXCOMP),LINEC)
C      EQUIVALENCE (STACK(1+4*MAXCOM+2*MXCOMP),LINEE)
C      EQUIVALENCE (STACK(1+4*MAXCOM+3*MXCOMP),WORDP)
C  NOTE 1+4*MAXCOM+3*MXCOMP MUST BE < MAXSA*STKREC*3
      EQUIVALENCE (PERC,CC)
      EQUIVALENCE (POLYA,DD)
      EQUIVALENCE (POLYB,RR)
      EQUIVALENCE (SCORE,SS)
C  NOTE MAXSA SHOULD BE SIMILAR TO IDPOLY
      EXTERNAL NORP
      DATA MATDNA/
     +1,0,0,0,0,
     +0,1,0,0,0,
     +0,0,1,0,0,
     +0,0,0,1,0,
     +0,0,0,0,0/
      DATA CHRDNA/'T','C','A','G','-'/
C   Initialise help
      CALL INTHLP('sip', TOPOPT)
      CALL UNITNO(KBIN,KBOUT,DEVNOS,MAXDEV)
C      I = 1+4*MAXCOM+3*MXCOMP
C      J = MAXSA*STKREC*3
C      IF(I.GT.J) THEN
C        WRITE(KBOUT,*)'Equivalencing of arrays suspect'
C        GO TO 9999
C      END IF
      IFORNO = 0
      LIBNO = 2
      IGORT = 0
C   OPEN UNIT DEVNOS(3) FOR PLOTTING
      CALL OPENGR(DEVNOS(3))
      WRITE(KBOUT,1000)
1000  FORMAT(
     +' SIP (Similarity investigation program) V5.0 July 1992',/,
     +' Author: Rodger Staden')
      FILEV = ' '
      FILEH = ' '
C   GET HELP POINTERS
      CALL SETHLP(HELPS,HELPE,BOTOPT,TOPOPT,POINTF,DEVNOS(4),KBOUT)
      CALL INITGR(KBIN,KBOUT,HELPS(0),HELPE(0),HELPF,DEVNOS(4))
      CALL GETMRG(
     +ISXMAX,ISYMAX,MARGL,MARGR,MARG1,MARG2,1,DEVNOS(1),FILEM)
      LCONST = LCONSM
      JSAME = 0
      MARKI = 0
      RNSD = 3.
      IGAPH = 10
      IGAPG = 10
      MARGB=MARG1(1)
      MARGT=MARG2(1)
      IHX=0
      IHY=0
      IOK=0
      IDMIN = 0
      LENGTH = 11
      LB=(LENGTH+1)/2
      LF=LENGTH/2
      IDEV=KBOUT
      MOPT=0
      IDIMH = 0
      IDIMV = 0
20    CONTINUE
C 
C      CALL SHOWFU(KBOUT,'Select horizontal sequence')
      FILEH = ' '
      IDEVLL = DEVNOS(5)
      IDEVEN = DEVNOS(6)
      IDEVAN = DEVNOS(7)
      IDEVDL = DEVNOS(8)
      IDEVLF = DEVNOS(9)
      CALL RDSEQ(
     +SEQHC,MAXSEQ,IDIMH,ISH1,ISH2,ISTARH,IENDH,IDIMHA,IDIMBH,
     +DEVNOS(1),FILNAM,KBIN,KBOUT,
     +HELPS(3),HELPE(3),HELPF,DEVNOS(4),IDEV,IFORNO,
     +IDEVLL,IDEVEN,IDEVAN,IDEVDL,
     +IDEVLF,LIBNO,LIBLF,HIST(-MAXCOM),MAXCOM,IOK)
      FILEH = FILNAM
      CURFIL = FILEV(1:1+INDEX(FILEV,' '))//FILEH
      CALL SHOWFI(KBOUT,CURFIL)
      IF((IOK.NE.0).OR.(IDIMH.LT.LENGTH)) GO TO 20
      CALL REDEFD(IDIMH,ISH1,ISH2,MAXCOM,IDIMHA,ISTARH,IENDH,IDIMBH)
      IDM = NORP(SEQHC,IDIMBH)
      IF(IDMIN.NE.IDM)THEN
        IF(IDM.EQ.26) THEN
C   READ IN THE SCORE MATRIX (USUALLY MDM78)
          CALL GETMAT(DEVNOS(1),FILEP,MATRIX,IDM,CHRSET,KBOUT,IOK)
          IF(IOK.NE.0) GO TO 9999
          L17 = 3
          MINPER = 2
        ELSE
          L17 = 7
          MINPER = 8
          CALL FILLI(MATRIX,676,0)
          CALL COPYI(MATDNA,MATRIX,25)
        END IF
        CALL INITLU(IDM)
C   NEED TO KNOW LARGEST SCORE IN MATRIX FOR STATS
        MATMAX=0
        DO 7 I=1,IDM
        DO 7 J=1,IDM
          IF(MATMAX.LT.MATRIX(I,J))MATMAX=MATRIX(I,J)
7       CONTINUE
        MATMAX=MATMAX+1
C   SET AVERAGE SCORE FOR DEFAULTS
        AVSCOR=0.75
        IF(IDM.EQ.26)AVSCOR=AVSCOR*16.
C   GET INITIAL SCORE
        MINPRO = INT(AVSCOR*LENGTH)
      END IF
      IDMIN = IDM
C   GIVE COMPOSITION AS A CHECK
      IF(IDIMBH.GT.0)THEN
        IF(IDM.EQ.5)CALL BCOMP(SEQHC,IDIMBH,
     +ISH1,ISH2,ISTARH,KBOUT)
        IF(IDM.EQ.26)CALL MWCALC(SEQHC,
     +IDIMBH,ISH1-ISTARH+1,ISH2-ISTARH+1,KBOUT,KBOUT)
      END IF
C   set pointers to sequence
********
C  MAXSEQ = THE DIMENSION OF THE RAM BUFFER SEQ
C  IDIMH  = THE ACTUAL SEQUENCE LENGTH (AND THEREFORE THE NUMBER OF ELEMENTS 
C           IN THE DISK BUFFER)
C  ISTARH = THE SEQUENCE NUMBER OF THE CHARACTER OCCUPYING SEQHC(1)
C  ISH1   = THE SEQUENCE NUMBER OF THE FIRST CHARACTER IN THE ACTIVE REGION
C  ISH2   = THE SEQUENCE NUMBER OF THE LAST CHARACTER IN THE ACTIVE REGION
C  IDIMHA = ISH2-ISH11+1 I.E. THE NUMBER OF ELEMENTS IN THE ACTIVE REGION
C  IENDH  = THE SEQUENCE NUMBER OF THE LAST ELEMENT OF SEQ
C  IDIMBH = IEND-ISTART+1 I.E. THE NUMBER OF ELEMENTS IN THE RAM BUFFER
********
      IF((IOK.NE.0).OR.(IDIMHA.LT.LENGTH)) GO TO 9999
      IDIMHP=IDIMHA+LENGTH
      CALL CONNUM(SEQHC(ISH1-ISTARH+1),SEQH(MSPO21),IDIMHA)
      CALL FILEDG(SEQHC,IDIMBH,ISH1,ISH2,SEQH,MXCOMP,LENGTH,
     +CHRSET,IDM,MSPO2,ISTARH,IENDH)
      NOROO='N'
      NOROE='N'
      IH1 = ISH1
      I = MIN(MAXSA,IDIMHA) - 1
      IH2 = IH1 + I
      IF(OPT.EQ.3)GO TO 30
C
21    CONTINUE
C      CALL SHOWFU(KBOUT,'Select vertical sequence')
      FILEV = ' '
      CALL RDSEQ(
     +SEQVC,MAXSEQ,IDIMV,ISV1,ISV2,ISTARV,IENDV,IDIMVA,IDIMBV,
     +DEVNOS(1),FILNAM,KBIN,KBOUT,
     +HELPS(3),HELPE(3),HELPF,DEVNOS(4),IDEV,IFORNO,
     +IDEVLL,IDEVEN,IDEVAN,IDEVDL,
     +IDEVLF,LIBNO,LIBLF,HIST(-MAXCOM),MAXCOM,IOK)
      FILEV = FILNAM
      CURFIL = FILEV(1:1+INDEX(FILEV,' '))//FILEH
      CALL SHOWFI(KBOUT,CURFIL)
      IF((IOK.NE.0).OR.(IDIMV.LT.LENGTH)) GO TO 21
      CALL REDEFD(IDIMV,ISV1,ISV2,MAXCOM,IDIMVA,ISTARV,IENDV,IDIMBV)
C   GIVE COMPOSITION AS A CHECK
      IDM = NORP(SEQVC,IDIMBV)
      IF(IDM.NE.IDMIN) CALL INITLU(IDM)
      IF(IDIMBV.GT.0)THEN
        IF(IDM.EQ.5)CALL BCOMP(SEQVC,IDIMBV,
     +ISV1,ISV2,ISTARV,KBOUT)
        IF(IDM.EQ.26)CALL MWCALC(SEQVC,
     +IDIMBV,ISV1-ISTARV+1,ISV2-ISTARV+1,KBOUT,KBOUT)
      END IF
C   set pointers to sequence
********
C  MAXSEQ = THE DIMENSION OF THE RAM BUFFER SEQ
C  IDIMV  = THE ACTUAL SEQUENCE LENGTH (AND THEREFORE THE NUMBER OF ELEMENTS 
C           IN THE DISK BUFFER)
C  ISTARV = THE SEQUENCE NUMBER OF THE CHARACTER OCCUPYING SEQHC(1)
C  ISV1   = THE SEQUENCE NUMBER OF THE FIRST CHARACTER IN THE ACTIVE REGION
C  ISV2   = THE SEQUENCE NUMBER OF THE LAST CHARACTER IN THE ACTIVE REGION
C  IDIMVA = ISV2-ISV1+1 I.E. THE NUMBER OF ELEMENTS IN THE ACTIVE REGION
C  IENDV  = THE SEQUENCE NUMBER OF THE LAST ELEMENT OF SEQ
C  IDIMBV = IEND-ISTART+1 I.E. THE NUMBER OF ELEMENTS IN THE RAM BUFFER
********
C   change region
C      IF(IDIMV.GT.0)THEN
C      CALL REDEFA
C     +(SEQVC,IDIMV,ISV1,ISV2,MAXCOM,IDIMVA,ISTARV,IENDV,IDIMBV,
C     +DEVNOS(6),KBIN,KBOUT,
C     +IOK,SEQW,LREC,NREC,
C     +HELPS(4),HELPE(4),HELPF,DEVNOS(4),MACTYP,MACSHT,MACLNG)
C      END IF
*****************
      IF((IOK.NE.0).OR.(IDIMVA.LT.LENGTH)) GO TO 9999
**********************
      IDIMVP=IDIMVA+LENGTH
C   DECODE CHARACTERS INTO INTEGERS IN ACTIVE REGION
      CALL CONNUM(SEQVC(ISV1-ISTARV+1),SEQV(MSPO21),IDIMVA)
      CALL FILEDG(SEQVC,IDIMBV,ISV1,ISV2,SEQV,MXCOMP,LENGTH,
     +CHRSET,IDM,MSPO2,ISTARV,IENDV)
      NOROO='N'
      NOROE='N'
      IV1 = ISV1
      I = MIN(MAXSA,IDIMVA) - 1
      IV2 = IV1 + I
      IF(OPT.EQ.3)GO TO 30
1008  FORMAT(I6)
      WRITE(KBOUT,*)' '
      CALL DSTAT(FILEH,ISH1,ISH2,FILEV,ISV1,ISV2,
     +  LENGTH,MINPRO,MINPER,KBOUT,JSAME,MARKI)
30    CONTINUE
C
C  MENU AND OPTION SELECTION  <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
C
C
      CALL BPAUSE(KBIN,KBOUT,IOK)
C      IF(IOK.EQ.1) GO TO 9999
C      CALL SHOWFU(KBOUT,' ')
      CALL MENU(OPT,KOPT,MOPT,MAXOPT,MINMEN,KBIN,KBOUT,
     +HELPS(0),HELPE(0),HELPF,DEVNOS(4))
1010  FORMAT(I6)
      IF(OPT.EQ.18) THEN
C        CALL SHOWFU(KBOUT,'Plot proportional matches')
        IF(KOPT.EQ.1) THEN
        CALL DP20(LENGTH,MXSPAN,IDIMVA,IDIMHA,IDIMVP,IDIMHP,
     +  NOROO,NOROE,MAXSEQ,
     +  SEQVC,IDIMBV,ISV1,ISV2,SEQV,MXCOMP,
     +  CHRSET,IDM,MSPO2,ISTARV,IENDV,
     +  SEQHC,IDIMBH,ISH1,ISH2,SEQH,
     +  ISTARH,IENDH,LB,LF,
     +  KBIN,KBOUT,HELPS(OPT),HELPE(OPT),HELPF,DEVNOS(4),IOK)
        IF(IOK.NE.0) GO TO 30
        CALL DP21(MATMAX,AVSCOR,LENGTH,MINPRO,
     +  KBIN,KBOUT,HELPS(OPT),HELPE(OPT),HELPF,DEVNOS(4),IOK)
        IF(IOK.NE.0) GO TO 30
        END IF
        CALL CLEARV
        CALL VECTOM
        CALL FRAME(MARGL,MARGR,MARGB,MARGT,ISXMAX,ISYMAX)
        ISAME = 0
        IF((JSAME.EQ.1).AND.(ISH1.EQ.ISV1)) ISAME = 1
        IF(MARKI.EQ.1)THEN
          CALL DIAPRI(SEQV(MSPO21-LB),SEQV(LF+MSPO21),
     +    IDIMVP,SEQH(MSPO21-LB),SEQH(LF+MSPO21),
     +    IDIMHP,MATRIX,IDM,LINEB,LINEC,LINEE,LENGTH,MINPRO,
     +    MARGL,MARGR,MARGB,MARGT,ISXMAX,ISYMAX,KBOUT,ISAME)
        ELSE
          CALL DIAPRO(SEQV(MSPO21-LB),SEQV(LF+MSPO21),
     +    IDIMVP,SEQH(MSPO21-LB),SEQH(LF+MSPO21),
     +    IDIMHP,MATRIX,IDM,LINEB,LINEC,LINEE,LENGTH,MINPRO,
     +    MARGL,MARGR,MARGB,MARGT,ISXMAX,ISYMAX,KBOUT,ISAME)
        END IF
        CALL VT100M
        GO TO 30
      END IF
      IF(OPT.EQ.17) THEN
C        CALL SHOWFU(KBOUT,'Plot identical matches')
        IDO = 1
        IF(KOPT.EQ.1) THEN
          CALL DP22(IDM,MINPER,
     +    KBIN,KBOUT,HELPS(OPT),HELPE(OPT),HELPF,DEVNOS(4),IOK)
          IF(IOK.NE.0) GO TO 30
          PROMPT(1) = 'Plot'
          PROMPT(2) = 'List'
          CALL RADION('Select output mode',PROMPT,2,IDO,
     +    HELPS(OPT),HELPE(OPT),HELPF,DEVNOS(4),KBIN,KBOUT)
          IF (IDO.LT.1) GO TO 30
        END IF
        L17A = MIN(MINPER,L17)
        IDE = (IDM-1)**L17A
        ISAME = 0
        IF((JSAME.EQ.1).AND.(ISH1.EQ.ISV1)) ISAME = 1
        IF (IDO.EQ.1) THEN
          CALL CLEARV
          CALL VECTOM
          CALL FRAME(MARGL,MARGR,MARGB,MARGT,ISXMAX,ISYMAX)
          CALL CFSQ(SEQV(MSPO21),IDIMVA,SEQH(MSPO21),
     +    IDIMHA,POSN,WORDP,IDE,IDM,CONSTS,LCONST,L17A,MINPER,
     +    MARGL,MARGR,MARGB,MARGT,ISXMAX,ISYMAX,KBOUT,ISAME)
          CALL VT100M
        ELSE
          CALL CFSQL(SEQV(MSPO21),IDIMVA,SEQH(MSPO21),
     +    IDIMHA,POSN,WORDP,IDE,IDM,CONSTS,LCONST,L17A,MINPER,
     +    IDEV,KBOUT,ISAME,SEQVC(ISV1),HIST,MAXCOM)
        END IF
        GO TO 30
      END IF
C
C   RESET PROPORTIONAL SCORE
C
      IF(OPT.EQ.21)THEN
C        CALL SHOWFU(KBOUT,'Reset proportional score')
        CALL DP21(MATMAX,AVSCOR,LENGTH,MINPRO,
     +  KBIN,KBOUT,HELPS(OPT),HELPE(OPT),HELPF,DEVNOS(4),IOK)
        GO TO 30
      END IF
C
C   RESET IDENTITIES SCORE
C
      IF(OPT.EQ.22)THEN
C        CALL SHOWFU(KBOUT,'Reset identities score')
        CALL DP22(IDM,MINPER,
     +  KBIN,KBOUT,HELPS(OPT),HELPE(OPT),HELPF,DEVNOS(4),IOK)
        GO TO 30
      END IF
C
C   RESET SPAN (LENGTH)
C
      IF(OPT.EQ.20)THEN
C        CALL SHOWFU(KBOUT,'Reset span length')
        CALL DP20(LENGTH,MXSPAN,IDIMVA,IDIMHA,IDIMVP,IDIMHP,
     +  NOROO,NOROE,MAXSEQ,
     +  SEQVC,IDIMBV,ISV1,ISV2,SEQV,MXCOMP,
     +  CHRSET,IDM,MSPO2,ISTARV,IENDV,
     +  SEQHC,IDIMBH,ISH1,ISH2,SEQH,
     +  ISTARH,IENDH,LB,LF,
     +  KBIN,KBOUT,HELPS(OPT),HELPE(OPT),HELPF,DEVNOS(4),IOK)
        GO TO 30
      END IF
C
C   RESET HORIZONTAL REGION
C
      IF(OPT.EQ.4)THEN
C        CALL SHOWFU(KBOUT,'Reset active region')
        CALL QVORH(VORH,HELPS(OPT),HELPE(OPT),HELPF,DEVNOS(4),
     +  KBIN,KBOUT,FILEH,FILEV)
        IF(VORH.EQ.'H')THEN
          CALL GTREG(KBIN,KBOUT,ISTARH,IENDH,ISH1,ISH2,
     +    'Define active region',
     +    HELPS(OPT),HELPE(OPT),HELPF,DEVNOS(4),IOK)
       CALL REDEFD(IDIMH,ISH1,ISH2,MAXCOM,IDIMHA,ISTARH,IENDH,IDIMBH)
C      CALL REDEFA
C     +(SEQHC,IDIMH,ISH1,ISH2,MAXCOM,IDIMHA,ISTARH,IENDH,IDIMBH,
C     +DEVNOS(5),KBIN,KBOUT,
C     +IOK,SEQW,LREC,NREC,
C     +HELPS(4),HELPE(4),HELPF,DEVNOS(4),MACTYP,MACSHT,MACLNG)
          IDIMHP=IDIMHA+LENGTH
          CALL CONNUM(SEQHC(ISH1-ISTARH+1),SEQH(MSPO21),IDIMHA)
          CALL FILEDG(SEQHC,IDIMBH,ISH1,ISH2,SEQH,MXCOMP,LENGTH,
     +    CHRSET,IDM,MSPO2,ISTARH,IENDH)
          NOROO='N'
          NOROE='N'
          IH1 = ISH1
          I = MIN(MAXSA,IDIMHA) - 1
          IH2 = IH1 + I
          GO TO 30
        END IF
        IF(VORH.EQ.'V')THEN
C
C  RESET VERTICAL REGION
C
          CALL GTREG(KBIN,KBOUT,ISTARV,IENDV,ISV1,ISV2,
     +    'Define active region',
     +    HELPS(OPT),HELPE(OPT),HELPF,DEVNOS(4),IOK)
          CALL REDEFD(IDIMV,ISV1,ISV2,MAXCOM,IDIMVA,
     +    ISTARV,IENDV,IDIMBV)
C      CALL REDEFA
C     +(SEQVC,IDIMV,ISV1,ISV2,MAXCOM,IDIMVA,ISTARV,IENDV,IDIMBV,
C     +DEVNOS(6),KBIN,KBOUT,
C     +IOK,SEQW,LREC,NREC,
C     +HELPS(4),HELPE(4),HELPF,DEVNOS(4),MACTYP,MACSHT,MACLNG)
          IDIMVP=IDIMVA+LENGTH
C   DECODE CHARACTERS INTO INTEGERS IN ACTIVE REGION
          CALL CONNUM(SEQVC(ISV1-ISTARV+1),SEQV(MSPO21),IDIMVA)
          CALL FILEDG(SEQVC,IDIMBV,ISV1,ISV2,SEQV,MXCOMP,LENGTH,
     +    CHRSET,IDM,MSPO2,ISTARV,IENDV)
          NOROO='N'
          NOROE='N'
          I = MIN(MAXSA,IDIMVA) - 1
          IV1 = ISV1
          IV2 = IV1 + I
          GO TO 30
        END IF
      END IF
C
C   WRITE ALIGNED SEQUENCES
C
      IF(OPT.EQ.5)THEN
C        CALL SHOWFU(KBOUT,'List sequences')
        CALL WRITAL(SEQHC,IDIMBH,ISTARH,IENDH,
     +  SEQVC,IDIMBV,ISTARV,IENDV,IDEV,KBIN,KBOUT,
     +  HELPS(OPT),HELPE(OPT),HELPF,DEVNOS(4))
        GO TO 30
      END IF
C
C   XHAIRS
C
      IF(OPT.EQ.13)THEN
C        CALL SHOWFU(KBOUT,'Use crosshair')
        XMIN=ISH1
        XMAX=ISH2
        YMIN=ISV1
        YMAX=ISV2
        CALL CLEARV
        CALL XHAIRN(XMAX,XMIN,YMAX,YMIN,MARGL,MARGR,MARGB,MARGT,
     +  ISXMAX,ISYMAX,IHX,IHY,N,KBOUT,
     +  SEQHC,ISTARH,IDIMBH,SEQVC,ISTARV,IDIMBV,2)
        GO TO 30
      END IF
C
C   RULER
C
      IF(OPT.EQ.12)THEN
C        CALL SHOWFU(KBOUT,'Display ruler')
        WRITE(KBOUT,*)' Horizontal scale'
        CALL RULER(ISH1,ISH2,MARGL,MARGR,MARGB,MARGT,
     +  ISXMAX,ISYMAX,KBIN,KBOUT,1,
     +  HELPS(OPT),HELPE(OPT),HELPF,DEVNOS(4))
        WRITE(KBOUT,*)' Vertical scale'
        CALL RULER(ISV1,ISV2,MARGL,MARGR,MARGB,MARGT,
     +  ISXMAX,ISYMAX,KBIN,KBOUT,2,
     +  HELPS(OPT),HELPE(OPT),HELPF,DEVNOS(4))
        GO TO 30
      END IF
C
C   CLEAR GRAPHICS
C
      IF(OPT.EQ.10)THEN
        CALL CLEARG
        GO TO 30
      END IF
C
C   KLEAR TEXT
C
      IF(OPT.EQ.11)THEN
        CALL CLEARV
        GO TO 30
      END IF
C
C   CALCULATE EXPECTED SCORES
C
      IF(OPT.EQ.23)THEN
C        CALL SHOWFU(KBOUT,'Calculate expected proportional scores')
        IF(KOPT.EQ.1) THEN
        CALL DP20(LENGTH,MXSPAN,IDIMVA,IDIMHA,IDIMVP,IDIMHP,
     +  NOROO,NOROE,MAXSEQ,
     +  SEQVC,IDIMBV,ISV1,ISV2,SEQV,MXCOMP,
     +  CHRSET,IDM,MSPO2,ISTARV,IENDV,
     +  SEQHC,IDIMBH,ISH1,ISH2,SEQH,
     +  ISTARH,IENDH,LB,LF,
     +  KBIN,KBOUT,HELPS(OPT),HELPE(OPT),HELPF,DEVNOS(4),IOK)
        IF(IOK.NE.0) GO TO 30
        CALL DP21(MATMAX,AVSCOR,LENGTH,MINPRO,
     +  KBIN,KBOUT,HELPS(OPT),HELPE(OPT),HELPF,DEVNOS(4),IOK)
        IF(IOK.NE.0) GO TO 30
        END IF
C   ARRAYS LARGE ENOUGH?
        ITEMP=MATMAX*LENGTH
        IF(ITEMP.LE.IDPOLY)THEN
          CALL DIAEXP(SEQVC,IDIMBV,SEQHC,IDIMBH,
     +    MATRIX,IDM,LENGTH,MATMAX,POLYA,POLYB,IDPOLY,KBIN,KBOUT,
     +    IDEV,CHRSET,NOROE,HELPS(OPT),HELPE(OPT),HELPF,DEVNOS(4))
          GO TO 30
        END IF
C   OVER RANGE
        CALL ERROM(KBOUT,
     +'Polynomial out of range with this span and matrix')
        GO TO 30
      END IF
C
C   CALCULATE OBSERVED SCORES (NO PLOTING)
C
      IF(OPT.EQ.24)THEN
C        CALL SHOWFU(KBOUT,'Calculate observed proportional scores')
        IF(KOPT.EQ.1) THEN
        CALL DP20(LENGTH,MXSPAN,IDIMVA,IDIMHA,IDIMVP,IDIMHP,
     +  NOROO,NOROE,MAXSEQ,
     +  SEQVC,IDIMBV,ISV1,ISV2,SEQV,MXCOMP,
     +  CHRSET,IDM,MSPO2,ISTARV,IENDV,
     +  SEQHC,IDIMBH,ISH1,ISH2,SEQH,
     +  ISTARH,IENDH,LB,LF,
     +  KBIN,KBOUT,HELPS(OPT),HELPE(OPT),HELPF,DEVNOS(4),IOK)
        IF(IOK.NE.0) GO TO 30
        CALL DP21(MATMAX,AVSCOR,LENGTH,MINPRO,
     +  KBIN,KBOUT,HELPS(OPT),HELPE(OPT),HELPF,DEVNOS(4),IOK)
        IF(IOK.NE.0) GO TO 30
        END IF
        ISAME = 0
        IF((JSAME.EQ.1).AND.(ISH1.EQ.ISV1)) ISAME = 1
        CALL DIAOBS(SEQV(MSPO21-LB),SEQV(LF+MSPO21),IDIMVP,
     +  SEQH(MSPO21-LB),SEQH(LF+MSPO21),IDIMHP,MATRIX,IDM,
     +  LINEB,LINEC,LINEE,LENGTH,NOROO,PERC,SCORE,IDPOLY,KBIN,KBOUT,
     +  IDEV,HELPS(OPT),HELPE(OPT),HELPF,DEVNOS(4),ISAME)
        GO TO 30
      END IF
C
C   SHOW CURRENT PARAMETERS
C
      IF(OPT.EQ.25)THEN
        CALL CLEARV
C        CALL SHOWFU(KBOUT,'Show settings')
        CALL DSTAT(FILEH,ISH1,ISH2,FILEV,ISV1,ISV2,
     +  LENGTH,MINPRO,MINPER,KBOUT,JSAME,MARKI)
        GO TO 30
      END IF
C
C   QUICK SCAN
C
      IF(OPT.EQ.26)THEN
C        CALL SHOWFU(KBOUT,'Quick scan')
        IF(KOPT.EQ.1) THEN
        CALL DP22(IDM,MINPER,
     +  KBIN,KBOUT,HELPS(OPT),HELPE(OPT),HELPF,DEVNOS(4),IOK)
        IF(IOK.NE.0) GO TO 30
        CALL DP20(LENGTH,MXSPAN,IDIMVA,IDIMHA,IDIMVP,IDIMHP,
     +  NOROO,NOROE,MAXSEQ,
     +  SEQVC,IDIMBV,ISV1,ISV2,SEQV,MXCOMP,
     +  CHRSET,IDM,MSPO2,ISTARV,IENDV,
     +  SEQHC,IDIMBH,ISH1,ISH2,SEQH,
     +  ISTARH,IENDH,LB,LF,
     +  KBIN,KBOUT,HELPS(OPT),HELPE(OPT),HELPF,DEVNOS(4),IOK)
        IF(IOK.NE.0) GO TO 30
        CALL DP21(MATMAX,AVSCOR,LENGTH,MINPRO,
     +  KBIN,KBOUT,HELPS(OPT),HELPE(OPT),HELPF,DEVNOS(4),IOK)
        IF(IOK.NE.0) GO TO 30
        CALL DP33(RNSD,
     +  KBIN,KBOUT,HELPS(OPT),HELPE(OPT),HELPF,DEVNOS(4),IOK)
        IF(IOK.NE.0) GO TO 30
        END IF
        CALL CLEARV
        CALL VECTOM
        CALL FRAME(MARGL,MARGR,MARGB,MARGT,ISXMAX,ISYMAX)
        L17A = MIN(MINPER,L17)
        IDE = (IDM-1)**L17A
        CALL VT100M
        ISAME = 0
        IF((JSAME.EQ.1).AND.(ISH1.EQ.ISV1)) ISAME = 1
        CALL QICKS(SEQV(MSPO21),IDIMVA,POSN,WORDP,IDE,SEQH(MSPO21),
     +  IDIMHA,CONSTS,L17A,IDM,LCONST,HIST,
     +  MAXCOM,MATRIX,SEQS,
     +  MAIND,MARGL,MARGR,MARGB,MARGT,ISXMAX,ISYMAX,
     +  LENGTH,MINPRO,KBOUT,RNSD,ISAME)
        CALL VT100M
C        CALL SHOBOX(SEQVC,IDIMBV,SEQHC,IDIMBH,IWX1,IWX2,IWX3,IWX4,
C     +  HELPS(OPT),HELPE(OPT),
C     +  HELPF,DEVNOS(4),KBIN,KBOUT,ISTARH,ISTARV,IENDH,IENDV)
        GO TO 30
      END IF
C
C  EDIT
C
      IF(OPT.EQ.9)THEN
C        CALL SHOWFU(KBOUT,'Edit sequences')
        CALL ALIGN(SEQVC,IDIMV,SEQHC,IDIMH,MAXSEQ,KBIN,KBOUT,
     +  HELPS(OPT),HELPE(OPT),HELPF,DEVNOS(4),ISTARV,IENDV,
     +  ISTARH,IENDH,FILEH,FILEV)
        WRITE(KBOUT,1003)
1003  FORMAT(
     +' Warning: if you have just edited either sequence you must',/,
     +' redefine the active region before comparing the sequences')
C   MAY HAVE CHANGED SEQUENCE SO REDO INTEGER VERSION
C   DECODE CHARACTERS INTO INTEGERS IN ACTIVE REGION
C      CALL CONNUM(SEQVC(ISV1-ISTARV+1),SEQV(MSPO21),IDIMVA,CHRSET,IDM)
C      CALL FILEDG(SEQVC,IDIMVA,ISV1,ISV2,SEQV,MXCOMP,LENGTH,
C     +CHRSET,IDM,MSPO2,ISTARV,IENDV)
C   DECODE CHARACTERS INTO INTEGERS IN ACTIVE REGION
C      CALL CONNUM(SEQHC(ISH1-ISTARH+1),SEQH(MSPO21),IDIMHA,CHRSET,IDM)
C      CALL FILEDG(SEQHC,IDIMHA,ISH1,ISH2,SEQH,MXCOMP,LENGTH,
C     +CHRSET,IDM,MSPO2,ISTARH,IENDH)
C      NOROO='N'
C      NOROE='N'
      GO TO 30
      END IF
C
C   WRITE HORIZONTAL SEQUENCE TO DISK
C
      IF(OPT.EQ.8)THEN
C        CALL SHOWFU(KBOUT,'Save active sequence')
        CALL QVORH(VORH,HELPS(OPT),HELPE(OPT),HELPF,DEVNOS(4),
     +  KBIN,KBOUT,FILEH,FILEV)
        IF(VORH.EQ.'H')THEN
          CALL WRTACT(DEVNOS(1),FILOUT,KBIN,KBOUT,
     +    SEQHC(ISH1),IDIMHA,HELPS(OPT),HELPE(OPT),HELPF,DEVNOS(4))
          GO TO 30
        END IF
C
C  WRITE VERTICAL SEQUENCE TO DISK
C
        IF(VORH.EQ.'V')THEN
          CALL WRTACT(DEVNOS(1),FILOUT,KBIN,KBOUT,
     +    SEQVC(ISV1),IDIMVA,HELPS(OPT),HELPE(OPT),HELPF,DEVNOS(4))
          GO TO 30
         END IF
      END IF
C
C   STOP
C
      IF(OPT.EQ.2) GO TO 9999
C
C  DRAW A /
C
      IF(OPT.EQ.27)THEN
C        CALL SHOWFU(KBOUT,'Draw diagonal line')
        CALL VECTOM
        CALL DIALIN(ISXMAX,ISYMAX,MARGL,MARGR,MARGB,MARGT)
        GO TO 30
      END IF
C
C   GIVE HELP
C
      IF(OPT.EQ.1)THEN
C        CALL SHOWFU(KBOUT,'Help')
      CALL HELP(HELPS,HELPE,BOTOPT,TOPOPT,HELPF,DEVNOS(4),KBIN,KBOUT)
      GO TO 30
      END IF
C
C   READ NEW SEQUENCE
C
      IF(OPT.EQ.3)THEN
C        CALL SHOWFU(KBOUT,'Read new sequence')
        CALL QVORH(VORH,HELPS(OPT),HELPE(OPT),HELPF,DEVNOS(4),
     +  KBIN,KBOUT,FILEH,FILEV)
        IF(VORH.EQ.'H')GO TO 20
        IF(VORH.EQ.'V')GO TO 21
      END IF
C
C  CHANGE MARGINS
C
      IF(OPT.EQ.14)THEN
C        CALL SHOWFU(KBOUT,'Change margins')
        CALL MARGC1(ISXMAX,ISYMAX,MARGL,MARGR,MARGB,MARGT,
     +  HELPS(OPT),HELPE(OPT),HELPF,DEVNOS(4),KBIN,KBOUT)
        GO TO 30
      END IF
C
C  LIST MATCHING SPANS
C
      IF(OPT.EQ.19)THEN
C        CALL SHOWFU(KBOUT,'List matching spans')
        IF(KOPT.EQ.1) THEN
        CALL DP20(LENGTH,MXSPAN,IDIMVA,IDIMHA,IDIMVP,IDIMHP,
     +  NOROO,NOROE,MAXSEQ,
     +  SEQVC,IDIMBV,ISV1,ISV2,SEQV,MXCOMP,
     +  CHRSET,IDM,MSPO2,ISTARV,IENDV,
     +  SEQHC,IDIMBH,ISH1,ISH2,SEQH,
     +  ISTARH,IENDH,LB,LF,
     +  KBIN,KBOUT,HELPS(OPT),HELPE(OPT),HELPF,DEVNOS(4),IOK)
        IF(IOK.NE.0) GO TO 30
        CALL DP21(MATMAX,AVSCOR,LENGTH,MINPRO,
     +  KBIN,KBOUT,HELPS(OPT),HELPE(OPT),HELPF,DEVNOS(4),IOK)
        IF(IOK.NE.0) GO TO 30
        END IF
        ISAME = 0
        IF((JSAME.EQ.1).AND.(ISH1.EQ.ISV1)) ISAME = 1
        CALL DIAPRW(SEQV(MSPO21-LB),SEQV(LF+MSPO21),
     +  IDIMVP,SEQH(MSPO21-LB),SEQH(LF+MSPO21),
     +  IDIMHP,MATRIX,IDM,LINEB,LINEC,LINEE,
     +  LENGTH,MINPRO,SEQVC,IDIMBV,SEQHC,IDIMBH,
     +  KBOUT,IDEV,ISV1,ISH1,ISTARH,ISTARV,ISAME)
        GO TO 30
      END IF
C
C  LABEL
C
      IF(OPT.EQ.15)THEN
C        CALL SHOWFU(KBOUT,'Label a diagram')
        CALL LABLER(KBIN,KBOUT,ISXMAX,ISYMAX,
     +  HELPS(OPT),HELPE(OPT),HELPF,DEVNOS(4))
      END IF
C
C  type text
C
      IF(OPT.EQ.6)THEN
C        CALL SHOWFU(KBOUT,'Page through a text file')
         CALL TTEXT(DEVNOS(1),FILNAM,KBIN,KBOUT,
     +   HELPS(OPT),HELPE(OPT),HELPF,DEVNOS(4))
      END IF
C
C  DIRECT OUTPUT TO DISK
C
      IF(OPT.EQ.7)THEN
C        CALL SHOWFU(KBOUT,'Redirect output')
        CALL REDIR(IDEV,DEVNOS(2),DEVNOS(3),IGORT,FILNAM,KBIN,KBOUT,
     +  HELPS(OPT),HELPE(OPT),HELPF,DEVNOS(4),KOPT)
        GO TO 30
      END IF
C
C  ALIGN
C
      IF(OPT.EQ.28)THEN
C        CALL SHOWFU(KBOUT,'Align the sequences')
        IF(KOPT.EQ.1) THEN
          WRITE(KBOUT,1019)
          CALL BPAUSE(KBIN,KBOUT,IOK)
          IF(IOK.NE.0) GO TO 30
          XMIN=ISH1
          XMAX=ISH2
          YMIN=ISV1
          YMAX=ISV2
          CALL CLEARV
          CALL XHAIRN(XMAX,XMIN,YMAX,YMIN,MARGL,MARGR,MARGB,MARGT,
     +    ISXMAX,ISYMAX,IHX,IHY,N,KBOUT,
     +    SEQHC,ISTARH,IDIMBH,SEQVC,ISTARV,IDIMBV,2)
          IH1 = MAX(1,IHX)
          IV1 = MAX(1,IHY)
          CALL XHAIRN(XMAX,XMIN,YMAX,YMIN,MARGL,MARGR,MARGB,MARGT,
     +    ISXMAX,ISYMAX,IHX,IHY,N,KBOUT,
     +    SEQHC,ISTARH,IDIMBH,SEQVC,ISTARV,IDIMBV,2)
          IH2 = MIN(IHX,IDIMH)
          IV2 = MIN(IHY,IDIMV)
        END IF
        LENV = IV2 - IV1 + 1
        LENH = IH2 - IH1 + 1
        IF((LENV.LT.10).OR.(LENH.LT.10)
     +  .OR.(LENV.GT.MAXSA).OR.(LENH.GT.MAXSA)) THEN
          WRITE(KBOUT,1019)
1019      FORMAT(/,
     +' Define the region to align using the cross-hair.',/,
     +' First identify the bottom left position and exit',/,
     +' the cross-hair routine. Then the top right.')
          GO TO 30
        END IF
        IF(KOPT.EQ.1) THEN
        CALL GETGAP(KBIN,KBOUT,IGAPG,IGAPH,
     +  HELPS(OPT),HELPE(OPT),HELPF,DEVNOS(4),IDM,IOK)
        IF(IOK.NE.0) GO TO 30
        END IF
        WRITE(KBOUT,*)'Aligning region',IV1,' to',IV2
        WRITE(KBOUT,*)'    with region',IH1,' to',IH2
        CALL PAMDIS(MATRIX,WTS,IDM)
        CALL ALIGNM(SEQV(MSPO21+IV1-ISV1),SEQH(MSPO21+IH1-ISH1),
     +  LENV,LENH,CC,DD,RR,SS,SOP,MAXSA,IV1,IH1,IDIMBV,IDIMBH,
     +  MAXSEQ,
     +  WTS,IDM,ISCORE,IGAPG,IGAPH,KBIN,KBOUT,IDEV,SEQVC,
     +  SEQHC,SEQ1A,SEQ2A,
     +  HELPS(OPT),HELPE(OPT),HELPF,DEVNOS(4),KEEP,STACK,MAXSTK,STKREC,
     +  IOK)
        NOROE = 'N'
        NOROO = 'N'
        IF(IOK.NE.0)GO TO 30
C  IDIMH  = THE ACTUAL SEQUENCE LENGTH (AND THEREFORE THE NUMBER OF ELEMENTS 
C           IN THE DISK BUFFER)
C  ISTARH = THE SEQUENCE NUMBER OF THE CHARACTER OCCUPYING SEQHC(1)
C  ISH1   = THE SEQUENCE NUMBER OF THE FIRST CHARACTER IN THE ACTIVE REGION
C  ISH2   = THE SEQUENCE NUMBER OF THE LAST CHARACTER IN THE ACTIVE REGION
C  IDIMHA = ISH2-ISH11+1 I.E. THE NUMBER OF ELEMENTS IN THE ACTIVE REGION
C  IENDH  = THE SEQUENCE NUMBER OF THE LAST ELEMENT OF SEQ
C  IDIMBH = IEND-ISTART+1 I.E. THE NUMBER OF ELEMENTS IN THE RAM BUFFER
      IF(KEEP.EQ.0) THEN
        WRITE(KBOUT,1004)
1004    FORMAT(' Warning: active region extended rightwards')
C  NEXT FOUR LINES FOR NONDISKBUFFERED VERSION !!!!!!!!!!!
        IDIMV = MAX(IDIMV,IDIMBV)
        IDIMBV = IDIMV
        IDIMH = MAX(IDIMH,IDIMBH)
        IDIMBH = IDIMH
        IENDV = IDIMV
        IENDH = IDIMH
        ISV2  = MIN(IENDV,ISV1+MAXCOM-1)
        ISH2  = MIN(IENDH,ISH1+MAXCOM-1)
        IDIMVA = ISV2 - ISV1 + 1
        IENDBV = IENDV - ISTARV + 1
        IDIMHA = ISH2 - ISH1 + 1
        IENDBH = IENDH - ISTARH + 1
        IDIMHP=IDIMHA+LENGTH
        CALL CONNUM(SEQHC(ISH1-ISTARH+1),SEQH(MSPO21),IDIMHA)
        CALL FILEDG(SEQHC,IDIMBH,ISH1,ISH2,SEQH,MXCOMP,LENGTH,
     +  CHRSET,IDM,MSPO2,ISTARH,IENDH)
        IDIMVP=IDIMVA+LENGTH
        CALL CONNUM(SEQVC(ISV1-ISTARV+1),SEQV(MSPO21),IDIMVA)
        CALL FILEDG(SEQVC,IDIMBV,ISV1,ISV2,SEQV,MXCOMP,LENGTH,
     +  CHRSET,IDM,MSPO2,ISTARV,IENDV)
        NOROO='N'
        NOROE='N'
      END IF
      GO TO 30
      END IF
********
C
C   COMPLEMENT SEQUENCE
C
      IF(OPT.EQ.29)THEN
C        CALL SHOWFU(KBOUT,'Complement sequence')
        IF(IDM.EQ.26)THEN
          WRITE(KBOUT,*)'Not done for proteins!'
          GO TO 30
        ELSE
          CALL QVORH(VORH,HELPS(OPT),HELPE(OPT),HELPF,DEVNOS(4),
     +    KBIN,KBOUT,FILEH,FILEV)
          IF(VORH.EQ.'H')THEN
            CALL SQREV(SEQHC,IDIMBH)
            CALL SQCOM(SEQHC,IDIMBH)
            CALL CONNUM(SEQHC(ISH1-ISTARH+1),SEQH(MSPO21),IDIMHA)
            CALL FILEDG(SEQHC,IDIMBH,ISH1,ISH2,SEQH,MXCOMP,LENGTH,
     +      CHRSET,IDM,MSPO2,ISTARH,IENDH)
            NOROO='N'
            NOROE='N'
            GO TO 30
          END IF
          IF(VORH.EQ.'V')THEN
            CALL SQREV(SEQVC,IDIMBV)
            CALL SQCOM(SEQVC,IDIMBV)
C   DECODE CHARACTERS INTO INTEGERS IN ACTIVE REGION
            CALL CONNUM(SEQVC(ISV1-ISTARV+1),SEQV(MSPO21),IDIMVA)
            CALL FILEDG(SEQVC,IDIMBV,ISV1,ISV2,SEQV,MXCOMP,LENGTH,
     +      CHRSET,IDM,MSPO2,ISTARV,IENDV)
            NOROO='N'
            NOROE='N'
            GO TO 30
          END IF
        END IF
        GO TO 30
      END IF
C
C  SWITCH MAIN DIAGONAL
C
      IF(OPT.EQ.30)THEN
C        CALL SHOWFU(KBOUT,'Toggle main diagonal switch')
        CALL SWTCHD(JSAME,KBIN,KBOUT,
     +  HELPS(OPT),HELPE(OPT),HELPF,DEVNOS(4))
        NOROO='N'
        GO TO 30
      END IF
      IF(OPT.EQ.31)THEN
C        CALL SHOWFU(KBOUT,'Toggle identities switch')
        CALL SWTCHI(MARKI,KBIN,KBOUT,
     +  HELPS(OPT),HELPE(OPT),HELPF,DEVNOS(4))
        GO TO 30
      END IF
C
C  CHANGE SCORE MATRIX
C
      IF(OPT.EQ.32)THEN
C        CALL SHOWFU(KBOUT,'Select score matrix')
        CALL MATTIN(MATRIX,IDM,FILNAM,FILEP,CHRSET,
     +  KBIN,KBOUT,DEVNOS(1),MATMAX,
     +  HELPS(OPT),HELPE(OPT),HELPF,DEVNOS(4))
C   SET AVERAGE SCORE FOR DEFAULTS
        AVSCOR=0.75
        IF(MATMAX.GT.3)AVSCOR=AVSCOR*16.
        NOROO='N'
        NOROE='N'
        GO TO 30
      END IF
      IF(OPT.EQ.33) THEN
C        CALL SHOWFU(KBOUT,'Reset number of standard deviations')
        CALL DP33(RNSD,
     +  KBIN,KBOUT,HELPS(OPT),HELPE(OPT),HELPF,DEVNOS(4),IOK)
        GO TO 30
      END IF
      IF(OPT.EQ.34) THEN
C        CALL SHOWFU(KBOUT,'Reset gap penalties')
        CALL GETGAP(KBIN,KBOUT,IGAPG,IGAPH,
     +  HELPS(OPT),HELPE(OPT),HELPF,DEVNOS(4),IDM,IOK)
        GO TO 30
      END IF
      GO TO 30
9999  CONTINUE
      CALL SHUTD
      END
