JAL-789 pairwise alignment via calculations dialog, allowing score model to be changed
[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.analysis.scoremodels.ScoreMatrix;
25 import jalview.api.analysis.ScoreModelI;
26 import jalview.api.analysis.SimilarityParamsI;
27 import jalview.datamodel.Alignment;
28 import jalview.datamodel.AlignmentView;
29 import jalview.datamodel.SequenceGroup;
30 import jalview.datamodel.SequenceI;
31 import jalview.jbgui.GPairwiseAlignPanel;
32 import jalview.util.MessageManager;
33 import jalview.viewmodel.AlignmentViewport;
34
35 import java.awt.event.ActionEvent;
36 import java.util.Vector;
37
38 /**
39  * DOCUMENT ME!
40  * 
41  * @author $author$
42  * @version $Revision$
43  */
44 public class PairwiseAlignPanel extends GPairwiseAlignPanel
45 {
46
47   private static final String DASHES = "---------------------\n";
48
49   AlignmentViewport av;
50
51   Vector<SequenceI> sequences;
52
53   /**
54    * Creates a new PairwiseAlignPanel object.
55    * 
56    * @param viewport
57    *          contains selected sequences to align
58    */
59   public PairwiseAlignPanel(AlignmentViewport viewport)
60   {
61     this(viewport,null);
62   }
63  
64   /**
65    * Creates a new PairwiseAlignPanel object.
66    * 
67    * @param viewport
68    *          contains selected sequences to align
69    */
70
71   public PairwiseAlignPanel(AlignmentViewport viewport, ScoreMatrix params)
72   {
73     super();
74     this.av = viewport;
75
76     sequences = new Vector<SequenceI>();
77
78     SequenceGroup selectionGroup = viewport.getSelectionGroup();
79     boolean isSelection = selectionGroup != null
80             && selectionGroup.getSize() > 0;
81     AlignmentView view = viewport.getAlignmentView(isSelection);
82     // String[] seqStrings = viewport.getViewAsString(true);
83     String[] seqStrings = view
84             .getSequenceStrings(viewport.getGapCharacter());
85
86     SequenceI[] seqs;
87     if (isSelection)
88     {
89       seqs = (SequenceI[]) view
90               .getAlignmentAndHiddenColumns(viewport.getGapCharacter())[0];
91     }
92     else
93     {
94       seqs = av.getAlignment().getSequencesArray();
95     }
96
97     String type = (viewport.getAlignment().isNucleotide()) ? AlignSeq.DNA
98             : AlignSeq.PEP;
99
100     float[][] scores = new float[seqs.length][seqs.length];
101     double totscore = 0D;
102     int count = seqs.length;
103     boolean first = true;
104
105     for (int i = 1; i < count; i++)
106     {
107       for (int j = 0; j < i; j++)
108       {
109         AlignSeq as = new AlignSeq(seqs[i], seqStrings[i], seqs[j],
110                 seqStrings[j], type);
111
112         if (as.s1str.length() == 0 || as.s2str.length() == 0)
113         {
114           continue;
115         }
116         if (params!=null)
117         {
118           as.setScoreMatrix(params);
119         }
120         as.calcScoreMatrix();
121         as.traceAlignment();
122
123         if (!first)
124         {
125           jalview.bin.Console.outPrintln(DASHES);
126           textarea.append(DASHES);
127         }
128         first = false;
129         as.printAlignment(System.out);
130         scores[i][j] = as.getMaxScore() / as.getASeq1().length;
131         totscore = totscore + scores[i][j];
132
133         textarea.append(as.getOutput());
134         sequences.add(as.getAlignedSeq1());
135         sequences.add(as.getAlignedSeq2());
136       }
137     }
138
139     if (count > 2)
140     {
141       printScoreMatrix(seqs, scores, totscore);
142     }
143   }
144
145   /**
146    * Prints a matrix of seqi-seqj pairwise alignment scores to sysout
147    * 
148    * @param seqs
149    * @param scores
150    * @param totscore
151    */
152   protected void printScoreMatrix(SequenceI[] seqs, float[][] scores,
153           double totscore)
154   {
155     System.out
156             .println("Pairwise alignment scaled similarity score matrix\n");
157
158     for (int i = 0; i < seqs.length; i++)
159     {
160       jalview.bin.Console.outPrintln(
161               String.format("%3d %s", i + 1, seqs[i].getDisplayId(true)));
162     }
163
164     /*
165      * table heading columns for sequences 1, 2, 3...
166      */
167     System.out.print("\n ");
168     for (int i = 0; i < seqs.length; i++)
169     {
170       System.out.print(String.format("%7d", i + 1));
171     }
172     jalview.bin.Console.outPrintln();
173
174     for (int i = 0; i < seqs.length; i++)
175     {
176       System.out.print(String.format("%3d", i + 1));
177       for (int j = 0; j < i; j++)
178       {
179         /*
180          * as a fraction of tot score, outputs are 0 <= score <= 1
181          */
182         System.out.print(String.format("%7.3f", scores[i][j] / totscore));
183       }
184       jalview.bin.Console.outPrintln();
185     }
186
187     jalview.bin.Console.outPrintln("\n");
188   }
189
190   /**
191    * DOCUMENT ME!
192    * 
193    * @param e
194    *          DOCUMENT ME!
195    */
196   @Override
197   protected void viewInEditorButton_actionPerformed(ActionEvent e)
198   {
199     SequenceI[] seq = new SequenceI[sequences.size()];
200
201     for (int i = 0; i < sequences.size(); i++)
202     {
203       seq[i] = sequences.elementAt(i);
204     }
205
206     AlignFrame af = new AlignFrame(new Alignment(seq),
207             AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
208
209     Desktop.addInternalFrame(af,
210             MessageManager.getString("label.pairwise_aligned_sequences"),
211             AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
212   }
213 }