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.datamodel.Alignment;
25 import jalview.datamodel.AlignmentView;
26 import jalview.datamodel.SequenceGroup;
27 import jalview.datamodel.SequenceI;
28 import jalview.jbgui.GPairwiseAlignPanel;
29 import jalview.util.MessageManager;
30 import jalview.viewmodel.AlignmentViewport;
31 import jalview.math.MiscMath;
33 import java.beans.PropertyChangeListener;
34 import java.awt.event.ActionEvent;
35 import java.util.Vector;
36 import javax.swing.event.SwingPropertyChangeSupport;
44 public class PairwiseAlignPanel extends GPairwiseAlignPanel
47 private static final String DASHES = "---------------------\n";
49 private float[][] scores;
51 private float[][] alignmentScores; // scores used by PaSiMap
53 private int GAP_OPEN_COST;
55 private int GAP_EXTEND_COST;
59 Vector<SequenceI> sequences;
61 private String alignmentOutput;
63 private boolean suppressTextbox;
65 private boolean discardAlignments;
67 private boolean endGaps;
70 public static final String TOTAL = "total";
72 public static final String PROGRESS = "progress";
78 private SequenceGroup selection;
82 private SequenceI[] seqs=null;
85 * Creates a new PairwiseAlignPanel object.
89 * @param endGaps ~ toggle gaps and the beginning and end of sequences
91 public PairwiseAlignPanel(AlignmentViewport viewport)
93 this(viewport, null, false, 120, 20, true); // default penalties used in AlignSeq
95 public PairwiseAlignPanel(AlignmentViewport viewport, boolean endGaps, int gapOpenCost, int gapExtendCost)
97 this(viewport, null, endGaps, gapOpenCost, gapExtendCost, true);
100 public PairwiseAlignPanel(AlignmentViewport viewport, SequenceGroup selection, boolean endGaps,
101 int gapOpenCost, int gapExtendCost, boolean run)
105 this.GAP_OPEN_COST = gapOpenCost;
106 this.GAP_EXTEND_COST = gapExtendCost;
107 this.endGaps = endGaps;
108 this.selection = selection;
109 this.total = MiscMath.combinations(av.getAlignment().getHeight(), 2);
113 System.out.println("Creating pap");
116 public void calculate()
119 StringBuilder sb = new StringBuilder(1024);
121 sequences = new Vector<SequenceI>();
125 if (selection != null)
127 // given a set of sequences to compare
128 seqs = selection.getSelectionAsNewSequences(av.getAlignment());
129 seqStrings = new String[seqs.length];
131 for (SequenceI seq : seqs)
133 seqStrings[s++] = seq.getSequenceAsString();
138 SequenceGroup selectionGroup = av.getSelectionGroup();
139 boolean isSelection = selectionGroup != null
140 && selectionGroup.getSize() > 0;
141 AlignmentView view = av.getAlignmentView(isSelection);
142 seqStrings = view.getSequenceStrings(av.getGapCharacter());
145 seqs = (SequenceI[]) view
146 .getAlignmentAndHiddenColumns(av.getGapCharacter())[0];
150 seqs = av.getAlignment().getSequencesArray();
154 String type = (av.getAlignment().isNucleotide()) ? AlignSeq.DNA
157 float[][] scores = new float[seqs.length][seqs.length];
158 float[][] alignmentScores = new float[seqs.length][seqs.length];
159 double totscore = 0D;
160 int count = seqs.length;
161 suppressTextbox = count<10;
162 discardAlignments = count<15;
163 boolean first = true;
166 firePropertyChange(TOTAL, 0, total);
168 suppressTextbox = count<10;
169 discardAlignments = count<15;
171 for (int i = 1; i < count; i++)
173 // fill diagonal alignmentScores with Float.NaN
174 alignmentScores[i - 1][i - 1] = Float.NaN;
175 for (int j = 0; j < i; j++)
177 AlignSeq as = new AlignSeq(seqs[i], seqStrings[i], seqs[j],
178 seqStrings[j], type, GAP_OPEN_COST, GAP_EXTEND_COST);
180 if (as.s1str.length() == 0 || as.s2str.length() == 0)
185 as.calcScoreMatrix();
188 as.traceAlignmentWithEndGaps();
198 jalview.bin.Console.outPrintln(DASHES);
199 textarea.append(DASHES);
203 if (discardAlignments) {
204 as.printAlignment(System.out);
206 scores[i][j] = as.getMaxScore() / as.getASeq1().length;
207 alignmentScores[i][j] = as.getAlignmentScore();
208 totscore = totscore + scores[i][j];
211 textarea.append(as.getOutput());
212 sb.append(as.getOutput());
214 if (discardAlignments)
216 sequences.add(as.getAlignedSeq1());
217 sequences.add(as.getAlignedSeq2());
219 firePropertyChange(PROGRESS, progress, ++progress);
222 alignmentScores[count - 1][count - 1] = Float.NaN;
224 this.scores = scores;
225 this.alignmentScores = alignmentScores;
229 printScoreMatrix(seqs, scores, totscore);
232 alignmentOutput = sb.toString();
235 public float[][] getScores()
240 public float[][] getAlignmentScores()
242 return this.alignmentScores;
245 public String getAlignmentOutput()
247 return this.alignmentOutput;
251 * Prints a matrix of seqi-seqj pairwise alignment scores to sysout
257 protected void printScoreMatrix(SequenceI[] seqs, float[][] scores,
261 .println("Pairwise alignment scaled similarity score matrix\n");
263 for (int i = 0; i < seqs.length; i++)
265 jalview.bin.Console.outPrintln(
266 String.format("%3d %s", i + 1, seqs[i].getDisplayId(true)));
270 * table heading columns for sequences 1, 2, 3...
272 System.out.print("\n ");
273 for (int i = 0; i < seqs.length; i++)
275 System.out.print(String.format("%7d", i + 1));
277 jalview.bin.Console.outPrintln();
279 for (int i = 0; i < seqs.length; i++)
281 System.out.print(String.format("%3d", i + 1));
282 for (int j = 0; j < i; j++)
285 * as a fraction of tot score, outputs are 0 <= score <= 1
287 System.out.print(String.format("%7.3f", scores[i][j] / totscore));
289 jalview.bin.Console.outPrintln();
292 jalview.bin.Console.outPrintln("\n");
302 protected void viewInEditorButton_actionPerformed(ActionEvent e)
304 SequenceI[] seq = new SequenceI[sequences.size()];
306 for (int i = 0; i < sequences.size(); i++)
308 seq[i] = sequences.elementAt(i);
311 AlignFrame af = new AlignFrame(new Alignment(seq),
312 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
314 Desktop.addInternalFrame(af,
315 MessageManager.getString("label.pairwise_aligned_sequences"),
316 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
319 public long getTotal()
324 public long getProgress()
328 public SequenceI[] getInputSequences()