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