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.Comparison;
26 import jalview.util.MessageManager;
28 import java.util.ArrayList;
29 import java.util.Collections;
30 import java.util.Enumeration;
31 import java.util.HashSet;
32 import java.util.Hashtable;
33 import java.util.List;
36 import java.util.Vector;
39 * Data structure to hold and manipulate a multiple sequence alignment
45 public class Alignment implements AlignmentI
47 protected Alignment dataset;
49 protected List<SequenceI> sequences;
51 protected List<SequenceGroup> groups;
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;
63 public AlignmentAnnotation[] annotations;
65 HiddenSequences hiddenSequences;
67 public Hashtable alignmentProperties;
69 private List<AlignedCodonFrame> codonFrameList;
71 private void initAlignment(SequenceI[] seqs)
73 groups = Collections.synchronizedList(new ArrayList<SequenceGroup>());
74 hiddenSequences = new HiddenSequences(this);
75 codonFrameList = new ArrayList<AlignedCodonFrame>();
77 if (Comparison.isNucleotide(seqs))
86 sequences = Collections.synchronizedList(new ArrayList<SequenceI>());
88 for (int 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]);
110 * Share the same dataset sequence mappings (if any).
112 this.setCodonFrames(al.getCodonFrames());
116 * Make an alignment from an array of Sequences.
120 public Alignment(SequenceI[] seqs)
126 * Make a new alignment from an array of SeqCigars
131 public Alignment(SeqCigar[] alseqs)
133 SequenceI[] seqs = SeqCigar.createAlignmentSequences(alseqs,
134 gapCharacter, new ColumnSelection(), null);
139 * Make a new alignment from an CigarArray JBPNote - can only do this when
140 * compactAlignment does not contain hidden regions. JBPNote - must also check
141 * that compactAlignment resolves to a set of SeqCigars - or construct them
144 * @param compactAlignment
147 public static AlignmentI createAlignment(CigarArray compactAlignment)
151 .getString("error.alignment_cigararray_not_implemented"));
152 // this(compactAlignment.refCigars);
156 public List<SequenceI> getSequences()
162 public List<SequenceI> getSequences(
163 Map<SequenceI, SequenceCollectionI> hiddenReps)
165 // TODO: in jalview 2.8 we don't do anything with hiddenreps - fix design to
171 public SequenceI[] getSequencesArray()
173 if (sequences == null)
177 synchronized (sequences)
179 return sequences.toArray(new SequenceI[sequences.size()]);
184 * Returns a map of lists of sequences keyed by sequence name.
189 public Map<String, List<SequenceI>> getSequencesByName()
191 return AlignmentUtils.getSequencesByName(this);
200 * @return DOCUMENT ME!
203 public SequenceI getSequenceAt(int i)
205 synchronized (sequences)
207 if (i > -1 && i < sequences.size())
209 return sequences.get(i);
216 * Adds a sequence to the alignment. Recalculates maxLength and size.
221 public void addSequence(SequenceI snew)
225 // maintain dataset integrity
226 if (snew.getDatasetSequence() != null)
228 getDataset().addSequence(snew.getDatasetSequence());
232 // derive new sequence
233 SequenceI adding = snew.deriveSequence();
234 getDataset().addSequence(adding.getDatasetSequence());
238 if (sequences == null)
240 initAlignment(new SequenceI[] { snew });
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 createDatasetAlignment();
995 else if (dataset == null && data != null)
998 for (int i = 0; i < getHeight(); i++)
1000 SequenceI currentSeq = getSequenceAt(i);
1001 SequenceI dsq = currentSeq.getDatasetSequence();
1004 dsq = currentSeq.createDatasetSequence();
1005 dataset.addSequence(dsq);
1009 while (dsq.getDatasetSequence() != null)
1011 dsq = dsq.getDatasetSequence();
1013 if (dataset.findIndex(dsq) == -1)
1015 dataset.addSequence(dsq);
1020 dataset.addAlignmentRef();
1024 * Creates a new dataset for this alignment. Can only be done once - if
1025 * dataset is not null this will not be performed.
1027 public void createDatasetAlignment()
1029 if (dataset != null)
1033 SequenceI[] seqs = new SequenceI[getHeight()];
1034 SequenceI currentSeq;
1035 for (int i = 0; i < getHeight(); i++)
1037 currentSeq = getSequenceAt(i);
1038 if (currentSeq.getDatasetSequence() != null)
1040 seqs[i] = currentSeq.getDatasetSequence();
1044 seqs[i] = currentSeq.createDatasetSequence();
1048 dataset = new Alignment(seqs);
1049 // move mappings to the dataset alignment
1050 dataset.codonFrameList = this.codonFrameList;
1051 this.codonFrameList = null;
1055 * reference count for number of alignments referencing this one.
1057 int alignmentRefs = 0;
1060 * increase reference count to this alignment.
1062 private void addAlignmentRef()
1068 public Alignment getDataset()
1074 public boolean padGaps()
1076 boolean modified = false;
1078 // Remove excess gaps from the end of alignment
1082 for (int i = 0; i < sequences.size(); i++)
1084 current = getSequenceAt(i);
1085 for (int j = current.getLength(); j > maxLength; j--)
1088 && !jalview.util.Comparison.isGap(current.getCharAt(j)))
1099 for (int i = 0; i < sequences.size(); i++)
1101 current = getSequenceAt(i);
1102 cLength = current.getLength();
1104 if (cLength < maxLength)
1106 current.insertCharAt(cLength, maxLength - cLength, gapCharacter);
1109 else if (current.getLength() > maxLength)
1111 current.deleteChars(maxLength, current.getLength());
1118 * Justify the sequences to the left or right by deleting and inserting gaps
1119 * before the initial residue or after the terminal residue
1122 * true if alignment padded to right, false to justify to left
1123 * @return true if alignment was changed
1126 public boolean justify(boolean right)
1128 boolean modified = false;
1130 // Remove excess gaps from the end of alignment
1132 int ends[] = new int[sequences.size() * 2];
1134 for (int i = 0; i < sequences.size(); i++)
1136 current = getSequenceAt(i);
1137 // This should really be a sequence method
1138 ends[i * 2] = current.findIndex(current.getStart());
1139 ends[i * 2 + 1] = current.findIndex(current.getStart()
1140 + current.getLength());
1141 boolean hitres = false;
1142 for (int j = 0, rs = 0, ssiz = current.getLength(); j < ssiz; j++)
1144 if (!jalview.util.Comparison.isGap(current.getCharAt(j)))
1153 ends[i * 2 + 1] = j;
1154 if (j - ends[i * 2] > maxLength)
1156 maxLength = j - ends[i * 2];
1164 // now edit the flanking gaps to justify to either left or right
1165 int cLength, extent, diff;
1166 for (int i = 0; i < sequences.size(); i++)
1168 current = getSequenceAt(i);
1170 cLength = 1 + ends[i * 2 + 1] - ends[i * 2];
1171 diff = maxLength - cLength; // number of gaps to indent
1172 extent = current.getLength();
1176 if (extent > ends[i * 2 + 1])
1178 current.deleteChars(ends[i * 2 + 1] + 1, extent);
1181 if (ends[i * 2] > diff)
1183 current.deleteChars(0, ends[i * 2] - diff);
1188 if (ends[i * 2] < diff)
1190 current.insertCharAt(0, diff - ends[i * 2], gapCharacter);
1198 if (ends[i * 2] > 0)
1200 current.deleteChars(0, ends[i * 2]);
1202 ends[i * 2 + 1] -= ends[i * 2];
1203 extent -= ends[i * 2];
1205 if (extent > maxLength)
1207 current.deleteChars(maxLength + 1, extent);
1212 if (extent < maxLength)
1214 current.insertCharAt(extent, maxLength - extent, gapCharacter);
1224 public HiddenSequences getHiddenSequences()
1226 return hiddenSequences;
1230 public CigarArray getCompactAlignment()
1232 synchronized (sequences)
1234 SeqCigar alseqs[] = new SeqCigar[sequences.size()];
1236 for (SequenceI seq : sequences)
1238 alseqs[i++] = new SeqCigar(seq);
1240 CigarArray cal = new CigarArray(alseqs);
1241 cal.addOperation(CigarArray.M, getWidth());
1247 public void setProperty(Object key, Object value)
1249 if (alignmentProperties == null)
1251 alignmentProperties = new Hashtable();
1254 alignmentProperties.put(key, value);
1258 public Object getProperty(Object key)
1260 if (alignmentProperties != null)
1262 return alignmentProperties.get(key);
1271 public Hashtable getProperties()
1273 return alignmentProperties;
1277 * Adds the given mapping to the stored set. Note this may be held on the
1278 * dataset alignment.
1281 public void addCodonFrame(AlignedCodonFrame codons)
1283 List<AlignedCodonFrame> acfs = getCodonFrames();
1284 if (codons != null && acfs != null && !acfs.contains(codons))
1294 * jalview.datamodel.AlignmentI#getCodonFrame(jalview.datamodel.SequenceI)
1297 public List<AlignedCodonFrame> getCodonFrame(SequenceI seq)
1303 List<AlignedCodonFrame> cframes = new ArrayList<AlignedCodonFrame>();
1304 for (AlignedCodonFrame acf : getCodonFrames())
1306 if (acf.involvesSequence(seq))
1315 * Sets the codon frame mappings (replacing any existing mappings). Note the
1316 * mappings are set on the dataset alignment instead if there is one.
1318 * @see jalview.datamodel.AlignmentI#setCodonFrames()
1321 public void setCodonFrames(List<AlignedCodonFrame> acfs)
1323 if (dataset != null)
1325 dataset.setCodonFrames(acfs);
1329 this.codonFrameList = acfs;
1334 * Returns the set of codon frame mappings. Any changes to the returned set
1335 * will affect the alignment. The mappings are held on (and read from) the
1336 * dataset alignment if there is one.
1338 * @see jalview.datamodel.AlignmentI#getCodonFrames()
1341 public List<AlignedCodonFrame> getCodonFrames()
1343 return dataset != null ? dataset.getCodonFrames() : codonFrameList;
1347 * Removes the given mapping from the stored set. Note that the mappings are
1348 * held on the dataset alignment if there is one.
1351 public boolean removeCodonFrame(AlignedCodonFrame codons)
1353 List<AlignedCodonFrame> acfs = getCodonFrames();
1354 if (codons == null || acfs == null)
1358 return acfs.remove(codons);
1362 public void append(AlignmentI toappend)
1364 if (toappend == this)
1366 System.err.println("Self append may cause a deadlock.");
1368 // TODO test this method for a future 2.5 release
1369 // currently tested for use in jalview.gui.SequenceFetcher
1370 boolean samegap = toappend.getGapCharacter() == getGapCharacter();
1371 char oldc = toappend.getGapCharacter();
1372 boolean hashidden = toappend.getHiddenSequences() != null
1373 && toappend.getHiddenSequences().hiddenSequences != null;
1374 // get all sequences including any hidden ones
1375 List<SequenceI> sqs = (hashidden) ? toappend.getHiddenSequences()
1376 .getFullAlignment().getSequences() : toappend.getSequences();
1381 for (SequenceI addedsq : sqs)
1385 char[] oldseq = addedsq.getSequence();
1386 for (int c = 0; c < oldseq.length; c++)
1388 if (oldseq[c] == oldc)
1390 oldseq[c] = gapCharacter;
1394 addSequence(addedsq);
1398 AlignmentAnnotation[] alan = toappend.getAlignmentAnnotation();
1399 for (int a = 0; alan != null && a < alan.length; a++)
1401 addAnnotation(alan[a]);
1404 getCodonFrames().addAll(toappend.getCodonFrames());
1406 List<SequenceGroup> sg = toappend.getGroups();
1409 for (SequenceGroup _sg : sg)
1414 if (toappend.getHiddenSequences() != null)
1416 HiddenSequences hs = toappend.getHiddenSequences();
1417 if (hiddenSequences == null)
1419 hiddenSequences = new HiddenSequences(this);
1421 if (hs.hiddenSequences != null)
1423 for (int s = 0; s < hs.hiddenSequences.length; s++)
1425 // hide the newly appended sequence in the alignment
1426 if (hs.hiddenSequences[s] != null)
1428 hiddenSequences.hideSequence(hs.hiddenSequences[s]);
1433 if (toappend.getProperties() != null)
1435 // we really can't do very much here - just try to concatenate strings
1436 // where property collisions occur.
1437 Enumeration key = toappend.getProperties().keys();
1438 while (key.hasMoreElements())
1440 Object k = key.nextElement();
1441 Object ourval = this.getProperty(k);
1442 Object toapprop = toappend.getProperty(k);
1445 if (ourval.getClass().equals(toapprop.getClass())
1446 && !ourval.equals(toapprop))
1448 if (ourval instanceof String)
1451 this.setProperty(k, ((String) ourval) + "; "
1452 + ((String) toapprop));
1456 if (ourval instanceof Vector)
1459 Enumeration theirv = ((Vector) toapprop).elements();
1460 while (theirv.hasMoreElements())
1462 ((Vector) ourval).addElement(theirv);
1470 // just add new property directly
1471 setProperty(k, toapprop);
1479 public AlignmentAnnotation findOrCreateAnnotation(String name,
1480 String calcId, boolean autoCalc, SequenceI seqRef,
1481 SequenceGroup groupRef)
1483 assert (name != null);
1484 if (annotations != null)
1486 for (AlignmentAnnotation annot : getAlignmentAnnotation())
1488 if (annot.autoCalculated == autoCalc && (name.equals(annot.label))
1489 && (calcId == null || annot.getCalcId().equals(calcId))
1490 && annot.sequenceRef == seqRef
1491 && annot.groupRef == groupRef)
1497 AlignmentAnnotation annot = new AlignmentAnnotation(name, name,
1498 new Annotation[1], 0f, 0f, AlignmentAnnotation.BAR_GRAPH);
1499 annot.hasText = false;
1500 annot.setCalcId(new String(calcId));
1501 annot.autoCalculated = autoCalc;
1504 annot.setSequenceRef(seqRef);
1506 annot.groupRef = groupRef;
1507 addAnnotation(annot);
1513 public Iterable<AlignmentAnnotation> findAnnotation(String calcId)
1515 ArrayList<AlignmentAnnotation> aa = new ArrayList<AlignmentAnnotation>();
1516 for (AlignmentAnnotation a : getAlignmentAnnotation())
1518 if (a.getCalcId() == calcId
1519 || (a.getCalcId() != null && calcId != null && a.getCalcId()
1529 * Returns an iterable collection of any annotations that match on given
1530 * sequence ref, calcId and label (ignoring null values).
1533 public Iterable<AlignmentAnnotation> findAnnotations(SequenceI seq,
1534 String calcId, String label)
1536 ArrayList<AlignmentAnnotation> aa = new ArrayList<AlignmentAnnotation>();
1537 for (AlignmentAnnotation ann : getAlignmentAnnotation())
1539 if (ann.getCalcId() != null && ann.getCalcId().equals(calcId)
1540 && ann.sequenceRef != null && ann.sequenceRef == seq
1541 && ann.label != null && ann.label.equals(label))
1550 public void moveSelectedSequencesByOne(SequenceGroup sg,
1551 Map<SequenceI, SequenceCollectionI> map, boolean up)
1553 synchronized (sequences)
1558 for (int i = 1, iSize = sequences.size(); i < iSize; i++)
1560 SequenceI seq = sequences.get(i);
1561 if (!sg.getSequences(map).contains(seq))
1566 SequenceI temp = sequences.get(i - 1);
1567 if (sg.getSequences(null).contains(temp))
1572 sequences.set(i, temp);
1573 sequences.set(i - 1, seq);
1578 for (int i = sequences.size() - 2; i > -1; i--)
1580 SequenceI seq = sequences.get(i);
1581 if (!sg.getSequences(map).contains(seq))
1586 SequenceI temp = sequences.get(i + 1);
1587 if (sg.getSequences(map).contains(temp))
1592 sequences.set(i, temp);
1593 sequences.set(i + 1, seq);
1601 public void validateAnnotation(AlignmentAnnotation alignmentAnnotation)
1603 alignmentAnnotation.validateRangeAndDisplay();
1604 if (isNucleotide() && alignmentAnnotation.isValidStruc())
1606 hasRNAStructure = true;
1610 private SequenceI seqrep = null;
1614 * @return the representative sequence for this group
1617 public SequenceI getSeqrep()
1623 * set the representative sequence for this group. Note - this affects the
1624 * interpretation of the Hidereps attribute.
1627 * the seqrep to set (null means no sequence representative)
1630 public void setSeqrep(SequenceI seqrep)
1632 this.seqrep = seqrep;
1637 * @return true if group has a sequence representative
1640 public boolean hasSeqrep()
1642 return seqrep != null;
1646 public int getEndRes()
1648 return getWidth() - 1;
1652 public int getStartRes()
1658 * In the case of AlignmentI - returns the dataset for the alignment, if set
1661 * @see jalview.datamodel.AnnotatedCollectionI#getContext()
1664 public AnnotatedCollectionI getContext()
1670 * Align this alignment like the given (mapped) one.
1673 public int alignAs(AlignmentI al)
1676 * Currently retains unmapped gaps (in introns), regaps mapped regions
1679 return alignAs(al, false, true);
1683 * Align this alignment 'the same as' the given one. Mapped sequences only are
1684 * realigned. If both of the same type (nucleotide/protein) then align both
1685 * identically. If this is nucleotide and the other is protein, make 3 gaps
1686 * for each gap in the protein sequences. If this is protein and the other is
1687 * nucleotide, insert a gap for each 3 gaps (or part thereof) between
1688 * nucleotide bases. If this is protein and the other is nucleotide, gaps
1689 * protein to match the relative ordering of codons in the nucleotide.
1691 * Parameters control whether gaps in exon (mapped) and intron (unmapped)
1692 * regions are preserved. Gaps that connect introns to exons are treated
1693 * conservatively, i.e. only preserved if both intron and exon gaps are
1697 * @param preserveMappedGaps
1698 * if true, gaps within and between mapped codons are preserved
1699 * @param preserveUnmappedGaps
1700 * if true, gaps within and between unmapped codons are preserved
1703 public int alignAs(AlignmentI al, boolean preserveMappedGaps,
1704 boolean preserveUnmappedGaps)
1706 // TODO should this method signature be the one in the interface?
1707 boolean thisIsNucleotide = this.isNucleotide();
1708 boolean thatIsProtein = !al.isNucleotide();
1709 if (!thatIsProtein && !thisIsNucleotide)
1711 return AlignmentUtils.alignProteinAsDna(this, al);
1713 return AlignmentUtils.alignAs(this, al);
1717 * Returns the alignment in Fasta format. Behaviour of this method is not
1718 * guaranteed between versions.
1721 public String toString()
1723 return new FastaFile().print(getSequencesArray());
1727 * Returns the set of distinct sequence names. No ordering is guaranteed.
1730 public Set<String> getSequenceNames()
1732 Set<String> names = new HashSet<String>();
1733 for (SequenceI seq : getSequences())
1735 names.add(seq.getName());
1741 public boolean hasValidSequence()
1743 boolean hasValidSeq = false;
1744 for (SequenceI seq : getSequences())
1746 if ((seq.getEnd() - seq.getStart()) > 0)
1756 * Update any mappings to 'virtual' sequences to compatible real ones, if
1757 * present in the added sequences. Returns a count of mappings updated.
1763 public int realiseMappings(List<SequenceI> seqs)
1766 for (SequenceI seq : seqs)
1768 for (AlignedCodonFrame mapping : getCodonFrames())
1770 count += mapping.realiseWith(seq);
1777 * Returns the first AlignedCodonFrame that has a mapping between the given
1785 public AlignedCodonFrame getMapping(SequenceI mapFrom, SequenceI mapTo)
1787 for (AlignedCodonFrame acf : getCodonFrames())
1789 if (acf.getAaForDnaSeq(mapFrom) == mapTo)
1798 public int[] getVisibleStartAndEndIndex(List<int[]> hiddenCols)
1800 int[] alignmentStartEnd = new int[] { 0, getWidth() - 1 };
1801 int startPos = alignmentStartEnd[0];
1802 int endPos = alignmentStartEnd[1];
1804 int[] lowestRange = new int[] { -1, -1 };
1805 int[] higestRange = new int[] { -1, -1 };
1807 for (int[] hiddenCol : hiddenCols)
1809 lowestRange = (hiddenCol[0] <= startPos) ? hiddenCol : lowestRange;
1810 higestRange = (hiddenCol[1] >= endPos) ? hiddenCol : higestRange;
1813 if (lowestRange[0] == -1 && lowestRange[1] == -1)
1815 startPos = alignmentStartEnd[0];
1819 startPos = lowestRange[1] + 1;
1822 if (higestRange[0] == -1 && higestRange[1] == -1)
1824 endPos = alignmentStartEnd[1];
1828 endPos = higestRange[0] - 1;
1830 return new int[] { startPos, endPos };