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;
27 * Implements the SequenceI interface for a char[] based sequence object.
32 public class Sequence implements SequenceI
34 SequenceI datasetSequence;
38 private char[] sequence;
53 * This annotation is displayed below the alignment but the positions are tied
54 * to the residues of this sequence
59 * The index of the sequence in a MSA
63 /** array of seuqence features - may not be null for a valid sequence object */
64 public SequenceFeature[] sequenceFeatures;
67 * Creates a new Sequence object.
72 * string to form a possibly gapped sequence out of
74 * first position of non-gap residue in the sequence
76 * last position of ungapped residues (nearly always only used for
79 public Sequence(String name, String sequence, int start, int end)
82 this.sequence = sequence.toCharArray();
89 public Sequence(String name, char[] sequence, int start, int end)
92 this.sequence = sequence;
99 com.stevesoft.pat.Regex limitrx = new com.stevesoft.pat.Regex(
100 "[/][0-9]{1,}[-][0-9]{1,}$");
102 com.stevesoft.pat.Regex endrx = new com.stevesoft.pat.Regex("[0-9]{1,}$");
109 .println("POSSIBLE IMPLEMENTATION ERROR: null sequence name passed to constructor.");
112 // Does sequence have the /start-end signiature?
113 if (limitrx.search(name))
115 name = limitrx.left();
116 endrx.search(limitrx.stringMatched());
117 setStart(Integer.parseInt(limitrx.stringMatched().substring(1,
118 endrx.matchedFrom() - 1)));
119 setEnd(Integer.parseInt(endrx.stringMatched()));
123 void checkValidRange()
125 // Note: JAL-774 : http://issues.jalview.org/browse/JAL-774?focusedCommentId=11239&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-11239
128 for (int j = 0; j < sequence.length; j++)
130 if (!jalview.util.Comparison.isGap(sequence[j]))
148 * Creates a new Sequence object.
155 public Sequence(String name, String sequence)
157 this(name, sequence, 1, -1);
161 * Creates a new Sequence object with new features, DBRefEntries,
162 * AlignmentAnnotations, and PDBIds but inherits any existing dataset sequence
168 public Sequence(SequenceI seq)
170 this(seq, seq.getAnnotation());
174 * Create a new sequence object with new features, DBRefEntries, and PDBIds
175 * but inherits any existing dataset sequence reference, and duplicate of any
176 * annotation that is present in the given annotation array.
179 * the sequence to be copied
180 * @param alAnnotation
181 * an array of annotation including some associated with seq
183 public Sequence(SequenceI seq, AlignmentAnnotation[] alAnnotation)
185 this(seq.getName(), seq.getSequence(), seq.getStart(), seq.getEnd());
186 description = seq.getDescription();
187 if (seq.getSequenceFeatures() != null)
189 SequenceFeature[] sf = seq.getSequenceFeatures();
190 for (int i = 0; i < sf.length; i++)
192 addSequenceFeature(new SequenceFeature(sf[i]));
195 setDatasetSequence(seq.getDatasetSequence());
196 if (datasetSequence == null && seq.getDBRef() != null)
198 // only copy DBRefs if we really are a dataset sequence
199 DBRefEntry[] dbr = seq.getDBRef();
200 for (int i = 0; i < dbr.length; i++)
202 addDBRef(new DBRefEntry(dbr[i]));
205 if (seq.getAnnotation() != null)
207 AlignmentAnnotation[] sqann = seq.getAnnotation();
208 for (int i = 0; i < sqann.length; i++)
210 if (sqann[i] == null)
214 boolean found = (alAnnotation == null);
217 for (int apos = 0; !found && apos < alAnnotation.length; apos++)
219 found = (alAnnotation[apos] == sqann[i]);
224 // only copy the given annotation
225 AlignmentAnnotation newann = new AlignmentAnnotation(sqann[i]);
226 addAlignmentAnnotation(newann);
230 if (seq.getPDBId() != null)
232 Vector ids = seq.getPDBId();
233 Enumeration e = ids.elements();
234 while (e.hasMoreElements())
236 this.addPDBId(new PDBEntry((PDBEntry) e.nextElement()));
247 public void setSequenceFeatures(SequenceFeature[] features)
249 sequenceFeatures = features;
252 public synchronized void addSequenceFeature(SequenceFeature sf)
254 if (sequenceFeatures == null)
256 sequenceFeatures = new SequenceFeature[0];
259 for (int i = 0; i < sequenceFeatures.length; i++)
261 if (sequenceFeatures[i].equals(sf))
267 SequenceFeature[] temp = new SequenceFeature[sequenceFeatures.length + 1];
268 System.arraycopy(sequenceFeatures, 0, temp, 0, sequenceFeatures.length);
269 temp[sequenceFeatures.length] = sf;
271 sequenceFeatures = temp;
274 public void deleteFeature(SequenceFeature sf)
276 if (sequenceFeatures == null)
282 for (index = 0; index < sequenceFeatures.length; index++)
284 if (sequenceFeatures[index].equals(sf))
290 if (index == sequenceFeatures.length)
295 int sfLength = sequenceFeatures.length;
298 sequenceFeatures = null;
302 SequenceFeature[] temp = new SequenceFeature[sfLength - 1];
303 System.arraycopy(sequenceFeatures, 0, temp, 0, index);
305 if (index < sfLength)
307 System.arraycopy(sequenceFeatures, index + 1, temp, index,
308 sequenceFeatures.length - index - 1);
311 sequenceFeatures = temp;
318 * @return DOCUMENT ME!
320 public SequenceFeature[] getSequenceFeatures()
322 return sequenceFeatures;
325 public void addPDBId(PDBEntry entry)
329 pdbIds = new Vector();
331 if (!pdbIds.contains(entry))
333 pdbIds.addElement(entry);
343 public void setPDBId(Vector id)
351 * @return DOCUMENT ME!
353 public Vector getPDBId()
361 * @return DOCUMENT ME!
363 public String getDisplayId(boolean jvsuffix)
365 StringBuffer result = new StringBuffer(name);
368 result.append("/" + start + "-" + end);
371 return result.toString();
380 public void setName(String name)
389 * @return DOCUMENT ME!
391 public String getName()
402 public void setStart(int start)
410 * @return DOCUMENT ME!
412 public int getStart()
423 public void setEnd(int end)
431 * @return DOCUMENT ME!
441 * @return DOCUMENT ME!
443 public int getLength()
445 return this.sequence.length;
454 public void setSequence(String seq)
456 this.sequence = seq.toCharArray();
460 public String getSequenceAsString()
462 return new String(sequence);
465 public String getSequenceAsString(int start, int end)
467 return new String(getSequence(start, end));
470 public char[] getSequence()
478 * @see jalview.datamodel.SequenceI#getSequence(int, int)
480 public char[] getSequence(int start, int end)
484 // JBPNote - left to user to pad the result here (TODO:Decide on this
486 if (start >= sequence.length)
491 if (end >= sequence.length)
493 end = sequence.length;
496 char[] reply = new char[end - start];
497 System.arraycopy(sequence, start, reply, 0, end - start);
503 * make a new Sequence object from start to end (including gaps) over this
512 public SequenceI getSubSequence(int start, int end)
518 char[] seq = getSequence(start, end);
523 int nstart = findPosition(start);
524 int nend = findPosition(end) - 1;
525 // JBPNote - this is an incomplete copy.
526 SequenceI nseq = new Sequence(this.getName(), seq, nstart, nend);
527 nseq.setDescription(description);
528 if (datasetSequence != null)
530 nseq.setDatasetSequence(datasetSequence);
534 nseq.setDatasetSequence(this);
545 * @return DOCUMENT ME!
547 public char getCharAt(int i)
549 if (i < sequence.length)
565 public void setDescription(String desc)
567 this.description = desc;
573 * @return DOCUMENT ME!
575 public String getDescription()
577 return this.description;
581 * Return the alignment position for a sequence position
584 * lying from start to end
586 * @return aligned position of residue pos
588 public int findIndex(int pos)
590 // returns the alignment position for a residue
593 // Rely on end being at least as long as the length of the sequence.
594 while ((i < sequence.length) && (j <= end) && (j <= pos))
596 if (!jalview.util.Comparison.isGap(sequence[i]))
604 if ((j == end) && (j < pos))
615 * Returns the sequence position for an alignment position
618 * column index in alignment (from 1)
620 * @return residue number for residue (left of and) nearest ith column
622 public int findPosition(int i)
626 int seqlen = sequence.length;
627 while ((j < i) && (j < seqlen))
629 if (!jalview.util.Comparison.isGap(sequence[j]))
641 * Returns an int array where indices correspond to each residue in the
642 * sequence and the element value gives its position in the alignment
644 * @return int[SequenceI.getEnd()-SequenceI.getStart()+1] or null if no
645 * residues in SequenceI object
647 public int[] gapMap()
649 String seq = jalview.analysis.AlignSeq.extractGaps(
650 jalview.util.Comparison.GapChars, new String(sequence));
651 int[] map = new int[seq.length()];
655 while (j < sequence.length)
657 if (!jalview.util.Comparison.isGap(sequence[j]))
671 * @see jalview.datamodel.SequenceI#findPositionMap()
673 public int[] findPositionMap()
675 int map[] = new int[sequence.length];
678 int seqlen = sequence.length;
682 if (!jalview.util.Comparison.isGap(sequence[j]))
695 * @see jalview.datamodel.SequenceI#deleteChars(int, int)
697 public void deleteChars(int i, int j)
699 int newstart = start, newend = end;
700 if (i >= sequence.length)
707 if (j >= sequence.length)
710 System.arraycopy(sequence, 0, tmp, 0, i);
714 tmp = new char[sequence.length - j + i];
715 System.arraycopy(sequence, 0, tmp, 0, i);
716 System.arraycopy(sequence, j, tmp, i, sequence.length - j);
718 boolean createNewDs = false;
719 for (int s = i; s < j; s++)
721 if (jalview.schemes.ResidueProperties.aaIndex[sequence[s]] != 23)
729 int sindex = findIndex(start) - 1;
732 // delete characters including start of sequence
733 newstart = findPosition(j);
734 break; // don't need to search for any more residue characters.
738 // delete characters after start.
739 int eindex = findIndex(end) - 1;
742 // delete characters at end of sequence
743 newend = findPosition(i - 1);
744 break; // don't need to search for any more residue characters.
749 newend--; // decrease end position by one for the deleted residue
750 // and search further
756 // deletion occured in the middle of the sequence
757 if (createNewDs && this.datasetSequence != null)
759 // construct a new sequence
760 Sequence ds = new Sequence(datasetSequence);
761 // TODO: remove any non-inheritable properties ?
762 // TODO: create a sequence mapping (since there is a relation here ?)
763 ds.deleteChars(i, j);
764 datasetSequence = ds;
781 public void insertCharAt(int i, int length, char c)
783 char[] tmp = new char[sequence.length + length];
785 if (i >= sequence.length)
787 System.arraycopy(sequence, 0, tmp, 0, sequence.length);
792 System.arraycopy(sequence, 0, tmp, 0, i);
802 if (i < sequence.length)
804 System.arraycopy(sequence, i, tmp, index, sequence.length - i);
810 public void insertCharAt(int i, char c)
812 insertCharAt(i, 1, c);
815 public String getVamsasId()
820 public void setVamsasId(String id)
825 public void setDBRef(DBRefEntry[] dbref)
830 public DBRefEntry[] getDBRef()
832 if (dbrefs == null && datasetSequence != null
833 && this != datasetSequence)
835 return datasetSequence.getDBRef();
840 public void addDBRef(DBRefEntry entry)
844 dbrefs = new DBRefEntry[0];
847 int i, iSize = dbrefs.length;
849 for (i = 0; i < iSize; i++)
851 if (dbrefs[i].equalRef(entry))
853 if (entry.getMap() != null)
855 if (dbrefs[i].getMap() == null)
857 // overwrite with 'superior' entry that contains a mapping.
865 DBRefEntry[] temp = new DBRefEntry[iSize + 1];
866 System.arraycopy(dbrefs, 0, temp, 0, iSize);
867 temp[temp.length - 1] = entry;
872 public void setDatasetSequence(SequenceI seq)
874 datasetSequence = seq;
877 public SequenceI getDatasetSequence()
879 return datasetSequence;
882 public AlignmentAnnotation[] getAnnotation()
884 if (annotation == null)
889 AlignmentAnnotation[] ret = new AlignmentAnnotation[annotation.size()];
890 for (int r = 0; r < ret.length; r++)
892 ret[r] = (AlignmentAnnotation) annotation.elementAt(r);
898 public void addAlignmentAnnotation(AlignmentAnnotation annotation)
900 if (this.annotation == null)
902 this.annotation = new Vector();
904 if (!this.annotation.contains(annotation))
906 this.annotation.addElement(annotation);
908 annotation.setSequenceRef(this);
911 public void removeAlignmentAnnotation(AlignmentAnnotation annotation)
913 if (this.annotation != null)
915 this.annotation.removeElement(annotation);
916 if (this.annotation.size() == 0)
917 this.annotation = null;
922 * test if this is a valid candidate for another sequence's dataset sequence.
925 private boolean isValidDatasetSequence()
927 if (datasetSequence != null)
931 for (int i = 0; i < sequence.length; i++)
933 if (jalview.util.Comparison.isGap(sequence[i]))
944 * @see jalview.datamodel.SequenceI#deriveSequence()
946 public SequenceI deriveSequence()
948 SequenceI seq = new Sequence(this);
949 if (datasetSequence != null)
951 // duplicate current sequence with same dataset
952 seq.setDatasetSequence(datasetSequence);
956 if (isValidDatasetSequence())
958 // Use this as dataset sequence
959 seq.setDatasetSequence(this);
963 // Create a new, valid dataset sequence
965 ds.setSequence(AlignSeq.extractGaps(
966 jalview.util.Comparison.GapChars, new String(sequence)));
967 setDatasetSequence(ds);
968 ds.setSequenceFeatures(getSequenceFeatures());
969 seq = this; // and return this sequence as the derived sequence.
978 * @see jalview.datamodel.SequenceI#createDatasetSequence()
980 public SequenceI createDatasetSequence()
982 if (datasetSequence == null)
984 datasetSequence = new Sequence(getName(), AlignSeq.extractGaps(
985 jalview.util.Comparison.GapChars, getSequenceAsString()),
986 getStart(), getEnd());
987 datasetSequence.setSequenceFeatures(getSequenceFeatures());
988 datasetSequence.setDescription(getDescription());
989 setSequenceFeatures(null);
990 // move database references onto dataset sequence
991 datasetSequence.setDBRef(getDBRef());
993 datasetSequence.setPDBId(getPDBId());
995 datasetSequence.updatePDBIds();
997 return datasetSequence;
1004 * jalview.datamodel.SequenceI#setAlignmentAnnotation(AlignmmentAnnotation[]
1007 public void setAlignmentAnnotation(AlignmentAnnotation[] annotations)
1009 if (annotation != null)
1011 annotation.removeAllElements();
1013 if (annotations != null)
1015 for (int i = 0; i < annotations.length; i++)
1017 if (annotations[i] != null)
1018 addAlignmentAnnotation(annotations[i]);
1026 * @see jalview.datamodel.SequenceI#getAnnotation(java.lang.String)
1028 public AlignmentAnnotation[] getAnnotation(String label)
1030 if (annotation == null || annotation.size() == 0)
1035 Vector subset = new Vector();
1036 Enumeration e = annotation.elements();
1037 while (e.hasMoreElements())
1039 AlignmentAnnotation ann = (AlignmentAnnotation) e.nextElement();
1040 if (ann.label != null && ann.label.equals(label))
1042 subset.addElement(ann);
1045 if (subset.size() == 0)
1049 AlignmentAnnotation[] anns = new AlignmentAnnotation[subset.size()];
1051 e = subset.elements();
1052 while (e.hasMoreElements())
1054 anns[i++] = (AlignmentAnnotation) e.nextElement();
1056 subset.removeAllElements();
1060 public boolean updatePDBIds()
1062 if (datasetSequence != null)
1064 // TODO: could merge DBRefs
1065 return datasetSequence.updatePDBIds();
1067 if (dbrefs == null || dbrefs.length == 0)
1071 Vector newpdb = new Vector();
1072 for (int i = 0; i < dbrefs.length; i++)
1074 if (DBRefSource.PDB.equals(dbrefs[i].getSource()))
1076 PDBEntry pdbe = new PDBEntry();
1077 pdbe.setId(dbrefs[i].getAccessionId());
1078 if (pdbIds == null || pdbIds.size() == 0)
1080 newpdb.addElement(pdbe);
1084 Enumeration en = pdbIds.elements();
1085 boolean matched = false;
1086 while (!matched && en.hasMoreElements())
1088 PDBEntry anentry = (PDBEntry) en.nextElement();
1089 if (anentry.getId().equals(pdbe.getId()))
1096 newpdb.addElement(pdbe);
1101 if (newpdb.size() > 0)
1103 Enumeration en = newpdb.elements();
1104 while (en.hasMoreElements())
1106 addPDBId((PDBEntry) en.nextElement());
1117 * jalview.datamodel.SequenceI#transferAnnotation(jalview.datamodel.SequenceI,
1118 * jalview.datamodel.Mapping)
1120 public void transferAnnotation(SequenceI entry, Mapping mp)
1122 if (datasetSequence != null)
1124 datasetSequence.transferAnnotation(entry, mp);
1127 if (entry.getDatasetSequence() != null)
1129 transferAnnotation(entry.getDatasetSequence(), mp);
1132 // transfer any new features from entry onto sequence
1133 if (entry.getSequenceFeatures() != null)
1136 SequenceFeature[] sfs = entry.getSequenceFeatures();
1137 for (int si = 0; si < sfs.length; si++)
1139 SequenceFeature sf[] = (mp != null) ? mp.locateFeature(sfs[si])
1140 : new SequenceFeature[]
1141 { new SequenceFeature(sfs[si]) };
1142 if (sf != null && sf.length > 0)
1144 for (int sfi = 0; sfi < sf.length; sfi++)
1146 addSequenceFeature(sf[sfi]);
1152 // transfer PDB entries
1153 if (entry.getPDBId() != null)
1155 Enumeration e = entry.getPDBId().elements();
1156 while (e.hasMoreElements())
1158 PDBEntry pdb = (PDBEntry) e.nextElement();
1162 // transfer database references
1163 DBRefEntry[] entryRefs = entry.getDBRef();
1164 if (entryRefs != null)
1166 for (int r = 0; r < entryRefs.length; r++)
1168 DBRefEntry newref = new DBRefEntry(entryRefs[r]);
1169 if (newref.getMap() != null && mp != null)
1171 // remap ref using our local mapping
1173 // we also assume all version string setting is done by dbSourceProxy
1175 * if (!newref.getSource().equalsIgnoreCase(dbSource)) {
1176 * newref.setSource(dbSource); }
1184 * @return The index (zero-based) on this sequence in the MSA.
1185 * It returns {@code -1} if this information is not available.
1187 public int getIndex() { return index; }
1190 * Defines the position of this sequence in the MSA.
1191 * Use the value {@code -1} if this information is undefined.
1193 * @param The position for this sequence. This value is zero-based (zero for this first sequence)
1195 public void setIndex(int value) { index = value; }