/*
- * Jalview - A Sequence Alignment Editor and Viewer
+* Jalview - A Sequence Alignment Editor and Viewer
* Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
- */
+*
+* This program is free software; you can redistribute it and/or
+* modify it under the terms of the GNU General Public License
+* as published by the Free Software Foundation; either version 2
+* of the License, or (at your option) any later version.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with this program; if not, write to the Free Software
+* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
+*/
package jalview.datamodel;
+
import java.util.*;
import jalview.analysis.*;
{
SequenceI datasetSequence;
String name;
- private char[] sequence;
+ private char [] sequence;
String description;
int start;
int end;
/** DOCUMENT ME!! */
public SequenceFeature[] sequenceFeatures;
+
/**
* Creates a new Sequence object.
*
checkValidRange();
}
- public Sequence(String name, char[] sequence, int start, int end)
+ public Sequence(String name, char [] sequence, int start, int end)
{
this.name = name;
this.sequence = sequence;
int endRes = 0;
for (int j = 0; j < sequence.length; j++)
{
- if (!jalview.util.Comparison.isGap(sequence[j]))
+ if (!jalview.util.Comparison.isGap( sequence[j] ))
{
endRes++;
}
}
/**
- * Creates a new Sequence object.
- *
+ * Creates a new Sequence object with new features, DBRefEntries, AlignmentAnnotations, and PDBIds
+ * but inherits any existing dataset sequence reference.
* @param seq DOCUMENT ME!
*/
public Sequence(SequenceI seq)
{
+ this(seq, seq.getAnnotation());
+ }
+ /**
+ * Create a new sequence object with new features, DBRefEntries, and PDBIds
+ * but inherits any existing dataset sequence reference, and duplicate of
+ * any annotation that is present in the given annotation array.
+ * @param seq the sequence to be copied
+ * @param alAnnotation an array of annotation including some associated with seq
+ */
+ public Sequence(SequenceI seq, AlignmentAnnotation[] alAnnotation)
+ {
this(seq.getName(),
- seq.getSequence(),
- seq.getStart(),
- seq.getEnd());
+ seq.getSequence(),
+ seq.getStart(),
+ seq.getEnd());
description = seq.getDescription();
+ if (seq.getSequenceFeatures()!=null) {
+ SequenceFeature[] sf = seq.getSequenceFeatures();
+ for (int i=0; i<sf.length; i++) {
+ addSequenceFeature(new SequenceFeature(sf[i]));
+ }
+ }
+ if (seq.getDBRef()!=null) {
+ DBRefEntry[] dbr = seq.getDBRef();
+ for (int i=0; i<dbr.length; i++) {
+ addDBRef(new DBRefEntry(dbr[i]));
+ }
+ }
+ setDatasetSequence(seq.getDatasetSequence());
+ if (seq.getAnnotation()!=null && alAnnotation!=null) {
+ AlignmentAnnotation[] sqann = seq.getAnnotation();
+ for (int i=0;i<sqann.length; i++)
+ {
+ boolean found = sqann == alAnnotation;
+ if (!found)
+ {
+ for (int apos = 0; !found && apos<alAnnotation.length; apos++)
+ {
+ found = (alAnnotation[apos] == sqann[i]);
+ }
+ }
+ AlignmentAnnotation newann = new AlignmentAnnotation(sqann[i]);
+ addAlignmentAnnotation(newann);
+ }
+ }
+ if (seq.getPDBId()!=null) {
+ Vector ids = seq.getPDBId();
+ Enumeration e = ids.elements();
+ while (e.hasMoreElements()) {
+ this.addPDBId(new PDBEntry((PDBEntry) e.nextElement()));
+ }
+ }
}
/**
public void deleteFeature(SequenceFeature sf)
{
- if (sequenceFeatures == null)
+ if(sequenceFeatures==null)
{
return;
}
- int index = 0;
+ int index=0;
for (index = 0; index < sequenceFeatures.length; index++)
{
if (sequenceFeatures[index].equals(sf))
}
}
- if (index == sequenceFeatures.length)
+
+ if(index==sequenceFeatures.length)
{
return;
}
int sfLength = sequenceFeatures.length;
- if (sfLength < 2)
+ if(sfLength<2)
{
sequenceFeatures = null;
}
else
{
- SequenceFeature[] temp = new SequenceFeature[sfLength - 1];
+ SequenceFeature[] temp = new SequenceFeature[sfLength-1];
System.arraycopy(sequenceFeatures, 0, temp, 0, index);
- if (index < sfLength)
+ if(index<sfLength)
{
System.arraycopy(sequenceFeatures,
index + 1,
temp,
- index, sequenceFeatures.length - index - 1);
+ index, sequenceFeatures.length - index -1);
}
sequenceFeatures = temp;
checkValidRange();
}
+
public String getSequenceAsString()
{
return new String(sequence);
return new String(getSequence(start, end));
}
- public char[] getSequence()
+
+ public char [] getSequence()
{
return sequence;
}
*
* @return DOCUMENT ME!
*/
- public char[] getSequence(int start, int end)
+ public char [] getSequence(int start, int end)
{
+ if (start<0)
+ start=0;
// JBPNote - left to user to pad the result here (TODO:Decide on this policy)
if (start >= sequence.length)
{
end = sequence.length;
}
- char[] reply = new char[end - start];
- System.arraycopy(sequence, start, reply, 0, end - start);
+ char [] reply = new char[end-start];
+ System.arraycopy(sequence, start, reply, 0, end-start);
return reply;
}
+
/**
* make a new Sequence object from start to end (including gaps) over this seqeunce
* @param start int
{
start = 0;
}
- char[] seq = getSequence(start, end);
+ char [] seq = getSequence(start, end);
if (seq.length == 0)
{
return null;
// JBPNote - this is an incomplete copy.
SequenceI nseq = new Sequence(this.getName(), seq, nstart, nend);
nseq.setDescription(description);
- if (datasetSequence != null)
+ if (datasetSequence!=null)
{
- nseq.setDatasetSequence(datasetSequence);
+ nseq.setDatasetSequence(datasetSequence);
}
else
{
- nseq.setDatasetSequence(this);
+ nseq.setDatasetSequence(this);
}
return nseq;
}
}
/**
- * DOCUMENT ME!
+ * Return the alignment position for a sequence position
*
- * @param pos DOCUMENT ME!
+ * @param pos lying from start to end
*
- * @return DOCUMENT ME!
+ * @return aligned position of residue pos
*/
public int findIndex(int pos)
{
int seqlen = sequence.length;
while ( (j < i) && (j < seqlen))
{
- if (!jalview.util.Comparison.isGap(sequence[j]))
+ if (!jalview.util.Comparison.isGap( sequence[j] ))
{
pos++;
}
return;
}
- char[] tmp;
+ char [] tmp;
if (j >= sequence.length)
{
tmp = new char[i];
- System.arraycopy(sequence, 0, tmp, 0, i);
+ System.arraycopy(sequence,0,tmp,0,i);
}
else
{
- tmp = new char[sequence.length - j + i];
- System.arraycopy(sequence, 0, tmp, 0, i);
- System.arraycopy(sequence, j, tmp, i, sequence.length - j);
+ tmp = new char[sequence.length-j+i];
+ System.arraycopy(sequence,0,tmp,0,i);
+ System.arraycopy(sequence,j,tmp,i,sequence.length-j);
}
if (this.datasetSequence != null)
}
+
/**
* DOCUMENT ME!
*
*/
public void insertCharAt(int i, int length, char c)
{
- char[] tmp = new char[sequence.length + length];
+ char [] tmp = new char[sequence.length+length];
if (i >= sequence.length)
{
i = sequence.length;
}
else
- {
+ {
System.arraycopy(sequence, 0, tmp, 0, i);
- }
+ }
+
int index = i;
while (length > 0)
{
- tmp[index++] = c;
+ tmp[ index++ ] = c;
length--;
}
if (i < sequence.length)
{
- System.arraycopy(sequence, i, tmp, index, sequence.length - i);
+ System.arraycopy(sequence, i, tmp, index, sequence.length-i );
}
sequence = tmp;
int i, iSize = dbrefs.length;
- for (i = 0; i < iSize; i++)
+ for(i=0; i<iSize; i++)
{
- if (dbrefs[i].getAccessionId().equals(entry.getAccessionId())
- && dbrefs[i].getSource().equals(entry.getSource())
- && dbrefs[i].getVersion().equals(entry.getVersion()))
+ if(dbrefs[i].equals(entry))
{
return;
}
}
this.annotation.addElement(annotation);
+ annotation.setSequenceRef(this);
}
+ public void removeAlignmentAnnotation(AlignmentAnnotation annotation)
+ {
+ if(this.annotation!=null)
+ {
+ this.annotation.removeElement(annotation);
+ if(this.annotation.size()==0)
+ this.annotation = null;
+ }
+ }
+
+
/**
* test if this is a valid candidate for another
* sequence's dataset sequence.
*/
private boolean isValidDatasetSequence()
{
- if (datasetSequence != null)
+ if (datasetSequence!=null)
{
- return false;
+ return false;
}
- for (int i = 0; i < sequence.length; i++)
+ for (int i=0;i<sequence.length; i++)
{
- if (jalview.util.Comparison.isGap(sequence[i]))
+ if (jalview.util.Comparison.isGap(sequence[i]))
{
- return false;
+ return false;
}
}
- return true;
+ return true;
}
-
/* (non-Javadoc)
* @see jalview.datamodel.SequenceI#deriveSequence()
*/
public SequenceI deriveSequence()
{
- SequenceI seq = new Sequence(name, sequence, start, end);
- seq.setDescription(description);
+ SequenceI seq=new Sequence(this);
if (datasetSequence != null)
{
+ // duplicate current sequence with same dataset
seq.setDatasetSequence(datasetSequence);
}
else
{
if (isValidDatasetSequence())
{
+ // Use this as dataset sequence
seq.setDatasetSequence(this);
+ } else {
+ // Create a new, valid dataset sequence
+ SequenceI ds = seq;
+ ds.setSequence(AlignSeq.extractGaps(jalview.util.Comparison.GapChars, new String(sequence)));
+ setDatasetSequence(ds);
+ seq = this; // and return this sequence as the derived sequence.
}
}
return seq;
}
+ /* (non-Javadoc)
+ * @see jalview.datamodel.SequenceI#setAlignmentAnnotation(AlignmmentAnnotation[] annotations)
+ */
+ public void setAlignmentAnnotation(AlignmentAnnotation[] annotations)
+ {
+ if (annotation!=null) {
+ annotation.removeAllElements();
+ }
+ if (annotations!=null) {
+ for (int i=0; i<annotations.length; i++)
+ {
+ if (annotations[i]!=null)
+ addAlignmentAnnotation(annotations[i]);
+ }
+ }
+ }
+
+ /* (non-Javadoc)
+ * @see jalview.datamodel.SequenceI#getAnnotation(java.lang.String)
+ */
+ public AlignmentAnnotation[] getAnnotation(String label)
+ {
+ if (annotation==null || annotation.size()==0)
+ {
+ return null;
+ }
+
+ Vector subset = new Vector();
+ Enumeration e = annotation.elements();
+ while (e.hasMoreElements())
+ {
+ AlignmentAnnotation ann = (AlignmentAnnotation) e.nextElement();
+ if (ann.label!=null && ann.label.equals(label))
+ {
+ subset.addElement(ann);
+ }
+ }
+ if (subset.size()==0)
+ {
+ return null;
+ }
+ AlignmentAnnotation[] anns = new AlignmentAnnotation[subset.size()];
+ int i=0;
+ e = subset.elements();
+ while (e.hasMoreElements())
+ {
+ anns[i++] = (AlignmentAnnotation) e.nextElement();
+ }
+ subset.removeAllElements();
+ return anns;
+ }
}
+
+