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;
22 import jalview.analysis.*;
26 * Implements the SequenceI interface for a char[] based sequence object.
31 public class Sequence implements SequenceI
33 SequenceI datasetSequence;
37 private char[] sequence;
52 * This annotation is displayed below the alignment but the positions are tied
53 * to the residues of this sequence
57 /** array of seuqence features - may not be null for a valid sequence object */
58 public SequenceFeature[] sequenceFeatures;
61 * Creates a new Sequence object.
66 * string to form a possibly gapped sequence out of
68 * first position of non-gap residue in the sequence
70 * last position of ungapped residues (nearly always only used for
73 public Sequence(String name, String sequence, int start, int end)
76 this.sequence = sequence.toCharArray();
83 public Sequence(String name, char[] sequence, int start, int end)
86 this.sequence = sequence;
93 com.stevesoft.pat.Regex limitrx = new com.stevesoft.pat.Regex(
94 "[/][0-9]{1,}[-][0-9]{1,}$");
96 com.stevesoft.pat.Regex endrx = new com.stevesoft.pat.Regex("[0-9]{1,}$");
103 .println("POSSIBLE IMPLEMENTATION ERROR: null sequence name passed to constructor.");
106 // Does sequence have the /start-end signiature?
107 if (limitrx.search(name))
109 name = limitrx.left();
110 endrx.search(limitrx.stringMatched());
111 setStart(Integer.parseInt(limitrx.stringMatched().substring(1,
112 endrx.matchedFrom() - 1)));
113 setEnd(Integer.parseInt(endrx.stringMatched()));
117 void checkValidRange()
119 // Note: JAL-774 : http://issues.jalview.org/browse/JAL-774?focusedCommentId=11239&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-11239
122 for (int j = 0; j < sequence.length; j++)
124 if (!jalview.util.Comparison.isGap(sequence[j]))
142 * Creates a new Sequence object.
149 public Sequence(String name, String sequence)
151 this(name, sequence, 1, -1);
155 * Creates a new Sequence object with new features, DBRefEntries,
156 * AlignmentAnnotations, and PDBIds but inherits any existing dataset sequence
162 public Sequence(SequenceI seq)
164 this(seq, seq.getAnnotation());
168 * Create a new sequence object with new features, DBRefEntries, and PDBIds
169 * but inherits any existing dataset sequence reference, and duplicate of any
170 * annotation that is present in the given annotation array.
173 * the sequence to be copied
174 * @param alAnnotation
175 * an array of annotation including some associated with seq
177 public Sequence(SequenceI seq, AlignmentAnnotation[] alAnnotation)
179 this(seq.getName(), seq.getSequence(), seq.getStart(), seq.getEnd());
180 description = seq.getDescription();
181 if (seq.getSequenceFeatures() != null)
183 SequenceFeature[] sf = seq.getSequenceFeatures();
184 for (int i = 0; i < sf.length; i++)
186 addSequenceFeature(new SequenceFeature(sf[i]));
189 setDatasetSequence(seq.getDatasetSequence());
190 if (datasetSequence == null && seq.getDBRef() != null)
192 // only copy DBRefs if we really are a dataset sequence
193 DBRefEntry[] dbr = seq.getDBRef();
194 for (int i = 0; i < dbr.length; i++)
196 addDBRef(new DBRefEntry(dbr[i]));
199 if (seq.getAnnotation() != null)
201 AlignmentAnnotation[] sqann = seq.getAnnotation();
202 for (int i = 0; i < sqann.length; i++)
204 if (sqann[i] == null)
208 boolean found = (alAnnotation == null);
211 for (int apos = 0; !found && apos < alAnnotation.length; apos++)
213 found = (alAnnotation[apos] == sqann[i]);
218 // only copy the given annotation
219 AlignmentAnnotation newann = new AlignmentAnnotation(sqann[i]);
220 addAlignmentAnnotation(newann);
224 if (seq.getPDBId() != null)
226 Vector ids = seq.getPDBId();
227 Enumeration e = ids.elements();
228 while (e.hasMoreElements())
230 this.addPDBId(new PDBEntry((PDBEntry) e.nextElement()));
241 public void setSequenceFeatures(SequenceFeature[] features)
243 sequenceFeatures = features;
246 public synchronized void addSequenceFeature(SequenceFeature sf)
248 if (sequenceFeatures == null)
250 sequenceFeatures = new SequenceFeature[0];
253 for (int i = 0; i < sequenceFeatures.length; i++)
255 if (sequenceFeatures[i].equals(sf))
261 SequenceFeature[] temp = new SequenceFeature[sequenceFeatures.length + 1];
262 System.arraycopy(sequenceFeatures, 0, temp, 0, sequenceFeatures.length);
263 temp[sequenceFeatures.length] = sf;
265 sequenceFeatures = temp;
268 public void deleteFeature(SequenceFeature sf)
270 if (sequenceFeatures == null)
276 for (index = 0; index < sequenceFeatures.length; index++)
278 if (sequenceFeatures[index].equals(sf))
284 if (index == sequenceFeatures.length)
289 int sfLength = sequenceFeatures.length;
292 sequenceFeatures = null;
296 SequenceFeature[] temp = new SequenceFeature[sfLength - 1];
297 System.arraycopy(sequenceFeatures, 0, temp, 0, index);
299 if (index < sfLength)
301 System.arraycopy(sequenceFeatures, index + 1, temp, index,
302 sequenceFeatures.length - index - 1);
305 sequenceFeatures = temp;
312 * @return DOCUMENT ME!
314 public SequenceFeature[] getSequenceFeatures()
316 return sequenceFeatures;
319 public void addPDBId(PDBEntry entry)
323 pdbIds = new Vector();
325 if (!pdbIds.contains(entry))
327 pdbIds.addElement(entry);
337 public void setPDBId(Vector id)
345 * @return DOCUMENT ME!
347 public Vector getPDBId()
355 * @return DOCUMENT ME!
357 public String getDisplayId(boolean jvsuffix)
359 StringBuffer result = new StringBuffer(name);
362 result.append("/" + start + "-" + end);
365 return result.toString();
374 public void setName(String name)
383 * @return DOCUMENT ME!
385 public String getName()
396 public void setStart(int start)
404 * @return DOCUMENT ME!
406 public int getStart()
417 public void setEnd(int end)
425 * @return DOCUMENT ME!
435 * @return DOCUMENT ME!
437 public int getLength()
439 return this.sequence.length;
448 public void setSequence(String seq)
450 this.sequence = seq.toCharArray();
454 public String getSequenceAsString()
456 return new String(sequence);
459 public String getSequenceAsString(int start, int end)
461 return new String(getSequence(start, end));
464 public char[] getSequence()
472 * @see jalview.datamodel.SequenceI#getSequence(int, int)
474 public char[] getSequence(int start, int end)
478 // JBPNote - left to user to pad the result here (TODO:Decide on this
480 if (start >= sequence.length)
485 if (end >= sequence.length)
487 end = sequence.length;
490 char[] reply = new char[end - start];
491 System.arraycopy(sequence, start, reply, 0, end - start);
497 * make a new Sequence object from start to end (including gaps) over this
506 public SequenceI getSubSequence(int start, int end)
512 char[] seq = getSequence(start, end);
517 int nstart = findPosition(start);
518 int nend = findPosition(end) - 1;
519 // JBPNote - this is an incomplete copy.
520 SequenceI nseq = new Sequence(this.getName(), seq, nstart, nend);
521 nseq.setDescription(description);
522 if (datasetSequence != null)
524 nseq.setDatasetSequence(datasetSequence);
528 nseq.setDatasetSequence(this);
539 * @return DOCUMENT ME!
541 public char getCharAt(int i)
543 if (i < sequence.length)
559 public void setDescription(String desc)
561 this.description = desc;
567 * @return DOCUMENT ME!
569 public String getDescription()
571 return this.description;
575 * Return the alignment position for a sequence position
578 * lying from start to end
580 * @return aligned position of residue pos
582 public int findIndex(int pos)
584 // returns the alignment position for a residue
587 // Rely on end being at least as long as the length of the sequence.
588 while ((i < sequence.length) && (j <= end) && (j <= pos))
590 if (!jalview.util.Comparison.isGap(sequence[i]))
598 if ((j == end) && (j < pos))
609 * Returns the sequence position for an alignment position
612 * column index in alignment (from 1)
614 * @return residue number for residue (left of and) nearest ith column
616 public int findPosition(int i)
620 int seqlen = sequence.length;
621 while ((j < i) && (j < seqlen))
623 if (!jalview.util.Comparison.isGap(sequence[j]))
635 * Returns an int array where indices correspond to each residue in the
636 * sequence and the element value gives its position in the alignment
638 * @return int[SequenceI.getEnd()-SequenceI.getStart()+1] or null if no
639 * residues in SequenceI object
641 public int[] gapMap()
643 String seq = jalview.analysis.AlignSeq.extractGaps(
644 jalview.util.Comparison.GapChars, new String(sequence));
645 int[] map = new int[seq.length()];
649 while (j < sequence.length)
651 if (!jalview.util.Comparison.isGap(sequence[j]))
665 * @see jalview.datamodel.SequenceI#findPositionMap()
667 public int[] findPositionMap()
669 int map[] = new int[sequence.length];
672 int seqlen = sequence.length;
676 if (!jalview.util.Comparison.isGap(sequence[j]))
689 * @see jalview.datamodel.SequenceI#deleteChars(int, int)
691 public void deleteChars(int i, int j)
693 int newstart = start, newend = end;
694 if (i >= sequence.length)
701 if (j >= sequence.length)
704 System.arraycopy(sequence, 0, tmp, 0, i);
708 tmp = new char[sequence.length - j + i];
709 System.arraycopy(sequence, 0, tmp, 0, i);
710 System.arraycopy(sequence, j, tmp, i, sequence.length - j);
712 boolean createNewDs = false;
713 for (int s = i; s < j; s++)
715 if (jalview.schemes.ResidueProperties.aaIndex[sequence[s]] != 23)
723 int sindex = findIndex(start) - 1;
726 // delete characters including start of sequence
727 newstart = findPosition(j);
728 break; // don't need to search for any more residue characters.
732 // delete characters after start.
733 int eindex = findIndex(end) - 1;
736 // delete characters at end of sequence
737 newend = findPosition(i - 1);
738 break; // don't need to search for any more residue characters.
743 newend--; // decrease end position by one for the deleted residue
744 // and search further
750 // deletion occured in the middle of the sequence
751 if (createNewDs && this.datasetSequence != null)
753 // construct a new sequence
754 Sequence ds = new Sequence(datasetSequence);
755 // TODO: remove any non-inheritable properties ?
756 // TODO: create a sequence mapping (since there is a relation here ?)
757 ds.deleteChars(i, j);
758 datasetSequence = ds;
775 public void insertCharAt(int i, int length, char c)
777 char[] tmp = new char[sequence.length + length];
779 if (i >= sequence.length)
781 System.arraycopy(sequence, 0, tmp, 0, sequence.length);
786 System.arraycopy(sequence, 0, tmp, 0, i);
796 if (i < sequence.length)
798 System.arraycopy(sequence, i, tmp, index, sequence.length - i);
804 public void insertCharAt(int i, char c)
806 insertCharAt(i, 1, c);
809 public String getVamsasId()
814 public void setVamsasId(String id)
819 public void setDBRef(DBRefEntry[] dbref)
824 public DBRefEntry[] getDBRef()
826 if (dbrefs == null && datasetSequence != null
827 && this != datasetSequence)
829 return datasetSequence.getDBRef();
834 public void addDBRef(DBRefEntry entry)
838 dbrefs = new DBRefEntry[0];
841 int i, iSize = dbrefs.length;
843 for (i = 0; i < iSize; i++)
845 if (dbrefs[i].equalRef(entry))
847 if (entry.getMap() != null)
849 if (dbrefs[i].getMap() == null)
851 // overwrite with 'superior' entry that contains a mapping.
859 DBRefEntry[] temp = new DBRefEntry[iSize + 1];
860 System.arraycopy(dbrefs, 0, temp, 0, iSize);
861 temp[temp.length - 1] = entry;
866 public void setDatasetSequence(SequenceI seq)
868 datasetSequence = seq;
871 public SequenceI getDatasetSequence()
873 return datasetSequence;
876 public AlignmentAnnotation[] getAnnotation()
878 if (annotation == null)
883 AlignmentAnnotation[] ret = new AlignmentAnnotation[annotation.size()];
884 for (int r = 0; r < ret.length; r++)
886 ret[r] = (AlignmentAnnotation) annotation.elementAt(r);
892 public void addAlignmentAnnotation(AlignmentAnnotation annotation)
894 if (this.annotation == null)
896 this.annotation = new Vector();
899 this.annotation.addElement(annotation);
900 annotation.setSequenceRef(this);
903 public void removeAlignmentAnnotation(AlignmentAnnotation annotation)
905 if (this.annotation != null)
907 this.annotation.removeElement(annotation);
908 if (this.annotation.size() == 0)
909 this.annotation = null;
914 * test if this is a valid candidate for another sequence's dataset sequence.
917 private boolean isValidDatasetSequence()
919 if (datasetSequence != null)
923 for (int i = 0; i < sequence.length; i++)
925 if (jalview.util.Comparison.isGap(sequence[i]))
936 * @see jalview.datamodel.SequenceI#deriveSequence()
938 public SequenceI deriveSequence()
940 SequenceI seq = new Sequence(this);
941 if (datasetSequence != null)
943 // duplicate current sequence with same dataset
944 seq.setDatasetSequence(datasetSequence);
948 if (isValidDatasetSequence())
950 // Use this as dataset sequence
951 seq.setDatasetSequence(this);
955 // Create a new, valid dataset sequence
957 ds.setSequence(AlignSeq.extractGaps(
958 jalview.util.Comparison.GapChars, new String(sequence)));
959 setDatasetSequence(ds);
960 ds.setSequenceFeatures(getSequenceFeatures());
961 seq = this; // and return this sequence as the derived sequence.
970 * @see jalview.datamodel.SequenceI#createDatasetSequence()
972 public SequenceI createDatasetSequence()
974 if (datasetSequence == null)
976 datasetSequence = new Sequence(getName(), AlignSeq.extractGaps(
977 jalview.util.Comparison.GapChars, getSequenceAsString()),
978 getStart(), getEnd());
979 datasetSequence.setSequenceFeatures(getSequenceFeatures());
980 datasetSequence.setDescription(getDescription());
981 setSequenceFeatures(null);
982 // move database references onto dataset sequence
983 datasetSequence.setDBRef(getDBRef());
985 datasetSequence.setPDBId(getPDBId());
987 datasetSequence.updatePDBIds();
989 return datasetSequence;
996 * jalview.datamodel.SequenceI#setAlignmentAnnotation(AlignmmentAnnotation[]
999 public void setAlignmentAnnotation(AlignmentAnnotation[] annotations)
1001 if (annotation != null)
1003 annotation.removeAllElements();
1005 if (annotations != null)
1007 for (int i = 0; i < annotations.length; i++)
1009 if (annotations[i] != null)
1010 addAlignmentAnnotation(annotations[i]);
1018 * @see jalview.datamodel.SequenceI#getAnnotation(java.lang.String)
1020 public AlignmentAnnotation[] getAnnotation(String label)
1022 if (annotation == null || annotation.size() == 0)
1027 Vector subset = new Vector();
1028 Enumeration e = annotation.elements();
1029 while (e.hasMoreElements())
1031 AlignmentAnnotation ann = (AlignmentAnnotation) e.nextElement();
1032 if (ann.label != null && ann.label.equals(label))
1034 subset.addElement(ann);
1037 if (subset.size() == 0)
1041 AlignmentAnnotation[] anns = new AlignmentAnnotation[subset.size()];
1043 e = subset.elements();
1044 while (e.hasMoreElements())
1046 anns[i++] = (AlignmentAnnotation) e.nextElement();
1048 subset.removeAllElements();
1052 public boolean updatePDBIds()
1054 if (datasetSequence != null)
1056 // TODO: could merge DBRefs
1057 return datasetSequence.updatePDBIds();
1059 if (dbrefs == null || dbrefs.length == 0)
1063 Vector newpdb = new Vector();
1064 for (int i = 0; i < dbrefs.length; i++)
1066 if (DBRefSource.PDB.equals(dbrefs[i].getSource()))
1068 PDBEntry pdbe = new PDBEntry();
1069 pdbe.setId(dbrefs[i].getAccessionId());
1070 if (pdbIds == null || pdbIds.size() == 0)
1072 newpdb.addElement(pdbe);
1076 Enumeration en = pdbIds.elements();
1077 boolean matched = false;
1078 while (!matched && en.hasMoreElements())
1080 PDBEntry anentry = (PDBEntry) en.nextElement();
1081 if (anentry.getId().equals(pdbe.getId()))
1088 newpdb.addElement(pdbe);
1093 if (newpdb.size() > 0)
1095 Enumeration en = newpdb.elements();
1096 while (en.hasMoreElements())
1098 addPDBId((PDBEntry) en.nextElement());
1109 * jalview.datamodel.SequenceI#transferAnnotation(jalview.datamodel.SequenceI,
1110 * jalview.datamodel.Mapping)
1112 public void transferAnnotation(SequenceI entry, Mapping mp)
1114 if (datasetSequence != null)
1116 datasetSequence.transferAnnotation(entry, mp);
1119 if (entry.getDatasetSequence() != null)
1121 transferAnnotation(entry.getDatasetSequence(), mp);
1124 // transfer any new features from entry onto sequence
1125 if (entry.getSequenceFeatures() != null)
1128 SequenceFeature[] sfs = entry.getSequenceFeatures();
1129 for (int si = 0; si < sfs.length; si++)
1131 SequenceFeature sf[] = (mp != null) ? mp.locateFeature(sfs[si])
1132 : new SequenceFeature[]
1133 { new SequenceFeature(sfs[si]) };
1134 if (sf != null && sf.length > 0)
1136 for (int sfi = 0; sfi < sf.length; sfi++)
1138 addSequenceFeature(sf[sfi]);
1144 // transfer PDB entries
1145 if (entry.getPDBId() != null)
1147 Enumeration e = entry.getPDBId().elements();
1148 while (e.hasMoreElements())
1150 PDBEntry pdb = (PDBEntry) e.nextElement();
1154 // transfer database references
1155 DBRefEntry[] entryRefs = entry.getDBRef();
1156 if (entryRefs != null)
1158 for (int r = 0; r < entryRefs.length; r++)
1160 DBRefEntry newref = new DBRefEntry(entryRefs[r]);
1161 if (newref.getMap() != null && mp != null)
1163 // remap ref using our local mapping
1165 // we also assume all version string setting is done by dbSourceProxy
1167 * if (!newref.getSource().equalsIgnoreCase(dbSource)) {
1168 * newref.setSource(dbSource); }