2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
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/>.
18 package jalview.datamodel;
20 import jalview.analysis.AlignSeq;
22 import java.util.Enumeration;
23 import java.util.Vector;
25 import fr.orsay.lri.varna.models.rna.RNA;
29 * Implements the SequenceI interface for a char[] based sequence object.
34 public class Sequence implements SequenceI
36 SequenceI datasetSequence;
40 private char[] sequence;
57 * This annotation is displayed below the alignment but the positions are tied
58 * to the residues of this sequence
63 * The index of the sequence in a MSA
67 /** array of sequence features - may not be null for a valid sequence object */
68 public SequenceFeature[] sequenceFeatures;
71 * Creates a new Sequence object.
76 * string to form a possibly gapped sequence out of
78 * first position of non-gap residue in the sequence
80 * last position of ungapped residues (nearly always only used for
83 public Sequence(String name, String sequence, int start, int end)
86 this.sequence = sequence.toCharArray();
93 public Sequence(String name, char[] sequence, int start, int end)
96 this.sequence = sequence;
103 com.stevesoft.pat.Regex limitrx = new com.stevesoft.pat.Regex(
104 "[/][0-9]{1,}[-][0-9]{1,}$");
106 com.stevesoft.pat.Regex endrx = new com.stevesoft.pat.Regex("[0-9]{1,}$");
113 .println("POSSIBLE IMPLEMENTATION ERROR: null sequence name passed to constructor.");
116 // Does sequence have the /start-end signiature?
117 if (limitrx.search(name))
119 name = limitrx.left();
120 endrx.search(limitrx.stringMatched());
121 setStart(Integer.parseInt(limitrx.stringMatched().substring(1,
122 endrx.matchedFrom() - 1)));
123 setEnd(Integer.parseInt(endrx.stringMatched()));
127 void checkValidRange()
129 // Note: JAL-774 : http://issues.jalview.org/browse/JAL-774?focusedCommentId=11239&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-11239
132 for (int j = 0; j < sequence.length; j++)
134 if (!jalview.util.Comparison.isGap(sequence[j]))
152 * Creates a new Sequence object.
159 public Sequence(String name, String sequence)
161 this(name, sequence, 1, -1);
165 * Creates a new Sequence object with new features, DBRefEntries,
166 * AlignmentAnnotations, and PDBIds but inherits any existing dataset sequence
172 public Sequence(SequenceI seq)
174 this(seq, seq.getAnnotation());
178 * Create a new sequence object with new features, DBRefEntries, and PDBIds
179 * but inherits any existing dataset sequence reference, and duplicate of any
180 * annotation that is present in the given annotation array.
183 * the sequence to be copied
184 * @param alAnnotation
185 * an array of annotation including some associated with seq
187 public Sequence(SequenceI seq, AlignmentAnnotation[] alAnnotation)
189 this(seq.getName(), seq.getSequence(), seq.getStart(), seq.getEnd());
190 description = seq.getDescription();
191 if (seq.getSequenceFeatures() != null)
193 SequenceFeature[] sf = seq.getSequenceFeatures();
194 for (int i = 0; i < sf.length; i++)
196 addSequenceFeature(new SequenceFeature(sf[i]));
199 setDatasetSequence(seq.getDatasetSequence());
200 if (datasetSequence == null && seq.getDBRef() != null)
202 // only copy DBRefs if we really are a dataset sequence
203 DBRefEntry[] dbr = seq.getDBRef();
204 for (int i = 0; i < dbr.length; i++)
206 addDBRef(new DBRefEntry(dbr[i]));
209 if (seq.getAnnotation() != null)
211 AlignmentAnnotation[] sqann = seq.getAnnotation();
212 for (int i = 0; i < sqann.length; i++)
214 if (sqann[i] == null)
218 boolean found = (alAnnotation == null);
221 for (int apos = 0; !found && apos < alAnnotation.length; apos++)
223 found = (alAnnotation[apos] == sqann[i]);
228 // only copy the given annotation
229 AlignmentAnnotation newann = new AlignmentAnnotation(sqann[i]);
230 addAlignmentAnnotation(newann);
234 if (seq.getPDBId() != null)
236 Vector ids = seq.getPDBId();
237 Enumeration e = ids.elements();
238 while (e.hasMoreElements())
240 this.addPDBId(new PDBEntry((PDBEntry) e.nextElement()));
251 public void setSequenceFeatures(SequenceFeature[] features)
253 sequenceFeatures = features;
256 public synchronized void addSequenceFeature(SequenceFeature sf)
258 if (sequenceFeatures == null)
260 sequenceFeatures = new SequenceFeature[0];
263 for (int i = 0; i < sequenceFeatures.length; i++)
265 if (sequenceFeatures[i].equals(sf))
271 SequenceFeature[] temp = new SequenceFeature[sequenceFeatures.length + 1];
272 System.arraycopy(sequenceFeatures, 0, temp, 0, sequenceFeatures.length);
273 temp[sequenceFeatures.length] = sf;
275 sequenceFeatures = temp;
278 public void deleteFeature(SequenceFeature sf)
280 if (sequenceFeatures == null)
286 for (index = 0; index < sequenceFeatures.length; index++)
288 if (sequenceFeatures[index].equals(sf))
294 if (index == sequenceFeatures.length)
299 int sfLength = sequenceFeatures.length;
302 sequenceFeatures = null;
306 SequenceFeature[] temp = new SequenceFeature[sfLength - 1];
307 System.arraycopy(sequenceFeatures, 0, temp, 0, index);
309 if (index < sfLength)
311 System.arraycopy(sequenceFeatures, index + 1, temp, index,
312 sequenceFeatures.length - index - 1);
315 sequenceFeatures = temp;
322 * @return DOCUMENT ME!
324 public SequenceFeature[] getSequenceFeatures()
326 return sequenceFeatures;
329 public void addPDBId(PDBEntry entry)
333 pdbIds = new Vector();
335 if (!pdbIds.contains(entry))
337 pdbIds.addElement(entry);
347 public void setPDBId(Vector id)
355 * @return DOCUMENT ME!
357 public Vector getPDBId()
365 * @return DOCUMENT ME!
367 public String getDisplayId(boolean jvsuffix)
369 StringBuffer result = new StringBuffer(name);
372 result.append("/" + start + "-" + end);
375 return result.toString();
384 public void setName(String name)
393 * @return DOCUMENT ME!
395 public String getName()
406 public void setStart(int start)
414 * @return DOCUMENT ME!
416 public int getStart()
427 public void setEnd(int end)
435 * @return DOCUMENT ME!
445 * @return DOCUMENT ME!
447 public int getLength()
449 return this.sequence.length;
458 public void setSequence(String seq)
460 this.sequence = seq.toCharArray();
464 public String getSequenceAsString()
466 return new String(sequence);
469 public String getSequenceAsString(int start, int end)
471 return new String(getSequence(start, end));
474 public char[] getSequence()
482 * @see jalview.datamodel.SequenceI#getSequence(int, int)
484 public char[] getSequence(int start, int end)
488 // JBPNote - left to user to pad the result here (TODO:Decide on this
490 if (start >= sequence.length)
495 if (end >= sequence.length)
497 end = sequence.length;
500 char[] reply = new char[end - start];
501 System.arraycopy(sequence, start, reply, 0, end - start);
507 * make a new Sequence object from start to end (including gaps) over this
516 public SequenceI getSubSequence(int start, int end)
522 char[] seq = getSequence(start, end);
527 int nstart = findPosition(start);
528 int nend = findPosition(end) - 1;
529 // JBPNote - this is an incomplete copy.
530 SequenceI nseq = new Sequence(this.getName(), seq, nstart, nend);
531 nseq.setDescription(description);
532 if (datasetSequence != null)
534 nseq.setDatasetSequence(datasetSequence);
538 nseq.setDatasetSequence(this);
549 * @return DOCUMENT ME!
551 public char getCharAt(int i)
553 if (i < sequence.length)
569 public void setDescription(String desc)
571 this.description = desc;
577 * @return DOCUMENT ME!
579 public String getDescription()
581 return this.description;
585 * Return the alignment position for a sequence position
588 * lying from start to end
590 * @return aligned position of residue pos
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);
718 tmp = new char[sequence.length - j + i];
719 System.arraycopy(sequence, 0, tmp, 0, i);
720 System.arraycopy(sequence, j, tmp, i, sequence.length - j);
722 boolean createNewDs = false;
723 for (int s = i; s < j; s++)
725 if (jalview.schemes.ResidueProperties.aaIndex[sequence[s]] != 23)
733 int sindex = findIndex(start) - 1;
736 // delete characters including start of sequence
737 newstart = findPosition(j);
738 break; // don't need to search for any more residue characters.
742 // delete characters after start.
743 int eindex = findIndex(end) - 1;
746 // delete characters at end of sequence
747 newend = findPosition(i - 1);
748 break; // don't need to search for any more residue characters.
753 newend--; // decrease end position by one for the deleted residue
754 // and search further
760 // deletion occured in the middle of the sequence
761 if (createNewDs && this.datasetSequence != null)
763 // construct a new sequence
764 Sequence ds = new Sequence(datasetSequence);
765 // TODO: remove any non-inheritable properties ?
766 // TODO: create a sequence mapping (since there is a relation here ?)
767 ds.deleteChars(i, j);
768 datasetSequence = ds;
785 public void insertCharAt(int i, int length, char c)
787 char[] tmp = new char[sequence.length + length];
789 if (i >= sequence.length)
791 System.arraycopy(sequence, 0, tmp, 0, sequence.length);
796 System.arraycopy(sequence, 0, tmp, 0, i);
806 if (i < sequence.length)
808 System.arraycopy(sequence, i, tmp, index, sequence.length - i);
814 public void insertCharAt(int i, char c)
816 insertCharAt(i, 1, c);
819 public String getVamsasId()
824 public void setVamsasId(String id)
829 public void setDBRef(DBRefEntry[] dbref)
834 public DBRefEntry[] getDBRef()
836 if (dbrefs == null && datasetSequence != null
837 && this != datasetSequence)
839 return datasetSequence.getDBRef();
844 public void addDBRef(DBRefEntry entry)
848 dbrefs = new DBRefEntry[0];
851 int i, iSize = dbrefs.length;
853 for (i = 0; i < iSize; i++)
855 if (dbrefs[i].equalRef(entry))
857 if (entry.getMap() != null)
859 if (dbrefs[i].getMap() == null)
861 // overwrite with 'superior' entry that contains a mapping.
869 DBRefEntry[] temp = new DBRefEntry[iSize + 1];
870 System.arraycopy(dbrefs, 0, temp, 0, iSize);
871 temp[temp.length - 1] = entry;
876 public void setDatasetSequence(SequenceI seq)
878 datasetSequence = seq;
881 public SequenceI getDatasetSequence()
883 return datasetSequence;
886 public AlignmentAnnotation[] getAnnotation()
888 if (annotation == null)
893 AlignmentAnnotation[] ret = new AlignmentAnnotation[annotation.size()];
894 for (int r = 0; r < ret.length; r++)
896 ret[r] = (AlignmentAnnotation) annotation.elementAt(r);
902 public void addAlignmentAnnotation(AlignmentAnnotation annotation)
904 if (this.annotation == null)
906 this.annotation = new Vector();
908 if (!this.annotation.contains(annotation))
910 this.annotation.addElement(annotation);
912 annotation.setSequenceRef(this);
915 public void removeAlignmentAnnotation(AlignmentAnnotation annotation)
917 if (this.annotation != null)
919 this.annotation.removeElement(annotation);
920 if (this.annotation.size() == 0)
921 this.annotation = null;
926 * test if this is a valid candidate for another sequence's dataset sequence.
929 private boolean isValidDatasetSequence()
931 if (datasetSequence != null)
935 for (int i = 0; i < sequence.length; i++)
937 if (jalview.util.Comparison.isGap(sequence[i]))
948 * @see jalview.datamodel.SequenceI#deriveSequence()
950 public SequenceI deriveSequence()
952 SequenceI seq = new Sequence(this);
953 if (datasetSequence != null)
955 // duplicate current sequence with same dataset
956 seq.setDatasetSequence(datasetSequence);
960 if (isValidDatasetSequence())
962 // Use this as dataset sequence
963 seq.setDatasetSequence(this);
967 // Create a new, valid dataset sequence
969 ds.setSequence(AlignSeq.extractGaps(
970 jalview.util.Comparison.GapChars, new String(sequence)));
971 setDatasetSequence(ds);
972 ds.setSequenceFeatures(getSequenceFeatures());
973 seq = this; // and return this sequence as the derived sequence.
982 * @see jalview.datamodel.SequenceI#createDatasetSequence()
984 public SequenceI createDatasetSequence()
986 if (datasetSequence == null)
988 datasetSequence = new Sequence(getName(), AlignSeq.extractGaps(
989 jalview.util.Comparison.GapChars, getSequenceAsString()),
990 getStart(), getEnd());
991 datasetSequence.setSequenceFeatures(getSequenceFeatures());
992 datasetSequence.setDescription(getDescription());
993 setSequenceFeatures(null);
994 // move database references onto dataset sequence
995 datasetSequence.setDBRef(getDBRef());
997 datasetSequence.setPDBId(getPDBId());
999 datasetSequence.updatePDBIds();
1001 return datasetSequence;
1008 * jalview.datamodel.SequenceI#setAlignmentAnnotation(AlignmmentAnnotation[]
1011 public void setAlignmentAnnotation(AlignmentAnnotation[] annotations)
1013 if (annotation != null)
1015 annotation.removeAllElements();
1017 if (annotations != null)
1019 for (int i = 0; i < annotations.length; i++)
1021 if (annotations[i] != null)
1022 addAlignmentAnnotation(annotations[i]);
1030 * @see jalview.datamodel.SequenceI#getAnnotation(java.lang.String)
1032 public AlignmentAnnotation[] getAnnotation(String label)
1034 if (annotation == null || annotation.size() == 0)
1039 Vector subset = new Vector();
1040 Enumeration e = annotation.elements();
1041 while (e.hasMoreElements())
1043 AlignmentAnnotation ann = (AlignmentAnnotation) e.nextElement();
1044 if (ann.label != null && ann.label.equals(label))
1046 subset.addElement(ann);
1049 if (subset.size() == 0)
1053 AlignmentAnnotation[] anns = new AlignmentAnnotation[subset.size()];
1055 e = subset.elements();
1056 while (e.hasMoreElements())
1058 anns[i++] = (AlignmentAnnotation) e.nextElement();
1060 subset.removeAllElements();
1064 public boolean updatePDBIds()
1066 if (datasetSequence != null)
1068 // TODO: could merge DBRefs
1069 return datasetSequence.updatePDBIds();
1071 if (dbrefs == null || dbrefs.length == 0)
1075 Vector newpdb = new Vector();
1076 for (int i = 0; i < dbrefs.length; i++)
1078 if (DBRefSource.PDB.equals(dbrefs[i].getSource()))
1080 PDBEntry pdbe = new PDBEntry();
1081 pdbe.setId(dbrefs[i].getAccessionId());
1082 if (pdbIds == null || pdbIds.size() == 0)
1084 newpdb.addElement(pdbe);
1088 Enumeration en = pdbIds.elements();
1089 boolean matched = false;
1090 while (!matched && en.hasMoreElements())
1092 PDBEntry anentry = (PDBEntry) en.nextElement();
1093 if (anentry.getId().equals(pdbe.getId()))
1100 newpdb.addElement(pdbe);
1105 if (newpdb.size() > 0)
1107 Enumeration en = newpdb.elements();
1108 while (en.hasMoreElements())
1110 addPDBId((PDBEntry) en.nextElement());
1121 * jalview.datamodel.SequenceI#transferAnnotation(jalview.datamodel.SequenceI,
1122 * jalview.datamodel.Mapping)
1124 public void transferAnnotation(SequenceI entry, Mapping mp)
1126 if (datasetSequence != null)
1128 datasetSequence.transferAnnotation(entry, mp);
1131 if (entry.getDatasetSequence() != null)
1133 transferAnnotation(entry.getDatasetSequence(), mp);
1136 // transfer any new features from entry onto sequence
1137 if (entry.getSequenceFeatures() != null)
1140 SequenceFeature[] sfs = entry.getSequenceFeatures();
1141 for (int si = 0; si < sfs.length; si++)
1143 SequenceFeature sf[] = (mp != null) ? mp.locateFeature(sfs[si])
1144 : new SequenceFeature[]
1145 { new SequenceFeature(sfs[si]) };
1146 if (sf != null && sf.length > 0)
1148 for (int sfi = 0; sfi < sf.length; sfi++)
1150 addSequenceFeature(sf[sfi]);
1156 // transfer PDB entries
1157 if (entry.getPDBId() != null)
1159 Enumeration e = entry.getPDBId().elements();
1160 while (e.hasMoreElements())
1162 PDBEntry pdb = (PDBEntry) e.nextElement();
1166 // transfer database references
1167 DBRefEntry[] entryRefs = entry.getDBRef();
1168 if (entryRefs != null)
1170 for (int r = 0; r < entryRefs.length; r++)
1172 DBRefEntry newref = new DBRefEntry(entryRefs[r]);
1173 if (newref.getMap() != null && mp != null)
1175 // remap ref using our local mapping
1177 // we also assume all version string setting is done by dbSourceProxy
1179 * if (!newref.getSource().equalsIgnoreCase(dbSource)) {
1180 * newref.setSource(dbSource); }
1188 * @return The index (zero-based) on this sequence in the MSA.
1189 * It returns {@code -1} if this information is not available.
1191 public int getIndex() { return index; }
1194 * Defines the position of this sequence in the MSA.
1195 * Use the value {@code -1} if this information is undefined.
1197 * @param The position for this sequence. This value is zero-based (zero for this first sequence)
1199 public void setIndex(int value) { index = value; }
1201 public void setRNA(RNA r){rna=r;}
1203 public RNA getRNA() { return rna; }