1 package jalview.viewmodel;
3 import java.util.Vector;
5 import jalview.analysis.PCA;
6 import jalview.datamodel.AlignmentView;
7 import jalview.datamodel.SequenceI;
8 import jalview.datamodel.SequencePoint;
9 import jalview.api.RotatableCanvasI;
14 public PCAModel(AlignmentView seqstrings2, SequenceI[] seqs2,
17 seqstrings = seqstrings2;
19 nucleotide = nucleotide2;
22 private volatile PCA pca;
26 AlignmentView seqstrings;
31 * use the identity matrix for calculating similarity between sequences.
33 private boolean nucleotide = false;
35 private Vector<SequencePoint> points;
37 private boolean jvCalcMode = true;
39 public boolean isJvCalcMode()
47 pca = new PCA(seqstrings.getSequenceStrings(' '), nucleotide);
48 pca.setJvCalcMode(jvCalcMode);
51 // Now find the component coordinates
54 while ((ii < seqs.length) && (seqs[ii] != null))
59 double[][] comps = new double[ii][ii];
61 for (int i = 0; i < ii; i++)
63 if (pca.getEigenvalue(i) > 1e-4)
65 comps[i] = pca.component(i);
69 top = pca.getM().rows - 1;
71 points = new Vector<SequencePoint>();
72 float[][] scores = pca.getComponents(top - 1, top - 2, top - 3, 100);
74 for (int i = 0; i < pca.getM().rows; i++)
76 SequencePoint sp = new SequencePoint(seqs[i], scores[i]);
77 points.addElement(sp);
82 public void updateRc(RotatableCanvasI rc)
84 rc.setPoints(points, pca.getM().rows);
87 public boolean isNucleotide()
92 public void setNucleotide(boolean nucleotide)
94 this.nucleotide = nucleotide;
100 * @return index of principle dimension of PCA
108 * update the 2d coordinates for the list of points to the given dimensions
109 * Principal dimension is getTop(). Next greatest eigenvector is getTop()-1.
110 * Note - pca.getComponents starts counting the spectrum from rank-2 to zero,
111 * rather than rank-1, so getComponents(dimN ...) == updateRcView(dimN+1 ..)
117 public void updateRcView(int dim1, int dim2, int dim3)
119 // note: actual indices for components are dim1-1, etc (patch for JAL-1123)
120 float[][] scores = pca.getComponents(dim1 - 1, dim2 - 1, dim3 - 1, 100);
122 for (int i = 0; i < pca.getM().rows; i++)
124 ((SequencePoint) points.elementAt(i)).coord = scores[i];
128 public String getDetails()
130 return pca.getDetails();
133 public AlignmentView getSeqtrings()
138 public String getPointsasCsv(boolean transformed, int xdim, int ydim,
141 StringBuffer csv = new StringBuffer();
142 csv.append("\"Sequence\"");
154 for (int d = 1, dmax = pca.component(1).length; d <= dmax; d++)
160 for (int s = 0; s < seqs.length; s++)
162 csv.append("\"" + seqs[s].getName() + "\"");
166 // output pca in correct order
167 fl = pca.component(s);
168 for (int d = fl.length - 1; d >= 0; d--)
176 // output current x,y,z coords for points
177 fl = getPointPosition(s);
178 for (int d = 0; d < fl.length; d++)
186 return csv.toString();
191 * @return x,y,z positions of point s (index into points) under current
194 public double[] getPointPosition(int s)
196 double pts[] = new double[3];
197 float[] p = points.elementAt(s).coord;
204 public void setJvCalcMode(boolean state)