2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ 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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.viewmodel;
23 import jalview.analysis.PCA;
24 import jalview.api.RotatableCanvasI;
25 import jalview.datamodel.AlignmentView;
26 import jalview.datamodel.SequenceI;
27 import jalview.datamodel.SequencePoint;
29 import java.util.Vector;
34 * Jalview 2.10.1 treated gaps as X (peptide) or N (nucleotide)
35 * for pairwise scoring; 2.10.2 uses gap score (last column) in
36 * score matrix (JAL-2397)
37 * Set this flag to true (via Groovy) for 2.10.1 behaviour
39 private static boolean scoreGapAsAny = false;
41 public PCAModel(AlignmentView seqstrings2, SequenceI[] seqs2,
44 seqstrings = seqstrings2;
46 nucleotide = nucleotide2;
47 score_matrix = nucleotide2 ? "PID" : "BLOSUM62";
50 private volatile PCA pca;
54 AlignmentView seqstrings;
59 * Score matrix used to calculate PC
64 * use the identity matrix for calculating similarity between sequences.
66 private boolean nucleotide = false;
68 private Vector<SequencePoint> points;
70 private boolean jvCalcMode = true;
72 public boolean isJvCalcMode()
79 char gapChar = scoreGapAsAny ? (nucleotide ? 'N' : 'X') : ' ';
80 String[] sequenceStrings = seqstrings.getSequenceStrings(gapChar);
81 pca = new PCA(sequenceStrings, nucleotide,
83 pca.setJvCalcMode(jvCalcMode);
86 // Now find the component coordinates
89 while ((ii < seqs.length) && (seqs[ii] != null))
94 int height = pca.getHeight();
95 // top = pca.getM().height() - 1;
98 points = new Vector<SequencePoint>();
99 float[][] scores = pca.getComponents(top - 1, top - 2, top - 3, 100);
101 for (int i = 0; i < height; i++)
103 SequencePoint sp = new SequencePoint(seqs[i], scores[i]);
104 points.addElement(sp);
108 public void updateRc(RotatableCanvasI rc)
110 rc.setPoints(points, pca.getHeight());
113 public boolean isNucleotide()
118 public void setNucleotide(boolean nucleotide)
120 this.nucleotide = nucleotide;
126 * @return index of principle dimension of PCA
134 * update the 2d coordinates for the list of points to the given dimensions
135 * Principal dimension is getTop(). Next greatest eigenvector is getTop()-1.
136 * Note - pca.getComponents starts counting the spectrum from rank-2 to zero,
137 * rather than rank-1, so getComponents(dimN ...) == updateRcView(dimN+1 ..)
143 public void updateRcView(int dim1, int dim2, int dim3)
145 // note: actual indices for components are dim1-1, etc (patch for JAL-1123)
146 float[][] scores = pca.getComponents(dim1 - 1, dim2 - 1, dim3 - 1, 100);
148 for (int i = 0; i < pca.getHeight(); i++)
150 points.elementAt(i).coord = scores[i];
154 public String getDetails()
156 return pca.getDetails();
159 public AlignmentView getSeqtrings()
164 public String getPointsasCsv(boolean transformed, int xdim, int ydim,
167 StringBuffer csv = new StringBuffer();
168 csv.append("\"Sequence\"");
180 for (int d = 1, dmax = pca.component(1).length; d <= dmax; d++)
186 for (int s = 0; s < seqs.length; s++)
188 csv.append("\"" + seqs[s].getName() + "\"");
192 // output pca in correct order
193 fl = pca.component(s);
194 for (int d = fl.length - 1; d >= 0; d--)
202 // output current x,y,z coords for points
203 fl = getPointPosition(s);
204 for (int d = 0; d < fl.length; d++)
212 return csv.toString();
217 * @return x,y,z positions of point s (index into points) under current
220 public double[] getPointPosition(int s)
222 double pts[] = new double[3];
223 float[] p = points.elementAt(s).coord;
230 public void setJvCalcMode(boolean state)
235 public String getScore_matrix()
240 public void setScore_matrix(String score_matrix)
242 this.score_matrix = score_matrix;