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