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.
23 import jalview.analysis.AlignSeq;
24 import jalview.analysis.scoremodels.ScoreMatrix;
25 import jalview.datamodel.Alignment;
26 import jalview.datamodel.AlignmentView;
27 import jalview.datamodel.SequenceGroup;
28 import jalview.datamodel.SequenceI;
29 import jalview.jbgui.GPairwiseAlignPanel;
30 import jalview.util.MessageManager;
31 import jalview.viewmodel.AlignmentViewport;
32 import jalview.math.MiscMath;
34 import java.awt.event.ActionEvent;
35 import java.util.Vector;
43 public class PairwiseAlignPanel extends GPairwiseAlignPanel
46 private static final String DASHES = "---------------------\n";
48 private float[][] scores;
50 private float[][] alignmentScores; // scores used by PaSiMap
52 private int GAP_OPEN_COST;
54 private int GAP_EXTEND_COST;
58 Vector<SequenceI> sequences;
60 private String alignmentOutput;
62 private boolean quiet;
64 private boolean discardAlignments;
66 private boolean endGaps;
69 public static final String TOTAL = "total";
71 public static final String PROGRESS = "progress";
77 private SequenceGroup selection;
81 private SequenceI[] seqs=null;
83 private ScoreMatrix scoreMatrix;
86 * Creates a new PairwiseAlignPanel object.
89 * contains selected sequences to align
90 * @param endGaps ~ toggle gaps and the beginning and end of sequences
92 public PairwiseAlignPanel(AlignmentViewport viewport)
94 this(viewport, null, false, 120, 20, true, null); // default penalties used in AlignSeq
96 public PairwiseAlignPanel(AlignmentViewport viewport, ScoreMatrix params)
98 this(viewport, null, false, 120, 20, true,params); // default penalties used in AlignSeq
100 public PairwiseAlignPanel(AlignmentViewport viewport, boolean endGaps, int gapOpenCost, int gapExtendCost)
102 this(viewport, null, endGaps, gapOpenCost, gapExtendCost, true, null);
106 * Create a new pairwise alignpanel with specified parameters and score model, and optionally start the calculation
111 * @param gapExtendCost
115 public PairwiseAlignPanel(AlignmentViewport viewport, SequenceGroup selection, boolean endGaps,
116 int gapOpenCost, int gapExtendCost, boolean run, ScoreMatrix scoreMatrix)
120 this.GAP_OPEN_COST = gapOpenCost;
121 this.GAP_EXTEND_COST = gapExtendCost;
122 this.endGaps = endGaps;
123 this.selection = selection;
124 this.total = MiscMath.combinations(av.getAlignment().getHeight(), 2);
125 this.scoreMatrix = scoreMatrix;
131 public void calculate()
133 calculate(scoreMatrix);
135 public void calculate (ScoreMatrix sm)
138 StringBuilder sb = new StringBuilder(1024);
140 sequences = new Vector<SequenceI>();
144 if (selection != null)
146 // given a set of sequences to compare
147 seqs = selection.getSelectionAsNewSequences(av.getAlignment());
148 seqStrings = new String[seqs.length];
150 for (SequenceI seq : seqs)
152 seqStrings[s++] = seq.getSequenceAsString();
157 SequenceGroup selectionGroup = av.getSelectionGroup();
158 boolean isSelection = selectionGroup != null
159 && selectionGroup.getSize() > 0;
160 AlignmentView view = av.getAlignmentView(isSelection);
161 seqStrings = view.getSequenceStrings(av.getGapCharacter());
164 seqs = (SequenceI[]) view
165 .getAlignmentAndHiddenColumns(av.getGapCharacter())[0];
169 seqs = av.getAlignment().getSequencesArray();
173 String type = (av.getAlignment().isNucleotide()) ? AlignSeq.DNA
176 float[][] scores = new float[seqs.length][seqs.length];
177 float[][] alignmentScores = new float[seqs.length][seqs.length];
178 double totscore = 0D;
179 int count = seqs.length;
181 boolean first = true;
184 firePropertyChange(TOTAL, 0, total);
186 for (int i = 1; i < count; i++)
188 // fill diagonal alignmentScores with Float.NaN
189 alignmentScores[i - 1][i - 1] = Float.NaN;
190 for (int j = 0; j < i; j++)
192 AlignSeq as = new AlignSeq(seqs[i], seqStrings[i], seqs[j],
193 seqStrings[j], type, GAP_OPEN_COST, GAP_EXTEND_COST);
196 as.setScoreMatrix(sm);
199 if (as.s1str.length() == 0 || as.s2str.length() == 0)
204 as.calcScoreMatrix();
207 as.traceAlignmentWithEndGaps();
215 if (!first && !quiet)
217 jalview.bin.Console.outPrintln(DASHES);
218 textarea.append(DASHES);
222 if (!discardAlignments) {
223 as.printAlignment(System.out);
225 scores[i][j] = as.getMaxScore() / as.getASeq1().length;
226 alignmentScores[i][j] = as.getAlignmentScore();
227 totscore = totscore + scores[i][j];
230 textarea.append(as.getOutput());
231 sb.append(as.getOutput());
233 if (!discardAlignments)
235 sequences.add(as.getAlignedSeq1());
236 sequences.add(as.getAlignedSeq2());
238 firePropertyChange(PROGRESS, progress, ++progress);
241 alignmentScores[count - 1][count - 1] = Float.NaN;
243 this.scores = scores;
244 this.alignmentScores = alignmentScores;
246 if (count > 2 && !quiet)
248 printScoreMatrix(seqs, scores, totscore);
251 alignmentOutput = sb.toString();
254 public float[][] getScores()
259 public float[][] getAlignmentScores()
261 return this.alignmentScores;
264 public String getAlignmentOutput()
266 return this.alignmentOutput;
270 * Prints a matrix of seqi-seqj pairwise alignment scores to sysout
276 protected void printScoreMatrix(SequenceI[] seqs, float[][] scores,
280 .println("Pairwise alignment scaled similarity score matrix\n");
282 for (int i = 0; i < seqs.length; i++)
284 jalview.bin.Console.outPrintln(
285 String.format("%3d %s", i + 1, seqs[i].getDisplayId(true)));
289 * table heading columns for sequences 1, 2, 3...
291 System.out.print("\n ");
292 for (int i = 0; i < seqs.length; i++)
294 System.out.print(String.format("%7d", i + 1));
296 jalview.bin.Console.outPrintln();
298 for (int i = 0; i < seqs.length; i++)
300 System.out.print(String.format("%3d", i + 1));
301 for (int j = 0; j < i; j++)
304 * as a fraction of tot score, outputs are 0 <= score <= 1
306 System.out.print(String.format("%7.3f", scores[i][j] / totscore));
308 jalview.bin.Console.outPrintln();
311 jalview.bin.Console.outPrintln("\n");
321 protected void viewInEditorButton_actionPerformed(ActionEvent e)
323 SequenceI[] seq = new SequenceI[sequences.size()];
325 for (int i = 0; i < sequences.size(); i++)
327 seq[i] = sequences.elementAt(i);
330 AlignFrame af = new AlignFrame(new Alignment(seq),
331 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
333 Desktop.addInternalFrame(af,
334 MessageManager.getString("label.pairwise_aligned_sequences"),
335 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
338 public long getTotal()
343 public long getProgress()
347 public SequenceI[] getInputSequences()
352 * Set to true to suppress output of progress to Console.stdout or GUI
355 public void setQuiet(boolean quiet)
360 * set this if you are only interested in final alignment scores
363 public void setDiscardAlignments(boolean discard)
365 discardAlignments=discard;