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);
718 tmp = new char[sequence.length - j + i];
719 System.arraycopy(sequence, 0, tmp, 0, i);
720 System.arraycopy(sequence, j, tmp, i, sequence.length - j);
722 boolean createNewDs = false;
723 // TODO: take a look at the new dataset creation validation method below -
724 // this could become time comsuming for large sequences - consider making it
726 for (int s = i; s < j; s++)
728 if (jalview.schemes.ResidueProperties.aaIndex[sequence[s]] != 23)
736 int sindex = findIndex(start) - 1;
739 // delete characters including start of sequence
740 newstart = findPosition(j);
741 break; // don't need to search for any more residue characters.
745 // delete characters after start.
746 int eindex = findIndex(end) - 1;
749 // delete characters at end of sequence
750 newend = findPosition(i - 1);
751 break; // don't need to search for any more residue characters.
756 newend--; // decrease end position by one for the deleted residue
757 // and search further
763 // deletion occured in the middle of the sequence
764 if (createNewDs && this.datasetSequence != null)
766 // construct a new sequence
767 Sequence ds = new Sequence(datasetSequence);
768 // TODO: remove any non-inheritable properties ?
769 // TODO: create a sequence mapping (since there is a relation here ?)
770 ds.deleteChars(i, j);
771 datasetSequence = ds;
788 public void insertCharAt(int i, int length, char c)
790 char[] tmp = new char[sequence.length + length];
792 if (i >= sequence.length)
794 System.arraycopy(sequence, 0, tmp, 0, sequence.length);
799 System.arraycopy(sequence, 0, tmp, 0, i);
809 if (i < sequence.length)
811 System.arraycopy(sequence, i, tmp, index, sequence.length - i);
817 public void insertCharAt(int i, char c)
819 insertCharAt(i, 1, c);
822 public String getVamsasId()
827 public void setVamsasId(String id)
832 public void setDBRef(DBRefEntry[] dbref)
837 public DBRefEntry[] getDBRef()
839 if (dbrefs == null && datasetSequence != null
840 && this != datasetSequence)
842 return datasetSequence.getDBRef();
847 public void addDBRef(DBRefEntry entry)
851 dbrefs = new DBRefEntry[0];
854 int i, iSize = dbrefs.length;
856 for (i = 0; i < iSize; i++)
858 if (dbrefs[i].equalRef(entry))
860 if (entry.getMap() != null)
862 if (dbrefs[i].getMap() == null)
864 // overwrite with 'superior' entry that contains a mapping.
872 DBRefEntry[] temp = new DBRefEntry[iSize + 1];
873 System.arraycopy(dbrefs, 0, temp, 0, iSize);
874 temp[temp.length - 1] = entry;
879 public void setDatasetSequence(SequenceI seq)
881 datasetSequence = seq;
884 public SequenceI getDatasetSequence()
886 return datasetSequence;
889 public AlignmentAnnotation[] getAnnotation()
891 if (annotation == null)
896 AlignmentAnnotation[] ret = new AlignmentAnnotation[annotation.size()];
897 for (int r = 0; r < ret.length; r++)
899 ret[r] = (AlignmentAnnotation) annotation.elementAt(r);
905 public void addAlignmentAnnotation(AlignmentAnnotation annotation)
907 if (this.annotation == null)
909 this.annotation = new Vector();
911 if (!this.annotation.contains(annotation))
913 this.annotation.addElement(annotation);
915 annotation.setSequenceRef(this);
918 public void removeAlignmentAnnotation(AlignmentAnnotation annotation)
920 if (this.annotation != null)
922 this.annotation.removeElement(annotation);
923 if (this.annotation.size() == 0)
924 this.annotation = null;
929 * test if this is a valid candidate for another sequence's dataset sequence.
932 private boolean isValidDatasetSequence()
934 if (datasetSequence != null)
938 for (int i = 0; i < sequence.length; i++)
940 if (jalview.util.Comparison.isGap(sequence[i]))
951 * @see jalview.datamodel.SequenceI#deriveSequence()
953 public SequenceI deriveSequence()
955 SequenceI seq = new Sequence(this);
956 if (datasetSequence != null)
958 // duplicate current sequence with same dataset
959 seq.setDatasetSequence(datasetSequence);
963 if (isValidDatasetSequence())
965 // Use this as dataset sequence
966 seq.setDatasetSequence(this);
970 // Create a new, valid dataset sequence
972 ds.setSequence(AlignSeq.extractGaps(
973 jalview.util.Comparison.GapChars, new String(sequence)));
974 setDatasetSequence(ds);
975 ds.setSequenceFeatures(getSequenceFeatures());
976 seq = this; // and return this sequence as the derived sequence.
985 * @see jalview.datamodel.SequenceI#createDatasetSequence()
987 public SequenceI createDatasetSequence()
989 if (datasetSequence == null)
991 datasetSequence = new Sequence(getName(), AlignSeq.extractGaps(
992 jalview.util.Comparison.GapChars, getSequenceAsString()),
993 getStart(), getEnd());
994 datasetSequence.setSequenceFeatures(getSequenceFeatures());
995 datasetSequence.setDescription(getDescription());
996 setSequenceFeatures(null);
997 // move database references onto dataset sequence
998 datasetSequence.setDBRef(getDBRef());
1000 datasetSequence.setPDBId(getPDBId());
1002 datasetSequence.updatePDBIds();
1004 return datasetSequence;
1011 * jalview.datamodel.SequenceI#setAlignmentAnnotation(AlignmmentAnnotation[]
1014 public void setAlignmentAnnotation(AlignmentAnnotation[] annotations)
1016 if (annotation != null)
1018 annotation.removeAllElements();
1020 if (annotations != null)
1022 for (int i = 0; i < annotations.length; i++)
1024 if (annotations[i] != null)
1025 addAlignmentAnnotation(annotations[i]);
1033 * @see jalview.datamodel.SequenceI#getAnnotation(java.lang.String)
1035 public AlignmentAnnotation[] getAnnotation(String label)
1037 if (annotation == null || annotation.size() == 0)
1042 Vector subset = new Vector();
1043 Enumeration e = annotation.elements();
1044 while (e.hasMoreElements())
1046 AlignmentAnnotation ann = (AlignmentAnnotation) e.nextElement();
1047 if (ann.label != null && ann.label.equals(label))
1049 subset.addElement(ann);
1052 if (subset.size() == 0)
1056 AlignmentAnnotation[] anns = new AlignmentAnnotation[subset.size()];
1058 e = subset.elements();
1059 while (e.hasMoreElements())
1061 anns[i++] = (AlignmentAnnotation) e.nextElement();
1063 subset.removeAllElements();
1067 public boolean updatePDBIds()
1069 if (datasetSequence != null)
1071 // TODO: could merge DBRefs
1072 return datasetSequence.updatePDBIds();
1074 if (dbrefs == null || dbrefs.length == 0)
1078 Vector newpdb = new Vector();
1079 for (int i = 0; i < dbrefs.length; i++)
1081 if (DBRefSource.PDB.equals(dbrefs[i].getSource()))
1083 PDBEntry pdbe = new PDBEntry();
1084 pdbe.setId(dbrefs[i].getAccessionId());
1085 if (pdbIds == null || pdbIds.size() == 0)
1087 newpdb.addElement(pdbe);
1091 Enumeration en = pdbIds.elements();
1092 boolean matched = false;
1093 while (!matched && en.hasMoreElements())
1095 PDBEntry anentry = (PDBEntry) en.nextElement();
1096 if (anentry.getId().equals(pdbe.getId()))
1103 newpdb.addElement(pdbe);
1108 if (newpdb.size() > 0)
1110 Enumeration en = newpdb.elements();
1111 while (en.hasMoreElements())
1113 addPDBId((PDBEntry) en.nextElement());
1124 * jalview.datamodel.SequenceI#transferAnnotation(jalview.datamodel.SequenceI,
1125 * jalview.datamodel.Mapping)
1127 public void transferAnnotation(SequenceI entry, Mapping mp)
1129 if (datasetSequence != null)
1131 datasetSequence.transferAnnotation(entry, mp);
1134 if (entry.getDatasetSequence() != null)
1136 transferAnnotation(entry.getDatasetSequence(), mp);
1139 // transfer any new features from entry onto sequence
1140 if (entry.getSequenceFeatures() != null)
1143 SequenceFeature[] sfs = entry.getSequenceFeatures();
1144 for (int si = 0; si < sfs.length; si++)
1146 SequenceFeature sf[] = (mp != null) ? mp.locateFeature(sfs[si])
1147 : new SequenceFeature[]
1148 { new SequenceFeature(sfs[si]) };
1149 if (sf != null && sf.length > 0)
1151 for (int sfi = 0; sfi < sf.length; sfi++)
1153 addSequenceFeature(sf[sfi]);
1159 // transfer PDB entries
1160 if (entry.getPDBId() != null)
1162 Enumeration e = entry.getPDBId().elements();
1163 while (e.hasMoreElements())
1165 PDBEntry pdb = (PDBEntry) e.nextElement();
1169 // transfer database references
1170 DBRefEntry[] entryRefs = entry.getDBRef();
1171 if (entryRefs != null)
1173 for (int r = 0; r < entryRefs.length; r++)
1175 DBRefEntry newref = new DBRefEntry(entryRefs[r]);
1176 if (newref.getMap() != null && mp != null)
1178 // remap ref using our local mapping
1180 // we also assume all version string setting is done by dbSourceProxy
1182 * if (!newref.getSource().equalsIgnoreCase(dbSource)) {
1183 * newref.setSource(dbSource); }
1191 * @return The index (zero-based) on this sequence in the MSA. It returns
1192 * {@code -1} if this information is not available.
1194 public int getIndex()
1200 * Defines the position of this sequence in the MSA. Use the value {@code -1}
1201 * if this information is undefined.
1204 * position for this sequence. This value is zero-based (zero for
1205 * this first sequence)
1207 public void setIndex(int value)
1212 public void setRNA(RNA r){rna=r;}
1214 public RNA getRNA() { return rna; }