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.awt.event.ActionEvent;
34 import java.util.Vector;
42 public class PairwiseAlignPanel extends GPairwiseAlignPanel
45 private static final String DASHES = "---------------------\n";
47 private float[][] scores;
49 private float[][] alignmentScores; // scores used by PaSiMap
51 private int GAP_OPEN_COST;
53 private int GAP_EXTEND_COST;
56 private long total = 0l;
57 private long ccount = 0l;
61 Vector<SequenceI> sequences;
63 private String alignmentOutput;
66 * Creates a new PairwiseAlignPanel object.
70 * @param endGaps ~ toggle gaps and the beginning and end of sequences
72 public PairwiseAlignPanel(AlignmentViewport viewport)
74 this(viewport, false, 120, 20); // default penalties used in AlignSeq
76 public PairwiseAlignPanel(AlignmentViewport viewport, boolean endGaps, int gapOpenCost, int gapExtendCost)
81 StringBuilder sb = new StringBuilder(1024);
83 sequences = new Vector<SequenceI>();
85 SequenceGroup selectionGroup = viewport.getSelectionGroup();
86 boolean isSelection = selectionGroup != null
87 && selectionGroup.getSize() > 0;
88 AlignmentView view = viewport.getAlignmentView(isSelection);
89 // String[] seqStrings = viewport.getViewAsString(true);
90 String[] seqStrings = view
91 .getSequenceStrings(viewport.getGapCharacter());
96 seqs = (SequenceI[]) view
97 .getAlignmentAndHiddenColumns(viewport.getGapCharacter())[0];
101 seqs = av.getAlignment().getSequencesArray();
104 String type = (viewport.getAlignment().isNucleotide()) ? AlignSeq.DNA
107 float[][] scores = new float[seqs.length][seqs.length];
108 float[][] alignmentScores = new float[seqs.length][seqs.length];
109 double totscore = 0D;
110 int count = seqs.length;
111 boolean first = true;
112 //AlignSeq as = new AlignSeq(seqs[1], seqStrings[1], seqs[0], seqStrings[0], type, gapOpenCost, gapExtendCost);
114 this.total = (long) MiscMath.permutations(count,2) / 2;
116 for (int i = 1; i < count; i++)
118 // fill diagonal alignmentScores with Float.NaN
119 alignmentScores[i-1][i-1] = Float.NaN;
120 for (int j = 0; j < i; j++)
122 AlignSeq as = new AlignSeq(seqs[i], seqStrings[i], seqs[j],
123 seqStrings[j], type, gapOpenCost, gapExtendCost);
124 // as.seqInit(seqs[i], seqStrings[i], seqs[j], seqStrings[j], type);
126 System.out.println(String.format("%d / %d", ccount, total));
128 if (as.s1str.length() == 0 || as.s2str.length() == 0)
133 as.calcScoreMatrix();
136 as.traceAlignmentWithEndGaps();
144 //System.out.println(DASHES);
145 textarea.append(DASHES);
149 //as.printAlignment(System.out);
150 scores[i][j] = as.getMaxScore() / as.getASeq1().length;
151 //System.out.println(String.format("%s x %s -> [%d][%d] (%f)", seqs[i].getName(), seqs[j].getName(), i, j, as.getAlignmentScore()));
152 alignmentScores[i][j] = as.getAlignmentScore();
153 totscore = totscore + scores[i][j];
155 textarea.append(as.getOutput());
156 sb.append(as.getOutput());
157 sequences.add(as.getAlignedSeq1());
158 sequences.add(as.getAlignedSeq2());
161 alignmentScores[count-1][count-1] = Float.NaN;
163 this.scores = scores;
164 this.alignmentScores = alignmentScores;
168 printScoreMatrix(seqs, scores, totscore);
171 alignmentOutput = sb.toString();
174 public float[][] getScores()
179 public float[][] getAlignmentScores()
181 return this.alignmentScores;
184 public String getAlignmentOutput()
186 return this.alignmentOutput;
190 * Prints a matrix of seqi-seqj pairwise alignment scores to sysout
196 protected void printScoreMatrix(SequenceI[] seqs, float[][] scores,
200 .println("Pairwise alignment scaled similarity score matrix\n");
202 for (int i = 0; i < seqs.length; i++)
205 String.format("%3d %s", i + 1, seqs[i].getDisplayId(true)));
209 * table heading columns for sequences 1, 2, 3...
211 System.out.print("\n ");
212 for (int i = 0; i < seqs.length; i++)
214 System.out.print(String.format("%7d", i + 1));
216 System.out.println();
218 for (int i = 0; i < seqs.length; i++)
220 System.out.print(String.format("%3d", i + 1));
221 for (int j = 0; j < i; j++)
224 * as a fraction of tot score, outputs are 0 <= score <= 1
226 System.out.print(String.format("%7.3f", scores[i][j] / totscore));
228 System.out.println();
231 System.out.println("\n");
241 protected void viewInEditorButton_actionPerformed(ActionEvent e)
243 SequenceI[] seq = new SequenceI[sequences.size()];
245 for (int i = 0; i < sequences.size(); i++)
247 seq[i] = sequences.elementAt(i);
250 AlignFrame af = new AlignFrame(new Alignment(seq),
251 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
253 Desktop.addInternalFrame(af,
254 MessageManager.getString("label.pairwise_aligned_sequences"),
255 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);