- * Add gaps into the sequences aligned to profileseq under the given
- * AlignmentView
- *
- * @param profileseq
- * sequence in al which sequences are aligned to
- * @param al
- * alignment to have gaps inserted into it
- * @param input
- * alignment view where sequence corresponding to profileseq is first
- * entry
- * @return new HiddenColumns for new alignment view, with insertions into
- * profileseq marked as hidden.
- */
- public static HiddenColumns propagateInsertions(SequenceI profileseq,
- AlignmentI al, AlignmentView input)
- {
- int profsqpos = 0;
-
- char gc = al.getGapCharacter();
- Object[] alandhidden = input.getAlignmentAndHiddenColumns(gc);
- HiddenColumns nview = (HiddenColumns) alandhidden[1];
- SequenceI origseq = ((SequenceI[]) alandhidden[0])[profsqpos];
- nview.propagateInsertions(profileseq, al, origseq);
- return nview;
- }
-
- /**
- *
- * @param profileseq
- * sequence in al which corresponds to origseq
- * @param al
- * alignment which is to have gaps inserted into it
- * @param origseq
- * sequence corresponding to profileseq which defines gap map for
- * modifying al
- */
- private void propagateInsertions(SequenceI profileseq, AlignmentI al,
- SequenceI origseq)
- {
- try
- {
- LOCK.writeLock().lock();
-
- char gc = al.getGapCharacter();
-
- // take the set of hidden columns, and the set of gaps in origseq,
- // and remove all the hidden gaps from hiddenColumns
-
- // first get the gaps as a Bitset
- BitSet gaps = origseq.gapBitset();
-
- // now calculate hidden ^ not(gap)
- BitSet hidden = new BitSet();
- markHiddenRegions(hidden);
- hidden.andNot(gaps);
- hiddenColumns = null;
- this.hideMarkedBits(hidden);
-
- // for each sequence in the alignment, except the profile sequence,
- // insert gaps corresponding to each hidden region but where each hidden
- // column region is shifted backwards by the number of preceding visible
- // gaps update hidden columns at the same time
- Iterator<int[]> regions = hiddenColumns.iterator();
- ArrayList<int[]> newhidden = new ArrayList<>();
-
- int numGapsBefore = 0;
- int gapPosition = 0;
- while (regions.hasNext())
- {
- // get region coordinates accounting for gaps
- // we can rely on gaps not being *in* hidden regions because we already
- // removed those
- int[] region = regions.next();
- while (gapPosition < region[0])
- {
- gapPosition++;
- if (gaps.get(gapPosition))
- {
- numGapsBefore++;
- }
- }
-
- int left = region[0] - numGapsBefore;
- int right = region[1] - numGapsBefore;
- newhidden.add(new int[] { left, right });
-
- // make a string with number of gaps = length of hidden region
- StringBuffer sb = new StringBuffer();
- for (int s = 0; s < right - left + 1; s++)
- {
- sb.append(gc);
- }
- padGaps(sb, left, profileseq, al);
-
- }
- hiddenColumns = newhidden;
- cursor.resetCursor(hiddenColumns);
- numColumns = 0;
- } finally
- {
- LOCK.writeLock().unlock();
- }
- }
-
- /**
- * Pad gaps in all sequences in alignment except profileseq
- *
- * @param sb
- * gap string to insert
- * @param left
- * position to insert at
- * @param profileseq
- * sequence not to pad
- * @param al
- * alignment to pad sequences in
- */
- private void padGaps(StringBuffer sb, int pos, SequenceI profileseq,
- AlignmentI al)
- {
- // loop over the sequences and pad with gaps where required
- for (int s = 0, ns = al.getHeight(); s < ns; s++)
- {
- SequenceI sqobj = al.getSequenceAt(s);
- if (sqobj != profileseq)
- {
- String sq = al.getSequenceAt(s).getSequenceAsString();
- if (sq.length() <= pos)
- {
- // pad sequence
- int diff = pos - sq.length() - 1;
- if (diff > 0)
- {
- // pad gaps
- sq = sq + sb;
- while ((diff = pos - sq.length() - 1) > 0)
- {
- if (diff >= sb.length())
- {
- sq += sb.toString();
- }
- else
- {
- char[] buf = new char[diff];
- sb.getChars(0, diff, buf, 0);
- sq += buf.toString();
- }
- }
- }
- sq += sb.toString();
- }
- else
- {
- al.getSequenceAt(s).setSequence(
- sq.substring(0, pos) + sb.toString() + sq.substring(pos));
- }
- }
- }
- }
-
- /*
- * Methods which only need read access to the hidden columns collection.
- * These methods should use a readLock to prevent other threads changing
- * the hidden columns collection while it is in use.
- */
-
- /**