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;
58 private String alignmentOutput;
61 * Creates a new PairwiseAlignPanel object.
65 * @param endGaps ~ toggle gaps and the beginning and end of sequences
67 public PairwiseAlignPanel(AlignmentViewport viewport)
69 this(viewport, false, 120, 20); // default penalties used in AlignSeq
71 public PairwiseAlignPanel(AlignmentViewport viewport, boolean endGaps, int gapOpenCost, int gapExtendCost)
76 StringBuilder sb = new StringBuilder(1024);
78 sequences = new Vector<SequenceI>();
80 SequenceGroup selectionGroup = viewport.getSelectionGroup();
81 boolean isSelection = selectionGroup != null
82 && selectionGroup.getSize() > 0;
83 AlignmentView view = viewport.getAlignmentView(isSelection);
84 // String[] seqStrings = viewport.getViewAsString(true);
85 String[] seqStrings = view
86 .getSequenceStrings(viewport.getGapCharacter());
91 seqs = (SequenceI[]) view
92 .getAlignmentAndHiddenColumns(viewport.getGapCharacter())[0];
96 seqs = av.getAlignment().getSequencesArray();
99 String type = (viewport.getAlignment().isNucleotide()) ? AlignSeq.DNA
102 float[][] scores = new float[seqs.length][seqs.length];
103 float[][] alignmentScores = new float[seqs.length][seqs.length];
104 double totscore = 0D;
105 int count = seqs.length;
106 boolean first = true;
107 //AlignSeq as = new AlignSeq(seqs[1], seqStrings[1], seqs[0], seqStrings[0], type, gapOpenCost, gapExtendCost);
109 for (int i = 1; i < count; i++)
111 // fill diagonal alignmentScores with Float.NaN
112 alignmentScores[i-1][i-1] = Float.NaN;
113 for (int j = 0; j < i; j++)
115 AlignSeq as = new AlignSeq(seqs[i], seqStrings[i], seqs[j],
116 seqStrings[j], type, gapOpenCost, gapExtendCost);
117 // as.seqInit(seqs[i], seqStrings[i], seqs[j], seqStrings[j], type);
119 if (as.s1str.length() == 0 || as.s2str.length() == 0)
124 as.calcScoreMatrix();
127 as.traceAlignmentWithEndGaps();
135 System.out.println(DASHES);
136 textarea.append(DASHES);
140 as.printAlignment(System.out);
141 scores[i][j] = as.getMaxScore() / as.getASeq1().length;
142 System.out.println(String.format("%s x %s -> [%d][%d] (%f)", seqs[i].getName(), seqs[j].getName(), i, j, as.getAlignmentScore()));
143 alignmentScores[i][j] = as.getAlignmentScore();
144 totscore = totscore + scores[i][j];
146 textarea.append(as.getOutput());
147 sb.append(as.getOutput());
148 sequences.add(as.getAlignedSeq1());
149 sequences.add(as.getAlignedSeq2());
152 alignmentScores[count-1][count-1] = Float.NaN;
154 this.scores = scores;
155 this.alignmentScores = alignmentScores;
159 printScoreMatrix(seqs, scores, totscore);
162 alignmentOutput = sb.toString();
165 public float[][] getScores()
170 public float[][] getAlignmentScores()
172 return this.alignmentScores;
175 public String getAlignmentOutput()
177 return this.alignmentOutput;
181 * Prints a matrix of seqi-seqj pairwise alignment scores to sysout
187 protected void printScoreMatrix(SequenceI[] seqs, float[][] scores,
191 .println("Pairwise alignment scaled similarity score matrix\n");
193 for (int i = 0; i < seqs.length; i++)
196 String.format("%3d %s", i + 1, seqs[i].getDisplayId(true)));
200 * table heading columns for sequences 1, 2, 3...
202 System.out.print("\n ");
203 for (int i = 0; i < seqs.length; i++)
205 System.out.print(String.format("%7d", i + 1));
207 System.out.println();
209 for (int i = 0; i < seqs.length; i++)
211 System.out.print(String.format("%3d", i + 1));
212 for (int j = 0; j < i; j++)
215 * as a fraction of tot score, outputs are 0 <= score <= 1
217 System.out.print(String.format("%7.3f", scores[i][j] / totscore));
219 System.out.println();
222 System.out.println("\n");
232 protected void viewInEditorButton_actionPerformed(ActionEvent e)
234 SequenceI[] seq = new SequenceI[sequences.size()];
236 for (int i = 0; i < sequences.size(); i++)
238 seq[i] = sequences.elementAt(i);
241 AlignFrame af = new AlignFrame(new Alignment(seq),
242 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
244 Desktop.addInternalFrame(af,
245 MessageManager.getString("label.pairwise_aligned_sequences"),
246 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);