From d7989f319488cc56440f822261bc5014c27e03c6 Mon Sep 17 00:00:00 2001 From: tcofoegbu Date: Tue, 9 Jun 2015 16:01:48 +0100 Subject: [PATCH] JAL-1488 bug-fix for incorrect start/end position in flatfiles exported from alignments with hidden columns --- src/jalview/gui/AlignFrame.java | 102 +++++++++++++++++++++++++++++---- src/jalview/gui/AnnotationLabels.java | 2 +- src/jalview/io/FormatAdapter.java | 62 ++++++++++++++++---- 3 files changed, 143 insertions(+), 23 deletions(-) diff --git a/src/jalview/gui/AlignFrame.java b/src/jalview/gui/AlignFrame.java index ad2bbc3..6481855 100644 --- a/src/jalview/gui/AlignFrame.java +++ b/src/jalview/gui/AlignFrame.java @@ -1126,7 +1126,8 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener, String output = f.formatSequences(format, exportData.getAlignment(), // class cast exceptions will // occur in the distant future - exportData.getOmitHidden(), f.getCacheSuffixDefault(format), + exportData.getOmitHidden(), exportData.getStartEndPostions(), + f.getCacheSuffixDefault(format), viewport.getColumnSelection()); if (output == null) @@ -1202,7 +1203,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener, cap.setText(new FormatAdapter(viewport).formatSequences( e.getActionCommand(), exportData.getAlignment(), - exportData.getOmitHidden(), + exportData.getOmitHidden(), exportData.getStartEndPostions(), viewport.getColumnSelection())); Desktop.addInternalFrame(cap, MessageManager.formatMessage( "label.alignment_output_command", new Object[] @@ -1219,10 +1220,17 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener, { AlignmentI alignmentToExport = null; String[] omitHidden = null; + int[] alignmentStartEnd = new int[2]; FeatureRenderer fr = new FeatureRenderer(this.alignPanel); viewport.setFeatureRenderer(fr); HiddenSequences hiddenSeqs = viewport.getAlignment() .getHiddenSequences(); + + + alignmentToExport = viewport.getAlignment(); + alignmentStartEnd = new int[] + { 0, alignmentToExport.getWidth() - 1 }; + if (viewport.hasHiddenColumns() || hiddenSeqs.getSize() > 0) { int reply = JOptionPane @@ -1237,19 +1245,81 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener, if (reply == JOptionPane.YES_OPTION) { + // export only visible region omitHidden = viewport.getViewAsString(false); + alignmentToExport = viewport.getAlignment(); + alignmentStartEnd = getStartEnd(alignmentStartEnd, viewport + .getColumnSelection().getHiddenColumns()); } else { - alignmentToExport = viewport.getAlignment().getHiddenSequences() - .getFullAlignment(); + // export all region including visible + alignmentToExport = hiddenSeqs.getFullAlignment(); } } - if (alignmentToExport == null) + + return new ExportData(alignmentToExport, omitHidden, alignmentStartEnd); + } + + private static int[] getStartEnd(int[] aligmentStartEnd, + List hiddenCols) + { + int startPos = aligmentStartEnd[0]; + int endPos = aligmentStartEnd[1]; + + int[] lowestRange = new int[2]; + int[] higestRange = new int[2]; + + for (int[] hiddenCol : hiddenCols) + { + // System.out.println("comparing : " + hiddenCol[0] + "-" + hiddenCol[1]); + + lowestRange = (hiddenCol[0] <= startPos) ? hiddenCol : lowestRange; + higestRange = (hiddenCol[1] >= endPos) ? hiddenCol : higestRange; + } + // System.out.println("min : " + lowestRange[0] + "-" + lowestRange[1]); + // System.out.println("max : " + higestRange[0] + "-" + higestRange[1]); + + if (lowestRange[0] == 0 && lowestRange[1] == 0) + { + startPos = aligmentStartEnd[0]; + } + else + { + startPos = lowestRange[1] + 1; + } + + if (higestRange[0] == 0 && higestRange[1] == 0) + { + endPos = aligmentStartEnd[1]; + } + else { - alignmentToExport = viewport.getAlignment(); + endPos = higestRange[0]; } - return new ExportData(alignmentToExport, omitHidden); + + // System.out.println("Export range : " + minPos + " - " + maxPos); + return new int[] + { startPos, endPos }; + } + + public static void main(String[] args) + { + ArrayList hiddenCols = new ArrayList(); + hiddenCols.add(new int[] + { 0, 4 }); + hiddenCols.add(new int[] + { 6, 9 }); + hiddenCols.add(new int[] + { 11, 12 }); + hiddenCols.add(new int[] + { 33, 33 }); + hiddenCols.add(new int[] + { 45, 50 }); + + int[] x = getStartEnd(new int[] + { 0, 50 }, hiddenCols); + // System.out.println("Export range : " + x[0] + " - " + x[1]); } /** @@ -1776,7 +1846,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener, } String output = new FormatAdapter().formatSequences("Fasta", seqs, - omitHidden); + omitHidden, null); StringSelection ss = new StringSelection(output); @@ -6001,11 +6071,13 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener, private String[] omitHidden; - public ExportData(AlignmentI align, String[] ommit) + private int[] startEnd; + + public ExportData(AlignmentI align, String[] ommit, int[] startEnd) { this.alignment = align; this.omitHidden = ommit; - System.out.println(); + this.startEnd = startEnd; } public AlignmentI getAlignment() @@ -6027,6 +6099,16 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener, { this.omitHidden = omitHidden; } + + public int[] getStartEndPostions() + { + return startEnd; + } + + public void setStartEndPostions(int[] startEnd) + { + this.startEnd = startEnd; + } } @Override diff --git a/src/jalview/gui/AnnotationLabels.java b/src/jalview/gui/AnnotationLabels.java index a544813..fad9dcd 100755 --- a/src/jalview/gui/AnnotationLabels.java +++ b/src/jalview/gui/AnnotationLabels.java @@ -875,7 +875,7 @@ public class AnnotationLabels extends JPanel implements MouseListener, } String output = new FormatAdapter().formatSequences("Fasta", seqs, - omitHidden); + omitHidden, null); Toolkit.getDefaultToolkit().getSystemClipboard() .setContents(new StringSelection(output), Desktop.instance); diff --git a/src/jalview/io/FormatAdapter.java b/src/jalview/io/FormatAdapter.java index 7c117b9..d241308 100755 --- a/src/jalview/io/FormatAdapter.java +++ b/src/jalview/io/FormatAdapter.java @@ -70,10 +70,11 @@ public class FormatAdapter extends AppletFormatAdapter } public String formatSequences(String format, SequenceI[] seqs, - String[] omitHiddenColumns) + String[] omitHiddenColumns, int[] exportRange) { - return formatSequences(format, replaceStrings(seqs, omitHiddenColumns)); + return formatSequences(format, + replaceStrings(seqs, omitHiddenColumns, exportRange)); } /** @@ -85,15 +86,48 @@ public class FormatAdapter extends AppletFormatAdapter * @return new sequences */ public SequenceI[] replaceStrings(SequenceI[] seqs, - String[] omitHiddenColumns) + String[] omitHiddenColumns, int[] startEnd) { if (omitHiddenColumns != null) { SequenceI[] tmp = new SequenceI[seqs.length]; + + int startRes; + int endRes; + int startIndex; + int endIndex; for (int i = 0; i < seqs.length; i++) { + startRes = seqs[i].getStart(); + endRes = seqs[i].getEnd(); + + startIndex = startEnd[0]; + endIndex = startEnd[1]; + + if (startEnd != null) + { + // get first non-gaped residue start position + while (jalview.util.Comparison.isGap(seqs[i] + .getCharAt(startIndex)) && startIndex < endIndex) + { + startIndex++; + } + + // get last non-gaped residue end position + while (jalview.util.Comparison.isGap(seqs[i].getCharAt(endIndex)) + && endIndex > startIndex) + { + endIndex--; + } + + startRes = seqs[i].findPosition(startIndex); + startRes = seqs[i].getStart() > 1 ? startRes - seqs[i].getStart() + : startRes; + endRes = seqs[i].findPosition(endIndex) - seqs[i].getStart(); + } + tmp[i] = new Sequence(seqs[i].getName(), omitHiddenColumns[i], - seqs[i].getStart(), seqs[i].getEnd()); + startRes, endRes); tmp[i].setDescription(seqs[i].getDescription()); } seqs = tmp; @@ -199,16 +233,17 @@ public class FormatAdapter extends AppletFormatAdapter } public String formatSequences(String format, AlignmentI alignment, - String[] omitHidden, ColumnSelection colSel) + String[] omitHidden, int[] exportRange, ColumnSelection colSel) { - return formatSequences(format, alignment, omitHidden, + return formatSequences(format, alignment, omitHidden, exportRange, getCacheSuffixDefault(format), colSel, null); } public String formatSequences(String format, AlignmentI alignment, - String[] omitHidden, ColumnSelection colSel, SequenceGroup sgp) + String[] omitHidden, int[] exportRange, ColumnSelection colSel, + SequenceGroup sgp) { - return formatSequences(format, alignment, omitHidden, + return formatSequences(format, alignment, omitHidden, exportRange, getCacheSuffixDefault(format), colSel, sgp); } @@ -225,14 +260,17 @@ public class FormatAdapter extends AppletFormatAdapter * @return string representation of the alignment formatted as format */ public String formatSequences(String format, AlignmentI alignment, - String[] omitHidden, boolean suffix, ColumnSelection colSel) + String[] omitHidden, int[] exportRange, boolean suffix, + ColumnSelection colSel) { - return formatSequences(format, alignment, omitHidden, suffix, colSel, + return formatSequences(format, alignment, omitHidden, exportRange, + suffix, colSel, null); } public String formatSequences(String format, AlignmentI alignment, - String[] omitHidden, boolean suffix, ColumnSelection colSel, + String[] omitHidden, int[] exportRange, boolean suffix, + ColumnSelection colSel, jalview.datamodel.SequenceGroup selgp) { if (omitHidden != null) @@ -242,7 +280,7 @@ public class FormatAdapter extends AppletFormatAdapter // TODO: JAL-1486 - set start and end for output correctly. basically, // AlignmentView.getVisibleContigs does this. Alignment alv = new Alignment(replaceStrings( - alignment.getSequencesArray(), omitHidden)); + alignment.getSequencesArray(), omitHidden, exportRange)); AlignmentAnnotation[] ala = alignment.getAlignmentAnnotation(); if (ala != null) { -- 1.7.10.2