general pairwise substitution score models for pairwise alignments and trees.
[jalview.git] / src / jalview / gui / 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 package jalview.gui;
20
21 import jalview.analysis.*;
22
23 import jalview.datamodel.*;
24
25 import jalview.jbgui.*;
26
27 import java.awt.event.*;
28
29 import java.util.*;
30
31
32 /**
33  * DOCUMENT ME!
34  *
35  * @author $author$
36  * @version $Revision$
37  */
38 public class PairwiseAlignPanel extends GPairwiseAlignPanel
39 {
40
41     AlignViewport av;
42     Vector sequences;
43
44     /**
45      * Creates a new PairwiseAlignPanel object.
46      *
47      * @param av DOCUMENT ME!
48      */
49     public PairwiseAlignPanel(AlignViewport av)
50     {
51         super();
52         this.av = av;
53
54         sequences = new Vector();
55
56         SequenceI [] seqs;
57         String []  seqStrings = av.getViewAsString(true);
58
59         if(av.getSelectionGroup()==null)
60         {
61           seqs = av.alignment.getSequencesArray();
62         }
63         else
64         {
65           seqs = av.getSelectionGroup().getSequencesInOrder(av.alignment);
66         }
67
68         String type=(av.alignment.isNucleotide()) ? AlignSeq.DNA : AlignSeq.PEP;
69         
70         float[][] scores = new float[seqs.length][seqs.length];
71         double totscore = 0;
72         int count = seqs.length;
73
74         Sequence seq;
75
76         for (int i = 1; i < count; i++)
77         {
78             for (int j = 0; j < i; j++)
79             {
80
81                 AlignSeq as = new AlignSeq(seqs[i], seqStrings[i],
82                         seqs[j], seqStrings[j], type);
83
84                 if(as.s1str.length()==0 || as.s2str.length()==0)
85                 {
86                   continue;
87                 }
88
89                 as.calcScoreMatrix();
90                 as.traceAlignment();
91
92
93                 as.printAlignment(System.out);
94                 scores[i][j] = (float) as.getMaxScore() / (float) as.getASeq1().length;
95                 totscore = totscore + scores[i][j];
96
97                 textarea.append(as.getOutput());
98                 seq = new Sequence(as.getS1().getName(),
99                                    as.getAStr1(),
100                                    as.getS1().getStart(),
101                                    as.getS1().getEnd()
102                 );
103                 sequences.add(seq);
104
105                 seq = new Sequence(as.getS2().getName(),
106                                    as.getAStr2(),
107                                    as.getS2().getStart(),
108                                    as.getS2().getEnd() );
109                 sequences.add(seq);
110             }
111         }
112
113         if (count > 2)
114         {
115             System.out.println(
116                 "Pairwise alignment scaled similarity score matrix\n");
117
118             for (int i = 0; i < count; i++)
119             {
120                 jalview.util.Format.print(System.out, "%s \n",
121                     ("" + i) + " " +
122                     seqs[i].getName());
123             }
124
125             System.out.println("\n");
126
127             for (int i = 0; i < count; i++)
128             {
129                 for (int j = 0; j < i; j++)
130                 {
131                     jalview.util.Format.print(System.out, "%7.3f",
132                         scores[i][j] / totscore);
133                 }
134             }
135
136             System.out.println("\n");
137         }
138     }
139
140     /**
141      * DOCUMENT ME!
142      *
143      * @param e DOCUMENT ME!
144      */
145     protected void viewInEditorButton_actionPerformed(ActionEvent e)
146     {
147         Sequence[] seq = new Sequence[sequences.size()];
148
149         for (int i = 0; i < sequences.size(); i++)
150         {
151             seq[i] = (Sequence) sequences.elementAt(i);
152         }
153
154         AlignFrame af = new AlignFrame(new Alignment(seq),
155                                            AlignFrame.DEFAULT_WIDTH,
156                                            AlignFrame.DEFAULT_HEIGHT);
157
158         Desktop.addInternalFrame(af, "Pairwise Aligned Sequences",
159             AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
160     }
161 }