Hidden representatives moved from sequence to viewport
[jalview.git] / src / jalview / appletgui / PairwiseAlignPanel.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer
3  * Copyright (C) 2006 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.event.*;
25
26 import jalview.analysis.*;
27 import jalview.datamodel.*;
28 import java.awt.*;
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 : 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],
73                                    seqs[j], 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() / (float) as.getASeq1().length;
85         totscore = totscore + scores[i][j];
86
87         textarea.append(as.getOutput());
88         seq = new Sequence(as.getS1().getName(),
89                            as.getAStr1(),
90                            as.getS1().getStart(),
91                            as.getS1().getEnd()
92             );
93         sequences.addElement(seq);
94
95         seq = new Sequence(as.getS2().getName(),
96                            as.getAStr2(),
97                            as.getS2().getStart(),
98                            as.getS2().getEnd());
99         sequences.addElement(seq);
100       }
101     }
102
103     if (count > 2)
104     {
105       System.out.println(
106           "Pairwise alignment scaled similarity score matrix\n");
107
108       for (int i = 0; i < count; i++)
109       {
110         jalview.util.Format.print(System.out, "%s \n",
111                                   ("" + i) + " " +
112                                   seqs[i].getName());
113       }
114
115       System.out.println("\n");
116
117       for (int i = 0; i < count; i++)
118       {
119         for (int j = 0; j < i; j++)
120         {
121           jalview.util.Format.print(System.out, "%7.3f",
122                                     scores[i][j] / totscore);
123         }
124       }
125
126       System.out.println("\n");
127     }
128   }
129   public void actionPerformed(ActionEvent evt)
130   {
131     if(evt.getSource()==viewInEditorButton)
132       viewInEditorButton_actionPerformed();
133   }
134
135   protected void viewInEditorButton_actionPerformed()
136   {
137
138     Sequence[] seq = new Sequence[sequences.size()];
139
140     for (int i = 0; i < sequences.size(); i++)
141     {
142       seq[i] = (Sequence) sequences.elementAt(i);
143     }
144
145     new AlignFrame(new Alignment(seq),
146                                    ap.av.applet,
147                                    "Pairwise Aligned Sequences",
148                                    false);
149
150   }
151   protected ScrollPane scrollPane = new ScrollPane();
152   protected TextArea textarea = new TextArea();
153   protected Button viewInEditorButton = new Button();
154   Panel jPanel1 = new Panel();
155   BorderLayout borderLayout1 = new BorderLayout();
156
157   private void jbInit() throws Exception {
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 }