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.analysis.scoremodels.ScoreModels;
25 import jalview.api.RotatableCanvasI;
26 import jalview.api.analysis.ScoreModelI;
27 import jalview.api.analysis.SimilarityParamsI;
28 import jalview.datamodel.AlignmentView;
29 import jalview.datamodel.SequenceI;
30 import jalview.datamodel.SequencePoint;
32 import java.util.Vector;
36 private volatile PCA pca;
40 AlignmentView seqstrings;
45 * Score model used to calculate PCA
47 ScoreModelI scoreModel;
49 private boolean nucleotide = false;
51 private Vector<SequencePoint> points;
53 private boolean jvCalcMode = true;
55 private SimilarityParamsI similarityParams;
58 * Constructor given sequence data and score calculation parameter options.
59 * The initial state is to compute PCA using a default score model (BLOSUM62
60 * for peptide, DNA for nucleotide).
67 public PCAModel(AlignmentView seqData, SequenceI[] sqs, boolean nuc,
68 SimilarityParamsI params)
73 scoreModel = ScoreModels.getInstance().getDefaultModel(!nucleotide);
74 similarityParams = params;
77 public boolean isJvCalcMode()
84 pca = new PCA(seqstrings, scoreModel, similarityParams);
85 pca.setJvCalcMode(jvCalcMode);
88 // Now find the component coordinates
91 while ((ii < seqs.length) && (seqs[ii] != null))
96 int height = pca.getHeight();
97 // top = pca.getM().height() - 1;
100 points = new Vector<SequencePoint>();
101 float[][] scores = pca.getComponents(top - 1, top - 2, top - 3, 100);
103 for (int i = 0; i < height; i++)
105 SequencePoint sp = new SequencePoint(seqs[i], scores[i]);
106 points.addElement(sp);
110 public void updateRc(RotatableCanvasI rc)
112 rc.setPoints(points, pca.getHeight());
115 public boolean isNucleotide()
120 public void setNucleotide(boolean nucleotide)
122 this.nucleotide = nucleotide;
128 * @return index of principle dimension of PCA
136 * update the 2d coordinates for the list of points to the given dimensions
137 * Principal dimension is getTop(). Next greatest eigenvector is getTop()-1.
138 * Note - pca.getComponents starts counting the spectrum from rank-2 to zero,
139 * rather than rank-1, so getComponents(dimN ...) == updateRcView(dimN+1 ..)
145 public void updateRcView(int dim1, int dim2, int dim3)
147 // note: actual indices for components are dim1-1, etc (patch for JAL-1123)
148 float[][] scores = pca.getComponents(dim1 - 1, dim2 - 1, dim3 - 1, 100);
150 for (int i = 0; i < pca.getHeight(); i++)
152 points.elementAt(i).coord = scores[i];
156 public String getDetails()
158 return pca.getDetails();
161 public AlignmentView getSeqtrings()
166 public String getPointsasCsv(boolean transformed, int xdim, int ydim,
169 StringBuffer csv = new StringBuffer();
170 csv.append("\"Sequence\"");
182 for (int d = 1, dmax = pca.component(1).length; d <= dmax; d++)
188 for (int s = 0; s < seqs.length; s++)
190 csv.append("\"" + seqs[s].getName() + "\"");
194 // output pca in correct order
195 fl = pca.component(s);
196 for (int d = fl.length - 1; d >= 0; d--)
204 // output current x,y,z coords for points
205 fl = getPointPosition(s);
206 for (int d = 0; d < fl.length; d++)
214 return csv.toString();
219 * @return x,y,z positions of point s (index into points) under current
222 public double[] getPointPosition(int s)
224 double pts[] = new double[3];
225 float[] p = points.elementAt(s).coord;
232 public void setJvCalcMode(boolean state)
237 public String getScoreModelName()
239 return scoreModel == null ? "" : scoreModel.getName();
242 public void setScoreModel(ScoreModelI sm)
244 this.scoreModel = sm;