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;
26 AlignmentView seqstrings;
31 * use the identity matrix for calculating similarity between sequences.
33 private boolean nucleotide=false;
35 private Vector<SequencePoint> points;
40 pca = new PCA(seqstrings.getSequenceStrings(' '), nucleotide);
43 // Now find the component coordinates
46 while ((ii < seqs.length) && (seqs[ii] != null))
51 double[][] comps = new double[ii][ii];
53 for (int i = 0; i < ii; i++)
55 if (pca.getEigenvalue(i) > 1e-4)
57 comps[i] = pca.component(i);
61 top = pca.getM().rows - 1;
63 points = new Vector<SequencePoint>();
64 float[][] scores = pca.getComponents(top - 1, top - 2, top - 3, 100);
66 for (int i = 0; i < pca.getM().rows; i++)
68 SequencePoint sp = new SequencePoint(seqs[i], scores[i]);
69 points.addElement(sp);
74 public void updateRc(RotatableCanvasI rc)
76 rc.setPoints(points, pca.getM().rows);
79 public boolean isNucleotide()
83 public void setNucleotide(boolean nucleotide)
85 this.nucleotide=nucleotide;
91 * @return index of principle dimension of PCA
99 * update the 2d coordinates for the list of points to the given dimensions
100 * Principal dimension is getTop(). Next greatest eigenvector is getTop()-1.
101 * Note - pca.getComponents starts counting the spectrum from rank-2 to zero, rather than rank-1, so getComponents(dimN ...) == updateRcView(dimN+1 ..)
106 public void updateRcView(int dim1, int dim2, int dim3)
108 // note: actual indices for components are dim1-1, etc (patch for JAL-1123)
109 float[][] scores = pca.getComponents(dim1-1, dim2-1, dim3-1, 100);
111 for (int i = 0; i < pca.getM().rows; i++)
113 ((SequencePoint) points.elementAt(i)).coord = scores[i];
117 public String getDetails()
119 return pca.getDetails();
122 public AlignmentView getSeqtrings()
126 public String getPointsasCsv(boolean transformed, int xdim, int ydim, int zdim)
128 StringBuffer csv = new StringBuffer();
129 csv.append("\"Sequence\"");
141 for (int d = 1, dmax = pca.component(1).length; d <= dmax; d++)
147 for (int s = 0; s < seqs.length; s++)
149 csv.append("\"" + seqs[s].getName() + "\"");
153 // output pca in correct order
154 fl = pca.component(s);
155 for (int d = fl.length - 1; d >= 0; d--)
163 // output current x,y,z coords for points
164 fl = getPointPosition(s);
165 for (int d = 0; d < fl.length; d++)
173 return csv.toString();
178 * @return x,y,z positions of point s (index into points) under current
181 public double[] getPointPosition(int s)
183 double pts[] = new double[3];
184 float[] p = points.elementAt(s).coord;