2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3 * Copyright (C) 2008 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;
23 import jalview.analysis.*;
27 * Implements the SequenceI interface for a char[] based sequence object.
32 public class Sequence implements SequenceI
34 SequenceI datasetSequence;
38 private char[] sequence;
53 * This annotation is displayed below the alignment but the positions are tied
54 * to the residues of this sequence
58 /** array of seuqence features - may not be null for a valid sequence object */
59 public SequenceFeature[] sequenceFeatures;
62 * Creates a new Sequence object.
67 * string to form a possibly gapped sequence out of
69 * first position of non-gap residue in the sequence
71 * last position of ungapped residues (nearly always only used
72 * for display purposes)
74 public Sequence(String name, String sequence, int start, int end)
77 this.sequence = sequence.toCharArray();
84 public Sequence(String name, char[] sequence, int start, int end)
87 this.sequence = sequence;
94 com.stevesoft.pat.Regex limitrx = new com.stevesoft.pat.Regex(
95 "[/][0-9]{1,}[-][0-9]{1,}$");
97 com.stevesoft.pat.Regex endrx = new com.stevesoft.pat.Regex("[0-9]{1,}$");
104 .println("POSSIBLE IMPLEMENTATION ERROR: null sequence name passed to constructor.");
107 // Does sequence have the /start-end signiature?
108 if (limitrx.search(name))
110 name = limitrx.left();
111 endrx.search(limitrx.stringMatched());
112 setStart(Integer.parseInt(limitrx.stringMatched().substring(1,
113 endrx.matchedFrom() - 1)));
114 setEnd(Integer.parseInt(endrx.stringMatched()));
118 void checkValidRange()
123 for (int j = 0; j < sequence.length; j++)
125 if (!jalview.util.Comparison.isGap(sequence[j]))
141 * Creates a new Sequence object.
148 public Sequence(String name, String sequence)
150 this(name, sequence, 1, -1);
154 * Creates a new Sequence object with new features, DBRefEntries,
155 * AlignmentAnnotations, and PDBIds but inherits any existing dataset sequence
161 public Sequence(SequenceI seq)
163 this(seq, seq.getAnnotation());
167 * Create a new sequence object with new features, DBRefEntries, and PDBIds
168 * but inherits any existing dataset sequence reference, and duplicate of any
169 * annotation that is present in the given annotation array.
172 * the sequence to be copied
173 * @param alAnnotation
174 * an array of annotation including some associated with seq
176 public Sequence(SequenceI seq, AlignmentAnnotation[] alAnnotation)
178 this(seq.getName(), seq.getSequence(), seq.getStart(), seq.getEnd());
179 description = seq.getDescription();
180 if (seq.getSequenceFeatures() != null)
182 SequenceFeature[] sf = seq.getSequenceFeatures();
183 for (int i = 0; i < sf.length; i++)
185 addSequenceFeature(new SequenceFeature(sf[i]));
188 setDatasetSequence(seq.getDatasetSequence());
189 if (datasetSequence == null && seq.getDBRef() != null)
191 // only copy DBRefs if we really are a dataset sequence
192 DBRefEntry[] dbr = seq.getDBRef();
193 for (int i = 0; i < dbr.length; i++)
195 addDBRef(new DBRefEntry(dbr[i]));
198 if (seq.getAnnotation() != null)
200 AlignmentAnnotation[] sqann = seq.getAnnotation();
201 for (int i = 0; i < sqann.length; i++)
203 if (sqann[i] == null)
207 boolean found = (alAnnotation == null);
210 for (int apos = 0; !found && apos < alAnnotation.length; apos++)
212 found = (alAnnotation[apos] == sqann[i]);
217 // only copy the given annotation
218 AlignmentAnnotation newann = new AlignmentAnnotation(sqann[i]);
219 addAlignmentAnnotation(newann);
223 if (seq.getPDBId() != null)
225 Vector ids = seq.getPDBId();
226 Enumeration e = ids.elements();
227 while (e.hasMoreElements())
229 this.addPDBId(new PDBEntry((PDBEntry) e.nextElement()));
240 public void setSequenceFeatures(SequenceFeature[] features)
242 sequenceFeatures = features;
245 public synchronized void addSequenceFeature(SequenceFeature sf)
247 if (sequenceFeatures == null)
249 sequenceFeatures = new SequenceFeature[0];
252 for (int i = 0; i < sequenceFeatures.length; i++)
254 if (sequenceFeatures[i].equals(sf))
260 SequenceFeature[] temp = new SequenceFeature[sequenceFeatures.length + 1];
261 System.arraycopy(sequenceFeatures, 0, temp, 0, sequenceFeatures.length);
262 temp[sequenceFeatures.length] = sf;
264 sequenceFeatures = temp;
267 public void deleteFeature(SequenceFeature sf)
269 if (sequenceFeatures == null)
275 for (index = 0; index < sequenceFeatures.length; index++)
277 if (sequenceFeatures[index].equals(sf))
283 if (index == sequenceFeatures.length)
288 int sfLength = sequenceFeatures.length;
291 sequenceFeatures = null;
295 SequenceFeature[] temp = new SequenceFeature[sfLength - 1];
296 System.arraycopy(sequenceFeatures, 0, temp, 0, index);
298 if (index < sfLength)
300 System.arraycopy(sequenceFeatures, index + 1, temp, index,
301 sequenceFeatures.length - index - 1);
304 sequenceFeatures = temp;
311 * @return DOCUMENT ME!
313 public SequenceFeature[] getSequenceFeatures()
315 return sequenceFeatures;
318 public void addPDBId(PDBEntry entry)
322 pdbIds = new Vector();
324 if (!pdbIds.contains(entry))
326 pdbIds.addElement(entry);
336 public void setPDBId(Vector id)
344 * @return DOCUMENT ME!
346 public Vector getPDBId()
354 * @return DOCUMENT ME!
356 public String getDisplayId(boolean jvsuffix)
358 StringBuffer result = new StringBuffer(name);
361 result.append("/" + start + "-" + end);
364 return result.toString();
373 public void setName(String name)
382 * @return DOCUMENT ME!
384 public String getName()
395 public void setStart(int start)
403 * @return DOCUMENT ME!
405 public int getStart()
416 public void setEnd(int end)
424 * @return DOCUMENT ME!
434 * @return DOCUMENT ME!
436 public int getLength()
438 return this.sequence.length;
447 public void setSequence(String seq)
449 this.sequence = seq.toCharArray();
453 public String getSequenceAsString()
455 return new String(sequence);
458 public String getSequenceAsString(int start, int end)
460 return new String(getSequence(start, end));
463 public char[] getSequence()
471 * @see jalview.datamodel.SequenceI#getSequence(int, int)
473 public char[] getSequence(int start, int end)
477 // JBPNote - left to user to pad the result here (TODO:Decide on this
479 if (start >= sequence.length)
484 if (end >= sequence.length)
486 end = sequence.length;
489 char[] reply = new char[end - start];
490 System.arraycopy(sequence, start, reply, 0, end - start);
496 * make a new Sequence object from start to end (including gaps) over this
505 public SequenceI getSubSequence(int start, int end)
511 char[] seq = getSequence(start, end);
516 int nstart = findPosition(start);
517 int nend = findPosition(end) - 1;
518 // JBPNote - this is an incomplete copy.
519 SequenceI nseq = new Sequence(this.getName(), seq, nstart, nend);
520 nseq.setDescription(description);
521 if (datasetSequence != null)
523 nseq.setDatasetSequence(datasetSequence);
527 nseq.setDatasetSequence(this);
538 * @return DOCUMENT ME!
540 public char getCharAt(int i)
542 if (i < sequence.length)
558 public void setDescription(String desc)
560 this.description = desc;
566 * @return DOCUMENT ME!
568 public String getDescription()
570 return this.description;
574 * Return the alignment position for a sequence position
577 * lying from start to end
579 * @return aligned position of residue pos
581 public int findIndex(int pos)
583 // returns the alignment position for a residue
587 while ((i < sequence.length) && (j <= end) && (j <= pos))
589 if (!jalview.util.Comparison.isGap(sequence[i]))
597 if ((j == end) && (j < pos))
608 * Returns the sequence position for an alignment position
611 * column index in alignment (from 1)
613 * @return residue number for residue (left of and) nearest ith column
615 public int findPosition(int i)
619 int seqlen = sequence.length;
620 while ((j < i) && (j < seqlen))
622 if (!jalview.util.Comparison.isGap(sequence[j]))
634 * Returns an int array where indices correspond to each residue in the
635 * sequence and the element value gives its position in the alignment
637 * @return int[SequenceI.getEnd()-SequenceI.getStart()+1] or null if no
638 * residues in SequenceI object
640 public int[] gapMap()
642 String seq = jalview.analysis.AlignSeq.extractGaps(
643 jalview.util.Comparison.GapChars, new String(sequence));
644 int[] map = new int[seq.length()];
648 while (j < sequence.length)
650 if (!jalview.util.Comparison.isGap(sequence[j]))
664 * @see jalview.datamodel.SequenceI#findPositionMap()
666 public int[] findPositionMap()
668 int map[] = new int[sequence.length];
671 int seqlen = sequence.length;
675 if (!jalview.util.Comparison.isGap(sequence[j]))
688 * @see jalview.datamodel.SequenceI#deleteChars(int, int)
690 public void deleteChars(int i, int j)
692 int newstart = start, newend = end;
693 if (i >= sequence.length)
700 if (j >= sequence.length)
703 System.arraycopy(sequence, 0, tmp, 0, i);
707 tmp = new char[sequence.length - j + i];
708 System.arraycopy(sequence, 0, tmp, 0, i);
709 System.arraycopy(sequence, j, tmp, i, sequence.length - j);
711 boolean createNewDs = false;
712 for (int s = i; s < j; s++)
714 if (jalview.schemes.ResidueProperties.aaIndex[sequence[s]] != 23)
722 int sindex = findIndex(start) - 1;
725 // delete characters including start of sequence
726 newstart = findPosition(j);
727 break; // don't need to search for any more residue characters.
731 // delete characters after start.
732 int eindex = findIndex(end) - 1;
735 // delete characters at end of sequence
736 newend = findPosition(i - 1);
737 break; // don't need to search for any more residue characters.
742 newend--; // decrease end position by one for the deleted residue
743 // and search further
749 // deletion occured in the middle of the sequence
750 if (createNewDs && this.datasetSequence != null)
752 // construct a new sequence
753 Sequence ds = new Sequence(datasetSequence);
754 // TODO: remove any non-inheritable properties ?
755 // TODO: create a sequence mapping (since there is a relation here ?)
756 ds.deleteChars(i, j);
757 datasetSequence = ds;
774 public void insertCharAt(int i, int length, char c)
776 char[] tmp = new char[sequence.length + length];
778 if (i >= sequence.length)
780 System.arraycopy(sequence, 0, tmp, 0, sequence.length);
785 System.arraycopy(sequence, 0, tmp, 0, i);
795 if (i < sequence.length)
797 System.arraycopy(sequence, i, tmp, index, sequence.length - i);
803 public void insertCharAt(int i, char c)
805 insertCharAt(i, 1, c);
808 public String getVamsasId()
813 public void setVamsasId(String id)
818 public void setDBRef(DBRefEntry[] dbref)
823 public DBRefEntry[] getDBRef()
825 if (dbrefs == null && datasetSequence != null
826 && this != datasetSequence)
828 return datasetSequence.getDBRef();
833 public void addDBRef(DBRefEntry entry)
837 dbrefs = new DBRefEntry[0];
840 int i, iSize = dbrefs.length;
842 for (i = 0; i < iSize; i++)
844 if (dbrefs[i].equalRef(entry))
846 if (entry.getMap() != null)
848 if (dbrefs[i].getMap() == null)
850 // overwrite with 'superior' entry that contains a mapping.
858 DBRefEntry[] temp = new DBRefEntry[iSize + 1];
859 System.arraycopy(dbrefs, 0, temp, 0, iSize);
860 temp[temp.length - 1] = entry;
865 public void setDatasetSequence(SequenceI seq)
867 datasetSequence = seq;
870 public SequenceI getDatasetSequence()
872 return datasetSequence;
875 public AlignmentAnnotation[] getAnnotation()
877 if (annotation == null)
882 AlignmentAnnotation[] ret = new AlignmentAnnotation[annotation.size()];
883 for (int r = 0; r < ret.length; r++)
885 ret[r] = (AlignmentAnnotation) annotation.elementAt(r);
891 public void addAlignmentAnnotation(AlignmentAnnotation annotation)
893 if (this.annotation == null)
895 this.annotation = new Vector();
898 this.annotation.addElement(annotation);
899 annotation.setSequenceRef(this);
902 public void removeAlignmentAnnotation(AlignmentAnnotation annotation)
904 if (this.annotation != null)
906 this.annotation.removeElement(annotation);
907 if (this.annotation.size() == 0)
908 this.annotation = null;
913 * test if this is a valid candidate for another sequence's dataset sequence.
916 private boolean isValidDatasetSequence()
918 if (datasetSequence != null)
922 for (int i = 0; i < sequence.length; i++)
924 if (jalview.util.Comparison.isGap(sequence[i]))
935 * @see jalview.datamodel.SequenceI#deriveSequence()
937 public SequenceI deriveSequence()
939 SequenceI seq = new Sequence(this);
940 if (datasetSequence != null)
942 // duplicate current sequence with same dataset
943 seq.setDatasetSequence(datasetSequence);
947 if (isValidDatasetSequence())
949 // Use this as dataset sequence
950 seq.setDatasetSequence(this);
954 // Create a new, valid dataset sequence
956 ds.setSequence(AlignSeq.extractGaps(
957 jalview.util.Comparison.GapChars, new String(sequence)));
958 setDatasetSequence(ds);
959 ds.setSequenceFeatures(getSequenceFeatures());
960 seq = this; // and return this sequence as the derived sequence.
969 * @see jalview.datamodel.SequenceI#createDatasetSequence()
971 public SequenceI createDatasetSequence()
973 if (datasetSequence == null)
975 datasetSequence = new Sequence(getName(), AlignSeq.extractGaps(
976 jalview.util.Comparison.GapChars, getSequenceAsString()),
977 getStart(), getEnd());
978 datasetSequence.setSequenceFeatures(getSequenceFeatures());
979 datasetSequence.setDescription(getDescription());
980 setSequenceFeatures(null);
981 // move database references onto dataset sequence
982 datasetSequence.setDBRef(getDBRef());
984 datasetSequence.setPDBId(getPDBId());
986 datasetSequence.updatePDBIds();
988 return datasetSequence;
994 * @see jalview.datamodel.SequenceI#setAlignmentAnnotation(AlignmmentAnnotation[]
997 public void setAlignmentAnnotation(AlignmentAnnotation[] annotations)
999 if (annotation != null)
1001 annotation.removeAllElements();
1003 if (annotations != null)
1005 for (int i = 0; i < annotations.length; i++)
1007 if (annotations[i] != null)
1008 addAlignmentAnnotation(annotations[i]);
1016 * @see jalview.datamodel.SequenceI#getAnnotation(java.lang.String)
1018 public AlignmentAnnotation[] getAnnotation(String label)
1020 if (annotation == null || annotation.size() == 0)
1025 Vector subset = new Vector();
1026 Enumeration e = annotation.elements();
1027 while (e.hasMoreElements())
1029 AlignmentAnnotation ann = (AlignmentAnnotation) e.nextElement();
1030 if (ann.label != null && ann.label.equals(label))
1032 subset.addElement(ann);
1035 if (subset.size() == 0)
1039 AlignmentAnnotation[] anns = new AlignmentAnnotation[subset.size()];
1041 e = subset.elements();
1042 while (e.hasMoreElements())
1044 anns[i++] = (AlignmentAnnotation) e.nextElement();
1046 subset.removeAllElements();
1050 public boolean updatePDBIds()
1052 if (datasetSequence != null)
1054 // TODO: could merge DBRefs
1055 return datasetSequence.updatePDBIds();
1057 if (dbrefs == null || dbrefs.length == 0)
1061 Vector newpdb = new Vector();
1062 for (int i = 0; i < dbrefs.length; i++)
1064 if (DBRefSource.PDB.equals(dbrefs[i].getSource()))
1066 PDBEntry pdbe = new PDBEntry();
1067 pdbe.setId(dbrefs[i].getAccessionId());
1068 if (pdbIds == null || pdbIds.size() == 0)
1070 newpdb.addElement(pdbe);
1074 Enumeration en = pdbIds.elements();
1075 boolean matched = false;
1076 while (!matched && en.hasMoreElements())
1078 PDBEntry anentry = (PDBEntry) en.nextElement();
1079 if (anentry.getId().equals(pdbe.getId()))
1086 newpdb.addElement(pdbe);
1091 if (newpdb.size() > 0)
1093 Enumeration en = newpdb.elements();
1094 while (en.hasMoreElements())
1096 addPDBId((PDBEntry) en.nextElement());
1106 * @see jalview.datamodel.SequenceI#transferAnnotation(jalview.datamodel.SequenceI,
1107 * jalview.datamodel.Mapping)
1109 public void transferAnnotation(SequenceI entry, Mapping mp)
1111 if (datasetSequence != null)
1113 datasetSequence.transferAnnotation(entry, mp);
1116 if (entry.getDatasetSequence() != null)
1118 transferAnnotation(entry.getDatasetSequence(), mp);
1121 // transfer any new features from entry onto sequence
1122 if (entry.getSequenceFeatures() != null)
1125 SequenceFeature[] sfs = entry.getSequenceFeatures();
1126 for (int si = 0; si < sfs.length; si++)
1128 SequenceFeature sf[] = (mp != null) ? mp.locateFeature(sfs[si])
1129 : new SequenceFeature[]
1130 { new SequenceFeature(sfs[si]) };
1131 if (sf != null && sf.length > 0)
1133 for (int sfi = 0; sfi < sf.length; sfi++)
1135 addSequenceFeature(sf[sfi]);
1141 // transfer PDB entries
1142 if (entry.getPDBId() != null)
1144 Enumeration e = entry.getPDBId().elements();
1145 while (e.hasMoreElements())
1147 PDBEntry pdb = (PDBEntry) e.nextElement();
1151 // transfer database references
1152 DBRefEntry[] entryRefs = entry.getDBRef();
1153 if (entryRefs != null)
1155 for (int r = 0; r < entryRefs.length; r++)
1157 DBRefEntry newref = new DBRefEntry(entryRefs[r]);
1158 if (newref.getMap() != null && mp != null)
1160 // remap ref using our local mapping
1162 // we also assume all version string setting is done by dbSourceProxy
1164 * if (!newref.getSource().equalsIgnoreCase(dbSource)) {
1165 * newref.setSource(dbSource); }