2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6)
3 * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
18 package jalview.datamodel;
22 import jalview.analysis.*;
26 * Implements the SequenceI interface for a char[] based sequence object.
31 public class Sequence implements SequenceI
33 SequenceI datasetSequence;
37 private char[] sequence;
52 * This annotation is displayed below the alignment but the positions are tied
53 * to the residues of this sequence
57 /** array of seuqence features - may not be null for a valid sequence object */
58 public SequenceFeature[] sequenceFeatures;
61 * Creates a new Sequence object.
66 * string to form a possibly gapped sequence out of
68 * first position of non-gap residue in the sequence
70 * last position of ungapped residues (nearly always only used for
73 public Sequence(String name, String sequence, int start, int end)
76 this.sequence = sequence.toCharArray();
83 public Sequence(String name, char[] sequence, int start, int end)
86 this.sequence = sequence;
93 com.stevesoft.pat.Regex limitrx = new com.stevesoft.pat.Regex(
94 "[/][0-9]{1,}[-][0-9]{1,}$");
96 com.stevesoft.pat.Regex endrx = new com.stevesoft.pat.Regex("[0-9]{1,}$");
103 .println("POSSIBLE IMPLEMENTATION ERROR: null sequence name passed to constructor.");
106 // Does sequence have the /start-end signiature?
107 if (limitrx.search(name))
109 name = limitrx.left();
110 endrx.search(limitrx.stringMatched());
111 setStart(Integer.parseInt(limitrx.stringMatched().substring(1,
112 endrx.matchedFrom() - 1)));
113 setEnd(Integer.parseInt(endrx.stringMatched()));
117 void checkValidRange()
122 for (int j = 0; j < sequence.length; j++)
124 if (!jalview.util.Comparison.isGap(sequence[j]))
140 * Creates a new Sequence object.
147 public Sequence(String name, String sequence)
149 this(name, sequence, 1, -1);
153 * Creates a new Sequence object with new features, DBRefEntries,
154 * AlignmentAnnotations, and PDBIds but inherits any existing dataset sequence
160 public Sequence(SequenceI seq)
162 this(seq, seq.getAnnotation());
166 * Create a new sequence object with new features, DBRefEntries, and PDBIds
167 * but inherits any existing dataset sequence reference, and duplicate of any
168 * annotation that is present in the given annotation array.
171 * the sequence to be copied
172 * @param alAnnotation
173 * an array of annotation including some associated with seq
175 public Sequence(SequenceI seq, AlignmentAnnotation[] alAnnotation)
177 this(seq.getName(), seq.getSequence(), seq.getStart(), seq.getEnd());
178 description = seq.getDescription();
179 if (seq.getSequenceFeatures() != null)
181 SequenceFeature[] sf = seq.getSequenceFeatures();
182 for (int i = 0; i < sf.length; i++)
184 addSequenceFeature(new SequenceFeature(sf[i]));
187 setDatasetSequence(seq.getDatasetSequence());
188 if (datasetSequence == null && seq.getDBRef() != null)
190 // only copy DBRefs if we really are a dataset sequence
191 DBRefEntry[] dbr = seq.getDBRef();
192 for (int i = 0; i < dbr.length; i++)
194 addDBRef(new DBRefEntry(dbr[i]));
197 if (seq.getAnnotation() != null)
199 AlignmentAnnotation[] sqann = seq.getAnnotation();
200 for (int i = 0; i < sqann.length; i++)
202 if (sqann[i] == null)
206 boolean found = (alAnnotation == null);
209 for (int apos = 0; !found && apos < alAnnotation.length; apos++)
211 found = (alAnnotation[apos] == sqann[i]);
216 // only copy the given annotation
217 AlignmentAnnotation newann = new AlignmentAnnotation(sqann[i]);
218 addAlignmentAnnotation(newann);
222 if (seq.getPDBId() != null)
224 Vector ids = seq.getPDBId();
225 Enumeration e = ids.elements();
226 while (e.hasMoreElements())
228 this.addPDBId(new PDBEntry((PDBEntry) e.nextElement()));
239 public void setSequenceFeatures(SequenceFeature[] features)
241 sequenceFeatures = features;
244 public synchronized void addSequenceFeature(SequenceFeature sf)
246 if (sequenceFeatures == null)
248 sequenceFeatures = new SequenceFeature[0];
251 for (int i = 0; i < sequenceFeatures.length; i++)
253 if (sequenceFeatures[i].equals(sf))
259 SequenceFeature[] temp = new SequenceFeature[sequenceFeatures.length + 1];
260 System.arraycopy(sequenceFeatures, 0, temp, 0, sequenceFeatures.length);
261 temp[sequenceFeatures.length] = sf;
263 sequenceFeatures = temp;
266 public void deleteFeature(SequenceFeature sf)
268 if (sequenceFeatures == null)
274 for (index = 0; index < sequenceFeatures.length; index++)
276 if (sequenceFeatures[index].equals(sf))
282 if (index == sequenceFeatures.length)
287 int sfLength = sequenceFeatures.length;
290 sequenceFeatures = null;
294 SequenceFeature[] temp = new SequenceFeature[sfLength - 1];
295 System.arraycopy(sequenceFeatures, 0, temp, 0, index);
297 if (index < sfLength)
299 System.arraycopy(sequenceFeatures, index + 1, temp, index,
300 sequenceFeatures.length - index - 1);
303 sequenceFeatures = temp;
310 * @return DOCUMENT ME!
312 public SequenceFeature[] getSequenceFeatures()
314 return sequenceFeatures;
317 public void addPDBId(PDBEntry entry)
321 pdbIds = new Vector();
323 if (!pdbIds.contains(entry))
325 pdbIds.addElement(entry);
335 public void setPDBId(Vector id)
343 * @return DOCUMENT ME!
345 public Vector getPDBId()
353 * @return DOCUMENT ME!
355 public String getDisplayId(boolean jvsuffix)
357 StringBuffer result = new StringBuffer(name);
360 result.append("/" + start + "-" + end);
363 return result.toString();
372 public void setName(String name)
381 * @return DOCUMENT ME!
383 public String getName()
394 public void setStart(int start)
402 * @return DOCUMENT ME!
404 public int getStart()
415 public void setEnd(int end)
423 * @return DOCUMENT ME!
433 * @return DOCUMENT ME!
435 public int getLength()
437 return this.sequence.length;
446 public void setSequence(String seq)
448 this.sequence = seq.toCharArray();
452 public String getSequenceAsString()
454 return new String(sequence);
457 public String getSequenceAsString(int start, int end)
459 return new String(getSequence(start, end));
462 public char[] getSequence()
470 * @see jalview.datamodel.SequenceI#getSequence(int, int)
472 public char[] getSequence(int start, int end)
476 // JBPNote - left to user to pad the result here (TODO:Decide on this
478 if (start >= sequence.length)
483 if (end >= sequence.length)
485 end = sequence.length;
488 char[] reply = new char[end - start];
489 System.arraycopy(sequence, start, reply, 0, end - start);
495 * make a new Sequence object from start to end (including gaps) over this
504 public SequenceI getSubSequence(int start, int end)
510 char[] seq = getSequence(start, end);
515 int nstart = findPosition(start);
516 int nend = findPosition(end) - 1;
517 // JBPNote - this is an incomplete copy.
518 SequenceI nseq = new Sequence(this.getName(), seq, nstart, nend);
519 nseq.setDescription(description);
520 if (datasetSequence != null)
522 nseq.setDatasetSequence(datasetSequence);
526 nseq.setDatasetSequence(this);
537 * @return DOCUMENT ME!
539 public char getCharAt(int i)
541 if (i < sequence.length)
557 public void setDescription(String desc)
559 this.description = desc;
565 * @return DOCUMENT ME!
567 public String getDescription()
569 return this.description;
573 * Return the alignment position for a sequence position
576 * lying from start to end
578 * @return aligned position of residue pos
580 public int findIndex(int pos)
582 // returns the alignment position for a residue
586 while ((i < sequence.length) && (j <= end) && (j <= pos))
588 if (!jalview.util.Comparison.isGap(sequence[i]))
596 if ((j == end) && (j < pos))
607 * Returns the sequence position for an alignment position
610 * column index in alignment (from 1)
612 * @return residue number for residue (left of and) nearest ith column
614 public int findPosition(int i)
618 int seqlen = sequence.length;
619 while ((j < i) && (j < seqlen))
621 if (!jalview.util.Comparison.isGap(sequence[j]))
633 * Returns an int array where indices correspond to each residue in the
634 * sequence and the element value gives its position in the alignment
636 * @return int[SequenceI.getEnd()-SequenceI.getStart()+1] or null if no
637 * residues in SequenceI object
639 public int[] gapMap()
641 String seq = jalview.analysis.AlignSeq.extractGaps(
642 jalview.util.Comparison.GapChars, new String(sequence));
643 int[] map = new int[seq.length()];
647 while (j < sequence.length)
649 if (!jalview.util.Comparison.isGap(sequence[j]))
663 * @see jalview.datamodel.SequenceI#findPositionMap()
665 public int[] findPositionMap()
667 int map[] = new int[sequence.length];
670 int seqlen = sequence.length;
674 if (!jalview.util.Comparison.isGap(sequence[j]))
687 * @see jalview.datamodel.SequenceI#deleteChars(int, int)
689 public void deleteChars(int i, int j)
691 int newstart = start, newend = end;
692 if (i >= sequence.length)
699 if (j >= sequence.length)
702 System.arraycopy(sequence, 0, tmp, 0, i);
706 tmp = new char[sequence.length - j + i];
707 System.arraycopy(sequence, 0, tmp, 0, i);
708 System.arraycopy(sequence, j, tmp, i, sequence.length - j);
710 boolean createNewDs = false;
711 for (int s = i; s < j; s++)
713 if (jalview.schemes.ResidueProperties.aaIndex[sequence[s]] != 23)
721 int sindex = findIndex(start) - 1;
724 // delete characters including start of sequence
725 newstart = findPosition(j);
726 break; // don't need to search for any more residue characters.
730 // delete characters after start.
731 int eindex = findIndex(end) - 1;
734 // delete characters at end of sequence
735 newend = findPosition(i - 1);
736 break; // don't need to search for any more residue characters.
741 newend--; // decrease end position by one for the deleted residue
742 // and search further
748 // deletion occured in the middle of the sequence
749 if (createNewDs && this.datasetSequence != null)
751 // construct a new sequence
752 Sequence ds = new Sequence(datasetSequence);
753 // TODO: remove any non-inheritable properties ?
754 // TODO: create a sequence mapping (since there is a relation here ?)
755 ds.deleteChars(i, j);
756 datasetSequence = ds;
773 public void insertCharAt(int i, int length, char c)
775 char[] tmp = new char[sequence.length + length];
777 if (i >= sequence.length)
779 System.arraycopy(sequence, 0, tmp, 0, sequence.length);
784 System.arraycopy(sequence, 0, tmp, 0, i);
794 if (i < sequence.length)
796 System.arraycopy(sequence, i, tmp, index, sequence.length - i);
802 public void insertCharAt(int i, char c)
804 insertCharAt(i, 1, c);
807 public String getVamsasId()
812 public void setVamsasId(String id)
817 public void setDBRef(DBRefEntry[] dbref)
822 public DBRefEntry[] getDBRef()
824 if (dbrefs == null && datasetSequence != null
825 && this != datasetSequence)
827 return datasetSequence.getDBRef();
832 public void addDBRef(DBRefEntry entry)
836 dbrefs = new DBRefEntry[0];
839 int i, iSize = dbrefs.length;
841 for (i = 0; i < iSize; i++)
843 if (dbrefs[i].equalRef(entry))
845 if (entry.getMap() != null)
847 if (dbrefs[i].getMap() == null)
849 // overwrite with 'superior' entry that contains a mapping.
857 DBRefEntry[] temp = new DBRefEntry[iSize + 1];
858 System.arraycopy(dbrefs, 0, temp, 0, iSize);
859 temp[temp.length - 1] = entry;
864 public void setDatasetSequence(SequenceI seq)
866 datasetSequence = seq;
869 public SequenceI getDatasetSequence()
871 return datasetSequence;
874 public AlignmentAnnotation[] getAnnotation()
876 if (annotation == null)
881 AlignmentAnnotation[] ret = new AlignmentAnnotation[annotation.size()];
882 for (int r = 0; r < ret.length; r++)
884 ret[r] = (AlignmentAnnotation) annotation.elementAt(r);
890 public void addAlignmentAnnotation(AlignmentAnnotation annotation)
892 if (this.annotation == null)
894 this.annotation = new Vector();
897 this.annotation.addElement(annotation);
898 annotation.setSequenceRef(this);
901 public void removeAlignmentAnnotation(AlignmentAnnotation annotation)
903 if (this.annotation != null)
905 this.annotation.removeElement(annotation);
906 if (this.annotation.size() == 0)
907 this.annotation = null;
912 * test if this is a valid candidate for another sequence's dataset sequence.
915 private boolean isValidDatasetSequence()
917 if (datasetSequence != null)
921 for (int i = 0; i < sequence.length; i++)
923 if (jalview.util.Comparison.isGap(sequence[i]))
934 * @see jalview.datamodel.SequenceI#deriveSequence()
936 public SequenceI deriveSequence()
938 SequenceI seq = new Sequence(this);
939 if (datasetSequence != null)
941 // duplicate current sequence with same dataset
942 seq.setDatasetSequence(datasetSequence);
946 if (isValidDatasetSequence())
948 // Use this as dataset sequence
949 seq.setDatasetSequence(this);
953 // Create a new, valid dataset sequence
955 ds.setSequence(AlignSeq.extractGaps(
956 jalview.util.Comparison.GapChars, new String(sequence)));
957 setDatasetSequence(ds);
958 ds.setSequenceFeatures(getSequenceFeatures());
959 seq = this; // and return this sequence as the derived sequence.
968 * @see jalview.datamodel.SequenceI#createDatasetSequence()
970 public SequenceI createDatasetSequence()
972 if (datasetSequence == null)
974 datasetSequence = new Sequence(getName(), AlignSeq.extractGaps(
975 jalview.util.Comparison.GapChars, getSequenceAsString()),
976 getStart(), getEnd());
977 datasetSequence.setSequenceFeatures(getSequenceFeatures());
978 datasetSequence.setDescription(getDescription());
979 setSequenceFeatures(null);
980 // move database references onto dataset sequence
981 datasetSequence.setDBRef(getDBRef());
983 datasetSequence.setPDBId(getPDBId());
985 datasetSequence.updatePDBIds();
987 return datasetSequence;
994 * 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());
1107 * jalview.datamodel.SequenceI#transferAnnotation(jalview.datamodel.SequenceI,
1108 * jalview.datamodel.Mapping)
1110 public void transferAnnotation(SequenceI entry, Mapping mp)
1112 if (datasetSequence != null)
1114 datasetSequence.transferAnnotation(entry, mp);
1117 if (entry.getDatasetSequence() != null)
1119 transferAnnotation(entry.getDatasetSequence(), mp);
1122 // transfer any new features from entry onto sequence
1123 if (entry.getSequenceFeatures() != null)
1126 SequenceFeature[] sfs = entry.getSequenceFeatures();
1127 for (int si = 0; si < sfs.length; si++)
1129 SequenceFeature sf[] = (mp != null) ? mp.locateFeature(sfs[si])
1130 : new SequenceFeature[]
1131 { new SequenceFeature(sfs[si]) };
1132 if (sf != null && sf.length > 0)
1134 for (int sfi = 0; sfi < sf.length; sfi++)
1136 addSequenceFeature(sf[sfi]);
1142 // transfer PDB entries
1143 if (entry.getPDBId() != null)
1145 Enumeration e = entry.getPDBId().elements();
1146 while (e.hasMoreElements())
1148 PDBEntry pdb = (PDBEntry) e.nextElement();
1152 // transfer database references
1153 DBRefEntry[] entryRefs = entry.getDBRef();
1154 if (entryRefs != null)
1156 for (int r = 0; r < entryRefs.length; r++)
1158 DBRefEntry newref = new DBRefEntry(entryRefs[r]);
1159 if (newref.getMap() != null && mp != null)
1161 // remap ref using our local mapping
1163 // we also assume all version string setting is done by dbSourceProxy
1165 * if (!newref.getSource().equalsIgnoreCase(dbSource)) {
1166 * newref.setSource(dbSource); }