2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, 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;
20 import jalview.analysis.AlignSeq;
22 import java.util.Enumeration;
23 import java.util.Vector;
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
59 * The index of the sequence in a MSA
63 /** array of seuqence features - may not be null for a valid sequence object */
64 public SequenceFeature[] sequenceFeatures;
67 * Creates a new Sequence object.
72 * string to form a possibly gapped sequence out of
74 * first position of non-gap residue in the sequence
76 * last position of ungapped residues (nearly always only used for
79 public Sequence(String name, String sequence, int start, int end)
82 this.sequence = sequence.toCharArray();
89 public Sequence(String name, char[] sequence, int start, int end)
92 this.sequence = sequence;
99 com.stevesoft.pat.Regex limitrx = new com.stevesoft.pat.Regex(
100 "[/][0-9]{1,}[-][0-9]{1,}$");
102 com.stevesoft.pat.Regex endrx = new com.stevesoft.pat.Regex("[0-9]{1,}$");
109 .println("POSSIBLE IMPLEMENTATION ERROR: null sequence name passed to constructor.");
112 // Does sequence have the /start-end signiature?
113 if (limitrx.search(name))
115 name = limitrx.left();
116 endrx.search(limitrx.stringMatched());
117 setStart(Integer.parseInt(limitrx.stringMatched().substring(1,
118 endrx.matchedFrom() - 1)));
119 setEnd(Integer.parseInt(endrx.stringMatched()));
123 void checkValidRange()
125 // Note: JAL-774 : http://issues.jalview.org/browse/JAL-774?focusedCommentId=11239&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-11239
128 for (int j = 0; j < sequence.length; j++)
130 if (!jalview.util.Comparison.isGap(sequence[j]))
148 * Creates a new Sequence object.
155 public Sequence(String name, String sequence)
157 this(name, sequence, 1, -1);
161 * Creates a new Sequence object with new features, DBRefEntries,
162 * AlignmentAnnotations, and PDBIds but inherits any existing dataset sequence
168 public Sequence(SequenceI seq)
170 this(seq, seq.getAnnotation());
174 * Create a new sequence object with new features, DBRefEntries, and PDBIds
175 * but inherits any existing dataset sequence reference, and duplicate of any
176 * annotation that is present in the given annotation array.
179 * the sequence to be copied
180 * @param alAnnotation
181 * an array of annotation including some associated with seq
183 public Sequence(SequenceI seq, AlignmentAnnotation[] alAnnotation)
185 this(seq.getName(), seq.getSequence(), seq.getStart(), seq.getEnd());
186 description = seq.getDescription();
187 if (seq.getSequenceFeatures() != null)
189 SequenceFeature[] sf = seq.getSequenceFeatures();
190 for (int i = 0; i < sf.length; i++)
192 addSequenceFeature(new SequenceFeature(sf[i]));
195 setDatasetSequence(seq.getDatasetSequence());
196 if (datasetSequence == null && seq.getDBRef() != null)
198 // only copy DBRefs if we really are a dataset sequence
199 DBRefEntry[] dbr = seq.getDBRef();
200 for (int i = 0; i < dbr.length; i++)
202 addDBRef(new DBRefEntry(dbr[i]));
205 if (seq.getAnnotation() != null)
207 AlignmentAnnotation[] sqann = seq.getAnnotation();
208 for (int i = 0; i < sqann.length; i++)
210 if (sqann[i] == null)
214 boolean found = (alAnnotation == null);
217 for (int apos = 0; !found && apos < alAnnotation.length; apos++)
219 found = (alAnnotation[apos] == sqann[i]);
224 // only copy the given annotation
225 AlignmentAnnotation newann = new AlignmentAnnotation(sqann[i]);
226 addAlignmentAnnotation(newann);
230 if (seq.getPDBId() != null)
232 Vector ids = seq.getPDBId();
233 Enumeration e = ids.elements();
234 while (e.hasMoreElements())
236 this.addPDBId(new PDBEntry((PDBEntry) e.nextElement()));
247 public void setSequenceFeatures(SequenceFeature[] features)
249 sequenceFeatures = features;
252 public synchronized void addSequenceFeature(SequenceFeature sf)
254 if (sequenceFeatures == null)
256 sequenceFeatures = new SequenceFeature[0];
259 for (int i = 0; i < sequenceFeatures.length; i++)
261 if (sequenceFeatures[i].equals(sf))
267 SequenceFeature[] temp = new SequenceFeature[sequenceFeatures.length + 1];
268 System.arraycopy(sequenceFeatures, 0, temp, 0, sequenceFeatures.length);
269 temp[sequenceFeatures.length] = sf;
271 sequenceFeatures = temp;
274 public void deleteFeature(SequenceFeature sf)
276 if (sequenceFeatures == null)
282 for (index = 0; index < sequenceFeatures.length; index++)
284 if (sequenceFeatures[index].equals(sf))
290 if (index == sequenceFeatures.length)
295 int sfLength = sequenceFeatures.length;
298 sequenceFeatures = null;
302 SequenceFeature[] temp = new SequenceFeature[sfLength - 1];
303 System.arraycopy(sequenceFeatures, 0, temp, 0, index);
305 if (index < sfLength)
307 System.arraycopy(sequenceFeatures, index + 1, temp, index,
308 sequenceFeatures.length - index - 1);
311 sequenceFeatures = temp;
318 * @return DOCUMENT ME!
320 public SequenceFeature[] getSequenceFeatures()
322 return sequenceFeatures;
325 public void addPDBId(PDBEntry entry)
329 pdbIds = new Vector();
331 if (!pdbIds.contains(entry))
333 pdbIds.addElement(entry);
343 public void setPDBId(Vector id)
351 * @return DOCUMENT ME!
353 public Vector getPDBId()
361 * @return DOCUMENT ME!
363 public String getDisplayId(boolean jvsuffix)
365 StringBuffer result = new StringBuffer(name);
368 result.append("/" + start + "-" + end);
371 return result.toString();
380 public void setName(String name)
389 * @return DOCUMENT ME!
391 public String getName()
402 public void setStart(int start)
410 * @return DOCUMENT ME!
412 public int getStart()
423 public void setEnd(int end)
431 * @return DOCUMENT ME!
441 * @return DOCUMENT ME!
443 public int getLength()
445 return this.sequence.length;
454 public void setSequence(String seq)
456 this.sequence = seq.toCharArray();
460 public String getSequenceAsString()
462 return new String(sequence);
465 public String getSequenceAsString(int start, int end)
467 return new String(getSequence(start, end));
470 public char[] getSequence()
478 * @see jalview.datamodel.SequenceI#getSequence(int, int)
480 public char[] getSequence(int start, int end)
484 // JBPNote - left to user to pad the result here (TODO:Decide on this
486 if (start >= sequence.length)
491 if (end >= sequence.length)
493 end = sequence.length;
496 char[] reply = new char[end - start];
497 System.arraycopy(sequence, start, reply, 0, end - start);
503 * make a new Sequence object from start to end (including gaps) over this
512 public SequenceI getSubSequence(int start, int end)
518 char[] seq = getSequence(start, end);
523 int nstart = findPosition(start);
524 int nend = findPosition(end) - 1;
525 // JBPNote - this is an incomplete copy.
526 SequenceI nseq = new Sequence(this.getName(), seq, nstart, nend);
527 nseq.setDescription(description);
528 if (datasetSequence != null)
530 nseq.setDatasetSequence(datasetSequence);
534 nseq.setDatasetSequence(this);
545 * @return DOCUMENT ME!
547 public char getCharAt(int i)
549 if (i < sequence.length)
565 public void setDescription(String desc)
567 this.description = desc;
573 * @return DOCUMENT ME!
575 public String getDescription()
577 return this.description;
582 * @see jalview.datamodel.SequenceI#findIndex(int)
584 public int findIndex(int pos)
586 // returns the alignment position for a residue
589 // Rely on end being at least as long as the length of the sequence.
590 while ((i < sequence.length) && (j <= end) && (j <= pos))
592 if (!jalview.util.Comparison.isGap(sequence[i]))
600 if ((j == end) && (j < pos))
611 * Returns the sequence position for an alignment position
614 * column index in alignment (from 1)
616 * @return residue number for residue (left of and) nearest ith column
618 public int findPosition(int i)
622 int seqlen = sequence.length;
623 while ((j < i) && (j < seqlen))
625 if (!jalview.util.Comparison.isGap(sequence[j]))
637 * Returns an int array where indices correspond to each residue in the
638 * sequence and the element value gives its position in the alignment
640 * @return int[SequenceI.getEnd()-SequenceI.getStart()+1] or null if no
641 * residues in SequenceI object
643 public int[] gapMap()
645 String seq = jalview.analysis.AlignSeq.extractGaps(
646 jalview.util.Comparison.GapChars, new String(sequence));
647 int[] map = new int[seq.length()];
651 while (j < sequence.length)
653 if (!jalview.util.Comparison.isGap(sequence[j]))
667 * @see jalview.datamodel.SequenceI#findPositionMap()
669 public int[] findPositionMap()
671 int map[] = new int[sequence.length];
674 int seqlen = sequence.length;
678 if (!jalview.util.Comparison.isGap(sequence[j]))
691 * @see jalview.datamodel.SequenceI#deleteChars(int, int)
693 public void deleteChars(int i, int j)
695 int newstart = start, newend = end;
696 if (i >= sequence.length)
703 if (j >= sequence.length)
706 System.arraycopy(sequence, 0, tmp, 0, i);
710 tmp = new char[sequence.length - j + i];
711 System.arraycopy(sequence, 0, tmp, 0, i);
712 System.arraycopy(sequence, j, tmp, i, sequence.length - j);
714 boolean createNewDs = false;
715 for (int s = i; s < j; s++)
717 if (jalview.schemes.ResidueProperties.aaIndex[sequence[s]] != 23)
725 int sindex = findIndex(start) - 1;
728 // delete characters including start of sequence
729 newstart = findPosition(j);
730 break; // don't need to search for any more residue characters.
734 // delete characters after start.
735 int eindex = findIndex(end) - 1;
738 // delete characters at end of sequence
739 newend = findPosition(i - 1);
740 break; // don't need to search for any more residue characters.
745 newend--; // decrease end position by one for the deleted residue
746 // and search further
752 // deletion occured in the middle of the sequence
753 if (createNewDs && this.datasetSequence != null)
755 // construct a new sequence
756 Sequence ds = new Sequence(datasetSequence);
757 // TODO: remove any non-inheritable properties ?
758 // TODO: create a sequence mapping (since there is a relation here ?)
759 ds.deleteChars(i, j);
760 datasetSequence = ds;
777 public void insertCharAt(int i, int length, char c)
779 char[] tmp = new char[sequence.length + length];
781 if (i >= sequence.length)
783 System.arraycopy(sequence, 0, tmp, 0, sequence.length);
788 System.arraycopy(sequence, 0, tmp, 0, i);
798 if (i < sequence.length)
800 System.arraycopy(sequence, i, tmp, index, sequence.length - i);
806 public void insertCharAt(int i, char c)
808 insertCharAt(i, 1, c);
811 public String getVamsasId()
816 public void setVamsasId(String id)
821 public void setDBRef(DBRefEntry[] dbref)
826 public DBRefEntry[] getDBRef()
828 if (dbrefs == null && datasetSequence != null
829 && this != datasetSequence)
831 return datasetSequence.getDBRef();
836 public void addDBRef(DBRefEntry entry)
840 dbrefs = new DBRefEntry[0];
843 int i, iSize = dbrefs.length;
845 for (i = 0; i < iSize; i++)
847 if (dbrefs[i].equalRef(entry))
849 if (entry.getMap() != null)
851 if (dbrefs[i].getMap() == null)
853 // overwrite with 'superior' entry that contains a mapping.
861 DBRefEntry[] temp = new DBRefEntry[iSize + 1];
862 System.arraycopy(dbrefs, 0, temp, 0, iSize);
863 temp[temp.length - 1] = entry;
868 public void setDatasetSequence(SequenceI seq)
870 datasetSequence = seq;
873 public SequenceI getDatasetSequence()
875 return datasetSequence;
878 public AlignmentAnnotation[] getAnnotation()
880 if (annotation == null)
885 AlignmentAnnotation[] ret = new AlignmentAnnotation[annotation.size()];
886 for (int r = 0; r < ret.length; r++)
888 ret[r] = (AlignmentAnnotation) annotation.elementAt(r);
894 public void addAlignmentAnnotation(AlignmentAnnotation annotation)
896 if (this.annotation == null)
898 this.annotation = new Vector();
900 if (!this.annotation.contains(annotation))
902 this.annotation.addElement(annotation);
904 annotation.setSequenceRef(this);
907 public void removeAlignmentAnnotation(AlignmentAnnotation annotation)
909 if (this.annotation != null)
911 this.annotation.removeElement(annotation);
912 if (this.annotation.size() == 0)
913 this.annotation = null;
918 * test if this is a valid candidate for another sequence's dataset sequence.
921 private boolean isValidDatasetSequence()
923 if (datasetSequence != null)
927 for (int i = 0; i < sequence.length; i++)
929 if (jalview.util.Comparison.isGap(sequence[i]))
940 * @see jalview.datamodel.SequenceI#deriveSequence()
942 public SequenceI deriveSequence()
944 SequenceI seq = new Sequence(this);
945 if (datasetSequence != null)
947 // duplicate current sequence with same dataset
948 seq.setDatasetSequence(datasetSequence);
952 if (isValidDatasetSequence())
954 // Use this as dataset sequence
955 seq.setDatasetSequence(this);
959 // Create a new, valid dataset sequence
961 ds.setSequence(AlignSeq.extractGaps(
962 jalview.util.Comparison.GapChars, new String(sequence)));
963 setDatasetSequence(ds);
964 ds.setSequenceFeatures(getSequenceFeatures());
965 seq = this; // and return this sequence as the derived sequence.
974 * @see jalview.datamodel.SequenceI#createDatasetSequence()
976 public SequenceI createDatasetSequence()
978 if (datasetSequence == null)
980 datasetSequence = new Sequence(getName(), AlignSeq.extractGaps(
981 jalview.util.Comparison.GapChars, getSequenceAsString()),
982 getStart(), getEnd());
983 datasetSequence.setSequenceFeatures(getSequenceFeatures());
984 datasetSequence.setDescription(getDescription());
985 setSequenceFeatures(null);
986 // move database references onto dataset sequence
987 datasetSequence.setDBRef(getDBRef());
989 datasetSequence.setPDBId(getPDBId());
991 datasetSequence.updatePDBIds();
993 return datasetSequence;
1000 * jalview.datamodel.SequenceI#setAlignmentAnnotation(AlignmmentAnnotation[]
1003 public void setAlignmentAnnotation(AlignmentAnnotation[] annotations)
1005 if (annotation != null)
1007 annotation.removeAllElements();
1009 if (annotations != null)
1011 for (int i = 0; i < annotations.length; i++)
1013 if (annotations[i] != null)
1014 addAlignmentAnnotation(annotations[i]);
1022 * @see jalview.datamodel.SequenceI#getAnnotation(java.lang.String)
1024 public AlignmentAnnotation[] getAnnotation(String label)
1026 if (annotation == null || annotation.size() == 0)
1031 Vector subset = new Vector();
1032 Enumeration e = annotation.elements();
1033 while (e.hasMoreElements())
1035 AlignmentAnnotation ann = (AlignmentAnnotation) e.nextElement();
1036 if (ann.label != null && ann.label.equals(label))
1038 subset.addElement(ann);
1041 if (subset.size() == 0)
1045 AlignmentAnnotation[] anns = new AlignmentAnnotation[subset.size()];
1047 e = subset.elements();
1048 while (e.hasMoreElements())
1050 anns[i++] = (AlignmentAnnotation) e.nextElement();
1052 subset.removeAllElements();
1056 public boolean updatePDBIds()
1058 if (datasetSequence != null)
1060 // TODO: could merge DBRefs
1061 return datasetSequence.updatePDBIds();
1063 if (dbrefs == null || dbrefs.length == 0)
1067 Vector newpdb = new Vector();
1068 for (int i = 0; i < dbrefs.length; i++)
1070 if (DBRefSource.PDB.equals(dbrefs[i].getSource()))
1072 PDBEntry pdbe = new PDBEntry();
1073 pdbe.setId(dbrefs[i].getAccessionId());
1074 if (pdbIds == null || pdbIds.size() == 0)
1076 newpdb.addElement(pdbe);
1080 Enumeration en = pdbIds.elements();
1081 boolean matched = false;
1082 while (!matched && en.hasMoreElements())
1084 PDBEntry anentry = (PDBEntry) en.nextElement();
1085 if (anentry.getId().equals(pdbe.getId()))
1092 newpdb.addElement(pdbe);
1097 if (newpdb.size() > 0)
1099 Enumeration en = newpdb.elements();
1100 while (en.hasMoreElements())
1102 addPDBId((PDBEntry) en.nextElement());
1113 * jalview.datamodel.SequenceI#transferAnnotation(jalview.datamodel.SequenceI,
1114 * jalview.datamodel.Mapping)
1116 public void transferAnnotation(SequenceI entry, Mapping mp)
1118 if (datasetSequence != null)
1120 datasetSequence.transferAnnotation(entry, mp);
1123 if (entry.getDatasetSequence() != null)
1125 transferAnnotation(entry.getDatasetSequence(), mp);
1128 // transfer any new features from entry onto sequence
1129 if (entry.getSequenceFeatures() != null)
1132 SequenceFeature[] sfs = entry.getSequenceFeatures();
1133 for (int si = 0; si < sfs.length; si++)
1135 SequenceFeature sf[] = (mp != null) ? mp.locateFeature(sfs[si])
1136 : new SequenceFeature[]
1137 { new SequenceFeature(sfs[si]) };
1138 if (sf != null && sf.length > 0)
1140 for (int sfi = 0; sfi < sf.length; sfi++)
1142 addSequenceFeature(sf[sfi]);
1148 // transfer PDB entries
1149 if (entry.getPDBId() != null)
1151 Enumeration e = entry.getPDBId().elements();
1152 while (e.hasMoreElements())
1154 PDBEntry pdb = (PDBEntry) e.nextElement();
1158 // transfer database references
1159 DBRefEntry[] entryRefs = entry.getDBRef();
1160 if (entryRefs != null)
1162 for (int r = 0; r < entryRefs.length; r++)
1164 DBRefEntry newref = new DBRefEntry(entryRefs[r]);
1165 if (newref.getMap() != null && mp != null)
1167 // remap ref using our local mapping
1169 // we also assume all version string setting is done by dbSourceProxy
1171 * if (!newref.getSource().equalsIgnoreCase(dbSource)) {
1172 * newref.setSource(dbSource); }
1180 * @return The index (zero-based) on this sequence in the MSA.
1181 * It returns {@code -1} if this information is not available.
1183 public int getIndex() { return index; }
1186 * Defines the position of this sequence in the MSA.
1187 * Use the value {@code -1} if this information is undefined.
1189 * @param The position for this sequence. This value is zero-based (zero for this first sequence)
1191 public void setIndex(int value) { index = value; }