-package jalview.datamodel;
-
-/**
- * <p>Title: </p>
- *
- * <p>Description: </p>
- *
- * <p>Copyright: Copyright (c) 2004</p>
- *
- * <p>Company: Dundee University</p>
- *
- * @author not attributable
- * @version 1.0
- */
-public class AlignmentView
-{
- /**
- * Transient object compactly representing a 'view' of an alignment - with discontinuities marked.
- */
- private SeqCigar[] sequences = null;
- private int[] contigs = null;
- private int width=0;
- public AlignmentView(CigarArray seqcigararray)
- {
- if (!seqcigararray.isSeqCigarArray())
- throw new Error("Implementation Error - can only make an alignment view from a CigarArray of sequences.");
- //contigs = seqcigararray.applyDeletions();
- contigs = seqcigararray.getDeletedRegions();
- sequences = seqcigararray.getSeqCigarArray();
- width = seqcigararray.getWidth(); // visible width
- }
-
- public void setSequences(SeqCigar[] sequences)
- {
- this.sequences = sequences;
- }
-
- public void setContigs(int[] contigs)
- {
- this.contigs = contigs;
- }
-
- public SeqCigar[] getSequences()
- {
- return sequences;
- }
-
- public int[] getContigs()
- {
- return contigs;
- }
- public Object[] getAlignmentAndColumnSelection(char gapCharacter) {
- ColumnSelection colsel = new ColumnSelection();
-
- return new Object[] { SeqCigar.createAlignmentSequences(sequences, gapCharacter, colsel, contigs), colsel};
- }
- /**
- * getSequenceStrings
- *
- * @param c char
- * @return String[]
- */
- public String[] getSequenceStrings(char c)
- {
- String[] seqs=new String[sequences.length];
- for (int n=0; n<sequences.length; n++) {
- seqs[n] = sequences[n].getSequenceString(c);
- }
- return seqs;
- }
- /**
- *
- * @return visible number of columns in alignment view
- */
- public int getWidth() {
- return width;
- }
-
- protected void setWidth(int width) {
- this.width = width;
- }
-
-}
+/*\r
+ * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)\r
+ * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle\r
+ * \r
+ * This file is part of Jalview.\r
+ * \r
+ * Jalview is free software: you can redistribute it and/or\r
+ * modify it under the terms of the GNU General Public License \r
+ * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.\r
+ * \r
+ * Jalview is distributed in the hope that it will be useful, but \r
+ * WITHOUT ANY WARRANTY; without even the implied warranty \r
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR \r
+ * PURPOSE. See the GNU General Public License for more details.\r
+ * \r
+ * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.\r
+ */\r
+package jalview.datamodel;\r
+\r
+import jalview.util.ShiftList;\r
+\r
+import java.io.PrintStream;\r
+import java.util.Enumeration;\r
+import java.util.Vector;\r
+\r
+/**\r
+ * Transient object compactly representing a 'view' of an alignment - with\r
+ * discontinuities marked. Extended in Jalview 2.7 to optionally record sequence\r
+ * groups and specific selected regions on the alignment.\r
+ */\r
+public class AlignmentView\r
+{\r
+ private SeqCigar[] sequences = null;\r
+\r
+ private int[] contigs = null;\r
+\r
+ private int width = 0;\r
+\r
+ private int firstCol = 0;\r
+\r
+ /**\r
+ * one or more ScGroup objects, which are referenced by each seqCigar's group\r
+ * membership\r
+ */\r
+ private Vector scGroups;\r
+\r
+ /**\r
+ * Group defined over SeqCigars. Unlike AlignmentI associated groups, each\r
+ * SequenceGroup hold just the essential properties for the group, but no\r
+ * references to the sequences involved. SeqCigars hold references to the\r
+ * seuqenceGroup entities themselves.\r
+ */\r
+ private class ScGroup\r
+ {\r
+ public Vector seqs;\r
+\r
+ public SequenceGroup sg;\r
+\r
+ ScGroup()\r
+ {\r
+ seqs = new Vector();\r
+ }\r
+ }\r
+\r
+ /**\r
+ * vector of selected seqCigars. This vector is also referenced by each\r
+ * seqCigar contained in it.\r
+ */\r
+ private Vector selected;\r
+\r
+ /**\r
+ * Construct an alignmentView from a live jalview alignment view. Note -\r
+ * hidden rows will be excluded from alignmentView\r
+ * \r
+ * @param alignment\r
+ * - alignment as referenced by an AlignViewport\r
+ * @param columnSelection\r
+ * -\r
+ * @param selection\r
+ * @param hasHiddenColumns\r
+ * - mark the hidden columns in columnSelection as hidden in the view\r
+ * @param selectedRegionOnly\r
+ * - when set, only include the selected region in the view,\r
+ * otherwise just mark the selected region on the constructed view.\r
+ * @param recordGroups\r
+ * - when set, any groups on the given alignment will be marked on\r
+ * the view\r
+ */\r
+ public AlignmentView(AlignmentI alignment,\r
+ ColumnSelection columnSelection, SequenceGroup selection,\r
+ boolean hasHiddenColumns, boolean selectedRegionOnly,\r
+ boolean recordGroups)\r
+ {\r
+ // refactored from AlignViewport.getAlignmentView(selectedOnly);\r
+ this(new jalview.datamodel.CigarArray(alignment,\r
+ (hasHiddenColumns ? columnSelection : null),\r
+ (selectedRegionOnly ? selection : null)),\r
+ (selectedRegionOnly && selection != null) ? selection\r
+ .getStartRes() : 0);\r
+ // walk down SeqCigar array and Alignment Array - optionally restricted by\r
+ // selected region.\r
+ // test group membership for each sequence in each group, store membership\r
+ // and record non-empty groups in group list.\r
+ // record / sub-select selected region on the alignment view\r
+ SequenceI[] selseqs;\r
+ if (selection != null)\r
+ {\r
+ Vector sel = selection.getSequences(null);\r
+ this.selected = new Vector();\r
+ selseqs = selection.getSequencesInOrder(alignment, selectedRegionOnly);\r
+ }\r
+ else\r
+ {\r
+ selseqs = alignment.getSequencesArray();\r
+ }\r
+\r
+ // get the alignment's group list and make a copy\r
+ Vector grps = new Vector();\r
+ Vector gg = alignment.getGroups();\r
+ Enumeration gge = gg.elements();\r
+ while (gge.hasMoreElements())\r
+ {\r
+ grps.addElement(gge.nextElement());\r
+ }\r
+ ScGroup[] sgrps = null;\r
+ boolean addedgps[] = null;\r
+ if (grps != null)\r
+ {\r
+ SequenceGroup sg;\r
+ if (selection != null && selectedRegionOnly)\r
+ {\r
+ // trim annotation to the region being stored.\r
+ // strip out any groups that do not actually intersect with the\r
+ // visible and selected region\r
+ int ssel = selection.getStartRes(), esel = selection.getEndRes();\r
+ Vector isg = new Vector();\r
+ Enumeration en = grps.elements();\r
+ while (en.hasMoreElements())\r
+ {\r
+ sg = (SequenceGroup) en.nextElement();\r
+\r
+ if (!(sg.getStartRes() > esel || sg.getEndRes() < ssel))\r
+ {\r
+ // adjust bounds of new group, if necessary.\r
+ if (sg.getStartRes() < ssel)\r
+ {\r
+ sg.setStartRes(ssel);\r
+ }\r
+ if (sg.getEndRes() > esel)\r
+ {\r
+ sg.setEndRes(esel);\r
+ }\r
+ sg.setStartRes(sg.getStartRes()-ssel+1);\r
+ sg.setEndRes(sg.getEndRes()-ssel+1);\r
+ \r
+ isg.addElement(sg);\r
+ }\r
+ }\r
+ grps = isg;\r
+ }\r
+\r
+ sgrps = new ScGroup[grps.size()];\r
+ addedgps = new boolean[grps.size()];\r
+ for (int g = 0; g < sgrps.length; g++)\r
+ {\r
+ sg = (SequenceGroup) grps.elementAt(g);\r
+ sgrps[g] = new ScGroup();\r
+ sgrps[g].sg = new SequenceGroup(sg);\r
+ addedgps[g] = false;\r
+ grps.setElementAt(sg.getSequences(null), g);\r
+ }\r
+ // grps now contains vectors (should be sets) for each group, so we can\r
+ // track when we've done with the group\r
+ }\r
+ int csi = 0;\r
+ for (int i = 0; i < selseqs.length; i++)\r
+ {\r
+ if (selseqs[i] != null)\r
+ {\r
+ if (selection != null && !selectedRegionOnly)\r
+ {\r
+ sequences[csi].setGroupMembership(selected);\r
+ selected.addElement(sequences[csi]);\r
+ }\r
+ if (grps != null)\r
+ {\r
+ for (int sg = 0; sg < sgrps.length; sg++)\r
+ {\r
+ if (((Vector) grps.elementAt(sg)).contains(selseqs[i]))\r
+ {\r
+ sequences[csi].setGroupMembership(sgrps[sg]);\r
+ sgrps[sg].sg.deleteSequence(selseqs[i], false);\r
+ sgrps[sg].seqs.addElement(sequences[csi]);\r
+ if (!addedgps[sg])\r
+ {\r
+ if (scGroups == null)\r
+ {\r
+ scGroups = new Vector();\r
+ }\r
+ addedgps[sg] = true;\r
+ scGroups.addElement(sgrps[sg]);\r
+ }\r
+ }\r
+ }\r
+ }\r
+ csi++;\r
+ }\r
+ }\r
+ // finally, delete the remaining sequences (if any) not selected\r
+ for (int sg = 0; sg < sgrps.length; sg++)\r
+ {\r
+ SequenceI[] sqs = sgrps[sg].sg.getSequencesAsArray(null);\r
+ for (int si = 0; si < sqs.length; si++)\r
+ {\r
+ sgrps[sg].sg.deleteSequence(sqs[si], false);\r
+ }\r
+ sgrps[sg] = null;\r
+ }\r
+ }\r
+\r
+ /**\r
+ * construct an alignmentView from a SeqCigarArray. Errors are thrown if the\r
+ * seqcigararray.isSeqCigarArray() flag is not set.\r
+ */\r
+ public AlignmentView(CigarArray seqcigararray)\r
+ {\r
+ if (!seqcigararray.isSeqCigarArray())\r
+ {\r
+ throw new Error(\r
+ "Implementation Error - can only make an alignment view from a CigarArray of sequences.");\r
+ }\r
+ // contigs = seqcigararray.applyDeletions();\r
+ contigs = seqcigararray.getDeletedRegions();\r
+ sequences = seqcigararray.getSeqCigarArray();\r
+ width = seqcigararray.getWidth(); // visible width\r
+ }\r
+\r
+ /**\r
+ * Create an alignmentView where the first column corresponds with the\r
+ * 'firstcol' column of some reference alignment\r
+ * \r
+ * @param sdata\r
+ * @param firstcol\r
+ */\r
+ public AlignmentView(CigarArray sdata, int firstcol)\r
+ {\r
+ this(sdata);\r
+ firstCol = firstcol;\r
+ }\r
+\r
+ public void setSequences(SeqCigar[] sequences)\r
+ {\r
+ this.sequences = sequences;\r
+ }\r
+\r
+ public void setContigs(int[] contigs)\r
+ {\r
+ this.contigs = contigs;\r
+ }\r
+\r
+ public SeqCigar[] getSequences()\r
+ {\r
+ return sequences;\r
+ }\r
+\r
+ /**\r
+ * @see CigarArray.getDeletedRegions\r
+ * @return int[] { vis_start, sym_start, length }\r
+ */\r
+ public int[] getContigs()\r
+ {\r
+ return contigs;\r
+ }\r
+\r
+ /**\r
+ * get the full alignment and a columnselection object marking the hidden\r
+ * regions\r
+ * \r
+ * @param gapCharacter\r
+ * char\r
+ * @return Object[] { SequenceI[], ColumnSelection}\r
+ */\r
+ public Object[] getAlignmentAndColumnSelection(char gapCharacter)\r
+ {\r
+ ColumnSelection colsel = new ColumnSelection();\r
+\r
+ return new Object[]\r
+ {\r
+ SeqCigar.createAlignmentSequences(sequences, gapCharacter, colsel,\r
+ contigs), colsel };\r
+ }\r
+\r
+ /**\r
+ * return the visible alignment corresponding to this view. Sequences in this\r
+ * alignment are edited versions of the parent sequences - where hidden\r
+ * regions have been removed. NOTE: the sequence data in this alignment is not\r
+ * complete!\r
+ * \r
+ * @param c\r
+ * @return\r
+ */\r
+ public AlignmentI getVisibleAlignment(char c)\r
+ {\r
+ SequenceI[] aln = getVisibleSeqs(c);\r
+\r
+ AlignmentI vcal = new Alignment(aln);\r
+ addPrunedGroupsInOrder(vcal, -1, -1, true);\r
+ return vcal;\r
+ }\r
+\r
+ /**\r
+ * add groups from view to the given alignment\r
+ * \r
+ * @param vcal\r
+ * @param gstart\r
+ * -1 or 0 to width-1\r
+ * @param gend\r
+ * -1 or gstart to width-1\r
+ * @param viscontigs\r
+ * - true if vcal is alignment of the visible regions of the view\r
+ * (e.g. as returned from getVisibleAlignment)\r
+ */\r
+ private void addPrunedGroupsInOrder(AlignmentI vcal, int gstart,\r
+ int gend, boolean viscontigs)\r
+ {\r
+ boolean r = false;\r
+ if (gstart > -1 && gstart <= gend)\r
+ {\r
+ r = true;\r
+ }\r
+\r
+ SequenceI[] aln = vcal.getSequencesArray();\r
+ {\r
+ /**\r
+ * prune any groups to the visible coordinates of the alignment.\r
+ */\r
+ {\r
+ int nvg = (scGroups != null) ? scGroups.size() : 0;\r
+ if (nvg > 0)\r
+ {\r
+ SequenceGroup[] nsg = new SequenceGroup[nvg];\r
+ for (int g = 0; g < nvg; g++)\r
+ {\r
+ SequenceGroup sg = ((ScGroup) scGroups.elementAt(g)).sg;\r
+ if (r)\r
+ {\r
+ if (sg.getStartRes() > gend || sg.getEndRes() < gstart)\r
+ {\r
+ // Skip this group\r
+ nsg[g] = null;\r
+ continue;\r
+ }\r
+ }\r
+\r
+ // clone group properties\r
+ nsg[g] = new SequenceGroup(sg);\r
+\r
+ // may need to shift/trim start and end ?\r
+ if (r && !viscontigs)\r
+ {\r
+ // Not fully tested code - routine not yet called with viscontigs==false\r
+ if (nsg[g].getStartRes() < gstart)\r
+ {\r
+ nsg[g].setStartRes(0);\r
+ }\r
+ else\r
+ {\r
+ nsg[g].setStartRes(nsg[g].getStartRes() - gstart);\r
+ nsg[g].setEndRes(nsg[g].getEndRes() - gstart);\r
+ }\r
+ if (nsg[g].getEndRes() > (gend-gstart))\r
+ {\r
+ nsg[g].setEndRes(gend-gstart);\r
+ }\r
+ }\r
+ }\r
+ if (viscontigs)\r
+ {\r
+ // prune groups to cover just the visible positions between\r
+ // gstart/gend.\r
+ if (contigs != null)\r
+ {\r
+ int p = 0;\r
+ ShiftList prune = new ShiftList();\r
+ if (r)\r
+ {\r
+ // adjust for start of alignment within visible window.\r
+ prune.addShift(gstart, -gstart); //\r
+ }\r
+ for (int h = 0; h < contigs.length; h += 3)\r
+ {\r
+ {\r
+ prune.addShift(p + contigs[h + 1], contigs[h + 2]\r
+ - contigs[h + 1]);\r
+ }\r
+ p = contigs[h + 1] + contigs[h + 2];\r
+ }\r
+ for (int g = 0; g < nsg.length; g++)\r
+ {\r
+ if (nsg[g] != null)\r
+ {\r
+ int s = nsg[g].getStartRes(), t = nsg[g].getEndRes();\r
+ int w = 1 + t - s;\r
+ if (r)\r
+ {\r
+ if (s < gstart)\r
+ {\r
+ s = gstart;\r
+ }\r
+ if (t > gend)\r
+ {\r
+ t = gend;\r
+ }\r
+ }\r
+ s = prune.shift(s);\r
+ t = prune.shift(t);\r
+ nsg[g].setStartRes(s);\r
+ nsg[g].setEndRes(t);\r
+ }\r
+ }\r
+ }\r
+ }\r
+\r
+ for (int nsq = 0; nsq < aln.length; nsq++)\r
+ {\r
+ for (int g = 0; g < nvg; g++)\r
+ {\r
+ if (nsg[g] != null\r
+ && sequences[nsq].isMemberOf(scGroups.elementAt(g)))\r
+ {\r
+ nsg[g].addSequence(aln[nsq], false);\r
+ }\r
+ }\r
+ }\r
+ for (int g = 0; g < nvg; g++)\r
+ {\r
+ if (nsg[g] != null && nsg[g].getSize() > 0)\r
+ {\r
+ vcal.addGroup(nsg[g]);\r
+ }\r
+ nsg[g] = null;\r
+ }\r
+ }\r
+ }\r
+ }\r
+ }\r
+\r
+ /**\r
+ * generate sequence array corresponding to the visible parts of the\r
+ * alignment.\r
+ * \r
+ * @param c\r
+ * @return\r
+ */\r
+ private SequenceI[] getVisibleSeqs(char c)\r
+ {\r
+ SequenceI[] aln = new SequenceI[sequences.length];\r
+ for (int i = 0, j = sequences.length; i < j; i++)\r
+ {\r
+ aln[i] = sequences[i].getSeq('-');\r
+ }\r
+ // Remove hidden regions from sequence objects.\r
+ String seqs[] = getSequenceStrings('-');\r
+ for (int i = 0, j = aln.length; i < j; i++)\r
+ {\r
+ aln[i].setSequence(seqs[i]);\r
+ }\r
+ return aln;\r
+ }\r
+\r
+ /**\r
+ * creates new alignment objects for all contiguous visible segments\r
+ * \r
+ * @param c\r
+ * @param start\r
+ * @param end\r
+ * @param regionOfInterest\r
+ * specify which sequences to include (or null to include all\r
+ * sequences)\r
+ * @return AlignmentI[] - all alignments where each sequence is a subsequence\r
+ * constructed from visible contig regions of view\r
+ */\r
+ public AlignmentI[] getVisibleContigAlignments(char c)\r
+ {\r
+ int nvc = 0;\r
+ int[] vcontigs = getVisibleContigs();\r
+ SequenceI[][] contigviews = getVisibleContigs(c);\r
+ AlignmentI[] vcals = new AlignmentI[contigviews.length];\r
+ for (nvc = 0; nvc < contigviews.length; nvc++)\r
+ {\r
+ vcals[nvc] = new Alignment(contigviews[nvc]);\r
+ if (scGroups!=null && scGroups.size()>0)\r
+ {\r
+ addPrunedGroupsInOrder(vcals[nvc], vcontigs[nvc*2],vcontigs[nvc*2+1], true);\r
+ }\r
+ }\r
+ return vcals;\r
+ }\r
+\r
+\r
+ /**\r
+ * get an array of visible sequence strings for a view on an alignment using\r
+ * the given gap character\r
+ * \r
+ * @param c\r
+ * char\r
+ * @return String[]\r
+ */\r
+ public String[] getSequenceStrings(char c)\r
+ {\r
+ String[] seqs = new String[sequences.length];\r
+ for (int n = 0; n < sequences.length; n++)\r
+ {\r
+ String fullseq = sequences[n].getSequenceString(c);\r
+ if (contigs != null)\r
+ {\r
+ seqs[n] = "";\r
+ int p = 0;\r
+ for (int h = 0; h < contigs.length; h += 3)\r
+ {\r
+ seqs[n] += fullseq.substring(p, contigs[h + 1]);\r
+ p = contigs[h + 1] + contigs[h + 2];\r
+ }\r
+ seqs[n] += fullseq.substring(p);\r
+ }\r
+ else\r
+ {\r
+ seqs[n] = fullseq;\r
+ }\r
+ }\r
+ return seqs;\r
+ }\r
+\r
+ /**\r
+ * \r
+ * @return visible number of columns in alignment view\r
+ */\r
+ public int getWidth()\r
+ {\r
+ return width;\r
+ }\r
+\r
+ protected void setWidth(int width)\r
+ {\r
+ this.width = width;\r
+ }\r
+\r
+ /**\r
+ * get the contiguous subalignments in an alignment view.\r
+ * \r
+ * @param gapCharacter\r
+ * char\r
+ * @return SequenceI[][]\r
+ */\r
+ public SequenceI[][] getVisibleContigs(char gapCharacter)\r
+ {\r
+ SequenceI[][] smsa;\r
+ int njobs = 1;\r
+ if (sequences == null || width <= 0)\r
+ {\r
+ return null;\r
+ }\r
+ if (contigs != null && contigs.length > 0)\r
+ {\r
+ int start = 0;\r
+ njobs = 0;\r
+ int fwidth = width;\r
+ for (int contig = 0; contig < contigs.length; contig += 3)\r
+ {\r
+ if ((contigs[contig + 1] - start) > 0)\r
+ {\r
+ njobs++;\r
+ }\r
+ fwidth += contigs[contig + 2]; // end up with full region width\r
+ // (including hidden regions)\r
+ start = contigs[contig + 1] + contigs[contig + 2];\r
+ }\r
+ if (start < fwidth)\r
+ {\r
+ njobs++;\r
+ }\r
+ smsa = new SequenceI[njobs][];\r
+ start = 0;\r
+ int j = 0;\r
+ for (int contig = 0; contig < contigs.length; contig += 3)\r
+ {\r
+ if (contigs[contig + 1] - start > 0)\r
+ {\r
+ SequenceI mseq[] = new SequenceI[sequences.length];\r
+ for (int s = 0; s < mseq.length; s++)\r
+ {\r
+ mseq[s] = sequences[s].getSeq(gapCharacter).getSubSequence(\r
+ start, contigs[contig + 1]);\r
+ }\r
+ smsa[j] = mseq;\r
+ j++;\r
+ }\r
+ start = contigs[contig + 1] + contigs[contig + 2];\r
+ }\r
+ if (start < fwidth)\r
+ {\r
+ SequenceI mseq[] = new SequenceI[sequences.length];\r
+ for (int s = 0; s < mseq.length; s++)\r
+ {\r
+ mseq[s] = sequences[s].getSeq(gapCharacter).getSubSequence(start,\r
+ fwidth + 1);\r
+ }\r
+ smsa[j] = mseq;\r
+ j++;\r
+ }\r
+ }\r
+ else\r
+ {\r
+ smsa = new SequenceI[1][];\r
+ smsa[0] = new SequenceI[sequences.length];\r
+ for (int s = 0; s < sequences.length; s++)\r
+ {\r
+ smsa[0][s] = sequences[s].getSeq(gapCharacter);\r
+ }\r
+ }\r
+ return smsa;\r
+ }\r
+\r
+ /**\r
+ * return full msa and hidden regions with visible blocks replaced with new\r
+ * sub alignments\r
+ * \r
+ * @param nvismsa\r
+ * SequenceI[][]\r
+ * @param orders\r
+ * AlignmentOrder[] corresponding to each SequenceI[] block.\r
+ * @return Object[]\r
+ */\r
+ public Object[] getUpdatedView(SequenceI[][] nvismsa,\r
+ AlignmentOrder[] orders, char gapCharacter)\r
+ {\r
+ if (sequences == null || width <= 0)\r
+ {\r
+ throw new Error("empty view cannot be updated.");\r
+ }\r
+ if (nvismsa == null)\r
+ {\r
+ throw new Error(\r
+ "nvismsa==null. use getAlignmentAndColumnSelection() instead.");\r
+ }\r
+ if (contigs != null && contigs.length > 0)\r
+ {\r
+ SequenceI[] alignment = new SequenceI[sequences.length];\r
+ ColumnSelection columnselection = new ColumnSelection();\r
+ if (contigs != null && contigs.length > 0)\r
+ {\r
+ int start = 0;\r
+ int nwidth = 0;\r
+ int owidth = width;\r
+ int j = 0;\r
+ for (int contig = 0; contig < contigs.length; contig += 3)\r
+ {\r
+ owidth += contigs[contig + 2]; // recover final column width\r
+ if (contigs[contig + 1] - start > 0)\r
+ {\r
+ int swidth = 0; // subalignment width\r
+ if (nvismsa[j] != null)\r
+ {\r
+ SequenceI mseq[] = nvismsa[j];\r
+ AlignmentOrder order = (orders == null) ? null : orders[j];\r
+ j++;\r
+ if (mseq.length != sequences.length)\r
+ {\r
+ throw new Error(\r
+ "Mismatch between number of sequences in block "\r
+ + j + " (" + mseq.length\r
+ + ") and the original view ("\r
+ + sequences.length + ")");\r
+ }\r
+ swidth = mseq[0].getLength(); // JBPNote: could ensure padded\r
+ // here.\r
+ for (int s = 0; s < mseq.length; s++)\r
+ {\r
+ if (alignment[s] == null)\r
+ {\r
+ alignment[s] = mseq[s];\r
+ }\r
+ else\r
+ {\r
+ alignment[s].setSequence(alignment[s]\r
+ .getSequenceAsString()\r
+ + mseq[s].getSequenceAsString());\r
+ if (mseq[s].getStart() <= mseq[s].getEnd())\r
+ {\r
+ alignment[s].setEnd(mseq[s].getEnd());\r
+ }\r
+ if (order != null)\r
+ {\r
+ order.updateSequence(mseq[s], alignment[s]);\r
+ }\r
+ }\r
+ }\r
+ }\r
+ else\r
+ {\r
+ // recover original alignment block or place gaps\r
+ if (true)\r
+ {\r
+ // recover input data\r
+ for (int s = 0; s < sequences.length; s++)\r
+ {\r
+ SequenceI oseq = sequences[s].getSeq(gapCharacter)\r
+ .getSubSequence(start, contigs[contig + 1]);\r
+ if (swidth < oseq.getLength())\r
+ {\r
+ swidth = oseq.getLength();\r
+ }\r
+ if (alignment[s] == null)\r
+ {\r
+ alignment[s] = oseq;\r
+ }\r
+ else\r
+ {\r
+ alignment[s].setSequence(alignment[s]\r
+ .getSequenceAsString()\r
+ + oseq.getSequenceAsString());\r
+ if (oseq.getEnd() >= oseq.getStart())\r
+ {\r
+ alignment[s].setEnd(oseq.getEnd());\r
+ }\r
+ }\r
+ }\r
+\r
+ }\r
+ j++;\r
+ }\r
+ nwidth += swidth;\r
+ }\r
+ // advance to begining of visible region\r
+ start = contigs[contig + 1] + contigs[contig + 2];\r
+ // add hidden segment to right of next region\r
+ for (int s = 0; s < sequences.length; s++)\r
+ {\r
+ SequenceI hseq = sequences[s].getSeq(gapCharacter)\r
+ .getSubSequence(contigs[contig + 1], start);\r
+ if (alignment[s] == null)\r
+ {\r
+ alignment[s] = hseq;\r
+ }\r
+ else\r
+ {\r
+ alignment[s].setSequence(alignment[s].getSequenceAsString()\r
+ + hseq.getSequenceAsString());\r
+ if (hseq.getEnd() >= hseq.getStart())\r
+ {\r
+ alignment[s].setEnd(hseq.getEnd());\r
+ }\r
+ }\r
+ }\r
+ // mark hidden segment as hidden in the new alignment\r
+ columnselection.hideColumns(nwidth, nwidth + contigs[contig + 2]\r
+ - 1);\r
+ nwidth += contigs[contig + 2];\r
+ }\r
+ // Do final segment - if it exists\r
+ if (j < nvismsa.length)\r
+ {\r
+ int swidth = 0;\r
+ if (nvismsa[j] != null)\r
+ {\r
+ SequenceI mseq[] = nvismsa[j];\r
+ AlignmentOrder order = (orders != null) ? orders[j] : null;\r
+ swidth = mseq[0].getLength();\r
+ for (int s = 0; s < mseq.length; s++)\r
+ {\r
+ if (alignment[s] == null)\r
+ {\r
+ alignment[s] = mseq[s];\r
+ }\r
+ else\r
+ {\r
+ alignment[s].setSequence(alignment[s].getSequenceAsString()\r
+ + mseq[s].getSequenceAsString());\r
+ if (mseq[s].getEnd() >= mseq[s].getStart())\r
+ {\r
+ alignment[s].setEnd(mseq[s].getEnd());\r
+ }\r
+ if (order != null)\r
+ {\r
+ order.updateSequence(mseq[s], alignment[s]);\r
+ }\r
+ }\r
+ }\r
+ }\r
+ else\r
+ {\r
+ if (start < owidth)\r
+ {\r
+ // recover input data or place gaps\r
+ if (true)\r
+ {\r
+ // recover input data\r
+ for (int s = 0; s < sequences.length; s++)\r
+ {\r
+ SequenceI oseq = sequences[s].getSeq(gapCharacter)\r
+ .getSubSequence(start, owidth + 1);\r
+ if (swidth < oseq.getLength())\r
+ {\r
+ swidth = oseq.getLength();\r
+ }\r
+ if (alignment[s] == null)\r
+ {\r
+ alignment[s] = oseq;\r
+ }\r
+ else\r
+ {\r
+ alignment[s].setSequence(alignment[s]\r
+ .getSequenceAsString()\r
+ + oseq.getSequenceAsString());\r
+ if (oseq.getEnd() >= oseq.getStart())\r
+ {\r
+ alignment[s].setEnd(oseq.getEnd());\r
+ }\r
+ }\r
+ }\r
+ nwidth += swidth;\r
+ }\r
+ else\r
+ {\r
+ // place gaps.\r
+ throw new Error("Padding not yet implemented.");\r
+ }\r
+ }\r
+ }\r
+ }\r
+ }\r
+ return new Object[]\r
+ { alignment, columnselection };\r
+ }\r
+ else\r
+ {\r
+ if (nvismsa.length != 1)\r
+ {\r
+ throw new Error(\r
+ "Mismatch between visible blocks to update and number of contigs in view (contigs=0,blocks="\r
+ + nvismsa.length);\r
+ }\r
+ if (nvismsa[0] != null)\r
+ {\r
+ return new Object[]\r
+ { nvismsa[0], new ColumnSelection() };\r
+ }\r
+ else\r
+ {\r
+ return getAlignmentAndColumnSelection(gapCharacter);\r
+ }\r
+ }\r
+ }\r
+\r
+ /**\r
+ * returns simple array of start end positions of visible range on alignment.\r
+ * vis_start and vis_end are inclusive - use\r
+ * SequenceI.getSubSequence(vis_start, vis_end+1) to recover visible sequence\r
+ * from underlying alignment.\r
+ * \r
+ * @return int[] { start_i, end_i } for 1<i<n visible regions.\r
+ */\r
+ public int[] getVisibleContigs()\r
+ {\r
+ if (contigs != null && contigs.length > 0)\r
+ {\r
+ int start = 0;\r
+ int nvis = 0;\r
+ int fwidth = width;\r
+ for (int contig = 0; contig < contigs.length; contig += 3)\r
+ {\r
+ if ((contigs[contig + 1] - start) > 0)\r
+ {\r
+ nvis++;\r
+ }\r
+ fwidth += contigs[contig + 2]; // end up with full region width\r
+ // (including hidden regions)\r
+ start = contigs[contig + 1] + contigs[contig + 2];\r
+ }\r
+ if (start < fwidth)\r
+ {\r
+ nvis++;\r
+ }\r
+ int viscontigs[] = new int[nvis * 2];\r
+ nvis = 0;\r
+ start = 0;\r
+ for (int contig = 0; contig < contigs.length; contig += 3)\r
+ {\r
+ if ((contigs[contig + 1] - start) > 0)\r
+ {\r
+ viscontigs[nvis] = start;\r
+ viscontigs[nvis + 1] = contigs[contig + 1] - 1; // end is inclusive\r
+ nvis += 2;\r
+ }\r
+ start = contigs[contig + 1] + contigs[contig + 2];\r
+ }\r
+ if (start < fwidth)\r
+ {\r
+ viscontigs[nvis] = start;\r
+ viscontigs[nvis + 1] = fwidth; // end is inclusive\r
+ nvis += 2;\r
+ }\r
+ return viscontigs;\r
+ }\r
+ else\r
+ {\r
+ return new int[]\r
+ { 0, width };\r
+ }\r
+ }\r
+\r
+ /**\r
+ * \r
+ * @return position of first visible column of AlignmentView within its\r
+ * parent's alignment reference frame\r
+ */\r
+ public int getAlignmentOrigin()\r
+ {\r
+ return firstCol;\r
+ }\r
+\r
+ /**\r
+ * compute a deletion map for the current view according to the given\r
+ * gap/match map\r
+ * \r
+ * @param gapMap\r
+ * (as returned from SequenceI.gapMap())\r
+ * @return int[] {intersection of visible regions with gapMap)\r
+ */\r
+ public int[] getVisibleContigMapFor(int[] gapMap)\r
+ {\r
+ int[] delMap = null;\r
+ int[] viscontigs = getVisibleContigs();\r
+ int spos = 0;\r
+ int i = 0;\r
+ if (viscontigs != null)\r
+ {\r
+ // viscontigs maps from a subset of the gapMap to the gapMap, so it will\r
+ // always be equal to or shorter than gapMap\r
+ delMap = new int[gapMap.length];\r
+ for (int contig = 0; contig < viscontigs.length; contig += 2)\r
+ {\r
+\r
+ while (spos < gapMap.length && gapMap[spos] < viscontigs[contig])\r
+ {\r
+ spos++;\r
+ }\r
+ while (spos < gapMap.length\r
+ && gapMap[spos] <= viscontigs[contig + 1])\r
+ {\r
+ delMap[i++] = spos++;\r
+ }\r
+ }\r
+ int tmap[] = new int[i];\r
+ System.arraycopy(delMap, 0, tmap, 0, i);\r
+ delMap = tmap;\r
+ }\r
+ return delMap;\r
+ }\r
+\r
+ /**\r
+ * apply the getSeq(gc) method to each sequence cigar, and return the array of\r
+ * edited sequences, optionally with hidden regions removed.\r
+ * \r
+ * @param gc\r
+ * gap character to use for insertions\r
+ * @param delete\r
+ * remove hidden regions from sequences. Note: currently implemented\r
+ * in a memory inefficient way - space needed is 2*result set for\r
+ * deletion\r
+ * \r
+ * @return SequenceI[]\r
+ */\r
+ public SequenceI[] getEditedSequences(char gc, boolean delete)\r
+ {\r
+ SeqCigar[] msf = getSequences();\r
+ SequenceI[] aln = new SequenceI[msf.length];\r
+ for (int i = 0, j = msf.length; i < j; i++)\r
+ {\r
+ aln[i] = msf[i].getSeq(gc);\r
+ }\r
+ if (delete)\r
+ {\r
+ String[] sqs = getSequenceStrings(gc);\r
+ for (int i = 0; i < sqs.length; i++)\r
+ {\r
+ aln[i].setSequence(sqs[i]);\r
+ sqs[i] = null;\r
+ }\r
+ }\r
+ return aln;\r
+ }\r
+\r
+ public static void summariseAlignmentView(AlignmentView view,\r
+ PrintStream os)\r
+ {\r
+ os.print("View has " + view.sequences.length + " of which ");\r
+ if (view.selected == null) {\r
+ os.print("None");\r
+ } else {\r
+ os.print(" "+view.selected.size());\r
+ }\r
+ os.println(" are selected.");\r
+ os.print("View is " + view.getWidth() + " columns wide");\r
+ int viswid = 0;\r
+ int[] contigs = view.getContigs();\r
+ if (contigs != null)\r
+ {\r
+ viswid = view.width;\r
+ for (int i = 0; i < contigs.length; i += 3)\r
+ {\r
+ viswid += contigs[i + 2];\r
+ }\r
+ os.println("with " + viswid + " visible columns spread over "\r
+ + contigs.length / 3 + " regions.");\r
+ }\r
+ else\r
+ {\r
+ viswid = view.width;\r
+ os.println(".");\r
+ }\r
+ if (view.scGroups != null)\r
+ {\r
+ os.println("There are " + view.scGroups.size()\r
+ + " groups defined on the view.");\r
+ for (int g = 0; g < view.scGroups.size(); g++)\r
+ {\r
+ ScGroup sgr = (ScGroup) view.scGroups.elementAt(g);\r
+ os.println("Group " + g + ": Name = " + sgr.sg.getName()\r
+ + " Contains " + sgr.seqs.size() + " Seqs.");\r
+ os.println("This group runs from " + sgr.sg.getStartRes() + " to "\r
+ + sgr.sg.getEndRes());\r
+ for (int s = 0; s < sgr.seqs.size(); s++)\r
+ {\r
+ if (!((SeqCigar) sgr.seqs.elementAt(s)).isMemberOf(sgr))\r
+ {\r
+ os.println("** WARNING: sequence "\r
+ + ((SeqCigar) sgr.seqs.elementAt(s)).toString()\r
+ + " is not marked as member of group.");\r
+ }\r
+ }\r
+ }\r
+ AlignmentI visal = view.getVisibleAlignment('-');\r
+ if (visal != null)\r
+ {\r
+ os.println("Vis. alignment is " + visal.getWidth()\r
+ + " wide and has " + visal.getHeight() + " seqs.");\r
+ if (visal.getGroups() != null && visal.getGroups().size() > 0)\r
+ {\r
+ SequenceGroup sg;\r
+ Enumeration en = visal.getGroups().elements();\r
+ int i = 1;\r
+ while (en.hasMoreElements())\r
+ {\r
+ sg = (SequenceGroup) en.nextElement();\r
+ os.println("Group " + (i++) + " begins at column "\r
+ + sg.getStartRes() + " and ends at " + sg.getEndRes());\r
+ }\r
+ }\r
+ }\r
+ }\r
+ }\r
+\r
+ public static void testSelectionViews(AlignmentI alignment,\r
+ ColumnSelection csel, SequenceGroup selection)\r
+ {\r
+ System.out.println("Testing standard view creation:\n");\r
+ AlignmentView view = null;\r
+ try\r
+ {\r
+ System.out\r
+ .println("View with no hidden columns, no limit to selection, no groups to be collected:");\r
+ view = new AlignmentView(alignment, csel, selection, false, false,\r
+ false);\r
+ summariseAlignmentView(view, System.out);\r
+\r
+ } catch (Exception e)\r
+ {\r
+ e.printStackTrace();\r
+ System.err\r
+ .println("Failed to generate alignment with selection but no groups marked.");\r
+ }\r
+ try\r
+ {\r
+ System.out\r
+ .println("View with no hidden columns, no limit to selection, and all groups to be collected:");\r
+ view = new AlignmentView(alignment, csel, selection, false, false,\r
+ true);\r
+ summariseAlignmentView(view, System.out);\r
+ } catch (Exception e)\r
+ {\r
+ e.printStackTrace();\r
+ System.err\r
+ .println("Failed to generate alignment with selection marked but no groups marked.");\r
+ }\r
+ try\r
+ {\r
+ System.out\r
+ .println("View with no hidden columns, limited to selection and no groups to be collected:");\r
+ view = new AlignmentView(alignment, csel, selection, false, true,\r
+ false);\r
+ summariseAlignmentView(view, System.out);\r
+ } catch (Exception e)\r
+ {\r
+ e.printStackTrace();\r
+ System.err\r
+ .println("Failed to generate alignment with selection restricted but no groups marked.");\r
+ }\r
+ try\r
+ {\r
+ System.out\r
+ .println("View with no hidden columns, limited to selection, and all groups to be collected:");\r
+ view = new AlignmentView(alignment, csel, selection, false, true,\r
+ true);\r
+ summariseAlignmentView(view, System.out);\r
+ } catch (Exception e)\r
+ {\r
+ e.printStackTrace();\r
+ System.err\r
+ .println("Failed to generate alignment with selection restricted and groups marked.");\r
+ }\r
+ try\r
+ {\r
+ System.out\r
+ .println("View *with* hidden columns, no limit to selection, no groups to be collected:");\r
+ view = new AlignmentView(alignment, csel, selection, true, false,\r
+ false);\r
+ summariseAlignmentView(view, System.out);\r
+ } catch (Exception e)\r
+ {\r
+ e.printStackTrace();\r
+ System.err\r
+ .println("Failed to generate alignment with selection but no groups marked.");\r
+ }\r
+ try\r
+ {\r
+ System.out\r
+ .println("View *with* hidden columns, no limit to selection, and all groups to be collected:");\r
+ view = new AlignmentView(alignment, csel, selection, true, false,\r
+ true);\r
+ summariseAlignmentView(view, System.out);\r
+ } catch (Exception e)\r
+ {\r
+ e.printStackTrace();\r
+ System.err\r
+ .println("Failed to generate alignment with selection marked but no groups marked.");\r
+ }\r
+ try\r
+ {\r
+ System.out\r
+ .println("View *with* hidden columns, limited to selection and no groups to be collected:");\r
+ view = new AlignmentView(alignment, csel, selection, true, true,\r
+ false);\r
+ summariseAlignmentView(view, System.out);\r
+ } catch (Exception e)\r
+ {\r
+ e.printStackTrace();\r
+ System.err\r
+ .println("Failed to generate alignment with selection restricted but no groups marked.");\r
+ }\r
+ try\r
+ {\r
+ System.out\r
+ .println("View *with* hidden columns, limited to selection, and all groups to be collected:");\r
+ view = new AlignmentView(alignment, csel, selection, true, true, true);\r
+ summariseAlignmentView(view, System.out);\r
+ } catch (Exception e)\r
+ {\r
+ e.printStackTrace();\r
+ System.err\r
+ .println("Failed to generate alignment with selection restricted and groups marked.");\r
+ }\r
+\r
+ }\r
+}\r