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.MessageManager;
29 import java.util.ArrayList;
30 import java.util.Collections;
31 import java.util.Enumeration;
32 import java.util.HashSet;
33 import java.util.Hashtable;
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)
230 // maintain dataset integrity
231 SequenceI dsseq = snew.getDatasetSequence();
234 // derive new sequence
235 SequenceI adding = snew.deriveSequence();
237 dsseq = snew.getDatasetSequence();
239 if (getDataset().findIndex(dsseq) == -1)
241 getDataset().addSequence(dsseq);
245 if (sequences == null)
247 initAlignment(new SequenceI[] { snew });
251 synchronized (sequences)
256 if (hiddenSequences != null)
258 hiddenSequences.adjustHeightSequenceAdded();
263 public SequenceI replaceSequenceAt(int i, SequenceI snew)
265 synchronized (sequences)
267 if (sequences.size() > i)
269 return sequences.set(i, snew);
275 hiddenSequences.adjustHeightSequenceAdded();
284 * @return DOCUMENT ME!
287 public List<SequenceGroup> getGroups()
293 public void finalize()
295 if (getDataset() != null)
297 getDataset().removeAlignmentRef();
304 hiddenSequences = null;
308 * decrement the alignmentRefs counter by one and call finalize if it goes to
311 private void removeAlignmentRef()
313 if (--alignmentRefs == 0)
326 public void deleteSequence(SequenceI s)
328 deleteSequence(findIndex(s));
338 public void deleteSequence(int i)
340 if (i > -1 && i < getHeight())
342 synchronized (sequences)
345 hiddenSequences.adjustHeightSequenceDeleted(i);
353 * @see jalview.datamodel.AlignmentI#findGroup(jalview.datamodel.SequenceI)
356 public SequenceGroup findGroup(SequenceI s)
358 synchronized (groups)
360 for (int i = 0; i < this.groups.size(); i++)
362 SequenceGroup sg = groups.get(i);
364 if (sg.getSequences(null).contains(s))
377 * jalview.datamodel.AlignmentI#findAllGroups(jalview.datamodel.SequenceI)
380 public SequenceGroup[] findAllGroups(SequenceI s)
382 ArrayList<SequenceGroup> temp = new ArrayList<SequenceGroup>();
384 synchronized (groups)
386 int gSize = groups.size();
387 for (int i = 0; i < gSize; i++)
389 SequenceGroup sg = groups.get(i);
390 if (sg == null || sg.getSequences() == null)
392 this.deleteGroup(sg);
397 if (sg.getSequences().contains(s))
403 SequenceGroup[] ret = new SequenceGroup[temp.size()];
404 return temp.toArray(ret);
409 public void addGroup(SequenceGroup sg)
411 synchronized (groups)
413 if (!groups.contains(sg))
415 if (hiddenSequences.getSize() > 0)
417 int i, iSize = sg.getSize();
418 for (i = 0; i < iSize; i++)
420 if (!sequences.contains(sg.getSequenceAt(i)))
422 sg.deleteSequence(sg.getSequenceAt(i), false);
428 if (sg.getSize() < 1)
440 * remove any annotation that references gp
443 * (if null, removes all group associated annotation)
445 private void removeAnnotationForGroup(SequenceGroup gp)
447 if (annotations == null || annotations.length == 0)
451 // remove annotation very quickly
452 AlignmentAnnotation[] t, todelete = new AlignmentAnnotation[annotations.length], tokeep = new AlignmentAnnotation[annotations.length];
456 for (i = 0, p = 0, k = 0; i < annotations.length; i++)
458 if (annotations[i].groupRef != null)
460 todelete[p++] = annotations[i];
464 tokeep[k++] = annotations[i];
470 for (i = 0, p = 0, k = 0; i < annotations.length; i++)
472 if (annotations[i].groupRef == gp)
474 todelete[p++] = annotations[i];
478 tokeep[k++] = annotations[i];
484 // clear out the group associated annotation.
485 for (i = 0; i < p; i++)
487 unhookAnnotation(todelete[i]);
490 t = new AlignmentAnnotation[k];
491 for (i = 0; i < k; i++)
500 public void deleteAllGroups()
502 synchronized (groups)
504 if (annotations != null)
506 removeAnnotationForGroup(null);
508 for (SequenceGroup sg : groups)
518 public void deleteGroup(SequenceGroup g)
520 synchronized (groups)
522 if (groups.contains(g))
524 removeAnnotationForGroup(g);
533 public SequenceI findName(String name)
535 return findName(name, false);
541 * @see jalview.datamodel.AlignmentI#findName(java.lang.String, boolean)
544 public SequenceI findName(String token, boolean b)
546 return findName(null, token, b);
552 * @see jalview.datamodel.AlignmentI#findName(SequenceI, java.lang.String,
556 public SequenceI findName(SequenceI startAfter, String token, boolean b)
561 String sqname = null;
562 if (startAfter != null)
564 // try to find the sequence in the alignment
565 boolean matched = false;
566 while (i < sequences.size())
568 if (getSequenceAt(i++) == startAfter)
579 while (i < sequences.size())
581 sq = getSequenceAt(i);
582 sqname = sq.getName();
583 if (sqname.equals(token) // exact match
584 || (b && // allow imperfect matches - case varies
585 (sqname.equalsIgnoreCase(token))))
587 return getSequenceAt(i);
597 public SequenceI[] findSequenceMatch(String name)
599 Vector matches = new Vector();
602 while (i < sequences.size())
604 if (getSequenceAt(i).getName().equals(name))
606 matches.addElement(getSequenceAt(i));
611 SequenceI[] result = new SequenceI[matches.size()];
612 for (i = 0; i < result.length; i++)
614 result[i] = (SequenceI) matches.elementAt(i);
624 * @see jalview.datamodel.AlignmentI#findIndex(jalview.datamodel.SequenceI)
627 public int findIndex(SequenceI s)
631 while (i < sequences.size())
633 if (s == getSequenceAt(i))
648 * jalview.datamodel.AlignmentI#findIndex(jalview.datamodel.SearchResults)
651 public int findIndex(SearchResults results)
655 while (i < sequences.size())
657 if (results.involvesSequence(getSequenceAt(i)))
669 * @return DOCUMENT ME!
672 public int getHeight()
674 return sequences.size();
680 * @return DOCUMENT ME!
683 public int getWidth()
687 for (int i = 0; i < sequences.size(); i++)
689 if (getSequenceAt(i).getLength() > maxLength)
691 maxLength = getSequenceAt(i).getLength();
705 public void setGapCharacter(char gc)
708 synchronized (sequences)
710 for (SequenceI seq : sequences)
712 seq.setSequence(seq.getSequenceAsString().replace('.', gc)
713 .replace('-', gc).replace(' ', gc));
721 * @return DOCUMENT ME!
724 public char getGapCharacter()
732 * @see jalview.datamodel.AlignmentI#isAligned()
735 public boolean isAligned()
737 return isAligned(false);
743 * @see jalview.datamodel.AlignmentI#isAligned(boolean)
746 public boolean isAligned(boolean includeHidden)
748 int width = getWidth();
749 if (hiddenSequences == null || hiddenSequences.getSize() == 0)
751 includeHidden = true; // no hidden sequences to check against.
753 for (int i = 0; i < sequences.size(); i++)
755 if (includeHidden || !hiddenSequences.isHidden(getSequenceAt(i)))
757 if (getSequenceAt(i).getLength() != width)
768 * Delete all annotations, including auto-calculated if the flag is set true.
769 * Returns true if at least one annotation was deleted, else false.
771 * @param includingAutoCalculated
775 public boolean deleteAllAnnotations(boolean includingAutoCalculated)
777 boolean result = false;
778 for (AlignmentAnnotation alan : getAlignmentAnnotation())
780 if (!alan.autoCalculated || includingAutoCalculated)
782 deleteAnnotation(alan);
792 * @seejalview.datamodel.AlignmentI#deleteAnnotation(jalview.datamodel.
793 * AlignmentAnnotation)
796 public boolean deleteAnnotation(AlignmentAnnotation aa)
798 return deleteAnnotation(aa, true);
802 public boolean deleteAnnotation(AlignmentAnnotation aa, boolean unhook)
806 if (annotations != null)
808 aSize = annotations.length;
816 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize - 1];
818 boolean swap = false;
821 for (int i = 0; i < aSize; i++)
823 if (annotations[i] == aa)
828 if (tIndex < temp.length)
830 temp[tIndex++] = annotations[i];
839 unhookAnnotation(aa);
846 * remove any object references associated with this annotation
850 private void unhookAnnotation(AlignmentAnnotation aa)
852 if (aa.sequenceRef != null)
854 aa.sequenceRef.removeAlignmentAnnotation(aa);
856 if (aa.groupRef != null)
858 // probably need to do more here in the future (post 2.5.0)
866 * @seejalview.datamodel.AlignmentI#addAnnotation(jalview.datamodel.
867 * AlignmentAnnotation)
870 public void addAnnotation(AlignmentAnnotation aa)
872 addAnnotation(aa, -1);
878 * @seejalview.datamodel.AlignmentI#addAnnotation(jalview.datamodel.
879 * AlignmentAnnotation, int)
882 public void addAnnotation(AlignmentAnnotation aa, int pos)
884 if (aa.getRNAStruc() != null)
886 hasRNAStructure = true;
890 if (annotations != null)
892 aSize = annotations.length + 1;
895 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
897 if (pos == -1 || pos >= aSize)
899 temp[aSize - 1] = aa;
908 for (i = 0; i < (aSize - 1); i++, p++)
916 temp[p] = annotations[i];
925 public void setAnnotationIndex(AlignmentAnnotation aa, int index)
927 if (aa == null || annotations == null || annotations.length - 1 < index)
932 int aSize = annotations.length;
933 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
937 for (int i = 0; i < aSize; i++)
946 temp[i] = annotations[i];
950 temp[i] = annotations[i - 1];
959 * returns all annotation on the alignment
961 public AlignmentAnnotation[] getAlignmentAnnotation()
967 public void setNucleotide(boolean b)
980 public boolean isNucleotide()
982 if (type == NUCLEOTIDE)
993 public boolean hasRNAStructure()
995 // TODO can it happen that structure is removed from alignment?
996 return hasRNAStructure;
1000 public void setDataset(AlignmentI data)
1002 if (dataset == null && data == null)
1004 createDatasetAlignment();
1006 else if (dataset == null && data != null)
1008 if (!(data instanceof Alignment))
1011 "Implementation Error: jalview.datamodel.Alignment does not yet support other implementations of AlignmentI as its dataset reference");
1013 dataset = (Alignment) data;
1014 for (int i = 0; i < getHeight(); i++)
1016 SequenceI currentSeq = getSequenceAt(i);
1017 SequenceI dsq = currentSeq.getDatasetSequence();
1020 dsq = currentSeq.createDatasetSequence();
1021 dataset.addSequence(dsq);
1025 while (dsq.getDatasetSequence() != null)
1027 dsq = dsq.getDatasetSequence();
1029 if (dataset.findIndex(dsq) == -1)
1031 dataset.addSequence(dsq);
1036 dataset.addAlignmentRef();
1040 * add dataset sequences to seq for currentSeq and any sequences it references
1042 private void resolveAndAddDatasetSeq(SequenceI currentSeq,
1043 Set<SequenceI> seqs, boolean createDatasetSequence)
1045 if (currentSeq.getDatasetSequence() != null)
1047 currentSeq = currentSeq.getDatasetSequence();
1051 if (createDatasetSequence)
1053 currentSeq = currentSeq.createDatasetSequence();
1056 if (seqs.contains(currentSeq))
1060 List<SequenceI> toProcess = new ArrayList<SequenceI>();
1061 toProcess.add(currentSeq);
1062 while (toProcess.size() > 0)
1065 SequenceI curDs = toProcess.remove(0);
1066 if (seqs.contains(curDs))
1071 // iterate over database references, making sure we add forward referenced
1073 if (curDs.getDBRefs() != null)
1075 for (DBRefEntry dbr : curDs.getDBRefs())
1077 if (dbr.getMap() != null && dbr.getMap().getTo() != null)
1079 if (dbr.getMap().getTo().getDatasetSequence() != null)
1081 throw new Error("Implementation error: Map.getTo() for dbref"
1082 + dbr + " is not a dataset sequence.");
1083 // TODO: if this happens, could also rewrite the reference to
1084 // point to new dataset sequence
1086 // we recurse to add all forward references to dataset sequences via
1088 toProcess.add(dbr.getMap().getTo());
1096 * Creates a new dataset for this alignment. Can only be done once - if
1097 * dataset is not null this will not be performed.
1099 public void createDatasetAlignment()
1101 if (dataset != null)
1105 // try to avoid using SequenceI.equals at this stage, it will be expensive
1106 Set<SequenceI> seqs = new jalview.util.LinkedIdentityHashSet<SequenceI>();
1108 for (int i = 0; i < getHeight(); i++)
1110 SequenceI currentSeq = getSequenceAt(i);
1111 resolveAndAddDatasetSeq(currentSeq, seqs, true);
1114 // verify all mappings are in dataset
1115 for (AlignedCodonFrame cf : codonFrameList)
1117 for (SequenceToSequenceMapping ssm : cf.getMappings())
1119 if (!seqs.contains(ssm.getFromSeq()))
1121 resolveAndAddDatasetSeq(ssm.getFromSeq(), seqs, false);
1123 if (!seqs.contains(ssm.getMapping().getTo()))
1125 resolveAndAddDatasetSeq(ssm.getMapping().getTo(), seqs, false);
1129 // finally construct dataset
1130 dataset = new Alignment(seqs.toArray(new SequenceI[seqs.size()]));
1131 // move mappings to the dataset alignment
1132 dataset.codonFrameList = this.codonFrameList;
1133 this.codonFrameList = null;
1137 * reference count for number of alignments referencing this one.
1139 int alignmentRefs = 0;
1142 * increase reference count to this alignment.
1144 private void addAlignmentRef()
1150 public Alignment getDataset()
1156 public boolean padGaps()
1158 boolean modified = false;
1160 // Remove excess gaps from the end of alignment
1164 for (int i = 0; i < sequences.size(); i++)
1166 current = getSequenceAt(i);
1167 for (int j = current.getLength(); j > maxLength; j--)
1170 && !jalview.util.Comparison.isGap(current.getCharAt(j)))
1181 for (int i = 0; i < sequences.size(); i++)
1183 current = getSequenceAt(i);
1184 cLength = current.getLength();
1186 if (cLength < maxLength)
1188 current.insertCharAt(cLength, maxLength - cLength, gapCharacter);
1191 else if (current.getLength() > maxLength)
1193 current.deleteChars(maxLength, current.getLength());
1200 * Justify the sequences to the left or right by deleting and inserting gaps
1201 * before the initial residue or after the terminal residue
1204 * true if alignment padded to right, false to justify to left
1205 * @return true if alignment was changed
1208 public boolean justify(boolean right)
1210 boolean modified = false;
1212 // Remove excess gaps from the end of alignment
1214 int ends[] = new int[sequences.size() * 2];
1216 for (int i = 0; i < sequences.size(); i++)
1218 current = getSequenceAt(i);
1219 // This should really be a sequence method
1220 ends[i * 2] = current.findIndex(current.getStart());
1221 ends[i * 2 + 1] = current.findIndex(current.getStart()
1222 + current.getLength());
1223 boolean hitres = false;
1224 for (int j = 0, rs = 0, ssiz = current.getLength(); j < ssiz; j++)
1226 if (!jalview.util.Comparison.isGap(current.getCharAt(j)))
1235 ends[i * 2 + 1] = j;
1236 if (j - ends[i * 2] > maxLength)
1238 maxLength = j - ends[i * 2];
1246 // now edit the flanking gaps to justify to either left or right
1247 int cLength, extent, diff;
1248 for (int i = 0; i < sequences.size(); i++)
1250 current = getSequenceAt(i);
1252 cLength = 1 + ends[i * 2 + 1] - ends[i * 2];
1253 diff = maxLength - cLength; // number of gaps to indent
1254 extent = current.getLength();
1258 if (extent > ends[i * 2 + 1])
1260 current.deleteChars(ends[i * 2 + 1] + 1, extent);
1263 if (ends[i * 2] > diff)
1265 current.deleteChars(0, ends[i * 2] - diff);
1270 if (ends[i * 2] < diff)
1272 current.insertCharAt(0, diff - ends[i * 2], gapCharacter);
1280 if (ends[i * 2] > 0)
1282 current.deleteChars(0, ends[i * 2]);
1284 ends[i * 2 + 1] -= ends[i * 2];
1285 extent -= ends[i * 2];
1287 if (extent > maxLength)
1289 current.deleteChars(maxLength + 1, extent);
1294 if (extent < maxLength)
1296 current.insertCharAt(extent, maxLength - extent, gapCharacter);
1306 public HiddenSequences getHiddenSequences()
1308 return hiddenSequences;
1312 public CigarArray getCompactAlignment()
1314 synchronized (sequences)
1316 SeqCigar alseqs[] = new SeqCigar[sequences.size()];
1318 for (SequenceI seq : sequences)
1320 alseqs[i++] = new SeqCigar(seq);
1322 CigarArray cal = new CigarArray(alseqs);
1323 cal.addOperation(CigarArray.M, getWidth());
1329 public void setProperty(Object key, Object value)
1331 if (alignmentProperties == null)
1333 alignmentProperties = new Hashtable();
1336 alignmentProperties.put(key, value);
1340 public Object getProperty(Object key)
1342 if (alignmentProperties != null)
1344 return alignmentProperties.get(key);
1353 public Hashtable getProperties()
1355 return alignmentProperties;
1359 * Adds the given mapping to the stored set. Note this may be held on the
1360 * dataset alignment.
1363 public void addCodonFrame(AlignedCodonFrame codons)
1365 List<AlignedCodonFrame> acfs = getCodonFrames();
1366 if (codons != null && acfs != null && !acfs.contains(codons))
1376 * jalview.datamodel.AlignmentI#getCodonFrame(jalview.datamodel.SequenceI)
1379 public List<AlignedCodonFrame> getCodonFrame(SequenceI seq)
1385 List<AlignedCodonFrame> cframes = new ArrayList<AlignedCodonFrame>();
1386 for (AlignedCodonFrame acf : getCodonFrames())
1388 if (acf.involvesSequence(seq))
1397 * Sets the codon frame mappings (replacing any existing mappings). Note the
1398 * mappings are set on the dataset alignment instead if there is one.
1400 * @see jalview.datamodel.AlignmentI#setCodonFrames()
1403 public void setCodonFrames(List<AlignedCodonFrame> acfs)
1405 if (dataset != null)
1407 dataset.setCodonFrames(acfs);
1411 this.codonFrameList = acfs;
1416 * Returns the set of codon frame mappings. Any changes to the returned set
1417 * will affect the alignment. The mappings are held on (and read from) the
1418 * dataset alignment if there is one.
1420 * @see jalview.datamodel.AlignmentI#getCodonFrames()
1423 public List<AlignedCodonFrame> getCodonFrames()
1425 // TODO: Fix this method to fix failing AlignedCodonFrame tests
1426 // this behaviour is currently incorrect. method should return codon frames
1427 // for just the alignment,
1428 // selected from dataset
1429 return dataset != null ? dataset.getCodonFrames() : codonFrameList;
1433 * Removes the given mapping from the stored set. Note that the mappings are
1434 * held on the dataset alignment if there is one.
1437 public boolean removeCodonFrame(AlignedCodonFrame codons)
1439 List<AlignedCodonFrame> acfs = getCodonFrames();
1440 if (codons == null || acfs == null)
1444 return acfs.remove(codons);
1448 public void append(AlignmentI toappend)
1450 // TODO JAL-1270 needs test coverage
1451 // currently tested for use in jalview.gui.SequenceFetcher
1452 boolean samegap = toappend.getGapCharacter() == getGapCharacter();
1453 char oldc = toappend.getGapCharacter();
1454 boolean hashidden = toappend.getHiddenSequences() != null
1455 && toappend.getHiddenSequences().hiddenSequences != null;
1456 // get all sequences including any hidden ones
1457 List<SequenceI> sqs = (hashidden) ? toappend.getHiddenSequences()
1458 .getFullAlignment().getSequences() : toappend.getSequences();
1461 // avoid self append deadlock by
1462 List<SequenceI> toappendsq = new ArrayList<SequenceI>();
1465 for (SequenceI addedsq : sqs)
1469 char[] oldseq = addedsq.getSequence();
1470 for (int c = 0; c < oldseq.length; c++)
1472 if (oldseq[c] == oldc)
1474 oldseq[c] = gapCharacter;
1478 toappendsq.add(addedsq);
1481 for (SequenceI addedsq : toappendsq)
1483 addSequence(addedsq);
1486 AlignmentAnnotation[] alan = toappend.getAlignmentAnnotation();
1487 for (int a = 0; alan != null && a < alan.length; a++)
1489 addAnnotation(alan[a]);
1493 getCodonFrames().addAll(toappend.getCodonFrames());
1495 List<SequenceGroup> sg = toappend.getGroups();
1498 for (SequenceGroup _sg : sg)
1503 if (toappend.getHiddenSequences() != null)
1505 HiddenSequences hs = toappend.getHiddenSequences();
1506 if (hiddenSequences == null)
1508 hiddenSequences = new HiddenSequences(this);
1510 if (hs.hiddenSequences != null)
1512 for (int s = 0; s < hs.hiddenSequences.length; s++)
1514 // hide the newly appended sequence in the alignment
1515 if (hs.hiddenSequences[s] != null)
1517 hiddenSequences.hideSequence(hs.hiddenSequences[s]);
1522 if (toappend.getProperties() != null)
1524 // we really can't do very much here - just try to concatenate strings
1525 // where property collisions occur.
1526 Enumeration key = toappend.getProperties().keys();
1527 while (key.hasMoreElements())
1529 Object k = key.nextElement();
1530 Object ourval = this.getProperty(k);
1531 Object toapprop = toappend.getProperty(k);
1534 if (ourval.getClass().equals(toapprop.getClass())
1535 && !ourval.equals(toapprop))
1537 if (ourval instanceof String)
1540 this.setProperty(k, ((String) ourval) + "; "
1541 + ((String) toapprop));
1545 if (ourval instanceof Vector)
1548 Enumeration theirv = ((Vector) toapprop).elements();
1549 while (theirv.hasMoreElements())
1551 ((Vector) ourval).addElement(theirv);
1559 // just add new property directly
1560 setProperty(k, toapprop);
1568 public AlignmentAnnotation findOrCreateAnnotation(String name,
1569 String calcId, boolean autoCalc, SequenceI seqRef,
1570 SequenceGroup groupRef)
1572 assert (name != null);
1573 if (annotations != null)
1575 for (AlignmentAnnotation annot : getAlignmentAnnotation())
1577 if (annot.autoCalculated == autoCalc && (name.equals(annot.label))
1578 && (calcId == null || annot.getCalcId().equals(calcId))
1579 && annot.sequenceRef == seqRef
1580 && annot.groupRef == groupRef)
1586 AlignmentAnnotation annot = new AlignmentAnnotation(name, name,
1587 new Annotation[1], 0f, 0f, AlignmentAnnotation.BAR_GRAPH);
1588 annot.hasText = false;
1589 annot.setCalcId(new String(calcId));
1590 annot.autoCalculated = autoCalc;
1593 annot.setSequenceRef(seqRef);
1595 annot.groupRef = groupRef;
1596 addAnnotation(annot);
1602 public Iterable<AlignmentAnnotation> findAnnotation(String calcId)
1604 ArrayList<AlignmentAnnotation> aa = new ArrayList<AlignmentAnnotation>();
1605 for (AlignmentAnnotation a : getAlignmentAnnotation())
1607 if (a.getCalcId() == calcId
1608 || (a.getCalcId() != null && calcId != null && a.getCalcId()
1618 * Returns an iterable collection of any annotations that match on given
1619 * sequence ref, calcId and label (ignoring null values).
1622 public Iterable<AlignmentAnnotation> findAnnotations(SequenceI seq,
1623 String calcId, String label)
1625 ArrayList<AlignmentAnnotation> aa = new ArrayList<AlignmentAnnotation>();
1626 for (AlignmentAnnotation ann : getAlignmentAnnotation())
1628 if (ann.getCalcId() != null && ann.getCalcId().equals(calcId)
1629 && ann.sequenceRef != null && ann.sequenceRef == seq
1630 && ann.label != null && ann.label.equals(label))
1639 public void moveSelectedSequencesByOne(SequenceGroup sg,
1640 Map<SequenceI, SequenceCollectionI> map, boolean up)
1642 synchronized (sequences)
1647 for (int i = 1, iSize = sequences.size(); i < iSize; i++)
1649 SequenceI seq = sequences.get(i);
1650 if (!sg.getSequences(map).contains(seq))
1655 SequenceI temp = sequences.get(i - 1);
1656 if (sg.getSequences(null).contains(temp))
1661 sequences.set(i, temp);
1662 sequences.set(i - 1, seq);
1667 for (int i = sequences.size() - 2; i > -1; i--)
1669 SequenceI seq = sequences.get(i);
1670 if (!sg.getSequences(map).contains(seq))
1675 SequenceI temp = sequences.get(i + 1);
1676 if (sg.getSequences(map).contains(temp))
1681 sequences.set(i, temp);
1682 sequences.set(i + 1, seq);
1690 public void validateAnnotation(AlignmentAnnotation alignmentAnnotation)
1692 alignmentAnnotation.validateRangeAndDisplay();
1693 if (isNucleotide() && alignmentAnnotation.isValidStruc())
1695 hasRNAStructure = true;
1699 private SequenceI seqrep = null;
1703 * @return the representative sequence for this group
1706 public SequenceI getSeqrep()
1712 * set the representative sequence for this group. Note - this affects the
1713 * interpretation of the Hidereps attribute.
1716 * the seqrep to set (null means no sequence representative)
1719 public void setSeqrep(SequenceI seqrep)
1721 this.seqrep = seqrep;
1726 * @return true if group has a sequence representative
1729 public boolean hasSeqrep()
1731 return seqrep != null;
1735 public int getEndRes()
1737 return getWidth() - 1;
1741 public int getStartRes()
1747 * In the case of AlignmentI - returns the dataset for the alignment, if set
1750 * @see jalview.datamodel.AnnotatedCollectionI#getContext()
1753 public AnnotatedCollectionI getContext()
1759 * Align this alignment like the given (mapped) one.
1762 public int alignAs(AlignmentI al)
1765 * Currently retains unmapped gaps (in introns), regaps mapped regions
1768 return alignAs(al, false, true);
1772 * Align this alignment 'the same as' the given one. Mapped sequences only are
1773 * realigned. If both of the same type (nucleotide/protein) then align both
1774 * identically. If this is nucleotide and the other is protein, make 3 gaps
1775 * for each gap in the protein sequences. If this is protein and the other is
1776 * nucleotide, insert a gap for each 3 gaps (or part thereof) between
1777 * nucleotide bases. If this is protein and the other is nucleotide, gaps
1778 * protein to match the relative ordering of codons in the nucleotide.
1780 * Parameters control whether gaps in exon (mapped) and intron (unmapped)
1781 * regions are preserved. Gaps that connect introns to exons are treated
1782 * conservatively, i.e. only preserved if both intron and exon gaps are
1783 * preserved. TODO: check caveats below where the implementation fails
1786 * - must have same dataset, and sequences in al must have equivalent
1787 * dataset sequence and start/end bounds under given mapping
1788 * @param preserveMappedGaps
1789 * if true, gaps within and between mapped codons are preserved
1790 * @param preserveUnmappedGaps
1791 * if true, gaps within and between unmapped codons are preserved
1794 public int alignAs(AlignmentI al, boolean preserveMappedGaps,
1795 boolean preserveUnmappedGaps)
1797 // TODO should this method signature be the one in the interface?
1798 // JBPComment - yes - neither flag is used, so should be deleted.
1799 boolean thisIsNucleotide = this.isNucleotide();
1800 boolean thatIsProtein = !al.isNucleotide();
1801 if (!thatIsProtein && !thisIsNucleotide)
1803 return AlignmentUtils.alignProteinAsDna(this, al);
1805 else if (thatIsProtein && thisIsNucleotide)
1807 return AlignmentUtils.alignCdsAsProtein(this, al);
1809 return AlignmentUtils.alignAs(this, al);
1813 * Returns the alignment in Fasta format. Behaviour of this method is not
1814 * guaranteed between versions.
1817 public String toString()
1819 return new FastaFile().print(getSequencesArray());
1823 * Returns the set of distinct sequence names. No ordering is guaranteed.
1826 public Set<String> getSequenceNames()
1828 Set<String> names = new HashSet<String>();
1829 for (SequenceI seq : getSequences())
1831 names.add(seq.getName());
1837 public boolean hasValidSequence()
1839 boolean hasValidSeq = false;
1840 for (SequenceI seq : getSequences())
1842 if ((seq.getEnd() - seq.getStart()) > 0)
1852 * Update any mappings to 'virtual' sequences to compatible real ones, if
1853 * present in the added sequences. Returns a count of mappings updated.
1859 public int realiseMappings(List<SequenceI> seqs)
1862 for (SequenceI seq : seqs)
1864 for (AlignedCodonFrame mapping : getCodonFrames())
1866 count += mapping.realiseWith(seq);
1873 * Returns the first AlignedCodonFrame that has a mapping between the given
1881 public AlignedCodonFrame getMapping(SequenceI mapFrom, SequenceI mapTo)
1883 for (AlignedCodonFrame acf : getCodonFrames())
1885 if (acf.getAaForDnaSeq(mapFrom) == mapTo)
1894 public int[] getVisibleStartAndEndIndex(List<int[]> hiddenCols)
1896 int[] alignmentStartEnd = new int[] { 0, getWidth() - 1 };
1897 int startPos = alignmentStartEnd[0];
1898 int endPos = alignmentStartEnd[1];
1900 int[] lowestRange = new int[] { -1, -1 };
1901 int[] higestRange = new int[] { -1, -1 };
1903 for (int[] hiddenCol : hiddenCols)
1905 lowestRange = (hiddenCol[0] <= startPos) ? hiddenCol : lowestRange;
1906 higestRange = (hiddenCol[1] >= endPos) ? hiddenCol : higestRange;
1909 if (lowestRange[0] == -1 && lowestRange[1] == -1)
1911 startPos = alignmentStartEnd[0];
1915 startPos = lowestRange[1] + 1;
1918 if (higestRange[0] == -1 && higestRange[1] == -1)
1920 endPos = alignmentStartEnd[1];
1924 endPos = higestRange[0] - 1;
1926 return new int[] { startPos, endPos };