2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
18 package jalview.viewmodel;
20 import java.util.Vector;
22 import jalview.analysis.PCA;
23 import jalview.datamodel.AlignmentView;
24 import jalview.datamodel.SequenceI;
25 import jalview.datamodel.SequencePoint;
26 import jalview.api.RotatableCanvasI;
31 public PCAModel(AlignmentView seqstrings2, SequenceI[] seqs2,
34 seqstrings = seqstrings2;
36 nucleotide = nucleotide2;
39 private volatile PCA pca;
43 AlignmentView seqstrings;
48 * use the identity matrix for calculating similarity between sequences.
50 private boolean nucleotide = false;
52 private Vector<SequencePoint> points;
54 private boolean jvCalcMode = true;
56 public boolean isJvCalcMode()
64 pca = new PCA(seqstrings.getSequenceStrings(' '), nucleotide);
65 pca.setJvCalcMode(jvCalcMode);
68 // Now find the component coordinates
71 while ((ii < seqs.length) && (seqs[ii] != null))
76 double[][] comps = new double[ii][ii];
78 for (int i = 0; i < ii; i++)
80 if (pca.getEigenvalue(i) > 1e-4)
82 comps[i] = pca.component(i);
86 top = pca.getM().rows - 1;
88 points = new Vector<SequencePoint>();
89 float[][] scores = pca.getComponents(top - 1, top - 2, top - 3, 100);
91 for (int i = 0; i < pca.getM().rows; i++)
93 SequencePoint sp = new SequencePoint(seqs[i], scores[i]);
94 points.addElement(sp);
99 public void updateRc(RotatableCanvasI rc)
101 rc.setPoints(points, pca.getM().rows);
104 public boolean isNucleotide()
109 public void setNucleotide(boolean nucleotide)
111 this.nucleotide = nucleotide;
117 * @return index of principle dimension of PCA
125 * update the 2d coordinates for the list of points to the given dimensions
126 * Principal dimension is getTop(). Next greatest eigenvector is getTop()-1.
127 * Note - pca.getComponents starts counting the spectrum from rank-2 to zero,
128 * rather than rank-1, so getComponents(dimN ...) == updateRcView(dimN+1 ..)
134 public void updateRcView(int dim1, int dim2, int dim3)
136 // note: actual indices for components are dim1-1, etc (patch for JAL-1123)
137 float[][] scores = pca.getComponents(dim1 - 1, dim2 - 1, dim3 - 1, 100);
139 for (int i = 0; i < pca.getM().rows; i++)
141 ((SequencePoint) points.elementAt(i)).coord = scores[i];
145 public String getDetails()
147 return pca.getDetails();
150 public AlignmentView getSeqtrings()
155 public String getPointsasCsv(boolean transformed, int xdim, int ydim,
158 StringBuffer csv = new StringBuffer();
159 csv.append("\"Sequence\"");
171 for (int d = 1, dmax = pca.component(1).length; d <= dmax; d++)
177 for (int s = 0; s < seqs.length; s++)
179 csv.append("\"" + seqs[s].getName() + "\"");
183 // output pca in correct order
184 fl = pca.component(s);
185 for (int d = fl.length - 1; d >= 0; d--)
193 // output current x,y,z coords for points
194 fl = getPointPosition(s);
195 for (int d = 0; d < fl.length; d++)
203 return csv.toString();
208 * @return x,y,z positions of point s (index into points) under current
211 public double[] getPointPosition(int s)
213 double pts[] = new double[3];
214 float[] p = points.elementAt(s).coord;
221 public void setJvCalcMode(boolean state)