X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fdatamodel%2FCigarArray.java;h=78d34edd07f0435c43ce9b1f541c0678ec35d5eb;hb=a45774ee31d9f35d4eff46d54d7deab719afb092;hp=9b60e566929ade7150328476a74cacf45f8557dd;hpb=174230b4233d9ce80f94527768d2cd2f76da11ab;p=jalview.git diff --git a/src/jalview/datamodel/CigarArray.java b/src/jalview/datamodel/CigarArray.java index 9b60e56..78d34ed 100644 --- a/src/jalview/datamodel/CigarArray.java +++ b/src/jalview/datamodel/CigarArray.java @@ -1,53 +1,62 @@ /* - * Jalview - A Sequence Alignment Editor and Viewer - * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7) + * Copyright (C) 2011 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle + * + * This file is part of Jalview. + * + * Jalview is free software: you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * + * Jalview is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with Jalview. If not, see . */ package jalview.datamodel; +import java.util.Vector; + public class CigarArray extends CigarBase { - /** - * Do CIGAR operations on a set of sequences from many other cigars - * BAD THINGS WILL HAPPEN IF A CIGARARRAY IS PASSED TO A CIGARARRAY - * or a CIGARCIGAR is given a CIGARARRAY to insert gaps into. - */ - /** - * array of subject cigars - */ - public CigarSimple refCigars[]=null; - private boolean seqcigararray=false; - private CigarArray() { + /** + * Do CIGAR operations on a set of sequences from many other cigars BAD THINGS + * WILL HAPPEN IF A CIGARARRAY IS PASSED TO A CIGARARRAY or a CIGARCIGAR is + * given a CIGARARRAY to insert gaps into. + */ + /** + * array of subject cigars + */ + public CigarSimple refCigars[] = null; + + private boolean seqcigararray = false; + + private CigarArray() + { super(); } /** * isSeqCigarArray() + * * @return boolean true if all refCigars resolve to a SeqCigar or a CigarCigar */ public boolean isSeqCigarArray() { return seqcigararray; } + /** - * Apply CIGAR operations to several cigars in parallel - * will throw an error if any of cigar are actually CigarArrays. - * @param cigar Cigar[] + * Apply CIGAR operations to several cigars in parallel will throw an error if + * any of cigar are actually CigarArrays. + * + * @param cigar + * Cigar[] */ - public CigarArray(CigarSimple[] cigars) { + public CigarArray(CigarSimple[] cigars) + { super(); seqcigararray = true; if (cigars != null && cigars.length > 0) @@ -56,151 +65,315 @@ public class CigarArray extends CigarBase for (int c = 0; c < cigars.length; c++) { refCigars[c] = cigars[c]; - if (! ( (cigars[c] instanceof SeqCigar) - || cigars[c] instanceof CigarCigar)) + if (!((cigars[c] instanceof SeqCigar) || cigars[c] instanceof CigarCigar)) { seqcigararray = false; } } } } + + /** + * 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 + * @param GapChar + * char * @return Object[][] */ - protected Object[][] getArrayofSequenceAndDeletions(char GapChar) { - if (refCigars == null || refCigars.length == 0 || length == 0) { - return null; + protected Object[][] getArrayofSequenceAndDeletions(char GapChar) + { + if (refCigars == null || refCigars.length == 0 || length == 0) + { + return null; + } + Object[][] sqanddels = new Object[refCigars.length][]; + for (int c = 0; c < refCigars.length; c++) + { + String refString = refCigars[c].getSequenceString(GapChar); + if (refString != null) + { + sqanddels[c] = getSequenceAndDeletions(refString, GapChar); } - Object[][] sqanddels = new Object[refCigars.length][]; - for (int c=0; c