2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 package jalview.datamodel;
24 import jalview.analysis.*;
28 * Implements the SequenceI interface for a char[] based sequence object.
36 SequenceI datasetSequence;
38 private char [] sequence;
46 /** This annotation is displayed below the alignment but the
47 * positions are tied to the residues of this sequence */
50 /** array of seuqence features - may not be null for a valid sequence object */
51 public SequenceFeature[] sequenceFeatures;
55 * Creates a new Sequence object.
57 * @param name display name string
58 * @param sequence string to form a possibly gapped sequence out of
59 * @param start first position of non-gap residue in the sequence
60 * @param end last position of ungapped residues (nearly always only used for display purposes)
62 public Sequence(String name, String sequence, int start, int end)
65 this.sequence = sequence.toCharArray();
72 public Sequence(String name, char [] sequence, int start, int end)
75 this.sequence = sequence;
82 com.stevesoft.pat.Regex limitrx = new com.stevesoft.pat.Regex(
83 "[/][0-9]{1,}[-][0-9]{1,}$");
84 com.stevesoft.pat.Regex endrx = new com.stevesoft.pat.Regex(
91 System.err.println("POSSIBLE IMPLEMENTATION ERROR: null sequence name passed to constructor.");
94 // Does sequence have the /start-end signiature?
95 if (limitrx.search(name))
97 name = limitrx.left();
98 endrx.search(limitrx.stringMatched());
99 setStart(Integer.parseInt(limitrx.stringMatched().substring(1,
100 endrx.matchedFrom() - 1)));
101 setEnd(Integer.parseInt(endrx.stringMatched()));
105 void checkValidRange()
110 for (int j = 0; j < sequence.length; j++)
112 if (!jalview.util.Comparison.isGap( sequence[j] ))
128 * Creates a new Sequence object.
130 * @param name DOCUMENT ME!
131 * @param sequence DOCUMENT ME!
133 public Sequence(String name, String sequence)
135 this(name, sequence, 1, -1);
139 * Creates a new Sequence object with new features, DBRefEntries, AlignmentAnnotations, and PDBIds
140 * but inherits any existing dataset sequence reference.
141 * @param seq DOCUMENT ME!
143 public Sequence(SequenceI seq)
145 this(seq, seq.getAnnotation());
148 * Create a new sequence object with new features, DBRefEntries, and PDBIds
149 * but inherits any existing dataset sequence reference, and duplicate of
150 * any annotation that is present in the given annotation array.
151 * @param seq the sequence to be copied
152 * @param alAnnotation an array of annotation including some associated with seq
154 public Sequence(SequenceI seq, AlignmentAnnotation[] alAnnotation)
160 description = seq.getDescription();
161 if (seq.getSequenceFeatures()!=null) {
162 SequenceFeature[] sf = seq.getSequenceFeatures();
163 for (int i=0; i<sf.length; i++) {
164 addSequenceFeature(new SequenceFeature(sf[i]));
167 if (seq.getDBRef()!=null) {
168 DBRefEntry[] dbr = seq.getDBRef();
169 for (int i=0; i<dbr.length; i++) {
170 addDBRef(new DBRefEntry(dbr[i]));
173 setDatasetSequence(seq.getDatasetSequence());
174 if (seq.getAnnotation()!=null) {
175 AlignmentAnnotation[] sqann = seq.getAnnotation();
176 for (int i=0;i<sqann.length; i++)
182 boolean found = (alAnnotation==null);
185 for (int apos = 0; !found && apos<alAnnotation.length; apos++)
187 found = (alAnnotation[apos] == sqann[i]);
192 // only copy the given annotation
193 AlignmentAnnotation newann = new AlignmentAnnotation(sqann[i]);
194 addAlignmentAnnotation(newann);
198 if (seq.getPDBId()!=null) {
199 Vector ids = seq.getPDBId();
200 Enumeration e = ids.elements();
201 while (e.hasMoreElements()) {
202 this.addPDBId(new PDBEntry((PDBEntry) e.nextElement()));
210 * @param v DOCUMENT ME!
212 public void setSequenceFeatures(SequenceFeature[] features)
214 sequenceFeatures = features;
217 public synchronized void addSequenceFeature(SequenceFeature sf)
219 if (sequenceFeatures == null)
221 sequenceFeatures = new SequenceFeature[0];
224 for (int i = 0; i < sequenceFeatures.length; i++)
226 if (sequenceFeatures[i].equals(sf))
232 SequenceFeature[] temp = new SequenceFeature[sequenceFeatures.length + 1];
233 System.arraycopy(sequenceFeatures, 0, temp, 0, sequenceFeatures.length);
234 temp[sequenceFeatures.length] = sf;
236 sequenceFeatures = temp;
239 public void deleteFeature(SequenceFeature sf)
241 if(sequenceFeatures==null)
247 for (index = 0; index < sequenceFeatures.length; index++)
249 if (sequenceFeatures[index].equals(sf))
256 if(index==sequenceFeatures.length)
261 int sfLength = sequenceFeatures.length;
264 sequenceFeatures = null;
268 SequenceFeature[] temp = new SequenceFeature[sfLength-1];
269 System.arraycopy(sequenceFeatures, 0, temp, 0, index);
273 System.arraycopy(sequenceFeatures,
276 index, sequenceFeatures.length - index -1);
279 sequenceFeatures = temp;
286 * @return DOCUMENT ME!
288 public SequenceFeature[] getSequenceFeatures()
290 return sequenceFeatures;
293 public void addPDBId(PDBEntry entry)
297 pdbIds = new Vector();
300 pdbIds.addElement(entry);
306 * @param id DOCUMENT ME!
308 public void setPDBId(Vector id)
316 * @return DOCUMENT ME!
318 public Vector getPDBId()
326 * @return DOCUMENT ME!
328 public String getDisplayId(boolean jvsuffix)
330 StringBuffer result = new StringBuffer(name);
333 result.append("/" + start + "-" + end);
336 return result.toString();
342 * @param name DOCUMENT ME!
344 public void setName(String name)
353 * @return DOCUMENT ME!
355 public String getName()
363 * @param start DOCUMENT ME!
365 public void setStart(int start)
373 * @return DOCUMENT ME!
375 public int getStart()
383 * @param end DOCUMENT ME!
385 public void setEnd(int end)
393 * @return DOCUMENT ME!
403 * @return DOCUMENT ME!
405 public int getLength()
407 return this.sequence.length;
413 * @param seq DOCUMENT ME!
415 public void setSequence(String seq)
417 this.sequence = seq.toCharArray();
422 public String getSequenceAsString()
424 return new String(sequence);
427 public String getSequenceAsString(int start, int end)
429 return new String(getSequence(start, end));
433 public char [] getSequence()
438 * @see jalview.datamodel.SequenceI#getSequence(int, int)
440 public char [] getSequence(int start, int end)
444 // JBPNote - left to user to pad the result here (TODO:Decide on this policy)
445 if (start >= sequence.length)
450 if (end >= sequence.length)
452 end = sequence.length;
455 char [] reply = new char[end-start];
456 System.arraycopy(sequence, start, reply, 0, end-start);
463 * make a new Sequence object from start to end (including gaps) over this seqeunce
468 public SequenceI getSubSequence(int start, int end)
474 char [] seq = getSequence(start, end);
479 int nstart = findPosition(start);
480 int nend = findPosition(end) - 1;
481 // JBPNote - this is an incomplete copy.
482 SequenceI nseq = new Sequence(this.getName(), seq, nstart, nend);
483 nseq.setDescription(description);
484 if (datasetSequence!=null)
486 nseq.setDatasetSequence(datasetSequence);
490 nseq.setDatasetSequence(this);
498 * @param i DOCUMENT ME!
500 * @return DOCUMENT ME!
502 public char getCharAt(int i)
504 if (i < sequence.length)
517 * @param desc DOCUMENT ME!
519 public void setDescription(String desc)
521 this.description = desc;
527 * @return DOCUMENT ME!
529 public String getDescription()
531 return this.description;
535 * Return the alignment position for a sequence position
537 * @param pos lying from start to end
539 * @return aligned position of residue pos
541 public int findIndex(int pos)
543 // returns the alignment position for a residue
547 while ( (i < sequence.length) && (j <= end) && (j <= pos))
549 if (!jalview.util.Comparison.isGap(sequence[i]))
557 if ( (j == end) && (j < pos))
568 * Returns the sequence position for an alignment position
570 * @param i column index in alignment (from 1)
572 * @return residue number for residue (left of and) nearest ith column
574 public int findPosition(int i)
578 int seqlen = sequence.length;
579 while ( (j < i) && (j < seqlen))
581 if (!jalview.util.Comparison.isGap( sequence[j] ))
593 * Returns an int array where indices correspond to each residue in the sequence and the element value gives its position in the alignment
595 * @return int[SequenceI.getEnd()-SequenceI.getStart()+1] or null if no residues in SequenceI object
597 public int[] gapMap()
599 String seq = jalview.analysis.AlignSeq.extractGaps(jalview.util.Comparison.
600 GapChars, new String(sequence));
601 int[] map = new int[seq.length()];
605 while (j < sequence.length)
607 if (!jalview.util.Comparison.isGap(sequence[j]))
619 * @see jalview.datamodel.SequenceI#deleteChars(int, int)
621 public void deleteChars(int i, int j)
623 int newstart=start,newend=end;
624 if (i >= sequence.length)
631 if (j >= sequence.length)
634 System.arraycopy(sequence,0,tmp,0,i);
638 tmp = new char[sequence.length-j+i];
639 System.arraycopy(sequence,0,tmp,0,i);
640 System.arraycopy(sequence,j,tmp,i,sequence.length-j);
642 boolean createNewDs=false;
643 for (int s = i; s < j; s++)
645 if (jalview.schemes.ResidueProperties.aaIndex[sequence[s]] != 23)
651 int sindex = findIndex(start)-1;
654 // delete characters including start of sequence
655 newstart = findPosition(j);
656 break; // don't need to search for any more residue characters.
658 // delete characters after start.
659 int eindex = findIndex(end)-1;
662 // delete characters at end of sequence
663 newend = findPosition(i-1);
664 break; // don't need to search for any more residue characters.
667 newend--; // decrease end position by one for the deleted residue and search further
673 // deletion occured in the middle of the sequence
674 if (createNewDs && this.datasetSequence != null)
676 // construct a new sequence
677 Sequence ds = new Sequence(datasetSequence);
678 // TODO: remove any non-inheritable properties ?
679 // TODO: create a sequence mapping (since there is a relation here ?)
680 ds.deleteChars(i, j);
681 datasetSequence = ds;
692 * @param i DOCUMENT ME!
693 * @param c DOCUMENT ME!
694 * @param chop DOCUMENT ME!
696 public void insertCharAt(int i, int length, char c)
698 char [] tmp = new char[sequence.length+length];
700 if (i >= sequence.length)
702 System.arraycopy(sequence, 0, tmp, 0, sequence.length);
707 System.arraycopy(sequence, 0, tmp, 0, i);
718 if (i < sequence.length)
720 System.arraycopy(sequence, i, tmp, index, sequence.length-i );
726 public void insertCharAt(int i, char c)
728 insertCharAt(i, 1, c);
731 public String getVamsasId()
736 public void setVamsasId(String id)
741 public void setDBRef(DBRefEntry[] dbref)
746 public DBRefEntry[] getDBRef()
751 public void addDBRef(DBRefEntry entry)
755 dbrefs = new DBRefEntry[0];
758 int i, iSize = dbrefs.length;
760 for(i=0; i<iSize; i++)
762 if(dbrefs[i].equalRef(entry))
764 if (entry.getMap()!=null)
766 if (dbrefs[i].getMap()==null)
768 // overwrite with 'superior' entry that contains a mapping.
776 DBRefEntry[] temp = new DBRefEntry[iSize + 1];
777 System.arraycopy(dbrefs, 0, temp, 0, iSize);
778 temp[temp.length - 1] = entry;
783 public void setDatasetSequence(SequenceI seq)
785 datasetSequence = seq;
788 public SequenceI getDatasetSequence()
790 return datasetSequence;
793 public AlignmentAnnotation[] getAnnotation()
795 if (annotation == null)
800 AlignmentAnnotation[] ret = new AlignmentAnnotation[annotation.size()];
801 for (int r = 0; r < ret.length; r++)
803 ret[r] = (AlignmentAnnotation) annotation.elementAt(r);
809 public void addAlignmentAnnotation(AlignmentAnnotation annotation)
811 if (this.annotation == null)
813 this.annotation = new Vector();
816 this.annotation.addElement(annotation);
817 annotation.setSequenceRef(this);
820 public void removeAlignmentAnnotation(AlignmentAnnotation annotation)
822 if(this.annotation!=null)
824 this.annotation.removeElement(annotation);
825 if(this.annotation.size()==0)
826 this.annotation = null;
832 * test if this is a valid candidate for another
833 * sequence's dataset sequence.
836 private boolean isValidDatasetSequence()
838 if (datasetSequence!=null)
842 for (int i=0;i<sequence.length; i++)
844 if (jalview.util.Comparison.isGap(sequence[i]))
852 * @see jalview.datamodel.SequenceI#deriveSequence()
854 public SequenceI deriveSequence()
856 SequenceI seq=new Sequence(this);
857 if (datasetSequence != null)
859 // duplicate current sequence with same dataset
860 seq.setDatasetSequence(datasetSequence);
864 if (isValidDatasetSequence())
866 // Use this as dataset sequence
867 seq.setDatasetSequence(this);
869 // Create a new, valid dataset sequence
871 ds.setSequence(AlignSeq.extractGaps(jalview.util.Comparison.GapChars, new String(sequence)));
872 setDatasetSequence(ds);
873 ds.setSequenceFeatures(getSequenceFeatures());
874 seq = this; // and return this sequence as the derived sequence.
881 * @see jalview.datamodel.SequenceI#createDatasetSequence()
883 public SequenceI createDatasetSequence()
885 if (datasetSequence==null)
887 datasetSequence = new Sequence(getName(),
888 AlignSeq.extractGaps(
889 jalview.util.Comparison.GapChars,
890 getSequenceAsString()),
893 datasetSequence.setSequenceFeatures(getSequenceFeatures());
894 datasetSequence.setDescription(getDescription());
895 setSequenceFeatures(null);
896 // move database references onto dataset sequence
897 datasetSequence.setDBRef(getDBRef());
899 datasetSequence.setPDBId(getPDBId());
901 datasetSequence.updatePDBIds();
903 return datasetSequence;
906 * @see jalview.datamodel.SequenceI#setAlignmentAnnotation(AlignmmentAnnotation[] annotations)
908 public void setAlignmentAnnotation(AlignmentAnnotation[] annotations)
910 if (annotation!=null) {
911 annotation.removeAllElements();
913 if (annotations!=null) {
914 for (int i=0; i<annotations.length; i++)
916 if (annotations[i]!=null)
917 addAlignmentAnnotation(annotations[i]);
923 * @see jalview.datamodel.SequenceI#getAnnotation(java.lang.String)
925 public AlignmentAnnotation[] getAnnotation(String label)
927 if (annotation==null || annotation.size()==0)
932 Vector subset = new Vector();
933 Enumeration e = annotation.elements();
934 while (e.hasMoreElements())
936 AlignmentAnnotation ann = (AlignmentAnnotation) e.nextElement();
937 if (ann.label!=null && ann.label.equals(label))
939 subset.addElement(ann);
942 if (subset.size()==0)
946 AlignmentAnnotation[] anns = new AlignmentAnnotation[subset.size()];
948 e = subset.elements();
949 while (e.hasMoreElements())
951 anns[i++] = (AlignmentAnnotation) e.nextElement();
953 subset.removeAllElements();
957 public boolean updatePDBIds()
959 if (datasetSequence!=null)
961 // TODO: could merge DBRefs
962 return datasetSequence.updatePDBIds();
964 if (dbrefs==null || dbrefs.length==0)
968 Vector newpdb = new Vector();
969 for (int i=0; i<dbrefs.length;i++)
971 if (DBRefSource.PDB.equals(dbrefs[i].getSource()))
973 PDBEntry pdbe = new PDBEntry();
974 pdbe.setId(dbrefs[i].getAccessionId());
975 if (pdbIds==null || pdbIds.size()==0)
977 newpdb.addElement(pdbe);
979 Enumeration en = pdbIds.elements();
980 boolean matched=false;
981 while (!matched && en.hasMoreElements())
983 PDBEntry anentry = (PDBEntry) en.nextElement();
984 if (anentry.getId().equals(pdbe.getId()))
991 newpdb.addElement(pdbe);
998 Enumeration en = newpdb.elements();
999 while (en.hasMoreElements())
1001 addPDBId((PDBEntry) en.nextElement());