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