1. Project Clover database Fri Dec 6 2024 13:47:14 GMT
  2. Package jalview.gui

File PairwiseAlignPanel.java

 

Coverage histogram

../../img/srcFileCovDistChart5.png
43% of files have more coverage

Code metrics

46
126
24
1
481
306
50
0.4
5.25
24
2.08

Classes

Class
Line #
Actions
PairwiseAlignPanel 43 126 50
0.5051020450.5%
 

Contributing tests

This file is covered by 2 tests. .

Source view

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.datamodel.Alignment;
26    import jalview.datamodel.AlignmentView;
27    import jalview.datamodel.SequenceGroup;
28    import jalview.datamodel.SequenceI;
29    import jalview.jbgui.GPairwiseAlignPanel;
30    import jalview.util.MessageManager;
31    import jalview.viewmodel.AlignmentViewport;
32    import jalview.math.MiscMath;
33   
34    import java.awt.event.ActionEvent;
35    import java.util.Vector;
36   
37    /**
38    * DOCUMENT ME!
39    *
40    * @author $author$
41    * @version $Revision$
42    */
 
43    public class PairwiseAlignPanel extends GPairwiseAlignPanel
44    {
45   
46    private static final String DASHES = "---------------------\n";
47   
48    private float[][] scores;
49   
50    private float[][] alignmentScores; // scores used by PaSiMap
51   
52    private int GAP_OPEN_COST;
53   
54    private int GAP_EXTEND_COST;
55   
56    AlignmentViewport av;
57   
58    Vector<SequenceI> sequences;
59   
60    private String alignmentOutput;
61   
62    private boolean quiet;
63   
64    private boolean discardAlignments;
65   
66    private boolean endGaps;
67   
68    // for listening
69    public static final String TOTAL = "total";
70   
71    public static final String PROGRESS = "progress";
72   
73    protected static final String ETA = "eta_in_minutes";
74   
75    public static final String PROGRESSCOMPLETE = "finished_stop_progress";
76    public static final String PROGRESSMESSAGE = "message_in_progress";
77   
78    private volatile boolean cancelled;
79   
80    private long total;
81   
82    private long progress;
83   
84    private SequenceGroup selection;
85   
86    /**
87    * input sequences
88    */
89    private SequenceI[] seqs = null;
90   
91    private ScoreMatrix scoreMatrix;
92   
93    /**
94    * remaining time
95    */
96    private double etime=Double.NaN;
97   
98    /**
99    * Creates a new PairwiseAlignPanel object.
100    *
101    * @param viewport
102    * contains selected sequences to align
103    * @param endGaps
104    * ~ toggle gaps and the beginning and end of sequences
105    */
 
106  2 toggle public PairwiseAlignPanel(AlignmentViewport viewport)
107    {
108  2 this(viewport, null, false, 120, 20, true, null); // default penalties used
109    // in AlignSeq
110    }
111   
 
112  0 toggle public PairwiseAlignPanel(AlignmentViewport viewport, ScoreMatrix params)
113    {
114  0 this(viewport, null, false, 120, 20, true, params); // default penalties
115    // used in AlignSeq
116    }
117   
 
118  0 toggle public PairwiseAlignPanel(AlignmentViewport viewport, boolean endGaps,
119    int gapOpenCost, int gapExtendCost)
120    {
121  0 this(viewport, null, endGaps, gapOpenCost, gapExtendCost, true, null);
122    }
123   
124    /**
125    * Create a new pairwise alignpanel with specified parameters and score model,
126    * and optionally start the calculation
127    *
128    * @param viewport
129    * @param selection
130    * @param endGaps
131    * @param gapOpenCost
132    * @param gapExtendCost
133    * @param run
134    * @param scoreMatrix
135    */
 
136  2 toggle public PairwiseAlignPanel(AlignmentViewport viewport,
137    SequenceGroup selection, boolean endGaps, int gapOpenCost,
138    int gapExtendCost, boolean run, ScoreMatrix scoreMatrix)
139    {
140  2 super();
141  2 this.av = viewport;
142  2 this.GAP_OPEN_COST = gapOpenCost;
143  2 this.GAP_EXTEND_COST = gapExtendCost;
144  2 this.endGaps = endGaps;
145  2 this.selection = selection;
146  2 this.total = av.getAlignment().getHeight();
147  2 total = (total*total-total)/2;
148  2 this.scoreMatrix = scoreMatrix;
149  2 if (run)
150    {
151  2 calculate();
152    }
153    }
154   
 
155  2 toggle public void calculate()
156    {
157  2 calculate(scoreMatrix);
158    }
159   
 
160  2 toggle public void calculate(ScoreMatrix sm)
161    {
162  2 cancelled=false;
163  2 StringBuilder sb = new StringBuilder(1024);
164   
165  2 sequences = new Vector<SequenceI>();
166  2 String[] seqStrings;
167  2 seqs = null;
168   
169  2 if (selection != null)
170    {
171    // given a set of sequences to compare
172  0 seqs = selection.getSelectionAsNewSequences(av.getAlignment());
173  0 seqStrings = new String[seqs.length];
174  0 int s = 0;
175  0 for (SequenceI seq : seqs)
176    {
177  0 seqStrings[s++] = seq.getSequenceAsString();
178    }
179    }
180    else
181    {
182  2 SequenceGroup selectionGroup = av.getSelectionGroup();
183  2 boolean isSelection = selectionGroup != null
184    && selectionGroup.getSize() > 0;
185  2 AlignmentView view = av.getAlignmentView(isSelection);
186  2 seqStrings = view.getSequenceStrings(av.getGapCharacter());
187  2 if (isSelection)
188    {
189  1 seqs = (SequenceI[]) view
190    .getAlignmentAndHiddenColumns(av.getGapCharacter())[0];
191    }
192    else
193    {
194  1 seqs = av.getAlignment().getSequencesArray();
195    }
196    }
197   
198  2 String type = (av.getAlignment().isNucleotide()) ? AlignSeq.DNA
199    : AlignSeq.PEP;
200   
201  2 float[][] scores = new float[seqs.length][seqs.length];
202  2 float[][] alignmentScores = new float[seqs.length][seqs.length];
203  2 double totscore = 0D;
204  2 int count = seqs.length;
205  2 int fracprogress=0;
206  2 boolean first = true;
207  2 long time=System.currentTimeMillis();
208  2 long fprogress = 0;
209  2 firePropertyChange(TOTAL, 0, 500);
210   
211  4 for (int i = 1; i < count; i++)
212    {
213    // fill diagonal alignmentScores with Float.NaN
214  2 alignmentScores[i - 1][i - 1] = Float.NaN;
215  4 for (int j = 0; j < i; j++)
216    {
217  2 if (cancelled)
218    {
219  0 alignmentOutput = "Alignment was cancelled.";
220  0 return;
221    }
222  2 AlignSeq as = new AlignSeq(seqs[i], seqStrings[i], seqs[j],
223    seqStrings[j], type, GAP_OPEN_COST, GAP_EXTEND_COST);
224   
225  2 if (sm != null)
226    {
227  0 as.setScoreMatrix(sm);
228    }
229   
230  2 if (as.s1str.length() == 0 || as.s2str.length() == 0)
231    {
232  0 continue;
233    }
234   
235  2 as.calcScoreMatrix();
236  2 if (endGaps)
237    {
238  0 as.traceAlignmentWithEndGaps();
239    }
240    else
241    {
242  2 as.traceAlignment();
243    }
244  2 as.scoreAlignment();
245   
246  2 if (!first && !quiet)
247    {
248  0 jalview.bin.Console.outPrintln(DASHES);
249  0 textarea.append(DASHES);
250  0 sb.append(DASHES);
251    }
252  2 first = false;
253  2 if (!discardAlignments)
254    {
255  2 as.printAlignment(System.out);
256    }
257  2 scores[i][j] = as.getMaxScore() / as.getASeq1().length;
258  2 alignmentScores[i][j] = as.getAlignmentScore();
259  2 totscore = totscore + scores[i][j];
260  2 if (!quiet)
261    {
262  2 textarea.append(as.getOutput());
263  2 sb.append(as.getOutput());
264    }
265  2 if (!discardAlignments)
266    {
267  2 sequences.add(as.getAlignedSeq1());
268  2 sequences.add(as.getAlignedSeq2());
269    }
270  2 ++fprogress;
271    }
272  2 if (i<count)
273    {
274  2 int newfracprogress=(int) Math.floor((500.0*(double)fprogress)/((double)total));
275    // need to fake a different starting value until we have an ETA calculated
276  2 firePropertyChange(PROGRESS, fracprogress, newfracprogress);
277  2 fracprogress = newfracprogress;
278  2 progress=fprogress;
279    // remaining time in minutes ~ is remaining*(elapsed time)/progress;
280  2 double lasteta=etime;
281  2 double rate = ((double)(System.currentTimeMillis()-time))/(double)progress;
282  2 etime = rate*(total-progress)/60000;
283  2 firePropertyChange(ETA, lasteta,etime);
284    }
285    }
286  2 alignmentScores[count - 1][count - 1] = Float.NaN;
287    // done - mark progress as indeterminate again
288  2 firePropertyChange(TOTAL, -1, -2);
289   
290   
291  2 this.scores = scores;
292  2 this.alignmentScores = alignmentScores;
293   
294  2 if (count > 2 && !quiet)
295    {
296  0 printScoreMatrix(seqs, scores, totscore);
297    }
298   
299  2 alignmentOutput = sb.toString();
300    }
301   
 
302  0 toggle public boolean hasEta()
303    {
304  0 return etime>0;
305    }
 
306  0 toggle public double getEta()
307    {
308  0 return etime;
309    }
310    /**
311    * stops the run() loop ASAP
312    */
 
313  0 toggle public void cancel()
314    {
315  0 cancelled=true;
316    }
317   
 
318  0 toggle public float[][] getScores()
319    {
320  0 return this.scores;
321    }
322   
 
323  0 toggle public float[][] getAlignmentScores()
324    {
325  0 return this.alignmentScores;
326    }
327   
 
328  0 toggle public String getAlignmentOutput()
329    {
330  0 return this.alignmentOutput;
331    }
332   
333    /**
334    * Prints a matrix of seqi-seqj pairwise alignment scores to sysout
335    *
336    * @param seqs
337    * @param scores
338    * @param totscore
339    */
 
340  0 toggle protected void printScoreMatrix(SequenceI[] seqs, float[][] scores,
341    double totscore)
342    {
343  0 System.out
344    .println("Pairwise alignment scaled similarity score matrix "+getPairwiseSimscoresAsString()+"\n");
345   
346  0 for (int i = 0; i < seqs.length; i++)
347    {
348  0 jalview.bin.Console.outPrintln(
349    String.format("%3d %s", i + 1, seqs[i].getDisplayId(true)));
350    }
351   
352    /*
353    * table heading columns for sequences 1, 2, 3...
354    */
355  0 System.out.print("\n ");
356  0 for (int i = 0; i < seqs.length; i++)
357    {
358  0 System.out.print(String.format("%7d", i + 1));
359    }
360  0 jalview.bin.Console.outPrintln();
361   
362  0 for (int i = 0; i < seqs.length; i++)
363    {
364  0 System.out.print(String.format("%3d", i + 1));
365  0 for (int j = 0; j < i; j++)
366    {
367    /*
368    * as a fraction of tot score, outputs are 0 <= score <= 1
369    */
370  0 System.out.print(String.format("%7.3f", scores[i][j] / totscore));
371    }
372  0 jalview.bin.Console.outPrintln();
373    }
374   
375  0 jalview.bin.Console.outPrintln("\n");
376    }
377   
 
378  0 toggle public String getPairwiseSimscoresAsString()
379    {
380  0 return (scoreMatrix != null
381    ? " (" + scoreMatrix.getName() + ", open=" + GAP_OPEN_COST
382    + ", extend=" + GAP_EXTEND_COST
383  0 + (endGaps ? ", with endGaps" : ", no endGaps") + ")"
384    : "");
385    }
386   
387    /**
388    * DOCUMENT ME!
389    *
390    * @param e
391    * DOCUMENT ME!
392    */
 
393  0 toggle @Override
394    protected void viewInEditorButton_actionPerformed(ActionEvent e)
395    {
396  0 SequenceI[] seq = new SequenceI[sequences.size()];
397   
398  0 for (int i = 0; i < sequences.size(); i++)
399    {
400  0 seq[i] = sequences.elementAt(i);
401    }
402   
403  0 AlignFrame af = new AlignFrame(new Alignment(seq),
404    AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
405   
406  0 Desktop.addInternalFrame(af,
407    MessageManager.getString("label.pairwise_aligned_sequences")+" "+getPairwiseSimscoresAsString(),
408    AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
409    }
410   
 
411  0 toggle public long getTotal()
412    {
413  0 return total;
414    }
415   
 
416  0 toggle public long getProgress()
417    {
418  0 return progress;
419    }
420   
 
421  0 toggle public SequenceI[] getInputSequences()
422    {
423  0 return seqs;
424    }
425   
426    /**
427    * Set to true to suppress output of progress to Console.stdout or GUI
428    *
429    * @param quiet
430    */
 
431  0 toggle public void setQuiet(boolean quiet)
432    {
433  0 this.quiet = quiet;
434    }
435   
436    /**
437    * @return true if no textual alignment report was generated
438    */
 
439  0 toggle public boolean isQuiet()
440    {
441  0 return quiet;
442    }
443   
444    /**
445    * set this if you are only interested in final alignment scores
446    *
447    * @param discard
448    */
 
449  0 toggle public void setDiscardAlignments(boolean discard)
450    {
451  0 discardAlignments = discard;
452    }
453   
454    /**
455    * @return true if no alignments were saved
456    * @return
457    */
 
458  0 toggle public boolean isDiscardAlignments()
459    {
460  0 return discardAlignments;
461    }
462   
463    /**
464    *
465    * @return true if the calculation was cancelled before completion
466    */
 
467  0 toggle public boolean isCancelled()
468    {
469  0 return cancelled;
470    }
471   
472    /**
473    * sends status updates to the progress bar for this panel
474    * @param type - PROGRESSMESSAGE or PROGRESSCOMPLETE
475    * @param message - the message (may be internationalised key)
476    */
 
477  0 toggle public void updateProgress(String type, String message)
478    {
479  0 firePropertyChange(type, "", MessageManager.getStringOrReturn("progress", message));
480    }
481    }