2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.1)
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
19 package jalview.datamodel;
21 import jalview.analysis.AlignSeq;
23 import java.util.Enumeration;
24 import java.util.Vector;
26 import fr.orsay.lri.varna.models.rna.RNA;
30 * Implements the SequenceI interface for a char[] based sequence object.
35 public class Sequence implements SequenceI
37 SequenceI datasetSequence;
41 private char[] sequence;
58 * This annotation is displayed below the alignment but the positions are tied
59 * to the residues of this sequence
64 * The index of the sequence in a MSA
68 /** array of sequence features - may not be null for a valid sequence object */
69 public SequenceFeature[] sequenceFeatures;
72 * Creates a new Sequence object.
77 * string to form a possibly gapped sequence out of
79 * first position of non-gap residue in the sequence
81 * last position of ungapped residues (nearly always only used for
84 public Sequence(String name, String sequence, int start, int end)
87 this.sequence = sequence.toCharArray();
94 public Sequence(String name, char[] sequence, int start, int end)
97 this.sequence = sequence;
104 com.stevesoft.pat.Regex limitrx = new com.stevesoft.pat.Regex(
105 "[/][0-9]{1,}[-][0-9]{1,}$");
107 com.stevesoft.pat.Regex endrx = new com.stevesoft.pat.Regex("[0-9]{1,}$");
114 .println("POSSIBLE IMPLEMENTATION ERROR: null sequence name passed to constructor.");
117 // Does sequence have the /start-end signiature?
118 if (limitrx.search(name))
120 name = limitrx.left();
121 endrx.search(limitrx.stringMatched());
122 setStart(Integer.parseInt(limitrx.stringMatched().substring(1,
123 endrx.matchedFrom() - 1)));
124 setEnd(Integer.parseInt(endrx.stringMatched()));
128 void checkValidRange()
131 // http://issues.jalview.org/browse/JAL-774?focusedCommentId=11239&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-11239
134 for (int j = 0; j < sequence.length; j++)
136 if (!jalview.util.Comparison.isGap(sequence[j]))
155 * Creates a new Sequence object.
162 public Sequence(String name, String sequence)
164 this(name, sequence, 1, -1);
168 * Creates a new Sequence object with new features, DBRefEntries,
169 * AlignmentAnnotations, and PDBIds but inherits any existing dataset sequence
175 public Sequence(SequenceI seq)
177 this(seq, seq.getAnnotation());
181 * Create a new sequence object with new features, DBRefEntries, and PDBIds
182 * but inherits any existing dataset sequence reference, and duplicate of any
183 * annotation that is present in the given annotation array.
186 * the sequence to be copied
187 * @param alAnnotation
188 * an array of annotation including some associated with seq
190 public Sequence(SequenceI seq, AlignmentAnnotation[] alAnnotation)
192 this(seq.getName(), seq.getSequence(), seq.getStart(), seq.getEnd());
193 description = seq.getDescription();
194 if (seq.getSequenceFeatures() != null)
196 SequenceFeature[] sf = seq.getSequenceFeatures();
197 for (int i = 0; i < sf.length; i++)
199 addSequenceFeature(new SequenceFeature(sf[i]));
202 setDatasetSequence(seq.getDatasetSequence());
203 if (datasetSequence == null && seq.getDBRef() != null)
205 // only copy DBRefs if we really are a dataset sequence
206 DBRefEntry[] dbr = seq.getDBRef();
207 for (int i = 0; i < dbr.length; i++)
209 addDBRef(new DBRefEntry(dbr[i]));
212 if (seq.getAnnotation() != null)
214 AlignmentAnnotation[] sqann = seq.getAnnotation();
215 for (int i = 0; i < sqann.length; i++)
217 if (sqann[i] == null)
221 boolean found = (alAnnotation == null);
224 for (int apos = 0; !found && apos < alAnnotation.length; apos++)
226 found = (alAnnotation[apos] == sqann[i]);
231 // only copy the given annotation
232 AlignmentAnnotation newann = new AlignmentAnnotation(sqann[i]);
233 addAlignmentAnnotation(newann);
237 if (seq.getPDBId() != null)
239 Vector ids = seq.getPDBId();
240 Enumeration e = ids.elements();
241 while (e.hasMoreElements())
243 this.addPDBId(new PDBEntry((PDBEntry) e.nextElement()));
254 public void setSequenceFeatures(SequenceFeature[] features)
256 sequenceFeatures = features;
259 public synchronized void addSequenceFeature(SequenceFeature sf)
261 if (sequenceFeatures == null)
263 sequenceFeatures = new SequenceFeature[0];
266 for (int i = 0; i < sequenceFeatures.length; i++)
268 if (sequenceFeatures[i].equals(sf))
274 SequenceFeature[] temp = new SequenceFeature[sequenceFeatures.length + 1];
275 System.arraycopy(sequenceFeatures, 0, temp, 0, sequenceFeatures.length);
276 temp[sequenceFeatures.length] = sf;
278 sequenceFeatures = temp;
281 public void deleteFeature(SequenceFeature sf)
283 if (sequenceFeatures == null)
289 for (index = 0; index < sequenceFeatures.length; index++)
291 if (sequenceFeatures[index].equals(sf))
297 if (index == sequenceFeatures.length)
302 int sfLength = sequenceFeatures.length;
305 sequenceFeatures = null;
309 SequenceFeature[] temp = new SequenceFeature[sfLength - 1];
310 System.arraycopy(sequenceFeatures, 0, temp, 0, index);
312 if (index < sfLength)
314 System.arraycopy(sequenceFeatures, index + 1, temp, index,
315 sequenceFeatures.length - index - 1);
318 sequenceFeatures = temp;
325 * @return DOCUMENT ME!
327 public SequenceFeature[] getSequenceFeatures()
329 return sequenceFeatures;
332 public void addPDBId(PDBEntry entry)
336 pdbIds = new Vector();
338 if (!pdbIds.contains(entry))
340 pdbIds.addElement(entry);
350 public void setPDBId(Vector id)
358 * @return DOCUMENT ME!
360 public Vector getPDBId()
368 * @return DOCUMENT ME!
370 public String getDisplayId(boolean jvsuffix)
372 StringBuffer result = new StringBuffer(name);
375 result.append("/" + start + "-" + end);
378 return result.toString();
387 public void setName(String name)
396 * @return DOCUMENT ME!
398 public String getName()
409 public void setStart(int start)
417 * @return DOCUMENT ME!
419 public int getStart()
430 public void setEnd(int end)
438 * @return DOCUMENT ME!
448 * @return DOCUMENT ME!
450 public int getLength()
452 return this.sequence.length;
461 public void setSequence(String seq)
463 this.sequence = seq.toCharArray();
467 public String getSequenceAsString()
469 return new String(sequence);
472 public String getSequenceAsString(int start, int end)
474 return new String(getSequence(start, end));
477 public char[] getSequence()
485 * @see jalview.datamodel.SequenceI#getSequence(int, int)
487 public char[] getSequence(int start, int end)
491 // JBPNote - left to user to pad the result here (TODO:Decide on this
493 if (start >= sequence.length)
498 if (end >= sequence.length)
500 end = sequence.length;
503 char[] reply = new char[end - start];
504 System.arraycopy(sequence, start, reply, 0, end - start);
510 * make a new Sequence object from start to end (including gaps) over this
519 public SequenceI getSubSequence(int start, int end)
525 char[] seq = getSequence(start, end);
530 int nstart = findPosition(start);
531 int nend = findPosition(end) - 1;
532 // JBPNote - this is an incomplete copy.
533 SequenceI nseq = new Sequence(this.getName(), seq, nstart, nend);
534 nseq.setDescription(description);
535 if (datasetSequence != null)
537 nseq.setDatasetSequence(datasetSequence);
541 nseq.setDatasetSequence(this);
552 * @return DOCUMENT ME!
554 public char getCharAt(int i)
556 if (i < sequence.length)
572 public void setDescription(String desc)
574 this.description = desc;
580 * @return DOCUMENT ME!
582 public String getDescription()
584 return this.description;
590 * @see jalview.datamodel.SequenceI#findIndex(int)
592 public int findIndex(int pos)
594 // returns the alignment position for a residue
597 // Rely on end being at least as long as the length of the sequence.
598 while ((i < sequence.length) && (j <= end) && (j <= pos))
600 if (!jalview.util.Comparison.isGap(sequence[i]))
608 if ((j == end) && (j < pos))
619 * Returns the sequence position for an alignment position
622 * column index in alignment (from 1)
624 * @return residue number for residue (left of and) nearest ith column
626 public int findPosition(int i)
630 int seqlen = sequence.length;
631 while ((j < i) && (j < seqlen))
633 if (!jalview.util.Comparison.isGap(sequence[j]))
645 * Returns an int array where indices correspond to each residue in the
646 * sequence and the element value gives its position in the alignment
648 * @return int[SequenceI.getEnd()-SequenceI.getStart()+1] or null if no
649 * residues in SequenceI object
651 public int[] gapMap()
653 String seq = jalview.analysis.AlignSeq.extractGaps(
654 jalview.util.Comparison.GapChars, new String(sequence));
655 int[] map = new int[seq.length()];
659 while (j < sequence.length)
661 if (!jalview.util.Comparison.isGap(sequence[j]))
675 * @see jalview.datamodel.SequenceI#findPositionMap()
677 public int[] findPositionMap()
679 int map[] = new int[sequence.length];
682 int seqlen = sequence.length;
686 if (!jalview.util.Comparison.isGap(sequence[j]))
699 * @see jalview.datamodel.SequenceI#deleteChars(int, int)
701 public void deleteChars(int i, int j)
703 int newstart = start, newend = end;
704 if (i >= sequence.length)
711 if (j >= sequence.length)
714 System.arraycopy(sequence, 0, tmp, 0, i);
719 tmp = new char[sequence.length - j + i];
720 System.arraycopy(sequence, 0, tmp, 0, i);
721 System.arraycopy(sequence, j, tmp, i, sequence.length - j);
723 boolean createNewDs = false;
724 // TODO: take a look at the new dataset creation validation method below -
725 // this could become time comsuming for large sequences - consider making it
727 for (int s = i; s < j; s++)
729 if (jalview.schemes.ResidueProperties.aaIndex[sequence[s]] != 23)
737 int sindex = findIndex(start) - 1;
740 // delete characters including start of sequence
741 newstart = findPosition(j);
742 break; // don't need to search for any more residue characters.
746 // delete characters after start.
747 int eindex = findIndex(end) - 1;
750 // delete characters at end of sequence
751 newend = findPosition(i - 1);
752 break; // don't need to search for any more residue characters.
757 newend--; // decrease end position by one for the deleted residue
758 // and search further
764 // deletion occured in the middle of the sequence
765 if (createNewDs && this.datasetSequence != null)
767 // construct a new sequence
768 Sequence ds = new Sequence(datasetSequence);
769 // TODO: remove any non-inheritable properties ?
770 // TODO: create a sequence mapping (since there is a relation here ?)
771 ds.deleteChars(i, j);
772 datasetSequence = ds;
789 public void insertCharAt(int i, int length, char c)
791 char[] tmp = new char[sequence.length + length];
793 if (i >= sequence.length)
795 System.arraycopy(sequence, 0, tmp, 0, sequence.length);
800 System.arraycopy(sequence, 0, tmp, 0, i);
810 if (i < sequence.length)
812 System.arraycopy(sequence, i, tmp, index, sequence.length - i);
818 public void insertCharAt(int i, char c)
820 insertCharAt(i, 1, c);
823 public String getVamsasId()
828 public void setVamsasId(String id)
833 public void setDBRef(DBRefEntry[] dbref)
838 public DBRefEntry[] getDBRef()
840 if (dbrefs == null && datasetSequence != null
841 && this != datasetSequence)
843 return datasetSequence.getDBRef();
848 public void addDBRef(DBRefEntry entry)
852 dbrefs = new DBRefEntry[0];
855 int i, iSize = dbrefs.length;
857 for (i = 0; i < iSize; i++)
859 if (dbrefs[i].equalRef(entry))
861 if (entry.getMap() != null)
863 if (dbrefs[i].getMap() == null)
865 // overwrite with 'superior' entry that contains a mapping.
873 DBRefEntry[] temp = new DBRefEntry[iSize + 1];
874 System.arraycopy(dbrefs, 0, temp, 0, iSize);
875 temp[temp.length - 1] = entry;
880 public void setDatasetSequence(SequenceI seq)
882 datasetSequence = seq;
885 public SequenceI getDatasetSequence()
887 return datasetSequence;
890 public AlignmentAnnotation[] getAnnotation()
892 if (annotation == null)
897 AlignmentAnnotation[] ret = new AlignmentAnnotation[annotation.size()];
898 for (int r = 0; r < ret.length; r++)
900 ret[r] = (AlignmentAnnotation) annotation.elementAt(r);
906 public void addAlignmentAnnotation(AlignmentAnnotation annotation)
908 if (this.annotation == null)
910 this.annotation = new Vector();
912 if (!this.annotation.contains(annotation))
914 this.annotation.addElement(annotation);
916 annotation.setSequenceRef(this);
919 public void removeAlignmentAnnotation(AlignmentAnnotation annotation)
921 if (this.annotation != null)
923 this.annotation.removeElement(annotation);
924 if (this.annotation.size() == 0)
925 this.annotation = null;
930 * test if this is a valid candidate for another sequence's dataset sequence.
933 private boolean isValidDatasetSequence()
935 if (datasetSequence != null)
939 for (int i = 0; i < sequence.length; i++)
941 if (jalview.util.Comparison.isGap(sequence[i]))
952 * @see jalview.datamodel.SequenceI#deriveSequence()
954 public SequenceI deriveSequence()
956 SequenceI seq = new Sequence(this);
957 if (datasetSequence != null)
959 // duplicate current sequence with same dataset
960 seq.setDatasetSequence(datasetSequence);
964 if (isValidDatasetSequence())
966 // Use this as dataset sequence
967 seq.setDatasetSequence(this);
971 // Create a new, valid dataset sequence
973 ds.setSequence(AlignSeq.extractGaps(
974 jalview.util.Comparison.GapChars, new String(sequence)));
975 setDatasetSequence(ds);
976 ds.setSequenceFeatures(getSequenceFeatures());
977 seq = this; // and return this sequence as the derived sequence.
986 * @see jalview.datamodel.SequenceI#createDatasetSequence()
988 public SequenceI createDatasetSequence()
990 if (datasetSequence == null)
992 datasetSequence = new Sequence(getName(), AlignSeq.extractGaps(
993 jalview.util.Comparison.GapChars, getSequenceAsString()),
994 getStart(), getEnd());
995 datasetSequence.setSequenceFeatures(getSequenceFeatures());
996 datasetSequence.setDescription(getDescription());
997 setSequenceFeatures(null);
998 // move database references onto dataset sequence
999 datasetSequence.setDBRef(getDBRef());
1001 datasetSequence.setPDBId(getPDBId());
1003 datasetSequence.updatePDBIds();
1004 if (annotation != null)
1006 Vector<AlignmentAnnotation> _annot = annotation;
1008 for (AlignmentAnnotation aa : _annot)
1010 aa.sequenceRef = datasetSequence;
1011 aa.adjustForAlignment(); // uses annotation's own record of
1012 // sequence-column mapping
1013 datasetSequence.addAlignmentAnnotation(aa);
1017 return datasetSequence;
1024 * jalview.datamodel.SequenceI#setAlignmentAnnotation(AlignmmentAnnotation[]
1027 public void setAlignmentAnnotation(AlignmentAnnotation[] annotations)
1029 if (annotation != null)
1031 annotation.removeAllElements();
1033 if (annotations != null)
1035 for (int i = 0; i < annotations.length; i++)
1037 if (annotations[i] != null)
1038 addAlignmentAnnotation(annotations[i]);
1046 * @see jalview.datamodel.SequenceI#getAnnotation(java.lang.String)
1048 public AlignmentAnnotation[] getAnnotation(String label)
1050 if (annotation == null || annotation.size() == 0)
1055 Vector subset = new Vector();
1056 Enumeration e = annotation.elements();
1057 while (e.hasMoreElements())
1059 AlignmentAnnotation ann = (AlignmentAnnotation) e.nextElement();
1060 if (ann.label != null && ann.label.equals(label))
1062 subset.addElement(ann);
1065 if (subset.size() == 0)
1069 AlignmentAnnotation[] anns = new AlignmentAnnotation[subset.size()];
1071 e = subset.elements();
1072 while (e.hasMoreElements())
1074 anns[i++] = (AlignmentAnnotation) e.nextElement();
1076 subset.removeAllElements();
1080 public boolean updatePDBIds()
1082 if (datasetSequence != null)
1084 // TODO: could merge DBRefs
1085 return datasetSequence.updatePDBIds();
1087 if (dbrefs == null || dbrefs.length == 0)
1091 Vector newpdb = new Vector();
1092 for (int i = 0; i < dbrefs.length; i++)
1094 if (DBRefSource.PDB.equals(dbrefs[i].getSource()))
1096 PDBEntry pdbe = new PDBEntry();
1097 pdbe.setId(dbrefs[i].getAccessionId());
1098 if (pdbIds == null || pdbIds.size() == 0)
1100 newpdb.addElement(pdbe);
1104 Enumeration en = pdbIds.elements();
1105 boolean matched = false;
1106 while (!matched && en.hasMoreElements())
1108 PDBEntry anentry = (PDBEntry) en.nextElement();
1109 if (anentry.getId().equals(pdbe.getId()))
1116 newpdb.addElement(pdbe);
1121 if (newpdb.size() > 0)
1123 Enumeration en = newpdb.elements();
1124 while (en.hasMoreElements())
1126 addPDBId((PDBEntry) en.nextElement());
1137 * jalview.datamodel.SequenceI#transferAnnotation(jalview.datamodel.SequenceI,
1138 * jalview.datamodel.Mapping)
1140 public void transferAnnotation(SequenceI entry, Mapping mp)
1142 if (datasetSequence != null)
1144 datasetSequence.transferAnnotation(entry, mp);
1147 if (entry.getDatasetSequence() != null)
1149 transferAnnotation(entry.getDatasetSequence(), mp);
1152 // transfer any new features from entry onto sequence
1153 if (entry.getSequenceFeatures() != null)
1156 SequenceFeature[] sfs = entry.getSequenceFeatures();
1157 for (int si = 0; si < sfs.length; si++)
1159 SequenceFeature sf[] = (mp != null) ? mp.locateFeature(sfs[si])
1160 : new SequenceFeature[]
1161 { new SequenceFeature(sfs[si]) };
1162 if (sf != null && sf.length > 0)
1164 for (int sfi = 0; sfi < sf.length; sfi++)
1166 addSequenceFeature(sf[sfi]);
1172 // transfer PDB entries
1173 if (entry.getPDBId() != null)
1175 Enumeration e = entry.getPDBId().elements();
1176 while (e.hasMoreElements())
1178 PDBEntry pdb = (PDBEntry) e.nextElement();
1182 // transfer database references
1183 DBRefEntry[] entryRefs = entry.getDBRef();
1184 if (entryRefs != null)
1186 for (int r = 0; r < entryRefs.length; r++)
1188 DBRefEntry newref = new DBRefEntry(entryRefs[r]);
1189 if (newref.getMap() != null && mp != null)
1191 // remap ref using our local mapping
1193 // we also assume all version string setting is done by dbSourceProxy
1195 * if (!newref.getSource().equalsIgnoreCase(dbSource)) {
1196 * newref.setSource(dbSource); }
1204 * @return The index (zero-based) on this sequence in the MSA. It returns
1205 * {@code -1} if this information is not available.
1207 public int getIndex()
1213 * Defines the position of this sequence in the MSA. Use the value {@code -1}
1214 * if this information is undefined.
1217 * position for this sequence. This value is zero-based (zero for
1218 * this first sequence)
1220 public void setIndex(int value)
1225 public void setRNA(RNA r){rna=r;}
1227 public RNA getRNA() { return rna; }