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