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