JAL-1807 still testing
[jalviewjs.git] / unused / appletgui / PairwiseAlignPanel.java
1 /*\r
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)\r
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors\r
4  * \r
5  * This file is part of Jalview.\r
6  * \r
7  * Jalview is free software: you can redistribute it and/or\r
8  * modify it under the terms of the GNU General Public License \r
9  * as published by the Free Software Foundation, either version 3\r
10  * of the License, or (at your option) any later version.\r
11  *  \r
12  * Jalview is distributed in the hope that it will be useful, but \r
13  * WITHOUT ANY WARRANTY; without even the implied warranty \r
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR \r
15  * PURPOSE.  See the GNU General Public License for more details.\r
16  * \r
17  * You should have received a copy of the GNU General Public License\r
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.\r
19  * The Jalview Authors are detailed in the 'AUTHORS' file.\r
20  */\r
21 package jalview.appletgui;\r
22 \r
23 import jalview.analysis.AlignSeq;\r
24 import jalview.datamodel.Alignment;\r
25 import jalview.datamodel.Sequence;\r
26 import jalview.datamodel.SequenceI;\r
27 import jalview.util.Format;\r
28 import jalview.util.MessageManager;\r
29 \r
30 import java.awt.BorderLayout;\r
31 import javax.swing.JButton;\r
32 import javax.swing.JPanel;\r
33 import javax.swing.JScrollPane;\r
34 import javax.swing.JTextArea;\r
35 import java.awt.event.ActionEvent;\r
36 import java.awt.event.ActionListener;\r
37 import java.util.Vector;\r
38 \r
39 public class PairwiseAlignPanel extends JPanel implements ActionListener\r
40 {\r
41   Vector sequences = new Vector();\r
42 \r
43   AlignmentPanel ap;\r
44 \r
45   public PairwiseAlignPanel(AlignmentPanel ap)\r
46   {\r
47     try\r
48     {\r
49       jbInit();\r
50     } catch (Exception e)\r
51     {\r
52       e.printStackTrace();\r
53     }\r
54     this.ap = ap;\r
55     sequences = new Vector();\r
56 \r
57     SequenceI[] seqs;\r
58     String[] seqStrings = ap.av.getViewAsString(true);\r
59 \r
60     if (ap.av.getSelectionGroup() == null)\r
61     {\r
62       seqs = ap.av.getAlignment().getSequencesArray();\r
63     }\r
64     else\r
65     {\r
66       seqs = ap.av.getSelectionGroup().getSequencesInOrder(\r
67               ap.av.getAlignment());\r
68     }\r
69 \r
70     float scores[][] = new float[seqs.length][seqs.length];\r
71     double totscore = 0;\r
72     int count = ap.av.getSelectionGroup().getSize();\r
73     String type = (ap.av.getAlignment().isNucleotide()) ? AlignSeq.DNA\r
74             : AlignSeq.PEP;\r
75     Sequence seq;\r
76 \r
77     for (int i = 1; i < count; i++)\r
78     {\r
79       for (int j = 0; j < i; j++)\r
80       {\r
81 \r
82         AlignSeq as = new AlignSeq(seqs[i], seqStrings[i], seqs[j],\r
83                 seqStrings[j], type);\r
84 \r
85         if (as.s1str.length() == 0 || as.s2str.length() == 0)\r
86         {\r
87           continue;\r
88         }\r
89 \r
90         as.calcScoreMatrix();\r
91         as.traceAlignment();\r
92 \r
93         as.printAlignment(System.out);\r
94         scores[i][j] = (float) as.getMaxScore()\r
95                 / (float) as.getASeq1().length;\r
96         totscore = totscore + scores[i][j];\r
97 \r
98         textarea.append(as.getOutput());\r
99         sequences.add(as.getAlignedSeq1());\r
100         sequences.add(as.getAlignedSeq1());\r
101       }\r
102     }\r
103 \r
104     if (count > 2)\r
105     {\r
106       System.out\r
107               .println("Pairwise alignment scaled similarity score matrix\n");\r
108 \r
109       for (int i = 0; i < count; i++)\r
110       {\r
111         Format.print(System.out, "%s \n",\r
112                 ("" + i) + " "\r
113                 + seqs[i].getName());\r
114       }\r
115 \r
116       System.out.println("\n");\r
117 \r
118       for (int i = 0; i < count; i++)\r
119       {\r
120         for (int j = 0; j < i; j++)\r
121         {\r
122           Format.printDouble(System.out, "%7.3f", scores[i][j]\r
123                   / totscore);\r
124         }\r
125       }\r
126 \r
127       System.out.println("\n");\r
128     }\r
129   }\r
130 \r
131   public void actionPerformed(ActionEvent evt)\r
132   {\r
133     if (evt.getSource() == viewInEditorButton)\r
134     {\r
135       viewInEditorButton_actionPerformed();\r
136     }\r
137   }\r
138 \r
139   protected void viewInEditorButton_actionPerformed()\r
140   {\r
141 \r
142     Sequence[] seq = new Sequence[sequences.size()];\r
143 \r
144     for (int i = 0; i < sequences.size(); i++)\r
145     {\r
146       seq[i] = (Sequence) sequences.elementAt(i);\r
147     }\r
148 \r
149     new AlignFrame(new Alignment(seq), ap.av.applet,\r
150             "Pairwise Aligned Sequences", false);\r
151 \r
152   }\r
153 \r
154   protected JScrollPane scrollPane = new JScrollPane();\r
155 \r
156   protected JTextArea textarea = new JTextArea();\r
157 \r
158   protected JButton viewInEditorButton = new JButton();\r
159 \r
160   JPanel jPanel1 = new JPanel();\r
161 \r
162   BorderLayout borderLayout1 = new BorderLayout();\r
163 \r
164   private void jbInit() throws Exception\r
165   {\r
166     this.setLayout(borderLayout1);\r
167     textarea.setFont(new java.awt.Font("Monospaced", 0, 12));\r
168     textarea.setText("");\r
169     viewInEditorButton.setFont(new java.awt.Font("Verdana", 0, 12));\r
170     viewInEditorButton.setLabel(MessageManager\r
171             .getString("label.view_alignment_editor"));\r
172     viewInEditorButton.addActionListener(this);\r
173     this.add(scrollPane, BorderLayout.CENTER);\r
174     scrollPane.add(textarea);\r
175     this.add(jPanel1, BorderLayout.SOUTH);\r
176     jPanel1.add(viewInEditorButton, null);\r
177   }\r
178 \r
179 }\r