aa90b93f9009485a12fa9d3ac4cc294442458e15
[jalview.git] / src / jalview / viewmodel / PCAModel.java
1 package jalview.viewmodel;
2
3 import java.util.Vector;
4
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;
10
11 public class PCAModel
12 {
13
14   public PCAModel(AlignmentView seqstrings2, SequenceI[] seqs2,
15           boolean nucleotide2)
16   {
17     seqstrings = seqstrings2;
18     seqs = seqs2;
19     nucleotide = nucleotide2;
20   }
21
22   private volatile PCA pca;
23
24   int top;
25
26   AlignmentView seqstrings;
27
28   SequenceI[] seqs;
29
30   /**
31    * use the identity matrix for calculating similarity between sequences.
32    */
33   private boolean nucleotide = false;
34
35   private Vector<SequencePoint> points;
36
37   private boolean jvCalcMode = true;
38
39   public boolean isJvCalcMode()
40   {
41     return jvCalcMode;
42   }
43
44   public void run()
45   {
46
47     pca = new PCA(seqstrings.getSequenceStrings(' '), nucleotide);
48     pca.setJvCalcMode(jvCalcMode);
49     pca.run();
50
51     // Now find the component coordinates
52     int ii = 0;
53
54     while ((ii < seqs.length) && (seqs[ii] != null))
55     {
56       ii++;
57     }
58
59     double[][] comps = new double[ii][ii];
60
61     for (int i = 0; i < ii; i++)
62     {
63       if (pca.getEigenvalue(i) > 1e-4)
64       {
65         comps[i] = pca.component(i);
66       }
67     }
68
69     top = pca.getM().rows - 1;
70
71     points = new Vector<SequencePoint>();
72     float[][] scores = pca.getComponents(top - 1, top - 2, top - 3, 100);
73
74     for (int i = 0; i < pca.getM().rows; i++)
75     {
76       SequencePoint sp = new SequencePoint(seqs[i], scores[i]);
77       points.addElement(sp);
78     }
79
80   }
81
82   public void updateRc(RotatableCanvasI rc)
83   {
84     rc.setPoints(points, pca.getM().rows);
85   }
86
87   public boolean isNucleotide()
88   {
89     return nucleotide;
90   }
91
92   public void setNucleotide(boolean nucleotide)
93   {
94     this.nucleotide = nucleotide;
95   }
96
97   /**
98    * 
99    * 
100    * @return index of principle dimension of PCA
101    */
102   public int getTop()
103   {
104     return top;
105   }
106
107   /**
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 ..)
112    * 
113    * @param dim1
114    * @param dim2
115    * @param dim3
116    */
117   public void updateRcView(int dim1, int dim2, int dim3)
118   {
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);
121
122     for (int i = 0; i < pca.getM().rows; i++)
123     {
124       ((SequencePoint) points.elementAt(i)).coord = scores[i];
125     }
126   }
127
128   public String getDetails()
129   {
130     return pca.getDetails();
131   }
132
133   public AlignmentView getSeqtrings()
134   {
135     return seqstrings;
136   }
137
138   public String getPointsasCsv(boolean transformed, int xdim, int ydim,
139           int zdim)
140   {
141     StringBuffer csv = new StringBuffer();
142     csv.append("\"Sequence\"");
143     if (transformed)
144     {
145       csv.append(",");
146       csv.append(xdim);
147       csv.append(",");
148       csv.append(ydim);
149       csv.append(",");
150       csv.append(zdim);
151     }
152     else
153     {
154       for (int d = 1, dmax = pca.component(1).length; d <= dmax; d++)
155       {
156         csv.append("," + d);
157       }
158     }
159     csv.append("\n");
160     for (int s = 0; s < seqs.length; s++)
161     {
162       csv.append("\"" + seqs[s].getName() + "\"");
163       double fl[];
164       if (!transformed)
165       {
166         // output pca in correct order
167         fl = pca.component(s);
168         for (int d = fl.length - 1; d >= 0; d--)
169         {
170           csv.append(",");
171           csv.append(fl[d]);
172         }
173       }
174       else
175       {
176         // output current x,y,z coords for points
177         fl = getPointPosition(s);
178         for (int d = 0; d < fl.length; d++)
179         {
180           csv.append(",");
181           csv.append(fl[d]);
182         }
183       }
184       csv.append("\n");
185     }
186     return csv.toString();
187   }
188
189   /**
190    * 
191    * @return x,y,z positions of point s (index into points) under current
192    *         transform.
193    */
194   public double[] getPointPosition(int s)
195   {
196     double pts[] = new double[3];
197     float[] p = points.elementAt(s).coord;
198     pts[0] = p[0];
199     pts[1] = p[1];
200     pts[2] = p[2];
201     return pts;
202   }
203
204   public void setJvCalcMode(boolean state)
205   {
206     jvCalcMode = state;
207   }
208
209 }