Formatting
[jalview.git] / src / jalview / appletgui / PairwiseAlignPanel.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer
3  * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18  */
19
20 package jalview.appletgui;
21
22 import java.util.*;
23
24 import java.awt.*;
25 import java.awt.event.*;
26
27 import jalview.analysis.*;
28 import jalview.datamodel.*;
29
30 public class PairwiseAlignPanel
31     extends Panel implements ActionListener
32 {
33   Vector sequences = new Vector();
34   AlignmentPanel ap;
35
36   public PairwiseAlignPanel(AlignmentPanel ap)
37   {
38     try
39     {
40       jbInit();
41     }
42     catch (Exception e)
43     {
44       e.printStackTrace();
45     }
46     this.ap = ap;
47     sequences = new Vector();
48
49     SequenceI[] seqs;
50     String[] seqStrings = ap.av.getViewAsString(true);
51
52     if (ap.av.getSelectionGroup() == null)
53     {
54       seqs = ap.av.alignment.getSequencesArray();
55     }
56     else
57     {
58       seqs = ap.av.getSelectionGroup().getSequencesInOrder(ap.av.alignment);
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.alignment.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],
74                                    seqs[j], 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() / (float) as.getASeq1().length;
86         totscore = totscore + scores[i][j];
87
88         textarea.append(as.getOutput());
89         seq = new Sequence(as.getS1().getName(),
90                            as.getAStr1(),
91                            as.getS1().getStart(),
92                            as.getS1().getEnd()
93             );
94         sequences.addElement(seq);
95
96         seq = new Sequence(as.getS2().getName(),
97                            as.getAStr2(),
98                            as.getS2().getStart(),
99                            as.getS2().getEnd());
100         sequences.addElement(seq);
101       }
102     }
103
104     if (count > 2)
105     {
106       System.out.println(
107           "Pairwise alignment scaled similarity score matrix\n");
108
109       for (int i = 0; i < count; i++)
110       {
111         jalview.util.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           jalview.util.Format.print(System.out, "%7.3f",
123                                     scores[i][j] / 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),
150                    ap.av.applet,
151                    "Pairwise Aligned Sequences",
152                    false);
153
154   }
155
156   protected ScrollPane scrollPane = new ScrollPane();
157   protected TextArea textarea = new TextArea();
158   protected Button viewInEditorButton = new Button();
159   Panel jPanel1 = new Panel();
160   BorderLayout borderLayout1 = new BorderLayout();
161
162   private void jbInit()
163       throws Exception
164   {
165     this.setLayout(borderLayout1);
166     textarea.setFont(new java.awt.Font("Monospaced", 0, 12));
167     textarea.setText("");
168     viewInEditorButton.setFont(new java.awt.Font("Verdana", 0, 12));
169     viewInEditorButton.setLabel("View in alignment editor");
170     viewInEditorButton.addActionListener(this);
171     this.add(scrollPane, BorderLayout.CENTER);
172     scrollPane.add(textarea);
173     this.add(jPanel1, BorderLayout.SOUTH);
174     jPanel1.add(viewInEditorButton, null);
175   }
176
177 }