2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, 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()
126 // http://issues.jalview.org/browse/JAL-774?focusedCommentId=11239&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-11239
129 for (int j = 0; j < sequence.length; j++)
131 if (!jalview.util.Comparison.isGap(sequence[j]))
150 * Creates a new Sequence object.
157 public Sequence(String name, String sequence)
159 this(name, sequence, 1, -1);
163 * Creates a new Sequence object with new features, DBRefEntries,
164 * AlignmentAnnotations, and PDBIds but inherits any existing dataset sequence
170 public Sequence(SequenceI seq)
172 this(seq, seq.getAnnotation());
176 * Create a new sequence object with new features, DBRefEntries, and PDBIds
177 * but inherits any existing dataset sequence reference, and duplicate of any
178 * annotation that is present in the given annotation array.
181 * the sequence to be copied
182 * @param alAnnotation
183 * an array of annotation including some associated with seq
185 public Sequence(SequenceI seq, AlignmentAnnotation[] alAnnotation)
187 this(seq.getName(), seq.getSequence(), seq.getStart(), seq.getEnd());
188 description = seq.getDescription();
189 if (seq.getSequenceFeatures() != null)
191 SequenceFeature[] sf = seq.getSequenceFeatures();
192 for (int i = 0; i < sf.length; i++)
194 addSequenceFeature(new SequenceFeature(sf[i]));
197 setDatasetSequence(seq.getDatasetSequence());
198 if (datasetSequence == null && seq.getDBRef() != null)
200 // only copy DBRefs if we really are a dataset sequence
201 DBRefEntry[] dbr = seq.getDBRef();
202 for (int i = 0; i < dbr.length; i++)
204 addDBRef(new DBRefEntry(dbr[i]));
207 if (seq.getAnnotation() != null)
209 AlignmentAnnotation[] sqann = seq.getAnnotation();
210 for (int i = 0; i < sqann.length; i++)
212 if (sqann[i] == null)
216 boolean found = (alAnnotation == null);
219 for (int apos = 0; !found && apos < alAnnotation.length; apos++)
221 found = (alAnnotation[apos] == sqann[i]);
226 // only copy the given annotation
227 AlignmentAnnotation newann = new AlignmentAnnotation(sqann[i]);
228 addAlignmentAnnotation(newann);
232 if (seq.getPDBId() != null)
234 Vector ids = seq.getPDBId();
235 Enumeration e = ids.elements();
236 while (e.hasMoreElements())
238 this.addPDBId(new PDBEntry((PDBEntry) e.nextElement()));
249 public void setSequenceFeatures(SequenceFeature[] features)
251 sequenceFeatures = features;
254 public synchronized void addSequenceFeature(SequenceFeature sf)
256 if (sequenceFeatures == null)
258 sequenceFeatures = new SequenceFeature[0];
261 for (int i = 0; i < sequenceFeatures.length; i++)
263 if (sequenceFeatures[i].equals(sf))
269 SequenceFeature[] temp = new SequenceFeature[sequenceFeatures.length + 1];
270 System.arraycopy(sequenceFeatures, 0, temp, 0, sequenceFeatures.length);
271 temp[sequenceFeatures.length] = sf;
273 sequenceFeatures = temp;
276 public void deleteFeature(SequenceFeature sf)
278 if (sequenceFeatures == null)
284 for (index = 0; index < sequenceFeatures.length; index++)
286 if (sequenceFeatures[index].equals(sf))
292 if (index == sequenceFeatures.length)
297 int sfLength = sequenceFeatures.length;
300 sequenceFeatures = null;
304 SequenceFeature[] temp = new SequenceFeature[sfLength - 1];
305 System.arraycopy(sequenceFeatures, 0, temp, 0, index);
307 if (index < sfLength)
309 System.arraycopy(sequenceFeatures, index + 1, temp, index,
310 sequenceFeatures.length - index - 1);
313 sequenceFeatures = temp;
320 * @return DOCUMENT ME!
322 public SequenceFeature[] getSequenceFeatures()
324 return sequenceFeatures;
327 public void addPDBId(PDBEntry entry)
331 pdbIds = new Vector();
333 if (!pdbIds.contains(entry))
335 pdbIds.addElement(entry);
345 public void setPDBId(Vector id)
353 * @return DOCUMENT ME!
355 public Vector getPDBId()
363 * @return DOCUMENT ME!
365 public String getDisplayId(boolean jvsuffix)
367 StringBuffer result = new StringBuffer(name);
370 result.append("/" + start + "-" + end);
373 return result.toString();
382 public void setName(String name)
391 * @return DOCUMENT ME!
393 public String getName()
404 public void setStart(int start)
412 * @return DOCUMENT ME!
414 public int getStart()
425 public void setEnd(int end)
433 * @return DOCUMENT ME!
443 * @return DOCUMENT ME!
445 public int getLength()
447 return this.sequence.length;
456 public void setSequence(String seq)
458 this.sequence = seq.toCharArray();
462 public String getSequenceAsString()
464 return new String(sequence);
467 public String getSequenceAsString(int start, int end)
469 return new String(getSequence(start, end));
472 public char[] getSequence()
480 * @see jalview.datamodel.SequenceI#getSequence(int, int)
482 public char[] getSequence(int start, int end)
486 // JBPNote - left to user to pad the result here (TODO:Decide on this
488 if (start >= sequence.length)
493 if (end >= sequence.length)
495 end = sequence.length;
498 char[] reply = new char[end - start];
499 System.arraycopy(sequence, start, reply, 0, end - start);
505 * make a new Sequence object from start to end (including gaps) over this
514 public SequenceI getSubSequence(int start, int end)
520 char[] seq = getSequence(start, end);
525 int nstart = findPosition(start);
526 int nend = findPosition(end) - 1;
527 // JBPNote - this is an incomplete copy.
528 SequenceI nseq = new Sequence(this.getName(), seq, nstart, nend);
529 nseq.setDescription(description);
530 if (datasetSequence != null)
532 nseq.setDatasetSequence(datasetSequence);
536 nseq.setDatasetSequence(this);
547 * @return DOCUMENT ME!
549 public char getCharAt(int i)
551 if (i < sequence.length)
567 public void setDescription(String desc)
569 this.description = desc;
575 * @return DOCUMENT ME!
577 public String getDescription()
579 return this.description;
585 * @see jalview.datamodel.SequenceI#findIndex(int)
587 public int findIndex(int pos)
589 // returns the alignment position for a residue
592 // Rely on end being at least as long as the length of the sequence.
593 while ((i < sequence.length) && (j <= end) && (j <= pos))
595 if (!jalview.util.Comparison.isGap(sequence[i]))
603 if ((j == end) && (j < pos))
614 * Returns the sequence position for an alignment position
617 * column index in alignment (from 1)
619 * @return residue number for residue (left of and) nearest ith column
621 public int findPosition(int i)
625 int seqlen = sequence.length;
626 while ((j < i) && (j < seqlen))
628 if (!jalview.util.Comparison.isGap(sequence[j]))
640 * Returns an int array where indices correspond to each residue in the
641 * sequence and the element value gives its position in the alignment
643 * @return int[SequenceI.getEnd()-SequenceI.getStart()+1] or null if no
644 * residues in SequenceI object
646 public int[] gapMap()
648 String seq = jalview.analysis.AlignSeq.extractGaps(
649 jalview.util.Comparison.GapChars, new String(sequence));
650 int[] map = new int[seq.length()];
654 while (j < sequence.length)
656 if (!jalview.util.Comparison.isGap(sequence[j]))
670 * @see jalview.datamodel.SequenceI#findPositionMap()
672 public int[] findPositionMap()
674 int map[] = new int[sequence.length];
677 int seqlen = sequence.length;
681 if (!jalview.util.Comparison.isGap(sequence[j]))
694 * @see jalview.datamodel.SequenceI#deleteChars(int, int)
696 public void deleteChars(int i, int j)
698 int newstart = start, newend = end;
699 if (i >= sequence.length)
706 if (j >= sequence.length)
709 System.arraycopy(sequence, 0, tmp, 0, i);
714 tmp = new char[sequence.length - j + i];
715 System.arraycopy(sequence, 0, tmp, 0, i);
716 System.arraycopy(sequence, j, tmp, i, sequence.length - j);
718 boolean createNewDs = false;
719 // TODO: take a look at the new dataset creation validation method below -
720 // this could become time comsuming for large sequences - consider making it
722 for (int s = i; s < j; s++)
724 if (jalview.schemes.ResidueProperties.aaIndex[sequence[s]] != 23)
732 int sindex = findIndex(start) - 1;
735 // delete characters including start of sequence
736 newstart = findPosition(j);
737 break; // don't need to search for any more residue characters.
741 // delete characters after start.
742 int eindex = findIndex(end) - 1;
745 // delete characters at end of sequence
746 newend = findPosition(i - 1);
747 break; // don't need to search for any more residue characters.
752 newend--; // decrease end position by one for the deleted residue
753 // and search further
759 // deletion occured in the middle of the sequence
760 if (createNewDs && this.datasetSequence != null)
762 // construct a new sequence
763 Sequence ds = new Sequence(datasetSequence);
764 // TODO: remove any non-inheritable properties ?
765 // TODO: create a sequence mapping (since there is a relation here ?)
766 ds.deleteChars(i, j);
767 datasetSequence = ds;
784 public void insertCharAt(int i, int length, char c)
786 char[] tmp = new char[sequence.length + length];
788 if (i >= sequence.length)
790 System.arraycopy(sequence, 0, tmp, 0, sequence.length);
795 System.arraycopy(sequence, 0, tmp, 0, i);
805 if (i < sequence.length)
807 System.arraycopy(sequence, i, tmp, index, sequence.length - i);
813 public void insertCharAt(int i, char c)
815 insertCharAt(i, 1, c);
818 public String getVamsasId()
823 public void setVamsasId(String id)
828 public void setDBRef(DBRefEntry[] dbref)
833 public DBRefEntry[] getDBRef()
835 if (dbrefs == null && datasetSequence != null
836 && this != datasetSequence)
838 return datasetSequence.getDBRef();
843 public void addDBRef(DBRefEntry entry)
847 dbrefs = new DBRefEntry[0];
850 int i, iSize = dbrefs.length;
852 for (i = 0; i < iSize; i++)
854 if (dbrefs[i].equalRef(entry))
856 if (entry.getMap() != null)
858 if (dbrefs[i].getMap() == null)
860 // overwrite with 'superior' entry that contains a mapping.
868 DBRefEntry[] temp = new DBRefEntry[iSize + 1];
869 System.arraycopy(dbrefs, 0, temp, 0, iSize);
870 temp[temp.length - 1] = entry;
875 public void setDatasetSequence(SequenceI seq)
877 datasetSequence = seq;
880 public SequenceI getDatasetSequence()
882 return datasetSequence;
885 public AlignmentAnnotation[] getAnnotation()
887 if (annotation == null)
892 AlignmentAnnotation[] ret = new AlignmentAnnotation[annotation.size()];
893 for (int r = 0; r < ret.length; r++)
895 ret[r] = (AlignmentAnnotation) annotation.elementAt(r);
901 public void addAlignmentAnnotation(AlignmentAnnotation annotation)
903 if (this.annotation == null)
905 this.annotation = new Vector();
907 if (!this.annotation.contains(annotation))
909 this.annotation.addElement(annotation);
911 annotation.setSequenceRef(this);
914 public void removeAlignmentAnnotation(AlignmentAnnotation annotation)
916 if (this.annotation != null)
918 this.annotation.removeElement(annotation);
919 if (this.annotation.size() == 0)
920 this.annotation = null;
925 * test if this is a valid candidate for another sequence's dataset sequence.
928 private boolean isValidDatasetSequence()
930 if (datasetSequence != null)
934 for (int i = 0; i < sequence.length; i++)
936 if (jalview.util.Comparison.isGap(sequence[i]))
947 * @see jalview.datamodel.SequenceI#deriveSequence()
949 public SequenceI deriveSequence()
951 SequenceI seq = new Sequence(this);
952 if (datasetSequence != null)
954 // duplicate current sequence with same dataset
955 seq.setDatasetSequence(datasetSequence);
959 if (isValidDatasetSequence())
961 // Use this as dataset sequence
962 seq.setDatasetSequence(this);
966 // Create a new, valid dataset sequence
968 ds.setSequence(AlignSeq.extractGaps(
969 jalview.util.Comparison.GapChars, new String(sequence)));
970 setDatasetSequence(ds);
971 ds.setSequenceFeatures(getSequenceFeatures());
972 seq = this; // and return this sequence as the derived sequence.
981 * @see jalview.datamodel.SequenceI#createDatasetSequence()
983 public SequenceI createDatasetSequence()
985 if (datasetSequence == null)
987 datasetSequence = new Sequence(getName(), AlignSeq.extractGaps(
988 jalview.util.Comparison.GapChars, getSequenceAsString()),
989 getStart(), getEnd());
990 datasetSequence.setSequenceFeatures(getSequenceFeatures());
991 datasetSequence.setDescription(getDescription());
992 setSequenceFeatures(null);
993 // move database references onto dataset sequence
994 datasetSequence.setDBRef(getDBRef());
996 datasetSequence.setPDBId(getPDBId());
998 datasetSequence.updatePDBIds();
1000 return datasetSequence;
1007 * jalview.datamodel.SequenceI#setAlignmentAnnotation(AlignmmentAnnotation[]
1010 public void setAlignmentAnnotation(AlignmentAnnotation[] annotations)
1012 if (annotation != null)
1014 annotation.removeAllElements();
1016 if (annotations != null)
1018 for (int i = 0; i < annotations.length; i++)
1020 if (annotations[i] != null)
1021 addAlignmentAnnotation(annotations[i]);
1029 * @see jalview.datamodel.SequenceI#getAnnotation(java.lang.String)
1031 public AlignmentAnnotation[] getAnnotation(String label)
1033 if (annotation == null || annotation.size() == 0)
1038 Vector subset = new Vector();
1039 Enumeration e = annotation.elements();
1040 while (e.hasMoreElements())
1042 AlignmentAnnotation ann = (AlignmentAnnotation) e.nextElement();
1043 if (ann.label != null && ann.label.equals(label))
1045 subset.addElement(ann);
1048 if (subset.size() == 0)
1052 AlignmentAnnotation[] anns = new AlignmentAnnotation[subset.size()];
1054 e = subset.elements();
1055 while (e.hasMoreElements())
1057 anns[i++] = (AlignmentAnnotation) e.nextElement();
1059 subset.removeAllElements();
1063 public boolean updatePDBIds()
1065 if (datasetSequence != null)
1067 // TODO: could merge DBRefs
1068 return datasetSequence.updatePDBIds();
1070 if (dbrefs == null || dbrefs.length == 0)
1074 Vector newpdb = new Vector();
1075 for (int i = 0; i < dbrefs.length; i++)
1077 if (DBRefSource.PDB.equals(dbrefs[i].getSource()))
1079 PDBEntry pdbe = new PDBEntry();
1080 pdbe.setId(dbrefs[i].getAccessionId());
1081 if (pdbIds == null || pdbIds.size() == 0)
1083 newpdb.addElement(pdbe);
1087 Enumeration en = pdbIds.elements();
1088 boolean matched = false;
1089 while (!matched && en.hasMoreElements())
1091 PDBEntry anentry = (PDBEntry) en.nextElement();
1092 if (anentry.getId().equals(pdbe.getId()))
1099 newpdb.addElement(pdbe);
1104 if (newpdb.size() > 0)
1106 Enumeration en = newpdb.elements();
1107 while (en.hasMoreElements())
1109 addPDBId((PDBEntry) en.nextElement());
1120 * jalview.datamodel.SequenceI#transferAnnotation(jalview.datamodel.SequenceI,
1121 * jalview.datamodel.Mapping)
1123 public void transferAnnotation(SequenceI entry, Mapping mp)
1125 if (datasetSequence != null)
1127 datasetSequence.transferAnnotation(entry, mp);
1130 if (entry.getDatasetSequence() != null)
1132 transferAnnotation(entry.getDatasetSequence(), mp);
1135 // transfer any new features from entry onto sequence
1136 if (entry.getSequenceFeatures() != null)
1139 SequenceFeature[] sfs = entry.getSequenceFeatures();
1140 for (int si = 0; si < sfs.length; si++)
1142 SequenceFeature sf[] = (mp != null) ? mp.locateFeature(sfs[si])
1143 : new SequenceFeature[]
1144 { new SequenceFeature(sfs[si]) };
1145 if (sf != null && sf.length > 0)
1147 for (int sfi = 0; sfi < sf.length; sfi++)
1149 addSequenceFeature(sf[sfi]);
1155 // transfer PDB entries
1156 if (entry.getPDBId() != null)
1158 Enumeration e = entry.getPDBId().elements();
1159 while (e.hasMoreElements())
1161 PDBEntry pdb = (PDBEntry) e.nextElement();
1165 // transfer database references
1166 DBRefEntry[] entryRefs = entry.getDBRef();
1167 if (entryRefs != null)
1169 for (int r = 0; r < entryRefs.length; r++)
1171 DBRefEntry newref = new DBRefEntry(entryRefs[r]);
1172 if (newref.getMap() != null && mp != null)
1174 // remap ref using our local mapping
1176 // we also assume all version string setting is done by dbSourceProxy
1178 * if (!newref.getSource().equalsIgnoreCase(dbSource)) {
1179 * newref.setSource(dbSource); }
1187 * @return The index (zero-based) on this sequence in the MSA. It returns
1188 * {@code -1} if this information is not available.
1190 public int getIndex()
1196 * Defines the position of this sequence in the MSA. Use the value {@code -1}
1197 * if this information is undefined.
1200 * position for this sequence. This value is zero-based (zero for
1201 * this first sequence)
1203 public void setIndex(int value)