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