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.datamodel.AlignedCodonFrame.SequenceToSequenceMapping;
25 import jalview.io.FastaFile;
26 import jalview.util.Comparison;
27 import jalview.util.LinkedIdentityHashSet;
28 import jalview.util.MessageManager;
30 import java.util.ArrayList;
31 import java.util.Arrays;
32 import java.util.Collections;
33 import java.util.Enumeration;
34 import java.util.HashSet;
35 import java.util.Hashtable;
36 import java.util.List;
39 import java.util.Vector;
42 * Data structure to hold and manipulate a multiple sequence alignment
48 public class Alignment implements AlignmentI
50 private Alignment dataset;
52 protected List<SequenceI> sequences;
54 protected List<SequenceGroup> groups;
56 protected char gapCharacter = '-';
58 private boolean nucleotide = true;
60 public boolean hasRNAStructure = false;
62 public AlignmentAnnotation[] annotations;
64 HiddenSequences hiddenSequences;
66 HiddenColumns hiddenCols;
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 hiddenCols = new HiddenColumns();
77 codonFrameList = new ArrayList<>();
79 nucleotide = Comparison.isNucleotide(seqs);
81 sequences = Collections.synchronizedList(new ArrayList<SequenceI>());
83 for (int i = 0; i < seqs.length; i++)
85 sequences.add(seqs[i]);
91 * Make a 'copy' alignment - sequences have new copies of features and
92 * annotations, but share the original dataset sequences.
94 public Alignment(AlignmentI al)
96 SequenceI[] seqs = al.getSequencesArray();
97 for (int i = 0; i < seqs.length; i++)
99 seqs[i] = new Sequence(seqs[i]);
105 * Share the same dataset sequence mappings (if any).
107 if (dataset == null && al.getDataset() == null)
109 this.setCodonFrames(al.getCodonFrames());
114 * Make an alignment from an array of Sequences.
118 public Alignment(SequenceI[] seqs)
124 * Make a new alignment from an array of SeqCigars
129 public Alignment(SeqCigar[] alseqs)
131 SequenceI[] seqs = SeqCigar.createAlignmentSequences(alseqs,
132 gapCharacter, new HiddenColumns(), null);
137 * Make a new alignment from an CigarArray JBPNote - can only do this when
138 * compactAlignment does not contain hidden regions. JBPNote - must also check
139 * that compactAlignment resolves to a set of SeqCigars - or construct them
142 * @param compactAlignment
145 public static AlignmentI createAlignment(CigarArray compactAlignment)
147 throw new Error(MessageManager
148 .getString("error.alignment_cigararray_not_implemented"));
149 // this(compactAlignment.refCigars);
153 public List<SequenceI> getSequences()
159 public List<SequenceI> getSequences(
160 Map<SequenceI, SequenceCollectionI> hiddenReps)
162 // TODO: in jalview 2.8 we don't do anything with hiddenreps - fix design to
168 public SequenceI[] getSequencesArray()
170 if (sequences == null)
174 synchronized (sequences)
176 return sequences.toArray(new SequenceI[sequences.size()]);
181 * Returns a map of lists of sequences keyed by sequence name.
186 public Map<String, List<SequenceI>> getSequencesByName()
188 return AlignmentUtils.getSequencesByName(this);
192 public SequenceI getSequenceAt(int i)
194 synchronized (sequences)
196 if (i > -1 && i < sequences.size())
198 return sequences.get(i);
205 public SequenceI getSequenceAtAbsoluteIndex(int i)
207 SequenceI seq = null;
208 if (getHiddenSequences().getSize() > 0)
210 seq = getHiddenSequences().getHiddenSequence(i);
213 // didn't find the sequence in the hidden sequences, get it from the
215 int index = getHiddenSequences().findIndexWithoutHiddenSeqs(i);
216 seq = getSequenceAt(index);
221 seq = getSequenceAt(i);
227 * Adds a sequence to the alignment. Recalculates maxLength and size. Note
228 * this currently does not recalculate whether or not the alignment is
229 * nucleotide, so mixed alignments may have undefined behaviour.
234 public void addSequence(SequenceI snew)
239 // maintain dataset integrity
240 SequenceI dsseq = snew.getDatasetSequence();
243 // derive new sequence
244 SequenceI adding = snew.deriveSequence();
246 dsseq = snew.getDatasetSequence();
248 if (getDataset().findIndex(dsseq) == -1)
250 getDataset().addSequence(dsseq);
254 if (sequences == null)
256 initAlignment(new SequenceI[] { snew });
260 synchronized (sequences)
265 if (hiddenSequences != null)
267 hiddenSequences.adjustHeightSequenceAdded();
272 public SequenceI replaceSequenceAt(int i, SequenceI snew)
274 synchronized (sequences)
276 if (sequences.size() > i)
278 return sequences.set(i, snew);
284 hiddenSequences.adjustHeightSequenceAdded();
291 * Inserts a sequence at a point in the alignment.
294 * the index of the position the sequence is to be inserted in.
297 public void insertSequenceAt(int i, SequenceI snew)
299 synchronized (sequences)
301 if (sequences.size() > i)
303 sequences.add(i, snew);
310 hiddenSequences.adjustHeightSequenceAdded();
319 * @return DOCUMENT ME!
322 public List<SequenceGroup> getGroups()
328 public void finalize() throws Throwable
330 if (getDataset() != null)
332 getDataset().removeAlignmentRef();
340 * Defensively nulls out references in case this object is not garbage
343 void nullReferences()
349 hiddenSequences = null;
353 * decrement the alignmentRefs counter by one and null references if it goes
358 private void removeAlignmentRef() throws Throwable
360 if (--alignmentRefs == 0)
367 public void deleteSequence(SequenceI s)
369 synchronized (sequences)
371 deleteSequence(findIndex(s));
376 public void deleteSequence(int i)
378 synchronized (sequences)
380 if (i > -1 && i < getHeight())
383 hiddenSequences.adjustHeightSequenceDeleted(i);
389 public void deleteHiddenSequence(int i)
391 synchronized (sequences)
393 if (i > -1 && i < getHeight())
403 * @see jalview.datamodel.AlignmentI#findGroup(jalview.datamodel.SequenceI)
406 public SequenceGroup findGroup(SequenceI seq, int position)
408 synchronized (groups)
410 for (SequenceGroup sg : groups)
412 if (sg.getSequences(null).contains(seq))
414 if (position >= sg.getStartRes() && position <= sg.getEndRes())
428 * jalview.datamodel.AlignmentI#findAllGroups(jalview.datamodel.SequenceI)
431 public SequenceGroup[] findAllGroups(SequenceI s)
433 ArrayList<SequenceGroup> temp = new ArrayList<>();
435 synchronized (groups)
437 int gSize = groups.size();
438 for (int i = 0; i < gSize; i++)
440 SequenceGroup sg = groups.get(i);
441 if (sg == null || sg.getSequences() == null)
443 this.deleteGroup(sg);
448 if (sg.getSequences().contains(s))
454 SequenceGroup[] ret = new SequenceGroup[temp.size()];
455 return temp.toArray(ret);
460 public void addGroup(SequenceGroup sg)
462 synchronized (groups)
464 if (!groups.contains(sg))
466 if (hiddenSequences.getSize() > 0)
468 int i, iSize = sg.getSize();
469 for (i = 0; i < iSize; i++)
471 if (!sequences.contains(sg.getSequenceAt(i)))
473 sg.deleteSequence(sg.getSequenceAt(i), false);
479 if (sg.getSize() < 1)
484 sg.setContext(this, true);
491 * remove any annotation that references gp
494 * (if null, removes all group associated annotation)
496 private void removeAnnotationForGroup(SequenceGroup gp)
498 if (annotations == null || annotations.length == 0)
502 // remove annotation very quickly
503 AlignmentAnnotation[] t,
504 todelete = new AlignmentAnnotation[annotations.length],
505 tokeep = new AlignmentAnnotation[annotations.length];
509 for (i = 0, p = 0, k = 0; i < annotations.length; i++)
511 if (annotations[i].groupRef != null)
513 todelete[p++] = annotations[i];
517 tokeep[k++] = annotations[i];
523 for (i = 0, p = 0, k = 0; i < annotations.length; i++)
525 if (annotations[i].groupRef == gp)
527 todelete[p++] = annotations[i];
531 tokeep[k++] = annotations[i];
537 // clear out the group associated annotation.
538 for (i = 0; i < p; i++)
540 unhookAnnotation(todelete[i]);
543 t = new AlignmentAnnotation[k];
544 for (i = 0; i < k; i++)
553 public void deleteAllGroups()
555 synchronized (groups)
557 if (annotations != null)
559 removeAnnotationForGroup(null);
561 for (SequenceGroup sg : groups)
563 sg.setContext(null, false);
571 public void deleteGroup(SequenceGroup g)
573 synchronized (groups)
575 if (groups.contains(g))
577 removeAnnotationForGroup(g);
579 g.setContext(null, false);
586 public SequenceI findName(String name)
588 return findName(name, false);
594 * @see jalview.datamodel.AlignmentI#findName(java.lang.String, boolean)
597 public SequenceI findName(String token, boolean b)
599 return findName(null, token, b);
605 * @see jalview.datamodel.AlignmentI#findName(SequenceI, java.lang.String,
609 public SequenceI findName(SequenceI startAfter, String token, boolean b)
614 String sqname = null;
615 if (startAfter != null)
617 // try to find the sequence in the alignment
618 boolean matched = false;
619 while (i < sequences.size())
621 if (getSequenceAt(i++) == startAfter)
632 while (i < sequences.size())
634 sq = getSequenceAt(i);
635 sqname = sq.getName();
636 if (sqname.equals(token) // exact match
637 || (b && // allow imperfect matches - case varies
638 (sqname.equalsIgnoreCase(token))))
640 return getSequenceAt(i);
650 public SequenceI[] findSequenceMatch(String name)
652 Vector matches = new Vector();
655 while (i < sequences.size())
657 if (getSequenceAt(i).getName().equals(name))
659 matches.addElement(getSequenceAt(i));
664 SequenceI[] result = new SequenceI[matches.size()];
665 for (i = 0; i < result.length; i++)
667 result[i] = (SequenceI) matches.elementAt(i);
677 * @see jalview.datamodel.AlignmentI#findIndex(jalview.datamodel.SequenceI)
680 public int findIndex(SequenceI s)
684 while (i < sequences.size())
686 if (s == getSequenceAt(i))
701 * jalview.datamodel.AlignmentI#findIndex(jalview.datamodel.SearchResults)
704 public int findIndex(SearchResultsI results)
708 while (i < sequences.size())
710 if (results.involvesSequence(getSequenceAt(i)))
720 public int getHeight()
722 return sequences.size();
726 public int getAbsoluteHeight()
728 return sequences.size() + getHiddenSequences().getSize();
732 public int getWidth()
736 for (int i = 0; i < sequences.size(); i++)
738 if (getSequenceAt(i).getLength() > maxLength)
740 maxLength = getSequenceAt(i).getLength();
754 public void setGapCharacter(char gc)
757 synchronized (sequences)
759 for (SequenceI seq : sequences)
761 seq.setSequence(seq.getSequenceAsString().replace('.', gc)
762 .replace('-', gc).replace(' ', gc));
770 * @return DOCUMENT ME!
773 public char getGapCharacter()
781 * @see jalview.datamodel.AlignmentI#isAligned()
784 public boolean isAligned()
786 return isAligned(false);
792 * @see jalview.datamodel.AlignmentI#isAligned(boolean)
795 public boolean isAligned(boolean includeHidden)
797 int width = getWidth();
798 if (hiddenSequences == null || hiddenSequences.getSize() == 0)
800 includeHidden = true; // no hidden sequences to check against.
802 for (int i = 0; i < sequences.size(); i++)
804 if (includeHidden || !hiddenSequences.isHidden(getSequenceAt(i)))
806 if (getSequenceAt(i).getLength() != width)
817 public boolean isHidden(int alignmentIndex)
819 return (getHiddenSequences().getHiddenSequence(alignmentIndex) != null);
823 * Delete all annotations, including auto-calculated if the flag is set true.
824 * Returns true if at least one annotation was deleted, else false.
826 * @param includingAutoCalculated
830 public boolean deleteAllAnnotations(boolean includingAutoCalculated)
832 boolean result = false;
833 for (AlignmentAnnotation alan : getAlignmentAnnotation())
835 if (!alan.autoCalculated || includingAutoCalculated)
837 deleteAnnotation(alan);
847 * @seejalview.datamodel.AlignmentI#deleteAnnotation(jalview.datamodel.
848 * AlignmentAnnotation)
851 public boolean deleteAnnotation(AlignmentAnnotation aa)
853 return deleteAnnotation(aa, true);
857 public boolean deleteAnnotation(AlignmentAnnotation aa, boolean unhook)
861 if (annotations != null)
863 aSize = annotations.length;
871 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize - 1];
873 boolean swap = false;
876 for (int i = 0; i < aSize; i++)
878 if (annotations[i] == aa)
883 if (tIndex < temp.length)
885 temp[tIndex++] = annotations[i];
894 unhookAnnotation(aa);
901 * remove any object references associated with this annotation
905 private void unhookAnnotation(AlignmentAnnotation aa)
907 if (aa.sequenceRef != null)
909 aa.sequenceRef.removeAlignmentAnnotation(aa);
911 if (aa.groupRef != null)
913 // probably need to do more here in the future (post 2.5.0)
921 * @seejalview.datamodel.AlignmentI#addAnnotation(jalview.datamodel.
922 * AlignmentAnnotation)
925 public void addAnnotation(AlignmentAnnotation aa)
927 addAnnotation(aa, -1);
933 * @seejalview.datamodel.AlignmentI#addAnnotation(jalview.datamodel.
934 * AlignmentAnnotation, int)
937 public void addAnnotation(AlignmentAnnotation aa, int pos)
939 if (aa.getRNAStruc() != null)
941 hasRNAStructure = true;
945 if (annotations != null)
947 aSize = annotations.length + 1;
950 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
952 if (pos == -1 || pos >= aSize)
954 temp[aSize - 1] = aa;
963 for (i = 0; i < (aSize - 1); i++, p++)
971 temp[p] = annotations[i];
980 public void setAnnotationIndex(AlignmentAnnotation aa, int index)
982 if (aa == null || annotations == null || annotations.length - 1 < index)
987 int aSize = annotations.length;
988 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
992 for (int i = 0; i < aSize; i++)
1001 temp[i] = annotations[i];
1005 temp[i] = annotations[i - 1];
1014 * returns all annotation on the alignment
1016 public AlignmentAnnotation[] getAlignmentAnnotation()
1022 public boolean isNucleotide()
1028 public boolean hasRNAStructure()
1030 // TODO can it happen that structure is removed from alignment?
1031 return hasRNAStructure;
1035 public void setDataset(AlignmentI data)
1037 if (dataset == null && data == null)
1039 createDatasetAlignment();
1041 else if (dataset == null && data != null)
1045 throw new IllegalArgumentException("Circular dataset reference");
1047 if (!(data instanceof Alignment))
1050 "Implementation Error: jalview.datamodel.Alignment does not yet support other implementations of AlignmentI as its dataset reference");
1052 dataset = (Alignment) data;
1053 for (int i = 0; i < getHeight(); i++)
1055 SequenceI currentSeq = getSequenceAt(i);
1056 SequenceI dsq = currentSeq.getDatasetSequence();
1059 dsq = currentSeq.createDatasetSequence();
1060 dataset.addSequence(dsq);
1064 while (dsq.getDatasetSequence() != null)
1066 dsq = dsq.getDatasetSequence();
1068 if (dataset.findIndex(dsq) == -1)
1070 dataset.addSequence(dsq);
1075 dataset.addAlignmentRef();
1079 * add dataset sequences to seq for currentSeq and any sequences it references
1081 private void resolveAndAddDatasetSeq(SequenceI currentSeq,
1082 Set<SequenceI> seqs, boolean createDatasetSequence)
1084 SequenceI alignedSeq = currentSeq;
1085 if (currentSeq.getDatasetSequence() != null)
1087 currentSeq = currentSeq.getDatasetSequence();
1091 if (createDatasetSequence)
1093 currentSeq = currentSeq.createDatasetSequence();
1097 List<SequenceI> toProcess = new ArrayList<>();
1098 toProcess.add(currentSeq);
1099 while (toProcess.size() > 0)
1102 SequenceI curDs = toProcess.remove(0);
1104 if (!seqs.add(curDs))
1108 // iterate over database references, making sure we add forward referenced
1110 if (curDs.getDBRefs() != null)
1112 for (DBRefEntry dbr : curDs.getDBRefs())
1114 if (dbr.getMap() != null && dbr.getMap().getTo() != null)
1116 if (dbr.getMap().getTo() == alignedSeq)
1119 * update mapping to be to the newly created dataset sequence
1121 dbr.getMap().setTo(currentSeq);
1123 if (dbr.getMap().getTo().getDatasetSequence() != null)
1125 throw new Error("Implementation error: Map.getTo() for dbref "
1126 + dbr + " from " + curDs.getName()
1127 + " is not a dataset sequence.");
1129 // we recurse to add all forward references to dataset sequences via
1131 toProcess.add(dbr.getMap().getTo());
1139 * Creates a new dataset for this alignment. Can only be done once - if
1140 * dataset is not null this will not be performed.
1142 public void createDatasetAlignment()
1144 if (dataset != null)
1148 // try to avoid using SequenceI.equals at this stage, it will be expensive
1149 Set<SequenceI> seqs = new LinkedIdentityHashSet<>();
1151 for (int i = 0; i < getHeight(); i++)
1153 SequenceI currentSeq = getSequenceAt(i);
1154 resolveAndAddDatasetSeq(currentSeq, seqs, true);
1157 // verify all mappings are in dataset
1158 for (AlignedCodonFrame cf : codonFrameList)
1160 for (SequenceToSequenceMapping ssm : cf.getMappings())
1162 if (!seqs.contains(ssm.getFromSeq()))
1164 resolveAndAddDatasetSeq(ssm.getFromSeq(), seqs, false);
1166 if (!seqs.contains(ssm.getMapping().getTo()))
1168 resolveAndAddDatasetSeq(ssm.getMapping().getTo(), seqs, false);
1172 // finally construct dataset
1173 dataset = new Alignment(seqs.toArray(new SequenceI[seqs.size()]));
1174 // move mappings to the dataset alignment
1175 dataset.codonFrameList = this.codonFrameList;
1176 this.codonFrameList = null;
1180 * reference count for number of alignments referencing this one.
1182 int alignmentRefs = 0;
1185 * increase reference count to this alignment.
1187 private void addAlignmentRef()
1193 public Alignment getDataset()
1199 public boolean padGaps()
1201 boolean modified = false;
1203 // Remove excess gaps from the end of alignment
1207 for (int i = 0; i < sequences.size(); i++)
1209 current = getSequenceAt(i);
1210 for (int j = current.getLength(); j > maxLength; j--)
1213 && !jalview.util.Comparison.isGap(current.getCharAt(j)))
1224 for (int i = 0; i < sequences.size(); i++)
1226 current = getSequenceAt(i);
1227 cLength = current.getLength();
1229 if (cLength < maxLength)
1231 current.insertCharAt(cLength, maxLength - cLength, gapCharacter);
1234 else if (current.getLength() > maxLength)
1236 current.deleteChars(maxLength, current.getLength());
1243 * Justify the sequences to the left or right by deleting and inserting gaps
1244 * before the initial residue or after the terminal residue
1247 * true if alignment padded to right, false to justify to left
1248 * @return true if alignment was changed
1251 public boolean justify(boolean right)
1253 boolean modified = false;
1255 // Remove excess gaps from the end of alignment
1257 int ends[] = new int[sequences.size() * 2];
1259 for (int i = 0; i < sequences.size(); i++)
1261 current = getSequenceAt(i);
1262 // This should really be a sequence method
1263 ends[i * 2] = current.findIndex(current.getStart());
1264 ends[i * 2 + 1] = current
1265 .findIndex(current.getStart() + current.getLength());
1266 boolean hitres = false;
1267 for (int j = 0, rs = 0, ssiz = current.getLength(); j < ssiz; j++)
1269 if (!jalview.util.Comparison.isGap(current.getCharAt(j)))
1278 ends[i * 2 + 1] = j;
1279 if (j - ends[i * 2] > maxLength)
1281 maxLength = j - ends[i * 2];
1289 // now edit the flanking gaps to justify to either left or right
1290 int cLength, extent, diff;
1291 for (int i = 0; i < sequences.size(); i++)
1293 current = getSequenceAt(i);
1295 cLength = 1 + ends[i * 2 + 1] - ends[i * 2];
1296 diff = maxLength - cLength; // number of gaps to indent
1297 extent = current.getLength();
1301 if (extent > ends[i * 2 + 1])
1303 current.deleteChars(ends[i * 2 + 1] + 1, extent);
1306 if (ends[i * 2] > diff)
1308 current.deleteChars(0, ends[i * 2] - diff);
1313 if (ends[i * 2] < diff)
1315 current.insertCharAt(0, diff - ends[i * 2], gapCharacter);
1323 if (ends[i * 2] > 0)
1325 current.deleteChars(0, ends[i * 2]);
1327 ends[i * 2 + 1] -= ends[i * 2];
1328 extent -= ends[i * 2];
1330 if (extent > maxLength)
1332 current.deleteChars(maxLength + 1, extent);
1337 if (extent < maxLength)
1339 current.insertCharAt(extent, maxLength - extent, gapCharacter);
1349 public HiddenSequences getHiddenSequences()
1351 return hiddenSequences;
1355 public HiddenColumns getHiddenColumns()
1361 public CigarArray getCompactAlignment()
1363 synchronized (sequences)
1365 SeqCigar alseqs[] = new SeqCigar[sequences.size()];
1367 for (SequenceI seq : sequences)
1369 alseqs[i++] = new SeqCigar(seq);
1371 CigarArray cal = new CigarArray(alseqs);
1372 cal.addOperation(CigarArray.M, getWidth());
1378 public void setProperty(Object key, Object value)
1380 if (alignmentProperties == null)
1382 alignmentProperties = new Hashtable();
1385 alignmentProperties.put(key, value);
1389 public Object getProperty(Object key)
1391 if (alignmentProperties != null)
1393 return alignmentProperties.get(key);
1402 public Hashtable getProperties()
1404 return alignmentProperties;
1408 * Adds the given mapping to the stored set. Note this may be held on the
1409 * dataset alignment.
1412 public void addCodonFrame(AlignedCodonFrame codons)
1414 List<AlignedCodonFrame> acfs = getCodonFrames();
1415 if (codons != null && acfs != null && !acfs.contains(codons))
1425 * jalview.datamodel.AlignmentI#getCodonFrame(jalview.datamodel.SequenceI)
1428 public List<AlignedCodonFrame> getCodonFrame(SequenceI seq)
1434 List<AlignedCodonFrame> cframes = new ArrayList<>();
1435 for (AlignedCodonFrame acf : getCodonFrames())
1437 if (acf.involvesSequence(seq))
1446 * Sets the codon frame mappings (replacing any existing mappings). Note the
1447 * mappings are set on the dataset alignment instead if there is one.
1449 * @see jalview.datamodel.AlignmentI#setCodonFrames()
1452 public void setCodonFrames(List<AlignedCodonFrame> acfs)
1454 if (dataset != null)
1456 dataset.setCodonFrames(acfs);
1460 this.codonFrameList = acfs;
1465 * Returns the set of codon frame mappings. Any changes to the returned set
1466 * will affect the alignment. The mappings are held on (and read from) the
1467 * dataset alignment if there is one.
1469 * @see jalview.datamodel.AlignmentI#getCodonFrames()
1472 public List<AlignedCodonFrame> getCodonFrames()
1474 // TODO: Fix this method to fix failing AlignedCodonFrame tests
1475 // this behaviour is currently incorrect. method should return codon frames
1476 // for just the alignment,
1477 // selected from dataset
1478 return dataset != null ? dataset.getCodonFrames() : codonFrameList;
1482 * Removes the given mapping from the stored set. Note that the mappings are
1483 * held on the dataset alignment if there is one.
1486 public boolean removeCodonFrame(AlignedCodonFrame codons)
1488 List<AlignedCodonFrame> acfs = getCodonFrames();
1489 if (codons == null || acfs == null)
1493 return acfs.remove(codons);
1497 public void append(AlignmentI toappend)
1499 // TODO JAL-1270 needs test coverage
1500 // currently tested for use in jalview.gui.SequenceFetcher
1501 char oldc = toappend.getGapCharacter();
1502 boolean samegap = oldc == getGapCharacter();
1503 boolean hashidden = toappend.getHiddenSequences() != null
1504 && toappend.getHiddenSequences().hiddenSequences != null;
1505 // get all sequences including any hidden ones
1506 List<SequenceI> sqs = (hashidden)
1507 ? toappend.getHiddenSequences().getFullAlignment()
1509 : toappend.getSequences();
1512 // avoid self append deadlock by
1513 List<SequenceI> toappendsq = new ArrayList<>();
1516 for (SequenceI addedsq : sqs)
1520 addedsq.replace(oldc, gapCharacter);
1522 toappendsq.add(addedsq);
1525 for (SequenceI addedsq : toappendsq)
1527 addSequence(addedsq);
1530 AlignmentAnnotation[] alan = toappend.getAlignmentAnnotation();
1531 for (int a = 0; alan != null && a < alan.length; a++)
1533 addAnnotation(alan[a]);
1537 getCodonFrames().addAll(toappend.getCodonFrames());
1539 List<SequenceGroup> sg = toappend.getGroups();
1542 for (SequenceGroup _sg : sg)
1547 if (toappend.getHiddenSequences() != null)
1549 HiddenSequences hs = toappend.getHiddenSequences();
1550 if (hiddenSequences == null)
1552 hiddenSequences = new HiddenSequences(this);
1554 if (hs.hiddenSequences != null)
1556 for (int s = 0; s < hs.hiddenSequences.length; s++)
1558 // hide the newly appended sequence in the alignment
1559 if (hs.hiddenSequences[s] != null)
1561 hiddenSequences.hideSequence(hs.hiddenSequences[s]);
1566 if (toappend.getProperties() != null)
1568 // we really can't do very much here - just try to concatenate strings
1569 // where property collisions occur.
1570 Enumeration key = toappend.getProperties().keys();
1571 while (key.hasMoreElements())
1573 Object k = key.nextElement();
1574 Object ourval = this.getProperty(k);
1575 Object toapprop = toappend.getProperty(k);
1578 if (ourval.getClass().equals(toapprop.getClass())
1579 && !ourval.equals(toapprop))
1581 if (ourval instanceof String)
1585 ((String) ourval) + "; " + ((String) toapprop));
1589 if (ourval instanceof Vector)
1592 Enumeration theirv = ((Vector) toapprop).elements();
1593 while (theirv.hasMoreElements())
1595 ((Vector) ourval).addElement(theirv);
1603 // just add new property directly
1604 setProperty(k, toapprop);
1612 public AlignmentAnnotation findOrCreateAnnotation(String name,
1613 String calcId, boolean autoCalc, SequenceI seqRef,
1614 SequenceGroup groupRef)
1616 if (annotations != null)
1618 for (AlignmentAnnotation annot : getAlignmentAnnotation())
1620 if (annot.autoCalculated == autoCalc && (name.equals(annot.label))
1621 && (calcId == null || annot.getCalcId().equals(calcId))
1622 && annot.sequenceRef == seqRef
1623 && annot.groupRef == groupRef)
1629 AlignmentAnnotation annot = new AlignmentAnnotation(name, name,
1630 new Annotation[1], 0f, 0f, AlignmentAnnotation.BAR_GRAPH);
1631 annot.hasText = false;
1632 annot.setCalcId(new String(calcId));
1633 annot.autoCalculated = autoCalc;
1636 annot.setSequenceRef(seqRef);
1638 annot.groupRef = groupRef;
1639 addAnnotation(annot);
1645 public Iterable<AlignmentAnnotation> findAnnotation(String calcId)
1647 AlignmentAnnotation[] alignmentAnnotation = getAlignmentAnnotation();
1648 if (alignmentAnnotation != null)
1650 return AlignmentAnnotation.findAnnotation(
1651 Arrays.asList(getAlignmentAnnotation()), calcId);
1653 return Arrays.asList(new AlignmentAnnotation[] {});
1657 public Iterable<AlignmentAnnotation> findAnnotations(SequenceI seq,
1658 String calcId, String label)
1660 return AlignmentAnnotation.findAnnotations(
1661 Arrays.asList(getAlignmentAnnotation()), seq, calcId, label);
1665 public void moveSelectedSequencesByOne(SequenceGroup sg,
1666 Map<SequenceI, SequenceCollectionI> map, boolean up)
1668 synchronized (sequences)
1673 for (int i = 1, iSize = sequences.size(); i < iSize; i++)
1675 SequenceI seq = sequences.get(i);
1676 if (!sg.getSequences(map).contains(seq))
1681 SequenceI temp = sequences.get(i - 1);
1682 if (sg.getSequences(null).contains(temp))
1687 sequences.set(i, temp);
1688 sequences.set(i - 1, seq);
1693 for (int i = sequences.size() - 2; i > -1; i--)
1695 SequenceI seq = sequences.get(i);
1696 if (!sg.getSequences(map).contains(seq))
1701 SequenceI temp = sequences.get(i + 1);
1702 if (sg.getSequences(map).contains(temp))
1707 sequences.set(i, temp);
1708 sequences.set(i + 1, seq);
1716 public void validateAnnotation(AlignmentAnnotation alignmentAnnotation)
1718 alignmentAnnotation.validateRangeAndDisplay();
1719 if (isNucleotide() && alignmentAnnotation.isValidStruc())
1721 hasRNAStructure = true;
1725 private SequenceI seqrep = null;
1729 * @return the representative sequence for this group
1732 public SequenceI getSeqrep()
1738 * set the representative sequence for this group. Note - this affects the
1739 * interpretation of the Hidereps attribute.
1742 * the seqrep to set (null means no sequence representative)
1745 public void setSeqrep(SequenceI seqrep)
1747 this.seqrep = seqrep;
1752 * @return true if group has a sequence representative
1755 public boolean hasSeqrep()
1757 return seqrep != null;
1761 public int getEndRes()
1763 return getWidth() - 1;
1767 public int getStartRes()
1773 * In the case of AlignmentI - returns the dataset for the alignment, if set
1776 * @see jalview.datamodel.AnnotatedCollectionI#getContext()
1779 public AnnotatedCollectionI getContext()
1785 * Align this alignment like the given (mapped) one.
1788 public int alignAs(AlignmentI al)
1791 * Currently retains unmapped gaps (in introns), regaps mapped regions
1794 return alignAs(al, false, true);
1798 * Align this alignment 'the same as' the given one. Mapped sequences only are
1799 * realigned. If both of the same type (nucleotide/protein) then align both
1800 * identically. If this is nucleotide and the other is protein, make 3 gaps
1801 * for each gap in the protein sequences. If this is protein and the other is
1802 * nucleotide, insert a gap for each 3 gaps (or part thereof) between
1803 * nucleotide bases. If this is protein and the other is nucleotide, gaps
1804 * protein to match the relative ordering of codons in the nucleotide.
1806 * Parameters control whether gaps in exon (mapped) and intron (unmapped)
1807 * regions are preserved. Gaps that connect introns to exons are treated
1808 * conservatively, i.e. only preserved if both intron and exon gaps are
1809 * preserved. TODO: check caveats below where the implementation fails
1812 * - must have same dataset, and sequences in al must have equivalent
1813 * dataset sequence and start/end bounds under given mapping
1814 * @param preserveMappedGaps
1815 * if true, gaps within and between mapped codons are preserved
1816 * @param preserveUnmappedGaps
1817 * if true, gaps within and between unmapped codons are preserved
1820 public int alignAs(AlignmentI al, boolean preserveMappedGaps,
1821 boolean preserveUnmappedGaps)
1823 // TODO should this method signature be the one in the interface?
1824 // JBPComment - yes - neither flag is used, so should be deleted.
1825 boolean thisIsNucleotide = this.isNucleotide();
1826 boolean thatIsProtein = !al.isNucleotide();
1827 if (!thatIsProtein && !thisIsNucleotide)
1829 return AlignmentUtils.alignProteinAsDna(this, al);
1831 else if (thatIsProtein && thisIsNucleotide)
1833 return AlignmentUtils.alignCdsAsProtein(this, al);
1835 return AlignmentUtils.alignAs(this, al);
1839 * Returns the alignment in Fasta format. Behaviour of this method is not
1840 * guaranteed between versions.
1843 public String toString()
1845 return new FastaFile().print(getSequencesArray(), true);
1849 * Returns the set of distinct sequence names. No ordering is guaranteed.
1852 public Set<String> getSequenceNames()
1854 Set<String> names = new HashSet<>();
1855 for (SequenceI seq : getSequences())
1857 names.add(seq.getName());
1863 public boolean hasValidSequence()
1865 boolean hasValidSeq = false;
1866 for (SequenceI seq : getSequences())
1868 if ((seq.getEnd() - seq.getStart()) > 0)
1878 * Update any mappings to 'virtual' sequences to compatible real ones, if
1879 * present in the added sequences. Returns a count of mappings updated.
1885 public int realiseMappings(List<SequenceI> seqs)
1888 for (SequenceI seq : seqs)
1890 for (AlignedCodonFrame mapping : getCodonFrames())
1892 count += mapping.realiseWith(seq);
1899 * Returns the first AlignedCodonFrame that has a mapping between the given
1907 public AlignedCodonFrame getMapping(SequenceI mapFrom, SequenceI mapTo)
1909 for (AlignedCodonFrame acf : getCodonFrames())
1911 if (acf.getAaForDnaSeq(mapFrom) == mapTo)
1920 public void setHiddenColumns(HiddenColumns cols)
1926 * Returns all HMM consensus sequences. This will not return real sequences
1930 public List<SequenceI> getHMMConsensusSequences()
1932 List<SequenceI> seqs = new ArrayList<>();
1934 for (int position = 0; position < sequences.size(); position++)
1936 SequenceI seq = sequences.get(position);
1937 if (seq.isHMMConsensusSequence())