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.beans.PropertyChangeListener;
35 import java.awt.event.ActionEvent;
36 import java.util.Vector;
37 import javax.swing.event.SwingPropertyChangeSupport;
45 public class PairwiseAlignPanel extends GPairwiseAlignPanel
48 private static final String DASHES = "---------------------\n";
50 private float[][] scores;
52 private float[][] alignmentScores; // scores used by PaSiMap
54 private int GAP_OPEN_COST;
56 private int GAP_EXTEND_COST;
60 Vector<SequenceI> sequences;
62 private String alignmentOutput;
64 private boolean suppressTextbox;
66 private boolean discardAlignments;
68 private boolean endGaps;
71 public static final String TOTAL = "total";
73 public static final String PROGRESS = "progress";
79 private SequenceGroup selection;
83 private SequenceI[] seqs=null;
86 * Creates a new PairwiseAlignPanel object.
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); // default penalties used in AlignSeq
96 public PairwiseAlignPanel(AlignmentViewport viewport, boolean endGaps, int gapOpenCost, int gapExtendCost)
98 this(viewport, null, endGaps, gapOpenCost, gapExtendCost, true);
101 public PairwiseAlignPanel(AlignmentViewport viewport, SequenceGroup selection, boolean endGaps,
102 int gapOpenCost, int gapExtendCost, boolean run)
106 this.GAP_OPEN_COST = gapOpenCost;
107 this.GAP_EXTEND_COST = gapExtendCost;
108 this.endGaps = endGaps;
109 this.selection = selection;
110 this.total = MiscMath.combinations(av.getAlignment().getHeight(), 2);
114 System.out.println("Creating pap");
117 public void calculate()
121 public void calculate (ScoreMatrix sm)
124 StringBuilder sb = new StringBuilder(1024);
126 sequences = new Vector<SequenceI>();
130 if (selection != null)
132 // given a set of sequences to compare
133 seqs = selection.getSelectionAsNewSequences(av.getAlignment());
134 seqStrings = new String[seqs.length];
136 for (SequenceI seq : seqs)
138 seqStrings[s++] = seq.getSequenceAsString();
143 SequenceGroup selectionGroup = av.getSelectionGroup();
144 boolean isSelection = selectionGroup != null
145 && selectionGroup.getSize() > 0;
146 AlignmentView view = av.getAlignmentView(isSelection);
147 seqStrings = view.getSequenceStrings(av.getGapCharacter());
150 seqs = (SequenceI[]) view
151 .getAlignmentAndHiddenColumns(av.getGapCharacter())[0];
155 seqs = av.getAlignment().getSequencesArray();
159 String type = (av.getAlignment().isNucleotide()) ? AlignSeq.DNA
162 float[][] scores = new float[seqs.length][seqs.length];
163 float[][] alignmentScores = new float[seqs.length][seqs.length];
164 double totscore = 0D;
165 int count = seqs.length;
166 suppressTextbox = count>10;
167 discardAlignments = count>15;
168 boolean first = true;
171 firePropertyChange(TOTAL, 0, total);
173 for (int i = 1; i < count; i++)
175 // fill diagonal alignmentScores with Float.NaN
176 alignmentScores[i - 1][i - 1] = Float.NaN;
177 for (int j = 0; j < i; j++)
179 AlignSeq as = new AlignSeq(seqs[i], seqStrings[i], seqs[j],
180 seqStrings[j], type, GAP_OPEN_COST, GAP_EXTEND_COST);
182 as.setScoreMatrix(sm);
184 if (as.s1str.length() == 0 || as.s2str.length() == 0)
189 as.calcScoreMatrix();
192 as.traceAlignmentWithEndGaps();
200 if (!first && !suppressTextbox)
202 jalview.bin.Console.outPrintln(DASHES);
203 textarea.append(DASHES);
207 if (!discardAlignments) {
208 as.printAlignment(System.out);
210 scores[i][j] = as.getMaxScore() / as.getASeq1().length;
211 alignmentScores[i][j] = as.getAlignmentScore();
212 totscore = totscore + scores[i][j];
213 if (!suppressTextbox)
215 textarea.append(as.getOutput());
216 sb.append(as.getOutput());
218 if (!discardAlignments)
220 sequences.add(as.getAlignedSeq1());
221 sequences.add(as.getAlignedSeq2());
223 firePropertyChange(PROGRESS, progress, ++progress);
226 alignmentScores[count - 1][count - 1] = Float.NaN;
228 this.scores = scores;
229 this.alignmentScores = alignmentScores;
233 printScoreMatrix(seqs, scores, totscore);
236 alignmentOutput = sb.toString();
239 public float[][] getScores()
244 public float[][] getAlignmentScores()
246 return this.alignmentScores;
249 public String getAlignmentOutput()
251 return this.alignmentOutput;
255 * Prints a matrix of seqi-seqj pairwise alignment scores to sysout
261 protected void printScoreMatrix(SequenceI[] seqs, float[][] scores,
265 .println("Pairwise alignment scaled similarity score matrix\n");
267 for (int i = 0; i < seqs.length; i++)
269 jalview.bin.Console.outPrintln(
270 String.format("%3d %s", i + 1, seqs[i].getDisplayId(true)));
274 * table heading columns for sequences 1, 2, 3...
276 System.out.print("\n ");
277 for (int i = 0; i < seqs.length; i++)
279 System.out.print(String.format("%7d", i + 1));
281 jalview.bin.Console.outPrintln();
283 for (int i = 0; i < seqs.length; i++)
285 System.out.print(String.format("%3d", i + 1));
286 for (int j = 0; j < i; j++)
289 * as a fraction of tot score, outputs are 0 <= score <= 1
291 System.out.print(String.format("%7.3f", scores[i][j] / totscore));
293 jalview.bin.Console.outPrintln();
296 jalview.bin.Console.outPrintln("\n");
306 protected void viewInEditorButton_actionPerformed(ActionEvent e)
308 SequenceI[] seq = new SequenceI[sequences.size()];
310 for (int i = 0; i < sequences.size(); i++)
312 seq[i] = sequences.elementAt(i);
315 AlignFrame af = new AlignFrame(new Alignment(seq),
316 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
318 Desktop.addInternalFrame(af,
319 MessageManager.getString("label.pairwise_aligned_sequences"),
320 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
323 public long getTotal()
328 public long getProgress()
332 public SequenceI[] getInputSequences()