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