JAL-1517 source formatting
[jalview.git] / src / jalview / appletgui / PairwiseAlignPanel.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3  * Copyright (C) 2014 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.appletgui;
22
23 import java.util.*;
24
25 import java.awt.*;
26 import java.awt.event.*;
27
28 import jalview.analysis.*;
29 import jalview.datamodel.*;
30 import jalview.util.MessageManager;
31
32 public class PairwiseAlignPanel extends Panel implements ActionListener
33 {
34   Vector sequences = new Vector();
35
36   AlignmentPanel ap;
37
38   public PairwiseAlignPanel(AlignmentPanel ap)
39   {
40     try
41     {
42       jbInit();
43     } catch (Exception e)
44     {
45       e.printStackTrace();
46     }
47     this.ap = ap;
48     sequences = new Vector();
49
50     SequenceI[] seqs;
51     String[] seqStrings = ap.av.getViewAsString(true);
52
53     if (ap.av.getSelectionGroup() == null)
54     {
55       seqs = ap.av.getAlignment().getSequencesArray();
56     }
57     else
58     {
59       seqs = ap.av.getSelectionGroup().getSequencesInOrder(
60               ap.av.getAlignment());
61     }
62
63     float scores[][] = new float[seqs.length][seqs.length];
64     double totscore = 0;
65     int count = ap.av.getSelectionGroup().getSize();
66     String type = (ap.av.getAlignment().isNucleotide()) ? AlignSeq.DNA
67             : AlignSeq.PEP;
68     Sequence seq;
69
70     for (int i = 1; i < count; i++)
71     {
72       for (int j = 0; j < i; j++)
73       {
74
75         AlignSeq as = new AlignSeq(seqs[i], seqStrings[i], seqs[j],
76                 seqStrings[j], type);
77
78         if (as.s1str.length() == 0 || as.s2str.length() == 0)
79         {
80           continue;
81         }
82
83         as.calcScoreMatrix();
84         as.traceAlignment();
85
86         as.printAlignment(System.out);
87         scores[i][j] = (float) as.getMaxScore()
88                 / (float) as.getASeq1().length;
89         totscore = totscore + scores[i][j];
90
91         textarea.append(as.getOutput());
92         seq = new Sequence(as.getS1().getName(), as.getAStr1(), as.getS1()
93                 .getStart(), as.getS1().getEnd());
94         sequences.addElement(seq);
95
96         seq = new Sequence(as.getS2().getName(), as.getAStr2(), as.getS2()
97                 .getStart(), as.getS2().getEnd());
98         sequences.addElement(seq);
99       }
100     }
101
102     if (count > 2)
103     {
104       System.out
105               .println("Pairwise alignment scaled similarity score matrix\n");
106
107       for (int i = 0; i < count; i++)
108       {
109         jalview.util.Format.print(System.out, "%s \n", ("" + i) + " "
110                 + seqs[i].getName());
111       }
112
113       System.out.println("\n");
114
115       for (int i = 0; i < count; i++)
116       {
117         for (int j = 0; j < i; j++)
118         {
119           jalview.util.Format.print(System.out, "%7.3f", scores[i][j]
120                   / totscore);
121         }
122       }
123
124       System.out.println("\n");
125     }
126   }
127
128   public void actionPerformed(ActionEvent evt)
129   {
130     if (evt.getSource() == viewInEditorButton)
131     {
132       viewInEditorButton_actionPerformed();
133     }
134   }
135
136   protected void viewInEditorButton_actionPerformed()
137   {
138
139     Sequence[] seq = new Sequence[sequences.size()];
140
141     for (int i = 0; i < sequences.size(); i++)
142     {
143       seq[i] = (Sequence) sequences.elementAt(i);
144     }
145
146     new AlignFrame(new Alignment(seq), ap.av.applet,
147             "Pairwise Aligned Sequences", false);
148
149   }
150
151   protected ScrollPane scrollPane = new ScrollPane();
152
153   protected TextArea textarea = new TextArea();
154
155   protected Button viewInEditorButton = new Button();
156
157   Panel jPanel1 = new Panel();
158
159   BorderLayout borderLayout1 = new BorderLayout();
160
161   private void jbInit() throws Exception
162   {
163     this.setLayout(borderLayout1);
164     textarea.setFont(new java.awt.Font("Monospaced", 0, 12));
165     textarea.setText("");
166     viewInEditorButton.setFont(new java.awt.Font("Verdana", 0, 12));
167     viewInEditorButton.setLabel(MessageManager
168             .getString("label.view_alignment_editor"));
169     viewInEditorButton.addActionListener(this);
170     this.add(scrollPane, BorderLayout.CENTER);
171     scrollPane.add(textarea);
172     this.add(jPanel1, BorderLayout.SOUTH);
173     jPanel1.add(viewInEditorButton, null);
174   }
175
176 }