2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ 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.io.FastaFile;
25 import jalview.util.MessageManager;
27 import java.util.ArrayList;
28 import java.util.Enumeration;
29 import java.util.HashSet;
30 import java.util.Hashtable;
31 import java.util.LinkedHashSet;
32 import java.util.List;
35 import java.util.Vector;
38 * Data structure to hold and manipulate a multiple sequence alignment
44 public class Alignment implements AlignmentI
46 protected Alignment dataset;
48 protected List<SequenceI> sequences;
50 protected List<SequenceGroup> groups = java.util.Collections
51 .synchronizedList(new ArrayList<SequenceGroup>());
53 protected char gapCharacter = '-';
55 protected int type = NUCLEOTIDE;
57 public static final int PROTEIN = 0;
59 public static final int NUCLEOTIDE = 1;
61 public boolean hasRNAStructure = false;
64 public AlignmentAnnotation[] annotations;
66 HiddenSequences hiddenSequences = new HiddenSequences(this);
68 public Hashtable alignmentProperties;
70 private Set<AlignedCodonFrame> codonFrameList = new LinkedHashSet<AlignedCodonFrame>();
72 private void initAlignment(SequenceI[] seqs)
76 if (jalview.util.Comparison.isNucleotide(seqs))
85 sequences = java.util.Collections
86 .synchronizedList(new ArrayList<SequenceI>());
88 for (i = 0; i < seqs.length; i++)
90 sequences.add(seqs[i]);
96 * Make a 'copy' alignment - sequences have new copies of features and
97 * annotations, but share the original dataset sequences.
99 public Alignment(AlignmentI al)
101 SequenceI[] seqs = al.getSequencesArray();
102 for (int i = 0; i < seqs.length; i++)
104 seqs[i] = new Sequence(seqs[i]);
108 * Share the same dataset sequence mappings (if any). TODO: find a better
109 * place for these to live (alignment dataset?).
111 this.codonFrameList = ((Alignment) al).codonFrameList;
117 * Make an alignment from an array of Sequences.
121 public Alignment(SequenceI[] seqs)
127 * Make a new alignment from an array of SeqCigars
132 public Alignment(SeqCigar[] alseqs)
134 SequenceI[] seqs = SeqCigar.createAlignmentSequences(alseqs,
135 gapCharacter, new ColumnSelection(), null);
140 * Make a new alignment from an CigarArray JBPNote - can only do this when
141 * compactAlignment does not contain hidden regions. JBPNote - must also check
142 * that compactAlignment resolves to a set of SeqCigars - or construct them
145 * @param compactAlignment
148 public static AlignmentI createAlignment(CigarArray compactAlignment)
150 throw new Error(MessageManager.getString("error.alignment_cigararray_not_implemented"));
151 // this(compactAlignment.refCigars);
155 public List<SequenceI> getSequences()
161 public List<SequenceI> getSequences(
162 Map<SequenceI, SequenceCollectionI> hiddenReps)
164 // TODO: in jalview 2.8 we don't do anything with hiddenreps - fix design to
170 public SequenceI[] getSequencesArray()
172 if (sequences == null)
176 synchronized (sequences)
178 return sequences.toArray(new SequenceI[sequences.size()]);
183 * Returns a map of lists of sequences keyed by sequence name.
188 public Map<String, List<SequenceI>> getSequencesByName()
190 return AlignmentUtils.getSequencesByName(this);
199 * @return DOCUMENT ME!
202 public SequenceI getSequenceAt(int i)
204 synchronized (sequences)
206 if (i > -1 && i < sequences.size())
208 return sequences.get(i);
215 * Adds a sequence to the alignment. Recalculates maxLength and size.
220 public void addSequence(SequenceI snew)
224 // maintain dataset integrity
225 if (snew.getDatasetSequence() != null)
227 getDataset().addSequence(snew.getDatasetSequence());
231 // derive new sequence
232 SequenceI adding = snew.deriveSequence();
233 getDataset().addSequence(adding.getDatasetSequence());
237 if (sequences == null)
239 initAlignment(new SequenceI[]
244 synchronized (sequences)
249 if (hiddenSequences != null)
251 hiddenSequences.adjustHeightSequenceAdded();
256 * Adds a sequence to the alignment. Recalculates maxLength and size.
261 public void setSequenceAt(int i, SequenceI snew)
263 synchronized (sequences)
266 sequences.set(i, snew);
273 * @return DOCUMENT ME!
276 public List<SequenceGroup> getGroups()
282 public void finalize()
284 if (getDataset() != null)
286 getDataset().removeAlignmentRef();
293 hiddenSequences = null;
297 * decrement the alignmentRefs counter by one and call finalize if it goes to
300 private void removeAlignmentRef()
302 if (--alignmentRefs == 0)
315 public void deleteSequence(SequenceI s)
317 deleteSequence(findIndex(s));
327 public void deleteSequence(int i)
329 if (i > -1 && i < getHeight())
331 synchronized (sequences)
334 hiddenSequences.adjustHeightSequenceDeleted(i);
342 * @see jalview.datamodel.AlignmentI#findGroup(jalview.datamodel.SequenceI)
345 public SequenceGroup findGroup(SequenceI s)
347 synchronized (groups)
349 for (int i = 0; i < this.groups.size(); i++)
351 SequenceGroup sg = groups.get(i);
353 if (sg.getSequences(null).contains(s))
366 * jalview.datamodel.AlignmentI#findAllGroups(jalview.datamodel.SequenceI)
369 public SequenceGroup[] findAllGroups(SequenceI s)
371 ArrayList<SequenceGroup> temp = new ArrayList<SequenceGroup>();
373 synchronized (groups)
375 int gSize = groups.size();
376 for (int i = 0; i < gSize; i++)
378 SequenceGroup sg = groups.get(i);
379 if (sg == null || sg.getSequences() == null)
381 this.deleteGroup(sg);
386 if (sg.getSequences().contains(s))
392 SequenceGroup[] ret = new SequenceGroup[temp.size()];
393 return temp.toArray(ret);
398 public void addGroup(SequenceGroup sg)
400 synchronized (groups)
402 if (!groups.contains(sg))
404 if (hiddenSequences.getSize() > 0)
406 int i, iSize = sg.getSize();
407 for (i = 0; i < iSize; i++)
409 if (!sequences.contains(sg.getSequenceAt(i)))
411 sg.deleteSequence(sg.getSequenceAt(i), false);
417 if (sg.getSize() < 1)
429 * remove any annotation that references gp
432 * (if null, removes all group associated annotation)
434 private void removeAnnotationForGroup(SequenceGroup gp)
436 if (annotations == null || annotations.length == 0)
440 // remove annotation very quickly
441 AlignmentAnnotation[] t, todelete = new AlignmentAnnotation[annotations.length], tokeep = new AlignmentAnnotation[annotations.length];
445 for (i = 0, p = 0, k = 0; i < annotations.length; i++)
447 if (annotations[i].groupRef != null)
449 todelete[p++] = annotations[i];
453 tokeep[k++] = annotations[i];
459 for (i = 0, p = 0, k = 0; i < annotations.length; i++)
461 if (annotations[i].groupRef == gp)
463 todelete[p++] = annotations[i];
467 tokeep[k++] = annotations[i];
473 // clear out the group associated annotation.
474 for (i = 0; i < p; i++)
476 unhookAnnotation(todelete[i]);
479 t = new AlignmentAnnotation[k];
480 for (i = 0; i < k; i++)
489 public void deleteAllGroups()
491 synchronized (groups)
493 if (annotations != null)
495 removeAnnotationForGroup(null);
497 for (SequenceGroup sg : groups)
507 public void deleteGroup(SequenceGroup g)
509 synchronized (groups)
511 if (groups.contains(g))
513 removeAnnotationForGroup(g);
522 public SequenceI findName(String name)
524 return findName(name, false);
530 * @see jalview.datamodel.AlignmentI#findName(java.lang.String, boolean)
533 public SequenceI findName(String token, boolean b)
535 return findName(null, token, b);
541 * @see jalview.datamodel.AlignmentI#findName(SequenceI, java.lang.String,
545 public SequenceI findName(SequenceI startAfter, String token, boolean b)
550 String sqname = null;
551 if (startAfter != null)
553 // try to find the sequence in the alignment
554 boolean matched = false;
555 while (i < sequences.size())
557 if (getSequenceAt(i++) == startAfter)
568 while (i < sequences.size())
570 sq = getSequenceAt(i);
571 sqname = sq.getName();
572 if (sqname.equals(token) // exact match
573 || (b && // allow imperfect matches - case varies
574 (sqname.equalsIgnoreCase(token))))
576 return getSequenceAt(i);
586 public SequenceI[] findSequenceMatch(String name)
588 Vector matches = new Vector();
591 while (i < sequences.size())
593 if (getSequenceAt(i).getName().equals(name))
595 matches.addElement(getSequenceAt(i));
600 SequenceI[] result = new SequenceI[matches.size()];
601 for (i = 0; i < result.length; i++)
603 result[i] = (SequenceI) matches.elementAt(i);
613 * @see jalview.datamodel.AlignmentI#findIndex(jalview.datamodel.SequenceI)
616 public int findIndex(SequenceI s)
620 while (i < sequences.size())
622 if (s == getSequenceAt(i))
637 * jalview.datamodel.AlignmentI#findIndex(jalview.datamodel.SearchResults)
640 public int findIndex(SearchResults results)
644 while (i < sequences.size())
646 if (results.involvesSequence(getSequenceAt(i)))
658 * @return DOCUMENT ME!
661 public int getHeight()
663 return sequences.size();
669 * @return DOCUMENT ME!
672 public int getWidth()
676 for (int i = 0; i < sequences.size(); i++)
678 if (getSequenceAt(i).getLength() > maxLength)
680 maxLength = getSequenceAt(i).getLength();
694 public void setGapCharacter(char gc)
697 synchronized (sequences)
699 for (SequenceI seq : sequences)
701 seq.setSequence(seq.getSequenceAsString().replace('.', gc)
702 .replace('-', gc).replace(' ', gc));
710 * @return DOCUMENT ME!
713 public char getGapCharacter()
721 * @see jalview.datamodel.AlignmentI#isAligned()
724 public boolean isAligned()
726 return isAligned(false);
732 * @see jalview.datamodel.AlignmentI#isAligned(boolean)
735 public boolean isAligned(boolean includeHidden)
737 int width = getWidth();
738 if (hiddenSequences == null || hiddenSequences.getSize() == 0)
740 includeHidden = true; // no hidden sequences to check against.
742 for (int i = 0; i < sequences.size(); i++)
744 if (includeHidden || !hiddenSequences.isHidden(getSequenceAt(i)))
746 if (getSequenceAt(i).getLength() != width)
757 * Delete all annotations, including auto-calculated if the flag is set true.
758 * Returns true if at least one annotation was deleted, else false.
760 * @param includingAutoCalculated
764 public boolean deleteAllAnnotations(boolean includingAutoCalculated)
766 boolean result = false;
767 for (AlignmentAnnotation alan : getAlignmentAnnotation())
769 if (!alan.autoCalculated || includingAutoCalculated)
771 deleteAnnotation(alan);
781 * @seejalview.datamodel.AlignmentI#deleteAnnotation(jalview.datamodel.
782 * AlignmentAnnotation)
785 public boolean deleteAnnotation(AlignmentAnnotation aa)
787 return deleteAnnotation(aa, true);
791 public boolean deleteAnnotation(AlignmentAnnotation aa, boolean unhook)
795 if (annotations != null)
797 aSize = annotations.length;
805 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize - 1];
807 boolean swap = false;
810 for (int i = 0; i < aSize; i++)
812 if (annotations[i] == aa)
817 if (tIndex < temp.length)
819 temp[tIndex++] = annotations[i];
828 unhookAnnotation(aa);
835 * remove any object references associated with this annotation
839 private void unhookAnnotation(AlignmentAnnotation aa)
841 if (aa.sequenceRef != null)
843 aa.sequenceRef.removeAlignmentAnnotation(aa);
845 if (aa.groupRef != null)
847 // probably need to do more here in the future (post 2.5.0)
855 * @seejalview.datamodel.AlignmentI#addAnnotation(jalview.datamodel.
856 * AlignmentAnnotation)
859 public void addAnnotation(AlignmentAnnotation aa)
861 addAnnotation(aa, -1);
867 * @seejalview.datamodel.AlignmentI#addAnnotation(jalview.datamodel.
868 * AlignmentAnnotation, int)
871 public void addAnnotation(AlignmentAnnotation aa, int pos)
873 if (aa.getRNAStruc() != null)
875 hasRNAStructure = true;
879 if (annotations != null)
881 aSize = annotations.length + 1;
884 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
886 if (pos == -1 || pos >= aSize)
888 temp[aSize - 1] = aa;
897 for (i = 0; i < (aSize - 1); i++, p++)
905 temp[p] = annotations[i];
914 public void setAnnotationIndex(AlignmentAnnotation aa, int index)
916 if (aa == null || annotations == null || annotations.length - 1 < index)
921 int aSize = annotations.length;
922 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
926 for (int i = 0; i < aSize; i++)
935 temp[i] = annotations[i];
939 temp[i] = annotations[i - 1];
948 * returns all annotation on the alignment
950 public AlignmentAnnotation[] getAlignmentAnnotation()
956 public void setNucleotide(boolean b)
969 public boolean isNucleotide()
971 if (type == NUCLEOTIDE)
982 public boolean hasRNAStructure()
984 // TODO can it happen that structure is removed from alignment?
985 return hasRNAStructure;
989 public void setDataset(Alignment data)
991 if (dataset == null && data == null)
993 // Create a new dataset for this alignment.
994 // Can only be done once, if dataset is not null
995 // This will not be performed
996 SequenceI[] seqs = new SequenceI[getHeight()];
997 SequenceI currentSeq;
998 for (int i = 0; i < getHeight(); i++)
1000 currentSeq = getSequenceAt(i);
1001 if (currentSeq.getDatasetSequence() != null)
1003 seqs[i] = currentSeq.getDatasetSequence();
1007 seqs[i] = currentSeq.createDatasetSequence();
1011 dataset = new Alignment(seqs);
1013 else if (dataset == null && data != null)
1016 for (int i = 0; i < getHeight(); i++)
1018 SequenceI currentSeq = getSequenceAt(i);
1019 SequenceI dsq = currentSeq.getDatasetSequence();
1022 dsq = currentSeq.createDatasetSequence();
1023 dataset.addSequence(dsq);
1027 while (dsq.getDatasetSequence() != null)
1029 dsq = dsq.getDatasetSequence();
1031 if (dataset.findIndex(dsq) == -1)
1033 dataset.addSequence(dsq);
1038 dataset.addAlignmentRef();
1042 * reference count for number of alignments referencing this one.
1044 int alignmentRefs = 0;
1047 * increase reference count to this alignment.
1049 private void addAlignmentRef()
1055 public Alignment getDataset()
1061 public boolean padGaps()
1063 boolean modified = false;
1065 // Remove excess gaps from the end of alignment
1069 for (int i = 0; i < sequences.size(); i++)
1071 current = getSequenceAt(i);
1072 for (int j = current.getLength(); j > maxLength; j--)
1075 && !jalview.util.Comparison.isGap(current.getCharAt(j)))
1086 for (int i = 0; i < sequences.size(); i++)
1088 current = getSequenceAt(i);
1089 cLength = current.getLength();
1091 if (cLength < maxLength)
1093 current.insertCharAt(cLength, maxLength - cLength, gapCharacter);
1096 else if (current.getLength() > maxLength)
1098 current.deleteChars(maxLength, current.getLength());
1105 * Justify the sequences to the left or right by deleting and inserting gaps
1106 * before the initial residue or after the terminal residue
1109 * true if alignment padded to right, false to justify to left
1110 * @return true if alignment was changed
1113 public boolean justify(boolean right)
1115 boolean modified = false;
1117 // Remove excess gaps from the end of alignment
1119 int ends[] = new int[sequences.size() * 2];
1121 for (int i = 0; i < sequences.size(); i++)
1123 current = getSequenceAt(i);
1124 // This should really be a sequence method
1125 ends[i * 2] = current.findIndex(current.getStart());
1126 ends[i * 2 + 1] = current.findIndex(current.getStart()
1127 + current.getLength());
1128 boolean hitres = false;
1129 for (int j = 0, rs = 0, ssiz = current.getLength(); j < ssiz; j++)
1131 if (!jalview.util.Comparison.isGap(current.getCharAt(j)))
1140 ends[i * 2 + 1] = j;
1141 if (j - ends[i * 2] > maxLength)
1143 maxLength = j - ends[i * 2];
1151 // now edit the flanking gaps to justify to either left or right
1152 int cLength, extent, diff;
1153 for (int i = 0; i < sequences.size(); i++)
1155 current = getSequenceAt(i);
1157 cLength = 1 + ends[i * 2 + 1] - ends[i * 2];
1158 diff = maxLength - cLength; // number of gaps to indent
1159 extent = current.getLength();
1163 if (extent > ends[i * 2 + 1])
1165 current.deleteChars(ends[i * 2 + 1] + 1, extent);
1168 if (ends[i * 2] > diff)
1170 current.deleteChars(0, ends[i * 2] - diff);
1175 if (ends[i * 2] < diff)
1177 current.insertCharAt(0, diff - ends[i * 2], gapCharacter);
1185 if (ends[i * 2] > 0)
1187 current.deleteChars(0, ends[i * 2]);
1189 ends[i * 2 + 1] -= ends[i * 2];
1190 extent -= ends[i * 2];
1192 if (extent > maxLength)
1194 current.deleteChars(maxLength + 1, extent);
1199 if (extent < maxLength)
1201 current.insertCharAt(extent, maxLength - extent, gapCharacter);
1211 public HiddenSequences getHiddenSequences()
1213 return hiddenSequences;
1217 public CigarArray getCompactAlignment()
1219 synchronized (sequences)
1221 SeqCigar alseqs[] = new SeqCigar[sequences.size()];
1223 for (SequenceI seq : sequences)
1225 alseqs[i++] = new SeqCigar(seq);
1227 CigarArray cal = new CigarArray(alseqs);
1228 cal.addOperation(CigarArray.M, getWidth());
1234 public void setProperty(Object key, Object value)
1236 if (alignmentProperties == null)
1238 alignmentProperties = new Hashtable();
1241 alignmentProperties.put(key, value);
1245 public Object getProperty(Object key)
1247 if (alignmentProperties != null)
1249 return alignmentProperties.get(key);
1258 public Hashtable getProperties()
1260 return alignmentProperties;
1267 * jalview.datamodel.AlignmentI#addCodonFrame(jalview.datamodel.AlignedCodonFrame
1271 public void addCodonFrame(AlignedCodonFrame codons)
1275 codonFrameList.add(codons);
1283 * jalview.datamodel.AlignmentI#getCodonFrame(jalview.datamodel.SequenceI)
1286 public List<AlignedCodonFrame> getCodonFrame(SequenceI seq)
1292 List<AlignedCodonFrame> cframes = new ArrayList<AlignedCodonFrame>();
1293 for (AlignedCodonFrame acf : codonFrameList)
1295 if (acf.involvesSequence(seq))
1304 * Sets the codon frame mappings (replacing any existing mappings).
1306 * @see jalview.datamodel.AlignmentI#setCodonFrames()
1309 public void setCodonFrames(Set<AlignedCodonFrame> acfs)
1311 this.codonFrameList = acfs;
1315 * Returns the set of codon frame mappings. Any changes to the returned set
1316 * will affect the alignment.
1318 * @see jalview.datamodel.AlignmentI#getCodonFrames()
1321 public Set<AlignedCodonFrame> getCodonFrames()
1323 return codonFrameList;
1329 * @seejalview.datamodel.AlignmentI#removeCodonFrame(jalview.datamodel.
1330 * AlignedCodonFrame)
1333 public boolean removeCodonFrame(AlignedCodonFrame codons)
1335 if (codons == null || codonFrameList == null)
1339 return codonFrameList.remove(codons);
1343 public void append(AlignmentI toappend)
1345 if (toappend == this)
1347 System.err.println("Self append may cause a deadlock.");
1349 // TODO test this method for a future 2.5 release
1350 // currently tested for use in jalview.gui.SequenceFetcher
1351 boolean samegap = toappend.getGapCharacter() == getGapCharacter();
1352 char oldc = toappend.getGapCharacter();
1353 boolean hashidden = toappend.getHiddenSequences() != null
1354 && toappend.getHiddenSequences().hiddenSequences != null;
1355 // get all sequences including any hidden ones
1356 List<SequenceI> sqs = (hashidden) ? toappend.getHiddenSequences()
1357 .getFullAlignment().getSequences() : toappend.getSequences();
1362 for (SequenceI addedsq : sqs)
1366 char[] oldseq = addedsq.getSequence();
1367 for (int c = 0; c < oldseq.length; c++)
1369 if (oldseq[c] == oldc)
1371 oldseq[c] = gapCharacter;
1375 addSequence(addedsq);
1379 AlignmentAnnotation[] alan = toappend.getAlignmentAnnotation();
1380 for (int a = 0; alan != null && a < alan.length; a++)
1382 addAnnotation(alan[a]);
1385 this.codonFrameList.addAll(toappend.getCodonFrames());
1387 List<SequenceGroup> sg = toappend.getGroups();
1390 for (SequenceGroup _sg : sg)
1395 if (toappend.getHiddenSequences() != null)
1397 HiddenSequences hs = toappend.getHiddenSequences();
1398 if (hiddenSequences == null)
1400 hiddenSequences = new HiddenSequences(this);
1402 if (hs.hiddenSequences != null)
1404 for (int s = 0; s < hs.hiddenSequences.length; s++)
1406 // hide the newly appended sequence in the alignment
1407 if (hs.hiddenSequences[s] != null)
1409 hiddenSequences.hideSequence(hs.hiddenSequences[s]);
1414 if (toappend.getProperties() != null)
1416 // we really can't do very much here - just try to concatenate strings
1417 // where property collisions occur.
1418 Enumeration key = toappend.getProperties().keys();
1419 while (key.hasMoreElements())
1421 Object k = key.nextElement();
1422 Object ourval = this.getProperty(k);
1423 Object toapprop = toappend.getProperty(k);
1426 if (ourval.getClass().equals(toapprop.getClass())
1427 && !ourval.equals(toapprop))
1429 if (ourval instanceof String)
1432 this.setProperty(k, ((String) ourval) + "; "
1433 + ((String) toapprop));
1437 if (ourval instanceof Vector)
1440 Enumeration theirv = ((Vector) toapprop).elements();
1441 while (theirv.hasMoreElements())
1443 ((Vector) ourval).addElement(theirv);
1451 // just add new property directly
1452 setProperty(k, toapprop);
1460 public AlignmentAnnotation findOrCreateAnnotation(String name,
1461 String calcId, boolean autoCalc, SequenceI seqRef,
1462 SequenceGroup groupRef)
1464 assert (name != null);
1465 if (annotations != null)
1467 for (AlignmentAnnotation annot : getAlignmentAnnotation())
1469 if (annot.autoCalculated == autoCalc && (name.equals(annot.label))
1470 && (calcId == null || annot.getCalcId().equals(calcId))
1471 && annot.sequenceRef == seqRef
1472 && annot.groupRef == groupRef)
1478 AlignmentAnnotation annot = new AlignmentAnnotation(name, name,
1479 new Annotation[1], 0f, 0f, AlignmentAnnotation.BAR_GRAPH);
1480 annot.hasText = false;
1481 annot.setCalcId(new String(calcId));
1482 annot.autoCalculated = autoCalc;
1485 annot.setSequenceRef(seqRef);
1487 annot.groupRef = groupRef;
1488 addAnnotation(annot);
1494 public Iterable<AlignmentAnnotation> findAnnotation(String calcId)
1496 ArrayList<AlignmentAnnotation> aa = new ArrayList<AlignmentAnnotation>();
1497 for (AlignmentAnnotation a : getAlignmentAnnotation())
1499 if (a.getCalcId() == calcId
1500 || (a.getCalcId() != null && calcId != null && a.getCalcId()
1510 * Returns an iterable collection of any annotations that match on given
1511 * sequence ref, calcId and label (ignoring null values).
1514 public Iterable<AlignmentAnnotation> findAnnotations(SequenceI seq,
1515 String calcId, String label)
1517 ArrayList<AlignmentAnnotation> aa = new ArrayList<AlignmentAnnotation>();
1518 for (AlignmentAnnotation ann : getAlignmentAnnotation())
1520 if (ann.getCalcId() != null && ann.getCalcId().equals(calcId)
1521 && ann.sequenceRef != null && ann.sequenceRef == seq
1522 && ann.label != null && ann.label.equals(label))
1531 public void moveSelectedSequencesByOne(SequenceGroup sg,
1532 Map<SequenceI, SequenceCollectionI> map, boolean up)
1534 synchronized (sequences)
1539 for (int i = 1, iSize = sequences.size(); i < iSize; i++)
1541 SequenceI seq = sequences.get(i);
1542 if (!sg.getSequences(map).contains(seq))
1547 SequenceI temp = sequences.get(i - 1);
1548 if (sg.getSequences(null).contains(temp))
1553 sequences.set(i, temp);
1554 sequences.set(i - 1, seq);
1559 for (int i = sequences.size() - 2; i > -1; i--)
1561 SequenceI seq = sequences.get(i);
1562 if (!sg.getSequences(map).contains(seq))
1567 SequenceI temp = sequences.get(i + 1);
1568 if (sg.getSequences(map).contains(temp))
1573 sequences.set(i, temp);
1574 sequences.set(i + 1, seq);
1582 public void validateAnnotation(AlignmentAnnotation alignmentAnnotation)
1584 alignmentAnnotation.validateRangeAndDisplay();
1585 if (isNucleotide() && alignmentAnnotation.isValidStruc())
1587 hasRNAStructure = true;
1592 private SequenceI seqrep=null;
1596 * @return the representative sequence for this group
1598 public SequenceI getSeqrep()
1604 * set the representative sequence for this group. Note - this affects the
1605 * interpretation of the Hidereps attribute.
1608 * the seqrep to set (null means no sequence representative)
1610 public void setSeqrep(SequenceI seqrep)
1612 this.seqrep = seqrep;
1617 * @return true if group has a sequence representative
1619 public boolean hasSeqrep()
1621 return seqrep != null;
1625 public int getEndRes()
1627 return getWidth() - 1;
1631 public int getStartRes()
1637 * In the case of AlignmentI - returns the dataset for the alignment, if set
1640 * @see jalview.datamodel.AnnotatedCollectionI#getContext()
1643 public AnnotatedCollectionI getContext()
1649 * Align this alignment like the given (mapped) one.
1652 public int alignAs(AlignmentI al)
1655 * Currently retains unmapped gaps (in introns), regaps mapped regions
1658 return alignAs(al, false, true);
1662 * Align this alignment 'the same as' the given one. Mapped sequences only are
1663 * realigned. If both of the same type (nucleotide/protein) then align both
1664 * identically. If this is nucleotide and the other is protein, make 3 gaps
1665 * for each gap in the protein sequences. If this is protein and the other is
1666 * nucleotide, insert a gap for each 3 gaps (or part thereof) between
1667 * nucleotide bases. If this is protein and the other is nucleotide, gaps
1668 * protein to match the relative ordering of codons in the nucleotide.
1670 * Parameters control whether gaps in exon (mapped) and intron (unmapped)
1671 * regions are preserved. Gaps that connect introns to exons are treated
1672 * conservatively, i.e. only preserved if both intron and exon gaps are
1676 * @param preserveMappedGaps
1677 * if true, gaps within and between mapped codons are preserved
1678 * @param preserveUnmappedGaps
1679 * if true, gaps within and between unmapped codons are preserved
1682 public int alignAs(AlignmentI al, boolean preserveMappedGaps,
1683 boolean preserveUnmappedGaps)
1685 // TODO should this method signature be the one in the interface?
1687 boolean thisIsNucleotide = this.isNucleotide();
1688 boolean thatIsProtein = !al.isNucleotide();
1689 if (!thatIsProtein && !thisIsNucleotide)
1691 return AlignmentUtils.alignProteinAsDna(this, al);
1694 char thisGapChar = this.getGapCharacter();
1695 String gap = thisIsNucleotide && thatIsProtein ? String
1697 { thisGapChar, thisGapChar, thisGapChar }) : String
1698 .valueOf(thisGapChar);
1700 // TODO handle intron regions? Needs a 'holistic' alignment of dna,
1701 // not just sequence by sequence. But how to 'gap' intron regions?
1704 * Get mappings from 'that' alignment's sequences to this.
1706 for (SequenceI alignTo : getSequences())
1708 count += AlignmentUtils.alignSequenceAs(alignTo, al, gap, preserveMappedGaps,
1709 preserveUnmappedGaps) ? 1 : 0;
1715 * Returns the alignment in Fasta format. Behaviour of this method is not
1716 * guaranteed between versions.
1719 public String toString()
1721 return new FastaFile().print(getSequencesArray());
1725 * Returns the set of distinct sequence names. No ordering is guaranteed.
1728 public Set<String> getSequenceNames()
1730 Set<String> names = new HashSet<String>();
1731 for (SequenceI seq : getSequences())
1733 names.add(seq.getName());
1739 public boolean hasValidSequence()
1741 boolean hasValidSeq = false;
1742 for (SequenceI seq : getSequences())
1744 if ((seq.getEnd() - seq.getStart()) > 0)