1 /////////////////////////////////////////////////////////////////
4 // Main routines for MXSCARNA program.
5 /////////////////////////////////////////////////////////////////
8 #include "SafeVector.h"
9 #include "MultiSequence.h"
11 #include "ScoreType.h"
12 #include "ProbabilisticModel.h"
13 #include "EvolutionaryTree.h"
14 #include "SparseMatrix.h"
15 #include "BPPMatrix.hpp"
16 #include "StemCandidate.hpp"
17 #include "Globaldp.hpp"
19 #include "AlifoldMEA.h"
33 //#include "RfoldWrapper.hpp"
34 //static RFOLD::Rfold folder;
36 using namespace::MXSCARNA;
38 string parametersInputFilename = "";
39 string parametersOutputFilename = "no training";
40 string annotationFilename = "";
41 string weboutputFileName = "";
45 bool enableTraining = false;
46 bool enableVerbose = false;
47 bool enableAllPairs = false;
48 bool enableAnnotation = false;
49 bool enableViterbi = false;
50 bool enableClustalWOutput = false;
51 bool enableTrainEmissions = false;
52 bool enableAlignOrder = false;
53 bool enableWebOutput = false;
54 bool enableStockholmOutput = false;
55 bool enableMXSCARNAOutput = false;
56 bool enableMcCaskillMEAMode = false;
57 char bppmode = 's'; // by katoh
58 int numConsistencyReps = 2;
59 int numPreTrainingReps = 0;
60 int numIterativeRefinementReps = 100;
61 int scsLength = SCSLENGTH;
63 float gapOpenPenalty = 0;
64 float gapContinuePenalty = 0;
65 float threshhold = 1.0;
66 float BaseProbThreshold = BASEPROBTHRESHOLD;
67 float BasePairConst = BASEPAIRCONST;
68 int BandWidth = BANDWIDTH;
70 SafeVector<string> sequenceNames;
72 VF initDistrib (NumMatrixTypes);
73 VF gapOpen (2*NumInsertStates);
74 VF gapExtend (2*NumInsertStates);
75 VVF emitPairs (256, VF (256, 1e-10));
76 VF emitSingle (256, 1e-5);
78 string alphabet = alphabetDefault;
80 string *ssCons = NULL;
82 const int MIN_PRETRAINING_REPS = 0;
83 const int MAX_PRETRAINING_REPS = 20;
84 const int MIN_CONSISTENCY_REPS = 0;
85 const int MAX_CONSISTENCY_REPS = 5;
86 const int MIN_ITERATIVE_REFINEMENT_REPS = 0;
87 const int MAX_ITERATIVE_REFINEMENT_REPS = 1000;
89 /////////////////////////////////////////////////////////////////
90 // Function prototypes
91 /////////////////////////////////////////////////////////////////
94 void PrintParameters (const char *message, const VF &initDistrib, const VF &gapOpen,
95 const VF &gapExtend, const VVF &emitPairs, const VF &emitSingle, const char *filename);
96 MultiSequence *DoAlign (MultiSequence *sequence, const ProbabilisticModel &model, VF &initDistrib, VF &gapOpen, VF &gapExtend, VVF &emitPairs, VF &emitSingle);
97 SafeVector<string> ParseParams (int argc, char **argv);
98 void ReadParameters ();
99 MultiSequence *ComputeFinalAlignment (const TreeNode *tree, MultiSequence *sequences,
100 const SafeVector<SafeVector<SparseMatrix *> > &sparseMatrices,
101 const ProbabilisticModel &model,
102 SafeVector<BPPMatrix*> &BPPMatrices);
103 MultiSequence *AlignAlignments (MultiSequence *align1, MultiSequence *align2,
104 const SafeVector<SafeVector<SparseMatrix *> > &sparseMatrices,
105 const ProbabilisticModel &model, SafeVector<BPPMatrix*> &BPPMatrices, float identity);
106 SafeVector<SafeVector<SparseMatrix *> > DoRelaxation (MultiSequence *sequences,
107 SafeVector<SafeVector<SparseMatrix *> > &sparseMatrices);
108 void Relax (SparseMatrix *matXZ, SparseMatrix *matZY, VF &posterior);
109 void Relax1 (SparseMatrix *matXZ, SparseMatrix *matZY, VF &posterior);
110 void DoBasePairProbabilityRelaxation (MultiSequence *sequences,
111 SafeVector<SafeVector<SparseMatrix *> > &sparseMatrices,
112 SafeVector<BPPMatrix*> &BPPMatrices);
113 set<int> GetSubtree (const TreeNode *tree);
114 void TreeBasedBiPartitioning (const SafeVector<SafeVector<SparseMatrix *> > &sparseMatrices,
115 const ProbabilisticModel &model, MultiSequence* &alignment,
116 const TreeNode *tree, SafeVector<BPPMatrix*> &BPPMatrices);
117 void DoIterativeRefinement (const SafeVector<SafeVector<SparseMatrix *> > &sparseMatrices,
118 const ProbabilisticModel &model, MultiSequence* &alignment);
119 void WriteAnnotation (MultiSequence *alignment,
120 const SafeVector<SafeVector<SparseMatrix *> > &sparseMatrices);
121 int ComputeScore (const SafeVector<pair<int, int> > &active,
122 const SafeVector<SafeVector<SparseMatrix *> > &sparseMatrices);
123 std::vector<StemCandidate>* seq2scs(MultiSequence *Sequences, SafeVector<BPPMatrix*> &BPPMatrices, int BandWidth);
124 void removeConflicts(std::vector<StemCandidate> *pscs1, std::vector<StemCandidate> *pscs2, std::vector<int> *matchPSCS1, std::vector<int> *matchPSCS2);
132 /////////////////////////////////////////////////////////////////
135 // Calls all initialization routines and runs the MXSCARNA
137 /////////////////////////////////////////////////////////////////
140 int main (int argc, char **argv){
142 // print MXSCARNA heading
145 // parse program parameters
146 sequenceNames = ParseParams (argc, argv);
148 PrintParameters ("Using parameter set:", initDistrib, gapOpen, gapExtend, emitPairs, emitSingle, NULL);
150 // now, we'll process all the files given as input. If we are given
151 // several filenames as input, then we'll load all of those sequences
152 // simultaneously, as long as we're not training. On the other hand,
153 // if we are training, then we'll treat each file as a separate
156 if (enableMcCaskillMEAMode) {
157 MultiSequence *sequences = new MultiSequence(); assert (sequences);
158 for (int i = 0; i < (int) sequenceNames.size(); i++){
159 cerr << "Loading sequence file: " << sequenceNames[i] << endl;
160 sequences->LoadMFA (sequenceNames[i], true);
163 const int numSeqs = sequences->GetNumSequences();
164 SafeVector<BPPMatrix*> BPPMatrices;
166 // compute the base pairing matrices for each sequences
167 for(int i = 0; i < numSeqs; i++) {
168 Sequence *tmpSeq = sequences->GetSequence(i);
169 string seq = tmpSeq->GetString();
170 int n_seq = tmpSeq->GetLength();
171 BPPMatrix *bppmat = new BPPMatrix(bppmode, seq, n_seq); // modified by katoh
172 BPPMatrices.push_back(bppmat);
174 if (bppmode=='w') exit( 0 );
176 AlifoldMEA alifold(sequences, BPPMatrices, BasePairConst);
178 ssCons = alifold.getSScons();
180 if (enableStockholmOutput) {
181 sequences->WriteSTOCKHOLM (cout, ssCons);
183 else if (enableMXSCARNAOutput){
184 sequences->WriteMXSCARNA (cout, ssCons);
187 sequences->WriteMFA (cout, ssCons);
192 // if we are training
193 else if (enableTraining){
195 // build new model for aligning
196 ProbabilisticModel model (initDistrib, gapOpen, gapExtend, emitPairs, emitSingle);
198 // prepare to average parameters
199 for (int i = 0; i < (int) initDistrib.size(); i++) initDistrib[i] = 0;
200 for (int i = 0; i < (int) gapOpen.size(); i++) gapOpen[i] = 0;
201 for (int i = 0; i < (int) gapExtend.size(); i++) gapExtend[i] = 0;
202 if (enableTrainEmissions){
203 for (int i = 0; i < (int) emitPairs.size(); i++)
204 for (int j = 0; j < (int) emitPairs[i].size(); j++) emitPairs[i][j] = 0;
205 for (int i = 0; i < (int) emitSingle.size(); i++) emitSingle[i] = 0;
208 // align each file individually
209 for (int i = 0; i < (int) sequenceNames.size(); i++){
211 VF thisInitDistrib (NumMatrixTypes);
212 VF thisGapOpen (2*NumInsertStates);
213 VF thisGapExtend (2*NumInsertStates);
214 VVF thisEmitPairs (256, VF (256, 1e-10));
215 VF thisEmitSingle (256, 1e-5);
217 // load sequence file
218 MultiSequence *sequences = new MultiSequence(); assert (sequences);
219 cerr << "Loading sequence file: " << sequenceNames[i] << endl;
220 sequences->LoadMFA (sequenceNames[i], true);
223 DoAlign (sequences, model, thisInitDistrib, thisGapOpen, thisGapExtend, thisEmitPairs, thisEmitSingle);
225 // add in contribution of the derived parameters
226 for (int i = 0; i < (int) initDistrib.size(); i++) initDistrib[i] += thisInitDistrib[i];
227 for (int i = 0; i < (int) gapOpen.size(); i++) gapOpen[i] += thisGapOpen[i];
228 for (int i = 0; i < (int) gapExtend.size(); i++) gapExtend[i] += thisGapExtend[i];
229 if (enableTrainEmissions){
230 for (int i = 0; i < (int) emitPairs.size(); i++)
231 for (int j = 0; j < (int) emitPairs[i].size(); j++) emitPairs[i][j] += thisEmitPairs[i][j];
232 for (int i = 0; i < (int) emitSingle.size(); i++) emitSingle[i] += thisEmitSingle[i];
238 // compute new parameters and print them out
239 for (int i = 0; i < (int) initDistrib.size(); i++) initDistrib[i] /= (int) sequenceNames.size();
240 for (int i = 0; i < (int) gapOpen.size(); i++) gapOpen[i] /= (int) sequenceNames.size();
241 for (int i = 0; i < (int) gapExtend.size(); i++) gapExtend[i] /= (int) sequenceNames.size();
242 if (enableTrainEmissions){
243 for (int i = 0; i < (int) emitPairs.size(); i++)
244 for (int j = 0; j < (int) emitPairs[i].size(); j++) emitPairs[i][j] /= (int) sequenceNames.size();
245 for (int i = 0; i < (int) emitSingle.size(); i++) emitSingle[i] /= sequenceNames.size();
248 PrintParameters ("Trained parameter set:",
249 initDistrib, gapOpen, gapExtend, emitPairs, emitSingle,
250 parametersOutputFilename.c_str());
253 // if we are not training, we must simply want to align some sequences
255 // load all files together
256 MultiSequence *sequences = new MultiSequence(); assert (sequences);
257 for (int i = 0; i < (int) sequenceNames.size(); i++){
258 cerr << "Loading sequence file: " << sequenceNames[i] << endl;
260 sequences->LoadMFA (sequenceNames[i], true);
263 // do all "pre-training" repetitions first
265 for (int ct = 0; ct < numPreTrainingReps; ct++){
266 enableTraining = true;
268 // build new model for aligning
269 ProbabilisticModel model (initDistrib, gapOpen, gapExtend,
270 emitPairs, emitSingle);
272 // do initial alignments
273 DoAlign (sequences, model, initDistrib, gapOpen, gapExtend, emitPairs, emitSingle);
275 // print new parameters
276 PrintParameters ("Recomputed parameter set:", initDistrib, gapOpen, gapExtend, emitPairs, emitSingle, NULL);
278 enableTraining = false;
281 // now, we can perform the alignments and write them out
282 if (enableWebOutput) {
283 outputFile = new ofstream(weboutputFileName.c_str());
285 cerr << "cannot open output file." << weboutputFileName << endl;
288 *outputFile << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" << endl;
289 *outputFile << "<result>" << endl;
291 MultiSequence *alignment = DoAlign (sequences,
292 ProbabilisticModel (initDistrib, gapOpen, gapExtend, emitPairs, emitSingle),
293 initDistrib, gapOpen, gapExtend, emitPairs, emitSingle);
296 if (!enableAllPairs){
297 if (enableClustalWOutput) {
298 alignment->WriteALN (cout);
300 else if (enableWebOutput) {
301 alignment->WriteWEB (*outputFile, ssCons);
302 // computeStructureWithAlifold ();
304 else if (enableStockholmOutput) {
305 alignment->WriteSTOCKHOLM (cout, ssCons);
307 else if (enableMXSCARNAOutput) {
308 alignment->WriteMXSCARNA (cout, ssCons);
311 alignment->WriteMFA (cout, ssCons);
315 if (enableWebOutput) {
316 *outputFile << "</result>" << endl;
327 /////////////////////////////////////////////////////////////////
330 // Prints heading for PROBCONS program.
331 /////////////////////////////////////////////////////////////////
333 void PrintHeading (){
335 << "Multiplex SCARNA"<< endl
336 << "version " << VERSION << " - align multiple RNA sequences and print to standard output" << endl
337 << "Written by Yasuo Tabei" << endl
341 /////////////////////////////////////////////////////////////////
344 // Prints PROBCONS parameters to STDERR. If a filename is
345 // specified, then the parameters are also written to the file.
346 /////////////////////////////////////////////////////////////////
348 void PrintParameters (const char *message, const VF &initDistrib, const VF &gapOpen,
349 const VF &gapExtend, const VVF &emitPairs, const VF &emitSingle, const char *filename){
351 // print parameters to the screen
352 cerr << message << endl
353 << " initDistrib[] = { ";
354 for (int i = 0; i < NumMatrixTypes; i++) cerr << setprecision (10) << initDistrib[i] << " ";
356 << " gapOpen[] = { ";
357 for (int i = 0; i < NumInsertStates*2; i++) cerr << setprecision (10) << gapOpen[i] << " ";
359 << " gapExtend[] = { ";
360 for (int i = 0; i < NumInsertStates*2; i++) cerr << setprecision (10) << gapExtend[i] << " ";
365 for (int i = 0; i < 5; i++){
366 for (int j = 0; j <= i; j++){
367 cerr << emitPairs[(unsigned char) alphabet[i]][(unsigned char) alphabet[j]] << " ";
372 // if a file name is specified
375 // attempt to open the file for writing
376 FILE *file = fopen (filename, "w");
378 cerr << "ERROR: Unable to write parameter file: " << filename << endl;
382 // if successful, then write the parameters to the file
383 for (int i = 0; i < NumMatrixTypes; i++) fprintf (file, "%.10f ", initDistrib[i]); fprintf (file, "\n");
384 for (int i = 0; i < 2*NumInsertStates; i++) fprintf (file, "%.10f ", gapOpen[i]); fprintf (file, "\n");
385 for (int i = 0; i < 2*NumInsertStates; i++) fprintf (file, "%.10f ", gapExtend[i]); fprintf (file, "\n");
386 fprintf (file, "%s\n", alphabet.c_str());
387 for (int i = 0; i < (int) alphabet.size(); i++){
388 for (int j = 0; j <= i; j++)
389 fprintf (file, "%.10f ", emitPairs[(unsigned char) alphabet[i]][(unsigned char) alphabet[j]]);
390 fprintf (file, "\n");
392 for (int i = 0; i < (int) alphabet.size(); i++)
393 fprintf (file, "%.10f ", emitSingle[(unsigned char) alphabet[i]]);
394 fprintf (file, "\n");
399 /////////////////////////////////////////////////////////////////
402 // First computes all pairwise posterior probability matrices.
403 // Then, computes new parameters if training, or a final
404 // alignment, otherwise.
405 /////////////////////////////////////////////////////////////////
406 MultiSequence *DoAlign (MultiSequence *sequences, const ProbabilisticModel &model, VF &initDistrib, VF &gapOpen, VF &gapExtend, VVF &emitPairs, VF &emitSingle){
411 const int numSeqs = sequences->GetNumSequences();
412 VVF distances (numSeqs, VF (numSeqs, 0));
413 VVF identities (numSeqs, VF (numSeqs, 0));
414 SafeVector<SafeVector<SparseMatrix *> > sparseMatrices (numSeqs, SafeVector<SparseMatrix *>(numSeqs, NULL));
416 SafeVector<BPPMatrix*> BPPMatrices;
418 for(int i = 0; i < numSeqs; i++) {
419 Sequence *tmpSeq = sequences->GetSequence(i);
420 string seq = tmpSeq->GetString();
421 int n_seq = tmpSeq->GetLength();
422 BPPMatrix *bppmat = new BPPMatrix(bppmode, seq, n_seq, BASEPROBTHRESHOLD); // modified by katoh
423 BPPMatrices.push_back(bppmat);
427 // prepare to average parameters
428 for (int i = 0; i < (int) initDistrib.size(); i++) initDistrib[i] = 0;
429 for (int i = 0; i < (int) gapOpen.size(); i++) gapOpen[i] = 0;
430 for (int i = 0; i < (int) gapExtend.size(); i++) gapExtend[i] = 0;
431 if (enableTrainEmissions){
432 for (int i = 0; i < (int) emitPairs.size(); i++)
433 for (int j = 0; j < (int) emitPairs[i].size(); j++) emitPairs[i][j] = 0;
434 for (int i = 0; i < (int) emitSingle.size(); i++) emitSingle[i] = 0;
438 // skip posterior calculations if we just want to do Viterbi alignments
441 // do all pairwise alignments for posterior probability matrices
442 for (int a = 0; a < numSeqs-1; a++){
443 for (int b = a+1; b < numSeqs; b++){
444 Sequence *seq1 = sequences->GetSequence (a);
445 Sequence *seq2 = sequences->GetSequence (b);
449 cerr << "Computing posterior matrix: (" << a+1 << ") " << seq1->GetHeader() << " vs. "
450 << "(" << b+1 << ") " << seq2->GetHeader() << " -- ";
452 // compute forward and backward probabilities
453 VF *forward = model.ComputeForwardMatrix (seq1, seq2); assert (forward);
454 VF *backward = model.ComputeBackwardMatrix (seq1, seq2); assert (backward);
456 // if we are training, then we'll simply want to compute the
457 // expected counts for each region within the matrix separately;
458 // otherwise, we'll need to put all of the regions together and
459 // assemble a posterior probability match matrix
461 // so, if we're training
464 // compute new parameters
465 VF thisInitDistrib (NumMatrixTypes);
466 VF thisGapOpen (2*NumInsertStates);
467 VF thisGapExtend (2*NumInsertStates);
468 VVF thisEmitPairs (256, VF (256, 1e-10));
469 VF thisEmitSingle (256, 1e-5);
471 model.ComputeNewParameters (seq1, seq2, *forward, *backward, thisInitDistrib, thisGapOpen, thisGapExtend, thisEmitPairs, thisEmitSingle, enableTrainEmissions);
473 // add in contribution of the derived parameters
474 for (int i = 0; i < (int) initDistrib.size(); i++) initDistrib[i] += thisInitDistrib[i];
475 for (int i = 0; i < (int) gapOpen.size(); i++) gapOpen[i] += thisGapOpen[i];
476 for (int i = 0; i < (int) gapExtend.size(); i++) gapExtend[i] += thisGapExtend[i];
477 if (enableTrainEmissions){
478 for (int i = 0; i < (int) emitPairs.size(); i++)
479 for (int j = 0; j < (int) emitPairs[i].size(); j++) emitPairs[i][j] += thisEmitPairs[i][j];
480 for (int i = 0; i < (int) emitSingle.size(); i++) emitSingle[i] += thisEmitSingle[i];
483 // let us know that we're done.
484 if (enableVerbose) cerr << "done." << endl;
489 // compute posterior probability matrix
490 VF *posterior = model.ComputePosteriorMatrix (seq1, seq2, *forward, *backward); assert (posterior);
492 // compute sparse representations
493 sparseMatrices[a][b] = new SparseMatrix (seq1->GetLength(), seq2->GetLength(), *posterior);
494 sparseMatrices[b][a] = NULL;
496 if (!enableAllPairs){
497 // perform the pairwise sequence alignment
498 pair<SafeVector<char> *, float> alignment = model.ComputeAlignment (seq1->GetLength(),
502 Sequence *tmpSeq1 = seq1->AddGaps (alignment.first, 'X');
503 Sequence *tmpSeq2 = seq2->AddGaps (alignment.first, 'Y');
505 // compute sequence identity for each pair of sequenceses
506 int length = tmpSeq1->GetLength();
508 int misMatchCount = 0;
509 for (int k = 1; k <= length; k++) {
510 int ch1 = tmpSeq1->GetPosition(k);
511 int ch2 = tmpSeq2->GetPosition(k);
512 if (ch1 == ch2 && ch1 != '-' && ch2 != '-') { ++matchCount; }
513 else if (ch1 != ch2 && ch1 != '-' && ch2 != '-') { ++misMatchCount; }
516 identities[a][b] = identities[b][a] = (float)matchCount/(float)(matchCount + misMatchCount);
518 // compute "expected accuracy" distance for evolutionary tree computation
519 float distance = alignment.second / min (seq1->GetLength(), seq2->GetLength());
520 distances[a][b] = distances[b][a] = distance;
523 cerr << setprecision (10) << distance << endl;
525 delete alignment.first;
528 // let us know that we're done.
529 if (enableVerbose) cerr << "done." << endl;
542 // now average out parameters derived
544 // compute new parameters
545 for (int i = 0; i < (int) initDistrib.size(); i++) initDistrib[i] /= numSeqs * (numSeqs - 1) / 2;
546 for (int i = 0; i < (int) gapOpen.size(); i++) gapOpen[i] /= numSeqs * (numSeqs - 1) / 2;
547 for (int i = 0; i < (int) gapExtend.size(); i++) gapExtend[i] /= numSeqs * (numSeqs - 1) / 2;
549 if (enableTrainEmissions){
550 for (int i = 0; i < (int) emitPairs.size(); i++)
551 for (int j = 0; j < (int) emitPairs[i].size(); j++) emitPairs[i][j] /= numSeqs * (numSeqs - 1) / 2;
552 for (int i = 0; i < (int) emitSingle.size(); i++) emitSingle[i] /= numSeqs * (numSeqs - 1) / 2;
556 // see if we still want to do some alignments
561 // perform the consistency transformation the desired number of times
562 for (int r = 0; r < numConsistencyReps; r++){
563 SafeVector<SafeVector<SparseMatrix *> > newSparseMatrices = DoRelaxation (sequences, sparseMatrices);
565 // now replace the old posterior matrices
566 for (int i = 0; i < numSeqs; i++){
567 for (int j = 0; j < numSeqs; j++){
568 delete sparseMatrices[i][j];
569 sparseMatrices[i][j] = newSparseMatrices[i][j];
574 // for (int r = 0; r < 1; r++)
575 // DoBasePairProbabilityRelaxation(sequences, sparseMatrices, BPPMatrices);
579 MultiSequence *finalAlignment = NULL;
582 for (int a = 0; a < numSeqs-1; a++){
583 for (int b = a+1; b < numSeqs; b++){
584 Sequence *seq1 = sequences->GetSequence (a);
585 Sequence *seq2 = sequences->GetSequence (b);
588 cerr << "Performing pairwise alignment: (" << a+1 << ") " << seq1->GetHeader() << " vs. "
589 << "(" << b+1 << ") " << seq2->GetHeader() << " -- ";
592 // perform the pairwise sequence alignment
593 pair<SafeVector<char> *, float> alignment;
595 alignment = model.ComputeViterbiAlignment (seq1, seq2);
598 // build posterior matrix
599 VF *posterior = sparseMatrices[a][b]->GetPosterior(); assert (posterior);
600 int length = (seq1->GetLength() + 1) * (seq2->GetLength() + 1);
601 for (int i = 0; i < length; i++) (*posterior)[i] -= cutoff;
603 alignment = model.ComputeAlignment (seq1->GetLength(), seq2->GetLength(), *posterior);
608 // write pairwise alignments
609 string name = seq1->GetHeader() + "-" + seq2->GetHeader() + (enableClustalWOutput ? ".aln" : ".fasta");
610 ofstream outfile (name.c_str());
612 MultiSequence *result = new MultiSequence();
613 result->AddSequence (seq1->AddGaps(alignment.first, 'X'));
614 result->AddSequence (seq2->AddGaps(alignment.first, 'Y'));
615 result->WriteMFAseq (outfile); // by katoh
619 delete alignment.first;
625 // now if we still need to do a final multiple alignment
631 // compute the evolutionary tree
632 TreeNode *tree = TreeNode::ComputeTree (distances, identities);
634 if (enableWebOutput) {
635 *outputFile << "<tree>" << endl;
636 tree->Print (*outputFile, sequences);
637 *outputFile << "</tree>" << endl;
640 tree->Print (cerr, sequences);
643 // make the final alignment
644 finalAlignment = ComputeFinalAlignment (tree, sequences, sparseMatrices, model, BPPMatrices);
647 if (enableAnnotation){
648 WriteAnnotation (finalAlignment, sparseMatrices);
655 // delete sparse matrices
656 for (int a = 0; a < numSeqs-1; a++){
657 for (int b = a+1; b < numSeqs; b++){
658 delete sparseMatrices[a][b];
659 delete sparseMatrices[b][a];
664 //AlifoldMEA alifold(finalAlignment, BPPMatrices, BasePairConst);
666 //ssCons = alifold.getSScons();
668 return finalAlignment;
675 /////////////////////////////////////////////////////////////////
678 // Attempts to parse an integer from the character string given.
679 // Returns true only if no parsing error occurs.
680 /////////////////////////////////////////////////////////////////
682 bool GetInteger (char *data, int *val){
689 retVal = strtol (data, &endPtr, 0);
690 if (retVal == 0 && (errno != 0 || data == endPtr)) return false;
691 if (errno != 0 && (retVal == LONG_MAX || retVal == LONG_MIN)) return false;
692 if (retVal < (long) INT_MIN || retVal > (long) INT_MAX) return false;
697 /////////////////////////////////////////////////////////////////
700 // Attempts to parse a float from the character string given.
701 // Returns true only if no parsing error occurs.
702 /////////////////////////////////////////////////////////////////
704 bool GetFloat (char *data, float *val){
711 retVal = strtod (data, &endPtr);
712 if (retVal == 0 && (errno != 0 || data == endPtr)) return false;
713 if (errno != 0 && (retVal >= 1000000.0 || retVal <= -1000000.0)) return false;
714 *val = (float) retVal;
718 /////////////////////////////////////////////////////////////////
721 // Parse all command-line options.
722 /////////////////////////////////////////////////////////////////
724 SafeVector<string> ParseParams (int argc, char **argv){
728 cerr << "MXSCARNA comes with ABSOLUTELY NO WARRANTY. This is free software, and" << endl
729 << "you are welcome to redistribute it under certain conditions. See the" << endl
730 << "file COPYING.txt for details." << endl
733 << " mxscarna [OPTION]... [MFAFILE]..." << endl
735 << "Description:" << endl
736 << " Align sequences in MFAFILE(s) and print result to standard output" << endl
738 << " -clustalw" << endl
739 << " use CLUSTALW output format instead of MFA" << endl
741 << " -stockholm" << endl
742 << " use STOCKHOLM output format instead of MFA" << endl
744 << " -mxscarna" << endl
745 << " use MXSCARNA output format instead of MFA" << endl
747 << " -weboutput /<output_path>/<outputfilename>" << endl
748 << " use web output format" << endl
750 << " -c, --consistency REPS" << endl
751 << " use " << MIN_CONSISTENCY_REPS << " <= REPS <= " << MAX_CONSISTENCY_REPS
752 << " (default: " << numConsistencyReps << ") passes of consistency transformation" << endl
754 << " -ir, --iterative-refinement REPS" << endl
755 << " use " << MIN_ITERATIVE_REFINEMENT_REPS << " <= REPS <= " << MAX_ITERATIVE_REFINEMENT_REPS
756 << " (default: " << numIterativeRefinementReps << ") passes of iterative-refinement" << endl
758 << " -pre, --pre-training REPS" << endl
759 << " use " << MIN_PRETRAINING_REPS << " <= REPS <= " << MAX_PRETRAINING_REPS
760 << " (default: " << numPreTrainingReps << ") rounds of pretraining" << endl
763 << " generate all-pairs pairwise alignments" << endl
765 << " -viterbi" << endl
766 << " use Viterbi algorithm to generate all pairs (automatically enables -pairs)" << endl
768 << " -v, --verbose" << endl
769 << " report progress while aligning (default: " << (enableVerbose ? "on" : "off") << ")" << endl
771 << " -annot FILENAME" << endl
772 << " write annotation for multiple alignment to FILENAME" << endl
774 << " -t, --train FILENAME" << endl
775 << " compute EM transition probabilities, store in FILENAME (default: "
776 << parametersOutputFilename << ")" << endl
778 << " -e, --emissions" << endl
779 << " also reestimate emission probabilities (default: "
780 << (enableTrainEmissions ? "on" : "off") << ")" << endl
782 << " -p, --paramfile FILENAME" << endl
783 << " read parameters from FILENAME (default: "
784 << parametersInputFilename << ")" << endl
786 << " -a, --alignment-order" << endl
787 << " print sequences in alignment order rather than input order (default: "
788 << (enableAlignOrder ? "on" : "off") << ")" << endl
790 << " -s THRESHOLD" << endl
791 << " the threshold of SCS alignment" << endl
793 << " In default, for less than " << threshhold << ", the SCS aligment is applied. " << endl
794 << " -l SCSLENGTH" << endl
795 << " the length of stem candidates " << SCSLENGTH << endl
797 << " -b BASEPROBTRHESHHOLD" << endl
798 << " the threshold of base pairing probability " << BASEPROBTHRESHOLD << endl
800 << " -m, --mccaskillmea" << endl
801 << " McCaskill MEA MODE: input the clustalw format file and output the secondary structure predicted by McCaskill MEA" << endl
803 << " -g BASEPAIRSCORECONST" << endl
804 << " the control parameter of the prediction of base pairs, default:" << BasePairConst << endl
806 << " -w BANDWIDTH" << endl
807 << " the control parameter of the distance of stem candidates, default:" << BANDWIDTH << endl
811 // << " -go, --gap-open VALUE" << endl
812 // << " gap opening penalty of VALUE <= 0 (default: " << gapOpenPenalty << ")" << endl
814 // << " -ge, --gap-extension VALUE" << endl
815 // << " gap extension penalty of VALUE <= 0 (default: " << gapContinuePenalty << ")" << endl
817 // << " -co, --cutoff CUTOFF" << endl
818 // << " subtract 0 <= CUTOFF <= 1 (default: " << cutoff << ") from all posterior values before final alignment" << endl
824 SafeVector<string> sequenceNames;
828 for (int i = 1; i < argc; i++){
829 if (argv[i][0] == '-'){
832 if (!strcmp (argv[i], "-t") || !strcmp (argv[i], "--train")){
833 enableTraining = true;
835 parametersOutputFilename = string (argv[++i]);
837 cerr << "ERROR: Filename expected for option " << argv[i] << endl;
843 else if (!strcmp (argv[i], "-e") || !strcmp (argv[i], "--emissions")){
844 enableTrainEmissions = true;
848 else if (!strcmp (argv[i], "-p") || !strcmp (argv[i], "--paramfile")){
850 parametersInputFilename = string (argv[++i]);
852 cerr << "ERROR: Filename expected for option " << argv[i] << endl;
856 else if (! strcmp (argv[i], "-s")) {
858 if (!GetFloat (argv[++i], &tempFloat)){
859 cerr << "ERROR: Invalid floating-point value following option " << argv[i-1] << ": " << argv[i] << endl;
864 cerr << "ERROR: For option " << argv[i-1] << ", floating-point value must not be nagative." << endl;
868 threshhold = tempFloat;
872 cerr << "ERROR: Floating-point value expected for option " << argv[i] << endl;
877 else if (! strcmp (argv[i], "-l")) {
879 if (!GetInteger (argv[++i], &tempInt)){
880 cerr << "ERROR: Invalid integer following option " << argv[i-1] << ": " << argv[i] << endl;
884 if (tempInt <= 0 || 6 <= tempInt) {
885 cerr << "ERROR: For option " << argv[i-1] << ", integer must be between "
886 << "1 and 6" << "." << endl;
894 else if (! strcmp (argv[i], "-b")) {
896 if (!GetFloat (argv[++i], &tempFloat)){
897 cerr << "ERROR: Invalid floating-point value following option " << argv[i-1] << ": " << argv[i] << endl;
901 if (tempFloat < 0 && 1 < tempFloat) {
902 cerr << "ERROR: For option " << argv[i-1] << ", floating-point value must not be nagative." << endl;
906 BaseProbThreshold = tempFloat;
910 else if (! strcmp (argv[i], "-g")) {
912 if (!GetFloat (argv[++i], &tempFloat)){
913 cerr << "ERROR: Invalid floating-point value following option " << argv[i-1] << ": " << argv[i] << endl;
917 if (tempFloat < 0 && 1 < tempFloat) {
918 cerr << "ERROR: For option " << argv[i-1] << ", floating-point value must not be nagative." << endl;
922 BasePairConst = tempFloat;
927 // number of consistency transformations
928 else if (!strcmp (argv[i], "-c") || !strcmp (argv[i], "--consistency")){
930 if (!GetInteger (argv[++i], &tempInt)){
931 cerr << "ERROR: Invalid integer following option " << argv[i-1] << ": " << argv[i] << endl;
935 if (tempInt < MIN_CONSISTENCY_REPS || tempInt > MAX_CONSISTENCY_REPS){
936 cerr << "ERROR: For option " << argv[i-1] << ", integer must be between "
937 << MIN_CONSISTENCY_REPS << " and " << MAX_CONSISTENCY_REPS << "." << endl;
941 numConsistencyReps = tempInt;
945 cerr << "ERROR: Integer expected for option " << argv[i] << endl;
950 // number of randomized partitioning iterative refinement passes
951 else if (!strcmp (argv[i], "-ir") || !strcmp (argv[i], "--iterative-refinement")){
953 if (!GetInteger (argv[++i], &tempInt)){
954 cerr << "ERROR: Invalid integer following option " << argv[i-1] << ": " << argv[i] << endl;
958 if (tempInt < MIN_ITERATIVE_REFINEMENT_REPS || tempInt > MAX_ITERATIVE_REFINEMENT_REPS){
959 cerr << "ERROR: For option " << argv[i-1] << ", integer must be between "
960 << MIN_ITERATIVE_REFINEMENT_REPS << " and " << MAX_ITERATIVE_REFINEMENT_REPS << "." << endl;
964 numIterativeRefinementReps = tempInt;
968 cerr << "ERROR: Integer expected for option " << argv[i] << endl;
972 // number of EM pre-training rounds
973 else if (!strcmp (argv[i], "-pre") || !strcmp (argv[i], "--pre-training")){
975 if (!GetInteger (argv[++i], &tempInt)){
976 cerr << "ERROR: Invalid integer following option " << argv[i-1] << ": " << argv[i] << endl;
980 if (tempInt < MIN_PRETRAINING_REPS || tempInt > MAX_PRETRAINING_REPS){
981 cerr << "ERROR: For option " << argv[i-1] << ", integer must be between "
982 << MIN_PRETRAINING_REPS << " and " << MAX_PRETRAINING_REPS << "." << endl;
986 numPreTrainingReps = tempInt;
990 cerr << "ERROR: Integer expected for option " << argv[i] << endl;
995 // the distance of stem candidate
996 else if (!strcmp (argv[i], "-w")){
998 if (!GetInteger (argv[++i], &tempInt)){
999 cerr << "ERROR: Invalid integer following option " << argv[i-1] << ": " << argv[i] << endl;
1003 BandWidth = tempInt;
1007 cerr << "ERROR: Integer expected for option " << argv[i] << endl;
1013 else if (!strcmp (argv[i], "-go") || !strcmp (argv[i], "--gap-open")){
1015 if (!GetFloat (argv[++i], &tempFloat)){
1016 cerr << "ERROR: Invalid floating-point value following option " << argv[i-1] << ": " << argv[i] << endl;
1021 cerr << "ERROR: For option " << argv[i-1] << ", floating-point value must not be positive." << endl;
1025 gapOpenPenalty = tempFloat;
1029 cerr << "ERROR: Floating-point value expected for option " << argv[i] << endl;
1034 // gap extension penalty
1035 else if (!strcmp (argv[i], "-ge") || !strcmp (argv[i], "--gap-extension")){
1037 if (!GetFloat (argv[++i], &tempFloat)){
1038 cerr << "ERROR: Invalid floating-point value following option " << argv[i-1] << ": " << argv[i] << endl;
1043 cerr << "ERROR: For option " << argv[i-1] << ", floating-point value must not be positive." << endl;
1047 gapContinuePenalty = tempFloat;
1051 cerr << "ERROR: Floating-point value expected for option " << argv[i] << endl;
1056 // all-pairs pairwise alignments
1057 else if (!strcmp (argv[i], "-pairs")){
1058 enableAllPairs = true;
1061 // all-pairs pairwise Viterbi alignments
1062 else if (!strcmp (argv[i], "-viterbi")){
1063 enableAllPairs = true;
1064 enableViterbi = true;
1067 // read base-pairing probability from the '_bpp' file, by katoh
1068 else if (!strcmp (argv[i], "-readbpp")){
1072 // write base-pairing probability to stdout, by katoh
1073 else if (!strcmp (argv[i], "-writebpp")){
1078 else if (!strcmp (argv[i], "-annot")){
1079 enableAnnotation = true;
1081 annotationFilename = argv[++i];
1083 cerr << "ERROR: FILENAME expected for option " << argv[i] << endl;
1088 // clustalw output format
1089 else if (!strcmp (argv[i], "-clustalw")){
1090 enableClustalWOutput = true;
1092 // mxscarna output format
1093 else if (!strcmp (argv[i], "-mxscarna")) {
1094 enableMXSCARNAOutput = true;
1096 // stockholm output format
1097 else if (!strcmp (argv[i], "-stockholm")) {
1098 enableStockholmOutput = true;
1100 // web output format
1101 else if (!strcmp (argv[i], "-weboutput")) {
1103 weboutputFileName = string(argv[++i]);
1106 cerr << "ERROR: Invalid following option " << argv[i-1] << ": " << argv[i] << endl;
1110 enableWebOutput = true;
1114 else if (!strcmp (argv[i], "-co") || !strcmp (argv[i], "--cutoff")){
1116 if (!GetFloat (argv[++i], &tempFloat)){
1117 cerr << "ERROR: Invalid floating-point value following option " << argv[i-1] << ": " << argv[i] << endl;
1121 if (tempFloat < 0 || tempFloat > 1){
1122 cerr << "ERROR: For option " << argv[i-1] << ", floating-point value must be between 0 and 1." << endl;
1130 cerr << "ERROR: Floating-point value expected for option " << argv[i] << endl;
1135 // verbose reporting
1136 else if (!strcmp (argv[i], "-v") || !strcmp (argv[i], "--verbose")){
1137 enableVerbose = true;
1141 else if (!strcmp (argv[i], "-a") || !strcmp (argv[i], "--alignment-order")){
1142 enableAlignOrder = true;
1144 // McCaskill MEA MODE
1145 else if (!strcmp (argv[i], "-m") || !strcmp (argv[i], "--mccaskillmea")){
1146 enableMcCaskillMEAMode = true;
1150 cerr << "ERROR: Unrecognized option: " << argv[i] << endl;
1155 sequenceNames.push_back (string (argv[i]));
1159 if (enableTrainEmissions && !enableTraining){
1160 cerr << "ERROR: Training emissions (-e) requires training (-t)" << endl;
1164 return sequenceNames;
1167 /////////////////////////////////////////////////////////////////
1170 // Read initial distribution, transition, and emission
1171 // parameters from a file.
1172 /////////////////////////////////////////////////////////////////
1174 void ReadParameters (){
1178 emitPairs = VVF (256, VF (256, 1e-10));
1179 emitSingle = VF (256, 1e-5);
1181 // read initial state distribution and transition parameters
1183 if (parametersInputFilename == string ("")){
1184 if (NumInsertStates == 1){
1185 for (int i = 0; i < NumMatrixTypes; i++) initDistrib[i] = initDistrib1Default[i];
1186 for (int i = 0; i < 2*NumInsertStates; i++) gapOpen[i] = gapOpen1Default[i];
1187 for (int i = 0; i < 2*NumInsertStates; i++) gapExtend[i] = gapExtend1Default[i];
1189 else if (NumInsertStates == 2){
1190 for (int i = 0; i < NumMatrixTypes; i++) initDistrib[i] = initDistrib2Default[i];
1191 for (int i = 0; i < 2*NumInsertStates; i++) gapOpen[i] = gapOpen2Default[i];
1192 for (int i = 0; i < 2*NumInsertStates; i++) gapExtend[i] = gapExtend2Default[i];
1195 cerr << "ERROR: No default initial distribution/parameter settings exist" << endl
1196 << " for " << NumInsertStates << " pairs of insert states. Use --paramfile." << endl;
1200 alphabet = alphabetDefault;
1202 for (int i = 0; i < (int) alphabet.length(); i++){
1203 emitSingle[(unsigned char) tolower(alphabet[i])] = emitSingleDefault[i];
1204 emitSingle[(unsigned char) toupper(alphabet[i])] = emitSingleDefault[i];
1205 for (int j = 0; j <= i; j++){
1206 emitPairs[(unsigned char) tolower(alphabet[i])][(unsigned char) tolower(alphabet[j])] = emitPairsDefault[i][j];
1207 emitPairs[(unsigned char) tolower(alphabet[i])][(unsigned char) toupper(alphabet[j])] = emitPairsDefault[i][j];
1208 emitPairs[(unsigned char) toupper(alphabet[i])][(unsigned char) tolower(alphabet[j])] = emitPairsDefault[i][j];
1209 emitPairs[(unsigned char) toupper(alphabet[i])][(unsigned char) toupper(alphabet[j])] = emitPairsDefault[i][j];
1210 emitPairs[(unsigned char) tolower(alphabet[j])][(unsigned char) tolower(alphabet[i])] = emitPairsDefault[i][j];
1211 emitPairs[(unsigned char) tolower(alphabet[j])][(unsigned char) toupper(alphabet[i])] = emitPairsDefault[i][j];
1212 emitPairs[(unsigned char) toupper(alphabet[j])][(unsigned char) tolower(alphabet[i])] = emitPairsDefault[i][j];
1213 emitPairs[(unsigned char) toupper(alphabet[j])][(unsigned char) toupper(alphabet[i])] = emitPairsDefault[i][j];
1218 data.open (parametersInputFilename.c_str());
1220 cerr << "ERROR: Unable to read parameter file: " << parametersInputFilename << endl;
1225 for (int i = 0; i < 3; i++){
1226 if (!getline (data, line[i])){
1227 cerr << "ERROR: Unable to read transition parameters from parameter file: " << parametersInputFilename << endl;
1231 istringstream data2;
1232 data2.clear(); data2.str (line[0]); for (int i = 0; i < NumMatrixTypes; i++) data2 >> initDistrib[i];
1233 data2.clear(); data2.str (line[1]); for (int i = 0; i < 2*NumInsertStates; i++) data2 >> gapOpen[i];
1234 data2.clear(); data2.str (line[2]); for (int i = 0; i < 2*NumInsertStates; i++) data2 >> gapExtend[i];
1236 if (!getline (data, line[0])){
1237 cerr << "ERROR: Unable to read alphabet from scoring matrix file: " << parametersInputFilename << endl;
1241 // read alphabet as concatenation of all characters on alphabet line
1244 data2.clear(); data2.str (line[0]); while (data2 >> token) alphabet += token;
1246 for (int i = 0; i < (int) alphabet.size(); i++){
1247 for (int j = 0; j <= i; j++){
1250 emitPairs[(unsigned char) tolower(alphabet[i])][(unsigned char) tolower(alphabet[j])] = val;
1251 emitPairs[(unsigned char) tolower(alphabet[i])][(unsigned char) toupper(alphabet[j])] = val;
1252 emitPairs[(unsigned char) toupper(alphabet[i])][(unsigned char) tolower(alphabet[j])] = val;
1253 emitPairs[(unsigned char) toupper(alphabet[i])][(unsigned char) toupper(alphabet[j])] = val;
1254 emitPairs[(unsigned char) tolower(alphabet[j])][(unsigned char) tolower(alphabet[i])] = val;
1255 emitPairs[(unsigned char) tolower(alphabet[j])][(unsigned char) toupper(alphabet[i])] = val;
1256 emitPairs[(unsigned char) toupper(alphabet[j])][(unsigned char) tolower(alphabet[i])] = val;
1257 emitPairs[(unsigned char) toupper(alphabet[j])][(unsigned char) toupper(alphabet[i])] = val;
1261 for (int i = 0; i < (int) alphabet.size(); i++){
1264 emitSingle[(unsigned char) tolower(alphabet[i])] = val;
1265 emitSingle[(unsigned char) toupper(alphabet[i])] = val;
1271 /////////////////////////////////////////////////////////////////
1274 // Process the tree recursively. Returns the aligned sequences
1275 // corresponding to a node or leaf of the tree.
1276 /////////////////////////////////////////////////////////////////
1278 MultiSequence *ProcessTree (const TreeNode *tree, MultiSequence *sequences,
1279 const SafeVector<SafeVector<SparseMatrix *> > &sparseMatrices,
1280 const ProbabilisticModel &model, SafeVector<BPPMatrix*> &BPPMatrices) {
1281 MultiSequence *result;
1283 // check if this is a node of the alignment tree
1284 if (tree->GetSequenceLabel() == -1){
1285 MultiSequence *alignLeft = ProcessTree (tree->GetLeftChild(), sequences, sparseMatrices, model, BPPMatrices);
1286 MultiSequence *alignRight = ProcessTree (tree->GetRightChild(), sequences, sparseMatrices, model, BPPMatrices);
1289 assert (alignRight);
1291 result = AlignAlignments (alignLeft, alignRight, sparseMatrices, model, BPPMatrices, tree->GetIdentity());
1298 // otherwise, this is a leaf of the alignment tree
1300 result = new MultiSequence(); assert (result);
1301 result->AddSequence (sequences->GetSequence(tree->GetSequenceLabel())->Clone());
1307 /////////////////////////////////////////////////////////////////
1308 // ComputeFinalAlignment()
1310 // Compute the final alignment by calling ProcessTree(), then
1311 // performing iterative refinement as needed.
1312 /////////////////////////////////////////////////////////////////
1314 MultiSequence *ComputeFinalAlignment (const TreeNode *tree, MultiSequence *sequences,
1315 const SafeVector<SafeVector<SparseMatrix *> > &sparseMatrices,
1316 const ProbabilisticModel &model,
1317 SafeVector<BPPMatrix*> &BPPMatrices) {
1319 MultiSequence *alignment = ProcessTree (tree, sequences, sparseMatrices, model, BPPMatrices);
1321 if (enableAlignOrder){
1322 alignment->SaveOrdering();
1323 enableAlignOrder = false;
1326 // tree-based refinement
1327 // if you use the function, you can degrade the quality of the software.
1328 // TreeBasedBiPartitioning (sparseMatrices, model, alignment, tree, BPPMatrices);
1330 // iterative refinement
1332 for (int i = 0; i < numIterativeRefinementReps; i++)
1333 DoIterativeRefinement (sparseMatrices, model, alignment);
1337 // return final alignment
1341 /////////////////////////////////////////////////////////////////
1342 // AlignAlignments()
1344 // Returns the alignment of two MultiSequence objects.
1345 /////////////////////////////////////////////////////////////////
1347 MultiSequence *AlignAlignments (MultiSequence *align1, MultiSequence *align2,
1348 const SafeVector<SafeVector<SparseMatrix *> > &sparseMatrices,
1349 const ProbabilisticModel &model,
1350 SafeVector<BPPMatrix*> &BPPMatrices, float identity){
1352 // print some info about the alignment
1354 for (int i = 0; i < align1->GetNumSequences(); i++)
1355 cerr << ((i==0) ? "[" : ",") << align1->GetSequence(i)->GetLabel();
1357 for (int i = 0; i < align2->GetNumSequences(); i++)
1358 cerr << ((i==0) ? "[" : ",") << align2->GetSequence(i)->GetLabel();
1362 VF *posterior = model.BuildPosterior (align1, align2, sparseMatrices, cutoff);
1364 pair<SafeVector<char> *, float> alignment;
1365 // choose the alignment routine depending on the "cosmetic" gap penalties used
1366 if (gapOpenPenalty == 0 && gapContinuePenalty == 0) {
1368 if(identity <= threshhold) {
1369 std::vector<StemCandidate> *pscs1, *pscs2;
1370 pscs1 = seq2scs(align1, BPPMatrices, BandWidth);
1371 pscs2 = seq2scs(align2, BPPMatrices, BandWidth);
1372 std::vector<int> *matchPSCS1 = new std::vector<int>;
1373 std::vector<int> *matchPSCS2 = new std::vector<int>;
1375 Globaldp globaldp(pscs1, pscs2, align1, align2, matchPSCS1, matchPSCS2, posterior, BPPMatrices);
1376 //float scsScore = globaldp.Run();
1380 removeConflicts(pscs1, pscs2, matchPSCS1, matchPSCS2);
1382 alignment = model.ComputeAlignment2 (align1->GetSequence(0)->GetLength(), align2->GetSequence(0)->GetLength(), *posterior, pscs1, pscs2, matchPSCS1, matchPSCS2);
1386 alignment = model.ComputeAlignment (align1->GetSequence(0)->GetLength(), align2->GetSequence(0)->GetLength(), *posterior);
1390 alignment = model.ComputeAlignmentWithGapPenalties (align1, align2,
1391 *posterior, align1->GetNumSequences(), align2->GetNumSequences(),
1392 gapOpenPenalty, gapContinuePenalty);
1399 // compute total length of sequences
1401 for (int i = 0; i < align1->GetNumSequences(); i++)
1402 for (int j = 0; j < align2->GetNumSequences(); j++)
1403 totLength += min (align1->GetSequence(i)->GetLength(), align2->GetSequence(j)->GetLength());
1405 // give an "accuracy" measure for the alignment
1406 cerr << alignment.second / totLength << endl;
1409 // now build final alignment
1410 MultiSequence *result = new MultiSequence();
1411 for (int i = 0; i < align1->GetNumSequences(); i++)
1412 result->AddSequence (align1->GetSequence(i)->AddGaps(alignment.first, 'X'));
1413 for (int i = 0; i < align2->GetNumSequences(); i++)
1414 result->AddSequence (align2->GetSequence(i)->AddGaps(alignment.first, 'Y'));
1415 if (!enableAlignOrder)
1416 result->SortByLabel();
1418 // free temporary alignment
1419 delete alignment.first;
1424 /////////////////////////////////////////////////////////////////
1427 // Performs one round of the consistency transformation. The
1430 // P'(x[i]-y[j]) = --- sum sum P(x[i]-z[k]) P(z[k]-y[j])
1433 // where S = {x, y, all other sequences...}
1435 /////////////////////////////////////////////////////////////////
1437 SafeVector<SafeVector<SparseMatrix *> > DoRelaxation (MultiSequence *sequences,
1438 SafeVector<SafeVector<SparseMatrix *> > &sparseMatrices){
1439 const int numSeqs = sequences->GetNumSequences();
1441 SafeVector<SafeVector<SparseMatrix *> > newSparseMatrices (numSeqs, SafeVector<SparseMatrix *>(numSeqs, NULL));
1443 // for every pair of sequences
1444 for (int i = 0; i < numSeqs; i++){
1445 for (int j = i+1; j < numSeqs; j++){
1446 Sequence *seq1 = sequences->GetSequence (i);
1447 Sequence *seq2 = sequences->GetSequence (j);
1450 cerr << "Relaxing (" << i+1 << ") " << seq1->GetHeader() << " vs. "
1451 << "(" << j+1 << ") " << seq2->GetHeader() << ": ";
1453 // get the original posterior matrix
1454 VF *posteriorPtr = sparseMatrices[i][j]->GetPosterior(); assert (posteriorPtr);
1455 VF &posterior = *posteriorPtr;
1457 const int seq1Length = seq1->GetLength();
1458 const int seq2Length = seq2->GetLength();
1460 // contribution from the summation where z = x and z = y
1461 for (int k = 0; k < (seq1Length+1) * (seq2Length+1); k++) posterior[k] += posterior[k];
1464 cerr << sparseMatrices[i][j]->GetNumCells() << " --> ";
1466 // contribution from all other sequences
1467 for (int k = 0; k < numSeqs; k++) if (k != i && k != j){
1469 Relax1 (sparseMatrices[k][i], sparseMatrices[k][j], posterior);
1470 else if (k > i && k < j)
1471 Relax (sparseMatrices[i][k], sparseMatrices[k][j], posterior);
1473 SparseMatrix *temp = sparseMatrices[j][k]->ComputeTranspose();
1474 Relax (sparseMatrices[i][k], temp, posterior);
1479 // now renormalization
1480 for (int k = 0; k < (seq1Length+1) * (seq2Length+1); k++) posterior[k] /= numSeqs;
1482 // mask out positions not originally in the posterior matrix
1483 SparseMatrix *matXY = sparseMatrices[i][j];
1484 for (int y = 0; y <= seq2Length; y++) posterior[y] = 0;
1485 for (int x = 1; x <= seq1Length; x++){
1486 SafeVector<PIF>::iterator XYptr = matXY->GetRowPtr(x);
1487 SafeVector<PIF>::iterator XYend = XYptr + matXY->GetRowSize(x);
1488 VF::iterator base = posterior.begin() + x * (seq2Length + 1);
1490 while (XYptr != XYend){
1492 // zero out all cells until the first filled column
1493 while (curr < XYptr->first){
1498 // now, skip over this column
1503 // zero out cells after last column
1504 while (curr <= seq2Length){
1510 // save the new posterior matrix
1511 newSparseMatrices[i][j] = new SparseMatrix (seq1->GetLength(), seq2->GetLength(), posterior);
1512 newSparseMatrices[j][i] = NULL;
1515 cerr << newSparseMatrices[i][j]->GetNumCells() << " -- ";
1517 delete posteriorPtr;
1520 cerr << "done." << endl;
1524 return newSparseMatrices;
1527 /////////////////////////////////////////////////////////////////
1530 // Computes the consistency transformation for a single sequence
1531 // z, and adds the transformed matrix to "posterior".
1532 /////////////////////////////////////////////////////////////////
1534 void Relax (SparseMatrix *matXZ, SparseMatrix *matZY, VF &posterior){
1539 int lengthX = matXZ->GetSeq1Length();
1540 int lengthY = matZY->GetSeq2Length();
1541 assert (matXZ->GetSeq2Length() == matZY->GetSeq1Length());
1544 for (int i = 1; i <= lengthX; i++){
1545 SafeVector<PIF>::iterator XZptr = matXZ->GetRowPtr(i);
1546 SafeVector<PIF>::iterator XZend = XZptr + matXZ->GetRowSize(i);
1548 VF::iterator base = posterior.begin() + i * (lengthY + 1);
1550 // iterate through all x[i]-z[k]
1551 while (XZptr != XZend){
1552 SafeVector<PIF>::iterator ZYptr = matZY->GetRowPtr(XZptr->first);
1553 SafeVector<PIF>::iterator ZYend = ZYptr + matZY->GetRowSize(XZptr->first);
1554 const float XZval = XZptr->second;
1556 // iterate through all z[k]-y[j]
1557 while (ZYptr != ZYend){
1558 base[ZYptr->first] += XZval * ZYptr->second;
1566 /////////////////////////////////////////////////////////////////
1569 // Computes the consistency transformation for a single sequence
1570 // z, and adds the transformed matrix to "posterior".
1571 /////////////////////////////////////////////////////////////////
1573 void Relax1 (SparseMatrix *matZX, SparseMatrix *matZY, VF &posterior){
1578 int lengthZ = matZX->GetSeq1Length();
1579 int lengthY = matZY->GetSeq2Length();
1582 for (int k = 1; k <= lengthZ; k++){
1583 SafeVector<PIF>::iterator ZXptr = matZX->GetRowPtr(k);
1584 SafeVector<PIF>::iterator ZXend = ZXptr + matZX->GetRowSize(k);
1586 // iterate through all z[k]-x[i]
1587 while (ZXptr != ZXend){
1588 SafeVector<PIF>::iterator ZYptr = matZY->GetRowPtr(k);
1589 SafeVector<PIF>::iterator ZYend = ZYptr + matZY->GetRowSize(k);
1590 const float ZXval = ZXptr->second;
1591 VF::iterator base = posterior.begin() + ZXptr->first * (lengthY + 1);
1593 // iterate through all z[k]-y[j]
1594 while (ZYptr != ZYend){
1595 base[ZYptr->first] += ZXval * ZYptr->second;
1603 void DoBasePairProbabilityRelaxation (MultiSequence *sequences,
1604 SafeVector<SafeVector<SparseMatrix *> > &sparseMatrices,
1605 SafeVector<BPPMatrix*> &BPPMatrices) {
1606 const int numSeqs = sequences->GetNumSequences();
1608 for (int i = 0; i < numSeqs; i++) {
1609 Sequence *seq1 = sequences->GetSequence (i);
1610 BPPMatrix *seq1BppMatrix = BPPMatrices[seq1->GetLabel()];
1611 Trimat<float> consBppMat(seq1->GetLength() + 1);
1612 int seq1Length = seq1->GetLength();
1614 for (int k = 1; k <= seq1Length; k++) {
1615 for (int l = k; l <= seq1Length; l++) {
1616 consBppMat.ref(k, l) = seq1BppMatrix->GetProb(k, l);
1620 for (int j = i + 1; j < numSeqs; j++) {
1622 // VF *posteriorPtr = sparseMatrices[i][j]->GetPosterior()
1623 Sequence *seq2 = sequences->GetSequence (j);
1624 BPPMatrix *seq2BppMatrix = BPPMatrices[seq2->GetLabel()];
1625 // int seq2Length = seq2->GetLength();
1626 SparseMatrix *matchProb = sparseMatrices[i][j];
1628 // vector<PIF2> &probs1 = seq1BppMatrix->bppMat.data2;
1629 for(int k = 1; k <= seq1Length; k++) {
1630 for(int m = k, n = k; n <= k + 200 && m >= 1 && n <= seq1Length; m--, n++) {
1632 // for (int k = 0; k < (int)probs1.size(); k++) {
1633 // float tmpProb1 = probs1[k].prob;
1634 // int tmp1I = probs1[k].i;
1635 // int tmp1J = probs1[k].j;
1638 vector<PIF2> &probs2 = seq2BppMatrix->bppMat.data2;
1639 for(int l = 0; l < (int)probs2.size(); l++) {
1640 float tmpProb2 = probs2[l].prob;
1641 int tmp2I = probs2[l].i;
1642 int tmp2J = probs2[l].j;
1643 sumProb += matchProb->GetValue(m, tmp2I)*matchProb->GetValue(n, tmp2J)*tmpProb2;
1646 consBppMat.ref(m, n) += sumProb;
1649 for(int m = k, n = k + 1; n <= k + 200 && m >= 1 && n <= seq1Length; m--, n++) {
1651 // for (int k = 0; k < (int)probs1.size(); k++) {
1652 // float tmpProb1 = probs1[k].prob;
1653 // int tmp1I = probs1[k].i;
1654 // int tmp1J = probs1[k].j;
1657 vector<PIF2> &probs2 = seq2BppMatrix->bppMat.data2;
1658 for(int l = 0; l < (int)probs2.size(); l++) {
1659 float tmpProb2 = probs2[l].prob;
1660 int tmp2I = probs2[l].i;
1661 int tmp2J = probs2[l].j;
1662 sumProb += matchProb->GetValue(m, tmp2I)*matchProb->GetValue(n, tmp2J)*tmpProb2;
1665 consBppMat.ref(m, n) += sumProb;
1671 for(int k = 1; k <= seq1Length; k++) {
1672 for(int m = k, n = k; n <= k + 30 && m >= 1 && n <= seq1Length; m--, n++) {
1673 float tmpProb = seq1BppMatrix->GetProb(m, n);
1674 for(int l = 1; l <= seq2Length; l++) {
1675 for(int s = l, t = l; t <= l + 30 && s >= 1 && t <= seq2Length; s--, t++) {
1676 tmpProb += matchProb->GetValue(m,s)*matchProb->GetValue(n,t)*seq2BppMatrix->GetProb(s,t);
1678 for(int s = l, t = l + 1; t <= l + 31 && s >= 1 && t <= seq2Length; s--, t++) {
1679 tmpProb += matchProb->GetValue(m,s)*matchProb->GetValue(n,t)*seq2BppMatrix->GetProb(s,t);
1682 consBppMat.ref(m, n) += tmpProb;
1685 for(int m = k, n = k + 1; n <= k + 31 && m >= 1 && n <= seq1Length; m--, n++) {
1686 float tmpProb = seq1BppMatrix->GetProb(m, n);
1687 for(int l = 1; l <= seq2Length; l++) {
1688 for(int s = l, t = l; t <= l + 30 && s >= 1 && t <= seq2Length; s--, t++) {
1689 tmpProb += matchProb->GetValue(m,s)*matchProb->GetValue(n,t)*seq2BppMatrix->GetProb(s,t);
1691 for(int s = l, t = l + 1; t <= l + 31 && s >= 1 && t <= seq2Length; s--, t++) {
1692 tmpProb += matchProb->GetValue(m,s)*matchProb->GetValue(n,t)*seq2BppMatrix->GetProb(s,t);
1695 consBppMat.ref(m,n) += tmpProb;
1700 for (int m = 1; m <= seq1Length; m++) {
1701 for (int n = m + 4; n <= seq1Length; n++) {
1702 consBppMat.ref(m,n) = consBppMat.ref(m,n)/(float)numSeqs;
1705 seq1BppMatrix->updateBPPMatrix(consBppMat);
1709 /////////////////////////////////////////////////////////////////
1712 // Returns set containing all leaf labels of the current subtree.
1713 /////////////////////////////////////////////////////////////////
1715 set<int> GetSubtree (const TreeNode *tree){
1718 if (tree->GetSequenceLabel() == -1){
1719 s = GetSubtree (tree->GetLeftChild());
1720 set<int> t = GetSubtree (tree->GetRightChild());
1722 for (set<int>::iterator iter = t.begin(); iter != t.end(); ++iter)
1726 s.insert (tree->GetSequenceLabel());
1732 /////////////////////////////////////////////////////////////////
1733 // TreeBasedBiPartitioning
1735 // Uses the iterative refinement scheme from MUSCLE.
1736 /////////////////////////////////////////////////////////////////
1738 void TreeBasedBiPartitioning (const SafeVector<SafeVector<SparseMatrix *> > &sparseMatrices,
1739 const ProbabilisticModel &model, MultiSequence* &alignment,
1740 const TreeNode *tree, SafeVector<BPPMatrix*> &BPPMatrices){
1741 // check if this is a node of the alignment tree
1742 if (tree->GetSequenceLabel() == -1){
1743 TreeBasedBiPartitioning (sparseMatrices, model, alignment, tree->GetLeftChild(), BPPMatrices);
1744 TreeBasedBiPartitioning (sparseMatrices, model, alignment, tree->GetRightChild(), BPPMatrices);
1746 set<int> leftSubtree = GetSubtree (tree->GetLeftChild());
1747 set<int> rightSubtree = GetSubtree (tree->GetRightChild());
1748 set<int> leftSubtreeComplement, rightSubtreeComplement;
1750 // calculate complement of each subtree
1751 for (int i = 0; i < alignment->GetNumSequences(); i++){
1752 if (leftSubtree.find(i) == leftSubtree.end()) leftSubtreeComplement.insert (i);
1753 if (rightSubtree.find(i) == rightSubtree.end()) rightSubtreeComplement.insert (i);
1756 // perform realignments for edge to left child
1757 if (!leftSubtree.empty() && !leftSubtreeComplement.empty()){
1758 MultiSequence *groupOneSeqs = alignment->Project (leftSubtree); assert (groupOneSeqs);
1759 MultiSequence *groupTwoSeqs = alignment->Project (leftSubtreeComplement); assert (groupTwoSeqs);
1761 alignment = AlignAlignments (groupOneSeqs, groupTwoSeqs, sparseMatrices, model, BPPMatrices, tree->GetLeftChild()->GetIdentity());
1764 // perform realignments for edge to right child
1765 if (!rightSubtree.empty() && !rightSubtreeComplement.empty()){
1766 MultiSequence *groupOneSeqs = alignment->Project (rightSubtree); assert (groupOneSeqs);
1767 MultiSequence *groupTwoSeqs = alignment->Project (rightSubtreeComplement); assert (groupTwoSeqs);
1769 alignment = AlignAlignments (groupOneSeqs, groupTwoSeqs, sparseMatrices, model, BPPMatrices, tree->GetRightChild()->GetIdentity());
1774 /////////////////////////////////////////////////////////////////
1775 // DoterativeRefinement()
1777 // Performs a single round of randomized partionining iterative
1779 /////////////////////////////////////////////////////////////////
1781 void DoIterativeRefinement (const SafeVector<SafeVector<SparseMatrix *> > &sparseMatrices,
1782 const ProbabilisticModel &model, MultiSequence* &alignment){
1783 set<int> groupOne, groupTwo;
1785 // create two separate groups
1786 for (int i = 0; i < alignment->GetNumSequences(); i++){
1788 groupOne.insert (i);
1790 groupTwo.insert (i);
1793 if (groupOne.empty() || groupTwo.empty()) return;
1795 // project into the two groups
1796 MultiSequence *groupOneSeqs = alignment->Project (groupOne); assert (groupOneSeqs);
1797 MultiSequence *groupTwoSeqs = alignment->Project (groupTwo); assert (groupTwoSeqs);
1801 alignment = AlignAlignments (groupOneSeqs, groupTwoSeqs, sparseMatrices, model);
1803 delete groupOneSeqs;
1804 delete groupTwoSeqs;
1808 /////////////////////////////////////////////////////////////////
1809 // WriteAnnotation()
1811 // Computes annotation for multiple alignment and write values
1813 /////////////////////////////////////////////////////////////////
1815 void WriteAnnotation (MultiSequence *alignment,
1816 const SafeVector<SafeVector<SparseMatrix *> > &sparseMatrices){
1817 ofstream outfile (annotationFilename.c_str());
1819 if (outfile.fail()){
1820 cerr << "ERROR: Unable to write annotation file." << endl;
1824 const int alignLength = alignment->GetSequence(0)->GetLength();
1825 const int numSeqs = alignment->GetNumSequences();
1827 SafeVector<int> position (numSeqs, 0);
1828 SafeVector<SafeVector<char>::iterator> seqs (numSeqs);
1829 for (int i = 0; i < numSeqs; i++) seqs[i] = alignment->GetSequence(i)->GetDataPtr();
1830 SafeVector<pair<int,int> > active;
1831 active.reserve (numSeqs);
1834 for (int i = 1; i <= alignLength; i++){
1836 // find all aligned residues in this particular column
1838 for (int j = 0; j < numSeqs; j++){
1839 if (seqs[j][i] != '-'){
1840 active.push_back (make_pair(j, ++position[j]));
1844 outfile << setw(4) << ComputeScore (active, sparseMatrices) << endl;
1850 /////////////////////////////////////////////////////////////////
1853 // Computes the annotation score for a particular column.
1854 /////////////////////////////////////////////////////////////////
1856 int ComputeScore (const SafeVector<pair<int, int> > &active,
1857 const SafeVector<SafeVector<SparseMatrix *> > &sparseMatrices){
1859 if (active.size() <= 1) return 0;
1861 // ALTERNATIVE #1: Compute the average alignment score.
1864 for (int i = 0; i < (int) active.size(); i++){
1865 for (int j = i+1; j < (int) active.size(); j++){
1866 val += sparseMatrices[active[i].first][active[j].first]->GetValue(active[i].second, active[j].second);
1870 return (int) (200 * val / ((int) active.size() * ((int) active.size() - 1)));