2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.analysis;
23 import jalview.api.analysis.ScoreModelI;
24 import jalview.api.analysis.SimilarityParamsI;
25 import jalview.bin.Console;
26 import jalview.datamodel.AlignmentView;
27 import jalview.datamodel.Point;
28 import jalview.datamodel.SequenceI;
29 import jalview.gui.PairwiseAlignPanel;
30 import jalview.gui.PaSiMapPanel;
31 import jalview.math.Matrix;
32 import jalview.math.MatrixI;
33 import jalview.viewmodel.AlignmentViewport;
36 import java.io.PrintStream;
37 import java.util.Hashtable;
38 import java.util.Enumeration;
41 * Performs Principal Component Analysis on given sequences
42 * @AUTHOR MorellThomas
44 public class PaSiMap implements Runnable
49 final private AlignmentViewport seqs;
51 final private ScoreModelI scoreModel;
53 final private SimilarityParamsI similarityParams;
55 final private byte dim = 3;
60 private MatrixI pairwiseScores;
62 private MatrixI tridiagonal;
64 private MatrixI eigenMatrix;
67 * Constructor given the sequences to compute for, the similarity model to
68 * use, and a set of parameters for sequence comparison
74 //public PaSiMap(AlignmentView sequences, ScoreModelI sm,
75 //&! viewport or panel?
76 public PaSiMap(AlignmentViewport sequences, ScoreModelI sm,
77 SimilarityParamsI options)
79 this.seqs = sequences;
81 this.similarityParams = options;
88 * Index of diagonal within matrix
90 * @return Returns value of diagonal from matrix
92 public double getEigenvalue(int i)
94 return eigenMatrix.getD()[i];
109 * @return DOCUMENT ME!
111 public Point[] getComponents(int l, int n, int mm, float factor)
113 Point[] out = new Point[getHeight()];
115 for (int i = 0; i < out.length; i++)
117 float x = (float) component(i, l) * factor;
118 float y = (float) component(i, n) * factor;
119 float z = (float) component(i, mm) * factor;
120 out[i] = new Point(x, y, z);
123 System.out.println("Points:");
124 for (Point point : out)
126 System.out.println(point.toString());
138 * @return DOCUMENT ME!
140 public double[] component(int n)
142 // n = index of eigenvector
143 double[] out = new double[getHeight()];
145 for (int i = 0; i < out.length; i++)
147 out[i] = component(i, n);
161 * @return DOCUMENT ME!
163 double component(int row, int n)
168 for (int i = 0; i < pairwiseScores.width(); i++)
170 double pairwiseScore = pairwiseScores.getValue(row, i);
171 double eigenScore = eigenMatrix.getValue(i, n);
172 out += (pairwiseScores.getValue(row, i) * eigenMatrix.getValue(i, n));
175 return out / eigenMatrix.getD()[n];
177 System.out.println(String.format("row %d, col %d", row, n));
178 return eigenMatrix.getValue(row, n);
182 * Answers a formatted text report of the PaSiMap calculation results (matrices
183 * and eigenvalues) suitable for display
187 public String getDetails()
189 StringBuilder sb = new StringBuilder(1024);
190 sb.append("PaSiMap calculation using ").append(scoreModel.getName())
191 .append(" sequence similarity matrix\n========\n\n");
192 PrintStream ps = wrapOutputBuffer(sb);
195 * pairwise similarity scores
197 sb.append(" --- OrigT * Orig ---- \n");
198 pairwiseScores.print(ps, "%8.2f");
201 * tridiagonal matrix, with D and E vectors
204 sb.append(" ---Tridiag transform matrix ---\n");
205 sb.append(" --- D vector ---\n");
206 tridiagonal.printD(ps, "%15.4e");
208 sb.append("--- E vector ---\n");
209 tridiagonal.printE(ps, "%15.4e");
214 * eigenvalues matrix, with D vector
216 sb.append(" --- New diagonalization matrix ---\n");
217 eigenMatrix.print(ps, "%8.2f");
218 sb.append(" --- Eigenvalues ---\n");
219 eigenMatrix.printD(ps, "%15.4e");
222 return sb.toString();
226 * Performs the PaSiMap calculation
228 * creates a new gui/PairwiseAlignPanel with the input sequences (<++>/AlignmentViewport)
229 * uses analysis/AlignSeq to creatue the pairwise alignments and calculate the AlignmentScores (float for each pair)
230 * gets all float[][] scores from the gui/PairwiseAlignPanel
231 * checks the connections for each sequence with <++>/AlignmentViewport seqs.calculateConnectivity(float[][] scores, int dim) (from analysis/Connectivity) -- throws an Exception if insufficient
232 * creates a math/MatrixI pairwiseScores of the float[][] scores
233 * copys the scores and fills the diagonal to create a symmetric matrix using math/Matrix.fillDiagonal()
234 * performs the analysis/ccAnalysis with the symmetric matrix
235 * gets the eigenmatrix and the eigenvalues using math/Matrix.tqli()
242 // run needleman regardless if aligned or not
243 // gui.PairwiseAlignPanel <++>
244 PairwiseAlignPanel alignment = new PairwiseAlignPanel(seqs, true);
245 float[][] scores = alignment.getAlignmentScores(); //bigger index first -- eg scores[14][13]
247 Hashtable<SequenceI, Integer> connectivity = seqs.calculateConnectivity(scores, dim);
249 pairwiseScores = new Matrix(scores);
250 pairwiseScores.fillDiagonal();
252 ccAnalysis cc = new ccAnalysis(pairwiseScores, dim);
253 pairwiseScores = cc.run();
254 tridiagonal = pairwiseScores.copy();
255 //tridiagonal.tred();
257 /** perform the eigendecomposition for the plot */
258 eigenMatrix = tridiagonal.copy();
259 eigenMatrix.setD(pairwiseScores.getD());
260 //eigenMatrix.tqli();
261 System.out.println(getDetails());
264 } catch (Exception q)
266 Console.error("Error computing PaSiMap: " + q.getMessage());
272 * Returns a PrintStream that wraps (appends its output to) the given
278 protected PrintStream wrapOutputBuffer(StringBuilder sb)
280 PrintStream ps = new PrintStream(System.out)
283 public void print(String x)
289 public void println()
298 * Answers the N dimensions of the NxM PaSiMap matrix. This is the number of
299 * sequences involved in the pairwise score calculation.
303 public int getHeight()
305 // TODO can any of seqs[] be null?
306 return pairwiseScores.height();// seqs.getSequences().length;
310 * Answers the M dimensions of the NxM PaSiMap matrix. This is the number of
311 * sequences involved in the pairwise score calculation.
315 public int getWidth()
317 // TODO can any of seqs[] be null?
318 return pairwiseScores.width();// seqs.getSequences().length;
322 * Answers the sequence pairwise similarity scores which were the first step
323 * of the PaSiMap calculation
327 public MatrixI getPairwiseScores()
329 return pairwiseScores;
332 public void setPairwiseScores(MatrixI m)
337 public MatrixI getEigenmatrix()
342 public void setEigenmatrix(MatrixI m)
347 public MatrixI getTridiagonal()
352 public void setTridiagonal(MatrixI tridiagonal)
354 this.tridiagonal = tridiagonal;