Cleanup, note: rectangular selection works by holding mouse wheel and s (see line...
[jalview.git] / src / jalview / gui / PairwiseAlignPanel.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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.
11  *  
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.
16  * 
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.
20  */
21 package jalview.gui;
22
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
32 import java.awt.event.ActionEvent;
33 import java.util.Vector;
34
35 /**
36  * DOCUMENT ME!
37  * 
38  * @author $author$
39  * @version $Revision$
40  */
41 public class PairwiseAlignPanel extends GPairwiseAlignPanel
42 {
43
44   private static final String DASHES = "---------------------\n";
45
46   private float[][] scores;
47
48   private float[][] alignmentScores;    // scores used by PaSiMap
49
50   private int GAP_OPEN_COST;
51
52   private int GAP_EXTEND_COST;
53
54   AlignmentViewport av;
55
56   Vector<SequenceI> sequences;
57
58   private String alignmentOutput;
59
60   /**
61    * Creates a new PairwiseAlignPanel object.
62    * 
63    * @param viewport
64    *          DOCUMENT ME!
65    * @param endGaps ~ toggle gaps and the beginning and end of sequences
66    */
67   public PairwiseAlignPanel(AlignmentViewport viewport)
68   {
69     this(viewport, false, 120, 20);     // default penalties used in AlignSeq
70   }
71   public PairwiseAlignPanel(AlignmentViewport viewport, boolean endGaps, int gapOpenCost, int gapExtendCost)
72   {
73     super();
74     this.av = viewport;
75
76     StringBuilder sb = new StringBuilder(1024);
77
78     sequences = new Vector<SequenceI>();
79
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());
87
88     SequenceI[] seqs;
89     if (isSelection)
90     {
91       seqs = (SequenceI[]) view
92               .getAlignmentAndHiddenColumns(viewport.getGapCharacter())[0];
93     }
94     else
95     {
96       seqs = av.getAlignment().getSequencesArray();
97     }
98
99     String type = (viewport.getAlignment().isNucleotide()) ? AlignSeq.DNA
100             : AlignSeq.PEP;
101
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
108     for (int i = 1; i < count; i++)
109     {
110       // fill diagonal alignmentScores with Float.NaN
111       alignmentScores[i-1][i-1] = Float.NaN;
112       for (int j = 0; j < i; j++)
113       {
114         AlignSeq as = new AlignSeq(seqs[i], seqStrings[i], seqs[j],
115                 seqStrings[j], type, gapOpenCost, gapExtendCost);
116
117         if (as.s1str.length() == 0 || as.s2str.length() == 0)
118         {
119           continue;
120         }
121
122         as.calcScoreMatrix();
123         if (endGaps)
124         {
125           as.traceAlignmentWithEndGaps();
126         } else {
127           as.traceAlignment();
128         }
129         as.scoreAlignment();
130
131         if (!first)
132         {
133           System.out.println(DASHES);
134           textarea.append(DASHES);
135           sb.append(DASHES);
136         }
137         first = false;
138         as.printAlignment(System.out);
139         scores[i][j] = as.getMaxScore() / as.getASeq1().length;
140         alignmentScores[i][j] = as.getAlignmentScore();
141         totscore = totscore + scores[i][j];
142
143         textarea.append(as.getOutput());
144         sb.append(as.getOutput());
145         sequences.add(as.getAlignedSeq1());
146         sequences.add(as.getAlignedSeq2());
147       }
148     }
149     alignmentScores[count-1][count-1] = Float.NaN;
150
151     this.scores = scores;
152     this.alignmentScores = alignmentScores;
153
154     if (count > 2)
155     {
156       printScoreMatrix(seqs, scores, totscore);
157     }
158
159     alignmentOutput = sb.toString();
160   }
161
162   public float[][] getScores()
163   {
164     return this.scores;
165   }
166
167   public float[][] getAlignmentScores()
168   {
169     return this.alignmentScores;
170   }
171
172   public String getAlignmentOutput()
173   {
174     return this.alignmentOutput;
175   }
176
177   /**
178    * Prints a matrix of seqi-seqj pairwise alignment scores to sysout
179    * 
180    * @param seqs
181    * @param scores
182    * @param totscore
183    */
184   protected void printScoreMatrix(SequenceI[] seqs, float[][] scores,
185           double totscore)
186   {
187     System.out
188             .println("Pairwise alignment scaled similarity score matrix\n");
189
190     for (int i = 0; i < seqs.length; i++)
191     {
192       System.out.println(
193               String.format("%3d %s", i + 1, seqs[i].getDisplayId(true)));
194     }
195
196     /*
197      * table heading columns for sequences 1, 2, 3...
198      */
199     System.out.print("\n ");
200     for (int i = 0; i < seqs.length; i++)
201     {
202       System.out.print(String.format("%7d", i + 1));
203     }
204     System.out.println();
205
206     for (int i = 0; i < seqs.length; i++)
207     {
208       System.out.print(String.format("%3d", i + 1));
209       for (int j = 0; j < i; j++)
210       {
211         /*
212          * as a fraction of tot score, outputs are 0 <= score <= 1
213          */
214         System.out.print(String.format("%7.3f", scores[i][j] / totscore));
215       }
216       System.out.println();
217     }
218
219     System.out.println("\n");
220   }
221
222   /**
223    * DOCUMENT ME!
224    * 
225    * @param e
226    *          DOCUMENT ME!
227    */
228   @Override
229   protected void viewInEditorButton_actionPerformed(ActionEvent e)
230   {
231     SequenceI[] seq = new SequenceI[sequences.size()];
232
233     for (int i = 0; i < sequences.size(); i++)
234     {
235       seq[i] = sequences.elementAt(i);
236     }
237
238     AlignFrame af = new AlignFrame(new Alignment(seq),
239             AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
240
241     Desktop.addInternalFrame(af,
242             MessageManager.getString("label.pairwise_aligned_sequences"),
243             AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
244   }
245 }