X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=src%2Fjalview%2Fdatamodel%2FAlignmentView.java;h=a23aad5e837e04ad8d4e19a8feacadb76b2aba0b;hb=aa213924876640ba82e866da5a750d9cdaa816d2;hp=76f7bd829199770fec474a98d0a8357921d20a16;hpb=c4d991e1b3d77b8f18e705d671f0a26e289a5b5c;p=jalview.git
diff --git a/src/jalview/datamodel/AlignmentView.java b/src/jalview/datamodel/AlignmentView.java
index 76f7bd8..a23aad5 100644
--- a/src/jalview/datamodel/AlignmentView.java
+++ b/src/jalview/datamodel/AlignmentView.java
@@ -1,6 +1,6 @@
/*
- * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6)
- * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
+ * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
+ * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
*
* This file is part of Jalview.
*
@@ -17,32 +17,20 @@
*/
package jalview.datamodel;
+import jalview.util.ShiftList;
+
+import java.io.PrintStream;
+import java.util.Enumeration;
+import java.util.List;
+import java.util.Vector;
+
/**
- *
- * Title:
- *
- *
- *
- * Description:
- *
- *
- *
- * Copyright: Copyright (c) 2004
- *
- *
- *
- * Company: Dundee University
- *
- *
- * @author not attributable
- * @version 1.0
+ * Transient object compactly representing a 'view' of an alignment - with
+ * discontinuities marked. Extended in Jalview 2.7 to optionally record sequence
+ * groups and specific selected regions on the alignment.
*/
public class AlignmentView
{
- /**
- * Transient object compactly representing a 'view' of an alignment - with
- * discontinuities marked.
- */
private SeqCigar[] sequences = null;
private int[] contigs = null;
@@ -51,6 +39,186 @@ public class AlignmentView
private int firstCol = 0;
+ /**
+ * one or more ScGroup objects, which are referenced by each seqCigar's group
+ * membership
+ */
+ private Vector scGroups;
+
+ /**
+ * Group defined over SeqCigars. Unlike AlignmentI associated groups, each
+ * SequenceGroup hold just the essential properties for the group, but no
+ * references to the sequences involved. SeqCigars hold references to the
+ * seuqenceGroup entities themselves.
+ */
+ private class ScGroup
+ {
+ public Vector seqs;
+
+ public SequenceGroup sg;
+
+ ScGroup()
+ {
+ seqs = new Vector();
+ }
+ }
+
+ /**
+ * vector of selected seqCigars. This vector is also referenced by each
+ * seqCigar contained in it.
+ */
+ private Vector selected;
+
+ /**
+ * Construct an alignmentView from a live jalview alignment view. Note -
+ * hidden rows will be excluded from alignmentView
+ *
+ * @param alignment
+ * - alignment as referenced by an AlignViewport
+ * @param columnSelection
+ * -
+ * @param selection
+ * @param hasHiddenColumns
+ * - mark the hidden columns in columnSelection as hidden in the view
+ * @param selectedRegionOnly
+ * - when set, only include the selected region in the view,
+ * otherwise just mark the selected region on the constructed view.
+ * @param recordGroups
+ * - when set, any groups on the given alignment will be marked on
+ * the view
+ */
+ public AlignmentView(AlignmentI alignment,
+ ColumnSelection columnSelection, SequenceGroup selection,
+ boolean hasHiddenColumns, boolean selectedRegionOnly,
+ boolean recordGroups)
+ {
+ // refactored from AlignViewport.getAlignmentView(selectedOnly);
+ this(new jalview.datamodel.CigarArray(alignment,
+ (hasHiddenColumns ? columnSelection : null),
+ (selectedRegionOnly ? selection : null)),
+ (selectedRegionOnly && selection != null) ? selection
+ .getStartRes() : 0);
+ // walk down SeqCigar array and Alignment Array - optionally restricted by
+ // selected region.
+ // test group membership for each sequence in each group, store membership
+ // and record non-empty groups in group list.
+ // record / sub-select selected region on the alignment view
+ SequenceI[] selseqs;
+ if (selection != null)
+ {
+ List sel = selection.getSequences(null);
+ this.selected = new Vector();
+ selseqs = selection.getSequencesInOrder(alignment, selectedRegionOnly);
+ }
+ else
+ {
+ selseqs = alignment.getSequencesArray();
+ }
+
+ // get the alignment's group list and make a copy
+ Vector grps = new Vector();
+ List gg = alignment.getGroups();
+ grps.addAll(gg);
+ ScGroup[] sgrps = null;
+ boolean addedgps[] = null;
+ if (grps != null)
+ {
+ SequenceGroup sg;
+ if (selection != null && selectedRegionOnly)
+ {
+ // trim annotation to the region being stored.
+ // strip out any groups that do not actually intersect with the
+ // visible and selected region
+ int ssel = selection.getStartRes(), esel = selection.getEndRes();
+ Vector isg = new Vector();
+ Enumeration en = grps.elements();
+ while (en.hasMoreElements())
+ {
+ sg = (SequenceGroup) en.nextElement();
+
+ if (!(sg.getStartRes() > esel || sg.getEndRes() < ssel))
+ {
+ // adjust bounds of new group, if necessary.
+ if (sg.getStartRes() < ssel)
+ {
+ sg.setStartRes(ssel);
+ }
+ if (sg.getEndRes() > esel)
+ {
+ sg.setEndRes(esel);
+ }
+ sg.setStartRes(sg.getStartRes()-ssel+1);
+ sg.setEndRes(sg.getEndRes()-ssel+1);
+
+ isg.addElement(sg);
+ }
+ }
+ grps = isg;
+ }
+
+ sgrps = new ScGroup[grps.size()];
+ addedgps = new boolean[grps.size()];
+ for (int g = 0; g < sgrps.length; g++)
+ {
+ sg = (SequenceGroup) grps.elementAt(g);
+ sgrps[g] = new ScGroup();
+ sgrps[g].sg = new SequenceGroup(sg);
+ addedgps[g] = false;
+ grps.setElementAt(sg.getSequences(null), g);
+ }
+ // grps now contains vectors (should be sets) for each group, so we can
+ // track when we've done with the group
+ }
+ int csi = 0;
+ for (int i = 0; i < selseqs.length; i++)
+ {
+ if (selseqs[i] != null)
+ {
+ if (selection != null && !selectedRegionOnly)
+ {
+ sequences[csi].setGroupMembership(selected);
+ selected.addElement(sequences[csi]);
+ }
+ if (grps != null)
+ {
+ for (int sg = 0; sg < sgrps.length; sg++)
+ {
+ if (((Vector) grps.elementAt(sg)).contains(selseqs[i]))
+ {
+ sequences[csi].setGroupMembership(sgrps[sg]);
+ sgrps[sg].sg.deleteSequence(selseqs[i], false);
+ sgrps[sg].seqs.addElement(sequences[csi]);
+ if (!addedgps[sg])
+ {
+ if (scGroups == null)
+ {
+ scGroups = new Vector();
+ }
+ addedgps[sg] = true;
+ scGroups.addElement(sgrps[sg]);
+ }
+ }
+ }
+ }
+ csi++;
+ }
+ }
+ // finally, delete the remaining sequences (if any) not selected
+ for (int sg = 0; sg < sgrps.length; sg++)
+ {
+ SequenceI[] sqs = sgrps[sg].sg.getSequencesAsArray(null);
+ for (int si = 0; si < sqs.length; si++)
+ {
+ sgrps[sg].sg.deleteSequence(sqs[si], false);
+ }
+ sgrps[sg] = null;
+ }
+ }
+
+ /**
+ * construct an alignmentView from a SeqCigarArray. Errors are thrown if the
+ * seqcigararray.isSeqCigarArray() flag is not set.
+ */
public AlignmentView(CigarArray seqcigararray)
{
if (!seqcigararray.isSeqCigarArray())
@@ -120,7 +288,216 @@ public class AlignmentView
}
/**
- * getSequenceStrings
+ * return the visible alignment corresponding to this view. Sequences in this
+ * alignment are edited versions of the parent sequences - where hidden
+ * regions have been removed. NOTE: the sequence data in this alignment is not
+ * complete!
+ *
+ * @param c
+ * @return
+ */
+ public AlignmentI getVisibleAlignment(char c)
+ {
+ SequenceI[] aln = getVisibleSeqs(c);
+
+ AlignmentI vcal = new Alignment(aln);
+ addPrunedGroupsInOrder(vcal, -1, -1, true);
+ return vcal;
+ }
+
+ /**
+ * add groups from view to the given alignment
+ *
+ * @param vcal
+ * @param gstart
+ * -1 or 0 to width-1
+ * @param gend
+ * -1 or gstart to width-1
+ * @param viscontigs
+ * - true if vcal is alignment of the visible regions of the view
+ * (e.g. as returned from getVisibleAlignment)
+ */
+ private void addPrunedGroupsInOrder(AlignmentI vcal, int gstart,
+ int gend, boolean viscontigs)
+ {
+ boolean r = false;
+ if (gstart > -1 && gstart <= gend)
+ {
+ r = true;
+ }
+
+ SequenceI[] aln = vcal.getSequencesArray();
+ {
+ /**
+ * prune any groups to the visible coordinates of the alignment.
+ */
+ {
+ int nvg = (scGroups != null) ? scGroups.size() : 0;
+ if (nvg > 0)
+ {
+ SequenceGroup[] nsg = new SequenceGroup[nvg];
+ for (int g = 0; g < nvg; g++)
+ {
+ SequenceGroup sg = ((ScGroup) scGroups.elementAt(g)).sg;
+ if (r)
+ {
+ if (sg.getStartRes() > gend || sg.getEndRes() < gstart)
+ {
+ // Skip this group
+ nsg[g] = null;
+ continue;
+ }
+ }
+
+ // clone group properties
+ nsg[g] = new SequenceGroup(sg);
+
+ // may need to shift/trim start and end ?
+ if (r && !viscontigs)
+ {
+ // Not fully tested code - routine not yet called with viscontigs==false
+ if (nsg[g].getStartRes() < gstart)
+ {
+ nsg[g].setStartRes(0);
+ }
+ else
+ {
+ nsg[g].setStartRes(nsg[g].getStartRes() - gstart);
+ nsg[g].setEndRes(nsg[g].getEndRes() - gstart);
+ }
+ if (nsg[g].getEndRes() > (gend-gstart))
+ {
+ nsg[g].setEndRes(gend-gstart);
+ }
+ }
+ }
+ if (viscontigs)
+ {
+ // prune groups to cover just the visible positions between
+ // gstart/gend.
+ if (contigs != null)
+ {
+ int p = 0;
+ ShiftList prune = new ShiftList();
+ if (r)
+ {
+ // adjust for start of alignment within visible window.
+ prune.addShift(gstart, -gstart); //
+ }
+ for (int h = 0; h < contigs.length; h += 3)
+ {
+ {
+ prune.addShift(p + contigs[h + 1], contigs[h + 2]
+ - contigs[h + 1]);
+ }
+ p = contigs[h + 1] + contigs[h + 2];
+ }
+ for (int g = 0; g < nsg.length; g++)
+ {
+ if (nsg[g] != null)
+ {
+ int s = nsg[g].getStartRes(), t = nsg[g].getEndRes();
+ int w = 1 + t - s;
+ if (r)
+ {
+ if (s < gstart)
+ {
+ s = gstart;
+ }
+ if (t > gend)
+ {
+ t = gend;
+ }
+ }
+ s = prune.shift(s);
+ t = prune.shift(t);
+ nsg[g].setStartRes(s);
+ nsg[g].setEndRes(t);
+ }
+ }
+ }
+ }
+
+ for (int nsq = 0; nsq < aln.length; nsq++)
+ {
+ for (int g = 0; g < nvg; g++)
+ {
+ if (nsg[g] != null
+ && sequences[nsq].isMemberOf(scGroups.elementAt(g)))
+ {
+ nsg[g].addSequence(aln[nsq], false);
+ }
+ }
+ }
+ for (int g = 0; g < nvg; g++)
+ {
+ if (nsg[g] != null && nsg[g].getSize() > 0)
+ {
+ vcal.addGroup(nsg[g]);
+ }
+ nsg[g] = null;
+ }
+ }
+ }
+ }
+ }
+
+ /**
+ * generate sequence array corresponding to the visible parts of the
+ * alignment.
+ *
+ * @param c
+ * @return
+ */
+ private SequenceI[] getVisibleSeqs(char c)
+ {
+ SequenceI[] aln = new SequenceI[sequences.length];
+ for (int i = 0, j = sequences.length; i < j; i++)
+ {
+ aln[i] = sequences[i].getSeq('-');
+ }
+ // Remove hidden regions from sequence objects.
+ String seqs[] = getSequenceStrings('-');
+ for (int i = 0, j = aln.length; i < j; i++)
+ {
+ aln[i].setSequence(seqs[i]);
+ }
+ return aln;
+ }
+
+ /**
+ * creates new alignment objects for all contiguous visible segments
+ *
+ * @param c
+ * @param start
+ * @param end
+ * @param regionOfInterest
+ * specify which sequences to include (or null to include all
+ * sequences)
+ * @return AlignmentI[] - all alignments where each sequence is a subsequence
+ * constructed from visible contig regions of view
+ */
+ public AlignmentI[] getVisibleContigAlignments(char c)
+ {
+ int nvc = 0;
+ int[] vcontigs = getVisibleContigs();
+ SequenceI[][] contigviews = getVisibleContigs(c);
+ AlignmentI[] vcals = new AlignmentI[contigviews.length];
+ for (nvc = 0; nvc < contigviews.length; nvc++)
+ {
+ vcals[nvc] = new Alignment(contigviews[nvc]);
+ if (scGroups!=null && scGroups.size()>0)
+ {
+ addPrunedGroupsInOrder(vcals[nvc], vcontigs[nvc*2],vcontigs[nvc*2+1], true);
+ }
+ }
+ return vcals;
+ }
+
+
+ /**
+ * get an array of visible sequence strings for a view on an alignment using
+ * the given gap character
*
* @param c
* char
@@ -536,7 +913,6 @@ public class AlignmentView
*/
public int getAlignmentOrigin()
{
- // TODO Auto-generated method stub
return firstCol;
}
@@ -544,7 +920,8 @@ public class AlignmentView
* compute a deletion map for the current view according to the given
* gap/match map
*
- * @param gapMap (as returned from SequenceI.gapMap())
+ * @param gapMap
+ * (as returned from SequenceI.gapMap())
* @return int[] {intersection of visible regions with gapMap)
*/
public int[] getVisibleContigMapFor(int[] gapMap)
@@ -577,4 +954,217 @@ public class AlignmentView
}
return delMap;
}
+
+ /**
+ * apply the getSeq(gc) method to each sequence cigar, and return the array of
+ * edited sequences, optionally with hidden regions removed.
+ *
+ * @param gc
+ * gap character to use for insertions
+ * @param delete
+ * remove hidden regions from sequences. Note: currently implemented
+ * in a memory inefficient way - space needed is 2*result set for
+ * deletion
+ *
+ * @return SequenceI[]
+ */
+ public SequenceI[] getEditedSequences(char gc, boolean delete)
+ {
+ SeqCigar[] msf = getSequences();
+ SequenceI[] aln = new SequenceI[msf.length];
+ for (int i = 0, j = msf.length; i < j; i++)
+ {
+ aln[i] = msf[i].getSeq(gc);
+ }
+ if (delete)
+ {
+ String[] sqs = getSequenceStrings(gc);
+ for (int i = 0; i < sqs.length; i++)
+ {
+ aln[i].setSequence(sqs[i]);
+ sqs[i] = null;
+ }
+ }
+ return aln;
+ }
+
+ public static void summariseAlignmentView(AlignmentView view,
+ PrintStream os)
+ {
+ os.print("View has " + view.sequences.length + " of which ");
+ if (view.selected == null) {
+ os.print("None");
+ } else {
+ os.print(" "+view.selected.size());
+ }
+ os.println(" are selected.");
+ os.print("View is " + view.getWidth() + " columns wide");
+ int viswid = 0;
+ int[] contigs = view.getContigs();
+ if (contigs != null)
+ {
+ viswid = view.width;
+ for (int i = 0; i < contigs.length; i += 3)
+ {
+ viswid += contigs[i + 2];
+ }
+ os.println("with " + viswid + " visible columns spread over "
+ + contigs.length / 3 + " regions.");
+ }
+ else
+ {
+ viswid = view.width;
+ os.println(".");
+ }
+ if (view.scGroups != null)
+ {
+ os.println("There are " + view.scGroups.size()
+ + " groups defined on the view.");
+ for (int g = 0; g < view.scGroups.size(); g++)
+ {
+ ScGroup sgr = (ScGroup) view.scGroups.elementAt(g);
+ os.println("Group " + g + ": Name = " + sgr.sg.getName()
+ + " Contains " + sgr.seqs.size() + " Seqs.");
+ os.println("This group runs from " + sgr.sg.getStartRes() + " to "
+ + sgr.sg.getEndRes());
+ for (int s = 0; s < sgr.seqs.size(); s++)
+ {
+ if (!((SeqCigar) sgr.seqs.elementAt(s)).isMemberOf(sgr))
+ {
+ os.println("** WARNING: sequence "
+ + ((SeqCigar) sgr.seqs.elementAt(s)).toString()
+ + " is not marked as member of group.");
+ }
+ }
+ }
+ AlignmentI visal = view.getVisibleAlignment('-');
+ if (visal != null)
+ {
+ os.println("Vis. alignment is " + visal.getWidth()
+ + " wide and has " + visal.getHeight() + " seqs.");
+ if (visal.getGroups() != null && visal.getGroups().size() > 0)
+ {
+
+ int i = 1;
+ for (SequenceGroup sg:visal.getGroups())
+ {
+ os.println("Group " + (i++) + " begins at column "
+ + sg.getStartRes() + " and ends at " + sg.getEndRes());
+ }
+ }
+ }
+ }
+ }
+
+ public static void testSelectionViews(AlignmentI alignment,
+ ColumnSelection csel, SequenceGroup selection)
+ {
+ System.out.println("Testing standard view creation:\n");
+ AlignmentView view = null;
+ try
+ {
+ System.out
+ .println("View with no hidden columns, no limit to selection, no groups to be collected:");
+ view = new AlignmentView(alignment, csel, selection, false, false,
+ false);
+ summariseAlignmentView(view, System.out);
+
+ } catch (Exception e)
+ {
+ e.printStackTrace();
+ System.err
+ .println("Failed to generate alignment with selection but no groups marked.");
+ }
+ try
+ {
+ System.out
+ .println("View with no hidden columns, no limit to selection, and all groups to be collected:");
+ view = new AlignmentView(alignment, csel, selection, false, false,
+ true);
+ summariseAlignmentView(view, System.out);
+ } catch (Exception e)
+ {
+ e.printStackTrace();
+ System.err
+ .println("Failed to generate alignment with selection marked but no groups marked.");
+ }
+ try
+ {
+ System.out
+ .println("View with no hidden columns, limited to selection and no groups to be collected:");
+ view = new AlignmentView(alignment, csel, selection, false, true,
+ false);
+ summariseAlignmentView(view, System.out);
+ } catch (Exception e)
+ {
+ e.printStackTrace();
+ System.err
+ .println("Failed to generate alignment with selection restricted but no groups marked.");
+ }
+ try
+ {
+ System.out
+ .println("View with no hidden columns, limited to selection, and all groups to be collected:");
+ view = new AlignmentView(alignment, csel, selection, false, true,
+ true);
+ summariseAlignmentView(view, System.out);
+ } catch (Exception e)
+ {
+ e.printStackTrace();
+ System.err
+ .println("Failed to generate alignment with selection restricted and groups marked.");
+ }
+ try
+ {
+ System.out
+ .println("View *with* hidden columns, no limit to selection, no groups to be collected:");
+ view = new AlignmentView(alignment, csel, selection, true, false,
+ false);
+ summariseAlignmentView(view, System.out);
+ } catch (Exception e)
+ {
+ e.printStackTrace();
+ System.err
+ .println("Failed to generate alignment with selection but no groups marked.");
+ }
+ try
+ {
+ System.out
+ .println("View *with* hidden columns, no limit to selection, and all groups to be collected:");
+ view = new AlignmentView(alignment, csel, selection, true, false,
+ true);
+ summariseAlignmentView(view, System.out);
+ } catch (Exception e)
+ {
+ e.printStackTrace();
+ System.err
+ .println("Failed to generate alignment with selection marked but no groups marked.");
+ }
+ try
+ {
+ System.out
+ .println("View *with* hidden columns, limited to selection and no groups to be collected:");
+ view = new AlignmentView(alignment, csel, selection, true, true,
+ false);
+ summariseAlignmentView(view, System.out);
+ } catch (Exception e)
+ {
+ e.printStackTrace();
+ System.err
+ .println("Failed to generate alignment with selection restricted but no groups marked.");
+ }
+ try
+ {
+ System.out
+ .println("View *with* hidden columns, limited to selection, and all groups to be collected:");
+ view = new AlignmentView(alignment, csel, selection, true, true, true);
+ summariseAlignmentView(view, System.out);
+ } catch (Exception e)
+ {
+ e.printStackTrace();
+ System.err
+ .println("Failed to generate alignment with selection restricted and groups marked.");
+ }
+
+ }
}