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;
32 import java.awt.event.ActionEvent;
33 import java.util.Vector;
41 public class PairwiseAlignPanel extends GPairwiseAlignPanel
44 private static final String DASHES = "---------------------\n";
46 private float[][] scores;
48 private float[][] alignmentScores; // scores used by PaSiMap
50 private int GAP_OPEN_COST;
52 private int GAP_EXTEND_COST;
56 Vector<SequenceI> sequences;
59 * Creates a new PairwiseAlignPanel object.
63 * @param endGaps ~ toggle gaps and the beginning and end of sequences
65 public PairwiseAlignPanel(AlignmentViewport viewport)
67 this(viewport, false, 120, 20); // default penalties used in AlignSeq
69 public PairwiseAlignPanel(AlignmentViewport viewport, boolean endGaps, int gapOpenCost, int gapExtendCost)
74 sequences = new Vector<SequenceI>();
76 SequenceGroup selectionGroup = viewport.getSelectionGroup();
77 boolean isSelection = selectionGroup != null
78 && selectionGroup.getSize() > 0;
79 AlignmentView view = viewport.getAlignmentView(isSelection);
80 // String[] seqStrings = viewport.getViewAsString(true);
81 String[] seqStrings = view
82 .getSequenceStrings(viewport.getGapCharacter());
87 seqs = (SequenceI[]) view
88 .getAlignmentAndHiddenColumns(viewport.getGapCharacter())[0];
92 seqs = av.getAlignment().getSequencesArray();
95 String type = (viewport.getAlignment().isNucleotide()) ? AlignSeq.DNA
98 float[][] scores = new float[seqs.length][seqs.length];
99 float[][] alignmentScores = new float[seqs.length][seqs.length];
100 double totscore = 0D;
101 int count = seqs.length;
102 boolean first = true;
104 for (int i = 1; i < count; i++)
106 // fill diagonal alignmentScores with Float.NaN
107 alignmentScores[i-1][i-1] = Float.NaN;
108 for (int j = 0; j < i; j++)
110 AlignSeq as = new AlignSeq(seqs[i], seqStrings[i], seqs[j],
111 seqStrings[j], type, gapOpenCost, gapExtendCost);
113 if (as.s1str.length() == 0 || as.s2str.length() == 0)
118 as.calcScoreMatrix();
121 as.traceAlignmentWithEndGaps();
129 System.out.println(DASHES);
130 textarea.append(DASHES);
133 as.printAlignment(System.out);
134 scores[i][j] = as.getMaxScore() / as.getASeq1().length;
135 alignmentScores[i][j] = as.getAlignmentScore();
136 totscore = totscore + scores[i][j];
138 textarea.append(as.getOutput());
139 sequences.add(as.getAlignedSeq1());
140 sequences.add(as.getAlignedSeq2());
143 alignmentScores[count-1][count-1] = Float.NaN;
145 this.scores = scores;
146 this.alignmentScores = alignmentScores;
150 printScoreMatrix(seqs, scores, totscore);
154 public float[][] getScores()
159 public float[][] getAlignmentScores()
161 return this.alignmentScores;
165 * Prints a matrix of seqi-seqj pairwise alignment scores to sysout
171 protected void printScoreMatrix(SequenceI[] seqs, float[][] scores,
175 .println("Pairwise alignment scaled similarity score matrix\n");
177 for (int i = 0; i < seqs.length; i++)
180 String.format("%3d %s", i + 1, seqs[i].getDisplayId(true)));
184 * table heading columns for sequences 1, 2, 3...
186 System.out.print("\n ");
187 for (int i = 0; i < seqs.length; i++)
189 System.out.print(String.format("%7d", i + 1));
191 System.out.println();
193 for (int i = 0; i < seqs.length; i++)
195 System.out.print(String.format("%3d", i + 1));
196 for (int j = 0; j < i; j++)
199 * as a fraction of tot score, outputs are 0 <= score <= 1
201 System.out.print(String.format("%7.3f", scores[i][j] / totscore));
203 System.out.println();
206 System.out.println("\n");
216 protected void viewInEditorButton_actionPerformed(ActionEvent e)
218 SequenceI[] seq = new SequenceI[sequences.size()];
220 for (int i = 0; i < sequences.size(); i++)
222 seq[i] = sequences.elementAt(i);
225 AlignFrame af = new AlignFrame(new Alignment(seq),
226 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
228 Desktop.addInternalFrame(af,
229 MessageManager.getString("label.pairwise_aligned_sequences"),
230 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);