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;
62 private boolean hidden;
65 * This annotation is displayed below the alignment but the positions are tied
66 * to the residues of this sequence
68 * TODO: change to List<>
70 Vector<AlignmentAnnotation> annotation;
73 * The index of the sequence in a MSA
77 /** array of sequence features - may not be null for a valid sequence object */
78 public SequenceFeature[] sequenceFeatures;
81 * Creates a new Sequence object.
86 * string to form a possibly gapped sequence out of
88 * first position of non-gap residue in the sequence
90 * last position of ungapped residues (nearly always only used for
93 public Sequence(String name, String sequence, int start, int end)
96 this.sequence = sequence.toCharArray();
103 public Sequence(String name, char[] sequence, int start, int end)
106 this.sequence = sequence;
113 com.stevesoft.pat.Regex limitrx = new com.stevesoft.pat.Regex(
114 "[/][0-9]{1,}[-][0-9]{1,}$");
116 com.stevesoft.pat.Regex endrx = new com.stevesoft.pat.Regex("[0-9]{1,}$");
123 .println("POSSIBLE IMPLEMENTATION ERROR: null sequence name passed to constructor.");
126 // Does sequence have the /start-end signature?
127 if (limitrx.search(name))
129 name = limitrx.left();
130 endrx.search(limitrx.stringMatched());
131 setStart(Integer.parseInt(limitrx.stringMatched().substring(1,
132 endrx.matchedFrom() - 1)));
133 setEnd(Integer.parseInt(endrx.stringMatched()));
137 void checkValidRange()
140 // http://issues.jalview.org/browse/JAL-774?focusedCommentId=11239&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-11239
143 for (int j = 0; j < sequence.length; j++)
145 if (!jalview.util.Comparison.isGap(sequence[j]))
164 * Creates a new Sequence object.
171 public Sequence(String name, String sequence)
173 this(name, sequence, 1, -1);
177 * Creates a new Sequence object with new features, DBRefEntries,
178 * AlignmentAnnotations, and PDBIds but inherits any existing dataset sequence
184 public Sequence(SequenceI seq)
186 this(seq, seq.getAnnotation());
190 * Create a new sequence object with new features, DBRefEntries, and PDBIds
191 * but inherits any existing dataset sequence reference, and duplicate of any
192 * annotation that is present in the given annotation array.
195 * the sequence to be copied
196 * @param alAnnotation
197 * an array of annotation including some associated with seq
199 public Sequence(SequenceI seq, AlignmentAnnotation[] alAnnotation)
201 this(seq.getName(), seq.getSequence(), seq.getStart(), seq.getEnd());
202 description = seq.getDescription();
203 if (seq.getSequenceFeatures() != null)
205 SequenceFeature[] sf = seq.getSequenceFeatures();
206 for (int i = 0; i < sf.length; i++)
208 addSequenceFeature(new SequenceFeature(sf[i]));
211 setDatasetSequence(seq.getDatasetSequence());
212 if (datasetSequence == null && seq.getDBRef() != null)
214 // only copy DBRefs if we really are a dataset sequence
215 DBRefEntry[] dbr = seq.getDBRef();
216 for (int i = 0; i < dbr.length; i++)
218 addDBRef(new DBRefEntry(dbr[i]));
221 if (seq.getAnnotation() != null)
223 AlignmentAnnotation[] sqann = seq.getAnnotation();
224 for (int i = 0; i < sqann.length; i++)
226 if (sqann[i] == null)
230 boolean found = (alAnnotation == null);
233 for (int apos = 0; !found && apos < alAnnotation.length; apos++)
235 found = (alAnnotation[apos] == sqann[i]);
240 // only copy the given annotation
241 AlignmentAnnotation newann = new AlignmentAnnotation(sqann[i]);
242 addAlignmentAnnotation(newann);
246 if (seq.getPDBId() != null)
248 Vector ids = seq.getPDBId();
249 Enumeration e = ids.elements();
250 while (e.hasMoreElements())
252 this.addPDBId(new PDBEntry((PDBEntry) e.nextElement()));
263 public void setSequenceFeatures(SequenceFeature[] features)
265 sequenceFeatures = features;
268 public synchronized void addSequenceFeature(SequenceFeature sf)
270 if (sequenceFeatures == null)
272 sequenceFeatures = new SequenceFeature[0];
275 for (int i = 0; i < sequenceFeatures.length; i++)
277 if (sequenceFeatures[i].equals(sf))
283 SequenceFeature[] temp = new SequenceFeature[sequenceFeatures.length + 1];
284 System.arraycopy(sequenceFeatures, 0, temp, 0, sequenceFeatures.length);
285 temp[sequenceFeatures.length] = sf;
287 sequenceFeatures = temp;
290 public void deleteFeature(SequenceFeature sf)
292 if (sequenceFeatures == null)
298 for (index = 0; index < sequenceFeatures.length; index++)
300 if (sequenceFeatures[index].equals(sf))
306 if (index == sequenceFeatures.length)
311 int sfLength = sequenceFeatures.length;
314 sequenceFeatures = null;
318 SequenceFeature[] temp = new SequenceFeature[sfLength - 1];
319 System.arraycopy(sequenceFeatures, 0, temp, 0, index);
321 if (index < sfLength)
323 System.arraycopy(sequenceFeatures, index + 1, temp, index,
324 sequenceFeatures.length - index - 1);
327 sequenceFeatures = temp;
332 * Returns the sequence features (if any), looking first on the sequence, then
333 * on its dataset sequence, and so on until a non-null value is found (or
334 * none). This supports retrieval of sequence features stored on the sequence
335 * (as in the applet) or on the dataset sequence (as in the Desktop version).
339 public SequenceFeature[] getSequenceFeatures()
341 SequenceFeature[] features = sequenceFeatures;
343 SequenceI seq = this;
344 int count = 0; // failsafe against loop in sequence.datasetsequence...
345 while (features == null && seq.getDatasetSequence() != null
348 seq = seq.getDatasetSequence();
349 features = ((Sequence) seq).sequenceFeatures;
354 public void addPDBId(PDBEntry entry)
358 pdbIds = new Vector<PDBEntry>();
360 if (pdbIds.contains(entry))
362 updatePDBEntry(pdbIds.get(pdbIds.indexOf(entry)), entry);
366 pdbIds.addElement(entry);
370 private static void updatePDBEntry(PDBEntry oldEntry, PDBEntry newEntry)
372 if (newEntry.getFile() != null)
374 oldEntry.setFile(newEntry.getFile());
385 public void setPDBId(Vector<PDBEntry> id)
393 * @return DOCUMENT ME!
396 public Vector<PDBEntry> getPDBId()
404 * @return DOCUMENT ME!
406 public String getDisplayId(boolean jvsuffix)
408 StringBuffer result = new StringBuffer(name);
411 result.append("/" + start + "-" + end);
414 return result.toString();
423 public void setName(String name)
432 * @return DOCUMENT ME!
434 public String getName()
445 public void setStart(int start)
453 * @return DOCUMENT ME!
455 public int getStart()
466 public void setEnd(int end)
474 * @return DOCUMENT ME!
484 * @return DOCUMENT ME!
486 public int getLength()
488 return this.sequence.length;
497 public void setSequence(String seq)
499 this.sequence = seq.toCharArray();
503 public String getSequenceAsString()
505 return new String(sequence);
508 public String getSequenceAsString(int start, int end)
510 return new String(getSequence(start, end));
513 public char[] getSequence()
521 * @see jalview.datamodel.SequenceI#getSequence(int, int)
523 public char[] getSequence(int start, int end)
529 // JBPNote - left to user to pad the result here (TODO:Decide on this
531 if (start >= sequence.length)
536 if (end >= sequence.length)
538 end = sequence.length;
541 char[] reply = new char[end - start];
542 System.arraycopy(sequence, start, reply, 0, end - start);
548 public SequenceI getSubSequence(int start, int end)
554 char[] seq = getSequence(start, end);
559 int nstart = findPosition(start);
560 int nend = findPosition(end) - 1;
561 // JBPNote - this is an incomplete copy.
562 SequenceI nseq = new Sequence(this.getName(), seq, nstart, nend);
563 nseq.setDescription(description);
564 if (datasetSequence != null)
566 nseq.setDatasetSequence(datasetSequence);
570 nseq.setDatasetSequence(this);
581 * @return DOCUMENT ME!
583 public char getCharAt(int i)
585 if (i < sequence.length)
601 public void setDescription(String desc)
603 this.description = desc;
609 * @return DOCUMENT ME!
611 public String getDescription()
613 return this.description;
619 * @see jalview.datamodel.SequenceI#findIndex(int)
621 public int findIndex(int pos)
623 // returns the alignment position for a residue
626 // Rely on end being at least as long as the length of the sequence.
627 while ((i < sequence.length) && (j <= end) && (j <= pos))
629 if (!jalview.util.Comparison.isGap(sequence[i]))
637 if ((j == end) && (j < pos))
648 public int findPosition(int i)
652 int seqlen = sequence.length;
653 while ((j < i) && (j < seqlen))
655 if (!jalview.util.Comparison.isGap(sequence[j]))
667 * Returns an int array where indices correspond to each residue in the
668 * sequence and the element value gives its position in the alignment
670 * @return int[SequenceI.getEnd()-SequenceI.getStart()+1] or null if no
671 * residues in SequenceI object
673 public int[] gapMap()
675 String seq = jalview.analysis.AlignSeq.extractGaps(
676 jalview.util.Comparison.GapChars, new String(sequence));
677 int[] map = new int[seq.length()];
681 while (j < sequence.length)
683 if (!jalview.util.Comparison.isGap(sequence[j]))
695 public int[] findPositionMap()
697 int map[] = new int[sequence.length];
700 int seqlen = sequence.length;
704 if (!jalview.util.Comparison.isGap(sequence[j]))
715 public List<int[]> getInsertions()
717 ArrayList<int[]> map = new ArrayList<int[]>();
718 int lastj = -1, j = 0;
720 int seqlen = sequence.length;
723 if (jalview.util.Comparison.isGap(sequence[j]))
751 public void deleteChars(int i, int j)
753 int newstart = start, newend = end;
754 if (i >= sequence.length)
759 char[] tmp = StringUtils.deleteChars(sequence, i, j);
760 boolean createNewDs = false;
761 // TODO: take a (second look) at the dataset creation validation method for
762 // the very large sequence case
763 int eindex = -1, sindex = -1;
764 boolean ecalc = false, scalc = false;
765 for (int s = i; s < j; s++)
767 if (jalview.schemes.ResidueProperties.aaIndex[sequence[s]] != 23)
777 sindex = findIndex(start) - 1;
782 // delete characters including start of sequence
783 newstart = findPosition(j);
784 break; // don't need to search for any more residue characters.
788 // delete characters after start.
791 eindex = findIndex(end) - 1;
796 // delete characters at end of sequence
797 newend = findPosition(i - 1);
798 break; // don't need to search for any more residue characters.
803 newend--; // decrease end position by one for the deleted residue
804 // and search further
810 // deletion occured in the middle of the sequence
811 if (createNewDs && this.datasetSequence != null)
813 // construct a new sequence
814 Sequence ds = new Sequence(datasetSequence);
815 // TODO: remove any non-inheritable properties ?
816 // TODO: create a sequence mapping (since there is a relation here ?)
817 ds.deleteChars(i, j);
818 datasetSequence = ds;
826 public void insertCharAt(int i, int length, char c)
828 char[] tmp = new char[sequence.length + length];
830 if (i >= sequence.length)
832 System.arraycopy(sequence, 0, tmp, 0, sequence.length);
837 System.arraycopy(sequence, 0, tmp, 0, i);
847 if (i < sequence.length)
849 System.arraycopy(sequence, i, tmp, index, sequence.length - i);
856 public void insertCharAt(int i, char c)
858 insertCharAt(i, 1, c);
862 public String getVamsasId()
868 public void setVamsasId(String id)
874 public void setDBRef(DBRefEntry[] dbref)
880 public DBRefEntry[] getDBRef()
882 if (dbrefs == null && datasetSequence != null
883 && this != datasetSequence)
885 return datasetSequence.getDBRef();
891 public void addDBRef(DBRefEntry entry)
895 dbrefs = new DBRefEntry[0];
898 int i, iSize = dbrefs.length;
900 for (i = 0; i < iSize; i++)
902 if (dbrefs[i].equalRef(entry))
904 if (entry.getMap() != null)
906 if (dbrefs[i].getMap() == null)
908 // overwrite with 'superior' entry that contains a mapping.
916 DBRefEntry[] temp = new DBRefEntry[iSize + 1];
917 System.arraycopy(dbrefs, 0, temp, 0, iSize);
918 temp[temp.length - 1] = entry;
924 public void setDatasetSequence(SequenceI seq)
926 datasetSequence = seq;
930 public SequenceI getDatasetSequence()
932 return datasetSequence;
936 public AlignmentAnnotation[] getAnnotation()
938 return annotation == null ? null : annotation
939 .toArray(new AlignmentAnnotation[annotation.size()]);
944 public boolean hasAnnotation(AlignmentAnnotation ann)
946 return annotation == null ? false : annotation.contains(ann);
950 public void addAlignmentAnnotation(AlignmentAnnotation annotation)
952 if (this.annotation == null)
954 this.annotation = new Vector<AlignmentAnnotation>();
956 if (!this.annotation.contains(annotation))
958 this.annotation.addElement(annotation);
960 annotation.setSequenceRef(this);
963 public void removeAlignmentAnnotation(AlignmentAnnotation annotation)
965 if (this.annotation != null)
967 this.annotation.removeElement(annotation);
968 if (this.annotation.size() == 0)
970 this.annotation = null;
976 * test if this is a valid candidate for another sequence's dataset sequence.
979 private boolean isValidDatasetSequence()
981 if (datasetSequence != null)
985 for (int i = 0; i < sequence.length; i++)
987 if (jalview.util.Comparison.isGap(sequence[i]))
996 public SequenceI deriveSequence()
998 SequenceI seq = new Sequence(this);
999 if (datasetSequence != null)
1001 // duplicate current sequence with same dataset
1002 seq.setDatasetSequence(datasetSequence);
1006 if (isValidDatasetSequence())
1008 // Use this as dataset sequence
1009 seq.setDatasetSequence(this);
1013 // Create a new, valid dataset sequence
1015 ds.setSequence(AlignSeq.extractGaps(
1016 jalview.util.Comparison.GapChars, new String(sequence)));
1017 setDatasetSequence(ds);
1018 ds.setSequenceFeatures(getSequenceFeatures());
1019 seq = this; // and return this sequence as the derived sequence.
1028 * @see jalview.datamodel.SequenceI#createDatasetSequence()
1030 public SequenceI createDatasetSequence()
1032 if (datasetSequence == null)
1034 datasetSequence = new Sequence(getName(), AlignSeq.extractGaps(
1035 jalview.util.Comparison.GapChars, getSequenceAsString()),
1036 getStart(), getEnd());
1037 datasetSequence.setSequenceFeatures(getSequenceFeatures());
1038 datasetSequence.setDescription(getDescription());
1039 setSequenceFeatures(null);
1040 // move database references onto dataset sequence
1041 datasetSequence.setDBRef(getDBRef());
1043 datasetSequence.setPDBId(getPDBId());
1045 datasetSequence.updatePDBIds();
1046 if (annotation != null)
1048 for (AlignmentAnnotation aa : annotation)
1050 AlignmentAnnotation _aa = new AlignmentAnnotation(aa);
1051 _aa.sequenceRef = datasetSequence;
1052 _aa.adjustForAlignment(); // uses annotation's own record of
1053 // sequence-column mapping
1054 datasetSequence.addAlignmentAnnotation(_aa);
1058 return datasetSequence;
1065 * jalview.datamodel.SequenceI#setAlignmentAnnotation(AlignmmentAnnotation[]
1068 public void setAlignmentAnnotation(AlignmentAnnotation[] annotations)
1070 if (annotation != null)
1072 annotation.removeAllElements();
1074 if (annotations != null)
1076 for (int i = 0; i < annotations.length; i++)
1078 if (annotations[i] != null)
1080 addAlignmentAnnotation(annotations[i]);
1087 public AlignmentAnnotation[] getAnnotation(String label)
1089 if (annotation == null || annotation.size() == 0)
1094 Vector subset = new Vector();
1095 Enumeration e = annotation.elements();
1096 while (e.hasMoreElements())
1098 AlignmentAnnotation ann = (AlignmentAnnotation) e.nextElement();
1099 if (ann.label != null && ann.label.equals(label))
1101 subset.addElement(ann);
1104 if (subset.size() == 0)
1108 AlignmentAnnotation[] anns = new AlignmentAnnotation[subset.size()];
1110 e = subset.elements();
1111 while (e.hasMoreElements())
1113 anns[i++] = (AlignmentAnnotation) e.nextElement();
1115 subset.removeAllElements();
1120 public boolean updatePDBIds()
1122 if (datasetSequence != null)
1124 // TODO: could merge DBRefs
1125 return datasetSequence.updatePDBIds();
1127 if (dbrefs == null || dbrefs.length == 0)
1131 Vector newpdb = new Vector();
1132 for (int i = 0; i < dbrefs.length; i++)
1134 if (DBRefSource.PDB.equals(dbrefs[i].getSource()))
1136 PDBEntry pdbe = new PDBEntry();
1137 pdbe.setId(dbrefs[i].getAccessionId());
1138 if (pdbIds == null || pdbIds.size() == 0)
1140 newpdb.addElement(pdbe);
1144 Enumeration en = pdbIds.elements();
1145 boolean matched = false;
1146 while (!matched && en.hasMoreElements())
1148 PDBEntry anentry = (PDBEntry) en.nextElement();
1149 if (anentry.getId().equals(pdbe.getId()))
1156 newpdb.addElement(pdbe);
1161 if (newpdb.size() > 0)
1163 Enumeration en = newpdb.elements();
1164 while (en.hasMoreElements())
1166 addPDBId((PDBEntry) en.nextElement());
1174 public void transferAnnotation(SequenceI entry, Mapping mp)
1176 if (datasetSequence != null)
1178 datasetSequence.transferAnnotation(entry, mp);
1181 if (entry.getDatasetSequence() != null)
1183 transferAnnotation(entry.getDatasetSequence(), mp);
1186 // transfer any new features from entry onto sequence
1187 if (entry.getSequenceFeatures() != null)
1190 SequenceFeature[] sfs = entry.getSequenceFeatures();
1191 for (int si = 0; si < sfs.length; si++)
1193 SequenceFeature sf[] = (mp != null) ? mp.locateFeature(sfs[si])
1194 : new SequenceFeature[]
1195 { new SequenceFeature(sfs[si]) };
1196 if (sf != null && sf.length > 0)
1198 for (int sfi = 0; sfi < sf.length; sfi++)
1200 addSequenceFeature(sf[sfi]);
1206 // transfer PDB entries
1207 if (entry.getPDBId() != null)
1209 Enumeration e = entry.getPDBId().elements();
1210 while (e.hasMoreElements())
1212 PDBEntry pdb = (PDBEntry) e.nextElement();
1216 // transfer database references
1217 DBRefEntry[] entryRefs = entry.getDBRef();
1218 if (entryRefs != null)
1220 for (int r = 0; r < entryRefs.length; r++)
1222 DBRefEntry newref = new DBRefEntry(entryRefs[r]);
1223 if (newref.getMap() != null && mp != null)
1225 // remap ref using our local mapping
1227 // we also assume all version string setting is done by dbSourceProxy
1229 * if (!newref.getSource().equalsIgnoreCase(dbSource)) {
1230 * newref.setSource(dbSource); }
1238 * @return The index (zero-based) on this sequence in the MSA. It returns
1239 * {@code -1} if this information is not available.
1241 public int getIndex()
1247 * Defines the position of this sequence in the MSA. Use the value {@code -1}
1248 * if this information is undefined.
1251 * position for this sequence. This value is zero-based (zero for
1252 * this first sequence)
1254 public void setIndex(int value)
1259 public void setRNA(RNA r)
1270 public List<AlignmentAnnotation> getAlignmentAnnotations(String calcId,
1273 List<AlignmentAnnotation> result = new ArrayList<AlignmentAnnotation>();
1274 if (this.annotation != null)
1276 for (AlignmentAnnotation ann : annotation)
1278 if (ann.calcId != null && ann.calcId.equals(calcId)
1279 && ann.label != null && ann.label.equals(label))
1289 public boolean isHidden()
1295 public void setHidden(Boolean hidden)
1297 this.hidden = hidden;