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;
35 private volatile PCA pca;
39 AlignmentView seqstrings;
44 * Score model used to calculate PCA
46 ScoreModelI scoreModel;
48 private boolean nucleotide = false;
50 private Vector<SequencePoint> points;
52 private boolean jvCalcMode = true;
54 private SimilarityParamsI similarityParams;
57 * Constructor given sequence data, score model and score calculation
66 public PCAModel(AlignmentView seqData, SequenceI[] sqs, boolean nuc, ScoreModelI sm,
67 SimilarityParamsI params)
73 similarityParams = params;
76 public boolean isJvCalcMode()
83 pca = new PCA(seqstrings, scoreModel, similarityParams);
84 pca.setJvCalcMode(jvCalcMode);
87 // Now find the component coordinates
90 while ((ii < seqs.length) && (seqs[ii] != null))
95 int height = pca.getHeight();
96 // top = pca.getM().height() - 1;
99 points = new Vector<SequencePoint>();
100 float[][] scores = pca.getComponents(top - 1, top - 2, top - 3, 100);
102 for (int i = 0; i < height; i++)
104 SequencePoint sp = new SequencePoint(seqs[i], scores[i]);
105 points.addElement(sp);
109 public void updateRc(RotatableCanvasI rc)
111 rc.setPoints(points, pca.getHeight());
114 public boolean isNucleotide()
119 public void setNucleotide(boolean nucleotide)
121 this.nucleotide = nucleotide;
127 * @return index of principle dimension of PCA
135 * update the 2d coordinates for the list of points to the given dimensions
136 * Principal dimension is getTop(). Next greatest eigenvector is getTop()-1.
137 * Note - pca.getComponents starts counting the spectrum from rank-2 to zero,
138 * rather than rank-1, so getComponents(dimN ...) == updateRcView(dimN+1 ..)
144 public void updateRcView(int dim1, int dim2, int dim3)
146 // note: actual indices for components are dim1-1, etc (patch for JAL-1123)
147 float[][] scores = pca.getComponents(dim1 - 1, dim2 - 1, dim3 - 1, 100);
149 for (int i = 0; i < pca.getHeight(); i++)
151 points.elementAt(i).coord = scores[i];
155 public String getDetails()
157 return pca.getDetails();
160 public AlignmentView getSeqtrings()
165 public String getPointsasCsv(boolean transformed, int xdim, int ydim,
168 StringBuffer csv = new StringBuffer();
169 csv.append("\"Sequence\"");
181 for (int d = 1, dmax = pca.component(1).length; d <= dmax; d++)
187 for (int s = 0; s < seqs.length; s++)
189 csv.append("\"" + seqs[s].getName() + "\"");
193 // output pca in correct order
194 fl = pca.component(s);
195 for (int d = fl.length - 1; d >= 0; d--)
203 // output current x,y,z coords for points
204 fl = getPointPosition(s);
205 for (int d = 0; d < fl.length; d++)
213 return csv.toString();
218 * @return x,y,z positions of point s (index into points) under current
221 public double[] getPointPosition(int s)
223 double pts[] = new double[3];
224 float[] p = points.elementAt(s).coord;
231 public void setJvCalcMode(boolean state)
236 public String getScoreModelName()
238 return scoreModel == null ? "" : scoreModel.getName();
241 public void setScoreModel(ScoreModelI sm)
243 this.scoreModel = sm;