2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.datamodel;
23 import java.util.ArrayList;
24 import java.util.Enumeration;
25 import java.util.List;
26 import java.util.Vector;
28 import fr.orsay.lri.varna.models.rna.RNA;
30 import jalview.analysis.AlignSeq;
31 import jalview.util.StringUtils;
35 * Implements the SequenceI interface for a char[] based sequence object.
40 public class Sequence extends ASequence implements SequenceI
42 SequenceI datasetSequence;
46 private char[] sequence;
54 Vector<PDBEntry> pdbIds;
63 * This annotation is displayed below the alignment but the positions are tied
64 * to the residues of this sequence
66 * TODO: change to List<>
68 Vector<AlignmentAnnotation> annotation;
71 * The index of the sequence in a MSA
75 /** array of sequence features - may not be null for a valid sequence object */
76 public SequenceFeature[] sequenceFeatures;
79 * Creates a new Sequence object.
84 * string to form a possibly gapped sequence out of
86 * first position of non-gap residue in the sequence
88 * last position of ungapped residues (nearly always only used for
91 public Sequence(String name, String sequence, int start, int end)
93 initSeqAndName(name, sequence.toCharArray(), start, end);
96 public Sequence(String name, char[] sequence, int start, int end)
98 initSeqAndName(name, sequence, start, end);
102 * Stage 1 constructor - assign name, sequence, and set start and end fields.
103 * start and end are updated values from name2 if it ends with /start-end
110 protected void initSeqAndName(String name2, char[] sequence2, int start2,
114 this.sequence = sequence2;
121 com.stevesoft.pat.Regex limitrx = new com.stevesoft.pat.Regex(
122 "[/][0-9]{1,}[-][0-9]{1,}$");
124 com.stevesoft.pat.Regex endrx = new com.stevesoft.pat.Regex("[0-9]{1,}$");
131 .println("POSSIBLE IMPLEMENTATION ERROR: null sequence name passed to constructor.");
134 // Does sequence have the /start-end signature?
135 if (limitrx.search(name))
137 name = limitrx.left();
138 endrx.search(limitrx.stringMatched());
139 setStart(Integer.parseInt(limitrx.stringMatched().substring(1,
140 endrx.matchedFrom() - 1)));
141 setEnd(Integer.parseInt(endrx.stringMatched()));
145 void checkValidRange()
148 // http://issues.jalview.org/browse/JAL-774?focusedCommentId=11239&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-11239
151 for (int j = 0; j < sequence.length; j++)
153 if (!jalview.util.Comparison.isGap(sequence[j]))
172 * Creates a new Sequence object.
179 public Sequence(String name, String sequence)
181 this(name, sequence, 1, -1);
185 * Creates a new Sequence object with new features, DBRefEntries,
186 * AlignmentAnnotations, and PDBIds but inherits any existing dataset sequence
192 public Sequence(SequenceI seq)
194 this(seq, seq.getAnnotation());
198 * Create a new sequence object with new features, DBRefEntries, and PDBIds
199 * but inherits any existing dataset sequence reference, and duplicate of any
200 * annotation that is present in the given annotation array.
203 * the sequence to be copied
204 * @param alAnnotation
205 * an array of annotation including some associated with seq
207 public Sequence(SequenceI seq, AlignmentAnnotation[] alAnnotation)
209 initSeqFrom(seq, alAnnotation);
213 protected void initSeqFrom(SequenceI seq,
214 AlignmentAnnotation[] alAnnotation)
216 initSeqAndName(seq.getName(), seq.getSequence(), seq.getStart(),
218 description = seq.getDescription();
219 if (seq.getSequenceFeatures() != null)
221 SequenceFeature[] sf = seq.getSequenceFeatures();
222 for (int i = 0; i < sf.length; i++)
224 addSequenceFeature(new SequenceFeature(sf[i]));
227 setDatasetSequence(seq.getDatasetSequence());
228 if (datasetSequence == null && seq.getDBRef() != null)
230 // only copy DBRefs if we really are a dataset sequence
231 DBRefEntry[] dbr = seq.getDBRef();
232 for (int i = 0; i < dbr.length; i++)
234 addDBRef(new DBRefEntry(dbr[i]));
237 if (seq.getAnnotation() != null)
239 AlignmentAnnotation[] sqann = seq.getAnnotation();
240 for (int i = 0; i < sqann.length; i++)
242 if (sqann[i] == null)
246 boolean found = (alAnnotation == null);
249 for (int apos = 0; !found && apos < alAnnotation.length; apos++)
251 found = (alAnnotation[apos] == sqann[i]);
256 // only copy the given annotation
257 AlignmentAnnotation newann = new AlignmentAnnotation(sqann[i]);
258 addAlignmentAnnotation(newann);
262 if (seq.getPDBId() != null)
264 Vector ids = seq.getPDBId();
265 Enumeration e = ids.elements();
266 while (e.hasMoreElements())
268 this.addPDBId(new PDBEntry((PDBEntry) e.nextElement()));
279 public void setSequenceFeatures(SequenceFeature[] features)
281 sequenceFeatures = features;
284 public synchronized void addSequenceFeature(SequenceFeature sf)
286 if (sequenceFeatures == null)
288 sequenceFeatures = new SequenceFeature[0];
291 for (int i = 0; i < sequenceFeatures.length; i++)
293 if (sequenceFeatures[i].equals(sf))
299 SequenceFeature[] temp = new SequenceFeature[sequenceFeatures.length + 1];
300 System.arraycopy(sequenceFeatures, 0, temp, 0, sequenceFeatures.length);
301 temp[sequenceFeatures.length] = sf;
303 sequenceFeatures = temp;
306 public void deleteFeature(SequenceFeature sf)
308 if (sequenceFeatures == null)
314 for (index = 0; index < sequenceFeatures.length; index++)
316 if (sequenceFeatures[index].equals(sf))
322 if (index == sequenceFeatures.length)
327 int sfLength = sequenceFeatures.length;
330 sequenceFeatures = null;
334 SequenceFeature[] temp = new SequenceFeature[sfLength - 1];
335 System.arraycopy(sequenceFeatures, 0, temp, 0, index);
337 if (index < sfLength)
339 System.arraycopy(sequenceFeatures, index + 1, temp, index,
340 sequenceFeatures.length - index - 1);
343 sequenceFeatures = temp;
348 * Returns the sequence features (if any), looking first on the sequence, then
349 * on its dataset sequence, and so on until a non-null value is found (or
350 * none). This supports retrieval of sequence features stored on the sequence
351 * (as in the applet) or on the dataset sequence (as in the Desktop version).
355 public SequenceFeature[] getSequenceFeatures()
357 SequenceFeature[] features = sequenceFeatures;
359 SequenceI seq = this;
360 int count = 0; // failsafe against loop in sequence.datasetsequence...
361 while (features == null && seq.getDatasetSequence() != null
364 seq = seq.getDatasetSequence();
365 features = ((Sequence) seq).sequenceFeatures;
370 public void addPDBId(PDBEntry entry)
374 pdbIds = new Vector<PDBEntry>();
376 if (pdbIds.contains(entry))
378 updatePDBEntry(pdbIds.get(pdbIds.indexOf(entry)), entry);
382 pdbIds.addElement(entry);
386 private static void updatePDBEntry(PDBEntry oldEntry, PDBEntry newEntry)
388 if (newEntry.getFile() != null)
390 oldEntry.setFile(newEntry.getFile());
401 public void setPDBId(Vector<PDBEntry> id)
409 * @return DOCUMENT ME!
412 public Vector<PDBEntry> getPDBId()
420 * @return DOCUMENT ME!
422 public String getDisplayId(boolean jvsuffix)
424 StringBuffer result = new StringBuffer(name);
427 result.append("/" + start + "-" + end);
430 return result.toString();
439 public void setName(String name)
448 * @return DOCUMENT ME!
450 public String getName()
461 public void setStart(int start)
469 * @return DOCUMENT ME!
471 public int getStart()
482 public void setEnd(int end)
490 * @return DOCUMENT ME!
500 * @return DOCUMENT ME!
502 public int getLength()
504 return this.sequence.length;
513 public void setSequence(String seq)
515 this.sequence = seq.toCharArray();
519 public String getSequenceAsString()
521 return new String(sequence);
524 public String getSequenceAsString(int start, int end)
526 return new String(getSequence(start, end));
529 public char[] getSequence()
537 * @see jalview.datamodel.SequenceI#getSequence(int, int)
539 public char[] getSequence(int start, int end)
545 // JBPNote - left to user to pad the result here (TODO:Decide on this
547 if (start >= sequence.length)
552 if (end >= sequence.length)
554 end = sequence.length;
557 char[] reply = new char[end - start];
558 System.arraycopy(sequence, start, reply, 0, end - start);
564 public SequenceI getSubSequence(int start, int end)
570 char[] seq = getSequence(start, end);
575 int nstart = findPosition(start);
576 int nend = findPosition(end) - 1;
577 // JBPNote - this is an incomplete copy.
578 SequenceI nseq = new Sequence(this.getName(), seq, nstart, nend);
579 nseq.setDescription(description);
580 if (datasetSequence != null)
582 nseq.setDatasetSequence(datasetSequence);
586 nseq.setDatasetSequence(this);
597 * @return DOCUMENT ME!
599 public char getCharAt(int i)
601 if (i < sequence.length)
617 public void setDescription(String desc)
619 this.description = desc;
625 * @return DOCUMENT ME!
627 public String getDescription()
629 return this.description;
635 * @see jalview.datamodel.SequenceI#findIndex(int)
637 public int findIndex(int pos)
639 // returns the alignment position for a residue
642 // Rely on end being at least as long as the length of the sequence.
643 while ((i < sequence.length) && (j <= end) && (j <= pos))
645 if (!jalview.util.Comparison.isGap(sequence[i]))
653 if ((j == end) && (j < pos))
664 public int findPosition(int i)
668 int seqlen = sequence.length;
669 while ((j < i) && (j < seqlen))
671 if (!jalview.util.Comparison.isGap(sequence[j]))
683 * Returns an int array where indices correspond to each residue in the
684 * sequence and the element value gives its position in the alignment
686 * @return int[SequenceI.getEnd()-SequenceI.getStart()+1] or null if no
687 * residues in SequenceI object
689 public int[] gapMap()
691 String seq = jalview.analysis.AlignSeq.extractGaps(
692 jalview.util.Comparison.GapChars, new String(sequence));
693 int[] map = new int[seq.length()];
697 while (j < sequence.length)
699 if (!jalview.util.Comparison.isGap(sequence[j]))
711 public int[] findPositionMap()
713 int map[] = new int[sequence.length];
716 int seqlen = sequence.length;
720 if (!jalview.util.Comparison.isGap(sequence[j]))
731 public List<int[]> getInsertions()
733 ArrayList<int[]> map = new ArrayList<int[]>();
734 int lastj = -1, j = 0;
736 int seqlen = sequence.length;
739 if (jalview.util.Comparison.isGap(sequence[j]))
767 public void deleteChars(int i, int j)
769 int newstart = start, newend = end;
770 if (i >= sequence.length)
775 char[] tmp = StringUtils.deleteChars(sequence, i, j);
776 boolean createNewDs = false;
777 // TODO: take a (second look) at the dataset creation validation method for
778 // the very large sequence case
779 int eindex = -1, sindex = -1;
780 boolean ecalc = false, scalc = false;
781 for (int s = i; s < j; s++)
783 if (jalview.schemes.ResidueProperties.aaIndex[sequence[s]] != 23)
793 sindex = findIndex(start) - 1;
798 // delete characters including start of sequence
799 newstart = findPosition(j);
800 break; // don't need to search for any more residue characters.
804 // delete characters after start.
807 eindex = findIndex(end) - 1;
812 // delete characters at end of sequence
813 newend = findPosition(i - 1);
814 break; // don't need to search for any more residue characters.
819 newend--; // decrease end position by one for the deleted residue
820 // and search further
826 // deletion occured in the middle of the sequence
827 if (createNewDs && this.datasetSequence != null)
829 // construct a new sequence
830 Sequence ds = new Sequence(datasetSequence);
831 // TODO: remove any non-inheritable properties ?
832 // TODO: create a sequence mapping (since there is a relation here ?)
833 ds.deleteChars(i, j);
834 datasetSequence = ds;
842 public void insertCharAt(int i, int length, char c)
844 char[] tmp = new char[sequence.length + length];
846 if (i >= sequence.length)
848 System.arraycopy(sequence, 0, tmp, 0, sequence.length);
853 System.arraycopy(sequence, 0, tmp, 0, i);
863 if (i < sequence.length)
865 System.arraycopy(sequence, i, tmp, index, sequence.length - i);
872 public void insertCharAt(int i, char c)
874 insertCharAt(i, 1, c);
878 public String getVamsasId()
884 public void setVamsasId(String id)
890 public void setDBRef(DBRefEntry[] dbref)
896 public DBRefEntry[] getDBRef()
898 if (dbrefs == null && datasetSequence != null
899 && this != datasetSequence)
901 return datasetSequence.getDBRef();
907 public void addDBRef(DBRefEntry entry)
911 dbrefs = new DBRefEntry[0];
914 int i, iSize = dbrefs.length;
916 for (i = 0; i < iSize; i++)
918 if (dbrefs[i].equalRef(entry))
920 if (entry.getMap() != null)
922 if (dbrefs[i].getMap() == null)
924 // overwrite with 'superior' entry that contains a mapping.
932 DBRefEntry[] temp = new DBRefEntry[iSize + 1];
933 System.arraycopy(dbrefs, 0, temp, 0, iSize);
934 temp[temp.length - 1] = entry;
940 public void setDatasetSequence(SequenceI seq)
942 datasetSequence = seq;
946 public SequenceI getDatasetSequence()
948 return datasetSequence;
952 public AlignmentAnnotation[] getAnnotation()
954 return annotation == null ? null : annotation
955 .toArray(new AlignmentAnnotation[annotation.size()]);
960 public boolean hasAnnotation(AlignmentAnnotation ann)
962 return annotation == null ? false : annotation.contains(ann);
966 public void addAlignmentAnnotation(AlignmentAnnotation annotation)
968 if (this.annotation == null)
970 this.annotation = new Vector<AlignmentAnnotation>();
972 if (!this.annotation.contains(annotation))
974 this.annotation.addElement(annotation);
976 annotation.setSequenceRef(this);
979 public void removeAlignmentAnnotation(AlignmentAnnotation annotation)
981 if (this.annotation != null)
983 this.annotation.removeElement(annotation);
984 if (this.annotation.size() == 0)
986 this.annotation = null;
992 * test if this is a valid candidate for another sequence's dataset sequence.
995 private boolean isValidDatasetSequence()
997 if (datasetSequence != null)
1001 for (int i = 0; i < sequence.length; i++)
1003 if (jalview.util.Comparison.isGap(sequence[i]))
1012 public SequenceI deriveSequence()
1014 SequenceI seq = new Sequence(this);
1015 if (datasetSequence != null)
1017 // duplicate current sequence with same dataset
1018 seq.setDatasetSequence(datasetSequence);
1022 if (isValidDatasetSequence())
1024 // Use this as dataset sequence
1025 seq.setDatasetSequence(this);
1029 // Create a new, valid dataset sequence
1031 ds.setSequence(AlignSeq.extractGaps(
1032 jalview.util.Comparison.GapChars, new String(sequence)));
1033 setDatasetSequence(ds);
1034 ds.setSequenceFeatures(getSequenceFeatures());
1035 seq = this; // and return this sequence as the derived sequence.
1044 * @see jalview.datamodel.SequenceI#createDatasetSequence()
1046 public SequenceI createDatasetSequence()
1048 if (datasetSequence == null)
1050 datasetSequence = new Sequence(getName(), AlignSeq.extractGaps(
1051 jalview.util.Comparison.GapChars, getSequenceAsString()),
1052 getStart(), getEnd());
1053 datasetSequence.setSequenceFeatures(getSequenceFeatures());
1054 datasetSequence.setDescription(getDescription());
1055 setSequenceFeatures(null);
1056 // move database references onto dataset sequence
1057 datasetSequence.setDBRef(getDBRef());
1059 datasetSequence.setPDBId(getPDBId());
1061 datasetSequence.updatePDBIds();
1062 if (annotation != null)
1064 for (AlignmentAnnotation aa : annotation)
1066 AlignmentAnnotation _aa = new AlignmentAnnotation(aa);
1067 _aa.sequenceRef = datasetSequence;
1068 _aa.adjustForAlignment(); // uses annotation's own record of
1069 // sequence-column mapping
1070 datasetSequence.addAlignmentAnnotation(_aa);
1074 return datasetSequence;
1081 * jalview.datamodel.SequenceI#setAlignmentAnnotation(AlignmmentAnnotation[]
1084 public void setAlignmentAnnotation(AlignmentAnnotation[] annotations)
1086 if (annotation != null)
1088 annotation.removeAllElements();
1090 if (annotations != null)
1092 for (int i = 0; i < annotations.length; i++)
1094 if (annotations[i] != null)
1096 addAlignmentAnnotation(annotations[i]);
1103 public AlignmentAnnotation[] getAnnotation(String label)
1105 if (annotation == null || annotation.size() == 0)
1110 Vector subset = new Vector();
1111 Enumeration e = annotation.elements();
1112 while (e.hasMoreElements())
1114 AlignmentAnnotation ann = (AlignmentAnnotation) e.nextElement();
1115 if (ann.label != null && ann.label.equals(label))
1117 subset.addElement(ann);
1120 if (subset.size() == 0)
1124 AlignmentAnnotation[] anns = new AlignmentAnnotation[subset.size()];
1126 e = subset.elements();
1127 while (e.hasMoreElements())
1129 anns[i++] = (AlignmentAnnotation) e.nextElement();
1131 subset.removeAllElements();
1136 public boolean updatePDBIds()
1138 if (datasetSequence != null)
1140 // TODO: could merge DBRefs
1141 return datasetSequence.updatePDBIds();
1143 if (dbrefs == null || dbrefs.length == 0)
1147 Vector newpdb = new Vector();
1148 for (int i = 0; i < dbrefs.length; i++)
1150 if (DBRefSource.PDB.equals(dbrefs[i].getSource()))
1152 PDBEntry pdbe = new PDBEntry();
1153 pdbe.setId(dbrefs[i].getAccessionId());
1154 if (pdbIds == null || pdbIds.size() == 0)
1156 newpdb.addElement(pdbe);
1160 Enumeration en = pdbIds.elements();
1161 boolean matched = false;
1162 while (!matched && en.hasMoreElements())
1164 PDBEntry anentry = (PDBEntry) en.nextElement();
1165 if (anentry.getId().equals(pdbe.getId()))
1172 newpdb.addElement(pdbe);
1177 if (newpdb.size() > 0)
1179 Enumeration en = newpdb.elements();
1180 while (en.hasMoreElements())
1182 addPDBId((PDBEntry) en.nextElement());
1190 public void transferAnnotation(SequenceI entry, Mapping mp)
1192 if (datasetSequence != null)
1194 datasetSequence.transferAnnotation(entry, mp);
1197 if (entry.getDatasetSequence() != null)
1199 transferAnnotation(entry.getDatasetSequence(), mp);
1202 // transfer any new features from entry onto sequence
1203 if (entry.getSequenceFeatures() != null)
1206 SequenceFeature[] sfs = entry.getSequenceFeatures();
1207 for (int si = 0; si < sfs.length; si++)
1209 SequenceFeature sf[] = (mp != null) ? mp.locateFeature(sfs[si])
1210 : new SequenceFeature[]
1211 { new SequenceFeature(sfs[si]) };
1212 if (sf != null && sf.length > 0)
1214 for (int sfi = 0; sfi < sf.length; sfi++)
1216 addSequenceFeature(sf[sfi]);
1222 // transfer PDB entries
1223 if (entry.getPDBId() != null)
1225 Enumeration e = entry.getPDBId().elements();
1226 while (e.hasMoreElements())
1228 PDBEntry pdb = (PDBEntry) e.nextElement();
1232 // transfer database references
1233 DBRefEntry[] entryRefs = entry.getDBRef();
1234 if (entryRefs != null)
1236 for (int r = 0; r < entryRefs.length; r++)
1238 DBRefEntry newref = new DBRefEntry(entryRefs[r]);
1239 if (newref.getMap() != null && mp != null)
1241 // remap ref using our local mapping
1243 // we also assume all version string setting is done by dbSourceProxy
1245 * if (!newref.getSource().equalsIgnoreCase(dbSource)) {
1246 * newref.setSource(dbSource); }
1254 * @return The index (zero-based) on this sequence in the MSA. It returns
1255 * {@code -1} if this information is not available.
1257 public int getIndex()
1263 * Defines the position of this sequence in the MSA. Use the value {@code -1}
1264 * if this information is undefined.
1267 * position for this sequence. This value is zero-based (zero for
1268 * this first sequence)
1270 public void setIndex(int value)
1275 public void setRNA(RNA r)
1286 public List<AlignmentAnnotation> getAlignmentAnnotations(String calcId,
1289 List<AlignmentAnnotation> result = new ArrayList<AlignmentAnnotation>();
1290 if (this.annotation != null)
1292 for (AlignmentAnnotation ann : annotation)
1294 if (ann.calcId != null && ann.calcId.equals(calcId)
1295 && ann.label != null && ann.label.equals(label))