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 PairwiseAlignPanel alignment;
62 private MatrixI pairwiseScores;
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(AlignmentViewport sequences, ScoreModelI sm,
75 SimilarityParamsI options)
77 this.seqs = sequences;
79 this.similarityParams = options;
86 * Index of diagonal within matrix
88 * @return Returns value of diagonal from matrix
90 public double getEigenvalue(int i)
92 return eigenMatrix.getD()[i];
96 * Returns coordinates for each datapoint
104 * @param factor ~ is 1
106 * @return DOCUMENT ME!
108 public Point[] getComponents(int l, int n, int mm, float factor)
110 Point[] out = new Point[getHeight()];
112 for (int i = 0; i < out.length; i++)
114 float x = (float) component(i, l) * factor;
115 float y = (float) component(i, n) * factor;
116 float z = (float) component(i, mm) * factor;
117 out[i] = new Point(x, y, z);
129 * @return DOCUMENT ME!
131 public double[] component(int n)
133 // n = index of eigenvector
134 double[] out = new double[getHeight()];
136 for (int i = 0; i < out.length; i++)
138 out[i] = component(i, n);
152 * @return DOCUMENT ME!
154 double component(int row, int n)
156 return eigenMatrix.getValue(row, n);
160 * Answers a formatted text report of the PaSiMap calculation results (matrices
161 * and eigenvalues) suitable for display
165 public String getDetails()
167 StringBuilder sb = new StringBuilder(1024);
168 sb.append("PaSiMap calculation using ").append(scoreModel.getName())
169 .append(" sequence similarity matrix\n========\n\n");
170 PrintStream ps = wrapOutputBuffer(sb);
173 * coordinates matrix, with D vector
175 sb.append(" --- Pairwise correlation coefficients ---\n");
176 pairwiseScores.print(ps, "%8.6f ");
179 sb.append(" --- Eigenvalues ---\n");
180 eigenMatrix.printD(ps, "%15.4e");
183 sb.append(" --- Coordinates ---\n");
184 eigenMatrix.print(ps, "%8.6f ");
187 return sb.toString();
191 * Performs the PaSiMap calculation
193 * creates a new gui/PairwiseAlignPanel with the input sequences (AlignmentViewport)
194 * uses analysis/AlignSeq to creatue the pairwise alignments and calculate the AlignmentScores (float for each pair)
195 * gets all float[][] scores from the gui/PairwiseAlignPanel
196 * checks the connections for each sequence with AlignmentViewport seqs.calculateConnectivity(float[][] scores, int dim) (from analysis/Connectivity) -- throws an Exception if insufficient
197 * creates a math/MatrixI pairwiseScores of the float[][] scores
198 * copys the scores and fills the diagonal to create a symmetric matrix using math/Matrix.fillDiagonal()
199 * performs the analysis/ccAnalysis with the symmetric matrix
200 * gets the eigenmatrix and the eigenvalues using math/Matrix.tqli()
207 alignment = new PairwiseAlignPanel(seqs, true, 100, 5);
208 float[][] scores = alignment.getAlignmentScores(); //bigger index first -- eg scores[14][13]
210 Hashtable<SequenceI, Integer> connectivity = seqs.calculateConnectivity(scores, dim);
212 pairwiseScores = new Matrix(scores);
213 pairwiseScores.fillDiagonal();
215 eigenMatrix = pairwiseScores.copy();
217 ccAnalysis cc = new ccAnalysis(pairwiseScores, dim);
218 eigenMatrix = cc.run();
220 } catch (Exception q)
222 Console.error("Error computing PaSiMap: " + q.getMessage());
228 * Returns a PrintStream that wraps (appends its output to) the given
234 protected PrintStream wrapOutputBuffer(StringBuilder sb)
236 PrintStream ps = new PrintStream(System.out)
239 public void print(String x)
245 public void println()
254 * Answers the N dimensions of the NxM PaSiMap matrix. This is the number of
255 * sequences involved in the pairwise score calculation.
259 public int getHeight()
261 // TODO can any of seqs[] be null?
262 return eigenMatrix.height();// seqs.getSequences().length;
266 * Answers the M dimensions of the NxM PaSiMap matrix. This is the number of
267 * sequences involved in the pairwise score calculation.
271 public int getWidth()
273 // TODO can any of seqs[] be null?
274 return eigenMatrix.width();// seqs.getSequences().length;
278 * Answers the sequence pairwise similarity scores which were the first step
279 * of the PaSiMap calculation
283 public MatrixI getPairwiseScores()
285 return pairwiseScores;
288 public void setPairwiseScores(MatrixI m)
293 public MatrixI getEigenmatrix()
298 public void setEigenmatrix(MatrixI m)
303 public PairwiseAlignPanel getAlignments()
308 public String getAlignmentOutput()
310 return alignment.getAlignmentOutput();