2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.datamodel;
23 import jalview.analysis.AlignmentUtils;
24 import jalview.io.FastaFile;
25 import jalview.util.Comparison;
26 import jalview.util.MessageManager;
28 import java.util.ArrayList;
29 import java.util.Collections;
30 import java.util.Enumeration;
31 import java.util.HashSet;
32 import java.util.Hashtable;
33 import java.util.List;
36 import java.util.Vector;
39 * Data structure to hold and manipulate a multiple sequence alignment
45 public class Alignment implements AlignmentI
47 private Alignment dataset;
49 protected List<SequenceI> sequences;
51 protected List<SequenceGroup> groups;
53 protected char gapCharacter = '-';
55 protected int type = NUCLEOTIDE;
57 public static final int PROTEIN = 0;
59 public static final int NUCLEOTIDE = 1;
61 public boolean hasRNAStructure = false;
63 public AlignmentAnnotation[] annotations;
65 HiddenSequences hiddenSequences;
67 public Hashtable alignmentProperties;
69 private List<AlignedCodonFrame> codonFrameList;
71 private void initAlignment(SequenceI[] seqs)
73 groups = Collections.synchronizedList(new ArrayList<SequenceGroup>());
74 hiddenSequences = new HiddenSequences(this);
75 codonFrameList = new ArrayList<AlignedCodonFrame>();
77 if (Comparison.isNucleotide(seqs))
86 sequences = Collections.synchronizedList(new ArrayList<SequenceI>());
88 for (int i = 0; i < seqs.length; i++)
90 sequences.add(seqs[i]);
96 * Make a 'copy' alignment - sequences have new copies of features and
97 * annotations, but share the original dataset sequences.
99 public Alignment(AlignmentI al)
101 SequenceI[] seqs = al.getSequencesArray();
102 for (int i = 0; i < seqs.length; i++)
104 seqs[i] = new Sequence(seqs[i]);
110 * Share the same dataset sequence mappings (if any).
112 if (dataset == null && al.getDataset() == null)
114 this.setCodonFrames(al.getCodonFrames());
119 * Make an alignment from an array of Sequences.
123 public Alignment(SequenceI[] seqs)
129 * Make a new alignment from an array of SeqCigars
134 public Alignment(SeqCigar[] alseqs)
136 SequenceI[] seqs = SeqCigar.createAlignmentSequences(alseqs,
137 gapCharacter, new ColumnSelection(), null);
142 * Make a new alignment from an CigarArray JBPNote - can only do this when
143 * compactAlignment does not contain hidden regions. JBPNote - must also check
144 * that compactAlignment resolves to a set of SeqCigars - or construct them
147 * @param compactAlignment
150 public static AlignmentI createAlignment(CigarArray compactAlignment)
154 .getString("error.alignment_cigararray_not_implemented"));
155 // this(compactAlignment.refCigars);
159 public List<SequenceI> getSequences()
165 public List<SequenceI> getSequences(
166 Map<SequenceI, SequenceCollectionI> hiddenReps)
168 // TODO: in jalview 2.8 we don't do anything with hiddenreps - fix design to
174 public SequenceI[] getSequencesArray()
176 if (sequences == null)
180 synchronized (sequences)
182 return sequences.toArray(new SequenceI[sequences.size()]);
187 * Returns a map of lists of sequences keyed by sequence name.
192 public Map<String, List<SequenceI>> getSequencesByName()
194 return AlignmentUtils.getSequencesByName(this);
203 * @return DOCUMENT ME!
206 public SequenceI getSequenceAt(int i)
208 synchronized (sequences)
210 if (i > -1 && i < sequences.size())
212 return sequences.get(i);
219 * Adds a sequence to the alignment. Recalculates maxLength and size.
224 public void addSequence(SequenceI snew)
228 // maintain dataset integrity
229 if (snew.getDatasetSequence() != null)
231 getDataset().addSequence(snew.getDatasetSequence());
235 // derive new sequence
236 SequenceI adding = snew.deriveSequence();
237 getDataset().addSequence(adding.getDatasetSequence());
241 if (sequences == null)
243 initAlignment(new SequenceI[] { snew });
247 synchronized (sequences)
252 if (hiddenSequences != null)
254 hiddenSequences.adjustHeightSequenceAdded();
259 * Adds a sequence to the alignment. Recalculates maxLength and size.
264 public void setSequenceAt(int i, SequenceI snew)
266 synchronized (sequences)
269 sequences.set(i, snew);
276 * @return DOCUMENT ME!
279 public List<SequenceGroup> getGroups()
285 public void finalize()
287 if (getDataset() != null)
289 getDataset().removeAlignmentRef();
296 hiddenSequences = null;
300 * decrement the alignmentRefs counter by one and call finalize if it goes to
303 private void removeAlignmentRef()
305 if (--alignmentRefs == 0)
318 public void deleteSequence(SequenceI s)
320 deleteSequence(findIndex(s));
330 public void deleteSequence(int i)
332 if (i > -1 && i < getHeight())
334 synchronized (sequences)
337 hiddenSequences.adjustHeightSequenceDeleted(i);
345 * @see jalview.datamodel.AlignmentI#findGroup(jalview.datamodel.SequenceI)
348 public SequenceGroup findGroup(SequenceI s)
350 synchronized (groups)
352 for (int i = 0; i < this.groups.size(); i++)
354 SequenceGroup sg = groups.get(i);
356 if (sg.getSequences(null).contains(s))
369 * jalview.datamodel.AlignmentI#findAllGroups(jalview.datamodel.SequenceI)
372 public SequenceGroup[] findAllGroups(SequenceI s)
374 ArrayList<SequenceGroup> temp = new ArrayList<SequenceGroup>();
376 synchronized (groups)
378 int gSize = groups.size();
379 for (int i = 0; i < gSize; i++)
381 SequenceGroup sg = groups.get(i);
382 if (sg == null || sg.getSequences() == null)
384 this.deleteGroup(sg);
389 if (sg.getSequences().contains(s))
395 SequenceGroup[] ret = new SequenceGroup[temp.size()];
396 return temp.toArray(ret);
401 public void addGroup(SequenceGroup sg)
403 synchronized (groups)
405 if (!groups.contains(sg))
407 if (hiddenSequences.getSize() > 0)
409 int i, iSize = sg.getSize();
410 for (i = 0; i < iSize; i++)
412 if (!sequences.contains(sg.getSequenceAt(i)))
414 sg.deleteSequence(sg.getSequenceAt(i), false);
420 if (sg.getSize() < 1)
432 * remove any annotation that references gp
435 * (if null, removes all group associated annotation)
437 private void removeAnnotationForGroup(SequenceGroup gp)
439 if (annotations == null || annotations.length == 0)
443 // remove annotation very quickly
444 AlignmentAnnotation[] t, todelete = new AlignmentAnnotation[annotations.length], tokeep = new AlignmentAnnotation[annotations.length];
448 for (i = 0, p = 0, k = 0; i < annotations.length; i++)
450 if (annotations[i].groupRef != null)
452 todelete[p++] = annotations[i];
456 tokeep[k++] = annotations[i];
462 for (i = 0, p = 0, k = 0; i < annotations.length; i++)
464 if (annotations[i].groupRef == gp)
466 todelete[p++] = annotations[i];
470 tokeep[k++] = annotations[i];
476 // clear out the group associated annotation.
477 for (i = 0; i < p; i++)
479 unhookAnnotation(todelete[i]);
482 t = new AlignmentAnnotation[k];
483 for (i = 0; i < k; i++)
492 public void deleteAllGroups()
494 synchronized (groups)
496 if (annotations != null)
498 removeAnnotationForGroup(null);
500 for (SequenceGroup sg : groups)
510 public void deleteGroup(SequenceGroup g)
512 synchronized (groups)
514 if (groups.contains(g))
516 removeAnnotationForGroup(g);
525 public SequenceI findName(String name)
527 return findName(name, false);
533 * @see jalview.datamodel.AlignmentI#findName(java.lang.String, boolean)
536 public SequenceI findName(String token, boolean b)
538 return findName(null, token, b);
544 * @see jalview.datamodel.AlignmentI#findName(SequenceI, java.lang.String,
548 public SequenceI findName(SequenceI startAfter, String token, boolean b)
553 String sqname = null;
554 if (startAfter != null)
556 // try to find the sequence in the alignment
557 boolean matched = false;
558 while (i < sequences.size())
560 if (getSequenceAt(i++) == startAfter)
571 while (i < sequences.size())
573 sq = getSequenceAt(i);
574 sqname = sq.getName();
575 if (sqname.equals(token) // exact match
576 || (b && // allow imperfect matches - case varies
577 (sqname.equalsIgnoreCase(token))))
579 return getSequenceAt(i);
589 public SequenceI[] findSequenceMatch(String name)
591 Vector matches = new Vector();
594 while (i < sequences.size())
596 if (getSequenceAt(i).getName().equals(name))
598 matches.addElement(getSequenceAt(i));
603 SequenceI[] result = new SequenceI[matches.size()];
604 for (i = 0; i < result.length; i++)
606 result[i] = (SequenceI) matches.elementAt(i);
616 * @see jalview.datamodel.AlignmentI#findIndex(jalview.datamodel.SequenceI)
619 public int findIndex(SequenceI s)
623 while (i < sequences.size())
625 if (s == getSequenceAt(i))
640 * jalview.datamodel.AlignmentI#findIndex(jalview.datamodel.SearchResults)
643 public int findIndex(SearchResults results)
647 while (i < sequences.size())
649 if (results.involvesSequence(getSequenceAt(i)))
661 * @return DOCUMENT ME!
664 public int getHeight()
666 return sequences.size();
672 * @return DOCUMENT ME!
675 public int getWidth()
679 for (int i = 0; i < sequences.size(); i++)
681 if (getSequenceAt(i).getLength() > maxLength)
683 maxLength = getSequenceAt(i).getLength();
697 public void setGapCharacter(char gc)
700 synchronized (sequences)
702 for (SequenceI seq : sequences)
704 seq.setSequence(seq.getSequenceAsString().replace('.', gc)
705 .replace('-', gc).replace(' ', gc));
713 * @return DOCUMENT ME!
716 public char getGapCharacter()
724 * @see jalview.datamodel.AlignmentI#isAligned()
727 public boolean isAligned()
729 return isAligned(false);
735 * @see jalview.datamodel.AlignmentI#isAligned(boolean)
738 public boolean isAligned(boolean includeHidden)
740 int width = getWidth();
741 if (hiddenSequences == null || hiddenSequences.getSize() == 0)
743 includeHidden = true; // no hidden sequences to check against.
745 for (int i = 0; i < sequences.size(); i++)
747 if (includeHidden || !hiddenSequences.isHidden(getSequenceAt(i)))
749 if (getSequenceAt(i).getLength() != width)
760 * Delete all annotations, including auto-calculated if the flag is set true.
761 * Returns true if at least one annotation was deleted, else false.
763 * @param includingAutoCalculated
767 public boolean deleteAllAnnotations(boolean includingAutoCalculated)
769 boolean result = false;
770 for (AlignmentAnnotation alan : getAlignmentAnnotation())
772 if (!alan.autoCalculated || includingAutoCalculated)
774 deleteAnnotation(alan);
784 * @seejalview.datamodel.AlignmentI#deleteAnnotation(jalview.datamodel.
785 * AlignmentAnnotation)
788 public boolean deleteAnnotation(AlignmentAnnotation aa)
790 return deleteAnnotation(aa, true);
794 public boolean deleteAnnotation(AlignmentAnnotation aa, boolean unhook)
798 if (annotations != null)
800 aSize = annotations.length;
808 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize - 1];
810 boolean swap = false;
813 for (int i = 0; i < aSize; i++)
815 if (annotations[i] == aa)
820 if (tIndex < temp.length)
822 temp[tIndex++] = annotations[i];
831 unhookAnnotation(aa);
838 * remove any object references associated with this annotation
842 private void unhookAnnotation(AlignmentAnnotation aa)
844 if (aa.sequenceRef != null)
846 aa.sequenceRef.removeAlignmentAnnotation(aa);
848 if (aa.groupRef != null)
850 // probably need to do more here in the future (post 2.5.0)
858 * @seejalview.datamodel.AlignmentI#addAnnotation(jalview.datamodel.
859 * AlignmentAnnotation)
862 public void addAnnotation(AlignmentAnnotation aa)
864 addAnnotation(aa, -1);
870 * @seejalview.datamodel.AlignmentI#addAnnotation(jalview.datamodel.
871 * AlignmentAnnotation, int)
874 public void addAnnotation(AlignmentAnnotation aa, int pos)
876 if (aa.getRNAStruc() != null)
878 hasRNAStructure = true;
882 if (annotations != null)
884 aSize = annotations.length + 1;
887 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
889 if (pos == -1 || pos >= aSize)
891 temp[aSize - 1] = aa;
900 for (i = 0; i < (aSize - 1); i++, p++)
908 temp[p] = annotations[i];
917 public void setAnnotationIndex(AlignmentAnnotation aa, int index)
919 if (aa == null || annotations == null || annotations.length - 1 < index)
924 int aSize = annotations.length;
925 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
929 for (int i = 0; i < aSize; i++)
938 temp[i] = annotations[i];
942 temp[i] = annotations[i - 1];
951 * returns all annotation on the alignment
953 public AlignmentAnnotation[] getAlignmentAnnotation()
959 public void setNucleotide(boolean b)
972 public boolean isNucleotide()
974 if (type == NUCLEOTIDE)
985 public boolean hasRNAStructure()
987 // TODO can it happen that structure is removed from alignment?
988 return hasRNAStructure;
992 public void setDataset(AlignmentI data)
994 if (dataset == null && data == null)
996 createDatasetAlignment();
998 else if (dataset == null && data != null)
1000 if (!(data instanceof Alignment))
1003 "Implementation Error: jalview.datamodel.Alignment does not yet support other implementations of AlignmentI as its dataset reference");
1005 dataset = (Alignment) data;
1006 for (int i = 0; i < getHeight(); i++)
1008 SequenceI currentSeq = getSequenceAt(i);
1009 SequenceI dsq = currentSeq.getDatasetSequence();
1012 dsq = currentSeq.createDatasetSequence();
1013 dataset.addSequence(dsq);
1017 while (dsq.getDatasetSequence() != null)
1019 dsq = dsq.getDatasetSequence();
1021 if (dataset.findIndex(dsq) == -1)
1023 dataset.addSequence(dsq);
1028 dataset.addAlignmentRef();
1032 * Creates a new dataset for this alignment. Can only be done once - if
1033 * dataset is not null this will not be performed.
1035 public void createDatasetAlignment()
1037 if (dataset != null)
1041 SequenceI[] seqs = new SequenceI[getHeight()];
1042 SequenceI currentSeq;
1043 for (int i = 0; i < getHeight(); i++)
1045 currentSeq = getSequenceAt(i);
1046 if (currentSeq.getDatasetSequence() != null)
1048 seqs[i] = currentSeq.getDatasetSequence();
1052 seqs[i] = currentSeq.createDatasetSequence();
1056 dataset = new Alignment(seqs);
1057 // move mappings to the dataset alignment
1058 dataset.codonFrameList = this.codonFrameList;
1059 this.codonFrameList = null;
1063 * reference count for number of alignments referencing this one.
1065 int alignmentRefs = 0;
1068 * increase reference count to this alignment.
1070 private void addAlignmentRef()
1076 public Alignment getDataset()
1082 public boolean padGaps()
1084 boolean modified = false;
1086 // Remove excess gaps from the end of alignment
1090 for (int i = 0; i < sequences.size(); i++)
1092 current = getSequenceAt(i);
1093 for (int j = current.getLength(); j > maxLength; j--)
1096 && !jalview.util.Comparison.isGap(current.getCharAt(j)))
1107 for (int i = 0; i < sequences.size(); i++)
1109 current = getSequenceAt(i);
1110 cLength = current.getLength();
1112 if (cLength < maxLength)
1114 current.insertCharAt(cLength, maxLength - cLength, gapCharacter);
1117 else if (current.getLength() > maxLength)
1119 current.deleteChars(maxLength, current.getLength());
1126 * Justify the sequences to the left or right by deleting and inserting gaps
1127 * before the initial residue or after the terminal residue
1130 * true if alignment padded to right, false to justify to left
1131 * @return true if alignment was changed
1134 public boolean justify(boolean right)
1136 boolean modified = false;
1138 // Remove excess gaps from the end of alignment
1140 int ends[] = new int[sequences.size() * 2];
1142 for (int i = 0; i < sequences.size(); i++)
1144 current = getSequenceAt(i);
1145 // This should really be a sequence method
1146 ends[i * 2] = current.findIndex(current.getStart());
1147 ends[i * 2 + 1] = current.findIndex(current.getStart()
1148 + current.getLength());
1149 boolean hitres = false;
1150 for (int j = 0, rs = 0, ssiz = current.getLength(); j < ssiz; j++)
1152 if (!jalview.util.Comparison.isGap(current.getCharAt(j)))
1161 ends[i * 2 + 1] = j;
1162 if (j - ends[i * 2] > maxLength)
1164 maxLength = j - ends[i * 2];
1172 // now edit the flanking gaps to justify to either left or right
1173 int cLength, extent, diff;
1174 for (int i = 0; i < sequences.size(); i++)
1176 current = getSequenceAt(i);
1178 cLength = 1 + ends[i * 2 + 1] - ends[i * 2];
1179 diff = maxLength - cLength; // number of gaps to indent
1180 extent = current.getLength();
1184 if (extent > ends[i * 2 + 1])
1186 current.deleteChars(ends[i * 2 + 1] + 1, extent);
1189 if (ends[i * 2] > diff)
1191 current.deleteChars(0, ends[i * 2] - diff);
1196 if (ends[i * 2] < diff)
1198 current.insertCharAt(0, diff - ends[i * 2], gapCharacter);
1206 if (ends[i * 2] > 0)
1208 current.deleteChars(0, ends[i * 2]);
1210 ends[i * 2 + 1] -= ends[i * 2];
1211 extent -= ends[i * 2];
1213 if (extent > maxLength)
1215 current.deleteChars(maxLength + 1, extent);
1220 if (extent < maxLength)
1222 current.insertCharAt(extent, maxLength - extent, gapCharacter);
1232 public HiddenSequences getHiddenSequences()
1234 return hiddenSequences;
1238 public CigarArray getCompactAlignment()
1240 synchronized (sequences)
1242 SeqCigar alseqs[] = new SeqCigar[sequences.size()];
1244 for (SequenceI seq : sequences)
1246 alseqs[i++] = new SeqCigar(seq);
1248 CigarArray cal = new CigarArray(alseqs);
1249 cal.addOperation(CigarArray.M, getWidth());
1255 public void setProperty(Object key, Object value)
1257 if (alignmentProperties == null)
1259 alignmentProperties = new Hashtable();
1262 alignmentProperties.put(key, value);
1266 public Object getProperty(Object key)
1268 if (alignmentProperties != null)
1270 return alignmentProperties.get(key);
1279 public Hashtable getProperties()
1281 return alignmentProperties;
1285 * Adds the given mapping to the stored set. Note this may be held on the
1286 * dataset alignment.
1289 public void addCodonFrame(AlignedCodonFrame codons)
1291 List<AlignedCodonFrame> acfs = getCodonFrames();
1292 if (codons != null && acfs != null && !acfs.contains(codons))
1302 * jalview.datamodel.AlignmentI#getCodonFrame(jalview.datamodel.SequenceI)
1305 public List<AlignedCodonFrame> getCodonFrame(SequenceI seq)
1311 List<AlignedCodonFrame> cframes = new ArrayList<AlignedCodonFrame>();
1312 for (AlignedCodonFrame acf : getCodonFrames())
1314 if (acf.involvesSequence(seq))
1323 * Sets the codon frame mappings (replacing any existing mappings). Note the
1324 * mappings are set on the dataset alignment instead if there is one.
1326 * @see jalview.datamodel.AlignmentI#setCodonFrames()
1329 public void setCodonFrames(List<AlignedCodonFrame> acfs)
1331 if (dataset != null)
1333 dataset.setCodonFrames(acfs);
1337 this.codonFrameList = acfs;
1342 * Returns the set of codon frame mappings. Any changes to the returned set
1343 * will affect the alignment. The mappings are held on (and read from) the
1344 * dataset alignment if there is one.
1346 * @see jalview.datamodel.AlignmentI#getCodonFrames()
1349 public List<AlignedCodonFrame> getCodonFrames()
1351 // TODO: Fix this method to fix failing AlignedCodonFrame tests
1352 // this behaviour is currently incorrect. method should return codon frames
1353 // for just the alignment,
1354 // selected from dataset
1355 return dataset != null ? dataset.getCodonFrames() : codonFrameList;
1359 * Removes the given mapping from the stored set. Note that the mappings are
1360 * held on the dataset alignment if there is one.
1363 public boolean removeCodonFrame(AlignedCodonFrame codons)
1365 List<AlignedCodonFrame> acfs = getCodonFrames();
1366 if (codons == null || acfs == null)
1370 return acfs.remove(codons);
1374 public void append(AlignmentI toappend)
1376 // TODO JAL-1270 needs test coverage
1377 // currently tested for use in jalview.gui.SequenceFetcher
1378 boolean samegap = toappend.getGapCharacter() == getGapCharacter();
1379 char oldc = toappend.getGapCharacter();
1380 boolean hashidden = toappend.getHiddenSequences() != null
1381 && toappend.getHiddenSequences().hiddenSequences != null;
1382 // get all sequences including any hidden ones
1383 List<SequenceI> sqs = (hashidden) ? toappend.getHiddenSequences()
1384 .getFullAlignment().getSequences() : toappend.getSequences();
1387 // avoid self append deadlock by
1388 List<SequenceI> toappendsq = new ArrayList<SequenceI>();
1391 for (SequenceI addedsq : sqs)
1395 char[] oldseq = addedsq.getSequence();
1396 for (int c = 0; c < oldseq.length; c++)
1398 if (oldseq[c] == oldc)
1400 oldseq[c] = gapCharacter;
1404 toappendsq.add(addedsq);
1407 for (SequenceI addedsq : toappendsq)
1409 addSequence(addedsq);
1412 AlignmentAnnotation[] alan = toappend.getAlignmentAnnotation();
1413 for (int a = 0; alan != null && a < alan.length; a++)
1415 addAnnotation(alan[a]);
1419 getCodonFrames().addAll(toappend.getCodonFrames());
1421 List<SequenceGroup> sg = toappend.getGroups();
1424 for (SequenceGroup _sg : sg)
1429 if (toappend.getHiddenSequences() != null)
1431 HiddenSequences hs = toappend.getHiddenSequences();
1432 if (hiddenSequences == null)
1434 hiddenSequences = new HiddenSequences(this);
1436 if (hs.hiddenSequences != null)
1438 for (int s = 0; s < hs.hiddenSequences.length; s++)
1440 // hide the newly appended sequence in the alignment
1441 if (hs.hiddenSequences[s] != null)
1443 hiddenSequences.hideSequence(hs.hiddenSequences[s]);
1448 if (toappend.getProperties() != null)
1450 // we really can't do very much here - just try to concatenate strings
1451 // where property collisions occur.
1452 Enumeration key = toappend.getProperties().keys();
1453 while (key.hasMoreElements())
1455 Object k = key.nextElement();
1456 Object ourval = this.getProperty(k);
1457 Object toapprop = toappend.getProperty(k);
1460 if (ourval.getClass().equals(toapprop.getClass())
1461 && !ourval.equals(toapprop))
1463 if (ourval instanceof String)
1466 this.setProperty(k, ((String) ourval) + "; "
1467 + ((String) toapprop));
1471 if (ourval instanceof Vector)
1474 Enumeration theirv = ((Vector) toapprop).elements();
1475 while (theirv.hasMoreElements())
1477 ((Vector) ourval).addElement(theirv);
1485 // just add new property directly
1486 setProperty(k, toapprop);
1494 public AlignmentAnnotation findOrCreateAnnotation(String name,
1495 String calcId, boolean autoCalc, SequenceI seqRef,
1496 SequenceGroup groupRef)
1498 assert (name != null);
1499 if (annotations != null)
1501 for (AlignmentAnnotation annot : getAlignmentAnnotation())
1503 if (annot.autoCalculated == autoCalc && (name.equals(annot.label))
1504 && (calcId == null || annot.getCalcId().equals(calcId))
1505 && annot.sequenceRef == seqRef
1506 && annot.groupRef == groupRef)
1512 AlignmentAnnotation annot = new AlignmentAnnotation(name, name,
1513 new Annotation[1], 0f, 0f, AlignmentAnnotation.BAR_GRAPH);
1514 annot.hasText = false;
1515 annot.setCalcId(new String(calcId));
1516 annot.autoCalculated = autoCalc;
1519 annot.setSequenceRef(seqRef);
1521 annot.groupRef = groupRef;
1522 addAnnotation(annot);
1528 public Iterable<AlignmentAnnotation> findAnnotation(String calcId)
1530 ArrayList<AlignmentAnnotation> aa = new ArrayList<AlignmentAnnotation>();
1531 for (AlignmentAnnotation a : getAlignmentAnnotation())
1533 if (a.getCalcId() == calcId
1534 || (a.getCalcId() != null && calcId != null && a.getCalcId()
1544 * Returns an iterable collection of any annotations that match on given
1545 * sequence ref, calcId and label (ignoring null values).
1548 public Iterable<AlignmentAnnotation> findAnnotations(SequenceI seq,
1549 String calcId, String label)
1551 ArrayList<AlignmentAnnotation> aa = new ArrayList<AlignmentAnnotation>();
1552 for (AlignmentAnnotation ann : getAlignmentAnnotation())
1554 if (ann.getCalcId() != null && ann.getCalcId().equals(calcId)
1555 && ann.sequenceRef != null && ann.sequenceRef == seq
1556 && ann.label != null && ann.label.equals(label))
1565 public void moveSelectedSequencesByOne(SequenceGroup sg,
1566 Map<SequenceI, SequenceCollectionI> map, boolean up)
1568 synchronized (sequences)
1573 for (int i = 1, iSize = sequences.size(); i < iSize; i++)
1575 SequenceI seq = sequences.get(i);
1576 if (!sg.getSequences(map).contains(seq))
1581 SequenceI temp = sequences.get(i - 1);
1582 if (sg.getSequences(null).contains(temp))
1587 sequences.set(i, temp);
1588 sequences.set(i - 1, seq);
1593 for (int i = sequences.size() - 2; i > -1; i--)
1595 SequenceI seq = sequences.get(i);
1596 if (!sg.getSequences(map).contains(seq))
1601 SequenceI temp = sequences.get(i + 1);
1602 if (sg.getSequences(map).contains(temp))
1607 sequences.set(i, temp);
1608 sequences.set(i + 1, seq);
1616 public void validateAnnotation(AlignmentAnnotation alignmentAnnotation)
1618 alignmentAnnotation.validateRangeAndDisplay();
1619 if (isNucleotide() && alignmentAnnotation.isValidStruc())
1621 hasRNAStructure = true;
1625 private SequenceI seqrep = null;
1629 * @return the representative sequence for this group
1632 public SequenceI getSeqrep()
1638 * set the representative sequence for this group. Note - this affects the
1639 * interpretation of the Hidereps attribute.
1642 * the seqrep to set (null means no sequence representative)
1645 public void setSeqrep(SequenceI seqrep)
1647 this.seqrep = seqrep;
1652 * @return true if group has a sequence representative
1655 public boolean hasSeqrep()
1657 return seqrep != null;
1661 public int getEndRes()
1663 return getWidth() - 1;
1667 public int getStartRes()
1673 * In the case of AlignmentI - returns the dataset for the alignment, if set
1676 * @see jalview.datamodel.AnnotatedCollectionI#getContext()
1679 public AnnotatedCollectionI getContext()
1685 * Align this alignment like the given (mapped) one.
1688 public int alignAs(AlignmentI al)
1691 * Currently retains unmapped gaps (in introns), regaps mapped regions
1694 return alignAs(al, false, true);
1698 * Align this alignment 'the same as' the given one. Mapped sequences only are
1699 * realigned. If both of the same type (nucleotide/protein) then align both
1700 * identically. If this is nucleotide and the other is protein, make 3 gaps
1701 * for each gap in the protein sequences. If this is protein and the other is
1702 * nucleotide, insert a gap for each 3 gaps (or part thereof) between
1703 * nucleotide bases. If this is protein and the other is nucleotide, gaps
1704 * protein to match the relative ordering of codons in the nucleotide.
1706 * Parameters control whether gaps in exon (mapped) and intron (unmapped)
1707 * regions are preserved. Gaps that connect introns to exons are treated
1708 * conservatively, i.e. only preserved if both intron and exon gaps are
1709 * preserved. TODO: check caveats below where the implementation fails
1712 * - must have same dataset, and sequences in al must have equivalent
1713 * dataset sequence and start/end bounds under given mapping
1714 * @param preserveMappedGaps
1715 * if true, gaps within and between mapped codons are preserved
1716 * @param preserveUnmappedGaps
1717 * if true, gaps within and between unmapped codons are preserved
1720 public int alignAs(AlignmentI al, boolean preserveMappedGaps,
1721 boolean preserveUnmappedGaps)
1723 // TODO should this method signature be the one in the interface?
1724 boolean thisIsNucleotide = this.isNucleotide();
1725 boolean thatIsProtein = !al.isNucleotide();
1726 if (!thatIsProtein && !thisIsNucleotide)
1728 return AlignmentUtils.alignProteinAsDna(this, al);
1730 else if (thatIsProtein && thisIsNucleotide)
1732 return AlignmentUtils.alignCdsAsProtein(this, al);
1734 return AlignmentUtils.alignAs(this, al);
1738 * Returns the alignment in Fasta format. Behaviour of this method is not
1739 * guaranteed between versions.
1742 public String toString()
1744 return new FastaFile().print(getSequencesArray());
1748 * Returns the set of distinct sequence names. No ordering is guaranteed.
1751 public Set<String> getSequenceNames()
1753 Set<String> names = new HashSet<String>();
1754 for (SequenceI seq : getSequences())
1756 names.add(seq.getName());
1762 public boolean hasValidSequence()
1764 boolean hasValidSeq = false;
1765 for (SequenceI seq : getSequences())
1767 if ((seq.getEnd() - seq.getStart()) > 0)
1777 * Update any mappings to 'virtual' sequences to compatible real ones, if
1778 * present in the added sequences. Returns a count of mappings updated.
1784 public int realiseMappings(List<SequenceI> seqs)
1787 for (SequenceI seq : seqs)
1789 for (AlignedCodonFrame mapping : getCodonFrames())
1791 count += mapping.realiseWith(seq);
1798 * Returns the first AlignedCodonFrame that has a mapping between the given
1806 public AlignedCodonFrame getMapping(SequenceI mapFrom, SequenceI mapTo)
1808 for (AlignedCodonFrame acf : getCodonFrames())
1810 if (acf.getAaForDnaSeq(mapFrom) == mapTo)
1819 public int[] getVisibleStartAndEndIndex(List<int[]> hiddenCols)
1821 int[] alignmentStartEnd = new int[] { 0, getWidth() - 1 };
1822 int startPos = alignmentStartEnd[0];
1823 int endPos = alignmentStartEnd[1];
1825 int[] lowestRange = new int[] { -1, -1 };
1826 int[] higestRange = new int[] { -1, -1 };
1828 for (int[] hiddenCol : hiddenCols)
1830 lowestRange = (hiddenCol[0] <= startPos) ? hiddenCol : lowestRange;
1831 higestRange = (hiddenCol[1] >= endPos) ? hiddenCol : higestRange;
1834 if (lowestRange[0] == -1 && lowestRange[1] == -1)
1836 startPos = alignmentStartEnd[0];
1840 startPos = lowestRange[1] + 1;
1843 if (higestRange[0] == -1 && higestRange[1] == -1)
1845 endPos = alignmentStartEnd[1];
1849 endPos = higestRange[0] - 1;
1851 return new int[] { startPos, endPos };