JAL-1432 updated copyright notices
[jalview.git] / src / jalview / gui / PairwiseAlignPanel.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
3  * Copyright (C) 2014 The Jalview Authors
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  * The Jalview Authors are detailed in the 'AUTHORS' file.
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 extends GPairwiseAlignPanel
36 {
37
38   AlignViewport av;
39
40   Vector sequences;
41
42   /**
43    * Creates a new PairwiseAlignPanel object.
44    * 
45    * @param av
46    *          DOCUMENT ME!
47    */
48   public PairwiseAlignPanel(AlignViewport av)
49   {
50     super();
51     this.av = av;
52
53     sequences = new Vector();
54
55     SequenceI[] seqs;
56     String[] seqStrings = av.getViewAsString(true);
57
58     if (av.getSelectionGroup() == null)
59     {
60       seqs = av.getAlignment().getSequencesArray();
61     }
62     else
63     {
64       seqs = av.getSelectionGroup().getSequencesInOrder(av.getAlignment());
65     }
66
67     String type = (av.getAlignment().isNucleotide()) ? AlignSeq.DNA
68             : 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], seqs[j],
82                 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         as.printAlignment(System.out);
93         scores[i][j] = (float) as.getMaxScore()
94                 / (float) as.getASeq1().length;
95         totscore = totscore + scores[i][j];
96
97         textarea.append(as.getOutput());
98         seq = new Sequence(as.getS1().getName(), as.getAStr1(), as.getS1()
99                 .getStart(), as.getS1().getEnd());
100         sequences.add(seq);
101
102         seq = new Sequence(as.getS2().getName(), as.getAStr2(), as.getS2()
103                 .getStart(), as.getS2().getEnd());
104         sequences.add(seq);
105       }
106     }
107
108     if (count > 2)
109     {
110       System.out
111               .println("Pairwise alignment scaled similarity score matrix\n");
112
113       for (int i = 0; i < count; i++)
114       {
115         jalview.util.Format.print(System.out, "%s \n", ("" + i) + " "
116                 + seqs[i].getName());
117       }
118
119       System.out.println("\n");
120
121       for (int i = 0; i < count; i++)
122       {
123         for (int j = 0; j < i; j++)
124         {
125           jalview.util.Format.print(System.out, "%7.3f", scores[i][j]
126                   / totscore);
127         }
128       }
129
130       System.out.println("\n");
131     }
132   }
133
134   /**
135    * DOCUMENT ME!
136    * 
137    * @param e
138    *          DOCUMENT ME!
139    */
140   protected void viewInEditorButton_actionPerformed(ActionEvent e)
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     AlignFrame af = new AlignFrame(new Alignment(seq),
150             AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
151
152     Desktop.addInternalFrame(af, "Pairwise Aligned Sequences",
153             AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
154   }
155 }