Skip to content

Commit cb72185

Browse files
committed
Take the best alignment of the optimization trajectory cesymm
This ensures that optimization does not make the alignment worse, in case refinement was good enough.
1 parent d63491c commit cb72185

2 files changed

Lines changed: 63 additions & 25 deletions

File tree

biojava-structure/src/main/java/org/biojava/nbio/structure/symmetry/internal/CeSymmIterative.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,10 @@ else if (!r.isSignificant())
223223
*/
224224
private boolean buildAlignment() throws StructureException {
225225

226+
// If one level, nothing to build
227+
if (levels.size() == 1)
228+
return false;
229+
226230
// Initialize a new multiple alignment
227231
MultipleAlignment msa = new MultipleAlignmentImpl();
228232
msa.getEnsemble().setAtomArrays(new ArrayList<Atom[]>());

biojava-structure/src/main/java/org/biojava/nbio/structure/symmetry/internal/SymmOptimizer.java

Lines changed: 59 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,12 @@ public class SymmOptimizer {
9393
// Variables that store the history of the optimization - slower if on
9494
private static final boolean history = false;
9595
private static final int saveStep = 100;
96-
private static final String pathToHistory = "./results/";
96+
private static final String pathToHistory = "results/symm-opt/";
97+
private List<Long> timeHistory;
9798
private List<Integer> lengthHistory;
9899
private List<Double> rmsdHistory;
99-
private List<Double> scoreHistory;
100+
private List<Double> tmScoreHistory;
101+
private List<Double> mcScoreHistory;
100102

101103
/**
102104
* Constructor with a seed MultipleAligment storing a refined symmetry
@@ -136,14 +138,19 @@ public SymmOptimizer(CeSymmResult symmResult) {
136138

137139
private void initialize() throws StructureException, RefinerFailedException {
138140

139-
if (order == 1) {
141+
if (order == 1)
140142
throw new RefinerFailedException(
141-
"Non-symmetric seed slignment: order = 1");
142-
}
143-
if (repeatCore < 1) {
143+
"Non-symmetric seed alignment: order = 1");
144+
if (repeatCore < 1)
144145
throw new RefinerFailedException(
145-
"Seed alignment too short: repeat core length == 0");
146-
}
146+
"Seed alignment too short: repeat core length < 1");
147+
148+
// Initialize the history variables
149+
timeHistory = new ArrayList<Long>();
150+
lengthHistory = new ArrayList<Integer>();
151+
rmsdHistory = new ArrayList<Double>();
152+
mcScoreHistory = new ArrayList<Double>();
153+
tmScoreHistory = new ArrayList<Double>();
147154

148155
C = 20 * order;
149156

@@ -154,15 +161,13 @@ private void initialize() throws StructureException, RefinerFailedException {
154161

155162
// Store the residues aligned in the block
156163
List<Integer> aligned = new ArrayList<Integer>();
157-
for (int su = 0; su < order; su++) {
164+
for (int su = 0; su < order; su++)
158165
aligned.addAll(block.get(su));
159-
}
160166

161167
// Add any residue not aligned to the free pool
162168
for (int i = 0; i < atoms.length; i++) {
163-
if (!aligned.contains(i)) {
169+
if (!aligned.contains(i))
164170
freePool.add(i);
165-
}
166171
}
167172
checkGaps();
168173

@@ -190,14 +195,21 @@ public MultipleAlignment optimize() throws StructureException,
190195

191196
initialize();
192197

193-
// Initialize the history variables
194-
lengthHistory = new ArrayList<Integer>();
195-
rmsdHistory = new ArrayList<Double>();
196-
scoreHistory = new ArrayList<Double>();
198+
// Save the optimal alignment
199+
List<List<Integer>> optBlock = new ArrayList<List<Integer>>();
200+
List<Integer> optFreePool = new ArrayList<Integer>();
201+
optFreePool.addAll(freePool);
202+
for (int k = 0; k < order; k++) {
203+
List<Integer> b = new ArrayList<Integer>();
204+
b.addAll(block.get(k));
205+
optBlock.add(b);
206+
}
207+
double optScore = mcScore;
197208

198209
int conv = 0; // Number of steps without an alignment improvement
199210
int i = 1;
200211
int stepsToConverge = Math.max(maxIter / 50, 1000);
212+
long initialTime = System.nanoTime()/1000000;
201213

202214
while (i < maxIter && conv < stepsToConverge) {
203215

@@ -266,24 +278,44 @@ public MultipleAlignment optimize() throws StructureException,
266278

267279
logger.debug(i + ": --prob: " + prob + ", --score: " + AS
268280
+ ", --conv: " + conv);
281+
282+
// Store as the optimal alignment if better
283+
if (mcScore > optScore) {
284+
optBlock = new ArrayList<List<Integer>>();
285+
optFreePool = new ArrayList<Integer>();
286+
optFreePool.addAll(freePool);
287+
for (int k = 0; k < order; k++) {
288+
List<Integer> b = new ArrayList<Integer>();
289+
b.addAll(block.get(k));
290+
optBlock.add(b);
291+
}
292+
optScore = mcScore;
293+
}
269294

270295
if (history) {
271296
if (i % saveStep == 1) {
272297
// Get the correct superposition again
273298
updateMultipleAlignment();
274299

300+
timeHistory.add(System.nanoTime()/1000000 - initialTime);
275301
lengthHistory.add(length);
276302
rmsdHistory.add(msa.getScore(MultipleAlignmentScorer.RMSD));
277-
scoreHistory.add(msa.getScore(MultipleAlignmentScorer.AVGTM_SCORE));
303+
tmScoreHistory.add(msa
304+
.getScore(MultipleAlignmentScorer.AVGTM_SCORE));
305+
mcScoreHistory.add(mcScore);
278306
}
279307
}
280308

281309
i++;
282310
}
311+
312+
// Use the optimal alignment of the trajectory
313+
block = optBlock;
314+
freePool = optFreePool;
315+
mcScore = optScore;
316+
283317
// Superimpose and calculate final scores
284318
updateMultipleAlignment();
285-
mcScore = MultipleAlignmentScorer.getMCScore(msa, Gopen, Gextend,
286-
dCutoff);
287319
msa.putScore(MultipleAlignmentScorer.MC_SCORE, mcScore);
288320

289321
// Save the history to the results folder of the symmetry project
@@ -327,8 +359,8 @@ private void updateMultipleAlignment() throws StructureException,
327359
* are no more gaps than the maximum allowed: Rmin.
328360
* <p>
329361
* There must be at least Rmin residues different than null in every
330-
* alignment column.In case there is a column with more gaps than allowed it
331-
* will be shrinked (moved to freePool).
362+
* alignment column. In case there is a column with more gaps than allowed
363+
* it will be shrinked (moved to freePool).
332364
*
333365
* @return true if any columns has been shrinked and false otherwise
334366
*/
@@ -805,15 +837,17 @@ private double probabilityFunction(double AS, int m, int maxIter) {
805837
private void saveHistory(String folder) throws IOException {
806838

807839
String name = msa.getStructureIdentifier(0).getIdentifier();
808-
FileWriter writer = new FileWriter(folder + name + "-symm_optimization.csv");
809-
writer.append("Structure,Step,Repeat Length,RMSD,TM-Score\n");
840+
FileWriter writer = new FileWriter(folder + name
841+
+ "-symm_opt.csv");
842+
writer.append("Step,Time,RepeatLength,RMSD,TMscore,MCscore\n");
810843

811844
for (int i = 0; i < lengthHistory.size(); i++) {
812-
writer.append(name + ",");
813845
writer.append(i * saveStep + ",");
846+
writer.append(timeHistory.get(i) + ",");
814847
writer.append(lengthHistory.get(i) + ",");
815848
writer.append(rmsdHistory.get(i) + ",");
816-
writer.append(scoreHistory.get(i) + "\n");
849+
writer.append(tmScoreHistory.get(i) + ",");
850+
writer.append(mcScoreHistory.get(i) + "\n");
817851
}
818852

819853
writer.flush();

0 commit comments

Comments
 (0)