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.Iterator;
34 import java.util.List;
37 import java.util.Vector;
40 * Data structure to hold and manipulate a multiple sequence alignment
46 public class Alignment implements AlignmentI
48 private Alignment dataset;
50 protected List<SequenceI> sequences;
52 protected List<SequenceGroup> groups;
54 protected char gapCharacter = '-';
56 protected int type = NUCLEOTIDE;
58 public static final int PROTEIN = 0;
60 public static final int NUCLEOTIDE = 1;
62 public boolean hasRNAStructure = false;
64 public AlignmentAnnotation[] annotations;
66 HiddenSequences hiddenSequences;
68 public Hashtable alignmentProperties;
70 private List<AlignedCodonFrame> codonFrameList;
72 private void initAlignment(SequenceI[] seqs)
74 groups = Collections.synchronizedList(new ArrayList<SequenceGroup>());
75 hiddenSequences = new HiddenSequences(this);
76 codonFrameList = new ArrayList<AlignedCodonFrame>();
78 if (Comparison.isNucleotide(seqs))
87 sequences = Collections.synchronizedList(new ArrayList<SequenceI>());
89 for (int i = 0; i < seqs.length; i++)
91 sequences.add(seqs[i]);
97 * Make a 'copy' alignment - sequences have new copies of features and
98 * annotations, but share the original dataset sequences.
100 public Alignment(AlignmentI al)
102 SequenceI[] seqs = al.getSequencesArray();
103 for (int i = 0; i < seqs.length; i++)
105 seqs[i] = new Sequence(seqs[i]);
111 * Share the same dataset sequence mappings (if any).
113 if (dataset == null && al.getDataset() == null)
115 this.setCodonFrames(al.getCodonFrames());
120 * Make an alignment from an array of Sequences.
124 public Alignment(SequenceI[] seqs)
130 * Make a new alignment from an array of SeqCigars
135 public Alignment(SeqCigar[] alseqs)
137 SequenceI[] seqs = SeqCigar.createAlignmentSequences(alseqs,
138 gapCharacter, new ColumnSelection(), null);
143 * Make a new alignment from an CigarArray JBPNote - can only do this when
144 * compactAlignment does not contain hidden regions. JBPNote - must also check
145 * that compactAlignment resolves to a set of SeqCigars - or construct them
148 * @param compactAlignment
151 public static AlignmentI createAlignment(CigarArray compactAlignment)
155 .getString("error.alignment_cigararray_not_implemented"));
156 // this(compactAlignment.refCigars);
160 public List<SequenceI> getSequences()
166 public List<SequenceI> getSequences(
167 Map<SequenceI, SequenceCollectionI> hiddenReps)
169 // TODO: in jalview 2.8 we don't do anything with hiddenreps - fix design to
175 public SequenceI[] getSequencesArray()
177 if (sequences == null)
181 synchronized (sequences)
183 return sequences.toArray(new SequenceI[sequences.size()]);
188 * Returns a map of lists of sequences keyed by sequence name.
193 public Map<String, List<SequenceI>> getSequencesByName()
195 return AlignmentUtils.getSequencesByName(this);
204 * @return DOCUMENT ME!
207 public SequenceI getSequenceAt(int i)
209 synchronized (sequences)
211 if (i > -1 && i < sequences.size())
213 return sequences.get(i);
220 * Adds a sequence to the alignment. Recalculates maxLength and size.
225 public void addSequence(SequenceI snew)
229 // maintain dataset integrity
230 if (snew.getDatasetSequence() != null)
232 getDataset().addSequence(snew.getDatasetSequence());
236 // derive new sequence
237 SequenceI adding = snew.deriveSequence();
238 getDataset().addSequence(adding.getDatasetSequence());
242 if (sequences == null)
244 initAlignment(new SequenceI[] { snew });
248 synchronized (sequences)
253 if (hiddenSequences != null)
255 hiddenSequences.adjustHeightSequenceAdded();
260 * Adds a sequence to the alignment. Recalculates maxLength and size.
265 public void setSequenceAt(int i, SequenceI snew)
267 synchronized (sequences)
270 sequences.set(i, snew);
277 * @return DOCUMENT ME!
280 public List<SequenceGroup> getGroups()
286 public void finalize()
288 if (getDataset() != null)
290 getDataset().removeAlignmentRef();
297 hiddenSequences = null;
301 * decrement the alignmentRefs counter by one and call finalize if it goes to
304 private void removeAlignmentRef()
306 if (--alignmentRefs == 0)
319 public void deleteSequence(SequenceI s)
321 deleteSequence(findIndex(s));
331 public void deleteSequence(int i)
333 if (i > -1 && i < getHeight())
335 synchronized (sequences)
338 hiddenSequences.adjustHeightSequenceDeleted(i);
346 * @see jalview.datamodel.AlignmentI#findGroup(jalview.datamodel.SequenceI)
349 public SequenceGroup findGroup(SequenceI s)
351 synchronized (groups)
353 for (int i = 0; i < this.groups.size(); i++)
355 SequenceGroup sg = groups.get(i);
357 if (sg.getSequences(null).contains(s))
370 * jalview.datamodel.AlignmentI#findAllGroups(jalview.datamodel.SequenceI)
373 public SequenceGroup[] findAllGroups(SequenceI s)
375 ArrayList<SequenceGroup> temp = new ArrayList<SequenceGroup>();
377 synchronized (groups)
379 int gSize = groups.size();
380 for (int i = 0; i < gSize; i++)
382 SequenceGroup sg = groups.get(i);
383 if (sg == null || sg.getSequences() == null)
385 this.deleteGroup(sg);
390 if (sg.getSequences().contains(s))
396 SequenceGroup[] ret = new SequenceGroup[temp.size()];
397 return temp.toArray(ret);
402 public void addGroup(SequenceGroup sg)
404 synchronized (groups)
406 if (!groups.contains(sg))
408 if (hiddenSequences.getSize() > 0)
410 int i, iSize = sg.getSize();
411 for (i = 0; i < iSize; i++)
413 if (!sequences.contains(sg.getSequenceAt(i)))
415 sg.deleteSequence(sg.getSequenceAt(i), false);
421 if (sg.getSize() < 1)
433 * remove any annotation that references gp
436 * (if null, removes all group associated annotation)
438 private void removeAnnotationForGroup(SequenceGroup gp)
440 if (annotations == null || annotations.length == 0)
444 // remove annotation very quickly
445 AlignmentAnnotation[] t, todelete = new AlignmentAnnotation[annotations.length], tokeep = new AlignmentAnnotation[annotations.length];
449 for (i = 0, p = 0, k = 0; i < annotations.length; i++)
451 if (annotations[i].groupRef != null)
453 todelete[p++] = annotations[i];
457 tokeep[k++] = annotations[i];
463 for (i = 0, p = 0, k = 0; i < annotations.length; i++)
465 if (annotations[i].groupRef == gp)
467 todelete[p++] = annotations[i];
471 tokeep[k++] = annotations[i];
477 // clear out the group associated annotation.
478 for (i = 0; i < p; i++)
480 unhookAnnotation(todelete[i]);
483 t = new AlignmentAnnotation[k];
484 for (i = 0; i < k; i++)
493 public void deleteAllGroups()
495 synchronized (groups)
497 if (annotations != null)
499 removeAnnotationForGroup(null);
501 for (SequenceGroup sg : groups)
511 public void deleteGroup(SequenceGroup g)
513 synchronized (groups)
515 if (groups.contains(g))
517 removeAnnotationForGroup(g);
526 public SequenceI findName(String name)
528 return findName(name, false);
534 * @see jalview.datamodel.AlignmentI#findName(java.lang.String, boolean)
537 public SequenceI findName(String token, boolean b)
539 return findName(null, token, b);
545 * @see jalview.datamodel.AlignmentI#findName(SequenceI, java.lang.String,
549 public SequenceI findName(SequenceI startAfter, String token, boolean b)
554 String sqname = null;
555 if (startAfter != null)
557 // try to find the sequence in the alignment
558 boolean matched = false;
559 while (i < sequences.size())
561 if (getSequenceAt(i++) == startAfter)
572 while (i < sequences.size())
574 sq = getSequenceAt(i);
575 sqname = sq.getName();
576 if (sqname.equals(token) // exact match
577 || (b && // allow imperfect matches - case varies
578 (sqname.equalsIgnoreCase(token))))
580 return getSequenceAt(i);
590 public SequenceI[] findSequenceMatch(String name)
592 Vector matches = new Vector();
595 while (i < sequences.size())
597 if (getSequenceAt(i).getName().equals(name))
599 matches.addElement(getSequenceAt(i));
604 SequenceI[] result = new SequenceI[matches.size()];
605 for (i = 0; i < result.length; i++)
607 result[i] = (SequenceI) matches.elementAt(i);
617 * @see jalview.datamodel.AlignmentI#findIndex(jalview.datamodel.SequenceI)
620 public int findIndex(SequenceI s)
624 while (i < sequences.size())
626 if (s == getSequenceAt(i))
641 * jalview.datamodel.AlignmentI#findIndex(jalview.datamodel.SearchResults)
644 public int findIndex(SearchResults results)
648 while (i < sequences.size())
650 if (results.involvesSequence(getSequenceAt(i)))
662 * @return DOCUMENT ME!
665 public int getHeight()
667 return sequences.size();
673 * @return DOCUMENT ME!
676 public int getWidth()
680 for (int i = 0; i < sequences.size(); i++)
682 if (getSequenceAt(i).getLength() > maxLength)
684 maxLength = getSequenceAt(i).getLength();
698 public void setGapCharacter(char gc)
701 synchronized (sequences)
703 for (SequenceI seq : sequences)
705 seq.setSequence(seq.getSequenceAsString().replace('.', gc)
706 .replace('-', gc).replace(' ', gc));
714 * @return DOCUMENT ME!
717 public char getGapCharacter()
725 * @see jalview.datamodel.AlignmentI#isAligned()
728 public boolean isAligned()
730 return isAligned(false);
736 * @see jalview.datamodel.AlignmentI#isAligned(boolean)
739 public boolean isAligned(boolean includeHidden)
741 int width = getWidth();
742 if (hiddenSequences == null || hiddenSequences.getSize() == 0)
744 includeHidden = true; // no hidden sequences to check against.
746 for (int i = 0; i < sequences.size(); i++)
748 if (includeHidden || !hiddenSequences.isHidden(getSequenceAt(i)))
750 if (getSequenceAt(i).getLength() != width)
761 * Delete all annotations, including auto-calculated if the flag is set true.
762 * Returns true if at least one annotation was deleted, else false.
764 * @param includingAutoCalculated
768 public boolean deleteAllAnnotations(boolean includingAutoCalculated)
770 boolean result = false;
771 for (AlignmentAnnotation alan : getAlignmentAnnotation())
773 if (!alan.autoCalculated || includingAutoCalculated)
775 deleteAnnotation(alan);
785 * @seejalview.datamodel.AlignmentI#deleteAnnotation(jalview.datamodel.
786 * AlignmentAnnotation)
789 public boolean deleteAnnotation(AlignmentAnnotation aa)
791 return deleteAnnotation(aa, true);
795 public boolean deleteAnnotation(AlignmentAnnotation aa, boolean unhook)
799 if (annotations != null)
801 aSize = annotations.length;
809 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize - 1];
811 boolean swap = false;
814 for (int i = 0; i < aSize; i++)
816 if (annotations[i] == aa)
821 if (tIndex < temp.length)
823 temp[tIndex++] = annotations[i];
832 unhookAnnotation(aa);
839 * remove any object references associated with this annotation
843 private void unhookAnnotation(AlignmentAnnotation aa)
845 if (aa.sequenceRef != null)
847 aa.sequenceRef.removeAlignmentAnnotation(aa);
849 if (aa.groupRef != null)
851 // probably need to do more here in the future (post 2.5.0)
859 * @seejalview.datamodel.AlignmentI#addAnnotation(jalview.datamodel.
860 * AlignmentAnnotation)
863 public void addAnnotation(AlignmentAnnotation aa)
865 addAnnotation(aa, -1);
871 * @seejalview.datamodel.AlignmentI#addAnnotation(jalview.datamodel.
872 * AlignmentAnnotation, int)
875 public void addAnnotation(AlignmentAnnotation aa, int pos)
877 if (aa.getRNAStruc() != null)
879 hasRNAStructure = true;
883 if (annotations != null)
885 aSize = annotations.length + 1;
888 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
890 if (pos == -1 || pos >= aSize)
892 temp[aSize - 1] = aa;
901 for (i = 0; i < (aSize - 1); i++, p++)
909 temp[p] = annotations[i];
918 public void setAnnotationIndex(AlignmentAnnotation aa, int index)
920 if (aa == null || annotations == null || annotations.length - 1 < index)
925 int aSize = annotations.length;
926 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
930 for (int i = 0; i < aSize; i++)
939 temp[i] = annotations[i];
943 temp[i] = annotations[i - 1];
952 * returns all annotation on the alignment
954 public AlignmentAnnotation[] getAlignmentAnnotation()
960 public void setNucleotide(boolean b)
973 public boolean isNucleotide()
975 if (type == NUCLEOTIDE)
986 public boolean hasRNAStructure()
988 // TODO can it happen that structure is removed from alignment?
989 return hasRNAStructure;
993 public void setDataset(AlignmentI data)
995 if (dataset == null && data == null)
997 createDatasetAlignment();
999 else if (dataset == null && data != null)
1001 if (!(data instanceof Alignment))
1004 "Implementation Error: jalview.datamodel.Alignment does not yet support other implementations of AlignmentI as its dataset reference");
1006 dataset = (Alignment) data;
1007 for (int i = 0; i < getHeight(); i++)
1009 SequenceI currentSeq = getSequenceAt(i);
1010 SequenceI dsq = currentSeq.getDatasetSequence();
1013 dsq = currentSeq.createDatasetSequence();
1014 dataset.addSequence(dsq);
1018 while (dsq.getDatasetSequence() != null)
1020 dsq = dsq.getDatasetSequence();
1022 if (dataset.findIndex(dsq) == -1)
1024 dataset.addSequence(dsq);
1029 dataset.addAlignmentRef();
1033 * Creates a new dataset for this alignment. Can only be done once - if
1034 * dataset is not null this will not be performed.
1036 public void createDatasetAlignment()
1038 if (dataset != null)
1042 SequenceI[] seqs = new SequenceI[getHeight()];
1043 SequenceI currentSeq;
1044 for (int i = 0; i < getHeight(); i++)
1046 currentSeq = getSequenceAt(i);
1047 if (currentSeq.getDatasetSequence() != null)
1049 seqs[i] = currentSeq.getDatasetSequence();
1053 seqs[i] = currentSeq.createDatasetSequence();
1057 dataset = new Alignment(seqs);
1058 // move mappings to the dataset alignment
1059 dataset.codonFrameList = this.codonFrameList;
1060 this.codonFrameList = null;
1064 * reference count for number of alignments referencing this one.
1066 int alignmentRefs = 0;
1069 * increase reference count to this alignment.
1071 private void addAlignmentRef()
1077 public Alignment getDataset()
1083 public boolean padGaps()
1085 boolean modified = false;
1087 // Remove excess gaps from the end of alignment
1091 for (int i = 0; i < sequences.size(); i++)
1093 current = getSequenceAt(i);
1094 for (int j = current.getLength(); j > maxLength; j--)
1097 && !jalview.util.Comparison.isGap(current.getCharAt(j)))
1108 for (int i = 0; i < sequences.size(); i++)
1110 current = getSequenceAt(i);
1111 cLength = current.getLength();
1113 if (cLength < maxLength)
1115 current.insertCharAt(cLength, maxLength - cLength, gapCharacter);
1118 else if (current.getLength() > maxLength)
1120 current.deleteChars(maxLength, current.getLength());
1127 * Justify the sequences to the left or right by deleting and inserting gaps
1128 * before the initial residue or after the terminal residue
1131 * true if alignment padded to right, false to justify to left
1132 * @return true if alignment was changed
1135 public boolean justify(boolean right)
1137 boolean modified = false;
1139 // Remove excess gaps from the end of alignment
1141 int ends[] = new int[sequences.size() * 2];
1143 for (int i = 0; i < sequences.size(); i++)
1145 current = getSequenceAt(i);
1146 // This should really be a sequence method
1147 ends[i * 2] = current.findIndex(current.getStart());
1148 ends[i * 2 + 1] = current.findIndex(current.getStart()
1149 + current.getLength());
1150 boolean hitres = false;
1151 for (int j = 0, rs = 0, ssiz = current.getLength(); j < ssiz; j++)
1153 if (!jalview.util.Comparison.isGap(current.getCharAt(j)))
1162 ends[i * 2 + 1] = j;
1163 if (j - ends[i * 2] > maxLength)
1165 maxLength = j - ends[i * 2];
1173 // now edit the flanking gaps to justify to either left or right
1174 int cLength, extent, diff;
1175 for (int i = 0; i < sequences.size(); i++)
1177 current = getSequenceAt(i);
1179 cLength = 1 + ends[i * 2 + 1] - ends[i * 2];
1180 diff = maxLength - cLength; // number of gaps to indent
1181 extent = current.getLength();
1185 if (extent > ends[i * 2 + 1])
1187 current.deleteChars(ends[i * 2 + 1] + 1, extent);
1190 if (ends[i * 2] > diff)
1192 current.deleteChars(0, ends[i * 2] - diff);
1197 if (ends[i * 2] < diff)
1199 current.insertCharAt(0, diff - ends[i * 2], gapCharacter);
1207 if (ends[i * 2] > 0)
1209 current.deleteChars(0, ends[i * 2]);
1211 ends[i * 2 + 1] -= ends[i * 2];
1212 extent -= ends[i * 2];
1214 if (extent > maxLength)
1216 current.deleteChars(maxLength + 1, extent);
1221 if (extent < maxLength)
1223 current.insertCharAt(extent, maxLength - extent, gapCharacter);
1233 public HiddenSequences getHiddenSequences()
1235 return hiddenSequences;
1239 public CigarArray getCompactAlignment()
1241 synchronized (sequences)
1243 SeqCigar alseqs[] = new SeqCigar[sequences.size()];
1245 for (SequenceI seq : sequences)
1247 alseqs[i++] = new SeqCigar(seq);
1249 CigarArray cal = new CigarArray(alseqs);
1250 cal.addOperation(CigarArray.M, getWidth());
1256 public void setProperty(Object key, Object value)
1258 if (alignmentProperties == null)
1260 alignmentProperties = new Hashtable();
1263 alignmentProperties.put(key, value);
1267 public Object getProperty(Object key)
1269 if (alignmentProperties != null)
1271 return alignmentProperties.get(key);
1280 public Hashtable getProperties()
1282 return alignmentProperties;
1286 * Adds the given mapping to the stored set. Note this may be held on the
1287 * dataset alignment.
1290 public void addCodonFrame(AlignedCodonFrame codons)
1292 List<AlignedCodonFrame> acfs = getCodonFrames();
1293 if (codons != null && acfs != null && !acfs.contains(codons))
1303 * jalview.datamodel.AlignmentI#getCodonFrame(jalview.datamodel.SequenceI)
1306 public List<AlignedCodonFrame> getCodonFrame(SequenceI seq)
1312 List<AlignedCodonFrame> cframes = new ArrayList<AlignedCodonFrame>();
1313 for (AlignedCodonFrame acf : getCodonFrames())
1315 if (acf.involvesSequence(seq))
1324 * Sets the codon frame mappings (replacing any existing mappings). Note the
1325 * mappings are set on the dataset alignment instead if there is one.
1327 * @see jalview.datamodel.AlignmentI#setCodonFrames()
1330 public void setCodonFrames(List<AlignedCodonFrame> acfs)
1332 if (dataset != null)
1334 dataset.setCodonFrames(acfs);
1338 this.codonFrameList = acfs;
1343 * Returns the set of codon frame mappings. Any changes to the returned set
1344 * will affect the alignment. The mappings are held on (and read from) the
1345 * dataset alignment if there is one.
1347 * @see jalview.datamodel.AlignmentI#getCodonFrames()
1350 public List<AlignedCodonFrame> getCodonFrames()
1352 // TODO: Fix this method to fix failing AlignedCodonFrame tests
1353 // this behaviour is currently incorrect. method should return codon frames
1354 // for just the alignment,
1355 // selected from dataset
1356 return dataset != null ? dataset.getCodonFrames() : codonFrameList;
1360 * Removes the given mapping from the stored set. Note that the mappings are
1361 * held on the dataset alignment if there is one.
1364 public boolean removeCodonFrame(AlignedCodonFrame codons)
1366 List<AlignedCodonFrame> acfs = getCodonFrames();
1367 if (codons == null || acfs == null)
1371 return acfs.remove(codons);
1375 public void append(AlignmentI toappend)
1377 if (toappend == this)
1379 System.err.println("Self append may cause a deadlock.");
1381 // TODO test this method for a future 2.5 release
1382 // currently tested for use in jalview.gui.SequenceFetcher
1383 boolean samegap = toappend.getGapCharacter() == getGapCharacter();
1384 char oldc = toappend.getGapCharacter();
1385 boolean hashidden = toappend.getHiddenSequences() != null
1386 && toappend.getHiddenSequences().hiddenSequences != null;
1387 // get all sequences including any hidden ones
1388 List<SequenceI> sqs = (hashidden) ? toappend.getHiddenSequences()
1389 .getFullAlignment().getSequences() : toappend.getSequences();
1394 for (SequenceI addedsq : sqs)
1398 char[] oldseq = addedsq.getSequence();
1399 for (int c = 0; c < oldseq.length; c++)
1401 if (oldseq[c] == oldc)
1403 oldseq[c] = gapCharacter;
1407 addSequence(addedsq);
1411 AlignmentAnnotation[] alan = toappend.getAlignmentAnnotation();
1412 for (int a = 0; alan != null && a < alan.length; a++)
1414 addAnnotation(alan[a]);
1418 getCodonFrames().addAll(toappend.getCodonFrames());
1420 List<SequenceGroup> sg = toappend.getGroups();
1423 for (SequenceGroup _sg : sg)
1428 if (toappend.getHiddenSequences() != null)
1430 HiddenSequences hs = toappend.getHiddenSequences();
1431 if (hiddenSequences == null)
1433 hiddenSequences = new HiddenSequences(this);
1435 if (hs.hiddenSequences != null)
1437 for (int s = 0; s < hs.hiddenSequences.length; s++)
1439 // hide the newly appended sequence in the alignment
1440 if (hs.hiddenSequences[s] != null)
1442 hiddenSequences.hideSequence(hs.hiddenSequences[s]);
1447 if (toappend.getProperties() != null)
1449 // we really can't do very much here - just try to concatenate strings
1450 // where property collisions occur.
1451 Enumeration key = toappend.getProperties().keys();
1452 while (key.hasMoreElements())
1454 Object k = key.nextElement();
1455 Object ourval = this.getProperty(k);
1456 Object toapprop = toappend.getProperty(k);
1459 if (ourval.getClass().equals(toapprop.getClass())
1460 && !ourval.equals(toapprop))
1462 if (ourval instanceof String)
1465 this.setProperty(k, ((String) ourval) + "; "
1466 + ((String) toapprop));
1470 if (ourval instanceof Vector)
1473 Enumeration theirv = ((Vector) toapprop).elements();
1474 while (theirv.hasMoreElements())
1476 ((Vector) ourval).addElement(theirv);
1484 // just add new property directly
1485 setProperty(k, toapprop);
1493 public AlignmentAnnotation findOrCreateAnnotation(String name,
1494 String calcId, boolean autoCalc, SequenceI seqRef,
1495 SequenceGroup groupRef)
1497 assert (name != null);
1498 if (annotations != null)
1500 for (AlignmentAnnotation annot : getAlignmentAnnotation())
1502 if (annot.autoCalculated == autoCalc && (name.equals(annot.label))
1503 && (calcId == null || annot.getCalcId().equals(calcId))
1504 && annot.sequenceRef == seqRef
1505 && annot.groupRef == groupRef)
1511 AlignmentAnnotation annot = new AlignmentAnnotation(name, name,
1512 new Annotation[1], 0f, 0f, AlignmentAnnotation.BAR_GRAPH);
1513 annot.hasText = false;
1514 annot.setCalcId(new String(calcId));
1515 annot.autoCalculated = autoCalc;
1518 annot.setSequenceRef(seqRef);
1520 annot.groupRef = groupRef;
1521 addAnnotation(annot);
1527 public Iterable<AlignmentAnnotation> findAnnotation(String calcId)
1529 ArrayList<AlignmentAnnotation> aa = new ArrayList<AlignmentAnnotation>();
1530 for (AlignmentAnnotation a : getAlignmentAnnotation())
1532 if (a.getCalcId() == calcId
1533 || (a.getCalcId() != null && calcId != null && a.getCalcId()
1543 * Returns an iterable collection of any annotations that match on given
1544 * sequence ref, calcId and label (ignoring null values).
1547 public Iterable<AlignmentAnnotation> findAnnotations(SequenceI seq,
1548 String calcId, String label)
1550 ArrayList<AlignmentAnnotation> aa = new ArrayList<AlignmentAnnotation>();
1551 for (AlignmentAnnotation ann : getAlignmentAnnotation())
1553 if (ann.getCalcId() != null && ann.getCalcId().equals(calcId)
1554 && ann.sequenceRef != null && ann.sequenceRef == seq
1555 && ann.label != null && ann.label.equals(label))
1564 public void moveSelectedSequencesByOne(SequenceGroup sg,
1565 Map<SequenceI, SequenceCollectionI> map, boolean up)
1567 synchronized (sequences)
1572 for (int i = 1, iSize = sequences.size(); i < iSize; i++)
1574 SequenceI seq = sequences.get(i);
1575 if (!sg.getSequences(map).contains(seq))
1580 SequenceI temp = sequences.get(i - 1);
1581 if (sg.getSequences(null).contains(temp))
1586 sequences.set(i, temp);
1587 sequences.set(i - 1, seq);
1592 for (int i = sequences.size() - 2; i > -1; i--)
1594 SequenceI seq = sequences.get(i);
1595 if (!sg.getSequences(map).contains(seq))
1600 SequenceI temp = sequences.get(i + 1);
1601 if (sg.getSequences(map).contains(temp))
1606 sequences.set(i, temp);
1607 sequences.set(i + 1, seq);
1615 public void validateAnnotation(AlignmentAnnotation alignmentAnnotation)
1617 alignmentAnnotation.validateRangeAndDisplay();
1618 if (isNucleotide() && alignmentAnnotation.isValidStruc())
1620 hasRNAStructure = true;
1624 private SequenceI seqrep = null;
1628 * @return the representative sequence for this group
1631 public SequenceI getSeqrep()
1637 * set the representative sequence for this group. Note - this affects the
1638 * interpretation of the Hidereps attribute.
1641 * the seqrep to set (null means no sequence representative)
1644 public void setSeqrep(SequenceI seqrep)
1646 this.seqrep = seqrep;
1651 * @return true if group has a sequence representative
1654 public boolean hasSeqrep()
1656 return seqrep != null;
1660 public int getEndRes()
1662 return getWidth() - 1;
1666 public int getStartRes()
1672 * In the case of AlignmentI - returns the dataset for the alignment, if set
1675 * @see jalview.datamodel.AnnotatedCollectionI#getContext()
1678 public AnnotatedCollectionI getContext()
1684 * Align this alignment like the given (mapped) one.
1687 public int alignAs(AlignmentI al)
1690 * Currently retains unmapped gaps (in introns), regaps mapped regions
1693 return alignAs(al, false, true);
1697 * Align this alignment 'the same as' the given one. Mapped sequences only are
1698 * realigned. If both of the same type (nucleotide/protein) then align both
1699 * identically. If this is nucleotide and the other is protein, make 3 gaps
1700 * for each gap in the protein sequences. If this is protein and the other is
1701 * nucleotide, insert a gap for each 3 gaps (or part thereof) between
1702 * nucleotide bases. If this is protein and the other is nucleotide, gaps
1703 * protein to match the relative ordering of codons in the nucleotide.
1705 * Parameters control whether gaps in exon (mapped) and intron (unmapped)
1706 * regions are preserved. Gaps that connect introns to exons are treated
1707 * conservatively, i.e. only preserved if both intron and exon gaps are
1711 * @param preserveMappedGaps
1712 * if true, gaps within and between mapped codons are preserved
1713 * @param preserveUnmappedGaps
1714 * if true, gaps within and between unmapped codons are preserved
1717 public int alignAs(AlignmentI al, boolean preserveMappedGaps,
1718 boolean preserveUnmappedGaps)
1720 // TODO should this method signature be the one in the interface?
1721 boolean thisIsNucleotide = this.isNucleotide();
1722 boolean thatIsProtein = !al.isNucleotide();
1723 if (!thatIsProtein && !thisIsNucleotide)
1725 return AlignmentUtils.alignProteinAsDna(this, al);
1727 return AlignmentUtils.alignAs(this, al);
1731 * Returns the alignment in Fasta format. Behaviour of this method is not
1732 * guaranteed between versions.
1735 public String toString()
1737 return new FastaFile().print(getSequencesArray());
1741 * Returns the set of distinct sequence names. No ordering is guaranteed.
1744 public Set<String> getSequenceNames()
1746 Set<String> names = new HashSet<String>();
1747 for (SequenceI seq : getSequences())
1749 names.add(seq.getName());
1755 public boolean hasValidSequence()
1757 boolean hasValidSeq = false;
1758 for (SequenceI seq : getSequences())
1760 if ((seq.getEnd() - seq.getStart()) > 0)
1770 * Update any mappings to 'virtual' sequences to compatible real ones, if
1771 * present in the added sequences. Returns a count of mappings updated.
1777 public int realiseMappings(List<SequenceI> seqs)
1780 for (SequenceI seq : seqs)
1782 for (AlignedCodonFrame mapping : getCodonFrames())
1784 count += mapping.realiseWith(seq);
1791 * Returns the first AlignedCodonFrame that has a mapping between the given
1799 public AlignedCodonFrame getMapping(SequenceI mapFrom, SequenceI mapTo)
1801 for (AlignedCodonFrame acf : getCodonFrames())
1803 if (acf.getAaForDnaSeq(mapFrom) == mapTo)
1812 public int[] getVisibleStartAndEndIndex(List<int[]> hiddenCols)
1814 int[] alignmentStartEnd = new int[] { 0, getWidth() - 1 };
1815 int startPos = alignmentStartEnd[0];
1816 int endPos = alignmentStartEnd[1];
1818 int[] lowestRange = new int[] { -1, -1 };
1819 int[] higestRange = new int[] { -1, -1 };
1821 for (int[] hiddenCol : hiddenCols)
1823 lowestRange = (hiddenCol[0] <= startPos) ? hiddenCol : lowestRange;
1824 higestRange = (hiddenCol[1] >= endPos) ? hiddenCol : higestRange;
1827 if (lowestRange[0] == -1 && lowestRange[1] == -1)
1829 startPos = alignmentStartEnd[0];
1833 startPos = lowestRange[1] + 1;
1836 if (higestRange[0] == -1 && higestRange[1] == -1)
1838 endPos = alignmentStartEnd[1];
1842 endPos = higestRange[0] - 1;
1844 return new int[] { startPos, endPos };