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.Collections;
32 import java.util.Enumeration;
33 import java.util.HashSet;
34 import java.util.Hashtable;
35 import java.util.List;
38 import java.util.Vector;
41 * Data structure to hold and manipulate a multiple sequence alignment
47 public class Alignment implements AlignmentI
49 private Alignment dataset;
51 protected List<SequenceI> sequences;
53 protected List<SequenceGroup> groups;
55 protected char gapCharacter = '-';
57 private boolean nucleotide = true;
59 public boolean hasRNAStructure = false;
61 public AlignmentAnnotation[] annotations;
63 HiddenSequences hiddenSequences;
65 HiddenColumns hiddenCols;
67 public Hashtable alignmentProperties;
69 private List<AlignedCodonFrame> codonFrameList;
71 HiddenMarkovModel hmm;
73 private void initAlignment(SequenceI[] seqs)
75 groups = Collections.synchronizedList(new ArrayList<SequenceGroup>());
76 hiddenSequences = new HiddenSequences(this);
77 hiddenCols = new HiddenColumns();
78 codonFrameList = new ArrayList<>();
80 nucleotide = Comparison.isNucleotide(seqs);
82 sequences = Collections.synchronizedList(new ArrayList<SequenceI>());
84 for (int i = 0; i < seqs.length; i++)
86 sequences.add(seqs[i]);
92 * Make a 'copy' alignment - sequences have new copies of features and
93 * annotations, but share the original dataset sequences.
95 public Alignment(AlignmentI al)
97 SequenceI[] seqs = al.getSequencesArray();
98 for (int i = 0; i < seqs.length; i++)
100 seqs[i] = new Sequence(seqs[i]);
106 * Share the same dataset sequence mappings (if any).
108 if (dataset == null && al.getDataset() == null)
110 this.setCodonFrames(al.getCodonFrames());
115 * Make an alignment from an array of Sequences.
119 public Alignment(SequenceI[] seqs)
125 * Make a new alignment from an array of SeqCigars
130 public Alignment(SeqCigar[] alseqs)
132 SequenceI[] seqs = SeqCigar.createAlignmentSequences(alseqs,
133 gapCharacter, new HiddenColumns(), null);
138 * Make a new alignment from an CigarArray JBPNote - can only do this when
139 * compactAlignment does not contain hidden regions. JBPNote - must also check
140 * that compactAlignment resolves to a set of SeqCigars - or construct them
143 * @param compactAlignment
146 public static AlignmentI createAlignment(CigarArray compactAlignment)
150 .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);
195 public SequenceI getSequenceAt(int i)
197 synchronized (sequences)
199 if (i > -1 && i < sequences.size())
201 return sequences.get(i);
208 public SequenceI getSequenceAtAbsoluteIndex(int i)
210 SequenceI seq = null;
211 if (getHiddenSequences().getSize() > 0)
213 seq = getHiddenSequences().getHiddenSequence(i);
216 // didn't find the sequence in the hidden sequences, get it from the
218 int index = getHiddenSequences().findIndexWithoutHiddenSeqs(i);
219 seq = getSequenceAt(index);
224 seq = getSequenceAt(i);
230 * Adds a sequence to the alignment. Recalculates maxLength and size. Note
231 * this currently does not recalculate whether or not the alignment is
232 * nucleotide, so mixed alignments may have undefined behaviour.
237 public void addSequence(SequenceI snew)
242 // maintain dataset integrity
243 SequenceI dsseq = snew.getDatasetSequence();
246 // derive new sequence
247 SequenceI adding = snew.deriveSequence();
249 dsseq = snew.getDatasetSequence();
251 if (getDataset().findIndex(dsseq) == -1)
253 getDataset().addSequence(dsseq);
257 if (sequences == null)
259 initAlignment(new SequenceI[] { snew });
263 synchronized (sequences)
268 if (hiddenSequences != null)
270 hiddenSequences.adjustHeightSequenceAdded();
275 public SequenceI replaceSequenceAt(int i, SequenceI snew)
277 synchronized (sequences)
279 if (sequences.size() > i)
281 return sequences.set(i, snew);
287 hiddenSequences.adjustHeightSequenceAdded();
296 * @return DOCUMENT ME!
299 public List<SequenceGroup> getGroups()
305 public void finalize() throws Throwable
307 if (getDataset() != null)
309 getDataset().removeAlignmentRef();
317 * Defensively nulls out references in case this object is not garbage
320 void nullReferences()
326 hiddenSequences = null;
330 * decrement the alignmentRefs counter by one and null references if it goes
335 private void removeAlignmentRef() throws Throwable
337 if (--alignmentRefs == 0)
344 public void deleteSequence(SequenceI s)
346 synchronized (sequences)
348 deleteSequence(findIndex(s));
353 public void deleteSequence(int i)
355 synchronized (sequences)
357 if (i > -1 && i < getHeight())
360 hiddenSequences.adjustHeightSequenceDeleted(i);
366 public void deleteHiddenSequence(int i)
368 synchronized (sequences)
370 if (i > -1 && i < getHeight())
380 * @see jalview.datamodel.AlignmentI#findGroup(jalview.datamodel.SequenceI)
383 public SequenceGroup findGroup(SequenceI seq, int position)
385 synchronized (groups)
387 for (SequenceGroup sg : groups)
389 if (sg.getSequences(null).contains(seq))
391 if (position >= sg.getStartRes() && position <= sg.getEndRes())
405 * jalview.datamodel.AlignmentI#findAllGroups(jalview.datamodel.SequenceI)
408 public SequenceGroup[] findAllGroups(SequenceI s)
410 ArrayList<SequenceGroup> temp = new ArrayList<>();
412 synchronized (groups)
414 int gSize = groups.size();
415 for (int i = 0; i < gSize; i++)
417 SequenceGroup sg = groups.get(i);
418 if (sg == null || sg.getSequences() == null)
420 this.deleteGroup(sg);
425 if (sg.getSequences().contains(s))
431 SequenceGroup[] ret = new SequenceGroup[temp.size()];
432 return temp.toArray(ret);
437 public void addGroup(SequenceGroup sg)
439 synchronized (groups)
441 if (!groups.contains(sg))
443 if (hiddenSequences.getSize() > 0)
445 int i, iSize = sg.getSize();
446 for (i = 0; i < iSize; i++)
448 if (!sequences.contains(sg.getSequenceAt(i)))
450 sg.deleteSequence(sg.getSequenceAt(i), false);
456 if (sg.getSize() < 1)
461 sg.setContext(this, true);
468 * remove any annotation that references gp
471 * (if null, removes all group associated annotation)
473 private void removeAnnotationForGroup(SequenceGroup gp)
475 if (annotations == null || annotations.length == 0)
479 // remove annotation very quickly
480 AlignmentAnnotation[] t, todelete = new AlignmentAnnotation[annotations.length], tokeep = new AlignmentAnnotation[annotations.length];
484 for (i = 0, p = 0, k = 0; i < annotations.length; i++)
486 if (annotations[i].groupRef != null)
488 todelete[p++] = annotations[i];
492 tokeep[k++] = annotations[i];
498 for (i = 0, p = 0, k = 0; i < annotations.length; i++)
500 if (annotations[i].groupRef == gp)
502 todelete[p++] = annotations[i];
506 tokeep[k++] = annotations[i];
512 // clear out the group associated annotation.
513 for (i = 0; i < p; i++)
515 unhookAnnotation(todelete[i]);
518 t = new AlignmentAnnotation[k];
519 for (i = 0; i < k; i++)
528 public void deleteAllGroups()
530 synchronized (groups)
532 if (annotations != null)
534 removeAnnotationForGroup(null);
536 for (SequenceGroup sg : groups)
538 sg.setContext(null, false);
546 public void deleteGroup(SequenceGroup g)
548 synchronized (groups)
550 if (groups.contains(g))
552 removeAnnotationForGroup(g);
554 g.setContext(null, false);
561 public SequenceI findName(String name)
563 return findName(name, false);
569 * @see jalview.datamodel.AlignmentI#findName(java.lang.String, boolean)
572 public SequenceI findName(String token, boolean b)
574 return findName(null, token, b);
580 * @see jalview.datamodel.AlignmentI#findName(SequenceI, java.lang.String,
584 public SequenceI findName(SequenceI startAfter, String token, boolean b)
589 String sqname = null;
590 if (startAfter != null)
592 // try to find the sequence in the alignment
593 boolean matched = false;
594 while (i < sequences.size())
596 if (getSequenceAt(i++) == startAfter)
607 while (i < sequences.size())
609 sq = getSequenceAt(i);
610 sqname = sq.getName();
611 if (sqname.equals(token) // exact match
612 || (b && // allow imperfect matches - case varies
613 (sqname.equalsIgnoreCase(token))))
615 return getSequenceAt(i);
625 public SequenceI[] findSequenceMatch(String name)
627 Vector matches = new Vector();
630 while (i < sequences.size())
632 if (getSequenceAt(i).getName().equals(name))
634 matches.addElement(getSequenceAt(i));
639 SequenceI[] result = new SequenceI[matches.size()];
640 for (i = 0; i < result.length; i++)
642 result[i] = (SequenceI) matches.elementAt(i);
652 * @see jalview.datamodel.AlignmentI#findIndex(jalview.datamodel.SequenceI)
655 public int findIndex(SequenceI s)
659 while (i < sequences.size())
661 if (s == getSequenceAt(i))
676 * jalview.datamodel.AlignmentI#findIndex(jalview.datamodel.SearchResults)
679 public int findIndex(SearchResultsI results)
683 while (i < sequences.size())
685 if (results.involvesSequence(getSequenceAt(i)))
696 public int getHeight()
698 return sequences.size();
702 public int getAbsoluteHeight()
704 return sequences.size() + getHiddenSequences().getSize();
708 public int getWidth()
712 for (int i = 0; i < sequences.size(); i++)
714 if (getSequenceAt(i).getLength() > maxLength)
716 maxLength = getSequenceAt(i).getLength();
730 public void setGapCharacter(char gc)
733 synchronized (sequences)
735 for (SequenceI seq : sequences)
737 seq.setSequence(seq.getSequenceAsString().replace('.', gc)
738 .replace('-', gc).replace(' ', gc));
746 * @return DOCUMENT ME!
749 public char getGapCharacter()
757 * @see jalview.datamodel.AlignmentI#isAligned()
760 public boolean isAligned()
762 return isAligned(false);
768 * @see jalview.datamodel.AlignmentI#isAligned(boolean)
771 public boolean isAligned(boolean includeHidden)
773 int width = getWidth();
774 if (hiddenSequences == null || hiddenSequences.getSize() == 0)
776 includeHidden = true; // no hidden sequences to check against.
778 for (int i = 0; i < sequences.size(); i++)
780 if (includeHidden || !hiddenSequences.isHidden(getSequenceAt(i)))
782 if (getSequenceAt(i).getLength() != width)
793 public boolean isHidden(int alignmentIndex)
795 return (getHiddenSequences().getHiddenSequence(alignmentIndex) != null);
799 * Delete all annotations, including auto-calculated if the flag is set true.
800 * Returns true if at least one annotation was deleted, else false.
802 * @param includingAutoCalculated
806 public boolean deleteAllAnnotations(boolean includingAutoCalculated)
808 boolean result = false;
809 for (AlignmentAnnotation alan : getAlignmentAnnotation())
811 if (!alan.autoCalculated || includingAutoCalculated)
813 deleteAnnotation(alan);
823 * @seejalview.datamodel.AlignmentI#deleteAnnotation(jalview.datamodel.
824 * AlignmentAnnotation)
827 public boolean deleteAnnotation(AlignmentAnnotation aa)
829 return deleteAnnotation(aa, true);
833 public boolean deleteAnnotation(AlignmentAnnotation aa, boolean unhook)
837 if (annotations != null)
839 aSize = annotations.length;
847 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize - 1];
849 boolean swap = false;
852 for (int i = 0; i < aSize; i++)
854 if (annotations[i] == aa)
859 if (tIndex < temp.length)
861 temp[tIndex++] = annotations[i];
870 unhookAnnotation(aa);
877 * remove any object references associated with this annotation
881 private void unhookAnnotation(AlignmentAnnotation aa)
883 if (aa.sequenceRef != null)
885 aa.sequenceRef.removeAlignmentAnnotation(aa);
887 if (aa.groupRef != null)
889 // probably need to do more here in the future (post 2.5.0)
897 * @seejalview.datamodel.AlignmentI#addAnnotation(jalview.datamodel.
898 * AlignmentAnnotation)
901 public void addAnnotation(AlignmentAnnotation aa)
903 addAnnotation(aa, -1);
909 * @seejalview.datamodel.AlignmentI#addAnnotation(jalview.datamodel.
910 * AlignmentAnnotation, int)
913 public void addAnnotation(AlignmentAnnotation aa, int pos)
915 if (aa.getRNAStruc() != null)
917 hasRNAStructure = true;
921 if (annotations != null)
923 aSize = annotations.length + 1;
926 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
928 if (pos == -1 || pos >= aSize)
930 temp[aSize - 1] = aa;
939 for (i = 0; i < (aSize - 1); i++, p++)
947 temp[p] = annotations[i];
956 public void setAnnotationIndex(AlignmentAnnotation aa, int index)
958 if (aa == null || annotations == null || annotations.length - 1 < index)
963 int aSize = annotations.length;
964 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
968 for (int i = 0; i < aSize; i++)
977 temp[i] = annotations[i];
981 temp[i] = annotations[i - 1];
990 * returns all annotation on the alignment
992 public AlignmentAnnotation[] getAlignmentAnnotation()
998 public boolean isNucleotide()
1004 public boolean hasRNAStructure()
1006 // TODO can it happen that structure is removed from alignment?
1007 return hasRNAStructure;
1011 public void setDataset(AlignmentI data)
1013 if (dataset == null && data == null)
1015 createDatasetAlignment();
1017 else if (dataset == null && data != null)
1021 throw new IllegalArgumentException("Circular dataset reference");
1023 if (!(data instanceof Alignment))
1026 "Implementation Error: jalview.datamodel.Alignment does not yet support other implementations of AlignmentI as its dataset reference");
1028 dataset = (Alignment) data;
1029 for (int i = 0; i < getHeight(); i++)
1031 SequenceI currentSeq = getSequenceAt(i);
1032 SequenceI dsq = currentSeq.getDatasetSequence();
1035 dsq = currentSeq.createDatasetSequence();
1036 dataset.addSequence(dsq);
1040 while (dsq.getDatasetSequence() != null)
1042 dsq = dsq.getDatasetSequence();
1044 if (dataset.findIndex(dsq) == -1)
1046 dataset.addSequence(dsq);
1051 dataset.addAlignmentRef();
1055 * add dataset sequences to seq for currentSeq and any sequences it references
1057 private void resolveAndAddDatasetSeq(SequenceI currentSeq,
1058 Set<SequenceI> seqs, boolean createDatasetSequence)
1060 SequenceI alignedSeq = currentSeq;
1061 if (currentSeq.getDatasetSequence() != null)
1063 currentSeq = currentSeq.getDatasetSequence();
1067 if (createDatasetSequence)
1069 currentSeq = currentSeq.createDatasetSequence();
1072 if (seqs.contains(currentSeq))
1076 List<SequenceI> toProcess = new ArrayList<>();
1077 toProcess.add(currentSeq);
1078 while (toProcess.size() > 0)
1081 SequenceI curDs = toProcess.remove(0);
1082 if (seqs.contains(curDs))
1087 // iterate over database references, making sure we add forward referenced
1089 if (curDs.getDBRefs() != null)
1091 for (DBRefEntry dbr : curDs.getDBRefs())
1093 if (dbr.getMap() != null && dbr.getMap().getTo() != null)
1095 if (dbr.getMap().getTo() == alignedSeq)
1098 * update mapping to be to the newly created dataset sequence
1100 dbr.getMap().setTo(currentSeq);
1102 if (dbr.getMap().getTo().getDatasetSequence() != null)
1105 "Implementation error: Map.getTo() for dbref " + dbr
1106 + " from " + curDs.getName()
1107 + " is not a dataset sequence.");
1109 // we recurse to add all forward references to dataset sequences via
1111 toProcess.add(dbr.getMap().getTo());
1119 * Creates a new dataset for this alignment. Can only be done once - if
1120 * dataset is not null this will not be performed.
1122 public void createDatasetAlignment()
1124 if (dataset != null)
1128 // try to avoid using SequenceI.equals at this stage, it will be expensive
1129 Set<SequenceI> seqs = new LinkedIdentityHashSet<>();
1131 for (int i = 0; i < getHeight(); i++)
1133 SequenceI currentSeq = getSequenceAt(i);
1134 resolveAndAddDatasetSeq(currentSeq, seqs, true);
1137 // verify all mappings are in dataset
1138 for (AlignedCodonFrame cf : codonFrameList)
1140 for (SequenceToSequenceMapping ssm : cf.getMappings())
1142 if (!seqs.contains(ssm.getFromSeq()))
1144 resolveAndAddDatasetSeq(ssm.getFromSeq(), seqs, false);
1146 if (!seqs.contains(ssm.getMapping().getTo()))
1148 resolveAndAddDatasetSeq(ssm.getMapping().getTo(), seqs, false);
1152 // finally construct dataset
1153 dataset = new Alignment(seqs.toArray(new SequenceI[seqs.size()]));
1154 // move mappings to the dataset alignment
1155 dataset.codonFrameList = this.codonFrameList;
1156 this.codonFrameList = null;
1160 * reference count for number of alignments referencing this one.
1162 int alignmentRefs = 0;
1165 * increase reference count to this alignment.
1167 private void addAlignmentRef()
1173 public Alignment getDataset()
1179 public boolean padGaps()
1181 boolean modified = false;
1183 // Remove excess gaps from the end of alignment
1187 for (int i = 0; i < sequences.size(); i++)
1189 current = getSequenceAt(i);
1190 for (int j = current.getLength(); j > maxLength; j--)
1193 && !jalview.util.Comparison.isGap(current.getCharAt(j)))
1204 for (int i = 0; i < sequences.size(); i++)
1206 current = getSequenceAt(i);
1207 cLength = current.getLength();
1209 if (cLength < maxLength)
1211 current.insertCharAt(cLength, maxLength - cLength, gapCharacter);
1214 else if (current.getLength() > maxLength)
1216 current.deleteChars(maxLength, current.getLength());
1223 * Justify the sequences to the left or right by deleting and inserting gaps
1224 * before the initial residue or after the terminal residue
1227 * true if alignment padded to right, false to justify to left
1228 * @return true if alignment was changed
1231 public boolean justify(boolean right)
1233 boolean modified = false;
1235 // Remove excess gaps from the end of alignment
1237 int ends[] = new int[sequences.size() * 2];
1239 for (int i = 0; i < sequences.size(); i++)
1241 current = getSequenceAt(i);
1242 // This should really be a sequence method
1243 ends[i * 2] = current.findIndex(current.getStart());
1244 ends[i * 2 + 1] = current.findIndex(current.getStart()
1245 + current.getLength());
1246 boolean hitres = false;
1247 for (int j = 0, rs = 0, ssiz = current.getLength(); j < ssiz; j++)
1249 if (!jalview.util.Comparison.isGap(current.getCharAt(j)))
1258 ends[i * 2 + 1] = j;
1259 if (j - ends[i * 2] > maxLength)
1261 maxLength = j - ends[i * 2];
1269 // now edit the flanking gaps to justify to either left or right
1270 int cLength, extent, diff;
1271 for (int i = 0; i < sequences.size(); i++)
1273 current = getSequenceAt(i);
1275 cLength = 1 + ends[i * 2 + 1] - ends[i * 2];
1276 diff = maxLength - cLength; // number of gaps to indent
1277 extent = current.getLength();
1281 if (extent > ends[i * 2 + 1])
1283 current.deleteChars(ends[i * 2 + 1] + 1, extent);
1286 if (ends[i * 2] > diff)
1288 current.deleteChars(0, ends[i * 2] - diff);
1293 if (ends[i * 2] < diff)
1295 current.insertCharAt(0, diff - ends[i * 2], gapCharacter);
1303 if (ends[i * 2] > 0)
1305 current.deleteChars(0, ends[i * 2]);
1307 ends[i * 2 + 1] -= ends[i * 2];
1308 extent -= ends[i * 2];
1310 if (extent > maxLength)
1312 current.deleteChars(maxLength + 1, extent);
1317 if (extent < maxLength)
1319 current.insertCharAt(extent, maxLength - extent, gapCharacter);
1329 public HiddenSequences getHiddenSequences()
1331 return hiddenSequences;
1335 public HiddenColumns getHiddenColumns()
1341 public CigarArray getCompactAlignment()
1343 synchronized (sequences)
1345 SeqCigar alseqs[] = new SeqCigar[sequences.size()];
1347 for (SequenceI seq : sequences)
1349 alseqs[i++] = new SeqCigar(seq);
1351 CigarArray cal = new CigarArray(alseqs);
1352 cal.addOperation(CigarArray.M, getWidth());
1358 public void setProperty(Object key, Object value)
1360 if (alignmentProperties == null)
1362 alignmentProperties = new Hashtable();
1365 alignmentProperties.put(key, value);
1369 public Object getProperty(Object key)
1371 if (alignmentProperties != null)
1373 return alignmentProperties.get(key);
1382 public Hashtable getProperties()
1384 return alignmentProperties;
1388 * Adds the given mapping to the stored set. Note this may be held on the
1389 * dataset alignment.
1392 public void addCodonFrame(AlignedCodonFrame codons)
1394 List<AlignedCodonFrame> acfs = getCodonFrames();
1395 if (codons != null && acfs != null && !acfs.contains(codons))
1405 * jalview.datamodel.AlignmentI#getCodonFrame(jalview.datamodel.SequenceI)
1408 public List<AlignedCodonFrame> getCodonFrame(SequenceI seq)
1414 List<AlignedCodonFrame> cframes = new ArrayList<>();
1415 for (AlignedCodonFrame acf : getCodonFrames())
1417 if (acf.involvesSequence(seq))
1426 * Sets the codon frame mappings (replacing any existing mappings). Note the
1427 * mappings are set on the dataset alignment instead if there is one.
1429 * @see jalview.datamodel.AlignmentI#setCodonFrames()
1432 public void setCodonFrames(List<AlignedCodonFrame> acfs)
1434 if (dataset != null)
1436 dataset.setCodonFrames(acfs);
1440 this.codonFrameList = acfs;
1445 * Returns the set of codon frame mappings. Any changes to the returned set
1446 * will affect the alignment. The mappings are held on (and read from) the
1447 * dataset alignment if there is one.
1449 * @see jalview.datamodel.AlignmentI#getCodonFrames()
1452 public List<AlignedCodonFrame> getCodonFrames()
1454 // TODO: Fix this method to fix failing AlignedCodonFrame tests
1455 // this behaviour is currently incorrect. method should return codon frames
1456 // for just the alignment,
1457 // selected from dataset
1458 return dataset != null ? dataset.getCodonFrames() : codonFrameList;
1462 * Removes the given mapping from the stored set. Note that the mappings are
1463 * held on the dataset alignment if there is one.
1466 public boolean removeCodonFrame(AlignedCodonFrame codons)
1468 List<AlignedCodonFrame> acfs = getCodonFrames();
1469 if (codons == null || acfs == null)
1473 return acfs.remove(codons);
1477 public void append(AlignmentI toappend)
1479 // TODO JAL-1270 needs test coverage
1480 // currently tested for use in jalview.gui.SequenceFetcher
1481 boolean samegap = toappend.getGapCharacter() == getGapCharacter();
1482 char oldc = toappend.getGapCharacter();
1483 boolean hashidden = toappend.getHiddenSequences() != null
1484 && toappend.getHiddenSequences().hiddenSequences != null;
1485 // get all sequences including any hidden ones
1486 List<SequenceI> sqs = (hashidden) ? toappend.getHiddenSequences()
1487 .getFullAlignment().getSequences() : toappend.getSequences();
1490 // avoid self append deadlock by
1491 List<SequenceI> toappendsq = new ArrayList<>();
1494 for (SequenceI addedsq : sqs)
1498 char[] oldseq = addedsq.getSequence();
1499 for (int c = 0; c < oldseq.length; c++)
1501 if (oldseq[c] == oldc)
1503 oldseq[c] = gapCharacter;
1507 toappendsq.add(addedsq);
1510 for (SequenceI addedsq : toappendsq)
1512 addSequence(addedsq);
1515 AlignmentAnnotation[] alan = toappend.getAlignmentAnnotation();
1516 for (int a = 0; alan != null && a < alan.length; a++)
1518 addAnnotation(alan[a]);
1522 getCodonFrames().addAll(toappend.getCodonFrames());
1524 List<SequenceGroup> sg = toappend.getGroups();
1527 for (SequenceGroup _sg : sg)
1532 if (toappend.getHiddenSequences() != null)
1534 HiddenSequences hs = toappend.getHiddenSequences();
1535 if (hiddenSequences == null)
1537 hiddenSequences = new HiddenSequences(this);
1539 if (hs.hiddenSequences != null)
1541 for (int s = 0; s < hs.hiddenSequences.length; s++)
1543 // hide the newly appended sequence in the alignment
1544 if (hs.hiddenSequences[s] != null)
1546 hiddenSequences.hideSequence(hs.hiddenSequences[s]);
1551 if (toappend.getProperties() != null)
1553 // we really can't do very much here - just try to concatenate strings
1554 // where property collisions occur.
1555 Enumeration key = toappend.getProperties().keys();
1556 while (key.hasMoreElements())
1558 Object k = key.nextElement();
1559 Object ourval = this.getProperty(k);
1560 Object toapprop = toappend.getProperty(k);
1563 if (ourval.getClass().equals(toapprop.getClass())
1564 && !ourval.equals(toapprop))
1566 if (ourval instanceof String)
1569 this.setProperty(k, ((String) ourval) + "; "
1570 + ((String) toapprop));
1574 if (ourval instanceof Vector)
1577 Enumeration theirv = ((Vector) toapprop).elements();
1578 while (theirv.hasMoreElements())
1580 ((Vector) ourval).addElement(theirv);
1588 // just add new property directly
1589 setProperty(k, toapprop);
1597 public AlignmentAnnotation findOrCreateAnnotation(String name,
1598 String calcId, boolean autoCalc, SequenceI seqRef,
1599 SequenceGroup groupRef)
1601 if (annotations != null)
1603 for (AlignmentAnnotation annot : getAlignmentAnnotation())
1605 if (annot.autoCalculated == autoCalc && (name.equals(annot.label))
1606 && (calcId == null || annot.getCalcId().equals(calcId))
1607 && annot.sequenceRef == seqRef
1608 && annot.groupRef == groupRef)
1614 AlignmentAnnotation annot = new AlignmentAnnotation(name, name,
1615 new Annotation[1], 0f, 0f, AlignmentAnnotation.BAR_GRAPH);
1616 annot.hasText = false;
1617 annot.setCalcId(new String(calcId));
1618 annot.autoCalculated = autoCalc;
1621 annot.setSequenceRef(seqRef);
1623 annot.groupRef = groupRef;
1624 addAnnotation(annot);
1630 public Iterable<AlignmentAnnotation> findAnnotation(String calcId)
1632 List<AlignmentAnnotation> aa = new ArrayList<>();
1633 AlignmentAnnotation[] alignmentAnnotation = getAlignmentAnnotation();
1634 if (alignmentAnnotation != null)
1636 for (AlignmentAnnotation a : alignmentAnnotation)
1638 if (a.getCalcId() == calcId
1639 || (a.getCalcId() != null && calcId != null && a
1640 .getCalcId().equals(calcId)))
1650 public Iterable<AlignmentAnnotation> findAnnotations(SequenceI seq,
1651 String calcId, String label)
1653 ArrayList<AlignmentAnnotation> aa = new ArrayList<>();
1654 for (AlignmentAnnotation ann : getAlignmentAnnotation())
1656 if ((calcId == null || (ann.getCalcId() != null && ann.getCalcId()
1658 && (seq == null || (ann.sequenceRef != null && ann.sequenceRef == seq))
1659 && (label == null || (ann.label != null && ann.label
1669 public void moveSelectedSequencesByOne(SequenceGroup sg,
1670 Map<SequenceI, SequenceCollectionI> map, boolean up)
1672 synchronized (sequences)
1677 for (int i = 1, iSize = sequences.size(); i < iSize; i++)
1679 SequenceI seq = sequences.get(i);
1680 if (!sg.getSequences(map).contains(seq))
1685 SequenceI temp = sequences.get(i - 1);
1686 if (sg.getSequences(null).contains(temp))
1691 sequences.set(i, temp);
1692 sequences.set(i - 1, seq);
1697 for (int i = sequences.size() - 2; i > -1; i--)
1699 SequenceI seq = sequences.get(i);
1700 if (!sg.getSequences(map).contains(seq))
1705 SequenceI temp = sequences.get(i + 1);
1706 if (sg.getSequences(map).contains(temp))
1711 sequences.set(i, temp);
1712 sequences.set(i + 1, seq);
1720 public void validateAnnotation(AlignmentAnnotation alignmentAnnotation)
1722 alignmentAnnotation.validateRangeAndDisplay();
1723 if (isNucleotide() && alignmentAnnotation.isValidStruc())
1725 hasRNAStructure = true;
1729 private SequenceI seqrep = null;
1733 * @return the representative sequence for this group
1736 public SequenceI getSeqrep()
1742 * set the representative sequence for this group. Note - this affects the
1743 * interpretation of the Hidereps attribute.
1746 * the seqrep to set (null means no sequence representative)
1749 public void setSeqrep(SequenceI seqrep)
1751 this.seqrep = seqrep;
1756 * @return true if group has a sequence representative
1759 public boolean hasSeqrep()
1761 return seqrep != null;
1765 public int getEndRes()
1767 return getWidth() - 1;
1771 public int getStartRes()
1777 * In the case of AlignmentI - returns the dataset for the alignment, if set
1780 * @see jalview.datamodel.AnnotatedCollectionI#getContext()
1783 public AnnotatedCollectionI getContext()
1789 * Align this alignment like the given (mapped) one.
1792 public int alignAs(AlignmentI al)
1795 * Currently retains unmapped gaps (in introns), regaps mapped regions
1798 return alignAs(al, false, true);
1802 * Align this alignment 'the same as' the given one. Mapped sequences only are
1803 * realigned. If both of the same type (nucleotide/protein) then align both
1804 * identically. If this is nucleotide and the other is protein, make 3 gaps
1805 * for each gap in the protein sequences. If this is protein and the other is
1806 * nucleotide, insert a gap for each 3 gaps (or part thereof) between
1807 * nucleotide bases. If this is protein and the other is nucleotide, gaps
1808 * protein to match the relative ordering of codons in the nucleotide.
1810 * Parameters control whether gaps in exon (mapped) and intron (unmapped)
1811 * regions are preserved. Gaps that connect introns to exons are treated
1812 * conservatively, i.e. only preserved if both intron and exon gaps are
1813 * preserved. TODO: check caveats below where the implementation fails
1816 * - must have same dataset, and sequences in al must have equivalent
1817 * dataset sequence and start/end bounds under given mapping
1818 * @param preserveMappedGaps
1819 * if true, gaps within and between mapped codons are preserved
1820 * @param preserveUnmappedGaps
1821 * if true, gaps within and between unmapped codons are preserved
1824 public int alignAs(AlignmentI al, boolean preserveMappedGaps,
1825 boolean preserveUnmappedGaps)
1827 // TODO should this method signature be the one in the interface?
1828 // JBPComment - yes - neither flag is used, so should be deleted.
1829 boolean thisIsNucleotide = this.isNucleotide();
1830 boolean thatIsProtein = !al.isNucleotide();
1831 if (!thatIsProtein && !thisIsNucleotide)
1833 return AlignmentUtils.alignProteinAsDna(this, al);
1835 else if (thatIsProtein && thisIsNucleotide)
1837 return AlignmentUtils.alignCdsAsProtein(this, al);
1839 return AlignmentUtils.alignAs(this, al);
1843 * Returns the alignment in Fasta format. Behaviour of this method is not
1844 * guaranteed between versions.
1847 public String toString()
1849 return new FastaFile().print(getSequencesArray(), true);
1853 * Returns the set of distinct sequence names. No ordering is guaranteed.
1856 public Set<String> getSequenceNames()
1858 Set<String> names = new HashSet<>();
1859 for (SequenceI seq : getSequences())
1861 names.add(seq.getName());
1867 public boolean hasValidSequence()
1869 boolean hasValidSeq = false;
1870 for (SequenceI seq : getSequences())
1872 if ((seq.getEnd() - seq.getStart()) > 0)
1882 * Update any mappings to 'virtual' sequences to compatible real ones, if
1883 * present in the added sequences. Returns a count of mappings updated.
1889 public int realiseMappings(List<SequenceI> seqs)
1892 for (SequenceI seq : seqs)
1894 for (AlignedCodonFrame mapping : getCodonFrames())
1896 count += mapping.realiseWith(seq);
1903 * Returns the first AlignedCodonFrame that has a mapping between the given
1911 public AlignedCodonFrame getMapping(SequenceI mapFrom, SequenceI mapTo)
1913 for (AlignedCodonFrame acf : getCodonFrames())
1915 if (acf.getAaForDnaSeq(mapFrom) == mapTo)
1924 public int[] getVisibleStartAndEndIndex(List<int[]> hiddenCols)
1926 int[] alignmentStartEnd = new int[] { 0, getWidth() - 1 };
1927 int startPos = alignmentStartEnd[0];
1928 int endPos = alignmentStartEnd[1];
1930 int[] lowestRange = new int[] { -1, -1 };
1931 int[] higestRange = new int[] { -1, -1 };
1933 for (int[] hiddenCol : hiddenCols)
1935 lowestRange = (hiddenCol[0] <= startPos) ? hiddenCol : lowestRange;
1936 higestRange = (hiddenCol[1] >= endPos) ? hiddenCol : higestRange;
1939 if (lowestRange[0] == -1 && lowestRange[1] == -1)
1941 startPos = alignmentStartEnd[0];
1945 startPos = lowestRange[1] + 1;
1948 if (higestRange[0] == -1 && higestRange[1] == -1)
1950 endPos = alignmentStartEnd[1];
1954 endPos = higestRange[0] - 1;
1956 return new int[] { startPos, endPos };
1960 public void setHiddenColumns(HiddenColumns cols)
1966 public HiddenMarkovModel getHMM()
1973 public void setHMM(HiddenMarkovModel markov)