From 889807fe8cb8d665036abbdebb4522d752a151eb Mon Sep 17 00:00:00 2001 From: jprocter Date: Thu, 9 Dec 2010 15:31:33 +0000 Subject: [PATCH] new constructor and refactored methods for intersecting selection with alignment when constructing CigarArray (JAL-716) --- src/jalview/datamodel/CigarArray.java | 127 +++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) diff --git a/src/jalview/datamodel/CigarArray.java b/src/jalview/datamodel/CigarArray.java index 626143e..3180298 100644 --- a/src/jalview/datamodel/CigarArray.java +++ b/src/jalview/datamodel/CigarArray.java @@ -17,6 +17,8 @@ */ package jalview.datamodel; +import java.util.Vector; + public class CigarArray extends CigarBase { /** @@ -72,6 +74,131 @@ public class CigarArray extends CigarBase } /** + * construct a cigar array from the current alignment, or just the subset of the current alignment specified by selectionGroup. Any columns marked as hidden in columnSelection will be marked as deleted in the array. + * @param alignment + * @param columnSelection + * @param selectionGroup + */ + public CigarArray(AlignmentI alignment, ColumnSelection columnSelection, SequenceGroup selectionGroup) + { + this(constructSeqCigarArray(alignment, selectionGroup)); + constructFromAlignment(alignment, columnSelection!=null ? columnSelection.getHiddenColumns() : null, selectionGroup); + } + private static int[] _calcStartEndBounds(AlignmentI alignment, SequenceGroup selectionGroup) + { + int[] startend = new int[] { 0,0,0}; + if (selectionGroup != null) + { + startend[0] = selectionGroup.getSize(); + startend[1] = selectionGroup.getStartRes(); + startend[2] = selectionGroup.getEndRes(); // inclusive for start and end in + // SeqCigar constructor + } + else + { + startend[0] = alignment.getHeight(); + startend[2] = alignment.getWidth() - 1; + } + return startend; + } + public static SeqCigar[] constructSeqCigarArray(AlignmentI alignment, SequenceGroup selectionGroup) + { + SequenceI[] seqs = null; + int i, iSize; + int _startend[] = _calcStartEndBounds(alignment, selectionGroup); + int start = _startend[1],end=_startend[2]; + if (selectionGroup != null) + { + iSize = selectionGroup.getSize(); + seqs = selectionGroup.getSequencesInOrder(alignment); + start = selectionGroup.getStartRes(); + end = selectionGroup.getEndRes(); // inclusive for start and end in + // SeqCigar constructor + } + else + { + iSize = alignment.getHeight(); + seqs = alignment.getSequencesArray(); + end = alignment.getWidth() - 1; + } + SeqCigar[] selseqs = new SeqCigar[iSize]; + for (i = 0; i < iSize; i++) + { + selseqs[i] = new SeqCigar(seqs[i], start, end); + } + return selseqs; + } + /** + * internal constructor function - called by CigarArray(AlignmentI, ...); + * @param alignment + * @param columnSelection - vector of visible regions as returned from columnSelection.getHiddenColumns() + * @param selectionGroup + */ + private void constructFromAlignment(AlignmentI alignment, Vector columnSelection, SequenceGroup selectionGroup) + { + int[] _startend = _calcStartEndBounds(alignment, selectionGroup); + int start = _startend[1],end=_startend[2]; + // now construct the CigarArray operations + if (columnSelection!=null) + { + int[] region; + int hideStart, hideEnd; + int last = start; + for (int j = 0; last < end & j < columnSelection.size(); j++) + { + region = (int[]) columnSelection.elementAt(j); + hideStart = region[0]; + hideEnd = region[1]; + // edit hidden regions to selection range + if (hideStart < last) + { + if (hideEnd > last) + { + hideStart = last; + } + else + { + continue; + } + } + + if (hideStart > end) + { + break; + } + + if (hideEnd > end) + { + hideEnd = end; + } + + if (hideStart > hideEnd) + { + break; + } + /** + * form operations... + */ + if (last < hideStart) + { + addOperation(CigarArray.M, hideStart - last); + } + addOperation(CigarArray.D, 1 + hideEnd - hideStart); + last = hideEnd + 1; + } + // Final match if necessary. + if (last < end) + { + addOperation(CigarArray.M, end - last + 1); + } + } + else + { + addOperation(CigarArray.M, end - start + 1); + } + } + + /** * @see Cigar.getSequenceAndDeletions * @param GapChar * char -- 1.7.10.2