update author list in license for (JAL-826)
[jalview.git] / src / jalview / gui / PairwiseAlignPanel.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3  * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, 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.gui;
19
20 import java.util.*;
21
22 import java.awt.event.*;
23
24 import jalview.analysis.*;
25 import jalview.datamodel.*;
26 import jalview.jbgui.*;
27
28 /**
29  * DOCUMENT ME!
30  * 
31  * @author $author$
32  * @version $Revision$
33  */
34 public class PairwiseAlignPanel extends GPairwiseAlignPanel
35 {
36
37   AlignViewport av;
38
39   Vector sequences;
40
41   /**
42    * Creates a new PairwiseAlignPanel object.
43    * 
44    * @param av
45    *          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
67             : AlignSeq.PEP;
68
69     float[][] scores = new float[seqs.length][seqs.length];
70     double totscore = 0;
71     int count = seqs.length;
72
73     Sequence seq;
74
75     for (int i = 1; i < count; i++)
76     {
77       for (int j = 0; j < i; j++)
78       {
79
80         AlignSeq as = new AlignSeq(seqs[i], seqStrings[i], seqs[j],
81                 seqStrings[j], type);
82
83         if (as.s1str.length() == 0 || as.s2str.length() == 0)
84         {
85           continue;
86         }
87
88         as.calcScoreMatrix();
89         as.traceAlignment();
90
91         as.printAlignment(System.out);
92         scores[i][j] = (float) as.getMaxScore()
93                 / (float) as.getASeq1().length;
94         totscore = totscore + scores[i][j];
95
96         textarea.append(as.getOutput());
97         seq = new Sequence(as.getS1().getName(), as.getAStr1(), as.getS1()
98                 .getStart(), as.getS1().getEnd());
99         sequences.add(seq);
100
101         seq = new Sequence(as.getS2().getName(), as.getAStr2(), as.getS2()
102                 .getStart(), as.getS2().getEnd());
103         sequences.add(seq);
104       }
105     }
106
107     if (count > 2)
108     {
109       System.out
110               .println("Pairwise alignment scaled similarity score matrix\n");
111
112       for (int i = 0; i < count; i++)
113       {
114         jalview.util.Format.print(System.out, "%s \n", ("" + i) + " "
115                 + seqs[i].getName());
116       }
117
118       System.out.println("\n");
119
120       for (int i = 0; i < count; i++)
121       {
122         for (int j = 0; j < i; j++)
123         {
124           jalview.util.Format.print(System.out, "%7.3f", scores[i][j]
125                   / totscore);
126         }
127       }
128
129       System.out.println("\n");
130     }
131   }
132
133   /**
134    * DOCUMENT ME!
135    * 
136    * @param e
137    *          DOCUMENT ME!
138    */
139   protected void viewInEditorButton_actionPerformed(ActionEvent e)
140   {
141     Sequence[] seq = new Sequence[sequences.size()];
142
143     for (int i = 0; i < sequences.size(); i++)
144     {
145       seq[i] = (Sequence) sequences.elementAt(i);
146     }
147
148     AlignFrame af = new AlignFrame(new Alignment(seq),
149             AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
150
151     Desktop.addInternalFrame(af, "Pairwise Aligned Sequences",
152             AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
153   }
154 }