*/
/**
* @author JimP
- *
+ *
*/
public class Alignment implements AlignmentI
{
* Make a new alignment from an array of SeqCigars
*
* @param seqs
- * SeqCigar[]
+ * SeqCigar[]
*/
public Alignment(SeqCigar[] alseqs)
{
* appropriately.
*
* @param compactAlignment
- * CigarArray
+ * CigarArray
*/
public static AlignmentI createAlignment(CigarArray compactAlignment)
{
* DOCUMENT ME!
*
* @param i
- * DOCUMENT ME!
+ * DOCUMENT ME!
*
* @return DOCUMENT ME!
*/
* DOCUMENT ME!
*
* @param s
- * DOCUMENT ME!
+ * DOCUMENT ME!
*/
public void deleteSequence(SequenceI s)
{
* DOCUMENT ME!
*
* @param i
- * DOCUMENT ME!
+ * DOCUMENT ME!
*/
public void deleteSequence(int i)
{
* DOCUMENT ME!
*
* @param s
- * DOCUMENT ME!
+ * DOCUMENT ME!
*
* @return DOCUMENT ME!
*/
}
/**
- * DOCUMENT ME!
+ * remove any annotation that references gp
+ * @param gp (if null, removes all group associated annotation)
*/
+ private void removeAnnotationForGroup(SequenceGroup gp)
+ {
+ if (annotations==null || annotations.length==0)
+ {
+ return;
+ }
+ // remove annotation very quickly
+ AlignmentAnnotation[] t, todelete = new AlignmentAnnotation[annotations.length], tokeep = new AlignmentAnnotation[annotations.length];
+ int i, p, k;
+ if (gp == null)
+ {
+ for (i = 0, p = 0, k = 0; i < annotations.length; i++)
+ {
+ if (annotations[i].groupRef != null)
+ {
+ todelete[p++] = annotations[i];
+ }
+ else
+ {
+ tokeep[k++] = annotations[i];
+ }
+ }
+ }
+ else
+ {
+ for (i = 0, p = 0, k = 0; i < annotations.length; i++)
+ {
+ if (annotations[i].groupRef == gp)
+ {
+ todelete[p++] = annotations[i];
+ }
+ else
+ {
+ tokeep[k++] = annotations[i];
+ }
+ }
+ }
+ if (p > 0)
+ {
+ // clear out the group associated annotation.
+ for (i = 0; i < p; i++)
+ {
+ unhookAnnotation(todelete[i]);
+ todelete[i] = null;
+ }
+ t = new AlignmentAnnotation[k];
+ for (i = 0; i < k; i++)
+ {
+ t[i] = tokeep[i];
+ }
+ annotations = t;
+ }
+ }
+
public void deleteAllGroups()
{
+ if (annotations != null)
+ {
+ removeAnnotationForGroup(null);
+ }
groups.removeAllElements();
}
{
if (groups.contains(g))
{
- if (annotations!=null && annotations.length>0)
- {
- if (g.hasAnnotationRows())
- { // remove any annotation references.
- deleteAnnotation(g.getConsensus());// todo - create=false flag so we don't create another object unnecessarily
- deleteAnnotation(g.getConservationRow());
- }
- }
+ removeAnnotationForGroup(g);
groups.removeElement(g);
}
}
* (non-Javadoc)
*
* @see jalview.datamodel.AlignmentI#findName(SequenceI, java.lang.String,
- * boolean)
+ * boolean)
*/
public SequenceI findName(SequenceI startAfter, String token, boolean b)
{
/*
* (non-Javadoc)
*
- * @see jalview.datamodel.AlignmentI#findIndex(jalview.datamodel.SearchResults)
+ * @see
+ * jalview.datamodel.AlignmentI#findIndex(jalview.datamodel.SearchResults)
*/
public int findIndex(SearchResults results)
{
* DOCUMENT ME!
*
* @param gc
- * DOCUMENT ME!
+ * DOCUMENT ME!
*/
public void setGapCharacter(char gc)
{
/*
* (non-Javadoc)
*
- * @see jalview.datamodel.AlignmentI#deleteAnnotation(jalview.datamodel.AlignmentAnnotation)
+ * @seejalview.datamodel.AlignmentI#deleteAnnotation(jalview.datamodel.
+ * AlignmentAnnotation)
*/
public boolean deleteAnnotation(AlignmentAnnotation aa)
{
if (swap)
{
annotations = temp;
- if (aa.sequenceRef != null)
- aa.sequenceRef.removeAlignmentAnnotation(aa);
+ unhookAnnotation(aa);
}
return swap;
}
- /* (non-Javadoc)
- * @see jalview.datamodel.AlignmentI#addAnnotation(jalview.datamodel.AlignmentAnnotation)
+ /**
+ * remove any object references associated with this annotation
+ *
+ * @param aa
+ */
+ private void unhookAnnotation(AlignmentAnnotation aa)
+ {
+ if (aa.sequenceRef != null)
+ {
+ aa.sequenceRef.removeAlignmentAnnotation(aa);
+ }
+ if (aa.groupRef != null)
+ {
+ // probably need to do more here in the future (post 2.5.0)
+ aa.groupRef = null;
+ }
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @seejalview.datamodel.AlignmentI#addAnnotation(jalview.datamodel.
+ * AlignmentAnnotation)
*/
public void addAnnotation(AlignmentAnnotation aa)
{
addAnnotation(aa, -1);
}
-
- /* (non-Javadoc)
- * @see jalview.datamodel.AlignmentI#addAnnotation(jalview.datamodel.AlignmentAnnotation, int)
+
+ /*
+ * (non-Javadoc)
+ *
+ * @seejalview.datamodel.AlignmentI#addAnnotation(jalview.datamodel.
+ * AlignmentAnnotation, int)
*/
public void addAnnotation(AlignmentAnnotation aa, int pos)
{
AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
int i = 0;
- if (pos==-1 || pos>=aSize) {
+ if (pos == -1 || pos >= aSize)
+ {
temp[aSize - 1] = aa;
- } else {
+ }
+ else
+ {
temp[pos] = aa;
}
if (aSize > 1)
{
- int p=0;
- for (i = 0; i < (aSize-1); i++,p++)
+ int p = 0;
+ for (i = 0; i < (aSize - 1); i++, p++)
{
- if (p==pos)
+ if (p == pos)
{
p++;
}
- if (p<temp.length) {
+ if (p < temp.length)
+ {
temp[p] = annotations[i];
}
}
}
return modified;
}
+
/**
- * Justify the sequences to the left or right by deleting and inserting gaps before the initial residue or after the terminal residue
- * @param right true if alignment padded to right, false to justify to left
+ * Justify the sequences to the left or right by deleting and inserting gaps
+ * before the initial residue or after the terminal residue
+ *
+ * @param right
+ * true if alignment padded to right, false to justify to left
* @return true if alignment was changed
*/
public boolean justify(boolean right)
// Remove excess gaps from the end of alignment
int maxLength = -1;
- int ends[] = new int[sequences.size()*2];
+ int ends[] = new int[sequences.size() * 2];
SequenceI current;
for (int i = 0; i < sequences.size(); i++)
{
current = getSequenceAt(i);
// This should really be a sequence method
- ends[i*2] = current.findIndex(current.getStart());
- ends[i*2+1] = current.findIndex(current.getStart()+current.getLength());
- boolean hitres=false;
- for (int j = 0,rs=0,ssiz=current.getLength(); j<ssiz; j++)
+ ends[i * 2] = current.findIndex(current.getStart());
+ ends[i * 2 + 1] = current.findIndex(current.getStart()
+ + current.getLength());
+ boolean hitres = false;
+ for (int j = 0, rs = 0, ssiz = current.getLength(); j < ssiz; j++)
{
if (!jalview.util.Comparison.isGap(current.getCharAt(j)))
{
if (!hitres)
{
- ends[i*2] = j;
- hitres=true;
- } else {
- ends[i*2+1] = j;
- if (j-ends[i*2]>maxLength)
+ ends[i * 2] = j;
+ hitres = true;
+ }
+ else
+ {
+ ends[i * 2 + 1] = j;
+ if (j - ends[i * 2] > maxLength)
{
- maxLength = j-ends[i*2];
+ maxLength = j - ends[i * 2];
}
}
}
maxLength++;
// now edit the flanking gaps to justify to either left or right
- int cLength,extent,diff;
+ int cLength, extent, diff;
for (int i = 0; i < sequences.size(); i++)
{
current = getSequenceAt(i);
-
- cLength = 1+ends[i*2+1]-ends[i*2];
- diff = maxLength-cLength; // number of gaps to indent
+
+ cLength = 1 + ends[i * 2 + 1] - ends[i * 2];
+ diff = maxLength - cLength; // number of gaps to indent
extent = current.getLength();
if (right)
{
// right justify
- if (extent>ends[i*2+1])
+ if (extent > ends[i * 2 + 1])
{
- current.deleteChars(ends[i*2+1]+1, extent);
+ current.deleteChars(ends[i * 2 + 1] + 1, extent);
modified = true;
}
- if (ends[i*2]>diff)
+ if (ends[i * 2] > diff)
{
- current.deleteChars(0, ends[i*2]-diff);
+ current.deleteChars(0, ends[i * 2] - diff);
modified = true;
- } else {
- if (ends[i*2]<diff)
+ }
+ else
+ {
+ if (ends[i * 2] < diff)
{
- current.insertCharAt(0, diff-ends[i*2],gapCharacter);
+ current.insertCharAt(0, diff - ends[i * 2], gapCharacter);
modified = true;
}
}
- } else {
+ }
+ else
+ {
// left justify
- if (ends[i*2]>0)
+ if (ends[i * 2] > 0)
{
- current.deleteChars(0, ends[i*2]);
+ current.deleteChars(0, ends[i * 2]);
modified = true;
- ends[i*2+1]-=ends[i*2];
- extent-=ends[i*2];
+ ends[i * 2 + 1] -= ends[i * 2];
+ extent -= ends[i * 2];
}
- if (extent>maxLength)
+ if (extent > maxLength)
{
- current.deleteChars(maxLength+1, extent);
+ current.deleteChars(maxLength + 1, extent);
modified = true;
- } else {
- if (extent<maxLength)
+ }
+ else
+ {
+ if (extent < maxLength)
{
- current.insertCharAt(extent, maxLength-extent,gapCharacter);
+ current.insertCharAt(extent, maxLength - extent, gapCharacter);
modified = true;
}
}
/*
* (non-Javadoc)
*
- * @see jalview.datamodel.AlignmentI#addCodonFrame(jalview.datamodel.AlignedCodonFrame)
+ * @see
+ * jalview.datamodel.AlignmentI#addCodonFrame(jalview.datamodel.AlignedCodonFrame
+ * )
*/
public void addCodonFrame(AlignedCodonFrame codons)
{
/*
* (non-Javadoc)
*
- * @see jalview.datamodel.AlignmentI#getCodonFrame(jalview.datamodel.SequenceI)
+ * @see
+ * jalview.datamodel.AlignmentI#getCodonFrame(jalview.datamodel.SequenceI)
*/
public AlignedCodonFrame[] getCodonFrame(SequenceI seq)
{
/*
* (non-Javadoc)
*
- * @see jalview.datamodel.AlignmentI#removeCodonFrame(jalview.datamodel.AlignedCodonFrame)
+ * @seejalview.datamodel.AlignmentI#removeCodonFrame(jalview.datamodel.
+ * AlignedCodonFrame)
*/
public boolean removeCodonFrame(AlignedCodonFrame codons)
{
{
// TODO test this method for a future 2.5 release
// currently tested for use in jalview.gui.SequenceFetcher
- boolean samegap = toappend.getGapCharacter()==getGapCharacter();
+ boolean samegap = toappend.getGapCharacter() == getGapCharacter();
char oldc = toappend.getGapCharacter();
- boolean hashidden = toappend.getHiddenSequences()!=null && toappend.getHiddenSequences().hiddenSequences!=null;
+ boolean hashidden = toappend.getHiddenSequences() != null
+ && toappend.getHiddenSequences().hiddenSequences != null;
// get all sequences including any hidden ones
- Vector sqs = (hashidden) ? toappend.getHiddenSequences().getFullAlignment().getSequences() : toappend.getSequences();
+ Vector sqs = (hashidden) ? toappend.getHiddenSequences()
+ .getFullAlignment().getSequences() : toappend.getSequences();
if (sqs != null)
{
Enumeration sq = sqs.elements();
while (sq.hasMoreElements())
{
- SequenceI addedsq=(SequenceI) sq.nextElement();
+ SequenceI addedsq = (SequenceI) sq.nextElement();
if (!samegap)
{
char[] oldseq = addedsq.getSequence();
- for (int c=0;c<oldseq.length;c++)
+ for (int c = 0; c < oldseq.length; c++)
{
- if (oldseq[c]==oldc)
+ if (oldseq[c] == oldc)
{
oldseq[c] = gapCharacter;
}
addGroup((SequenceGroup) el.nextElement());
}
}
- if (toappend.getHiddenSequences()!=null)
+ if (toappend.getHiddenSequences() != null)
{
HiddenSequences hs = toappend.getHiddenSequences();
- if (hiddenSequences==null)
+ if (hiddenSequences == null)
{
hiddenSequences = new HiddenSequences(this);
}
- if (hs.hiddenSequences!=null)
+ if (hs.hiddenSequences != null)
{
- for (int s=0;s<hs.hiddenSequences.length; s++)
+ for (int s = 0; s < hs.hiddenSequences.length; s++)
{
// hide the newly appended sequence in the alignment
- if (hs.hiddenSequences[s]!=null)
+ if (hs.hiddenSequences[s] != null)
{
hiddenSequences.hideSequence(hs.hiddenSequences[s]);
}
}
}
}
- if (toappend.getProperties()!=null)
+ if (toappend.getProperties() != null)
{
- // we really can't do very much here - just try to concatenate strings where property collisions occur.
+ // we really can't do very much here - just try to concatenate strings
+ // where property collisions occur.
Enumeration key = toappend.getProperties().keys();
while (key.hasMoreElements())
{
Object k = key.nextElement();
Object ourval = this.getProperty(k);
Object toapprop = toappend.getProperty(k);
- if (ourval!=null)
+ if (ourval != null)
{
- if (ourval.getClass().equals(toapprop.getClass()) && !ourval.equals(toapprop))
+ if (ourval.getClass().equals(toapprop.getClass())
+ && !ourval.equals(toapprop))
{
if (ourval instanceof String)
{
// append strings
- this.setProperty(k, ((String) ourval)+"; "+((String) toapprop));
- } else {
+ this.setProperty(k, ((String) ourval) + "; "
+ + ((String) toapprop));
+ }
+ else
+ {
if (ourval instanceof Vector)
{
// append vectors
Enumeration theirv = ((Vector) toapprop).elements();
while (theirv.hasMoreElements())
{
- ((Vector)ourval).addElement(theirv);
+ ((Vector) ourval).addElement(theirv);
}
}
}
}
- } else {
+ }
+ else
+ {
// just add new property directly
setProperty(k, toapprop);
}
-
+
}
}
}