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.viewmodel;
23 import jalview.analysis.PCA;
24 import jalview.api.RotatableCanvasI;
25 import jalview.api.analysis.ScoreModelI;
26 import jalview.api.analysis.SimilarityParamsI;
27 import jalview.datamodel.AlignmentView;
28 import jalview.datamodel.SequenceI;
29 import jalview.datamodel.SequencePoint;
31 import java.util.Vector;
38 private final AlignmentView seqstrings;
40 private final SequenceI[] seqs;
42 private final SimilarityParamsI similarityParams;
45 * options - score model, nucleotide / protein
47 private ScoreModelI scoreModel;
49 private boolean nucleotide = false;
58 private Vector<SequencePoint> points;
61 * Constructor given sequence data, score model and score calculation
70 public PCAModel(AlignmentView seqData, SequenceI[] sqs, boolean nuc,
71 ScoreModelI modelName, SimilarityParamsI params)
76 scoreModel = modelName;
77 similarityParams = params;
81 * Performs the PCA calculation (in the same thread) and extracts result data
82 * needed for visualisation by PCAPanel
84 public void calculate()
86 pca = new PCA(seqstrings, scoreModel, similarityParams);
87 pca.run(); // executes in same thread, wait for completion
89 // Now find the component coordinates
92 while ((ii < seqs.length) && (seqs[ii] != null))
97 int height = pca.getHeight();
98 // top = pca.getM().height() - 1;
101 points = new Vector<>();
102 float[][] scores = pca.getComponents(top - 1, top - 2, top - 3, 100);
104 for (int i = 0; i < height; i++)
106 SequencePoint sp = new SequencePoint(seqs[i], scores[i]);
107 points.addElement(sp);
111 public void updateRc(RotatableCanvasI rc)
113 rc.setPoints(points, pca.getHeight());
116 public boolean isNucleotide()
121 public void setNucleotide(boolean nucleotide)
123 this.nucleotide = nucleotide;
129 * @return index of principle dimension of PCA
137 * update the 2d coordinates for the list of points to the given dimensions
138 * Principal dimension is getTop(). Next greatest eigenvector is getTop()-1.
139 * Note - pca.getComponents starts counting the spectrum from rank-2 to zero,
140 * rather than rank-1, so getComponents(dimN ...) == updateRcView(dimN+1 ..)
146 public void updateRcView(int dim1, int dim2, int dim3)
148 // note: actual indices for components are dim1-1, etc (patch for JAL-1123)
149 float[][] scores = pca.getComponents(dim1 - 1, dim2 - 1, dim3 - 1, 100);
151 for (int i = 0; i < pca.getHeight(); i++)
153 points.elementAt(i).coord = scores[i];
157 public String getDetails()
159 return pca.getDetails();
162 public AlignmentView getSeqtrings()
167 public String getPointsasCsv(boolean transformed, int xdim, int ydim,
170 StringBuffer csv = new StringBuffer();
171 csv.append("\"Sequence\"");
183 for (int d = 1, dmax = pca.component(1).length; d <= dmax; d++)
189 for (int s = 0; s < seqs.length; s++)
191 csv.append("\"" + seqs[s].getName() + "\"");
195 // output pca in correct order
196 fl = pca.component(s);
197 for (int d = fl.length - 1; d >= 0; d--)
205 // output current x,y,z coords for points
206 fl = getPointPosition(s);
207 for (int d = 0; d < fl.length; d++)
215 return csv.toString();
220 * @return x,y,z positions of point s (index into points) under current
223 public double[] getPointPosition(int s)
225 double pts[] = new double[3];
226 float[] p = points.elementAt(s).coord;
233 public String getScoreModelName()
235 return scoreModel == null ? "" : scoreModel.getName();
238 public void setScoreModel(ScoreModelI sm)
240 this.scoreModel = sm;