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.AlignmentUtils;
24 import jalview.util.MessageManager;
26 import java.util.ArrayList;
27 import java.util.Enumeration;
28 import java.util.Hashtable;
29 import java.util.List;
31 import java.util.Vector;
34 * Data structure to hold and manipulate a multiple sequence alignment
40 public class Alignment implements AlignmentI
42 protected Alignment dataset;
44 protected List<SequenceI> sequences;
46 protected List<SequenceGroup> groups = java.util.Collections
47 .synchronizedList(new ArrayList<SequenceGroup>());
49 protected char gapCharacter = '-';
51 protected int type = NUCLEOTIDE;
53 public static final int PROTEIN = 0;
55 public static final int NUCLEOTIDE = 1;
57 public boolean hasRNAStructure = false;
60 public AlignmentAnnotation[] annotations;
62 HiddenSequences hiddenSequences = new HiddenSequences(this);
64 public Hashtable alignmentProperties;
66 private void initAlignment(SequenceI[] seqs)
70 if (jalview.util.Comparison.isNucleotide(seqs))
79 sequences = java.util.Collections
80 .synchronizedList(new ArrayList<SequenceI>());
82 for (i = 0; i < seqs.length; i++)
84 sequences.add(seqs[i]);
90 * Make an alignment from an array of Sequences.
94 public Alignment(SequenceI[] seqs)
100 * Make a new alignment from an array of SeqCigars
105 public Alignment(SeqCigar[] alseqs)
107 SequenceI[] seqs = SeqCigar.createAlignmentSequences(alseqs,
108 gapCharacter, new ColumnSelection(), null);
113 * Make a new alignment from an CigarArray JBPNote - can only do this when
114 * compactAlignment does not contain hidden regions. JBPNote - must also check
115 * that compactAlignment resolves to a set of SeqCigars - or construct them
118 * @param compactAlignment
121 public static AlignmentI createAlignment(CigarArray compactAlignment)
123 throw new Error(MessageManager.getString("error.alignment_cigararray_not_implemented"));
124 // this(compactAlignment.refCigars);
130 * @return DOCUMENT ME!
133 public List<SequenceI> getSequences()
139 public List<SequenceI> getSequences(
140 Map<SequenceI, SequenceCollectionI> hiddenReps)
142 // TODO: in jalview 2.8 we don't do anything with hiddenreps - fix design to
148 public SequenceI[] getSequencesArray()
150 if (sequences == null)
154 synchronized (sequences)
156 return sequences.toArray(new SequenceI[sequences.size()]);
161 * Returns a map of lists of sequences keyed by sequence name.
166 public Map<String, List<SequenceI>> getSequencesByName()
168 return AlignmentUtils.getSequencesByName(this);
177 * @return DOCUMENT ME!
180 public SequenceI getSequenceAt(int i)
182 synchronized (sequences)
184 if (i > -1 && i < sequences.size())
186 return sequences.get(i);
193 * Adds a sequence to the alignment. Recalculates maxLength and size.
198 public void addSequence(SequenceI snew)
202 // maintain dataset integrity
203 if (snew.getDatasetSequence() != null)
205 getDataset().addSequence(snew.getDatasetSequence());
209 // derive new sequence
210 SequenceI adding = snew.deriveSequence();
211 getDataset().addSequence(adding.getDatasetSequence());
215 if (sequences == null)
217 initAlignment(new SequenceI[]
222 synchronized (sequences)
227 if (hiddenSequences != null)
229 hiddenSequences.adjustHeightSequenceAdded();
234 * Adds a sequence to the alignment. Recalculates maxLength and size.
239 public void setSequenceAt(int i, SequenceI snew)
241 SequenceI oldseq = getSequenceAt(i);
243 synchronized (sequences)
245 sequences.set(i, snew);
252 * @return DOCUMENT ME!
255 public List<SequenceGroup> getGroups()
261 public void finalize()
263 if (getDataset() != null)
265 getDataset().removeAlignmentRef();
272 hiddenSequences = null;
276 * decrement the alignmentRefs counter by one and call finalize if it goes to
279 private void removeAlignmentRef()
281 if (--alignmentRefs == 0)
294 public void deleteSequence(SequenceI s)
296 deleteSequence(findIndex(s));
306 public void deleteSequence(int i)
308 if (i > -1 && i < getHeight())
310 synchronized (sequences)
314 hiddenSequences.adjustHeightSequenceDeleted(i);
321 * @see jalview.datamodel.AlignmentI#findGroup(jalview.datamodel.SequenceI)
324 public SequenceGroup findGroup(SequenceI s)
326 synchronized (groups)
328 for (int i = 0; i < this.groups.size(); i++)
330 SequenceGroup sg = groups.get(i);
332 if (sg.getSequences(null).contains(s))
345 * jalview.datamodel.AlignmentI#findAllGroups(jalview.datamodel.SequenceI)
348 public SequenceGroup[] findAllGroups(SequenceI s)
350 ArrayList<SequenceGroup> temp = new ArrayList<SequenceGroup>();
352 synchronized (groups)
354 int gSize = groups.size();
355 for (int i = 0; i < gSize; i++)
357 SequenceGroup sg = groups.get(i);
358 if (sg == null || sg.getSequences(null) == null)
360 this.deleteGroup(sg);
365 if (sg.getSequences(null).contains(s))
371 SequenceGroup[] ret = new SequenceGroup[temp.size()];
372 return temp.toArray(ret);
377 public void addGroup(SequenceGroup sg)
379 synchronized (groups)
381 if (!groups.contains(sg))
383 if (hiddenSequences.getSize() > 0)
385 int i, iSize = sg.getSize();
386 for (i = 0; i < iSize; i++)
388 if (!sequences.contains(sg.getSequenceAt(i)))
390 sg.deleteSequence(sg.getSequenceAt(i), false);
396 if (sg.getSize() < 1)
408 * remove any annotation that references gp
411 * (if null, removes all group associated annotation)
413 private void removeAnnotationForGroup(SequenceGroup gp)
415 if (annotations == null || annotations.length == 0)
419 // remove annotation very quickly
420 AlignmentAnnotation[] t, todelete = new AlignmentAnnotation[annotations.length], tokeep = new AlignmentAnnotation[annotations.length];
424 for (i = 0, p = 0, k = 0; i < annotations.length; i++)
426 if (annotations[i].groupRef != null)
428 todelete[p++] = annotations[i];
432 tokeep[k++] = annotations[i];
438 for (i = 0, p = 0, k = 0; i < annotations.length; i++)
440 if (annotations[i].groupRef == gp)
442 todelete[p++] = annotations[i];
446 tokeep[k++] = annotations[i];
452 // clear out the group associated annotation.
453 for (i = 0; i < p; i++)
455 unhookAnnotation(todelete[i]);
458 t = new AlignmentAnnotation[k];
459 for (i = 0; i < k; i++)
468 public void deleteAllGroups()
470 synchronized (groups)
472 if (annotations != null)
474 removeAnnotationForGroup(null);
476 for (SequenceGroup sg : groups)
486 public void deleteGroup(SequenceGroup g)
488 synchronized (groups)
490 if (groups.contains(g))
492 removeAnnotationForGroup(g);
501 public SequenceI findName(String name)
503 return findName(name, false);
509 * @see jalview.datamodel.AlignmentI#findName(java.lang.String, boolean)
512 public SequenceI findName(String token, boolean b)
514 return findName(null, token, b);
520 * @see jalview.datamodel.AlignmentI#findName(SequenceI, java.lang.String,
524 public SequenceI findName(SequenceI startAfter, String token, boolean b)
529 String sqname = null;
530 if (startAfter != null)
532 // try to find the sequence in the alignment
533 boolean matched = false;
534 while (i < sequences.size())
536 if (getSequenceAt(i++) == startAfter)
547 while (i < sequences.size())
549 sq = getSequenceAt(i);
550 sqname = sq.getName();
551 if (sqname.equals(token) // exact match
552 || (b && // allow imperfect matches - case varies
553 (sqname.equalsIgnoreCase(token))))
555 return getSequenceAt(i);
565 public SequenceI[] findSequenceMatch(String name)
567 Vector matches = new Vector();
570 while (i < sequences.size())
572 if (getSequenceAt(i).getName().equals(name))
574 matches.addElement(getSequenceAt(i));
579 SequenceI[] result = new SequenceI[matches.size()];
580 for (i = 0; i < result.length; i++)
582 result[i] = (SequenceI) matches.elementAt(i);
592 * @see jalview.datamodel.AlignmentI#findIndex(jalview.datamodel.SequenceI)
595 public int findIndex(SequenceI s)
599 while (i < sequences.size())
601 if (s == getSequenceAt(i))
616 * jalview.datamodel.AlignmentI#findIndex(jalview.datamodel.SearchResults)
619 public int findIndex(SearchResults results)
623 while (i < sequences.size())
625 if (results.involvesSequence(getSequenceAt(i)))
637 * @return DOCUMENT ME!
640 public int getHeight()
642 return sequences.size();
648 * @return DOCUMENT ME!
651 public int getWidth()
655 for (int i = 0; i < sequences.size(); i++)
657 if (getSequenceAt(i).getLength() > maxLength)
659 maxLength = getSequenceAt(i).getLength();
673 public void setGapCharacter(char gc)
676 synchronized (sequences)
678 for (SequenceI seq : sequences)
680 seq.setSequence(seq.getSequenceAsString().replace('.', gc)
681 .replace('-', gc).replace(' ', gc));
689 * @return DOCUMENT ME!
692 public char getGapCharacter()
700 * @see jalview.datamodel.AlignmentI#isAligned()
703 public boolean isAligned()
705 return isAligned(false);
711 * @see jalview.datamodel.AlignmentI#isAligned(boolean)
714 public boolean isAligned(boolean includeHidden)
716 int width = getWidth();
717 if (hiddenSequences == null || hiddenSequences.getSize() == 0)
719 includeHidden = true; // no hidden sequences to check against.
721 for (int i = 0; i < sequences.size(); i++)
723 if (includeHidden || !hiddenSequences.isHidden(getSequenceAt(i)))
725 if (getSequenceAt(i).getLength() != width)
738 * @seejalview.datamodel.AlignmentI#deleteAnnotation(jalview.datamodel.
739 * AlignmentAnnotation)
742 public boolean deleteAnnotation(AlignmentAnnotation aa)
744 return deleteAnnotation(aa, true);
748 public boolean deleteAnnotation(AlignmentAnnotation aa, boolean unhook)
752 if (annotations != null)
754 aSize = annotations.length;
762 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize - 1];
764 boolean swap = false;
767 for (int i = 0; i < aSize; i++)
769 if (annotations[i] == aa)
774 if (tIndex < temp.length)
776 temp[tIndex++] = annotations[i];
785 unhookAnnotation(aa);
792 * remove any object references associated with this annotation
796 private void unhookAnnotation(AlignmentAnnotation aa)
798 if (aa.sequenceRef != null)
800 aa.sequenceRef.removeAlignmentAnnotation(aa);
802 if (aa.groupRef != null)
804 // probably need to do more here in the future (post 2.5.0)
812 * @seejalview.datamodel.AlignmentI#addAnnotation(jalview.datamodel.
813 * AlignmentAnnotation)
816 public void addAnnotation(AlignmentAnnotation aa)
818 addAnnotation(aa, -1);
824 * @seejalview.datamodel.AlignmentI#addAnnotation(jalview.datamodel.
825 * AlignmentAnnotation, int)
828 public void addAnnotation(AlignmentAnnotation aa, int pos)
830 if (aa.getRNAStruc() != null)
832 hasRNAStructure = true;
836 if (annotations != null)
838 aSize = annotations.length + 1;
841 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
843 if (pos == -1 || pos >= aSize)
845 temp[aSize - 1] = aa;
854 for (i = 0; i < (aSize - 1); i++, p++)
862 temp[p] = annotations[i];
871 public void setAnnotationIndex(AlignmentAnnotation aa, int index)
873 if (aa == null || annotations == null || annotations.length - 1 < index)
878 int aSize = annotations.length;
879 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
883 for (int i = 0; i < aSize; i++)
892 temp[i] = annotations[i];
896 temp[i] = annotations[i - 1];
905 * returns all annotation on the alignment
907 public AlignmentAnnotation[] getAlignmentAnnotation()
913 public void setNucleotide(boolean b)
926 public boolean isNucleotide()
928 if (type == NUCLEOTIDE)
939 public boolean hasRNAStructure()
941 // TODO can it happen that structure is removed from alignment?
942 return hasRNAStructure;
946 public void setDataset(Alignment data)
948 if (dataset == null && data == null)
950 // Create a new dataset for this alignment.
951 // Can only be done once, if dataset is not null
952 // This will not be performed
953 SequenceI[] seqs = new SequenceI[getHeight()];
954 SequenceI currentSeq;
955 for (int i = 0; i < getHeight(); i++)
957 currentSeq = getSequenceAt(i);
958 if (currentSeq.getDatasetSequence() != null)
960 seqs[i] = currentSeq.getDatasetSequence();
964 seqs[i] = currentSeq.createDatasetSequence();
968 dataset = new Alignment(seqs);
970 else if (dataset == null && data != null)
973 for (int i = 0; i < getHeight(); i++)
975 SequenceI currentSeq = getSequenceAt(i);
976 SequenceI dsq = currentSeq.getDatasetSequence();
979 dsq = currentSeq.createDatasetSequence();
980 dataset.addSequence(dsq);
984 while (dsq.getDatasetSequence() != null)
986 dsq = dsq.getDatasetSequence();
988 if (dataset.findIndex(dsq) == -1)
990 dataset.addSequence(dsq);
995 dataset.addAlignmentRef();
999 * reference count for number of alignments referencing this one.
1001 int alignmentRefs = 0;
1004 * increase reference count to this alignment.
1006 private void addAlignmentRef()
1012 public Alignment getDataset()
1018 public boolean padGaps()
1020 boolean modified = false;
1022 // Remove excess gaps from the end of alignment
1026 for (int i = 0; i < sequences.size(); i++)
1028 current = getSequenceAt(i);
1029 for (int j = current.getLength(); j > maxLength; j--)
1032 && !jalview.util.Comparison.isGap(current.getCharAt(j)))
1043 for (int i = 0; i < sequences.size(); i++)
1045 current = getSequenceAt(i);
1046 cLength = current.getLength();
1048 if (cLength < maxLength)
1050 current.insertCharAt(cLength, maxLength - cLength, gapCharacter);
1053 else if (current.getLength() > maxLength)
1055 current.deleteChars(maxLength, current.getLength());
1062 * Justify the sequences to the left or right by deleting and inserting gaps
1063 * before the initial residue or after the terminal residue
1066 * true if alignment padded to right, false to justify to left
1067 * @return true if alignment was changed
1070 public boolean justify(boolean right)
1072 boolean modified = false;
1074 // Remove excess gaps from the end of alignment
1076 int ends[] = new int[sequences.size() * 2];
1078 for (int i = 0; i < sequences.size(); i++)
1080 current = getSequenceAt(i);
1081 // This should really be a sequence method
1082 ends[i * 2] = current.findIndex(current.getStart());
1083 ends[i * 2 + 1] = current.findIndex(current.getStart()
1084 + current.getLength());
1085 boolean hitres = false;
1086 for (int j = 0, rs = 0, ssiz = current.getLength(); j < ssiz; j++)
1088 if (!jalview.util.Comparison.isGap(current.getCharAt(j)))
1097 ends[i * 2 + 1] = j;
1098 if (j - ends[i * 2] > maxLength)
1100 maxLength = j - ends[i * 2];
1108 // now edit the flanking gaps to justify to either left or right
1109 int cLength, extent, diff;
1110 for (int i = 0; i < sequences.size(); i++)
1112 current = getSequenceAt(i);
1114 cLength = 1 + ends[i * 2 + 1] - ends[i * 2];
1115 diff = maxLength - cLength; // number of gaps to indent
1116 extent = current.getLength();
1120 if (extent > ends[i * 2 + 1])
1122 current.deleteChars(ends[i * 2 + 1] + 1, extent);
1125 if (ends[i * 2] > diff)
1127 current.deleteChars(0, ends[i * 2] - diff);
1132 if (ends[i * 2] < diff)
1134 current.insertCharAt(0, diff - ends[i * 2], gapCharacter);
1142 if (ends[i * 2] > 0)
1144 current.deleteChars(0, ends[i * 2]);
1146 ends[i * 2 + 1] -= ends[i * 2];
1147 extent -= ends[i * 2];
1149 if (extent > maxLength)
1151 current.deleteChars(maxLength + 1, extent);
1156 if (extent < maxLength)
1158 current.insertCharAt(extent, maxLength - extent, gapCharacter);
1168 public HiddenSequences getHiddenSequences()
1170 return hiddenSequences;
1174 public CigarArray getCompactAlignment()
1176 synchronized (sequences)
1178 SeqCigar alseqs[] = new SeqCigar[sequences.size()];
1180 for (SequenceI seq : sequences)
1182 alseqs[i++] = new SeqCigar(seq);
1184 CigarArray cal = new CigarArray(alseqs);
1185 cal.addOperation(CigarArray.M, getWidth());
1191 public void setProperty(Object key, Object value)
1193 if (alignmentProperties == null)
1195 alignmentProperties = new Hashtable();
1198 alignmentProperties.put(key, value);
1202 public Object getProperty(Object key)
1204 if (alignmentProperties != null)
1206 return alignmentProperties.get(key);
1215 public Hashtable getProperties()
1217 return alignmentProperties;
1220 AlignedCodonFrame[] codonFrameList = null;
1226 * jalview.datamodel.AlignmentI#addCodonFrame(jalview.datamodel.AlignedCodonFrame
1230 public void addCodonFrame(AlignedCodonFrame codons)
1236 if (codonFrameList == null)
1238 codonFrameList = new AlignedCodonFrame[]
1242 AlignedCodonFrame[] t = new AlignedCodonFrame[codonFrameList.length + 1];
1243 System.arraycopy(codonFrameList, 0, t, 0, codonFrameList.length);
1244 t[codonFrameList.length] = codons;
1251 * @see jalview.datamodel.AlignmentI#getCodonFrame(int)
1254 public AlignedCodonFrame getCodonFrame(int index)
1256 return codonFrameList[index];
1263 * jalview.datamodel.AlignmentI#getCodonFrame(jalview.datamodel.SequenceI)
1266 public AlignedCodonFrame[] getCodonFrame(SequenceI seq)
1268 if (seq == null || codonFrameList == null)
1272 Vector cframes = new Vector();
1273 for (int f = 0; f < codonFrameList.length; f++)
1275 if (codonFrameList[f].involvesSequence(seq))
1277 cframes.addElement(codonFrameList[f]);
1280 if (cframes.size() == 0)
1284 AlignedCodonFrame[] cfr = new AlignedCodonFrame[cframes.size()];
1285 cframes.copyInto(cfr);
1292 * @see jalview.datamodel.AlignmentI#getCodonFrames()
1295 public AlignedCodonFrame[] getCodonFrames()
1297 return codonFrameList;
1303 * @seejalview.datamodel.AlignmentI#removeCodonFrame(jalview.datamodel.
1304 * AlignedCodonFrame)
1307 public boolean removeCodonFrame(AlignedCodonFrame codons)
1309 if (codons == null || codonFrameList == null)
1313 boolean removed = false;
1314 int i = 0, iSize = codonFrameList.length;
1317 if (codonFrameList[i] == codons)
1322 System.arraycopy(codonFrameList, i + 1, codonFrameList, i, iSize
1336 public void append(AlignmentI toappend)
1338 if (toappend == this)
1340 System.err.println("Self append may cause a deadlock.");
1342 // TODO test this method for a future 2.5 release
1343 // currently tested for use in jalview.gui.SequenceFetcher
1344 boolean samegap = toappend.getGapCharacter() == getGapCharacter();
1345 char oldc = toappend.getGapCharacter();
1346 boolean hashidden = toappend.getHiddenSequences() != null
1347 && toappend.getHiddenSequences().hiddenSequences != null;
1348 // get all sequences including any hidden ones
1349 List<SequenceI> sqs = (hashidden) ? toappend.getHiddenSequences()
1350 .getFullAlignment().getSequences() : toappend.getSequences();
1355 for (SequenceI addedsq : sqs)
1359 char[] oldseq = addedsq.getSequence();
1360 for (int c = 0; c < oldseq.length; c++)
1362 if (oldseq[c] == oldc)
1364 oldseq[c] = gapCharacter;
1368 addSequence(addedsq);
1372 AlignmentAnnotation[] alan = toappend.getAlignmentAnnotation();
1373 for (int a = 0; alan != null && a < alan.length; a++)
1375 addAnnotation(alan[a]);
1377 AlignedCodonFrame[] acod = toappend.getCodonFrames();
1378 for (int a = 0; acod != null && a < acod.length; a++)
1380 this.addCodonFrame(acod[a]);
1382 List<SequenceGroup> sg = toappend.getGroups();
1385 for (SequenceGroup _sg : sg)
1390 if (toappend.getHiddenSequences() != null)
1392 HiddenSequences hs = toappend.getHiddenSequences();
1393 if (hiddenSequences == null)
1395 hiddenSequences = new HiddenSequences(this);
1397 if (hs.hiddenSequences != null)
1399 for (int s = 0; s < hs.hiddenSequences.length; s++)
1401 // hide the newly appended sequence in the alignment
1402 if (hs.hiddenSequences[s] != null)
1404 hiddenSequences.hideSequence(hs.hiddenSequences[s]);
1409 if (toappend.getProperties() != null)
1411 // we really can't do very much here - just try to concatenate strings
1412 // where property collisions occur.
1413 Enumeration key = toappend.getProperties().keys();
1414 while (key.hasMoreElements())
1416 Object k = key.nextElement();
1417 Object ourval = this.getProperty(k);
1418 Object toapprop = toappend.getProperty(k);
1421 if (ourval.getClass().equals(toapprop.getClass())
1422 && !ourval.equals(toapprop))
1424 if (ourval instanceof String)
1427 this.setProperty(k, ((String) ourval) + "; "
1428 + ((String) toapprop));
1432 if (ourval instanceof Vector)
1435 Enumeration theirv = ((Vector) toapprop).elements();
1436 while (theirv.hasMoreElements())
1438 ((Vector) ourval).addElement(theirv);
1446 // just add new property directly
1447 setProperty(k, toapprop);
1455 public AlignmentAnnotation findOrCreateAnnotation(String name,
1456 String calcId, boolean autoCalc, SequenceI seqRef,
1457 SequenceGroup groupRef)
1459 assert (name != null);
1460 if (annotations != null)
1462 for (AlignmentAnnotation annot : getAlignmentAnnotation())
1464 if (annot.autoCalculated == autoCalc && (name.equals(annot.label))
1465 && (calcId == null || annot.getCalcId().equals(calcId))
1466 && annot.sequenceRef == seqRef
1467 && annot.groupRef == groupRef)
1473 AlignmentAnnotation annot = new AlignmentAnnotation(name, name,
1474 new Annotation[1], 0f, 0f, AlignmentAnnotation.BAR_GRAPH);
1475 annot.hasText = false;
1476 annot.setCalcId(new String(calcId));
1477 annot.autoCalculated = autoCalc;
1480 annot.setSequenceRef(seqRef);
1482 annot.groupRef = groupRef;
1483 addAnnotation(annot);
1489 public Iterable<AlignmentAnnotation> findAnnotation(String calcId)
1491 ArrayList<AlignmentAnnotation> aa = new ArrayList<AlignmentAnnotation>();
1492 for (AlignmentAnnotation a : getAlignmentAnnotation())
1494 if (a.getCalcId() == calcId
1495 || (a.getCalcId() != null && calcId != null && a.getCalcId()
1505 * Returns an iterable collection of any annotations that match on given
1506 * sequence ref, calcId and label (ignoring null values).
1509 public Iterable<AlignmentAnnotation> findAnnotations(SequenceI seq,
1510 String calcId, String label)
1512 ArrayList<AlignmentAnnotation> aa = new ArrayList<AlignmentAnnotation>();
1513 for (AlignmentAnnotation ann : getAlignmentAnnotation())
1515 if (ann.getCalcId() != null && ann.getCalcId().equals(calcId)
1516 && ann.sequenceRef != null && ann.sequenceRef == seq
1517 && ann.label != null && ann.label.equals(label))
1526 public void moveSelectedSequencesByOne(SequenceGroup sg,
1527 Map<SequenceI, SequenceCollectionI> map, boolean up)
1529 synchronized (sequences)
1534 for (int i = 1, iSize = sequences.size(); i < iSize; i++)
1536 SequenceI seq = sequences.get(i);
1537 if (!sg.getSequences(map).contains(seq))
1542 SequenceI temp = sequences.get(i - 1);
1543 if (sg.getSequences(null).contains(temp))
1548 sequences.set(i, temp);
1549 sequences.set(i - 1, seq);
1554 for (int i = sequences.size() - 2; i > -1; i--)
1556 SequenceI seq = sequences.get(i);
1557 if (!sg.getSequences(map).contains(seq))
1562 SequenceI temp = sequences.get(i + 1);
1563 if (sg.getSequences(map).contains(temp))
1568 sequences.set(i, temp);
1569 sequences.set(i + 1, seq);
1577 public void validateAnnotation(AlignmentAnnotation alignmentAnnotation)
1579 alignmentAnnotation.validateRangeAndDisplay();
1580 if (isNucleotide() && alignmentAnnotation.isValidStruc())
1582 hasRNAStructure = true;
1587 public int getEndRes()
1589 return getWidth() - 1;
1593 public int getStartRes()
1599 * In the case of AlignmentI - returns the dataset for the alignment, if set
1602 * @see jalview.datamodel.AnnotatedCollectionI#getContext()
1605 public AnnotatedCollectionI getContext()
1611 * Align this alignment 'the same as' the given one. Mapped sequences only are
1612 * realigned. If both of the same type (nucleotide/protein) then align both
1613 * identically. If this is nucleotide and the other is protein, make 3 gaps
1614 * for each gap in the protein sequences. If this is protein and the other is
1615 * nucleotide, insert a gap for each 3 gaps (or part thereof) between
1616 * nucleotide bases. Does nothing if alignment of protein from cDNA is
1617 * requested (not yet implemented).
1622 public int alignAs(AlignmentI al)
1625 boolean thisIsNucleotide = this.isNucleotide();
1626 boolean thatIsProtein = !al.isNucleotide();
1627 if (!thatIsProtein && !thisIsNucleotide)
1630 .println("Alignment of protein from cDNA not yet implemented");
1632 // todo: build it - a variant of Dna.CdnaTranslate()
1634 char thisGapChar = this.getGapCharacter();
1635 char thatGapChar = al.getGapCharacter();
1636 String gap = thisIsNucleotide && thatIsProtein ? String
1638 { thisGapChar, thisGapChar, thisGapChar }) : String
1639 .valueOf(thisGapChar);
1640 int ratio = thisIsNucleotide && thatIsProtein ? 3 : 1;
1643 * Get mappings from 'that' alignment's sequences to this.
1645 for (SequenceI alignTo : getSequences())
1647 AlignedCodonFrame[] mappings = al.getCodonFrame(alignTo);
1648 if (mappings != null)
1650 for (AlignedCodonFrame mapping : mappings)
1652 count += alignSequenceAs(alignTo, mapping, thatGapChar, gap,
1661 * Align sequence 'seq' the same way as 'other'. Note this currently assumes
1662 * that we are aligned cDNA to match protein.
1665 * the sequence to be realigned
1667 * holds mapping from the sequence whose alignment is to be 'copied'
1668 * @param thatGapChar
1669 * gap character used in the 'other' sequence
1671 * character string represent a gap in the realigned sequence
1673 * the number of positions in the realigned sequence corresponding to
1674 * one in the 'other'
1675 * @return true if the sequence was realigned, false if it could not be
1677 protected boolean alignSequenceAs(SequenceI seq,
1678 AlignedCodonFrame mapping,
1680 String gap, int ratio)
1682 char myGapChar = gap.charAt(0);
1683 // TODO rework this to use the mapping to match 'this' to 'that' residue
1684 // position, to handle introns and exons correctly.
1685 // TODO generalise to work for Protein-Protein, dna-dna, dna-protein
1686 SequenceI alignFrom = mapping.getAaForDnaSeq(seq, false);
1687 if (alignFrom == null)
1691 final char[] thisSeq = seq.getSequence();
1692 final char[] thisDs = seq.getDatasetSequence().getSequence();
1693 final char[] thatAligned = alignFrom.getSequence();
1694 StringBuilder thisAligned = new StringBuilder(2 * thisDs.length);
1697 * Find the DNA dataset position that corresponds to the first protein
1698 * residue (e.g. ignoring start codon in cDNA).
1700 int[] dnaStart = mapping.getDnaPosition(seq.getDatasetSequence(), 1);
1701 int thisDsPosition = dnaStart == null ? 0 : dnaStart[0] - 1;
1705 * Copy aligned cDNA up to (excluding) the first mapped base.
1707 int basesWritten = 0;
1708 while (basesWritten < thisDsPosition && thisSeqPos < thisSeq.length)
1710 char c = thisSeq[thisSeqPos++];
1711 thisAligned.append(c);
1719 * Now traverse the aligned protein mirroring its gaps in cDNA.
1721 for (char thatChar : thatAligned)
1723 if (thatChar == thatGapChar)
1726 * Add (equivalent of) a gap
1728 thisAligned.append(gap);
1733 * Add (equivalent of) a residue
1735 for (int j = 0; j < ratio && thisDsPosition < thisDs.length; j++)
1737 thisAligned.append(thisDs[thisDsPosition++]);
1740 * Also advance over any gaps and the next residue in the old aligned
1743 while (thisSeq[thisSeqPos] == myGapChar
1744 && thisSeqPos < thisSeq.length)
1754 * Finally copy any 'extra' aligned cDNA (e.g. stop codon, introns).
1756 while (thisSeqPos < thisSeq.length)
1758 thisAligned.append(thisSeq[thisSeqPos++]);
1760 seq.setSequence(new String(thisAligned));