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