2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 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;
25 import java.util.Enumeration;
26 import java.util.Vector;
28 import fr.orsay.lri.varna.models.rna.RNA;
32 * Implements the SequenceI interface for a char[] based sequence object.
37 public class Sequence implements SequenceI
39 SequenceI datasetSequence;
43 private char[] sequence;
60 * This annotation is displayed below the alignment but the positions are tied
61 * to the residues of this sequence
66 * The index of the sequence in a MSA
70 /** array of sequence features - may not be null for a valid sequence object */
71 public SequenceFeature[] sequenceFeatures;
74 * Creates a new Sequence object.
79 * string to form a possibly gapped sequence out of
81 * first position of non-gap residue in the sequence
83 * last position of ungapped residues (nearly always only used for
86 public Sequence(String name, String sequence, int start, int end)
89 this.sequence = sequence.toCharArray();
96 public Sequence(String name, char[] sequence, int start, int end)
99 this.sequence = sequence;
106 com.stevesoft.pat.Regex limitrx = new com.stevesoft.pat.Regex(
107 "[/][0-9]{1,}[-][0-9]{1,}$");
109 com.stevesoft.pat.Regex endrx = new com.stevesoft.pat.Regex("[0-9]{1,}$");
116 .println("POSSIBLE IMPLEMENTATION ERROR: null sequence name passed to constructor.");
119 // Does sequence have the /start-end signiature?
120 if (limitrx.search(name))
122 name = limitrx.left();
123 endrx.search(limitrx.stringMatched());
124 setStart(Integer.parseInt(limitrx.stringMatched().substring(1,
125 endrx.matchedFrom() - 1)));
126 setEnd(Integer.parseInt(endrx.stringMatched()));
130 void checkValidRange()
133 // http://issues.jalview.org/browse/JAL-774?focusedCommentId=11239&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-11239
136 for (int j = 0; j < sequence.length; j++)
138 if (!jalview.util.Comparison.isGap(sequence[j]))
157 * Creates a new Sequence object.
164 public Sequence(String name, String sequence)
166 this(name, sequence, 1, -1);
170 * Creates a new Sequence object with new features, DBRefEntries,
171 * AlignmentAnnotations, and PDBIds but inherits any existing dataset sequence
177 public Sequence(SequenceI seq)
179 this(seq, seq.getAnnotation());
183 * Create a new sequence object with new features, DBRefEntries, and PDBIds
184 * but inherits any existing dataset sequence reference, and duplicate of any
185 * annotation that is present in the given annotation array.
188 * the sequence to be copied
189 * @param alAnnotation
190 * an array of annotation including some associated with seq
192 public Sequence(SequenceI seq, AlignmentAnnotation[] alAnnotation)
194 this(seq.getName(), seq.getSequence(), seq.getStart(), seq.getEnd());
195 description = seq.getDescription();
196 if (seq.getSequenceFeatures() != null)
198 SequenceFeature[] sf = seq.getSequenceFeatures();
199 for (int i = 0; i < sf.length; i++)
201 addSequenceFeature(new SequenceFeature(sf[i]));
204 setDatasetSequence(seq.getDatasetSequence());
205 if (datasetSequence == null && seq.getDBRef() != null)
207 // only copy DBRefs if we really are a dataset sequence
208 DBRefEntry[] dbr = seq.getDBRef();
209 for (int i = 0; i < dbr.length; i++)
211 addDBRef(new DBRefEntry(dbr[i]));
214 if (seq.getAnnotation() != null)
216 AlignmentAnnotation[] sqann = seq.getAnnotation();
217 for (int i = 0; i < sqann.length; i++)
219 if (sqann[i] == null)
223 boolean found = (alAnnotation == null);
226 for (int apos = 0; !found && apos < alAnnotation.length; apos++)
228 found = (alAnnotation[apos] == sqann[i]);
233 // only copy the given annotation
234 AlignmentAnnotation newann = new AlignmentAnnotation(sqann[i]);
235 addAlignmentAnnotation(newann);
239 if (seq.getPDBId() != null)
241 Vector ids = seq.getPDBId();
242 Enumeration e = ids.elements();
243 while (e.hasMoreElements())
245 this.addPDBId(new PDBEntry((PDBEntry) e.nextElement()));
256 public void setSequenceFeatures(SequenceFeature[] features)
258 sequenceFeatures = features;
261 public synchronized void addSequenceFeature(SequenceFeature sf)
263 if (sequenceFeatures == null)
265 sequenceFeatures = new SequenceFeature[0];
268 for (int i = 0; i < sequenceFeatures.length; i++)
270 if (sequenceFeatures[i].equals(sf))
276 SequenceFeature[] temp = new SequenceFeature[sequenceFeatures.length + 1];
277 System.arraycopy(sequenceFeatures, 0, temp, 0, sequenceFeatures.length);
278 temp[sequenceFeatures.length] = sf;
280 sequenceFeatures = temp;
283 public void deleteFeature(SequenceFeature sf)
285 if (sequenceFeatures == null)
291 for (index = 0; index < sequenceFeatures.length; index++)
293 if (sequenceFeatures[index].equals(sf))
299 if (index == sequenceFeatures.length)
304 int sfLength = sequenceFeatures.length;
307 sequenceFeatures = null;
311 SequenceFeature[] temp = new SequenceFeature[sfLength - 1];
312 System.arraycopy(sequenceFeatures, 0, temp, 0, index);
314 if (index < sfLength)
316 System.arraycopy(sequenceFeatures, index + 1, temp, index,
317 sequenceFeatures.length - index - 1);
320 sequenceFeatures = temp;
327 * @return DOCUMENT ME!
329 public SequenceFeature[] getSequenceFeatures()
331 return sequenceFeatures;
334 public void addPDBId(PDBEntry entry)
338 pdbIds = new Vector();
340 if (!pdbIds.contains(entry))
342 pdbIds.addElement(entry);
352 public void setPDBId(Vector id)
360 * @return DOCUMENT ME!
362 public Vector getPDBId()
370 * @return DOCUMENT ME!
372 public String getDisplayId(boolean jvsuffix)
374 StringBuffer result = new StringBuffer(name);
377 result.append("/" + start + "-" + end);
380 return result.toString();
389 public void setName(String name)
398 * @return DOCUMENT ME!
400 public String getName()
411 public void setStart(int start)
419 * @return DOCUMENT ME!
421 public int getStart()
432 public void setEnd(int end)
440 * @return DOCUMENT ME!
450 * @return DOCUMENT ME!
452 public int getLength()
454 return this.sequence.length;
463 public void setSequence(String seq)
465 this.sequence = seq.toCharArray();
469 public String getSequenceAsString()
471 return new String(sequence);
474 public String getSequenceAsString(int start, int end)
476 return new String(getSequence(start, end));
479 public char[] getSequence()
487 * @see jalview.datamodel.SequenceI#getSequence(int, int)
489 public char[] getSequence(int start, int end)
493 // JBPNote - left to user to pad the result here (TODO:Decide on this
495 if (start >= sequence.length)
500 if (end >= sequence.length)
502 end = sequence.length;
505 char[] reply = new char[end - start];
506 System.arraycopy(sequence, start, reply, 0, end - start);
512 * make a new Sequence object from start to end (including gaps) over this
521 public SequenceI getSubSequence(int start, int end)
527 char[] seq = getSequence(start, end);
532 int nstart = findPosition(start);
533 int nend = findPosition(end) - 1;
534 // JBPNote - this is an incomplete copy.
535 SequenceI nseq = new Sequence(this.getName(), seq, nstart, nend);
536 nseq.setDescription(description);
537 if (datasetSequence != null)
539 nseq.setDatasetSequence(datasetSequence);
543 nseq.setDatasetSequence(this);
554 * @return DOCUMENT ME!
556 public char getCharAt(int i)
558 if (i < sequence.length)
574 public void setDescription(String desc)
576 this.description = desc;
582 * @return DOCUMENT ME!
584 public String getDescription()
586 return this.description;
592 * @see jalview.datamodel.SequenceI#findIndex(int)
594 public int findIndex(int pos)
596 // returns the alignment position for a residue
599 // Rely on end being at least as long as the length of the sequence.
600 while ((i < sequence.length) && (j <= end) && (j <= pos))
602 if (!jalview.util.Comparison.isGap(sequence[i]))
610 if ((j == end) && (j < pos))
621 * Returns the sequence position for an alignment position
624 * column index in alignment (from 1)
626 * @return residue number for residue (left of and) nearest ith column
628 public int findPosition(int i)
632 int seqlen = sequence.length;
633 while ((j < i) && (j < seqlen))
635 if (!jalview.util.Comparison.isGap(sequence[j]))
647 * Returns an int array where indices correspond to each residue in the
648 * sequence and the element value gives its position in the alignment
650 * @return int[SequenceI.getEnd()-SequenceI.getStart()+1] or null if no
651 * residues in SequenceI object
653 public int[] gapMap()
655 String seq = jalview.analysis.AlignSeq.extractGaps(
656 jalview.util.Comparison.GapChars, new String(sequence));
657 int[] map = new int[seq.length()];
661 while (j < sequence.length)
663 if (!jalview.util.Comparison.isGap(sequence[j]))
677 * @see jalview.datamodel.SequenceI#findPositionMap()
679 public int[] findPositionMap()
681 int map[] = new int[sequence.length];
684 int seqlen = sequence.length;
688 if (!jalview.util.Comparison.isGap(sequence[j]))
701 * @see jalview.datamodel.SequenceI#deleteChars(int, int)
703 public void deleteChars(int i, int j)
705 int newstart = start, newend = end;
706 if (i >= sequence.length)
713 if (j >= sequence.length)
716 System.arraycopy(sequence, 0, tmp, 0, i);
721 tmp = new char[sequence.length - j + i];
722 System.arraycopy(sequence, 0, tmp, 0, i);
723 System.arraycopy(sequence, j, tmp, i, sequence.length - j);
725 boolean createNewDs = false;
726 // TODO: take a look at the new dataset creation validation method below -
727 // this could become time comsuming for large sequences - consider making it
729 for (int s = i; s < j; s++)
731 if (jalview.schemes.ResidueProperties.aaIndex[sequence[s]] != 23)
739 int sindex = findIndex(start) - 1;
742 // delete characters including start of sequence
743 newstart = findPosition(j);
744 break; // don't need to search for any more residue characters.
748 // delete characters after start.
749 int eindex = findIndex(end) - 1;
752 // delete characters at end of sequence
753 newend = findPosition(i - 1);
754 break; // don't need to search for any more residue characters.
759 newend--; // decrease end position by one for the deleted residue
760 // and search further
766 // deletion occured in the middle of the sequence
767 if (createNewDs && this.datasetSequence != null)
769 // construct a new sequence
770 Sequence ds = new Sequence(datasetSequence);
771 // TODO: remove any non-inheritable properties ?
772 // TODO: create a sequence mapping (since there is a relation here ?)
773 ds.deleteChars(i, j);
774 datasetSequence = ds;
791 public void insertCharAt(int i, int length, char c)
793 char[] tmp = new char[sequence.length + length];
795 if (i >= sequence.length)
797 System.arraycopy(sequence, 0, tmp, 0, sequence.length);
802 System.arraycopy(sequence, 0, tmp, 0, i);
812 if (i < sequence.length)
814 System.arraycopy(sequence, i, tmp, index, sequence.length - i);
820 public void insertCharAt(int i, char c)
822 insertCharAt(i, 1, c);
825 public String getVamsasId()
830 public void setVamsasId(String id)
835 public void setDBRef(DBRefEntry[] dbref)
840 public DBRefEntry[] getDBRef()
842 if (dbrefs == null && datasetSequence != null
843 && this != datasetSequence)
845 return datasetSequence.getDBRef();
850 public void addDBRef(DBRefEntry entry)
854 dbrefs = new DBRefEntry[0];
857 int i, iSize = dbrefs.length;
859 for (i = 0; i < iSize; i++)
861 if (dbrefs[i].equalRef(entry))
863 if (entry.getMap() != null)
865 if (dbrefs[i].getMap() == null)
867 // overwrite with 'superior' entry that contains a mapping.
875 DBRefEntry[] temp = new DBRefEntry[iSize + 1];
876 System.arraycopy(dbrefs, 0, temp, 0, iSize);
877 temp[temp.length - 1] = entry;
882 public void setDatasetSequence(SequenceI seq)
884 datasetSequence = seq;
887 public SequenceI getDatasetSequence()
889 return datasetSequence;
892 public AlignmentAnnotation[] getAnnotation()
894 if (annotation == null)
899 AlignmentAnnotation[] ret = new AlignmentAnnotation[annotation.size()];
900 for (int r = 0; r < ret.length; r++)
902 ret[r] = (AlignmentAnnotation) annotation.elementAt(r);
908 public void addAlignmentAnnotation(AlignmentAnnotation annotation)
910 if (this.annotation == null)
912 this.annotation = new Vector();
914 if (!this.annotation.contains(annotation))
916 this.annotation.addElement(annotation);
918 annotation.setSequenceRef(this);
921 public void removeAlignmentAnnotation(AlignmentAnnotation annotation)
923 if (this.annotation != null)
925 this.annotation.removeElement(annotation);
926 if (this.annotation.size() == 0)
927 this.annotation = null;
932 * test if this is a valid candidate for another sequence's dataset sequence.
935 private boolean isValidDatasetSequence()
937 if (datasetSequence != null)
941 for (int i = 0; i < sequence.length; i++)
943 if (jalview.util.Comparison.isGap(sequence[i]))
954 * @see jalview.datamodel.SequenceI#deriveSequence()
956 public SequenceI deriveSequence()
958 SequenceI seq = new Sequence(this);
959 if (datasetSequence != null)
961 // duplicate current sequence with same dataset
962 seq.setDatasetSequence(datasetSequence);
966 if (isValidDatasetSequence())
968 // Use this as dataset sequence
969 seq.setDatasetSequence(this);
973 // Create a new, valid dataset sequence
975 ds.setSequence(AlignSeq.extractGaps(
976 jalview.util.Comparison.GapChars, new String(sequence)));
977 setDatasetSequence(ds);
978 ds.setSequenceFeatures(getSequenceFeatures());
979 seq = this; // and return this sequence as the derived sequence.
988 * @see jalview.datamodel.SequenceI#createDatasetSequence()
990 public SequenceI createDatasetSequence()
992 if (datasetSequence == null)
994 datasetSequence = new Sequence(getName(), AlignSeq.extractGaps(
995 jalview.util.Comparison.GapChars, getSequenceAsString()),
996 getStart(), getEnd());
997 datasetSequence.setSequenceFeatures(getSequenceFeatures());
998 datasetSequence.setDescription(getDescription());
999 setSequenceFeatures(null);
1000 // move database references onto dataset sequence
1001 datasetSequence.setDBRef(getDBRef());
1003 datasetSequence.setPDBId(getPDBId());
1005 datasetSequence.updatePDBIds();
1006 if (annotation != null)
1008 Vector<AlignmentAnnotation> _annot = annotation;
1010 for (AlignmentAnnotation aa : _annot)
1012 aa.sequenceRef = datasetSequence;
1013 aa.adjustForAlignment(); // uses annotation's own record of
1014 // sequence-column mapping
1015 datasetSequence.addAlignmentAnnotation(aa);
1019 return datasetSequence;
1026 * jalview.datamodel.SequenceI#setAlignmentAnnotation(AlignmmentAnnotation[]
1029 public void setAlignmentAnnotation(AlignmentAnnotation[] annotations)
1031 if (annotation != null)
1033 annotation.removeAllElements();
1035 if (annotations != null)
1037 for (int i = 0; i < annotations.length; i++)
1039 if (annotations[i] != null)
1040 addAlignmentAnnotation(annotations[i]);
1048 * @see jalview.datamodel.SequenceI#getAnnotation(java.lang.String)
1050 public AlignmentAnnotation[] getAnnotation(String label)
1052 if (annotation == null || annotation.size() == 0)
1057 Vector subset = new Vector();
1058 Enumeration e = annotation.elements();
1059 while (e.hasMoreElements())
1061 AlignmentAnnotation ann = (AlignmentAnnotation) e.nextElement();
1062 if (ann.label != null && ann.label.equals(label))
1064 subset.addElement(ann);
1067 if (subset.size() == 0)
1071 AlignmentAnnotation[] anns = new AlignmentAnnotation[subset.size()];
1073 e = subset.elements();
1074 while (e.hasMoreElements())
1076 anns[i++] = (AlignmentAnnotation) e.nextElement();
1078 subset.removeAllElements();
1082 public boolean updatePDBIds()
1084 if (datasetSequence != null)
1086 // TODO: could merge DBRefs
1087 return datasetSequence.updatePDBIds();
1089 if (dbrefs == null || dbrefs.length == 0)
1093 Vector newpdb = new Vector();
1094 for (int i = 0; i < dbrefs.length; i++)
1096 if (DBRefSource.PDB.equals(dbrefs[i].getSource()))
1098 PDBEntry pdbe = new PDBEntry();
1099 pdbe.setId(dbrefs[i].getAccessionId());
1100 if (pdbIds == null || pdbIds.size() == 0)
1102 newpdb.addElement(pdbe);
1106 Enumeration en = pdbIds.elements();
1107 boolean matched = false;
1108 while (!matched && en.hasMoreElements())
1110 PDBEntry anentry = (PDBEntry) en.nextElement();
1111 if (anentry.getId().equals(pdbe.getId()))
1118 newpdb.addElement(pdbe);
1123 if (newpdb.size() > 0)
1125 Enumeration en = newpdb.elements();
1126 while (en.hasMoreElements())
1128 addPDBId((PDBEntry) en.nextElement());
1139 * jalview.datamodel.SequenceI#transferAnnotation(jalview.datamodel.SequenceI,
1140 * jalview.datamodel.Mapping)
1142 public void transferAnnotation(SequenceI entry, Mapping mp)
1144 if (datasetSequence != null)
1146 datasetSequence.transferAnnotation(entry, mp);
1149 if (entry.getDatasetSequence() != null)
1151 transferAnnotation(entry.getDatasetSequence(), mp);
1154 // transfer any new features from entry onto sequence
1155 if (entry.getSequenceFeatures() != null)
1158 SequenceFeature[] sfs = entry.getSequenceFeatures();
1159 for (int si = 0; si < sfs.length; si++)
1161 SequenceFeature sf[] = (mp != null) ? mp.locateFeature(sfs[si])
1162 : new SequenceFeature[]
1163 { new SequenceFeature(sfs[si]) };
1164 if (sf != null && sf.length > 0)
1166 for (int sfi = 0; sfi < sf.length; sfi++)
1168 addSequenceFeature(sf[sfi]);
1174 // transfer PDB entries
1175 if (entry.getPDBId() != null)
1177 Enumeration e = entry.getPDBId().elements();
1178 while (e.hasMoreElements())
1180 PDBEntry pdb = (PDBEntry) e.nextElement();
1184 // transfer database references
1185 DBRefEntry[] entryRefs = entry.getDBRef();
1186 if (entryRefs != null)
1188 for (int r = 0; r < entryRefs.length; r++)
1190 DBRefEntry newref = new DBRefEntry(entryRefs[r]);
1191 if (newref.getMap() != null && mp != null)
1193 // remap ref using our local mapping
1195 // we also assume all version string setting is done by dbSourceProxy
1197 * if (!newref.getSource().equalsIgnoreCase(dbSource)) {
1198 * newref.setSource(dbSource); }
1206 * @return The index (zero-based) on this sequence in the MSA. It returns
1207 * {@code -1} if this information is not available.
1209 public int getIndex()
1215 * Defines the position of this sequence in the MSA. Use the value {@code -1}
1216 * if this information is undefined.
1219 * position for this sequence. This value is zero-based (zero for
1220 * this first sequence)
1222 public void setIndex(int value)
1227 public void setRNA(RNA r)