Clover icon

Coverage Report

  1. Project Clover database Thu Nov 7 2024 17:01:39 GMT
  2. Package jalview.gui

File PairwiseAlignPanel.java

 

Coverage histogram

../../img/srcFileCovDistChart0.png
0% 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.00%
 

Contributing tests

No tests hitting this source file were found.

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  0 toggle public PairwiseAlignPanel(AlignmentViewport viewport)
107    {
108  0 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  0 toggle public PairwiseAlignPanel(AlignmentViewport viewport,
137    SequenceGroup selection, boolean endGaps, int gapOpenCost,
138    int gapExtendCost, boolean run, ScoreMatrix scoreMatrix)
139    {
140  0 super();
141  0 this.av = viewport;
142  0 this.GAP_OPEN_COST = gapOpenCost;
143  0 this.GAP_EXTEND_COST = gapExtendCost;
144  0 this.endGaps = endGaps;
145  0 this.selection = selection;
146  0 this.total = av.getAlignment().getHeight();
147  0 total = (total*total-total)/2;
148  0 this.scoreMatrix = scoreMatrix;
149  0 if (run)
150    {
151  0 calculate();
152    }
153    }
154   
 
155  0 toggle public void calculate()
156    {
157  0 calculate(scoreMatrix);
158    }
159   
 
160  0 toggle public void calculate(ScoreMatrix sm)
161    {
162  0 cancelled=false;
163  0 StringBuilder sb = new StringBuilder(1024);
164   
165  0 sequences = new Vector<SequenceI>();
166  0 String[] seqStrings;
167  0 seqs = null;
168   
169  0 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  0 SequenceGroup selectionGroup = av.getSelectionGroup();
183  0 boolean isSelection = selectionGroup != null
184    && selectionGroup.getSize() > 0;
185  0 AlignmentView view = av.getAlignmentView(isSelection);
186  0 seqStrings = view.getSequenceStrings(av.getGapCharacter());
187  0 if (isSelection)
188    {
189  0 seqs = (SequenceI[]) view
190    .getAlignmentAndHiddenColumns(av.getGapCharacter())[0];
191    }
192    else
193    {
194  0 seqs = av.getAlignment().getSequencesArray();
195    }
196    }
197   
198  0 String type = (av.getAlignment().isNucleotide()) ? AlignSeq.DNA
199    : AlignSeq.PEP;
200   
201  0 float[][] scores = new float[seqs.length][seqs.length];
202  0 float[][] alignmentScores = new float[seqs.length][seqs.length];
203  0 double totscore = 0D;
204  0 int count = seqs.length;
205  0 int fracprogress=0;
206  0 boolean first = true;
207  0 long time=System.currentTimeMillis();
208  0 long fprogress = 0;
209  0 firePropertyChange(TOTAL, 0, 500);
210   
211  0 for (int i = 1; i < count; i++)
212    {
213    // fill diagonal alignmentScores with Float.NaN
214  0 alignmentScores[i - 1][i - 1] = Float.NaN;
215  0 for (int j = 0; j < i; j++)
216    {
217  0 if (cancelled)
218    {
219  0 alignmentOutput = "Alignment was cancelled.";
220  0 return;
221    }
222  0 AlignSeq as = new AlignSeq(seqs[i], seqStrings[i], seqs[j],
223    seqStrings[j], type, GAP_OPEN_COST, GAP_EXTEND_COST);
224   
225  0 if (sm != null)
226    {
227  0 as.setScoreMatrix(sm);
228    }
229   
230  0 if (as.s1str.length() == 0 || as.s2str.length() == 0)
231    {
232  0 continue;
233    }
234   
235  0 as.calcScoreMatrix();
236  0 if (endGaps)
237    {
238  0 as.traceAlignmentWithEndGaps();
239    }
240    else
241    {
242  0 as.traceAlignment();
243    }
244  0 as.scoreAlignment();
245   
246  0 if (!first && !quiet)
247    {
248  0 jalview.bin.Console.outPrintln(DASHES);
249  0 textarea.append(DASHES);
250  0 sb.append(DASHES);
251    }
252  0 first = false;
253  0 if (!discardAlignments)
254    {
255  0 as.printAlignment(System.out);
256    }
257  0 scores[i][j] = as.getMaxScore() / as.getASeq1().length;
258  0 alignmentScores[i][j] = as.getAlignmentScore();
259  0 totscore = totscore + scores[i][j];
260  0 if (!quiet)
261    {
262  0 textarea.append(as.getOutput());
263  0 sb.append(as.getOutput());
264    }
265  0 if (!discardAlignments)
266    {
267  0 sequences.add(as.getAlignedSeq1());
268  0 sequences.add(as.getAlignedSeq2());
269    }
270  0 ++fprogress;
271    }
272  0 if (i<count)
273    {
274  0 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  0 firePropertyChange(PROGRESS, fracprogress, newfracprogress);
277  0 fracprogress = newfracprogress;
278  0 progress=fprogress;
279    // remaining time in minutes ~ is remaining*(elapsed time)/progress;
280  0 double lasteta=etime;
281  0 double rate = ((double)(System.currentTimeMillis()-time))/(double)progress;
282  0 etime = rate*(total-progress)/60000;
283  0 firePropertyChange(ETA, lasteta,etime);
284    }
285    }
286  0 alignmentScores[count - 1][count - 1] = Float.NaN;
287    // done - mark progress as indeterminate again
288  0 firePropertyChange(TOTAL, -1, -2);
289   
290   
291  0 this.scores = scores;
292  0 this.alignmentScores = alignmentScores;
293   
294  0 if (count > 2 && !quiet)
295    {
296  0 printScoreMatrix(seqs, scores, totscore);
297    }
298   
299  0 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    }