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-2024.
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  import java.util.ArrayList;
44  import java.util.List;
45  import java.util.Random;
46  
47  /** This implements single-rotamer changes in the framework of the rotamer energy matrices. */
48  public class RotamerMatrixMove implements MCMove {
49  
50    private final boolean useAllElims;
51    /**
52     * CurrentRots should point to the same array as being used in the overlying MetropolisMC
53     * implementation.
54     */
55    private final int[] currentRots;
56  
57    private final List<Integer> allowedRes;
58    private final List<List<Integer>> allowedRots;
59    private final int nAllowed;
60    private final RotamerOptimization rotamerOptimization;
61    private final boolean monteCarloTesting;
62    /** When we take a step, we need to remember which rotamer of which residue was changed. */
63    private int changedRes;
64  
65    private int changedRot;
66  
67    /**
68     * Constructs the RotamerMatrixMove set; at present, a new object must be made if rotamers or
69     * residues are changed outside the scope of this class.
70     *
71     * @param useAllElims Use eliminated pair/triple info.
72     * @param rotamers Initial rotamer set.
73     * @param residues Array of residues.
74     * @param rotamerOptimization RotamerOptimization instance.
75     * @param eliminatedRotamers Eliminated rotamers instance.
76     * @param monteCarloTesting True for MC testing.
77     */
78    public RotamerMatrixMove(boolean useAllElims, int[] rotamers, Residue[] residues,
79        RotamerOptimization rotamerOptimization, EliminatedRotamers eliminatedRotamers,
80        boolean monteCarloTesting) {
81      this.useAllElims = useAllElims;
82      this.rotamerOptimization = rotamerOptimization;
83      this.monteCarloTesting = monteCarloTesting;
84  
85      int nRes = rotamers.length;
86      currentRots = rotamers;
87  
88      allowedRes = new ArrayList<>(nRes);
89      allowedRots = new ArrayList<>(nRes);
90  
91      for (int i = 0; i < nRes; i++) {
92        ArrayList<Integer> resAllowed = new ArrayList<>();
93  
94        int lenRi = residues[i].getRotamers().length;
95        for (int ri = 0; ri < lenRi; ri++) {
96          if (!eliminatedRotamers.check(i, ri)) {
97            resAllowed.add(ri);
98          }
99        }
100 
101       if (resAllowed.size() > 1) {
102         resAllowed.trimToSize();
103         allowedRes.add(i);
104         allowedRots.add(resAllowed);
105       }
106     }
107 
108     ((ArrayList<Integer>) allowedRes).trimToSize();
109     nAllowed = allowedRes.size();
110   }
111 
112   @Override
113   public void move() {
114     boolean validMove = !useAllElims;
115     int indexI;
116     int indexRI;
117     do {
118       // resI and rotI correspond to their positions in allowedRes and
119       // allowedRots. indexI and indexRI correspond to their numbers
120       // in the rotamer matrix.
121 
122       Random rand = new Random();
123       if (monteCarloTesting) {
124         rand.setSeed(nAllowed);
125       }
126       int resI = rand.nextInt(nAllowed);
127       indexI = allowedRes.get(resI);
128       List<Integer> allowedRotsI = allowedRots.get(resI);
129       int lenRi = allowedRotsI.size();
130       int rotI = rand.nextInt(lenRi);
131       indexRI = allowedRotsI.get(rotI);
132       if (useAllElims) {
133         validMove = rotamerOptimization.checkValidMove(indexI, indexRI, currentRots);
134       }
135     } while (!validMove);
136 
137     changedRes = indexI;
138     changedRot = currentRots[indexI];
139 
140     currentRots[indexI] = indexRI;
141   }
142 
143   @Override
144   public void revertMove() {
145     currentRots[changedRes] = changedRot;
146   }
147 
148   @Override
149   public String toString() {
150     return "Rotamer moves utilizing a rotamer energy matrix";
151   }
152 }