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;
25 import fr.orsay.lri.varna.models.rna.RNA;
29 * Implements the SequenceI interface for a char[] based sequence object.
34 public class Sequence implements SequenceI
36 SequenceI datasetSequence;
40 private char[] sequence;
57 * This annotation is displayed below the alignment but the positions are tied
58 * to the residues of this sequence
63 * The index of the sequence in a MSA
67 /** array of sequence features - may not be null for a valid sequence object */
68 public SequenceFeature[] sequenceFeatures;
71 * Creates a new Sequence object.
76 * string to form a possibly gapped sequence out of
78 * first position of non-gap residue in the sequence
80 * last position of ungapped residues (nearly always only used for
83 public Sequence(String name, String sequence, int start, int end)
86 this.sequence = sequence.toCharArray();
93 public Sequence(String name, char[] sequence, int start, int end)
96 this.sequence = sequence;
103 com.stevesoft.pat.Regex limitrx = new com.stevesoft.pat.Regex(
104 "[/][0-9]{1,}[-][0-9]{1,}$");
106 com.stevesoft.pat.Regex endrx = new com.stevesoft.pat.Regex("[0-9]{1,}$");
113 .println("POSSIBLE IMPLEMENTATION ERROR: null sequence name passed to constructor.");
116 // Does sequence have the /start-end signiature?
117 if (limitrx.search(name))
119 name = limitrx.left();
120 endrx.search(limitrx.stringMatched());
121 setStart(Integer.parseInt(limitrx.stringMatched().substring(1,
122 endrx.matchedFrom() - 1)));
123 setEnd(Integer.parseInt(endrx.stringMatched()));
127 void checkValidRange()
130 // http://issues.jalview.org/browse/JAL-774?focusedCommentId=11239&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-11239
133 for (int j = 0; j < sequence.length; j++)
135 if (!jalview.util.Comparison.isGap(sequence[j]))
154 * Creates a new Sequence object.
161 public Sequence(String name, String sequence)
163 this(name, sequence, 1, -1);
167 * Creates a new Sequence object with new features, DBRefEntries,
168 * AlignmentAnnotations, and PDBIds but inherits any existing dataset sequence
174 public Sequence(SequenceI seq)
176 this(seq, seq.getAnnotation());
180 * Create a new sequence object with new features, DBRefEntries, and PDBIds
181 * but inherits any existing dataset sequence reference, and duplicate of any
182 * annotation that is present in the given annotation array.
185 * the sequence to be copied
186 * @param alAnnotation
187 * an array of annotation including some associated with seq
189 public Sequence(SequenceI seq, AlignmentAnnotation[] alAnnotation)
191 this(seq.getName(), seq.getSequence(), seq.getStart(), seq.getEnd());
192 description = seq.getDescription();
193 if (seq.getSequenceFeatures() != null)
195 SequenceFeature[] sf = seq.getSequenceFeatures();
196 for (int i = 0; i < sf.length; i++)
198 addSequenceFeature(new SequenceFeature(sf[i]));
201 setDatasetSequence(seq.getDatasetSequence());
202 if (datasetSequence == null && seq.getDBRef() != null)
204 // only copy DBRefs if we really are a dataset sequence
205 DBRefEntry[] dbr = seq.getDBRef();
206 for (int i = 0; i < dbr.length; i++)
208 addDBRef(new DBRefEntry(dbr[i]));
211 if (seq.getAnnotation() != null)
213 AlignmentAnnotation[] sqann = seq.getAnnotation();
214 for (int i = 0; i < sqann.length; i++)
216 if (sqann[i] == null)
220 boolean found = (alAnnotation == null);
223 for (int apos = 0; !found && apos < alAnnotation.length; apos++)
225 found = (alAnnotation[apos] == sqann[i]);
230 // only copy the given annotation
231 AlignmentAnnotation newann = new AlignmentAnnotation(sqann[i]);
232 addAlignmentAnnotation(newann);
236 if (seq.getPDBId() != null)
238 Vector ids = seq.getPDBId();
239 Enumeration e = ids.elements();
240 while (e.hasMoreElements())
242 this.addPDBId(new PDBEntry((PDBEntry) e.nextElement()));
253 public void setSequenceFeatures(SequenceFeature[] features)
255 sequenceFeatures = features;
258 public synchronized void addSequenceFeature(SequenceFeature sf)
260 if (sequenceFeatures == null)
262 sequenceFeatures = new SequenceFeature[0];
265 for (int i = 0; i < sequenceFeatures.length; i++)
267 if (sequenceFeatures[i].equals(sf))
273 SequenceFeature[] temp = new SequenceFeature[sequenceFeatures.length + 1];
274 System.arraycopy(sequenceFeatures, 0, temp, 0, sequenceFeatures.length);
275 temp[sequenceFeatures.length] = sf;
277 sequenceFeatures = temp;
280 public void deleteFeature(SequenceFeature sf)
282 if (sequenceFeatures == null)
288 for (index = 0; index < sequenceFeatures.length; index++)
290 if (sequenceFeatures[index].equals(sf))
296 if (index == sequenceFeatures.length)
301 int sfLength = sequenceFeatures.length;
304 sequenceFeatures = null;
308 SequenceFeature[] temp = new SequenceFeature[sfLength - 1];
309 System.arraycopy(sequenceFeatures, 0, temp, 0, index);
311 if (index < sfLength)
313 System.arraycopy(sequenceFeatures, index + 1, temp, index,
314 sequenceFeatures.length - index - 1);
317 sequenceFeatures = temp;
324 * @return DOCUMENT ME!
326 public SequenceFeature[] getSequenceFeatures()
328 return sequenceFeatures;
331 public void addPDBId(PDBEntry entry)
335 pdbIds = new Vector();
337 if (!pdbIds.contains(entry))
339 pdbIds.addElement(entry);
349 public void setPDBId(Vector id)
357 * @return DOCUMENT ME!
359 public Vector getPDBId()
367 * @return DOCUMENT ME!
369 public String getDisplayId(boolean jvsuffix)
371 StringBuffer result = new StringBuffer(name);
374 result.append("/" + start + "-" + end);
377 return result.toString();
386 public void setName(String name)
395 * @return DOCUMENT ME!
397 public String getName()
408 public void setStart(int start)
416 * @return DOCUMENT ME!
418 public int getStart()
429 public void setEnd(int end)
437 * @return DOCUMENT ME!
447 * @return DOCUMENT ME!
449 public int getLength()
451 return this.sequence.length;
460 public void setSequence(String seq)
462 this.sequence = seq.toCharArray();
466 public String getSequenceAsString()
468 return new String(sequence);
471 public String getSequenceAsString(int start, int end)
473 return new String(getSequence(start, end));
476 public char[] getSequence()
484 * @see jalview.datamodel.SequenceI#getSequence(int, int)
486 public char[] getSequence(int start, int end)
490 // JBPNote - left to user to pad the result here (TODO:Decide on this
492 if (start >= sequence.length)
497 if (end >= sequence.length)
499 end = sequence.length;
502 char[] reply = new char[end - start];
503 System.arraycopy(sequence, start, reply, 0, end - start);
509 * make a new Sequence object from start to end (including gaps) over this
518 public SequenceI getSubSequence(int start, int end)
524 char[] seq = getSequence(start, end);
529 int nstart = findPosition(start);
530 int nend = findPosition(end) - 1;
531 // JBPNote - this is an incomplete copy.
532 SequenceI nseq = new Sequence(this.getName(), seq, nstart, nend);
533 nseq.setDescription(description);
534 if (datasetSequence != null)
536 nseq.setDatasetSequence(datasetSequence);
540 nseq.setDatasetSequence(this);
551 * @return DOCUMENT ME!
553 public char getCharAt(int i)
555 if (i < sequence.length)
571 public void setDescription(String desc)
573 this.description = desc;
579 * @return DOCUMENT ME!
581 public String getDescription()
583 return this.description;
589 * @see jalview.datamodel.SequenceI#findIndex(int)
591 public int findIndex(int pos)
593 // returns the alignment position for a residue
596 // Rely on end being at least as long as the length of the sequence.
597 while ((i < sequence.length) && (j <= end) && (j <= pos))
599 if (!jalview.util.Comparison.isGap(sequence[i]))
607 if ((j == end) && (j < pos))
618 * Returns the sequence position for an alignment position
621 * column index in alignment (from 1)
623 * @return residue number for residue (left of and) nearest ith column
625 public int findPosition(int i)
629 int seqlen = sequence.length;
630 while ((j < i) && (j < seqlen))
632 if (!jalview.util.Comparison.isGap(sequence[j]))
644 * Returns an int array where indices correspond to each residue in the
645 * sequence and the element value gives its position in the alignment
647 * @return int[SequenceI.getEnd()-SequenceI.getStart()+1] or null if no
648 * residues in SequenceI object
650 public int[] gapMap()
652 String seq = jalview.analysis.AlignSeq.extractGaps(
653 jalview.util.Comparison.GapChars, new String(sequence));
654 int[] map = new int[seq.length()];
658 while (j < sequence.length)
660 if (!jalview.util.Comparison.isGap(sequence[j]))
674 * @see jalview.datamodel.SequenceI#findPositionMap()
676 public int[] findPositionMap()
678 int map[] = new int[sequence.length];
681 int seqlen = sequence.length;
685 if (!jalview.util.Comparison.isGap(sequence[j]))
698 * @see jalview.datamodel.SequenceI#deleteChars(int, int)
700 public void deleteChars(int i, int j)
702 int newstart = start, newend = end;
703 if (i >= sequence.length)
710 if (j >= sequence.length)
713 System.arraycopy(sequence, 0, tmp, 0, i);
717 tmp = new char[sequence.length - j + i];
718 System.arraycopy(sequence, 0, tmp, 0, i);
719 System.arraycopy(sequence, j, tmp, i, sequence.length - j);
721 boolean createNewDs = false;
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)
1208 public void setRNA(RNA r){rna=r;}
1210 public RNA getRNA() { return rna; }