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