Clover icon

Coverage Report

  1. Project Clover database Mon Nov 18 2024 09:38:20 GMT
  2. Package jalview.gui

File PairwiseAlignPanel.java

 

Coverage histogram

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

Code metrics

48
128
24
1
486
312
51
0.4
5.33
24
2.12

Classes

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