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 eigenMatrix;
65 * Constructor given the sequences to compute for, the similarity model to
66 * use, and a set of parameters for sequence comparison
72 public PaSiMap(AlignmentViewport sequences, ScoreModelI sm,
73 SimilarityParamsI options)
75 this.seqs = sequences;
77 this.similarityParams = options;
84 * Index of diagonal within matrix
86 * @return Returns value of diagonal from matrix
88 public double getEigenvalue(int i)
90 return eigenMatrix.getD()[i];
94 * Returns coordinates for each datapoint
102 * @param factor ~ is 1
104 * @return DOCUMENT ME!
106 public Point[] getComponents(int l, int n, int mm, float factor)
108 Point[] out = new Point[getHeight()];
110 for (int i = 0; i < out.length; i++)
112 float x = (float) component(i, l) * factor;
113 float y = (float) component(i, n) * factor;
114 float z = (float) component(i, mm) * factor;
115 out[i] = new Point(x, y, z);
127 * @return DOCUMENT ME!
129 public double[] component(int n)
131 // n = index of eigenvector
132 double[] out = new double[getHeight()];
134 for (int i = 0; i < out.length; i++)
136 out[i] = component(i, n);
150 * @return DOCUMENT ME!
152 double component(int row, int n)
154 return eigenMatrix.getValue(row, n);
158 * Answers a formatted text report of the PaSiMap calculation results (matrices
159 * and eigenvalues) suitable for display
163 public String getDetails()
165 StringBuilder sb = new StringBuilder(1024);
166 sb.append("PaSiMap calculation using ").append(scoreModel.getName())
167 .append(" sequence similarity matrix\n========\n\n");
168 PrintStream ps = wrapOutputBuffer(sb);
171 * coordinates matrix, with D vector
173 sb.append(" --- Pairwise correlation coefficients ---\n");
174 pairwiseScores.print(ps, "%8.6f ");
177 sb.append(" --- Eigenvalues ---\n");
178 eigenMatrix.printD(ps, "%15.4e");
181 sb.append(" --- Coordinates ---\n");
182 eigenMatrix.print(ps, "%8.6f ");
185 return sb.toString();
189 * Performs the PaSiMap calculation
191 * creates a new gui/PairwiseAlignPanel with the input sequences (AlignmentViewport)
192 * uses analysis/AlignSeq to creatue the pairwise alignments and calculate the AlignmentScores (float for each pair)
193 * gets all float[][] scores from the gui/PairwiseAlignPanel
194 * checks the connections for each sequence with AlignmentViewport seqs.calculateConnectivity(float[][] scores, int dim) (from analysis/Connectivity) -- throws an Exception if insufficient
195 * creates a math/MatrixI pairwiseScores of the float[][] scores
196 * copys the scores and fills the diagonal to create a symmetric matrix using math/Matrix.fillDiagonal()
197 * performs the analysis/ccAnalysis with the symmetric matrix
198 * gets the eigenmatrix and the eigenvalues using math/Matrix.tqli()
205 PairwiseAlignPanel alignment = new PairwiseAlignPanel(seqs, true, 100, 5);
206 float[][] scores = alignment.getAlignmentScores(); //bigger index first -- eg scores[14][13]
208 Hashtable<SequenceI, Integer> connectivity = seqs.calculateConnectivity(scores, dim);
210 pairwiseScores = new Matrix(scores);
211 pairwiseScores.fillDiagonal();
213 eigenMatrix = pairwiseScores.copy();
215 ccAnalysis cc = new ccAnalysis(pairwiseScores, dim);
216 eigenMatrix = cc.run();
218 } catch (Exception q)
220 Console.error("Error computing PaSiMap: " + q.getMessage());
226 * Returns a PrintStream that wraps (appends its output to) the given
232 protected PrintStream wrapOutputBuffer(StringBuilder sb)
234 PrintStream ps = new PrintStream(System.out)
237 public void print(String x)
243 public void println()
252 * Answers the N dimensions of the NxM PaSiMap matrix. This is the number of
253 * sequences involved in the pairwise score calculation.
257 public int getHeight()
259 // TODO can any of seqs[] be null?
260 return eigenMatrix.height();// seqs.getSequences().length;
264 * Answers the M dimensions of the NxM PaSiMap matrix. This is the number of
265 * sequences involved in the pairwise score calculation.
269 public int getWidth()
271 // TODO can any of seqs[] be null?
272 return eigenMatrix.width();// seqs.getSequences().length;
276 * Answers the sequence pairwise similarity scores which were the first step
277 * of the PaSiMap calculation
281 public MatrixI getPairwiseScores()
283 return pairwiseScores;
286 public void setPairwiseScores(MatrixI m)
291 public MatrixI getEigenmatrix()
296 public void setEigenmatrix(MatrixI m)