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