2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
3 * Copyright (C) 2014 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 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/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
19 package jalview.viewmodel;
21 import java.util.Vector;
23 import jalview.analysis.PCA;
24 import jalview.datamodel.AlignmentView;
25 import jalview.datamodel.SequenceI;
26 import jalview.datamodel.SequencePoint;
27 import jalview.api.RotatableCanvasI;
32 public PCAModel(AlignmentView seqstrings2, SequenceI[] seqs2,
35 seqstrings = seqstrings2;
37 nucleotide = nucleotide2;
38 score_matrix = nucleotide2 ? "PID" : "BLOSUM62";
41 private volatile PCA pca;
45 AlignmentView seqstrings;
50 * Score matrix used to calculate PC
55 * use the identity matrix for calculating similarity between sequences.
57 private boolean nucleotide = false;
59 private Vector<SequencePoint> points;
61 private boolean jvCalcMode = true;
63 public boolean isJvCalcMode()
71 pca = new PCA(seqstrings.getSequenceStrings(' '), nucleotide, score_matrix);
72 pca.setJvCalcMode(jvCalcMode);
75 // Now find the component coordinates
78 while ((ii < seqs.length) && (seqs[ii] != null))
83 double[][] comps = new double[ii][ii];
85 for (int i = 0; i < ii; i++)
87 if (pca.getEigenvalue(i) > 1e-4)
89 comps[i] = pca.component(i);
93 top = pca.getM().rows - 1;
95 points = new Vector<SequencePoint>();
96 float[][] scores = pca.getComponents(top - 1, top - 2, top - 3, 100);
98 for (int i = 0; i < pca.getM().rows; i++)
100 SequencePoint sp = new SequencePoint(seqs[i], scores[i]);
101 points.addElement(sp);
106 public void updateRc(RotatableCanvasI rc)
108 rc.setPoints(points, pca.getM().rows);
111 public boolean isNucleotide()
116 public void setNucleotide(boolean nucleotide)
118 this.nucleotide = nucleotide;
124 * @return index of principle dimension of PCA
132 * update the 2d coordinates for the list of points to the given dimensions
133 * Principal dimension is getTop(). Next greatest eigenvector is getTop()-1.
134 * Note - pca.getComponents starts counting the spectrum from rank-2 to zero,
135 * rather than rank-1, so getComponents(dimN ...) == updateRcView(dimN+1 ..)
141 public void updateRcView(int dim1, int dim2, int dim3)
143 // note: actual indices for components are dim1-1, etc (patch for JAL-1123)
144 float[][] scores = pca.getComponents(dim1 - 1, dim2 - 1, dim3 - 1, 100);
146 for (int i = 0; i < pca.getM().rows; i++)
148 ((SequencePoint) points.elementAt(i)).coord = scores[i];
152 public String getDetails()
154 return pca.getDetails();
157 public AlignmentView getSeqtrings()
162 public String getPointsasCsv(boolean transformed, int xdim, int ydim,
165 StringBuffer csv = new StringBuffer();
166 csv.append("\"Sequence\"");
178 for (int d = 1, dmax = pca.component(1).length; d <= dmax; d++)
184 for (int s = 0; s < seqs.length; s++)
186 csv.append("\"" + seqs[s].getName() + "\"");
190 // output pca in correct order
191 fl = pca.component(s);
192 for (int d = fl.length - 1; d >= 0; d--)
200 // output current x,y,z coords for points
201 fl = getPointPosition(s);
202 for (int d = 0; d < fl.length; d++)
210 return csv.toString();
215 * @return x,y,z positions of point s (index into points) under current
218 public double[] getPointPosition(int s)
220 double pts[] = new double[3];
221 float[] p = points.elementAt(s).coord;
228 public void setJvCalcMode(boolean state)
233 public String getScore_matrix()
238 public void setScore_matrix(String score_matrix)
240 this.score_matrix = score_matrix;