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