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