bc2c27cd19f1f1db7333b63a94012403a17f69dc
[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         sequences.add(as.getAlignedSeq1());
103         sequences.add(as.getAlignedSeq2());
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,
152             MessageManager.getString("label.pairwise_aligned_sequences"),
153             AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
154   }
155 }