22f1368e6494b80fa79bed80354f704c01253d2b
[jalview.git] / src / jalview / gui / PairwiseAlignPanel.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ 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 jalview.analysis.AlignSeq;
24 import jalview.datamodel.Alignment;
25 import jalview.datamodel.Sequence;
26 import jalview.datamodel.SequenceI;
27 import jalview.jbgui.GPairwiseAlignPanel;
28 import jalview.util.MessageManager;
29 import jalview.viewmodel.AlignmentViewport;
30
31 import java.awt.event.ActionEvent;
32 import java.util.Vector;
33
34 /**
35  * DOCUMENT ME!
36  * 
37  * @author $author$
38  * @version $Revision$
39  */
40 public class PairwiseAlignPanel extends GPairwiseAlignPanel
41 {
42
43   AlignmentViewport av;
44
45   Vector sequences;
46
47   /**
48    * Creates a new PairwiseAlignPanel object.
49    * 
50    * @param av
51    *          DOCUMENT ME!
52    */
53   public PairwiseAlignPanel(AlignmentViewport av)
54   {
55     super();
56     this.av = av;
57
58     sequences = new Vector();
59
60     SequenceI[] seqs;
61     String[] seqStrings = av.getViewAsString(true);
62
63     if (av.getSelectionGroup() == null)
64     {
65       seqs = av.getAlignment().getSequencesArray();
66     }
67     else
68     {
69       seqs = av.getSelectionGroup().getSequencesInOrder(av.getAlignment());
70     }
71
72     String type = (av.getAlignment().isNucleotide()) ? AlignSeq.DNA
73             : AlignSeq.PEP;
74
75     float[][] scores = new float[seqs.length][seqs.length];
76     double totscore = 0;
77     int count = seqs.length;
78
79     Sequence seq;
80
81     for (int i = 1; i < count; i++)
82     {
83       for (int j = 0; j < i; j++)
84       {
85
86         AlignSeq as = new AlignSeq(seqs[i], seqStrings[i], seqs[j],
87                 seqStrings[j], type);
88
89         if (as.s1str.length() == 0 || as.s2str.length() == 0)
90         {
91           continue;
92         }
93
94         as.calcScoreMatrix();
95         as.traceAlignment();
96
97         as.printAlignment(System.out);
98         scores[i][j] = (float) as.getMaxScore()
99                 / (float) as.getASeq1().length;
100         totscore = totscore + scores[i][j];
101
102         textarea.append(as.getOutput());
103         sequences.add(as.getAlignedSeq1());
104         sequences.add(as.getAlignedSeq2());
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,
153             MessageManager.getString("label.pairwise_aligned_sequences"),
154             AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
155   }
156 }