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.mc;
39  
40  import static java.lang.String.format;
41  import static org.apache.commons.math3.util.FastMath.min;
42  
43  import ffx.algorithms.dynamics.thermostats.Thermostat;
44  import ffx.potential.ForceFieldEnergy;
45  import ffx.potential.MolecularAssembly;
46  import ffx.potential.bonded.AminoAcidUtils;
47  import ffx.potential.bonded.Residue;
48  import ffx.potential.bonded.AminoAcidUtils.AminoAcid3;
49  import ffx.potential.parsers.PDBFilter;
50  import java.io.File;
51  import java.util.List;
52  import java.util.concurrent.ThreadLocalRandom;
53  import java.util.logging.Logger;
54  import org.apache.commons.io.FilenameUtils;
55  
56  /**
57   * Conformational Biased Monte Carlo (applied to ALL torsions of a peptide side-chain).
58   *
59   * <p>This method is described by Frenkel/Smit in "Understanding Molecular Simulation" Chapters
60   * 13.2,13.3 This uses the "conformational biasing" method to select whole rotamer transformations
61   * that are frequently accepted.
62   *
63   * @author Stephen D. LuCore
64   */
65  public class RosenbluthCBMC implements MonteCarloListener {
66  
67    private static final Logger logger = Logger.getLogger(RosenbluthCBMC.class.getName());
68  
69    /** The MolecularAssembly to operate on. */
70    private final MolecularAssembly molecularAssembly;
71    /** The ForceFieldEnergy in use. */
72    private final ForceFieldEnergy forceFieldEnergy;
73    /** The temperature in use. */
74    private final double temperature;
75    /** At each move, one of these residues will be chosen as the target. */
76    private final List<Residue> targets;
77    /** Number of mcUpdate() calls (e.g. MD steps) between move proposals. */
78    private final int mcFrequency;
79    /** Size of the trial sets, k. */
80    private final int trialSetSize;
81    /** Keeps track of calls to mcUpdate (e.g. MD steps). */
82    private int steps = 0;
83    /** Counters for proposed and accepted moves. */
84    private int numMovesProposed = 0;
85    /** Writes PDBs of each trial set and original/proposed configurations. */
86    private final boolean writeSnapshots;
87    /** PDBFilter to write out result. */
88    private PDBFilter writer = null;
89  
90    /**
91     * RRMC constructor.
92     *
93     * @param targets Residues to undergo RRMC.
94     * @param mcFrequency Number of MD steps between RRMC proposals.
95     * @param trialSetSize Larger values cost more but increase acceptance.
96     * @param molecularAssembly a {@link ffx.potential.MolecularAssembly} object.
97     * @param ffe a {@link ffx.potential.ForceFieldEnergy} object.
98     * @param thermostat a {@link ffx.algorithms.dynamics.thermostats.Thermostat} object.
99     * @param writeSnapshots a boolean.
100    */
101   public RosenbluthCBMC(MolecularAssembly molecularAssembly, ForceFieldEnergy ffe,
102       Thermostat thermostat, List<Residue> targets, int mcFrequency, int trialSetSize,
103       boolean writeSnapshots) {
104     this.targets = targets;
105     this.mcFrequency = mcFrequency;
106     this.trialSetSize = trialSetSize;
107     this.molecularAssembly = molecularAssembly;
108     this.forceFieldEnergy = ffe;
109     this.writeSnapshots = writeSnapshots;
110 
111     if (thermostat != null) {
112       temperature = thermostat.getTargetTemperature();
113     } else {
114       temperature = 298.15;
115     }
116 
117     for (int i = targets.size() - 1; i >= 0; i--) {
118       AminoAcid3 name = AminoAcidUtils.AminoAcid3.valueOf(targets.get(i).getName());
119       if (name == AminoAcidUtils.AminoAcid3.GLY || name == AminoAcidUtils.AminoAcid3.PRO
120           || name == AminoAcidUtils.AminoAcid3.ALA) {
121         targets.remove(i);
122       }
123     }
124     if (targets.isEmpty()) {
125       logger.severe(" Empty target list for CMBC.");
126     }
127   }
128 
129   /**
130    * cbmcStep.
131    *
132    * @return a boolean.
133    */
134   public boolean cbmcStep() {
135     numMovesProposed++;
136     boolean accepted;
137 
138     // Select a target residue.
139     int index = ThreadLocalRandom.current().nextInt(targets.size());
140     Residue target = targets.get(index);
141     RosenbluthChiAllMove cbmcMove = new RosenbluthChiAllMove(molecularAssembly, target, trialSetSize,
142         forceFieldEnergy, temperature, writeSnapshots, numMovesProposed, true);
143     if (cbmcMove.getMode() == RosenbluthChiAllMove.MODE.CHEAP) {
144       return cbmcMove.wasAccepted();
145     }
146     double Wn = cbmcMove.getWn();
147     double Wo = cbmcMove.getWo();
148     double criterion = min(1, Wn / Wo);
149     double rng = ThreadLocalRandom.current().nextDouble();
150     logger.info(format("    rng:    %5.2f", rng));
151     if (rng < criterion) {
152       cbmcMove.move();
153       logger.info(format(" Accepted!  Energy: %.4f\n", cbmcMove.finalEnergy));
154       accepted = true;
155       write();
156     } else {
157       logger.info(" Denied.\n");
158       accepted = false;
159     }
160 
161     return accepted;
162   }
163 
164   /**
165    * controlStep.
166    *
167    * @return a boolean.
168    */
169   public boolean controlStep() {
170     int index = ThreadLocalRandom.current().nextInt(targets.size());
171     Residue target = targets.get(index);
172     RosenbluthChiAllMove cbmcMove = new RosenbluthChiAllMove(molecularAssembly, target, -1,
173         forceFieldEnergy, temperature, false, numMovesProposed, true);
174     return cbmcMove.wasAccepted();
175   }
176 
177   /** {@inheritDoc} */
178   @Override
179   public boolean mcUpdate(double temperature) {
180     steps++;
181     if (steps % mcFrequency == 0) {
182       return cbmcStep();
183     }
184     return false;
185   }
186 
187   /** Write out a PDB file. */
188   private void write() {
189     if (writer == null) {
190       writer = new PDBFilter(molecularAssembly.getFile(), molecularAssembly, null, null);
191     }
192     String filename = molecularAssembly.getFile().getAbsolutePath();
193     if (!filename.contains("_mc")) {
194       filename = FilenameUtils.removeExtension(filename) + "_mc.pdb";
195     }
196     File file = new File(filename);
197     writer.writeFile(file, false);
198   }
199 }