X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=src%2Fjalview%2Fgui%2FPairwiseAlignPanel.java;h=087d7f686ecef64d20b08c864e5f3129cdb48ea2;hb=a3fac343a7a385f8ec33a739ada884ebd0e170d8;hp=66ceffea67b67f9a1577a3a07dc5c94098c96f5e;hpb=ad15cff29620f960119f80176f1fd443da9f6763;p=jalview.git diff --git a/src/jalview/gui/PairwiseAlignPanel.java b/src/jalview/gui/PairwiseAlignPanel.java index 66ceffe..087d7f6 100755 --- a/src/jalview/gui/PairwiseAlignPanel.java +++ b/src/jalview/gui/PairwiseAlignPanel.java @@ -22,13 +22,18 @@ package jalview.gui; import jalview.analysis.AlignSeq; import jalview.datamodel.Alignment; -import jalview.datamodel.Sequence; +import jalview.datamodel.AlignmentView; +import jalview.datamodel.SequenceGroup; import jalview.datamodel.SequenceI; import jalview.jbgui.GPairwiseAlignPanel; import jalview.util.MessageManager; +import jalview.viewmodel.AlignmentViewport; +import jalview.math.MiscMath; +import java.beans.PropertyChangeListener; import java.awt.event.ActionEvent; import java.util.Vector; +import javax.swing.event.SwingPropertyChangeSupport; /** * DOCUMENT ME! @@ -39,51 +44,115 @@ import java.util.Vector; public class PairwiseAlignPanel extends GPairwiseAlignPanel { - AlignViewport av; + private static final String DASHES = "---------------------\n"; - Vector sequences; + private float[][] scores; + + private float[][] alignmentScores; // scores used by PaSiMap + + private int GAP_OPEN_COST; + + private int GAP_EXTEND_COST; + + AlignmentViewport av; + + Vector sequences; + + private String alignmentOutput; + + private boolean suppressTextbox; + + private boolean discardAlignments; + + private boolean endGaps; + + // for listening + public static final String TOTAL = "total"; + + public static final String PROGRESS = "progress"; + + private int total; + + private int progress; /** * Creates a new PairwiseAlignPanel object. * - * @param av + * @param viewport * DOCUMENT ME! + * @param endGaps ~ toggle gaps and the beginning and end of sequences */ - public PairwiseAlignPanel(AlignViewport av) + public PairwiseAlignPanel(AlignmentViewport viewport) + { + this(viewport, false, 120, 20, true); // default penalties used in AlignSeq + } + public PairwiseAlignPanel(AlignmentViewport viewport, boolean endGaps, int gapOpenCost, int gapExtendCost) + { + this(viewport, endGaps, gapOpenCost, gapExtendCost, true); + } + public PairwiseAlignPanel(AlignmentViewport viewport, boolean endGaps, int gapOpenCost, int gapExtendCost, boolean run) { super(); - this.av = av; + this.av = viewport; + this.GAP_OPEN_COST = gapOpenCost; + this.GAP_EXTEND_COST = gapExtendCost; + this.endGaps = endGaps; + this.total = MiscMath.combinations(av.getAlignment().getHeight(), 2); + + if (run) + calculate(); +System.out.println("Creating pap"); + } - sequences = new Vector(); + public void calculate() + { - SequenceI[] seqs; - String[] seqStrings = av.getViewAsString(true); + SequenceGroup selectionGroup = av.getSelectionGroup(); + StringBuilder sb = new StringBuilder(1024); + + sequences = new Vector(); + + boolean isSelection = selectionGroup != null + && selectionGroup.getSize() > 0; + AlignmentView view = av.getAlignmentView(isSelection); + // String[] seqStrings = av.getViewAsString(true); + String[] seqStrings = view + .getSequenceStrings(av.getGapCharacter()); - if (av.getSelectionGroup() == null) + SequenceI[] seqs; + if (isSelection) { - seqs = av.getAlignment().getSequencesArray(); + seqs = (SequenceI[]) view + .getAlignmentAndHiddenColumns(av.getGapCharacter())[0]; } else { - seqs = av.getSelectionGroup().getSequencesInOrder(av.getAlignment()); + seqs = av.getAlignment().getSequencesArray(); } String type = (av.getAlignment().isNucleotide()) ? AlignSeq.DNA : AlignSeq.PEP; float[][] scores = new float[seqs.length][seqs.length]; - double totscore = 0; + float[][] alignmentScores = new float[seqs.length][seqs.length]; + double totscore = 0D; int count = seqs.length; + boolean first = true; + + progress = 0; + firePropertyChange(TOTAL, 0, total); - Sequence seq; + suppressTextbox = count<10; + discardAlignments = count<15; for (int i = 1; i < count; i++) { + // fill diagonal alignmentScores with Float.NaN + alignmentScores[i - 1][i - 1] = Float.NaN; for (int j = 0; j < i; j++) { - AlignSeq as = new AlignSeq(seqs[i], seqStrings[i], seqs[j], - seqStrings[j], type); + seqStrings[j], type, GAP_OPEN_COST, GAP_EXTEND_COST); if (as.s1str.length() == 0 || as.s2str.length() == 0) { @@ -91,43 +160,113 @@ public class PairwiseAlignPanel extends GPairwiseAlignPanel } as.calcScoreMatrix(); - as.traceAlignment(); + if (endGaps) + { + as.traceAlignmentWithEndGaps(); + } else { + as.traceAlignment(); + } + as.scoreAlignment(); - as.printAlignment(System.out); - scores[i][j] = (float) as.getMaxScore() - / (float) as.getASeq1().length; + if (!first) + { + System.out.println(DASHES); + textarea.append(DASHES); + sb.append(DASHES); + } + first = false; + if (discardAlignments) { + as.printAlignment(System.out); + } + scores[i][j] = as.getMaxScore() / as.getASeq1().length; + alignmentScores[i][j] = as.getAlignmentScore(); totscore = totscore + scores[i][j]; - textarea.append(as.getOutput()); - sequences.add(as.getAlignedSeq1()); - sequences.add(as.getAlignedSeq2()); + if (suppressTextbox) + { + textarea.append(as.getOutput()); + sb.append(as.getOutput()); + } + if (discardAlignments) + { + sequences.add(as.getAlignedSeq1()); + sequences.add(as.getAlignedSeq2()); + } + + firePropertyChange(PROGRESS, progress, ++progress); } } + alignmentScores[count - 1][count - 1] = Float.NaN; + + this.scores = scores; + this.alignmentScores = alignmentScores; if (count > 2) { - System.out - .println("Pairwise alignment scaled similarity score matrix\n"); + printScoreMatrix(seqs, scores, totscore); + } - for (int i = 0; i < count; i++) - { - jalview.util.Format.print(System.out, "%s \n", ("" + i) + " " - + seqs[i].getName()); - } + alignmentOutput = sb.toString(); + } + + public float[][] getScores() + { + return this.scores; + } + + public float[][] getAlignmentScores() + { + return this.alignmentScores; + } + + public String getAlignmentOutput() + { + return this.alignmentOutput; + } + + /** + * Prints a matrix of seqi-seqj pairwise alignment scores to sysout + * + * @param seqs + * @param scores + * @param totscore + */ + protected void printScoreMatrix(SequenceI[] seqs, float[][] scores, + double totscore) + { + System.out + .println("Pairwise alignment scaled similarity score matrix\n"); + + for (int i = 0; i < seqs.length; i++) + { + System.out.println( + String.format("%3d %s", i + 1, seqs[i].getDisplayId(true))); + } - System.out.println("\n"); + /* + * table heading columns for sequences 1, 2, 3... + */ + System.out.print("\n "); + for (int i = 0; i < seqs.length; i++) + { + System.out.print(String.format("%7d", i + 1)); + } + System.out.println(); - for (int i = 0; i < count; i++) + for (int i = 0; i < seqs.length; i++) + { + System.out.print(String.format("%3d", i + 1)); + for (int j = 0; j < i; j++) { - for (int j = 0; j < i; j++) - { - jalview.util.Format.print(System.out, "%7.3f", scores[i][j] - / totscore); - } + /* + * as a fraction of tot score, outputs are 0 <= score <= 1 + */ + System.out.print(String.format("%7.3f", scores[i][j] / totscore)); } - - System.out.println("\n"); + System.out.println(); } + + System.out.println("\n"); } /** @@ -136,13 +275,14 @@ public class PairwiseAlignPanel extends GPairwiseAlignPanel * @param e * DOCUMENT ME! */ + @Override protected void viewInEditorButton_actionPerformed(ActionEvent e) { - Sequence[] seq = new Sequence[sequences.size()]; + SequenceI[] seq = new SequenceI[sequences.size()]; for (int i = 0; i < sequences.size(); i++) { - seq[i] = (Sequence) sequences.elementAt(i); + seq[i] = sequences.elementAt(i); } AlignFrame af = new AlignFrame(new Alignment(seq), @@ -152,4 +292,14 @@ public class PairwiseAlignPanel extends GPairwiseAlignPanel MessageManager.getString("label.pairwise_aligned_sequences"), AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT); } + + public long getTotal() + { + return total; + } + + public long getProgress() + { + return progress; + } }