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 implements SequenceI
42 SequenceI datasetSequence;
46 private char[] sequence;
54 Vector<PDBEntry> pdbIds;
64 * This annotation is displayed below the alignment but the positions are tied
65 * to the residues of this sequence
67 * TODO: change to List<>
69 Vector<AlignmentAnnotation> annotation;
72 * The index of the sequence in a MSA
76 /** array of sequence features - may not be null for a valid sequence object */
77 public SequenceFeature[] sequenceFeatures;
80 * Creates a new Sequence object.
85 * string to form a possibly gapped sequence out of
87 * first position of non-gap residue in the sequence
89 * last position of ungapped residues (nearly always only used for
92 public Sequence(String name, String sequence, int start, int end)
95 this.sequence = sequence.toCharArray();
102 public Sequence(String name, char[] sequence, int start, int end)
105 this.sequence = sequence;
112 com.stevesoft.pat.Regex limitrx = new com.stevesoft.pat.Regex(
113 "[/][0-9]{1,}[-][0-9]{1,}$");
115 com.stevesoft.pat.Regex endrx = new com.stevesoft.pat.Regex("[0-9]{1,}$");
122 .println("POSSIBLE IMPLEMENTATION ERROR: null sequence name passed to constructor.");
125 // Does sequence have the /start-end signature?
126 if (limitrx.search(name))
128 name = limitrx.left();
129 endrx.search(limitrx.stringMatched());
130 setStart(Integer.parseInt(limitrx.stringMatched().substring(1,
131 endrx.matchedFrom() - 1)));
132 setEnd(Integer.parseInt(endrx.stringMatched()));
136 void checkValidRange()
139 // http://issues.jalview.org/browse/JAL-774?focusedCommentId=11239&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-11239
142 for (int j = 0; j < sequence.length; j++)
144 if (!jalview.util.Comparison.isGap(sequence[j]))
163 * Creates a new Sequence object.
170 public Sequence(String name, String sequence)
172 this(name, sequence, 1, -1);
176 * Creates a new Sequence object with new features, DBRefEntries,
177 * AlignmentAnnotations, and PDBIds but inherits any existing dataset sequence
183 public Sequence(SequenceI seq)
185 this(seq, seq.getAnnotation());
189 * Create a new sequence object with new features, DBRefEntries, and PDBIds
190 * but inherits any existing dataset sequence reference, and duplicate of any
191 * annotation that is present in the given annotation array.
194 * the sequence to be copied
195 * @param alAnnotation
196 * an array of annotation including some associated with seq
198 public Sequence(SequenceI seq, AlignmentAnnotation[] alAnnotation)
200 this(seq.getName(), seq.getSequence(), seq.getStart(), seq.getEnd());
201 description = seq.getDescription();
202 if (seq.getSequenceFeatures() != null)
204 SequenceFeature[] sf = seq.getSequenceFeatures();
205 for (int i = 0; i < sf.length; i++)
207 addSequenceFeature(new SequenceFeature(sf[i]));
210 setDatasetSequence(seq.getDatasetSequence());
211 if (datasetSequence == null && seq.getDBRef() != null)
213 // only copy DBRefs if we really are a dataset sequence
214 DBRefEntry[] dbr = seq.getDBRef();
215 for (int i = 0; i < dbr.length; i++)
217 addDBRef(new DBRefEntry(dbr[i]));
220 if (seq.getAnnotation() != null)
222 AlignmentAnnotation[] sqann = seq.getAnnotation();
223 for (int i = 0; i < sqann.length; i++)
225 if (sqann[i] == null)
229 boolean found = (alAnnotation == null);
232 for (int apos = 0; !found && apos < alAnnotation.length; apos++)
234 found = (alAnnotation[apos] == sqann[i]);
239 // only copy the given annotation
240 AlignmentAnnotation newann = new AlignmentAnnotation(sqann[i]);
241 addAlignmentAnnotation(newann);
245 if (seq.getPDBId() != null)
247 Vector ids = seq.getPDBId();
248 Enumeration e = ids.elements();
249 while (e.hasMoreElements())
251 this.addPDBId(new PDBEntry((PDBEntry) e.nextElement()));
262 public void setSequenceFeatures(SequenceFeature[] features)
264 sequenceFeatures = features;
267 public synchronized void addSequenceFeature(SequenceFeature sf)
269 if (sequenceFeatures == null)
271 sequenceFeatures = new SequenceFeature[0];
274 for (int i = 0; i < sequenceFeatures.length; i++)
276 if (sequenceFeatures[i].equals(sf))
282 SequenceFeature[] temp = new SequenceFeature[sequenceFeatures.length + 1];
283 System.arraycopy(sequenceFeatures, 0, temp, 0, sequenceFeatures.length);
284 temp[sequenceFeatures.length] = sf;
286 sequenceFeatures = temp;
289 public void deleteFeature(SequenceFeature sf)
291 if (sequenceFeatures == null)
297 for (index = 0; index < sequenceFeatures.length; index++)
299 if (sequenceFeatures[index].equals(sf))
305 if (index == sequenceFeatures.length)
310 int sfLength = sequenceFeatures.length;
313 sequenceFeatures = null;
317 SequenceFeature[] temp = new SequenceFeature[sfLength - 1];
318 System.arraycopy(sequenceFeatures, 0, temp, 0, index);
320 if (index < sfLength)
322 System.arraycopy(sequenceFeatures, index + 1, temp, index,
323 sequenceFeatures.length - index - 1);
326 sequenceFeatures = temp;
331 * Returns the sequence features (if any), looking first on the sequence, then
332 * on its dataset sequence, and so on until a non-null value is found (or
333 * none). This supports retrieval of sequence features stored on the sequence
334 * (as in the applet) or on the dataset sequence (as in the Desktop version).
338 public SequenceFeature[] getSequenceFeatures()
340 SequenceFeature[] features = sequenceFeatures;
342 SequenceI seq = this;
343 int count = 0; // failsafe against loop in sequence.datasetsequence...
344 while (features == null && seq.getDatasetSequence() != null
347 seq = seq.getDatasetSequence();
348 features = ((Sequence) seq).sequenceFeatures;
353 public void addPDBId(PDBEntry entry)
357 pdbIds = new Vector<PDBEntry>();
359 if (pdbIds.contains(entry))
361 updatePDBEntry(pdbIds.get(pdbIds.indexOf(entry)), entry);
365 pdbIds.addElement(entry);
369 private static void updatePDBEntry(PDBEntry oldEntry, PDBEntry newEntry)
371 if (newEntry.getFile() != null)
373 oldEntry.setFile(newEntry.getFile());
384 public void setPDBId(Vector<PDBEntry> id)
392 * @return DOCUMENT ME!
395 public Vector<PDBEntry> getPDBId()
403 * @return DOCUMENT ME!
405 public String getDisplayId(boolean jvsuffix)
407 StringBuffer result = new StringBuffer(name);
410 result.append("/" + start + "-" + end);
413 return result.toString();
422 public void setName(String name)
431 * @return DOCUMENT ME!
433 public String getName()
444 public void setStart(int start)
452 * @return DOCUMENT ME!
454 public int getStart()
465 public void setEnd(int end)
473 * @return DOCUMENT ME!
483 * @return DOCUMENT ME!
485 public int getLength()
487 return this.sequence.length;
496 public void setSequence(String seq)
498 this.sequence = seq.toCharArray();
502 public String getSequenceAsString()
504 return new String(sequence);
507 public String getSequenceAsString(int start, int end)
509 return new String(getSequence(start, end));
512 public char[] getSequence()
520 * @see jalview.datamodel.SequenceI#getSequence(int, int)
522 public char[] getSequence(int start, int end)
528 // JBPNote - left to user to pad the result here (TODO:Decide on this
530 if (start >= sequence.length)
535 if (end >= sequence.length)
537 end = sequence.length;
540 char[] reply = new char[end - start];
541 System.arraycopy(sequence, start, reply, 0, end - start);
547 public SequenceI getSubSequence(int start, int end)
553 char[] seq = getSequence(start, end);
558 int nstart = findPosition(start);
559 int nend = findPosition(end) - 1;
560 // JBPNote - this is an incomplete copy.
561 SequenceI nseq = new Sequence(this.getName(), seq, nstart, nend);
562 nseq.setDescription(description);
563 if (datasetSequence != null)
565 nseq.setDatasetSequence(datasetSequence);
569 nseq.setDatasetSequence(this);
580 * @return DOCUMENT ME!
582 public char getCharAt(int i)
584 if (i < sequence.length)
600 public void setDescription(String desc)
602 this.description = desc;
608 * @return DOCUMENT ME!
610 public String getDescription()
612 return this.description;
618 * @see jalview.datamodel.SequenceI#findIndex(int)
620 public int findIndex(int pos)
622 // returns the alignment position for a residue
625 // Rely on end being at least as long as the length of the sequence.
626 while ((i < sequence.length) && (j <= end) && (j <= pos))
628 if (!jalview.util.Comparison.isGap(sequence[i]))
636 if ((j == end) && (j < pos))
647 public int findPosition(int i)
651 int seqlen = sequence.length;
652 while ((j < i) && (j < seqlen))
654 if (!jalview.util.Comparison.isGap(sequence[j]))
666 * Returns an int array where indices correspond to each residue in the
667 * sequence and the element value gives its position in the alignment
669 * @return int[SequenceI.getEnd()-SequenceI.getStart()+1] or null if no
670 * residues in SequenceI object
672 public int[] gapMap()
674 String seq = jalview.analysis.AlignSeq.extractGaps(
675 jalview.util.Comparison.GapChars, new String(sequence));
676 int[] map = new int[seq.length()];
680 while (j < sequence.length)
682 if (!jalview.util.Comparison.isGap(sequence[j]))
694 public int[] findPositionMap()
696 int map[] = new int[sequence.length];
699 int seqlen = sequence.length;
703 if (!jalview.util.Comparison.isGap(sequence[j]))
714 public List<int[]> getInsertions()
716 ArrayList<int[]> map = new ArrayList<int[]>();
717 int lastj = -1, j = 0;
719 int seqlen = sequence.length;
722 if (jalview.util.Comparison.isGap(sequence[j]))
750 public void deleteChars(int i, int j)
752 int newstart = start, newend = end;
753 if (i >= sequence.length)
758 char[] tmp = StringUtils.deleteChars(sequence, i, j);
759 boolean createNewDs = false;
760 // TODO: take a (second look) at the dataset creation validation method for
761 // the very large sequence case
762 int eindex = -1, sindex = -1;
763 boolean ecalc = false, scalc = false;
764 for (int s = i; s < j; s++)
766 if (jalview.schemes.ResidueProperties.aaIndex[sequence[s]] != 23)
776 sindex = findIndex(start) - 1;
781 // delete characters including start of sequence
782 newstart = findPosition(j);
783 break; // don't need to search for any more residue characters.
787 // delete characters after start.
790 eindex = findIndex(end) - 1;
795 // delete characters at end of sequence
796 newend = findPosition(i - 1);
797 break; // don't need to search for any more residue characters.
802 newend--; // decrease end position by one for the deleted residue
803 // and search further
809 // deletion occured in the middle of the sequence
810 if (createNewDs && this.datasetSequence != null)
812 // construct a new sequence
813 Sequence ds = new Sequence(datasetSequence);
814 // TODO: remove any non-inheritable properties ?
815 // TODO: create a sequence mapping (since there is a relation here ?)
816 ds.deleteChars(i, j);
817 datasetSequence = ds;
825 public void insertCharAt(int i, int length, char c)
827 char[] tmp = new char[sequence.length + length];
829 if (i >= sequence.length)
831 System.arraycopy(sequence, 0, tmp, 0, sequence.length);
836 System.arraycopy(sequence, 0, tmp, 0, i);
846 if (i < sequence.length)
848 System.arraycopy(sequence, i, tmp, index, sequence.length - i);
855 public void insertCharAt(int i, char c)
857 insertCharAt(i, 1, c);
861 public String getVamsasId()
867 public void setVamsasId(String id)
873 public void setDBRef(DBRefEntry[] dbref)
879 public DBRefEntry[] getDBRef()
881 if (dbrefs == null && datasetSequence != null
882 && this != datasetSequence)
884 return datasetSequence.getDBRef();
890 public void addDBRef(DBRefEntry entry)
894 dbrefs = new DBRefEntry[0];
897 int i, iSize = dbrefs.length;
899 for (i = 0; i < iSize; i++)
901 if (dbrefs[i].equalRef(entry))
903 if (entry.getMap() != null)
905 if (dbrefs[i].getMap() == null)
907 // overwrite with 'superior' entry that contains a mapping.
915 DBRefEntry[] temp = new DBRefEntry[iSize + 1];
916 System.arraycopy(dbrefs, 0, temp, 0, iSize);
917 temp[temp.length - 1] = entry;
923 public void setDatasetSequence(SequenceI seq)
925 datasetSequence = seq;
929 public SequenceI getDatasetSequence()
931 return datasetSequence;
935 public AlignmentAnnotation[] getAnnotation()
937 return annotation == null ? null : annotation
938 .toArray(new AlignmentAnnotation[annotation.size()]);
943 public boolean hasAnnotation(AlignmentAnnotation ann)
945 return annotation == null ? false : annotation.contains(ann);
949 public void addAlignmentAnnotation(AlignmentAnnotation annotation)
951 if (this.annotation == null)
953 this.annotation = new Vector<AlignmentAnnotation>();
955 if (!this.annotation.contains(annotation))
957 this.annotation.addElement(annotation);
959 annotation.setSequenceRef(this);
962 public void removeAlignmentAnnotation(AlignmentAnnotation annotation)
964 if (this.annotation != null)
966 this.annotation.removeElement(annotation);
967 if (this.annotation.size() == 0)
969 this.annotation = null;
975 * test if this is a valid candidate for another sequence's dataset sequence.
978 private boolean isValidDatasetSequence()
980 if (datasetSequence != null)
984 for (int i = 0; i < sequence.length; i++)
986 if (jalview.util.Comparison.isGap(sequence[i]))
995 public SequenceI deriveSequence()
997 SequenceI seq = new Sequence(this);
998 if (datasetSequence != null)
1000 // duplicate current sequence with same dataset
1001 seq.setDatasetSequence(datasetSequence);
1005 if (isValidDatasetSequence())
1007 // Use this as dataset sequence
1008 seq.setDatasetSequence(this);
1012 // Create a new, valid dataset sequence
1014 ds.setSequence(AlignSeq.extractGaps(
1015 jalview.util.Comparison.GapChars, new String(sequence)));
1016 setDatasetSequence(ds);
1017 ds.setSequenceFeatures(getSequenceFeatures());
1018 seq = this; // and return this sequence as the derived sequence.
1027 * @see jalview.datamodel.SequenceI#createDatasetSequence()
1029 public SequenceI createDatasetSequence()
1031 if (datasetSequence == null)
1033 datasetSequence = new Sequence(getName(), AlignSeq.extractGaps(
1034 jalview.util.Comparison.GapChars, getSequenceAsString()),
1035 getStart(), getEnd());
1036 datasetSequence.setSequenceFeatures(getSequenceFeatures());
1037 datasetSequence.setDescription(getDescription());
1038 setSequenceFeatures(null);
1039 // move database references onto dataset sequence
1040 datasetSequence.setDBRef(getDBRef());
1042 datasetSequence.setPDBId(getPDBId());
1044 datasetSequence.updatePDBIds();
1045 if (annotation != null)
1047 for (AlignmentAnnotation aa : annotation)
1049 AlignmentAnnotation _aa = new AlignmentAnnotation(aa);
1050 _aa.sequenceRef = datasetSequence;
1051 _aa.adjustForAlignment(); // uses annotation's own record of
1052 // sequence-column mapping
1053 datasetSequence.addAlignmentAnnotation(_aa);
1057 return datasetSequence;
1064 * jalview.datamodel.SequenceI#setAlignmentAnnotation(AlignmmentAnnotation[]
1067 public void setAlignmentAnnotation(AlignmentAnnotation[] annotations)
1069 if (annotation != null)
1071 annotation.removeAllElements();
1073 if (annotations != null)
1075 for (int i = 0; i < annotations.length; i++)
1077 if (annotations[i] != null)
1079 addAlignmentAnnotation(annotations[i]);
1086 public AlignmentAnnotation[] getAnnotation(String label)
1088 if (annotation == null || annotation.size() == 0)
1093 Vector subset = new Vector();
1094 Enumeration e = annotation.elements();
1095 while (e.hasMoreElements())
1097 AlignmentAnnotation ann = (AlignmentAnnotation) e.nextElement();
1098 if (ann.label != null && ann.label.equals(label))
1100 subset.addElement(ann);
1103 if (subset.size() == 0)
1107 AlignmentAnnotation[] anns = new AlignmentAnnotation[subset.size()];
1109 e = subset.elements();
1110 while (e.hasMoreElements())
1112 anns[i++] = (AlignmentAnnotation) e.nextElement();
1114 subset.removeAllElements();
1119 public boolean updatePDBIds()
1121 if (datasetSequence != null)
1123 // TODO: could merge DBRefs
1124 return datasetSequence.updatePDBIds();
1126 if (dbrefs == null || dbrefs.length == 0)
1130 Vector newpdb = new Vector();
1131 for (int i = 0; i < dbrefs.length; i++)
1133 if (DBRefSource.PDB.equals(dbrefs[i].getSource()))
1135 PDBEntry pdbe = new PDBEntry();
1136 pdbe.setId(dbrefs[i].getAccessionId());
1137 if (pdbIds == null || pdbIds.size() == 0)
1139 newpdb.addElement(pdbe);
1143 Enumeration en = pdbIds.elements();
1144 boolean matched = false;
1145 while (!matched && en.hasMoreElements())
1147 PDBEntry anentry = (PDBEntry) en.nextElement();
1148 if (anentry.getId().equals(pdbe.getId()))
1155 newpdb.addElement(pdbe);
1160 if (newpdb.size() > 0)
1162 Enumeration en = newpdb.elements();
1163 while (en.hasMoreElements())
1165 addPDBId((PDBEntry) en.nextElement());
1173 public void transferAnnotation(SequenceI entry, Mapping mp)
1175 if (datasetSequence != null)
1177 datasetSequence.transferAnnotation(entry, mp);
1180 if (entry.getDatasetSequence() != null)
1182 transferAnnotation(entry.getDatasetSequence(), mp);
1185 // transfer any new features from entry onto sequence
1186 if (entry.getSequenceFeatures() != null)
1189 SequenceFeature[] sfs = entry.getSequenceFeatures();
1190 for (int si = 0; si < sfs.length; si++)
1192 SequenceFeature sf[] = (mp != null) ? mp.locateFeature(sfs[si])
1193 : new SequenceFeature[]
1194 { new SequenceFeature(sfs[si]) };
1195 if (sf != null && sf.length > 0)
1197 for (int sfi = 0; sfi < sf.length; sfi++)
1199 addSequenceFeature(sf[sfi]);
1205 // transfer PDB entries
1206 if (entry.getPDBId() != null)
1208 Enumeration e = entry.getPDBId().elements();
1209 while (e.hasMoreElements())
1211 PDBEntry pdb = (PDBEntry) e.nextElement();
1215 // transfer database references
1216 DBRefEntry[] entryRefs = entry.getDBRef();
1217 if (entryRefs != null)
1219 for (int r = 0; r < entryRefs.length; r++)
1221 DBRefEntry newref = new DBRefEntry(entryRefs[r]);
1222 if (newref.getMap() != null && mp != null)
1224 // remap ref using our local mapping
1226 // we also assume all version string setting is done by dbSourceProxy
1228 * if (!newref.getSource().equalsIgnoreCase(dbSource)) {
1229 * newref.setSource(dbSource); }
1237 * @return The index (zero-based) on this sequence in the MSA. It returns
1238 * {@code -1} if this information is not available.
1240 public int getIndex()
1246 * Defines the position of this sequence in the MSA. Use the value {@code -1}
1247 * if this information is undefined.
1250 * position for this sequence. This value is zero-based (zero for
1251 * this first sequence)
1253 public void setIndex(int value)
1258 public void setRNA(RNA r)
1269 public List<AlignmentAnnotation> getAlignmentAnnotations(String calcId,
1272 List<AlignmentAnnotation> result = new ArrayList<AlignmentAnnotation>();
1273 if (this.annotation != null)
1275 for (AlignmentAnnotation ann : annotation)
1277 if (ann.calcId != null && ann.calcId.equals(calcId)
1278 && ann.label != null && ann.label.equals(label))