      SUBROUTINE READPR                                                 READ001
C-----------------------------------------------------------------------READ002
C                                                                       READ003
C  SUBROUTINE READPR  Read through appropriate Brookhaven file and      READ004
C                     perform required distance and angle calculations  READ005
C                                                                       READ006
C----------------------------------------------------------------------+READ007
                                                                        READ008
      INCLUDE 'anglen.inc'                                              READ009
                                                                        READ010
      CHARACTER*1   CHAIN                                               READ011
      CHARACTER*3   ATNAME, RSNAME                                      READ012
      CHARACTER*5   SEQNO                                               READ013
      CHARACTER*6   IDENT, INSEQ, LSTSEQ                                READ014
      CHARACTER*80  IREC                                                READ015
      INTEGER       FRESID, I, IBAT, IRESID, LINE, RESIDC, RESIDN       READ016
      LOGICAL       NEWCHN                                              READ017
      REAL          COORDC(3), COORDN(3), COORDS(3), DIST2, PEPBN2      READ018
                                                                        READ019
      PARAMETER    (PEPBN2 = 2.5 * 2.5)                                 READ020
                                                                        READ021
C---- Initialise variables                                              READ022
      IFAIL = .FALSE.                                                   READ023
      IRESID = 0                                                        READ024
CHECK v.2.1-->                                                          READ025
      LINE = 0                                                          READ026
CHECK v.2.1<--                                                          READ027
      LSTSEQ = '^^^^^^'                                                 READ028
      NEWCHN = .TRUE.                                                   READ029
      RESIDC = 0                                                        READ030
      RESIDN = 0                                                        READ031
                                                                        READ032
C---- Loop through Brookhaven file, searching for main-chain atoms      READ033
100   CONTINUE                                                          READ034
          READ(1,150,END=800,ERR=904) IREC                              READ035
150       FORMAT(A)                                                     READ036
          LINE = LINE + 1                                               READ037
          IDENT = IREC(1:6)                                             READ038
                                                                        READ039
C----     If record is an atom record, check its atom type              READ040
          IF (IDENT.EQ.'ATOM  ') THEN                                   READ041
                                                                        READ042
C----         Pick off atom name, residue names, sequence number, and B READ043
              ATNAME = IREC(14:16)                                      READ044
              RSNAME = IREC(18:20)                                      READ045
              CHAIN = IREC(22:22)                                       READ046
              INSEQ = IREC(22:27)                                       READ047
              SEQNO = IREC(23:27)                                       READ048
                                                                        READ049
C----         If new chain encountered, then flag a chain-break         READ050
CHECK v.3.0.1-->                                                        READ051
C170           CONTINUE                                                 READ052
CHECK v.3.0.1<--                                                        READ053
              IF (INSEQ(1:1).NE.LSTSEQ(1:1)) THEN                       READ054
                  NEWCHN = .TRUE.                                       READ055
                  LSTSEQ = '^^^^^^'                                     READ056
              ENDIF                                                     READ057
                                                                        READ058
C----         If sequence number has changed, increment residue-count   READ059
              IF (INSEQ.NE.LSTSEQ) THEN                                 READ060
                  IRESID = IRESID + 1                                   READ061
                  IF (IRESID.GT.MXONE) GO TO 906                        READ062
              ENDIF                                                     READ063
              LSTSEQ = INSEQ                                            READ064
                                                                        READ065
C             Check for chain break by looking for excessively long     READ066
C             peptide bonds C(i-1)->N(i)                                READ067
                                                                        READ068
C----         If atom is a main-chain C or N, store its coords if not   READ069
C             already done so                                           READ070
              IF (IRESID.NE.RESIDC .AND. (ATNAME.EQ.'C  ' .OR.          READ071
     &            ATNAME.EQ.'H  ')) THEN                                READ072
                  READ(IREC,180,ERR=902) (COORDC(I), I = 1, 3)          READ073
180               FORMAT(30X,3F8.0)                                     READ074
                  RESIDC = IRESID                                       READ075
              ELSE IF (IRESID.NE.RESIDN .AND. (ATNAME.EQ.'N  ' .OR.     READ076
     &            ATNAME.EQ.'D  ')) THEN                                READ077
                  READ(IREC,180,ERR=902) (COORDN(I), I = 1, 3)          READ078
                  RESIDN = IRESID                                       READ079
                                                                        READ080
C----             If have coords for C atom from previous residue,      READ081
c                 check whether distance between it and the current N   READ082
c                 indicates a chain break here                          READ083
                  IF (.NOT.NEWCHN .AND. RESIDN.EQ.RESIDC + 1 .AND.      READ084
     &                RESIDC.NE.0) THEN                                 READ085
                      DIST2 = (COORDN(1) - COORDC(1)) ** 2              READ086
     &                    + (COORDN(2) - COORDC(2)) ** 2                READ087
     &                    + (COORDN(3) - COORDC(3)) ** 2                READ088
                      IF (DIST2.GT.PEPBN2) THEN                         READ089
                          NEWCHN = .TRUE.                               READ090
                      ENDIF                                             READ091
                  ELSE                                                  READ092
                      NEWCHN = .TRUE.                                   READ093
                  ENDIF                                                 READ094
              ENDIF                                                     READ095
                                                                        READ096
C----         If have a chain-break here, store the number of the first READ097
C             residue in the new chain                                  READ098
              IF (NEWCHN) FRESID = IRESID                               READ099
                                                                        READ100
CHECK v.2.2-->                                                          READ101
C----         Retrieve the atomic coordinates                           READ102
              READ(IREC,180,ERR=902) (COORDS(I), I = 1, 3)              READ103
CHECK v.2.2<--                                                          READ104
                                                                        READ105
C----         Determine whether the atom is a main-chain atom           READ106
              IBAT = 0                                                  READ107
              DO 400, I = 1, NMATOM                                     READ108
                  IF (ATNAME.EQ.MATOM(I)) IBAT = I                      READ109
 400          CONTINUE                                                  READ110
                                                                        READ111
C----         If atom is a main-chain atom call the calculation routine READ112
              IF (IBAT.NE.0) THEN                                       READ113
CHECK v.2.2-->                                                          READ114
C                  READ(IREC,180,ERR=902) (COORDS(I), I = 1, 3)         READ115
CHECK v.2.2<--                                                          READ116
                  CALL STORAT(IRESID,RSNAME,CHAIN,SEQNO,IBAT,COORDS,    READ117
     &                NEWCHN)                                           READ118
              ENDIF                                                     READ119
                                                                        READ120
CHECK v.2.2-->                                                          READ121
C             See if atom is part of a planar group                     READ122
              CALL STORPL(IRESID,RSNAME,ATNAME,CHAIN,SEQNO,COORDS,      READ123
     &                NEWCHN)                                           READ124
CHECK v.2.2<--                                                          READ125
                                                                        READ126
C----     If TER record, then have encountered end of chain             READ127
          ELSE IF (IDENT.EQ.'TER   ') THEN                              READ128
              LSTSEQ = '^^^^^^'                                         READ129
          ENDIF                                                         READ130
      GO TO 100                                                         READ131
                                                                        READ132
C---- End of Brookhaven file reached, calculate last set of lengths and READ133
c     angles                                                            READ134
800   CONTINUE                                                          READ135
      IBAT = 0                                                          READ136
      NEWCHN = .TRUE.                                                   READ137
      CALL STORAT(IRESID,RSNAME,CHAIN,SEQNO,IBAT,COORDS,NEWCHN)         READ138
                                                                        READ139
CHECK v.2.2-->                                                          READ140
C---- Calculate planarity for last residue, if required                 READ141
CHECK v.3.2-->                                                          READ142
C      CALL STORPL(-999,' ',' ',' ',SEQNO,COORDS,NEWCHN)                READ143
      CALL STORPL(-999,'   ','   ',' ',SEQNO,COORDS,NEWCHN)             READ144
CHECK v.3.2<--                                                          READ145
CHECK v.2.2<--                                                          READ146
                                                                        READ147
      GO TO 999                                                         READ148
                                                                        READ149
C---- Fatal errors                                                      READ150
902   CONTINUE                                                          READ151
CHECK v.2.1.3-->                                                        READ152
C      PRINT*, 'Error in coords for atom: ', ATNAME, '-', RSNAME, '-',  READ153
C     -    INSEQ                                                        READ154
      PRINT*, '**** Error in coords for atom: ', ATNAME, '-', RSNAME,   READ155
     &    '-', INSEQ                                                    READ156
CHECK v.2.1.3<--                                                        READ157
      GO TO 100                                                         READ158
                                                                        READ159
904   CONTINUE                                                          READ160
CHECK v.2.1.3-->                                                        READ161
C      PRINT*, 'Data error reading file:  [', FILNEW, ']'               READ162
C      PRINT*, '    Line: ', LINE                                       READ163
      PRINT*, '**** Data error reading file:  [', FILNEW, ']'           READ164
      PRINT*, '    Line: ', LINE                                        READ165
CHECK v.2.1.3<--                                                        READ166
      GO TO 990                                                         READ167
                                                                        READ168
906   CONTINUE                                                          READ169
CHECK v.2.1.3-->                                                        READ170
C      PRINT*, 'Maximum number of residues per protein exceeded:',      READ171
C     -    MXONE                                                        READ172
      PRINT*, '**** Maximum number of residues per protein exceeded:',  READ173
     &    MXONE                                                         READ174
CHECK v.2.1.3<--                                                        READ175
      GO TO 990                                                         READ176
                                                                        READ177
990   CONTINUE                                                          READ178
      IFAIL = .TRUE.                                                    READ179
                                                                        READ180
999   CONTINUE                                                          READ181
      RETURN                                                            READ182
      END                                                               READ183
