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);
713 tmp = new char[sequence.length - j + i];
714 System.arraycopy(sequence, 0, tmp, 0, i);
715 System.arraycopy(sequence, j, tmp, i, sequence.length - j);
717 boolean createNewDs = false;
718 for (int s = i; s < j; s++)
720 if (jalview.schemes.ResidueProperties.aaIndex[sequence[s]] != 23)
728 int sindex = findIndex(start) - 1;
731 // delete characters including start of sequence
732 newstart = findPosition(j);
733 break; // don't need to search for any more residue characters.
737 // delete characters after start.
738 int eindex = findIndex(end) - 1;
741 // delete characters at end of sequence
742 newend = findPosition(i - 1);
743 break; // don't need to search for any more residue characters.
748 newend--; // decrease end position by one for the deleted residue
749 // and search further
755 // deletion occured in the middle of the sequence
756 if (createNewDs && this.datasetSequence != null)
758 // construct a new sequence
759 Sequence ds = new Sequence(datasetSequence);
760 // TODO: remove any non-inheritable properties ?
761 // TODO: create a sequence mapping (since there is a relation here ?)
762 ds.deleteChars(i, j);
763 datasetSequence = ds;
780 public void insertCharAt(int i, int length, char c)
782 char[] tmp = new char[sequence.length + length];
784 if (i >= sequence.length)
786 System.arraycopy(sequence, 0, tmp, 0, sequence.length);
791 System.arraycopy(sequence, 0, tmp, 0, i);
801 if (i < sequence.length)
803 System.arraycopy(sequence, i, tmp, index, sequence.length - i);
809 public void insertCharAt(int i, char c)
811 insertCharAt(i, 1, c);
814 public String getVamsasId()
819 public void setVamsasId(String id)
824 public void setDBRef(DBRefEntry[] dbref)
829 public DBRefEntry[] getDBRef()
831 if (dbrefs == null && datasetSequence != null
832 && this != datasetSequence)
834 return datasetSequence.getDBRef();
839 public void addDBRef(DBRefEntry entry)
843 dbrefs = new DBRefEntry[0];
846 int i, iSize = dbrefs.length;
848 for (i = 0; i < iSize; i++)
850 if (dbrefs[i].equalRef(entry))
852 if (entry.getMap() != null)
854 if (dbrefs[i].getMap() == null)
856 // overwrite with 'superior' entry that contains a mapping.
864 DBRefEntry[] temp = new DBRefEntry[iSize + 1];
865 System.arraycopy(dbrefs, 0, temp, 0, iSize);
866 temp[temp.length - 1] = entry;
871 public void setDatasetSequence(SequenceI seq)
873 datasetSequence = seq;
876 public SequenceI getDatasetSequence()
878 return datasetSequence;
881 public AlignmentAnnotation[] getAnnotation()
883 if (annotation == null)
888 AlignmentAnnotation[] ret = new AlignmentAnnotation[annotation.size()];
889 for (int r = 0; r < ret.length; r++)
891 ret[r] = (AlignmentAnnotation) annotation.elementAt(r);
897 public void addAlignmentAnnotation(AlignmentAnnotation annotation)
899 if (this.annotation == null)
901 this.annotation = new Vector();
903 if (!this.annotation.contains(annotation))
905 this.annotation.addElement(annotation);
907 annotation.setSequenceRef(this);
910 public void removeAlignmentAnnotation(AlignmentAnnotation annotation)
912 if (this.annotation != null)
914 this.annotation.removeElement(annotation);
915 if (this.annotation.size() == 0)
916 this.annotation = null;
921 * test if this is a valid candidate for another sequence's dataset sequence.
924 private boolean isValidDatasetSequence()
926 if (datasetSequence != null)
930 for (int i = 0; i < sequence.length; i++)
932 if (jalview.util.Comparison.isGap(sequence[i]))
943 * @see jalview.datamodel.SequenceI#deriveSequence()
945 public SequenceI deriveSequence()
947 SequenceI seq = new Sequence(this);
948 if (datasetSequence != null)
950 // duplicate current sequence with same dataset
951 seq.setDatasetSequence(datasetSequence);
955 if (isValidDatasetSequence())
957 // Use this as dataset sequence
958 seq.setDatasetSequence(this);
962 // Create a new, valid dataset sequence
964 ds.setSequence(AlignSeq.extractGaps(
965 jalview.util.Comparison.GapChars, new String(sequence)));
966 setDatasetSequence(ds);
967 ds.setSequenceFeatures(getSequenceFeatures());
968 seq = this; // and return this sequence as the derived sequence.
977 * @see jalview.datamodel.SequenceI#createDatasetSequence()
979 public SequenceI createDatasetSequence()
981 if (datasetSequence == null)
983 datasetSequence = new Sequence(getName(), AlignSeq.extractGaps(
984 jalview.util.Comparison.GapChars, getSequenceAsString()),
985 getStart(), getEnd());
986 datasetSequence.setSequenceFeatures(getSequenceFeatures());
987 datasetSequence.setDescription(getDescription());
988 setSequenceFeatures(null);
989 // move database references onto dataset sequence
990 datasetSequence.setDBRef(getDBRef());
992 datasetSequence.setPDBId(getPDBId());
994 datasetSequence.updatePDBIds();
996 return datasetSequence;
1003 * jalview.datamodel.SequenceI#setAlignmentAnnotation(AlignmmentAnnotation[]
1006 public void setAlignmentAnnotation(AlignmentAnnotation[] annotations)
1008 if (annotation != null)
1010 annotation.removeAllElements();
1012 if (annotations != null)
1014 for (int i = 0; i < annotations.length; i++)
1016 if (annotations[i] != null)
1017 addAlignmentAnnotation(annotations[i]);
1025 * @see jalview.datamodel.SequenceI#getAnnotation(java.lang.String)
1027 public AlignmentAnnotation[] getAnnotation(String label)
1029 if (annotation == null || annotation.size() == 0)
1034 Vector subset = new Vector();
1035 Enumeration e = annotation.elements();
1036 while (e.hasMoreElements())
1038 AlignmentAnnotation ann = (AlignmentAnnotation) e.nextElement();
1039 if (ann.label != null && ann.label.equals(label))
1041 subset.addElement(ann);
1044 if (subset.size() == 0)
1048 AlignmentAnnotation[] anns = new AlignmentAnnotation[subset.size()];
1050 e = subset.elements();
1051 while (e.hasMoreElements())
1053 anns[i++] = (AlignmentAnnotation) e.nextElement();
1055 subset.removeAllElements();
1059 public boolean updatePDBIds()
1061 if (datasetSequence != null)
1063 // TODO: could merge DBRefs
1064 return datasetSequence.updatePDBIds();
1066 if (dbrefs == null || dbrefs.length == 0)
1070 Vector newpdb = new Vector();
1071 for (int i = 0; i < dbrefs.length; i++)
1073 if (DBRefSource.PDB.equals(dbrefs[i].getSource()))
1075 PDBEntry pdbe = new PDBEntry();
1076 pdbe.setId(dbrefs[i].getAccessionId());
1077 if (pdbIds == null || pdbIds.size() == 0)
1079 newpdb.addElement(pdbe);
1083 Enumeration en = pdbIds.elements();
1084 boolean matched = false;
1085 while (!matched && en.hasMoreElements())
1087 PDBEntry anentry = (PDBEntry) en.nextElement();
1088 if (anentry.getId().equals(pdbe.getId()))
1095 newpdb.addElement(pdbe);
1100 if (newpdb.size() > 0)
1102 Enumeration en = newpdb.elements();
1103 while (en.hasMoreElements())
1105 addPDBId((PDBEntry) en.nextElement());
1116 * jalview.datamodel.SequenceI#transferAnnotation(jalview.datamodel.SequenceI,
1117 * jalview.datamodel.Mapping)
1119 public void transferAnnotation(SequenceI entry, Mapping mp)
1121 if (datasetSequence != null)
1123 datasetSequence.transferAnnotation(entry, mp);
1126 if (entry.getDatasetSequence() != null)
1128 transferAnnotation(entry.getDatasetSequence(), mp);
1131 // transfer any new features from entry onto sequence
1132 if (entry.getSequenceFeatures() != null)
1135 SequenceFeature[] sfs = entry.getSequenceFeatures();
1136 for (int si = 0; si < sfs.length; si++)
1138 SequenceFeature sf[] = (mp != null) ? mp.locateFeature(sfs[si])
1139 : new SequenceFeature[]
1140 { new SequenceFeature(sfs[si]) };
1141 if (sf != null && sf.length > 0)
1143 for (int sfi = 0; sfi < sf.length; sfi++)
1145 addSequenceFeature(sf[sfi]);
1151 // transfer PDB entries
1152 if (entry.getPDBId() != null)
1154 Enumeration e = entry.getPDBId().elements();
1155 while (e.hasMoreElements())
1157 PDBEntry pdb = (PDBEntry) e.nextElement();
1161 // transfer database references
1162 DBRefEntry[] entryRefs = entry.getDBRef();
1163 if (entryRefs != null)
1165 for (int r = 0; r < entryRefs.length; r++)
1167 DBRefEntry newref = new DBRefEntry(entryRefs[r]);
1168 if (newref.getMap() != null && mp != null)
1170 // remap ref using our local mapping
1172 // we also assume all version string setting is done by dbSourceProxy
1174 * if (!newref.getSource().equalsIgnoreCase(dbSource)) {
1175 * newref.setSource(dbSource); }
1183 * @return The index (zero-based) on this sequence in the MSA. It returns
1184 * {@code -1} if this information is not available.
1186 public int getIndex()
1192 * Defines the position of this sequence in the MSA. Use the value {@code -1}
1193 * if this information is undefined.
1196 * position for this sequence. This value is zero-based (zero for
1197 * this first sequence)
1199 public void setIndex(int value)