JAL-4366 compute coverage on each side of the pairwise alignment
[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.analysis.scoremodels.ScoreMatrix;
25 import jalview.datamodel.Alignment;
26 import jalview.datamodel.AlignmentView;
27 import jalview.datamodel.SequenceGroup;
28 import jalview.datamodel.SequenceI;
29 import jalview.jbgui.GPairwiseAlignPanel;
30 import jalview.util.MessageManager;
31 import jalview.viewmodel.AlignmentViewport;
32 import jalview.math.MiscMath;
33
34 import java.beans.PropertyChangeListener;
35 import java.awt.event.ActionEvent;
36 import java.util.Vector;
37 import javax.swing.event.SwingPropertyChangeSupport;
38
39 /**
40  * DOCUMENT ME!
41  * 
42  * @author $author$
43  * @version $Revision$
44  */
45 public class PairwiseAlignPanel extends GPairwiseAlignPanel
46 {
47
48   private static final String DASHES = "---------------------\n";
49
50   private float[][] scores;
51
52   private float[][] alignmentScores;    // scores used by PaSiMap
53
54   private int GAP_OPEN_COST;
55
56   private int GAP_EXTEND_COST;
57
58   AlignmentViewport av;
59
60   Vector<SequenceI> sequences;
61
62   private String alignmentOutput;
63
64   private boolean suppressTextbox;
65  
66   private boolean discardAlignments;
67
68   private boolean endGaps;
69
70   // for listening
71   public static final String TOTAL = "total";
72
73   public static final String PROGRESS = "progress";
74
75   private int total;
76
77   private int progress;
78
79   private SequenceGroup selection;
80   /**
81    * input sequences
82    */
83   private SequenceI[] seqs=null;
84
85   /**
86    * Creates a new PairwiseAlignPanel object.
87    * 
88    * @param viewport
89    *          DOCUMENT ME!
90    * @param endGaps ~ toggle gaps and the beginning and end of sequences
91    */
92   public PairwiseAlignPanel(AlignmentViewport viewport)
93   {
94     this(viewport, null, false, 120, 20, true); // default penalties used in AlignSeq
95   }
96   public PairwiseAlignPanel(AlignmentViewport viewport, boolean endGaps, int gapOpenCost, int gapExtendCost)
97   {
98     this(viewport, null, endGaps, gapOpenCost, gapExtendCost, true);
99   }
100
101   public PairwiseAlignPanel(AlignmentViewport viewport, SequenceGroup selection, boolean  endGaps,
102           int gapOpenCost, int gapExtendCost, boolean run)
103   {
104     super();
105     this.av = viewport;
106     this.GAP_OPEN_COST = gapOpenCost;
107     this.GAP_EXTEND_COST = gapExtendCost;
108     this.endGaps = endGaps;
109     this.selection = selection;
110     this.total = MiscMath.combinations(av.getAlignment().getHeight(), 2);
111
112     if (run)
113       calculate();
114     System.out.println("Creating pap");
115   }
116   
117   public void calculate()
118   {
119     calculate(null);
120   }
121   public void calculate (ScoreMatrix sm)
122   {
123
124     StringBuilder sb = new StringBuilder(1024);
125
126     sequences = new Vector<SequenceI>();
127     String[] seqStrings;
128     seqs=null;
129
130     if (selection != null)
131     {
132       // given a set of sequences to compare
133       seqs = selection.getSelectionAsNewSequences(av.getAlignment());
134       seqStrings = new String[seqs.length];
135       int s = 0;
136       for (SequenceI seq : seqs)
137       {
138         seqStrings[s++] = seq.getSequenceAsString();
139       }
140     }
141     else
142     {
143       SequenceGroup selectionGroup = av.getSelectionGroup();
144       boolean isSelection = selectionGroup != null
145               && selectionGroup.getSize() > 0;
146       AlignmentView view = av.getAlignmentView(isSelection);
147       seqStrings = view.getSequenceStrings(av.getGapCharacter());
148       if (isSelection)
149       {
150         seqs = (SequenceI[]) view
151                 .getAlignmentAndHiddenColumns(av.getGapCharacter())[0];
152       }
153       else
154       {
155         seqs = av.getAlignment().getSequencesArray();
156       }
157     }
158     
159     String type = (av.getAlignment().isNucleotide()) ? AlignSeq.DNA
160             : AlignSeq.PEP;
161
162     float[][] scores = new float[seqs.length][seqs.length];
163     float[][] alignmentScores = new float[seqs.length][seqs.length];
164     double totscore = 0D;
165     int count = seqs.length;
166     suppressTextbox = count>10;
167     discardAlignments = count>15;
168     boolean first = true;
169
170     progress = 0;
171     firePropertyChange(TOTAL, 0, total);
172
173     for (int i = 1; i < count; i++)
174     {
175       // fill diagonal alignmentScores with Float.NaN
176       alignmentScores[i - 1][i - 1] = Float.NaN;
177       for (int j = 0; j < i; j++)
178       {
179         AlignSeq as = new AlignSeq(seqs[i], seqStrings[i], seqs[j],
180                 seqStrings[j], type, GAP_OPEN_COST, GAP_EXTEND_COST);
181         if (sm != null)
182           as.setScoreMatrix(sm);
183
184         if (as.s1str.length() == 0 || as.s2str.length() == 0)
185         {
186           continue;
187         }
188
189         as.calcScoreMatrix();
190         if (endGaps)
191         {
192           as.traceAlignmentWithEndGaps();
193         }
194         else
195         {
196           as.traceAlignment();
197         }
198         as.scoreAlignment();
199
200         if (!first && !suppressTextbox)
201         {
202           jalview.bin.Console.outPrintln(DASHES);
203           textarea.append(DASHES);
204           sb.append(DASHES);
205         }
206         first = false;
207         if (!discardAlignments) {
208           as.printAlignment(System.out);
209         }
210         scores[i][j] = as.getMaxScore() / as.getASeq1().length;
211         alignmentScores[i][j] = as.getAlignmentScore();
212         totscore = totscore + scores[i][j];
213         if (!suppressTextbox)
214         {
215           textarea.append(as.getOutput());
216           sb.append(as.getOutput());
217         }
218         if (!discardAlignments)
219         {
220           sequences.add(as.getAlignedSeq1());
221           sequences.add(as.getAlignedSeq2());
222         }
223         firePropertyChange(PROGRESS, progress, ++progress);
224       }
225     }
226     alignmentScores[count - 1][count - 1] = Float.NaN;
227
228     this.scores = scores;
229     this.alignmentScores = alignmentScores;
230
231     if (count > 2)
232     {
233       printScoreMatrix(seqs, scores, totscore);
234     }
235
236     alignmentOutput = sb.toString();
237   }
238
239   public float[][] getScores()
240   {
241     return this.scores;
242   }
243
244   public float[][] getAlignmentScores()
245   {
246     return this.alignmentScores;
247   }
248
249   public String getAlignmentOutput()
250   {
251     return this.alignmentOutput;
252   }
253
254   /**
255    * Prints a matrix of seqi-seqj pairwise alignment scores to sysout
256    * 
257    * @param seqs
258    * @param scores
259    * @param totscore
260    */
261   protected void printScoreMatrix(SequenceI[] seqs, float[][] scores,
262           double totscore)
263   {
264     System.out
265             .println("Pairwise alignment scaled similarity score matrix\n");
266
267     for (int i = 0; i < seqs.length; i++)
268     {
269       jalview.bin.Console.outPrintln(
270               String.format("%3d %s", i + 1, seqs[i].getDisplayId(true)));
271     }
272
273     /*
274      * table heading columns for sequences 1, 2, 3...
275      */
276     System.out.print("\n ");
277     for (int i = 0; i < seqs.length; i++)
278     {
279       System.out.print(String.format("%7d", i + 1));
280     }
281     jalview.bin.Console.outPrintln();
282
283     for (int i = 0; i < seqs.length; i++)
284     {
285       System.out.print(String.format("%3d", i + 1));
286       for (int j = 0; j < i; j++)
287       {
288         /*
289          * as a fraction of tot score, outputs are 0 <= score <= 1
290          */
291         System.out.print(String.format("%7.3f", scores[i][j] / totscore));
292       }
293       jalview.bin.Console.outPrintln();
294     }
295
296     jalview.bin.Console.outPrintln("\n");
297   }
298
299   /**
300    * DOCUMENT ME!
301    * 
302    * @param e
303    *          DOCUMENT ME!
304    */
305   @Override
306   protected void viewInEditorButton_actionPerformed(ActionEvent e)
307   {
308     SequenceI[] seq = new SequenceI[sequences.size()];
309
310     for (int i = 0; i < sequences.size(); i++)
311     {
312       seq[i] = sequences.elementAt(i);
313     }
314
315     AlignFrame af = new AlignFrame(new Alignment(seq),
316             AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
317
318     Desktop.addInternalFrame(af,
319             MessageManager.getString("label.pairwise_aligned_sequences"),
320             AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
321   }
322
323   public long getTotal()
324   {
325     return total;
326   }
327
328   public long getProgress()
329   {
330     return progress;
331   }
332   public SequenceI[] getInputSequences()
333   {
334     return seqs;
335   }
336 }