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 jalview.analysis.AlignSeq;
24 import jalview.util.StringUtils;
26 import java.util.ArrayList;
27 import java.util.Enumeration;
28 import java.util.List;
29 import java.util.Vector;
31 import fr.orsay.lri.varna.models.rna.RNA;
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.getAllPDBEntries() != null)
264 Vector<PDBEntry> ids = seq.getAllPDBEntries();
265 for (PDBEntry pdb : ids)
267 this.addPDBId(new PDBEntry(pdb));
279 public void setSequenceFeatures(SequenceFeature[] features)
281 sequenceFeatures = features;
285 public synchronized void addSequenceFeature(SequenceFeature sf)
287 if (sequenceFeatures == null)
289 sequenceFeatures = new SequenceFeature[0];
292 for (int i = 0; i < sequenceFeatures.length; i++)
294 if (sequenceFeatures[i].equals(sf))
300 SequenceFeature[] temp = new SequenceFeature[sequenceFeatures.length + 1];
301 System.arraycopy(sequenceFeatures, 0, temp, 0, sequenceFeatures.length);
302 temp[sequenceFeatures.length] = sf;
304 sequenceFeatures = temp;
308 public void deleteFeature(SequenceFeature sf)
310 if (sequenceFeatures == null)
316 for (index = 0; index < sequenceFeatures.length; index++)
318 if (sequenceFeatures[index].equals(sf))
324 if (index == sequenceFeatures.length)
329 int sfLength = sequenceFeatures.length;
332 sequenceFeatures = null;
336 SequenceFeature[] temp = new SequenceFeature[sfLength - 1];
337 System.arraycopy(sequenceFeatures, 0, temp, 0, index);
339 if (index < sfLength)
341 System.arraycopy(sequenceFeatures, index + 1, temp, index,
342 sequenceFeatures.length - index - 1);
345 sequenceFeatures = temp;
350 * Returns the sequence features (if any), looking first on the sequence, then
351 * on its dataset sequence, and so on until a non-null value is found (or
352 * none). This supports retrieval of sequence features stored on the sequence
353 * (as in the applet) or on the dataset sequence (as in the Desktop version).
358 public SequenceFeature[] getSequenceFeatures()
360 SequenceFeature[] features = sequenceFeatures;
362 SequenceI seq = this;
363 int count = 0; // failsafe against loop in sequence.datasetsequence...
364 while (features == null && seq.getDatasetSequence() != null
367 seq = seq.getDatasetSequence();
368 features = ((Sequence) seq).sequenceFeatures;
374 public void addPDBId(PDBEntry entry)
378 pdbIds = new Vector<PDBEntry>();
380 if (pdbIds.contains(entry))
382 updatePDBEntry(pdbIds.get(pdbIds.indexOf(entry)), entry);
386 pdbIds.addElement(entry);
390 private static void updatePDBEntry(PDBEntry oldEntry, PDBEntry newEntry)
392 if (newEntry.getFile() != null)
394 oldEntry.setFile(newEntry.getFile());
405 public void setPDBId(Vector<PDBEntry> id)
413 * @return DOCUMENT ME!
416 public Vector<PDBEntry> getAllPDBEntries()
424 * @return DOCUMENT ME!
427 public String getDisplayId(boolean jvsuffix)
429 StringBuffer result = new StringBuffer(name);
432 result.append("/" + start + "-" + end);
435 return result.toString();
445 public void setName(String name)
454 * @return DOCUMENT ME!
457 public String getName()
469 public void setStart(int start)
477 * @return DOCUMENT ME!
480 public int getStart()
492 public void setEnd(int end)
500 * @return DOCUMENT ME!
511 * @return DOCUMENT ME!
514 public int getLength()
516 return this.sequence.length;
526 public void setSequence(String seq)
528 this.sequence = seq.toCharArray();
533 public String getSequenceAsString()
535 return new String(sequence);
539 public String getSequenceAsString(int start, int end)
541 return new String(getSequence(start, end));
545 public char[] getSequence()
553 * @see jalview.datamodel.SequenceI#getSequence(int, int)
556 public char[] getSequence(int start, int end)
562 // JBPNote - left to user to pad the result here (TODO:Decide on this
564 if (start >= sequence.length)
569 if (end >= sequence.length)
571 end = sequence.length;
574 char[] reply = new char[end - start];
575 System.arraycopy(sequence, start, reply, 0, end - start);
581 public SequenceI getSubSequence(int start, int end)
587 char[] seq = getSequence(start, end);
592 int nstart = findPosition(start);
593 int nend = findPosition(end) - 1;
594 // JBPNote - this is an incomplete copy.
595 SequenceI nseq = new Sequence(this.getName(), seq, nstart, nend);
596 nseq.setDescription(description);
597 if (datasetSequence != null)
599 nseq.setDatasetSequence(datasetSequence);
603 nseq.setDatasetSequence(this);
614 * @return DOCUMENT ME!
617 public char getCharAt(int i)
619 if (i < sequence.length)
636 public void setDescription(String desc)
638 this.description = desc;
644 * @return DOCUMENT ME!
647 public String getDescription()
649 return this.description;
655 * @see jalview.datamodel.SequenceI#findIndex(int)
658 public int findIndex(int pos)
660 // returns the alignment position for a residue
663 // Rely on end being at least as long as the length of the sequence.
664 while ((i < sequence.length) && (j <= end) && (j <= pos))
666 if (!jalview.util.Comparison.isGap(sequence[i]))
674 if ((j == end) && (j < pos))
685 public int findPosition(int i)
689 int seqlen = sequence.length;
690 while ((j < i) && (j < seqlen))
692 if (!jalview.util.Comparison.isGap(sequence[j]))
704 * Returns an int array where indices correspond to each residue in the
705 * sequence and the element value gives its position in the alignment
707 * @return int[SequenceI.getEnd()-SequenceI.getStart()+1] or null if no
708 * residues in SequenceI object
711 public int[] gapMap()
713 String seq = jalview.analysis.AlignSeq.extractGaps(
714 jalview.util.Comparison.GapChars, new String(sequence));
715 int[] map = new int[seq.length()];
719 while (j < sequence.length)
721 if (!jalview.util.Comparison.isGap(sequence[j]))
733 public int[] findPositionMap()
735 int map[] = new int[sequence.length];
738 int seqlen = sequence.length;
742 if (!jalview.util.Comparison.isGap(sequence[j]))
753 public List<int[]> getInsertions()
755 ArrayList<int[]> map = new ArrayList<int[]>();
756 int lastj = -1, j = 0;
758 int seqlen = sequence.length;
761 if (jalview.util.Comparison.isGap(sequence[j]))
772 map.add(new int[] { lastj, j - 1 });
780 map.add(new int[] { lastj, j - 1 });
787 public void deleteChars(int i, int j)
789 int newstart = start, newend = end;
790 if (i >= sequence.length || i < 0)
795 char[] tmp = StringUtils.deleteChars(sequence, i, j);
796 boolean createNewDs = false;
797 // TODO: take a (second look) at the dataset creation validation method for
798 // the very large sequence case
799 int eindex = -1, sindex = -1;
800 boolean ecalc = false, scalc = false;
801 for (int s = i; s < j; s++)
803 if (jalview.schemes.ResidueProperties.aaIndex[sequence[s]] != 23)
813 sindex = findIndex(start) - 1;
818 // delete characters including start of sequence
819 newstart = findPosition(j);
820 break; // don't need to search for any more residue characters.
824 // delete characters after start.
827 eindex = findIndex(end) - 1;
832 // delete characters at end of sequence
833 newend = findPosition(i - 1);
834 break; // don't need to search for any more residue characters.
839 newend--; // decrease end position by one for the deleted residue
840 // and search further
846 // deletion occured in the middle of the sequence
847 if (createNewDs && this.datasetSequence != null)
849 // construct a new sequence
850 Sequence ds = new Sequence(datasetSequence);
851 // TODO: remove any non-inheritable properties ?
852 // TODO: create a sequence mapping (since there is a relation here ?)
853 ds.deleteChars(i, j);
854 datasetSequence = ds;
862 public void insertCharAt(int i, int length, char c)
864 char[] tmp = new char[sequence.length + length];
866 if (i >= sequence.length)
868 System.arraycopy(sequence, 0, tmp, 0, sequence.length);
873 System.arraycopy(sequence, 0, tmp, 0, i);
883 if (i < sequence.length)
885 System.arraycopy(sequence, i, tmp, index, sequence.length - i);
892 public void insertCharAt(int i, char c)
894 insertCharAt(i, 1, c);
898 public String getVamsasId()
904 public void setVamsasId(String id)
910 public void setDBRef(DBRefEntry[] dbref)
916 public DBRefEntry[] getDBRef()
918 if (dbrefs == null && datasetSequence != null
919 && this != datasetSequence)
921 return datasetSequence.getDBRef();
927 public void addDBRef(DBRefEntry entry)
931 dbrefs = new DBRefEntry[0];
934 int i, iSize = dbrefs.length;
936 for (i = 0; i < iSize; i++)
938 if (dbrefs[i].equalRef(entry))
940 if (entry.getMap() != null)
942 if (dbrefs[i].getMap() == null)
944 // overwrite with 'superior' entry that contains a mapping.
952 DBRefEntry[] temp = new DBRefEntry[iSize + 1];
953 System.arraycopy(dbrefs, 0, temp, 0, iSize);
954 temp[temp.length - 1] = entry;
960 public void setDatasetSequence(SequenceI seq)
962 // TODO check for circular reference before setting?
963 datasetSequence = seq;
967 public SequenceI getDatasetSequence()
969 return datasetSequence;
973 public AlignmentAnnotation[] getAnnotation()
975 return annotation == null ? null : annotation
976 .toArray(new AlignmentAnnotation[annotation.size()]);
980 public boolean hasAnnotation(AlignmentAnnotation ann)
982 return annotation == null ? false : annotation.contains(ann);
986 public void addAlignmentAnnotation(AlignmentAnnotation annotation)
988 if (this.annotation == null)
990 this.annotation = new Vector<AlignmentAnnotation>();
992 if (!this.annotation.contains(annotation))
994 this.annotation.addElement(annotation);
996 annotation.setSequenceRef(this);
1000 public void removeAlignmentAnnotation(AlignmentAnnotation annotation)
1002 if (this.annotation != null)
1004 this.annotation.removeElement(annotation);
1005 if (this.annotation.size() == 0)
1007 this.annotation = null;
1013 * test if this is a valid candidate for another sequence's dataset sequence.
1016 private boolean isValidDatasetSequence()
1018 if (datasetSequence != null)
1022 for (int i = 0; i < sequence.length; i++)
1024 if (jalview.util.Comparison.isGap(sequence[i]))
1033 public SequenceI deriveSequence()
1035 SequenceI seq = new Sequence(this);
1036 if (datasetSequence != null)
1038 // duplicate current sequence with same dataset
1039 seq.setDatasetSequence(datasetSequence);
1043 if (isValidDatasetSequence())
1045 // Use this as dataset sequence
1046 seq.setDatasetSequence(this);
1050 // Create a new, valid dataset sequence
1052 ds.setSequence(AlignSeq.extractGaps(
1053 jalview.util.Comparison.GapChars, new String(sequence)));
1054 setDatasetSequence(ds);
1055 ds.setSequenceFeatures(getSequenceFeatures());
1056 seq = this; // and return this sequence as the derived sequence.
1065 * @see jalview.datamodel.SequenceI#createDatasetSequence()
1068 public SequenceI createDatasetSequence()
1070 if (datasetSequence == null)
1072 datasetSequence = new Sequence(getName(), AlignSeq.extractGaps(
1073 jalview.util.Comparison.GapChars, getSequenceAsString()),
1074 getStart(), getEnd());
1075 datasetSequence.setSequenceFeatures(getSequenceFeatures());
1076 datasetSequence.setDescription(getDescription());
1077 setSequenceFeatures(null);
1078 // move database references onto dataset sequence
1079 datasetSequence.setDBRef(getDBRef());
1081 datasetSequence.setPDBId(getAllPDBEntries());
1083 datasetSequence.updatePDBIds();
1084 if (annotation != null)
1086 for (AlignmentAnnotation aa : annotation)
1088 AlignmentAnnotation _aa = new AlignmentAnnotation(aa);
1089 _aa.sequenceRef = datasetSequence;
1090 _aa.adjustForAlignment(); // uses annotation's own record of
1091 // sequence-column mapping
1092 datasetSequence.addAlignmentAnnotation(_aa);
1096 return datasetSequence;
1103 * jalview.datamodel.SequenceI#setAlignmentAnnotation(AlignmmentAnnotation[]
1107 public void setAlignmentAnnotation(AlignmentAnnotation[] annotations)
1109 if (annotation != null)
1111 annotation.removeAllElements();
1113 if (annotations != null)
1115 for (int i = 0; i < annotations.length; i++)
1117 if (annotations[i] != null)
1119 addAlignmentAnnotation(annotations[i]);
1126 public AlignmentAnnotation[] getAnnotation(String label)
1128 if (annotation == null || annotation.size() == 0)
1133 Vector subset = new Vector();
1134 Enumeration e = annotation.elements();
1135 while (e.hasMoreElements())
1137 AlignmentAnnotation ann = (AlignmentAnnotation) e.nextElement();
1138 if (ann.label != null && ann.label.equals(label))
1140 subset.addElement(ann);
1143 if (subset.size() == 0)
1147 AlignmentAnnotation[] anns = new AlignmentAnnotation[subset.size()];
1149 e = subset.elements();
1150 while (e.hasMoreElements())
1152 anns[i++] = (AlignmentAnnotation) e.nextElement();
1154 subset.removeAllElements();
1159 public boolean updatePDBIds()
1161 if (datasetSequence != null)
1163 // TODO: could merge DBRefs
1164 return datasetSequence.updatePDBIds();
1166 if (dbrefs == null || dbrefs.length == 0)
1170 Vector newpdb = new Vector();
1171 for (int i = 0; i < dbrefs.length; i++)
1173 if (DBRefSource.PDB.equals(dbrefs[i].getSource()))
1175 PDBEntry pdbe = new PDBEntry();
1176 pdbe.setId(dbrefs[i].getAccessionId());
1177 if (pdbIds == null || pdbIds.size() == 0)
1179 newpdb.addElement(pdbe);
1183 Enumeration en = pdbIds.elements();
1184 boolean matched = false;
1185 while (!matched && en.hasMoreElements())
1187 PDBEntry anentry = (PDBEntry) en.nextElement();
1188 if (anentry.getId().equals(pdbe.getId()))
1195 newpdb.addElement(pdbe);
1200 if (newpdb.size() > 0)
1202 Enumeration en = newpdb.elements();
1203 while (en.hasMoreElements())
1205 addPDBId((PDBEntry) en.nextElement());
1213 public void transferAnnotation(SequenceI entry, Mapping mp)
1215 if (datasetSequence != null)
1217 datasetSequence.transferAnnotation(entry, mp);
1220 if (entry.getDatasetSequence() != null)
1222 transferAnnotation(entry.getDatasetSequence(), mp);
1225 // transfer any new features from entry onto sequence
1226 if (entry.getSequenceFeatures() != null)
1229 SequenceFeature[] sfs = entry.getSequenceFeatures();
1230 for (int si = 0; si < sfs.length; si++)
1232 SequenceFeature sf[] = (mp != null) ? mp.locateFeature(sfs[si])
1233 : new SequenceFeature[] { new SequenceFeature(sfs[si]) };
1234 if (sf != null && sf.length > 0)
1236 for (int sfi = 0; sfi < sf.length; sfi++)
1238 addSequenceFeature(sf[sfi]);
1244 // transfer PDB entries
1245 if (entry.getAllPDBEntries() != null)
1247 Enumeration e = entry.getAllPDBEntries().elements();
1248 while (e.hasMoreElements())
1250 PDBEntry pdb = (PDBEntry) e.nextElement();
1254 // transfer database references
1255 DBRefEntry[] entryRefs = entry.getDBRef();
1256 if (entryRefs != null)
1258 for (int r = 0; r < entryRefs.length; r++)
1260 DBRefEntry newref = new DBRefEntry(entryRefs[r]);
1261 if (newref.getMap() != null && mp != null)
1263 // remap ref using our local mapping
1265 // we also assume all version string setting is done by dbSourceProxy
1267 * if (!newref.getSource().equalsIgnoreCase(dbSource)) {
1268 * newref.setSource(dbSource); }
1276 * @return The index (zero-based) on this sequence in the MSA. It returns
1277 * {@code -1} if this information is not available.
1280 public int getIndex()
1286 * Defines the position of this sequence in the MSA. Use the value {@code -1}
1287 * if this information is undefined.
1290 * position for this sequence. This value is zero-based (zero for
1291 * this first sequence)
1294 public void setIndex(int value)
1300 public void setRNA(RNA r)
1312 public List<AlignmentAnnotation> getAlignmentAnnotations(String calcId,
1315 List<AlignmentAnnotation> result = new ArrayList<AlignmentAnnotation>();
1316 if (this.annotation != null)
1318 for (AlignmentAnnotation ann : annotation)
1320 if (ann.calcId != null && ann.calcId.equals(calcId)
1321 && ann.label != null && ann.label.equals(label))
1331 public String toString()
1333 return getDisplayId(false);
1337 public PDBEntry getPDBEntry(String pdbIdStr)
1339 if (getDatasetSequence() == null
1340 || getDatasetSequence().getAllPDBEntries() == null)
1344 List<PDBEntry> entries = getDatasetSequence().getAllPDBEntries();
1345 for (PDBEntry entry : entries)
1347 if (entry.getId().equalsIgnoreCase(pdbIdStr))