2d86827366a338eddf43e05c92bdf642fee8d4f9
[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   private boolean suppressTextbox;
61
62   private boolean discardAlignments;
63
64   /**
65    * Creates a new PairwiseAlignPanel object.
66    * 
67    * @param viewport
68    *          DOCUMENT ME!
69    * @param endGaps ~ toggle gaps and the beginning and end of sequences
70    */
71   public PairwiseAlignPanel(AlignmentViewport viewport)
72   {
73     this(viewport, false, 120, 20);     // default penalties used in AlignSeq
74   }
75   public PairwiseAlignPanel(AlignmentViewport viewport, boolean endGaps, int gapOpenCost, int gapExtendCost)
76   {
77     super();
78     this.av = viewport;
79
80     StringBuilder sb = new StringBuilder(1024);
81
82     sequences = new Vector<SequenceI>();
83
84     SequenceGroup selectionGroup = viewport.getSelectionGroup();
85     boolean isSelection = selectionGroup != null
86             && selectionGroup.getSize() > 0;
87     AlignmentView view = viewport.getAlignmentView(isSelection);
88     // String[] seqStrings = viewport.getViewAsString(true);
89     String[] seqStrings = view
90             .getSequenceStrings(viewport.getGapCharacter());
91
92     SequenceI[] seqs;
93     if (isSelection)
94     {
95       seqs = (SequenceI[]) view
96               .getAlignmentAndHiddenColumns(viewport.getGapCharacter())[0];
97     }
98     else
99     {
100       seqs = av.getAlignment().getSequencesArray();
101     }
102
103     String type = (viewport.getAlignment().isNucleotide()) ? AlignSeq.DNA
104             : AlignSeq.PEP;
105
106     float[][] scores = new float[seqs.length][seqs.length];
107     float[][] alignmentScores = new float[seqs.length][seqs.length];
108     double totscore = 0D;
109     int count = seqs.length;
110     suppressTextbox = count<10;
111     discardAlignments = count<15;
112     boolean first = true;
113
114     for (int i = 1; i < count; i++)
115     {
116       // fill diagonal alignmentScores with Float.NaN
117       alignmentScores[i - 1][i - 1] = Float.NaN;
118       for (int j = 0; j < i; j++)
119       {
120         AlignSeq as = new AlignSeq(seqs[i], seqStrings[i], seqs[j],
121                 seqStrings[j], type, gapOpenCost, gapExtendCost);
122
123         if (as.s1str.length() == 0 || as.s2str.length() == 0)
124         {
125           continue;
126         }
127
128         as.calcScoreMatrix();
129         if (endGaps)
130         {
131           as.traceAlignmentWithEndGaps();
132         }
133         else
134         {
135           as.traceAlignment();
136         }
137         as.scoreAlignment();
138
139         if (!first)
140         {
141           System.out.println(DASHES);
142           textarea.append(DASHES);
143           sb.append(DASHES);
144         }
145         first = false;
146         if (discardAlignments) {
147           as.printAlignment(System.out);
148         }
149         scores[i][j] = as.getMaxScore() / as.getASeq1().length;
150         alignmentScores[i][j] = as.getAlignmentScore();
151         totscore = totscore + scores[i][j];
152         if (suppressTextbox)
153         {
154           textarea.append(as.getOutput());
155           sb.append(as.getOutput());
156         }
157         if (discardAlignments)
158         {
159           sequences.add(as.getAlignedSeq1());
160           sequences.add(as.getAlignedSeq2());
161         }
162       }
163     }
164     alignmentScores[count - 1][count - 1] = Float.NaN;
165
166     this.scores = scores;
167     this.alignmentScores = alignmentScores;
168
169     if (count > 2)
170     {
171       printScoreMatrix(seqs, scores, totscore);
172     }
173
174     alignmentOutput = sb.toString();
175   }
176
177   public float[][] getScores()
178   {
179     return this.scores;
180   }
181
182   public float[][] getAlignmentScores()
183   {
184     return this.alignmentScores;
185   }
186
187   public String getAlignmentOutput()
188   {
189     return this.alignmentOutput;
190   }
191
192   /**
193    * Prints a matrix of seqi-seqj pairwise alignment scores to sysout
194    * 
195    * @param seqs
196    * @param scores
197    * @param totscore
198    */
199   protected void printScoreMatrix(SequenceI[] seqs, float[][] scores,
200           double totscore)
201   {
202     System.out
203             .println("Pairwise alignment scaled similarity score matrix\n");
204
205     for (int i = 0; i < seqs.length; i++)
206     {
207       System.out.println(
208               String.format("%3d %s", i + 1, seqs[i].getDisplayId(true)));
209     }
210
211     /*
212      * table heading columns for sequences 1, 2, 3...
213      */
214     System.out.print("\n ");
215     for (int i = 0; i < seqs.length; i++)
216     {
217       System.out.print(String.format("%7d", i + 1));
218     }
219     System.out.println();
220
221     for (int i = 0; i < seqs.length; i++)
222     {
223       System.out.print(String.format("%3d", i + 1));
224       for (int j = 0; j < i; j++)
225       {
226         /*
227          * as a fraction of tot score, outputs are 0 <= score <= 1
228          */
229         System.out.print(String.format("%7.3f", scores[i][j] / totscore));
230       }
231       System.out.println();
232     }
233
234     System.out.println("\n");
235   }
236
237   /**
238    * DOCUMENT ME!
239    * 
240    * @param e
241    *          DOCUMENT ME!
242    */
243   @Override
244   protected void viewInEditorButton_actionPerformed(ActionEvent e)
245   {
246     SequenceI[] seq = new SequenceI[sequences.size()];
247
248     for (int i = 0; i < sequences.size(); i++)
249     {
250       seq[i] = sequences.elementAt(i);
251     }
252
253     AlignFrame af = new AlignFrame(new Alignment(seq),
254             AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
255
256     Desktop.addInternalFrame(af,
257             MessageManager.getString("label.pairwise_aligned_sequences"),
258             AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
259   }
260 }