Clover icon

Coverage Report

  1. Project Clover database Mon Nov 11 2024 15:14:12 GMT
  2. Package jalview.viewmodel

File SpatialModel.java

 

Coverage histogram

../../img/srcFileCovDistChart3.png
52% of files have more coverage

Code metrics

20
84
28
1
325
228
39
0.46
3
28
1.39

Classes

Class Line # Actions
SpatialModel 21 84 39
0.2954545329.5%
 

Contributing tests

This file is covered by 1 test. .

Source view

1    package jalview.viewmodel;
2   
3    import java.util.Arrays;
4    import java.util.List;
5    import java.util.Set;
6    import java.util.Vector;
7   
8    import jalview.analysis.Clusterer;
9    import jalview.analysis.SpatialCalculationI;
10    import jalview.api.RotatableCanvasI;
11    import jalview.api.analysis.ScoreModelI;
12    import jalview.api.analysis.SimilarityParamsI;
13    import jalview.datamodel.AlignmentView;
14    import jalview.datamodel.Point;
15    import jalview.datamodel.SequenceGroup;
16    import jalview.datamodel.SequenceI;
17    import jalview.datamodel.SequencePoint;
18    import jalview.math.Matrix;
19    import jalview.math.MatrixI;
20   
 
21    public abstract class SpatialModel<T extends SpatialCalculationI>
22    {
23    /*
24    * inputs
25    */
26    protected AlignmentViewport av;
27   
28    protected AlignmentView inputData;
29   
30    protected final SequenceI[] seqs;
31   
32    protected final SimilarityParamsI similarityParams;
33   
34    /*
35    * options - score model, nucleotide / protein
36    */
37    protected ScoreModelI scoreModel;
38   
39    protected boolean nucleotide = false;
40   
41    protected T outputModel;
42   
43    private int top;
44   
45    private List<SequencePoint> points;
46   
47    /**
48    * Constructor given sequence data, score model and score calculation
49    * parameter options.
50    *
51    * @param seqData
52    * @param sqs
53    * @param nuc
54    * @param modelName
55    * @param params
56    */
 
57  2 toggle public SpatialModel(AlignmentViewport av, AlignmentView seqData, SequenceI[] sqs, boolean nuc,
58    ScoreModelI modelName, SimilarityParamsI params)
59    {
60  2 this.av = av;
61  2 inputData = seqData;
62  2 seqs = sqs;
63  2 nucleotide = nuc;
64  2 scoreModel = modelName;
65  2 similarityParams = params;
66    }
67   
68    public abstract T constructModel();
69   
 
70  1 toggle public void calculate()
71    {
72  1 outputModel = constructModel();
73  1 outputModel.run(); // executes in same thread, wait for completion
74  1 if (outputModel.isCancelled())
75    {
76    // no more work to do
77  0 return;
78    }
79   
80    // Now find the component coordinates
81    // // delete this ?
82    // int ii = 0;
83    //
84    // while ((ii < seqs.length) && (seqs[ii] != null))
85    // {
86    // ii++;
87    // }
88   
89    // NB height for PaSiMap is Width.
90  1 int height = outputModel.getHeight();
91  1 int width = outputModel.getWidth();
92   
93    // top = pca.getM().height() - 1;
94    // top = pasimap.getWidth() - 1;
95  1 top = outputModel.getTop();
96   
97  1 points = new Vector<>();
98  1 Point[] scores = outputModel.getComponents(top - 1, top - 2, top - 3, 1f);
99   
100  16 for (int i = 0; i < height; i++)
101    {
102  15 SequencePoint sp = new SequencePoint(seqs[i], scores[i]);
103  15 points.add(sp);
104    }
105    }
106   
 
107  1 toggle public void updateRc(RotatableCanvasI rc)
108    {
109  1 rc.setPoints(points, outputModel.getHeight());
110    }
111   
 
112  0 toggle public boolean isNucleotide()
113    {
114  0 return nucleotide;
115    }
116   
 
117  0 toggle public void setNucleotide(boolean nucleotide)
118    {
119  0 this.nucleotide = nucleotide;
120    }
121   
122    /**
123    * Answers the index of the principal dimension of the PCA
124    *
125    * @return
126    */
 
127  1 toggle public int getTop()
128    {
129  1 return top;
130    }
131    /** ? used ?
132    *
133    * @param t
134    */
 
135  1 toggle public void setTop(int t)
136    {
137  1 top = t;
138    }
139   
140    /**
141    * Updates the 3D coordinates for the list of points to the given dimensions.
142    * Principal dimension is getTop(). Next greatest eigenvector is getTop()-1.
143    * Note - pca.getComponents starts counting the spectrum from rank-2 to zero,
144    * rather than rank-1, so getComponents(dimN ...) == updateRcView(dimN+1 ..)
145    *
146    * @param dim1
147    * @param dim2
148    * @param dim3
149    */
 
150  0 toggle public void updateRcView(int dim1, int dim2, int dim3)
151    {
152    // note: actual indices for components are dim1-1, etc (patch for JAL-1123)
153  0 Point[] scores = outputModel.getComponents(dim1 - 1, dim2 - 1, dim3 - 1, 1);
154   
155  0 for (int i = 0; i < outputModel.getHeight(); i++)
156    {
157  0 points.get(i).coord = scores[i];
158    }
159    }
160   
 
161  0 toggle public String getDetails()
162    {
163  0 return outputModel.getDetails();
164    }
165   
 
166  0 toggle public String getAlignmentOutput()
167    {
168  0 return outputModel.getAlignmentOutput();
169    }
170   
 
171  0 toggle public AlignmentViewport getInputAlignmentView()
172    {
173  0 return av;
174    }
 
175  0 toggle public AlignmentView getInputData()
176    {
177  0 return inputData;
178    }
179   
 
180  0 toggle public String getPointsasCsv(boolean transformed, int xdim, int ydim,
181    int zdim)
182    {
183  0 StringBuffer csv = new StringBuffer();
184  0 csv.append("\"Sequence\"");
185  0 if (transformed)
186    {
187  0 csv.append(",");
188  0 csv.append(xdim);
189  0 csv.append(",");
190  0 csv.append(ydim);
191  0 csv.append(",");
192  0 csv.append(zdim);
193    }
194    else
195    {
196    // pca.component(1).length == pasimap.getDim
197  0 for (int d = 1, dmax = (int) outputModel.getDim(); d <= dmax; d++)
198    {
199  0 csv.append("," + d);
200    }
201    }
202  0 csv.append("\n");
203  0 for (int s = 0; s < seqs.length; s++)
204    {
205  0 csv.append("\"" + seqs[s].getName() + "\"");
206  0 if (!transformed)
207    {
208  0 double[] fl = outputModel.component(s);
209  0 for (int d = fl.length - 1; d >= 0; d--)
210    {
211  0 csv.append(",");
212  0 csv.append(fl[d]);
213    }
214    }
215    else
216    {
217  0 Point p = points.get(s).coord;
218  0 csv.append(",").append(p.x);
219  0 csv.append(",").append(p.y);
220  0 csv.append(",").append(p.z);
221    }
222  0 csv.append("\n");
223    }
224  0 return csv.toString();
225    }
226   
 
227  3 toggle public String getScoreModelName()
228    {
229  3 return scoreModel == null ? "" : scoreModel.getName();
230    }
231   
 
232  0 toggle public void setScoreModel(ScoreModelI sm)
233    {
234  0 this.scoreModel = sm;
235    }
236   
237    /**
238    * Answers the parameters configured for pairwise similarity calculations
239    *
240    * @return
241    */
 
242  1 toggle public SimilarityParamsI getSimilarityParameters()
243    {
244  1 return similarityParams;
245    }
246   
 
247  1 toggle public List<SequencePoint> getSequencePoints()
248    {
249  1 return points;
250    }
251   
 
252  1 toggle public void setSequencePoints(List<SequencePoint> sp)
253    {
254  1 points = sp;
255    }
 
256  0 toggle public T getOutputModel() {
257  0 return outputModel;
258    }
 
259  0 toggle public void setOutputModel(T newModel)
260    {
261  0 outputModel = newModel;
262    }
263   
 
264  0 toggle public boolean isCancelled()
265    {
266  0 if (outputModel==null || outputModel.isCancelled())
267    {
268  0 return true;
269    }
270  0 return false;
271    }
272   
 
273  0 toggle public void cancel()
274    {
275  0 outputModel.cancel();
276    }
277   
 
278  0 toggle public boolean canCancel()
279    {
280  0 return (!isCancelled() && outputModel.isCancellable());
281    }
282   
283    List<SequenceGroup> groups = Arrays.asList(new SequenceGroup[] {});
 
284  0 toggle List<SequenceGroup> getGroups() {
285  0 return groups;
286    }
287   
 
288  0 toggle public void markGroups()
289    {
290  0 av.getAlignment().deleteAllGroups();
291  0 int w = av.getAlignment().getWidth();
292    // replace all groups with new groups
293    // assign a random(ish) idColour
294  0 for (jalview.datamodel.SequenceGroup sg : groups)
295    {
296  0 sg.setStartRes(0);
297  0 sg.setEndRes(w);
298  0 sg.idColour = new java.awt.Color((int) (Math.random() * 255),
299    (int) (Math.random() * 255), (int) (Math.random() * 255));
300  0 sg.recalcConservation();
301  0 av.getAlignment().addGroup(sg);
302  0 for (SequenceI sq : sg.getSequences())
303    {
304  0 av.setSequenceColour(sq, sg.getIdColour());
305    }
306    }
307    }
 
308  1 toggle public List<String> getClusterOptions()
309    {
310  1 return Arrays.asList(outputModel.getDistanceMatrices().keySet().toArray(new String[0]));
311    }
 
312  0 toggle public MatrixI getSimilarityMatrixFor(String key)
313    {
314  0 return outputModel.getDistanceMatrices().get(key);
315    }
316   
 
317  0 toggle public void doClustering(String method)
318    {
319  0 Clusterer ourClusterer = new Clusterer();
320  0 MatrixI distanceMatrx = outputModel.getDistanceMatrices().get(method);
321  0 ourClusterer.cluster(distanceMatrx);
322  0 groups = ourClusterer.createGroups(seqs);
323    }
324   
325    }