1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
|
/**
* Author: Mark Larkin
*
* Copyright (c) 2007 Des Higgins, Julie Thompson and Toby Gibson.
*
* Changes:
* Mark 30-5-2007: Changed iterationOnTreeNode function as it was adding in extra gaps
* at the end of an alignment.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "Iteration.h"
#include "../alignment/ObjectiveScore.h"
#include "../general/utils.h"
#include "../general/userparams.h"
#include "../tree/TreeInterface.h"
#include "../clustalw_version.h"
#include "MSA.h"
namespace clustalw
{
bool Iteration::iterationOnTreeNode(int numSeqsProf1, int numSeqsProf2, int& prfLength1,
int& prfLength2, SeqArray* seqArray)
{
Alignment alignmentToIterate;
int numSeqsInProfiles = numSeqsProf1 + numSeqsProf2;
if(numSeqsInProfiles <= 2)
{
return false;
}
SeqArray profileSeqs;
profileSeqs.resize(numSeqsInProfiles + 1);
// Copy the SeqArray!
for (int j = 0; ((j < numSeqsProf1 + numSeqsProf2) &&
(j < (int)seqArray->size())); j++)
{
profileSeqs[j + 1].clear();
profileSeqs[j + 1].resize(prfLength1 + 1);
for (int i = 0; i < prfLength1 && i < (int)(*seqArray)[j].size(); i++)
{
profileSeqs[j + 1][i + 1] = (*seqArray)[j][i];
}
}
alignmentToIterate.addSequences(&profileSeqs);
//userParameters->setNumIterations(numSeqsInProfiles * 2);
bool changed = false;
changed = removeFirstIterate(&alignmentToIterate);
if(changed)
{
SeqArray* iteratedSeqs = alignmentToIterate.getSeqArrayForRealloc();
string aaCodes = userParameters->getAminoAcidCodes();
int newPrf1Length = 0, newPrf2Length = 0;
for (int j = 0; j < numSeqsProf1 + numSeqsProf2; j++)
{
if(j < numSeqsProf1)
{
if(alignmentToIterate.getSeqLength(j + 1) > newPrf1Length)
{
newPrf1Length = alignmentToIterate.getSeqLength(j + 1);
}
}
else if(j < numSeqsProf1 + numSeqsProf2)
{
if(alignmentToIterate.getSeqLength(j + 1) > newPrf2Length)
{
newPrf2Length = alignmentToIterate.getSeqLength(j + 1);
}
}
}
prfLength1 = newPrf1Length; // mark 30-5-2007
prfLength2 = newPrf2Length; // mark 30-5-2007
for (int j = 0; j < numSeqsProf1 + numSeqsProf2; j++)
{
// I need to recalculate the prfLength1 and prfLength2
(*seqArray)[j].clear();
(*seqArray)[j].assign((*iteratedSeqs)[j + 1].begin() + 1,
(*iteratedSeqs)[j + 1].end());
(*seqArray)[j].resize(prfLength1 + extraEndElemNum, 31);
(*seqArray)[j][prfLength1] = ENDALN;
}
}
return true;
}
void Iteration::printSeqArray(SeqArray* arrayToPrint)
{
cout << "HERE IS THE SEQARRAY\n";
// I need to use iterators for everything here.
SeqArray::iterator mainBeginIt = arrayToPrint->begin();
SeqArray::iterator mainEndIt = arrayToPrint->end();
vector<int>::iterator begin, end;
string aaCodes = userParameters->getAminoAcidCodes();
for(; mainBeginIt != mainEndIt; mainBeginIt++)
{
if(mainBeginIt->size() > 0)
{
begin = mainBeginIt->begin() + 1;
end = mainBeginIt->end();
for(; begin != end; begin++)
{
if(*begin < (int)aaCodes.size())
{
cout << aaCodes[*begin];
}
else
{
cout << "-";
}
}
cout << "\n";
}
}
cout << "\n\n";
}
/**
* The function removeFirstIterate is used to perform the remove first iteration
* strategy on the finished alignment. It optimises the score give in alignScore.
* For iter = 1 to numIterations
* For seq i = 1 to numSeqs
* remove seq i
* if either of the profiles has all gaps, remove this column.
* realign using profileAlign
* if its better, keep it. If its not better, dont keep it.
* @param alnPtr The alignment object.
* @return true if it has been successful, false if it has not been successful.
*/
bool Iteration::removeFirstIterate(Alignment* alnPtr)
{
if(!alnPtr)
{
return false;
}
string p1TreeName;
p1TreeName = "";
string p2TreeName;
int nSeqs = alnPtr->getNumSeqs();
if(nSeqs <= 2)
{
return false;
}
DistMatrix distMat;
distMat.ResizeRect(nSeqs + 1);
ObjectiveScore scoreObj;
int iterate = userParameters->getDoRemoveFirstIteration();
userParameters->setDoRemoveFirstIteration(NONE);
double firstScore = scoreObj.getScore(alnPtr);
//cout << "firstScore = " << firstScore << "\n";
double score = 0;
double bestScore = firstScore;
double dscore;
int count;
bool scoreImproved = false;
bool scoreImprovedAnyIteration = false;
int prof1NumSeqs = 1;
// This will be used for removing gaps!!!
vector<int> profile1;
vector<int> profile2;
profile1.resize(nSeqs + 1, 0);
profile1[1] = 1;
profile2.resize(nSeqs + 1, 1);
profile2[0] = 0;
profile2[1] = 0;
vector<int> prof1Weight, prof2Weight;
int iterations = userParameters->getNumIterations();
//cout << "Max num iterations = " << iterations << "\n";
Alignment bestAlignSoFar;
TreeInterface tree;
// One iteration consists of removing each of the sequences, reseting all the gap
// only columns. If the score is better, the new alignment is kept.
for(int n = 1; n <= iterations; n++)
{
scoreImproved = false;
cout << "ITERATION " << n << " OF " << iterations << "\n";
for(int i = 1; i <= nSeqs; i++)
{
vector<Sequence> seqVector;
Alignment iterateAlign = *alnPtr;
iterateAlign.setProfile1NumSeqs(1);
// We remove the sequence i from the profile, and paste into the first position
// This is to make it easy to do the profile alignment.
vector<int> selected;
selected.resize(nSeqs + 1, 0);
selected[i] = 1;
seqVector = iterateAlign.cutSelectedSequencesFromAlignment(&selected);
iterateAlign.pasteSequencesIntoPosition(&seqVector, 0);
// Remove any gap only columns
iterateAlign.removeGapOnlyColsFromSelectedSeqs(&profile1);
iterateAlign.removeGapOnlyColsFromSelectedSeqs(&profile2);
// Calculate a simple distance matrix.
if(nSeqs - 1 >= 2)
{
for (int i = 1; i <= nSeqs; i++)
{
for (int j = i + 1; j <= nSeqs; j++)
{
dscore = iterateAlign.countid(i, j);
distMat(i, j) = (100.0 - dscore)/100.0;
}
}
/* temporary tree file
*
* can't use the safer mkstemp function here, because
* we just pass down the filename :(
*/
char buffer[L_tmpnam];
tmpnam (buffer);
p2TreeName = buffer + string(".dnd");
// should test here if file is writable
}
bool success = false;
prof1Weight.clear();
prof1Weight.resize(prof1NumSeqs);
prof2Weight.clear();
prof2Weight.resize(nSeqs);
tree.getWeightsForProfileAlign(&iterateAlign, &distMat, &p1TreeName, &prof1Weight,
&p2TreeName, &prof2Weight, nSeqs, prof1NumSeqs,
false, false, &success);
remove(p2TreeName.c_str());
if(!success)
{
/* returning false only means alignment hasn't
* changed, but here getWeightsForProfileAlign failed,
* most likely because p2TreeName couldn't be read. an
* error will be printed to console. clustalw should
* then exit, FIXME: clustalx users have to sit
* through all error messages until someone
* implements a way to return an exit code and react
* appropriately
*/
// does anyone know how to use
// (userParameters->getMenuFlag() ||
// !userParameters->getInteractive() instead?
char buf[1024];
utilityObject->myname(buf);
if (strcasecmp(buf, "clustalw")==0) {
exit(EXIT_FAILURE);
} else {
// the next two lines were here before the exit
// was added. keeping it for clustalx although it
// doesnt seem to make any sens
userParameters->setDoRemoveFirstIteration(iterate);
return false;
}
}
MSA* msaObj = new MSA();
iterateAlign.resetProfile1();
iterateAlign.resetProfile2();
// Do the profile alignment.
count = msaObj->doProfileAlign(&iterateAlign, &distMat,
&prof1Weight, &prof2Weight);
delete msaObj;
// Check if its better
score = scoreObj.getScore(&iterateAlign);
iterateAlign.setProfile1NumSeqs(0);
if(score < bestScore) // Might be a problem with this.
{
//cout << "**********************************************\n";
//cout << "***** Better score found using iteration *****\n";
//cout << "**********************************************\n";
bestScore = score;
bestAlignSoFar = iterateAlign;
scoreImproved = true;
scoreImprovedAnyIteration = true;
}
distMat.clearArray();
distMat.ResizeRect(nSeqs + 1);
}
if(scoreImproved == false)
{
cout << "Score was not improved in last iteration. Exiting...\n";
break;
}
}
//
// NOTE if we have improved it, then we need to update the sequences in alnPtr
// 1) get the unique id of seq i
// 2) get the sequence from new object using id
// 3) update the sequence in alnPtr
//
if(scoreImprovedAnyIteration) // If we need to update the alnPtr object.
{
cout << "Iteration improved Align score: " << bestScore << "\n";
int seqId;
const vector<int>* improvedSeq;
for(int i = 1; i <= nSeqs; i++) // For each seq in alnPtr
{
seqId = alnPtr->getUniqueId(i);
improvedSeq = bestAlignSoFar.getSequenceFromUniqueId(seqId);
alnPtr->updateSequence(i, improvedSeq);
}
}
cout << "FINAL score: " << bestScore << "\n";
userParameters->setDoRemoveFirstIteration(iterate);
return true; // It was successful.
}
}
|