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