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() throws Throwable
295 if (getDataset() != null)
297 getDataset().removeAlignmentRef();
305 * Defensively nulls out references in case this object is not garbage
308 void nullReferences()
314 hiddenSequences = null;
318 * decrement the alignmentRefs counter by one and null references if it goes
323 private void removeAlignmentRef() throws Throwable
325 if (--alignmentRefs == 0)
338 public void deleteSequence(SequenceI s)
340 deleteSequence(findIndex(s));
350 public void deleteSequence(int i)
352 if (i > -1 && i < getHeight())
354 synchronized (sequences)
357 hiddenSequences.adjustHeightSequenceDeleted(i);
365 * @see jalview.datamodel.AlignmentI#findGroup(jalview.datamodel.SequenceI)
368 public SequenceGroup findGroup(SequenceI s)
370 synchronized (groups)
372 for (int i = 0; i < this.groups.size(); i++)
374 SequenceGroup sg = groups.get(i);
376 if (sg.getSequences(null).contains(s))
389 * jalview.datamodel.AlignmentI#findAllGroups(jalview.datamodel.SequenceI)
392 public SequenceGroup[] findAllGroups(SequenceI s)
394 ArrayList<SequenceGroup> temp = new ArrayList<SequenceGroup>();
396 synchronized (groups)
398 int gSize = groups.size();
399 for (int i = 0; i < gSize; i++)
401 SequenceGroup sg = groups.get(i);
402 if (sg == null || sg.getSequences() == null)
404 this.deleteGroup(sg);
409 if (sg.getSequences().contains(s))
415 SequenceGroup[] ret = new SequenceGroup[temp.size()];
416 return temp.toArray(ret);
421 public void addGroup(SequenceGroup sg)
423 synchronized (groups)
425 if (!groups.contains(sg))
427 if (hiddenSequences.getSize() > 0)
429 int i, iSize = sg.getSize();
430 for (i = 0; i < iSize; i++)
432 if (!sequences.contains(sg.getSequenceAt(i)))
434 sg.deleteSequence(sg.getSequenceAt(i), false);
440 if (sg.getSize() < 1)
452 * remove any annotation that references gp
455 * (if null, removes all group associated annotation)
457 private void removeAnnotationForGroup(SequenceGroup gp)
459 if (annotations == null || annotations.length == 0)
463 // remove annotation very quickly
464 AlignmentAnnotation[] t, todelete = new AlignmentAnnotation[annotations.length], tokeep = new AlignmentAnnotation[annotations.length];
468 for (i = 0, p = 0, k = 0; i < annotations.length; i++)
470 if (annotations[i].groupRef != null)
472 todelete[p++] = annotations[i];
476 tokeep[k++] = annotations[i];
482 for (i = 0, p = 0, k = 0; i < annotations.length; i++)
484 if (annotations[i].groupRef == gp)
486 todelete[p++] = annotations[i];
490 tokeep[k++] = annotations[i];
496 // clear out the group associated annotation.
497 for (i = 0; i < p; i++)
499 unhookAnnotation(todelete[i]);
502 t = new AlignmentAnnotation[k];
503 for (i = 0; i < k; i++)
512 public void deleteAllGroups()
514 synchronized (groups)
516 if (annotations != null)
518 removeAnnotationForGroup(null);
520 for (SequenceGroup sg : groups)
530 public void deleteGroup(SequenceGroup g)
532 synchronized (groups)
534 if (groups.contains(g))
536 removeAnnotationForGroup(g);
545 public SequenceI findName(String name)
547 return findName(name, false);
553 * @see jalview.datamodel.AlignmentI#findName(java.lang.String, boolean)
556 public SequenceI findName(String token, boolean b)
558 return findName(null, token, b);
564 * @see jalview.datamodel.AlignmentI#findName(SequenceI, java.lang.String,
568 public SequenceI findName(SequenceI startAfter, String token, boolean b)
573 String sqname = null;
574 if (startAfter != null)
576 // try to find the sequence in the alignment
577 boolean matched = false;
578 while (i < sequences.size())
580 if (getSequenceAt(i++) == startAfter)
591 while (i < sequences.size())
593 sq = getSequenceAt(i);
594 sqname = sq.getName();
595 if (sqname.equals(token) // exact match
596 || (b && // allow imperfect matches - case varies
597 (sqname.equalsIgnoreCase(token))))
599 return getSequenceAt(i);
609 public SequenceI[] findSequenceMatch(String name)
611 Vector matches = new Vector();
614 while (i < sequences.size())
616 if (getSequenceAt(i).getName().equals(name))
618 matches.addElement(getSequenceAt(i));
623 SequenceI[] result = new SequenceI[matches.size()];
624 for (i = 0; i < result.length; i++)
626 result[i] = (SequenceI) matches.elementAt(i);
636 * @see jalview.datamodel.AlignmentI#findIndex(jalview.datamodel.SequenceI)
639 public int findIndex(SequenceI s)
643 while (i < sequences.size())
645 if (s == getSequenceAt(i))
660 * jalview.datamodel.AlignmentI#findIndex(jalview.datamodel.SearchResults)
663 public int findIndex(SearchResults results)
667 while (i < sequences.size())
669 if (results.involvesSequence(getSequenceAt(i)))
681 * @return DOCUMENT ME!
684 public int getHeight()
686 return sequences.size();
692 * @return DOCUMENT ME!
695 public int getWidth()
699 for (int i = 0; i < sequences.size(); i++)
701 if (getSequenceAt(i).getLength() > maxLength)
703 maxLength = getSequenceAt(i).getLength();
717 public void setGapCharacter(char gc)
720 synchronized (sequences)
722 for (SequenceI seq : sequences)
724 seq.setSequence(seq.getSequenceAsString().replace('.', gc)
725 .replace('-', gc).replace(' ', gc));
733 * @return DOCUMENT ME!
736 public char getGapCharacter()
744 * @see jalview.datamodel.AlignmentI#isAligned()
747 public boolean isAligned()
749 return isAligned(false);
755 * @see jalview.datamodel.AlignmentI#isAligned(boolean)
758 public boolean isAligned(boolean includeHidden)
760 int width = getWidth();
761 if (hiddenSequences == null || hiddenSequences.getSize() == 0)
763 includeHidden = true; // no hidden sequences to check against.
765 for (int i = 0; i < sequences.size(); i++)
767 if (includeHidden || !hiddenSequences.isHidden(getSequenceAt(i)))
769 if (getSequenceAt(i).getLength() != width)
780 * Delete all annotations, including auto-calculated if the flag is set true.
781 * Returns true if at least one annotation was deleted, else false.
783 * @param includingAutoCalculated
787 public boolean deleteAllAnnotations(boolean includingAutoCalculated)
789 boolean result = false;
790 for (AlignmentAnnotation alan : getAlignmentAnnotation())
792 if (!alan.autoCalculated || includingAutoCalculated)
794 deleteAnnotation(alan);
804 * @seejalview.datamodel.AlignmentI#deleteAnnotation(jalview.datamodel.
805 * AlignmentAnnotation)
808 public boolean deleteAnnotation(AlignmentAnnotation aa)
810 return deleteAnnotation(aa, true);
814 public boolean deleteAnnotation(AlignmentAnnotation aa, boolean unhook)
818 if (annotations != null)
820 aSize = annotations.length;
828 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize - 1];
830 boolean swap = false;
833 for (int i = 0; i < aSize; i++)
835 if (annotations[i] == aa)
840 if (tIndex < temp.length)
842 temp[tIndex++] = annotations[i];
851 unhookAnnotation(aa);
858 * remove any object references associated with this annotation
862 private void unhookAnnotation(AlignmentAnnotation aa)
864 if (aa.sequenceRef != null)
866 aa.sequenceRef.removeAlignmentAnnotation(aa);
868 if (aa.groupRef != null)
870 // probably need to do more here in the future (post 2.5.0)
878 * @seejalview.datamodel.AlignmentI#addAnnotation(jalview.datamodel.
879 * AlignmentAnnotation)
882 public void addAnnotation(AlignmentAnnotation aa)
884 addAnnotation(aa, -1);
890 * @seejalview.datamodel.AlignmentI#addAnnotation(jalview.datamodel.
891 * AlignmentAnnotation, int)
894 public void addAnnotation(AlignmentAnnotation aa, int pos)
896 if (aa.getRNAStruc() != null)
898 hasRNAStructure = true;
902 if (annotations != null)
904 aSize = annotations.length + 1;
907 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
909 if (pos == -1 || pos >= aSize)
911 temp[aSize - 1] = aa;
920 for (i = 0; i < (aSize - 1); i++, p++)
928 temp[p] = annotations[i];
937 public void setAnnotationIndex(AlignmentAnnotation aa, int index)
939 if (aa == null || annotations == null || annotations.length - 1 < index)
944 int aSize = annotations.length;
945 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
949 for (int i = 0; i < aSize; i++)
958 temp[i] = annotations[i];
962 temp[i] = annotations[i - 1];
971 * returns all annotation on the alignment
973 public AlignmentAnnotation[] getAlignmentAnnotation()
979 public void setNucleotide(boolean b)
992 public boolean isNucleotide()
994 if (type == NUCLEOTIDE)
1005 public boolean hasRNAStructure()
1007 // TODO can it happen that structure is removed from alignment?
1008 return hasRNAStructure;
1012 public void setDataset(AlignmentI data)
1014 if (dataset == null && data == null)
1016 createDatasetAlignment();
1018 else if (dataset == null && data != null)
1020 if (!(data instanceof Alignment))
1023 "Implementation Error: jalview.datamodel.Alignment does not yet support other implementations of AlignmentI as its dataset reference");
1025 dataset = (Alignment) data;
1026 for (int i = 0; i < getHeight(); i++)
1028 SequenceI currentSeq = getSequenceAt(i);
1029 SequenceI dsq = currentSeq.getDatasetSequence();
1032 dsq = currentSeq.createDatasetSequence();
1033 dataset.addSequence(dsq);
1037 while (dsq.getDatasetSequence() != null)
1039 dsq = dsq.getDatasetSequence();
1041 if (dataset.findIndex(dsq) == -1)
1043 dataset.addSequence(dsq);
1048 dataset.addAlignmentRef();
1052 * add dataset sequences to seq for currentSeq and any sequences it references
1054 private void resolveAndAddDatasetSeq(SequenceI currentSeq,
1055 Set<SequenceI> seqs, boolean createDatasetSequence)
1057 if (currentSeq.getDatasetSequence() != null)
1059 currentSeq = currentSeq.getDatasetSequence();
1063 if (createDatasetSequence)
1065 currentSeq = currentSeq.createDatasetSequence();
1068 if (seqs.contains(currentSeq))
1072 List<SequenceI> toProcess = new ArrayList<SequenceI>();
1073 toProcess.add(currentSeq);
1074 while (toProcess.size() > 0)
1077 SequenceI curDs = toProcess.remove(0);
1078 if (seqs.contains(curDs))
1083 // iterate over database references, making sure we add forward referenced
1085 if (curDs.getDBRefs() != null)
1087 for (DBRefEntry dbr : curDs.getDBRefs())
1089 if (dbr.getMap() != null && dbr.getMap().getTo() != null)
1091 if (dbr.getMap().getTo().getDatasetSequence() != null)
1093 throw new Error("Implementation error: Map.getTo() for dbref"
1094 + dbr + " is not a dataset sequence.");
1095 // TODO: if this happens, could also rewrite the reference to
1096 // point to new dataset sequence
1098 // we recurse to add all forward references to dataset sequences via
1100 toProcess.add(dbr.getMap().getTo());
1108 * Creates a new dataset for this alignment. Can only be done once - if
1109 * dataset is not null this will not be performed.
1111 public void createDatasetAlignment()
1113 if (dataset != null)
1117 // try to avoid using SequenceI.equals at this stage, it will be expensive
1118 Set<SequenceI> seqs = new jalview.util.LinkedIdentityHashSet<SequenceI>();
1120 for (int i = 0; i < getHeight(); i++)
1122 SequenceI currentSeq = getSequenceAt(i);
1123 resolveAndAddDatasetSeq(currentSeq, seqs, true);
1126 // verify all mappings are in dataset
1127 for (AlignedCodonFrame cf : codonFrameList)
1129 for (SequenceToSequenceMapping ssm : cf.getMappings())
1131 if (!seqs.contains(ssm.getFromSeq()))
1133 resolveAndAddDatasetSeq(ssm.getFromSeq(), seqs, false);
1135 if (!seqs.contains(ssm.getMapping().getTo()))
1137 resolveAndAddDatasetSeq(ssm.getMapping().getTo(), seqs, false);
1141 // finally construct dataset
1142 dataset = new Alignment(seqs.toArray(new SequenceI[seqs.size()]));
1143 // move mappings to the dataset alignment
1144 dataset.codonFrameList = this.codonFrameList;
1145 this.codonFrameList = null;
1149 * reference count for number of alignments referencing this one.
1151 int alignmentRefs = 0;
1154 * increase reference count to this alignment.
1156 private void addAlignmentRef()
1162 public Alignment getDataset()
1168 public boolean padGaps()
1170 boolean modified = false;
1172 // Remove excess gaps from the end of alignment
1176 for (int i = 0; i < sequences.size(); i++)
1178 current = getSequenceAt(i);
1179 for (int j = current.getLength(); j > maxLength; j--)
1182 && !jalview.util.Comparison.isGap(current.getCharAt(j)))
1193 for (int i = 0; i < sequences.size(); i++)
1195 current = getSequenceAt(i);
1196 cLength = current.getLength();
1198 if (cLength < maxLength)
1200 current.insertCharAt(cLength, maxLength - cLength, gapCharacter);
1203 else if (current.getLength() > maxLength)
1205 current.deleteChars(maxLength, current.getLength());
1212 * Justify the sequences to the left or right by deleting and inserting gaps
1213 * before the initial residue or after the terminal residue
1216 * true if alignment padded to right, false to justify to left
1217 * @return true if alignment was changed
1220 public boolean justify(boolean right)
1222 boolean modified = false;
1224 // Remove excess gaps from the end of alignment
1226 int ends[] = new int[sequences.size() * 2];
1228 for (int i = 0; i < sequences.size(); i++)
1230 current = getSequenceAt(i);
1231 // This should really be a sequence method
1232 ends[i * 2] = current.findIndex(current.getStart());
1233 ends[i * 2 + 1] = current.findIndex(current.getStart()
1234 + current.getLength());
1235 boolean hitres = false;
1236 for (int j = 0, rs = 0, ssiz = current.getLength(); j < ssiz; j++)
1238 if (!jalview.util.Comparison.isGap(current.getCharAt(j)))
1247 ends[i * 2 + 1] = j;
1248 if (j - ends[i * 2] > maxLength)
1250 maxLength = j - ends[i * 2];
1258 // now edit the flanking gaps to justify to either left or right
1259 int cLength, extent, diff;
1260 for (int i = 0; i < sequences.size(); i++)
1262 current = getSequenceAt(i);
1264 cLength = 1 + ends[i * 2 + 1] - ends[i * 2];
1265 diff = maxLength - cLength; // number of gaps to indent
1266 extent = current.getLength();
1270 if (extent > ends[i * 2 + 1])
1272 current.deleteChars(ends[i * 2 + 1] + 1, extent);
1275 if (ends[i * 2] > diff)
1277 current.deleteChars(0, ends[i * 2] - diff);
1282 if (ends[i * 2] < diff)
1284 current.insertCharAt(0, diff - ends[i * 2], gapCharacter);
1292 if (ends[i * 2] > 0)
1294 current.deleteChars(0, ends[i * 2]);
1296 ends[i * 2 + 1] -= ends[i * 2];
1297 extent -= ends[i * 2];
1299 if (extent > maxLength)
1301 current.deleteChars(maxLength + 1, extent);
1306 if (extent < maxLength)
1308 current.insertCharAt(extent, maxLength - extent, gapCharacter);
1318 public HiddenSequences getHiddenSequences()
1320 return hiddenSequences;
1324 public CigarArray getCompactAlignment()
1326 synchronized (sequences)
1328 SeqCigar alseqs[] = new SeqCigar[sequences.size()];
1330 for (SequenceI seq : sequences)
1332 alseqs[i++] = new SeqCigar(seq);
1334 CigarArray cal = new CigarArray(alseqs);
1335 cal.addOperation(CigarArray.M, getWidth());
1341 public void setProperty(Object key, Object value)
1343 if (alignmentProperties == null)
1345 alignmentProperties = new Hashtable();
1348 alignmentProperties.put(key, value);
1352 public Object getProperty(Object key)
1354 if (alignmentProperties != null)
1356 return alignmentProperties.get(key);
1365 public Hashtable getProperties()
1367 return alignmentProperties;
1371 * Adds the given mapping to the stored set. Note this may be held on the
1372 * dataset alignment.
1375 public void addCodonFrame(AlignedCodonFrame codons)
1377 List<AlignedCodonFrame> acfs = getCodonFrames();
1378 if (codons != null && acfs != null && !acfs.contains(codons))
1388 * jalview.datamodel.AlignmentI#getCodonFrame(jalview.datamodel.SequenceI)
1391 public List<AlignedCodonFrame> getCodonFrame(SequenceI seq)
1397 List<AlignedCodonFrame> cframes = new ArrayList<AlignedCodonFrame>();
1398 for (AlignedCodonFrame acf : getCodonFrames())
1400 if (acf.involvesSequence(seq))
1409 * Sets the codon frame mappings (replacing any existing mappings). Note the
1410 * mappings are set on the dataset alignment instead if there is one.
1412 * @see jalview.datamodel.AlignmentI#setCodonFrames()
1415 public void setCodonFrames(List<AlignedCodonFrame> acfs)
1417 if (dataset != null)
1419 dataset.setCodonFrames(acfs);
1423 this.codonFrameList = acfs;
1428 * Returns the set of codon frame mappings. Any changes to the returned set
1429 * will affect the alignment. The mappings are held on (and read from) the
1430 * dataset alignment if there is one.
1432 * @see jalview.datamodel.AlignmentI#getCodonFrames()
1435 public List<AlignedCodonFrame> getCodonFrames()
1437 // TODO: Fix this method to fix failing AlignedCodonFrame tests
1438 // this behaviour is currently incorrect. method should return codon frames
1439 // for just the alignment,
1440 // selected from dataset
1441 return dataset != null ? dataset.getCodonFrames() : codonFrameList;
1445 * Removes the given mapping from the stored set. Note that the mappings are
1446 * held on the dataset alignment if there is one.
1449 public boolean removeCodonFrame(AlignedCodonFrame codons)
1451 List<AlignedCodonFrame> acfs = getCodonFrames();
1452 if (codons == null || acfs == null)
1456 return acfs.remove(codons);
1460 public void append(AlignmentI toappend)
1462 // TODO JAL-1270 needs test coverage
1463 // currently tested for use in jalview.gui.SequenceFetcher
1464 boolean samegap = toappend.getGapCharacter() == getGapCharacter();
1465 char oldc = toappend.getGapCharacter();
1466 boolean hashidden = toappend.getHiddenSequences() != null
1467 && toappend.getHiddenSequences().hiddenSequences != null;
1468 // get all sequences including any hidden ones
1469 List<SequenceI> sqs = (hashidden) ? toappend.getHiddenSequences()
1470 .getFullAlignment().getSequences() : toappend.getSequences();
1473 // avoid self append deadlock by
1474 List<SequenceI> toappendsq = new ArrayList<SequenceI>();
1477 for (SequenceI addedsq : sqs)
1481 char[] oldseq = addedsq.getSequence();
1482 for (int c = 0; c < oldseq.length; c++)
1484 if (oldseq[c] == oldc)
1486 oldseq[c] = gapCharacter;
1490 toappendsq.add(addedsq);
1493 for (SequenceI addedsq : toappendsq)
1495 addSequence(addedsq);
1498 AlignmentAnnotation[] alan = toappend.getAlignmentAnnotation();
1499 for (int a = 0; alan != null && a < alan.length; a++)
1501 addAnnotation(alan[a]);
1505 getCodonFrames().addAll(toappend.getCodonFrames());
1507 List<SequenceGroup> sg = toappend.getGroups();
1510 for (SequenceGroup _sg : sg)
1515 if (toappend.getHiddenSequences() != null)
1517 HiddenSequences hs = toappend.getHiddenSequences();
1518 if (hiddenSequences == null)
1520 hiddenSequences = new HiddenSequences(this);
1522 if (hs.hiddenSequences != null)
1524 for (int s = 0; s < hs.hiddenSequences.length; s++)
1526 // hide the newly appended sequence in the alignment
1527 if (hs.hiddenSequences[s] != null)
1529 hiddenSequences.hideSequence(hs.hiddenSequences[s]);
1534 if (toappend.getProperties() != null)
1536 // we really can't do very much here - just try to concatenate strings
1537 // where property collisions occur.
1538 Enumeration key = toappend.getProperties().keys();
1539 while (key.hasMoreElements())
1541 Object k = key.nextElement();
1542 Object ourval = this.getProperty(k);
1543 Object toapprop = toappend.getProperty(k);
1546 if (ourval.getClass().equals(toapprop.getClass())
1547 && !ourval.equals(toapprop))
1549 if (ourval instanceof String)
1552 this.setProperty(k, ((String) ourval) + "; "
1553 + ((String) toapprop));
1557 if (ourval instanceof Vector)
1560 Enumeration theirv = ((Vector) toapprop).elements();
1561 while (theirv.hasMoreElements())
1563 ((Vector) ourval).addElement(theirv);
1571 // just add new property directly
1572 setProperty(k, toapprop);
1580 public AlignmentAnnotation findOrCreateAnnotation(String name,
1581 String calcId, boolean autoCalc, SequenceI seqRef,
1582 SequenceGroup groupRef)
1584 assert (name != null);
1585 if (annotations != null)
1587 for (AlignmentAnnotation annot : getAlignmentAnnotation())
1589 if (annot.autoCalculated == autoCalc && (name.equals(annot.label))
1590 && (calcId == null || annot.getCalcId().equals(calcId))
1591 && annot.sequenceRef == seqRef
1592 && annot.groupRef == groupRef)
1598 AlignmentAnnotation annot = new AlignmentAnnotation(name, name,
1599 new Annotation[1], 0f, 0f, AlignmentAnnotation.BAR_GRAPH);
1600 annot.hasText = false;
1601 annot.setCalcId(new String(calcId));
1602 annot.autoCalculated = autoCalc;
1605 annot.setSequenceRef(seqRef);
1607 annot.groupRef = groupRef;
1608 addAnnotation(annot);
1614 public Iterable<AlignmentAnnotation> findAnnotation(String calcId)
1616 ArrayList<AlignmentAnnotation> aa = new ArrayList<AlignmentAnnotation>();
1617 for (AlignmentAnnotation a : getAlignmentAnnotation())
1619 if (a.getCalcId() == calcId
1620 || (a.getCalcId() != null && calcId != null && a.getCalcId()
1630 * Returns an iterable collection of any annotations that match on given
1631 * sequence ref, calcId and label (ignoring null values).
1634 public Iterable<AlignmentAnnotation> findAnnotations(SequenceI seq,
1635 String calcId, String label)
1637 ArrayList<AlignmentAnnotation> aa = new ArrayList<AlignmentAnnotation>();
1638 for (AlignmentAnnotation ann : getAlignmentAnnotation())
1640 if (ann.getCalcId() != null && ann.getCalcId().equals(calcId)
1641 && ann.sequenceRef != null && ann.sequenceRef == seq
1642 && ann.label != null && ann.label.equals(label))
1651 public void moveSelectedSequencesByOne(SequenceGroup sg,
1652 Map<SequenceI, SequenceCollectionI> map, boolean up)
1654 synchronized (sequences)
1659 for (int i = 1, iSize = sequences.size(); i < iSize; i++)
1661 SequenceI seq = sequences.get(i);
1662 if (!sg.getSequences(map).contains(seq))
1667 SequenceI temp = sequences.get(i - 1);
1668 if (sg.getSequences(null).contains(temp))
1673 sequences.set(i, temp);
1674 sequences.set(i - 1, seq);
1679 for (int i = sequences.size() - 2; i > -1; i--)
1681 SequenceI seq = sequences.get(i);
1682 if (!sg.getSequences(map).contains(seq))
1687 SequenceI temp = sequences.get(i + 1);
1688 if (sg.getSequences(map).contains(temp))
1693 sequences.set(i, temp);
1694 sequences.set(i + 1, seq);
1702 public void validateAnnotation(AlignmentAnnotation alignmentAnnotation)
1704 alignmentAnnotation.validateRangeAndDisplay();
1705 if (isNucleotide() && alignmentAnnotation.isValidStruc())
1707 hasRNAStructure = true;
1711 private SequenceI seqrep = null;
1715 * @return the representative sequence for this group
1718 public SequenceI getSeqrep()
1724 * set the representative sequence for this group. Note - this affects the
1725 * interpretation of the Hidereps attribute.
1728 * the seqrep to set (null means no sequence representative)
1731 public void setSeqrep(SequenceI seqrep)
1733 this.seqrep = seqrep;
1738 * @return true if group has a sequence representative
1741 public boolean hasSeqrep()
1743 return seqrep != null;
1747 public int getEndRes()
1749 return getWidth() - 1;
1753 public int getStartRes()
1759 * In the case of AlignmentI - returns the dataset for the alignment, if set
1762 * @see jalview.datamodel.AnnotatedCollectionI#getContext()
1765 public AnnotatedCollectionI getContext()
1771 * Align this alignment like the given (mapped) one.
1774 public int alignAs(AlignmentI al)
1777 * Currently retains unmapped gaps (in introns), regaps mapped regions
1780 return alignAs(al, false, true);
1784 * Align this alignment 'the same as' the given one. Mapped sequences only are
1785 * realigned. If both of the same type (nucleotide/protein) then align both
1786 * identically. If this is nucleotide and the other is protein, make 3 gaps
1787 * for each gap in the protein sequences. If this is protein and the other is
1788 * nucleotide, insert a gap for each 3 gaps (or part thereof) between
1789 * nucleotide bases. If this is protein and the other is nucleotide, gaps
1790 * protein to match the relative ordering of codons in the nucleotide.
1792 * Parameters control whether gaps in exon (mapped) and intron (unmapped)
1793 * regions are preserved. Gaps that connect introns to exons are treated
1794 * conservatively, i.e. only preserved if both intron and exon gaps are
1795 * preserved. TODO: check caveats below where the implementation fails
1798 * - must have same dataset, and sequences in al must have equivalent
1799 * dataset sequence and start/end bounds under given mapping
1800 * @param preserveMappedGaps
1801 * if true, gaps within and between mapped codons are preserved
1802 * @param preserveUnmappedGaps
1803 * if true, gaps within and between unmapped codons are preserved
1806 public int alignAs(AlignmentI al, boolean preserveMappedGaps,
1807 boolean preserveUnmappedGaps)
1809 // TODO should this method signature be the one in the interface?
1810 // JBPComment - yes - neither flag is used, so should be deleted.
1811 boolean thisIsNucleotide = this.isNucleotide();
1812 boolean thatIsProtein = !al.isNucleotide();
1813 if (!thatIsProtein && !thisIsNucleotide)
1815 return AlignmentUtils.alignProteinAsDna(this, al);
1817 else if (thatIsProtein && thisIsNucleotide)
1819 return AlignmentUtils.alignCdsAsProtein(this, al);
1821 return AlignmentUtils.alignAs(this, al);
1825 * Returns the alignment in Fasta format. Behaviour of this method is not
1826 * guaranteed between versions.
1829 public String toString()
1831 return new FastaFile().print(getSequencesArray());
1835 * Returns the set of distinct sequence names. No ordering is guaranteed.
1838 public Set<String> getSequenceNames()
1840 Set<String> names = new HashSet<String>();
1841 for (SequenceI seq : getSequences())
1843 names.add(seq.getName());
1849 public boolean hasValidSequence()
1851 boolean hasValidSeq = false;
1852 for (SequenceI seq : getSequences())
1854 if ((seq.getEnd() - seq.getStart()) > 0)
1864 * Update any mappings to 'virtual' sequences to compatible real ones, if
1865 * present in the added sequences. Returns a count of mappings updated.
1871 public int realiseMappings(List<SequenceI> seqs)
1874 for (SequenceI seq : seqs)
1876 for (AlignedCodonFrame mapping : getCodonFrames())
1878 count += mapping.realiseWith(seq);
1885 * Returns the first AlignedCodonFrame that has a mapping between the given
1893 public AlignedCodonFrame getMapping(SequenceI mapFrom, SequenceI mapTo)
1895 for (AlignedCodonFrame acf : getCodonFrames())
1897 if (acf.getAaForDnaSeq(mapFrom) == mapTo)
1906 public int[] getVisibleStartAndEndIndex(List<int[]> hiddenCols)
1908 int[] alignmentStartEnd = new int[] { 0, getWidth() - 1 };
1909 int startPos = alignmentStartEnd[0];
1910 int endPos = alignmentStartEnd[1];
1912 int[] lowestRange = new int[] { -1, -1 };
1913 int[] higestRange = new int[] { -1, -1 };
1915 for (int[] hiddenCol : hiddenCols)
1917 lowestRange = (hiddenCol[0] <= startPos) ? hiddenCol : lowestRange;
1918 higestRange = (hiddenCol[1] >= endPos) ? hiddenCol : higestRange;
1921 if (lowestRange[0] == -1 && lowestRange[1] == -1)
1923 startPos = alignmentStartEnd[0];
1927 startPos = lowestRange[1] + 1;
1930 if (higestRange[0] == -1 && higestRange[1] == -1)
1932 endPos = alignmentStartEnd[1];
1936 endPos = higestRange[0] - 1;
1938 return new int[] { startPos, endPos };