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