return result;
}
- /* (non-Javadoc)
+
+ /*
+ * (non-Javadoc)
+ *
* @see jalview.datamodel.AlignmentI#findIndex(jalview.datamodel.SequenceI)
*/
public int findIndex(SequenceI s)
return -1;
}
- /* (non-Javadoc)
+
+ /*
+ * (non-Javadoc)
+ *
* @see jalview.datamodel.AlignmentI#findIndex(jalview.datamodel.SearchResults)
*/
public int findIndex(SearchResults results)
{
- int i=0;
-
- while (i<sequences.size())
+ int i = 0;
+
+ while (i < sequences.size())
{
if (results.involvesSequence(getSequenceAt(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
+ * @return true if alignment was changed
+ */
+ public boolean justify(boolean right)
+ {
+ boolean modified = false;
+
+ // Remove excess gaps from the end of alignment
+ int maxLength = -1;
+ 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++)
+ {
+ 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)
+ {
+ maxLength = j-ends[i*2];
+ }
+ }
+ }
+ }
+ }
+
+ maxLength++;
+ // now edit the flanking gaps to justify to either left or right
+ 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
+ extent = current.getLength();
+ if (right)
+ {
+ // right justify
+ if (extent>ends[i*2+1])
+ {
+ current.deleteChars(ends[i*2+1]+1, extent);
+ modified = true;
+ }
+ if (ends[i*2]>diff)
+ {
+ current.deleteChars(0, ends[i*2]-diff);
+ modified = true;
+ } else {
+ if (ends[i*2]<diff)
+ {
+ current.insertCharAt(0, diff-ends[i*2],gapCharacter);
+ modified = true;
+ }
+ }
+ } else {
+ // left justify
+ if (ends[i*2]>0)
+ {
+ current.deleteChars(0, ends[i*2]);
+ modified = true;
+ ends[i*2+1]-=ends[i*2];
+ extent-=ends[i*2];
+ }
+ if (extent>maxLength)
+ {
+ current.deleteChars(maxLength+1, extent);
+ modified = true;
+ } else {
+ if (extent<maxLength)
+ {
+ current.insertCharAt(extent, maxLength-extent,gapCharacter);
+ modified = true;
+ }
+ }
+ }
+ }
+ return modified;
+ }
public HiddenSequences getHiddenSequences()
{
return removed;
}
+ public void append(AlignmentI toappend)
+ {
+ // TODO test this method for a future 2.5 release
+ // currently tested for use in jalview.gui.SequenceFetcher
+ boolean samegap = toappend.getGapCharacter()==getGapCharacter();
+ char oldc = toappend.getGapCharacter();
+ 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();
+ if (sqs != null)
+ {
+ Enumeration sq = sqs.elements();
+ while (sq.hasMoreElements())
+ {
+ SequenceI addedsq=(SequenceI) sq.nextElement();
+ if (!samegap)
+ {
+ char[] oldseq = addedsq.getSequence();
+ for (int c=0;c<oldseq.length;c++)
+ {
+ if (oldseq[c]==oldc)
+ {
+ oldseq[c] = gapCharacter;
+ }
+ }
+ }
+ addSequence(addedsq);
+ }
+ }
+ AlignmentAnnotation[] alan = toappend.getAlignmentAnnotation();
+ for (int a = 0; alan != null && a < alan.length; a++)
+ {
+ addAnnotation(alan[a]);
+ }
+ AlignedCodonFrame[] acod = toappend.getCodonFrames();
+ for (int a = 0; acod != null && a < acod.length; a++)
+ {
+ this.addCodonFrame(acod[a]);
+ }
+ Vector sg = toappend.getGroups();
+ if (sg != null)
+ {
+ Enumeration el = sg.elements();
+ while (el.hasMoreElements())
+ {
+ addGroup((SequenceGroup) el.nextElement());
+ }
+ }
+ if (toappend.getHiddenSequences()!=null)
+ {
+ HiddenSequences hs = toappend.getHiddenSequences();
+ if (hiddenSequences==null)
+ {
+ hiddenSequences = new HiddenSequences(this);
+ }
+ if (hs.hiddenSequences!=null)
+ {
+ for (int s=0;s<hs.hiddenSequences.length; s++)
+ {
+ // hide the newly appended sequence in the alignment
+ if (hs.hiddenSequences[s]!=null)
+ {
+ hiddenSequences.hideSequence(hs.hiddenSequences[s]);
+ }
+ }
+ }
+ }
+ if (toappend.getProperties()!=null)
+ {
+ // 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.getClass().equals(toapprop.getClass()) && !ourval.equals(toapprop))
+ {
+ if (ourval instanceof String)
+ {
+ // append strings
+ 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);
+ }
+ }
+ }
+ }
+ } else {
+ // just add new property directly
+ setProperty(k, toapprop);
+ }
+
+ }
+ }
+ }
+
}