      SUBROUTINE STORPL(IRESID,RSNAME,ATNAM,INCHN,INSEQ,COORDS,NEWCHN)  STOR001
C-----------------------------------------------------------------------STOR002
CHECK v.2.2-->                                                          STOR003
C                                                                       STOR004
C  SUBROUTINE STORPL  Store atom coordinates for current atom and, when STOR005
C                     have required set for a planar group, calculate   STOR006
C                     the best-fit plane and atomic deviation from it   STOR007
C                                                                       STOR008
C----------------------------------------------------------------------+STOR009
                                                                        STOR010
      SAVE                                                              STOR011
                                                                        STOR012
      INCLUDE 'anglen.inc'                                              STOR013
                                                                        STOR014
      CHARACTER*1   CHAIN, INCHN                                        STOR015
      CHARACTER*3   ATNAM, RSNAME, SNAME                                STOR016
      CHARACTER*5   INSEQ, SEQNO                                        STOR017
CHECK v.3.2-->                                                          STOR018
C      INTEGER       CURRES, CURRNT, IATOM, IP, IRESID, LAST, NGOT,     STOR019
C     -              ORDVEC(3), PLATOM, PLRES, SRESID                   STOR020
      INTEGER       IATOM, IP, IRESID, NGOT, ORDVEC(3), PLATOM, PLRES,  STOR021
     &              SRESID                                              STOR022
CHECK v.3.2<--                                                          STOR023
      LOGICAL       FILLED(NPLATM), FIRST, NEWCHN, PERROR               STOR024
      REAL          CENTRE(3), COORDS(3), EIGVEC(3,3), NORMAL(3),       STOR025
CHECK v.3.0.1-->                                                        STOR026
C     -              RMSDIF, TCOORD(NPLATM), XYZ(3,NPLATM)              STOR027
     &              RMSDIF, TCOORD(3,NPLATM), XYZ(3,NPLATM)             STOR028
CHECK v.3.0.1<--                                                        STOR029
                                                                        STOR030
CHECK v.3.2-->                                                          STOR031
C      DATA CURRES, CURRNT, LAST / 0, 1, 2 /                            STOR032
CHECK v.3.2<--                                                          STOR033
      DATA FIRST  / .TRUE. /                                            STOR034
                                                                        STOR035
C---- Initialise variables on first entry into routine                  STOR036
      PERROR = .FALSE.                                                  STOR037
      IF (FIRST) THEN                                                   STOR038
                                                                        STOR039
C----     Initialise the residue details                                STOR040
          CHAIN = ' '                                                   STOR041
          FIRST = .FALSE.                                               STOR042
CHECK v.3.0.1-->                                                        STOR043
C          NEWCHN = .TRUE.                                              STOR044
CHECK v.3.0.1<--                                                        STOR045
          NGOT = 0                                                      STOR046
          DO 300, IATOM = 1, NPLATM                                     STOR047
              FILLED(IATOM) = .FALSE.                                   STOR048
              XYZ(1,IATOM) = 0.0                                        STOR049
              XYZ(2,IATOM) = 0.0                                        STOR050
              XYZ(3,IATOM) = 0.0                                        STOR051
 300      CONTINUE                                                      STOR052
          SEQNO = ' '                                                   STOR053
          SNAME = ' '                                                   STOR054
          SRESID = -9999                                                STOR055
      ENDIF                                                             STOR056
                                                                        STOR057
C---- If residue has changed, may need to perform planarity calculationsSTOR058
C     on previous residue                                               STOR059
      IF (NEWCHN .OR. IRESID.NE.SRESID) THEN                            STOR060
                                                                        STOR061
C----     Check that have at least 3 atoms                              STOR062
          IF (NGOT.GT.3) THEN                                           STOR063
                                                                        STOR064
C----         Check that have all the atoms required                    STOR065
              NGOT = 0                                                  STOR066
              DO 400, IATOM = 1, NPATOM(PLRES)                          STOR067
                  IF (FILLED(IATOM)) NGOT = NGOT + 1                    STOR068
 400          CONTINUE                                                  STOR069
                                                                        STOR070
C----         If have all the planar atoms for this residue, then       STOR071
C             calculate a best-fit plane                                STOR072
              IF (NGOT.EQ.NPATOM(PLRES)) THEN                           STOR073
                                                                        STOR074
C----             Calculate best-fit plane                              STOR075
                  CALL PLANE(XYZ,NGOT,CENTRE,NORMAL,EIGVEC,ORDVEC,      STOR076
     &                 PERROR)                                          STOR077
                  IF (.NOT.PERROR) THEN                                 STOR078
                                                                        STOR079
C----                 Calculate RMS distance of all points from best-fitSTOR080
C                     plane                                             STOR081
CHECK v.3.2-->                                                          STOR082
C                      CALL GETRMS(XYZ,NGOT,CENTRE,NORMAL,RMSDIF)       STOR083
                      CALL GETRMS(XYZ,NGOT,NORMAL,RMSDIF)               STOR084
CHECK v.3.2<--                                                          STOR085
                                                                        STOR086
C----                 Transform coordinates of atoms and write out      STOR087
                      CALL WRIPLN(XYZ,NGOT,EIGVEC,ORDVEC,SRESID,SEQNO,  STOR088
     &                    SNAME,CHAIN,RMSDIF,TCOORD,ANAME(2,PLRES),     STOR089
     &                    PDBFIL,PLRES)                                 STOR090
                  ENDIF                                                 STOR091
              ENDIF                                                     STOR092
          ENDIF                                                         STOR093
                                                                        STOR094
C----     Reinitalise all the variables                                 STOR095
          NGOT = 0                                                      STOR096
          DO 500, IATOM = 1, NPLATM                                     STOR097
              FILLED(IATOM) = .FALSE.                                   STOR098
              XYZ(1,IATOM) = 0.0                                        STOR099
              XYZ(2,IATOM) = 0.0                                        STOR100
              XYZ(3,IATOM) = 0.0                                        STOR101
 500      CONTINUE                                                      STOR102
      ENDIF                                                             STOR103
                                                                        STOR104
C---- Check whether the residue is one that has a planar group          STOR105
      IP = 0                                                            STOR106
      PLRES = 0                                                         STOR107
 600  CONTINUE                                                          STOR108
          IP = IP + 1                                                   STOR109
          IF (RSNAME.EQ.ANAME(1,IP)) PLRES = IP                         STOR110
      IF (PLRES.EQ.0 .AND. IP.LT.NPLANE) GO TO 600                      STOR111
                                                                        STOR112
C---- If not a planar group, then return                                STOR113
      IF (PLRES.EQ.0) GO TO 999                                         STOR114
                                                                        STOR115
C---- Check whether this is one of the atoms in the planar group        STOR116
      IP = 0                                                            STOR117
      PLATOM = 0                                                        STOR118
 700  CONTINUE                                                          STOR119
          IP = IP + 1                                                   STOR120
          IF (ATNAM.EQ.ANAME(IP + 1,PLRES)) PLATOM = IP                 STOR121
CHECK v.3.0.1-->                                                        STOR122
C      IF (PLATOM.EQ.0 .AND. IP.LT.NPLATM .AND.                         STOR123
C     -    ANAME(IP + 1,PLRES).NE.'   ') GO TO 700                      STOR124
      IF (PLATOM.EQ.0 .AND. IP.LT.NPLATM) GO TO 700                     STOR125
CHECK v.3.0.1<--                                                        STOR126
                                                                        STOR127
C---- If not one of the atoms in the planar group, then return          STOR128
      IF (PLATOM.EQ.0) GO TO 999                                        STOR129
                                                                        STOR130
C---- Store coordinates of current atom                                 STOR131
      CHAIN = INCHN                                                     STOR132
      FILLED(PLATOM) = .TRUE.                                           STOR133
CHECK v.3.0.1-->                                                        STOR134
C      NEWCHN = .FALSE.                                                 STOR135
CHECK v.3.0.1<--                                                        STOR136
      NGOT = NGOT + 1                                                   STOR137
      SEQNO = INSEQ                                                     STOR138
      SNAME = RSNAME                                                    STOR139
      SRESID = IRESID                                                   STOR140
      XYZ(1,PLATOM) = COORDS(1)                                         STOR141
      XYZ(2,PLATOM) = COORDS(2)                                         STOR142
      XYZ(3,PLATOM) = COORDS(3)                                         STOR143
                                                                        STOR144
 999  CONTINUE                                                          STOR145
      RETURN                                                            STOR146
      END                                                               STOR147
