dcddf8863e31b15310a277054d29772743733313
[jalview.git] / src / jalview / gui / PairwiseAlignPanel.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
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
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.gui;
22
23 import java.util.*;
24 import java.awt.event.*;
25
26 import jalview.analysis.*;
27 import jalview.datamodel.*;
28 import jalview.jbgui.*;
29 import jalview.util.MessageManager;
30
31 /**
32  * DOCUMENT ME!
33  * 
34  * @author $author$
35  * @version $Revision$
36  */
37 public class PairwiseAlignPanel extends GPairwiseAlignPanel
38 {
39
40   AlignViewport av;
41
42   Vector sequences;
43
44   /**
45    * Creates a new PairwiseAlignPanel object.
46    * 
47    * @param av
48    *          DOCUMENT ME!
49    */
50   public PairwiseAlignPanel(AlignViewport av)
51   {
52     super();
53     this.av = av;
54
55     sequences = new Vector();
56
57     SequenceI[] seqs;
58     String[] seqStrings = av.getViewAsString(true);
59
60     if (av.getSelectionGroup() == null)
61     {
62       seqs = av.getAlignment().getSequencesArray();
63     }
64     else
65     {
66       seqs = av.getSelectionGroup().getSequencesInOrder(av.getAlignment());
67     }
68
69     String type = (av.getAlignment().isNucleotide()) ? AlignSeq.DNA
70             : AlignSeq.PEP;
71
72     float[][] scores = new float[seqs.length][seqs.length];
73     double totscore = 0;
74     int count = seqs.length;
75
76     Sequence seq;
77
78     for (int i = 1; i < count; i++)
79     {
80       for (int j = 0; j < i; j++)
81       {
82
83         AlignSeq as = new AlignSeq(seqs[i], seqStrings[i], seqs[j],
84                 seqStrings[j], type);
85
86         if (as.s1str.length() == 0 || as.s2str.length() == 0)
87         {
88           continue;
89         }
90
91         as.calcScoreMatrix();
92         as.traceAlignment();
93
94         as.printAlignment(System.out);
95         scores[i][j] = (float) as.getMaxScore()
96                 / (float) as.getASeq1().length;
97         totscore = totscore + scores[i][j];
98
99         textarea.append(as.getOutput());
100         seq = new Sequence(as.getS1().getName(), as.getAStr1(), as.getS1()
101                 .getStart(), as.getS1().getEnd());
102         sequences.add(seq);
103
104         seq = new Sequence(as.getS2().getName(), as.getAStr2(), as.getS2()
105                 .getStart(), as.getS2().getEnd());
106         sequences.add(seq);
107       }
108     }
109
110     if (count > 2)
111     {
112       System.out
113               .println("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", ("" + i) + " "
118                 + seqs[i].getName());
119       }
120
121       System.out.println("\n");
122
123       for (int i = 0; i < count; i++)
124       {
125         for (int j = 0; j < i; j++)
126         {
127           jalview.util.Format.print(System.out, "%7.3f", scores[i][j]
128                   / totscore);
129         }
130       }
131
132       System.out.println("\n");
133     }
134   }
135
136   /**
137    * DOCUMENT ME!
138    * 
139    * @param e
140    *          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, AlignFrame.DEFAULT_HEIGHT);
153
154     Desktop.addInternalFrame(af,
155             MessageManager.getString("label.pairwise_aligned_sequences"),
156             AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
157   }
158 }