View Javadoc
1   // ******************************************************************************
2   //
3   // Title:       Force Field X.
4   // Description: Force Field X - Software for Molecular Biophysics.
5   // Copyright:   Copyright (c) Michael J. Schnieders 2001-2025.
6   //
7   // This file is part of Force Field X.
8   //
9   // Force Field X is free software; you can redistribute it and/or modify it
10  // under the terms of the GNU General Public License version 3 as published by
11  // the Free Software Foundation.
12  //
13  // Force Field X is distributed in the hope that it will be useful, but WITHOUT
14  // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  // FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16  // details.
17  //
18  // You should have received a copy of the GNU General Public License along with
19  // Force Field X; if not, write to the Free Software Foundation, Inc., 59 Temple
20  // Place, Suite 330, Boston, MA 02111-1307 USA
21  //
22  // Linking this library statically or dynamically with other modules is making a
23  // combined work based on this library. Thus, the terms and conditions of the
24  // GNU General Public License cover the whole combination.
25  //
26  // As a special exception, the copyright holders of this library give you
27  // permission to link this library with independent modules to produce an
28  // executable, regardless of the license terms of these independent modules, and
29  // to copy and distribute the resulting executable under terms of your choice,
30  // provided that you also meet, for each linked independent module, the terms
31  // and conditions of the license of that module. An independent module is a
32  // module which is not derived from or based on this library. If you modify this
33  // library, you may extend this exception to your version of the library, but
34  // you are not obligated to do so. If you do not wish to do so, delete this
35  // exception statement from your version.
36  //
37  // ******************************************************************************
38  package ffx.algorithms.optimize.manybody;
39  
40  import ffx.algorithms.mc.MCMove;
41  import ffx.algorithms.optimize.RotamerOptimization;
42  import ffx.potential.bonded.Residue;
43  
44  import java.util.ArrayList;
45  import java.util.List;
46  import java.util.Random;
47  
48  /** This implements single-rotamer changes in the framework of the rotamer energy matrices. */
49  public class RotamerMatrixMove implements MCMove {
50  
51    private final boolean useAllElims;
52    /**
53     * CurrentRots should point to the same array as being used in the overlying MetropolisMC
54     * implementation.
55     */
56    private final int[] currentRots;
57  
58    private final List<Integer> allowedRes;
59    private final List<List<Integer>> allowedRots;
60    private final int nAllowed;
61    private final RotamerOptimization rotamerOptimization;
62    private final boolean monteCarloTesting;
63    /** When we take a step, we need to remember which rotamer of which residue was changed. */
64    private int changedRes;
65  
66    private int changedRot;
67  
68    /**
69     * Constructs the RotamerMatrixMove set; at present, a new object must be made if rotamers or
70     * residues are changed outside the scope of this class.
71     *
72     * @param useAllElims Use eliminated pair/triple info.
73     * @param rotamers Initial rotamer set.
74     * @param residues Array of residues.
75     * @param rotamerOptimization RotamerOptimization instance.
76     * @param eliminatedRotamers Eliminated rotamers instance.
77     * @param monteCarloTesting True for MC testing.
78     */
79    public RotamerMatrixMove(boolean useAllElims, int[] rotamers, Residue[] residues,
80        RotamerOptimization rotamerOptimization, EliminatedRotamers eliminatedRotamers,
81        boolean monteCarloTesting) {
82      this.useAllElims = useAllElims;
83      this.rotamerOptimization = rotamerOptimization;
84      this.monteCarloTesting = monteCarloTesting;
85  
86      int nRes = rotamers.length;
87      currentRots = rotamers;
88  
89      allowedRes = new ArrayList<>(nRes);
90      allowedRots = new ArrayList<>(nRes);
91  
92      for (int i = 0; i < nRes; i++) {
93        ArrayList<Integer> resAllowed = new ArrayList<>();
94  
95        int lenRi = residues[i].getRotamers().length;
96        for (int ri = 0; ri < lenRi; ri++) {
97          if (!eliminatedRotamers.check(i, ri)) {
98            resAllowed.add(ri);
99          }
100       }
101 
102       if (resAllowed.size() > 1) {
103         resAllowed.trimToSize();
104         allowedRes.add(i);
105         allowedRots.add(resAllowed);
106       }
107     }
108 
109     ((ArrayList<Integer>) allowedRes).trimToSize();
110     nAllowed = allowedRes.size();
111   }
112 
113   @Override
114   public void move() {
115     boolean validMove = !useAllElims;
116     int indexI;
117     int indexRI;
118     do {
119       // resI and rotI correspond to their positions in allowedRes and
120       // allowedRots. indexI and indexRI correspond to their numbers
121       // in the rotamer matrix.
122 
123       Random rand = new Random();
124       if (monteCarloTesting) {
125         rand.setSeed(nAllowed);
126       }
127       int resI = rand.nextInt(nAllowed);
128       indexI = allowedRes.get(resI);
129       List<Integer> allowedRotsI = allowedRots.get(resI);
130       int lenRi = allowedRotsI.size();
131       int rotI = rand.nextInt(lenRi);
132       indexRI = allowedRotsI.get(rotI);
133       if (useAllElims) {
134         validMove = rotamerOptimization.checkValidMove(indexI, indexRI, currentRots);
135       }
136     } while (!validMove);
137 
138     changedRes = indexI;
139     changedRot = currentRots[indexI];
140 
141     currentRots[indexI] = indexRI;
142   }
143 
144   @Override
145   public void revertMove() {
146     currentRots[changedRes] = changedRot;
147   }
148 
149   @Override
150   public String toString() {
151     return "Rotamer moves utilizing a rotamer energy matrix";
152   }
153 }