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.jsdev.GenericFileAdapter;
26 import jalview.util.Comparison;
27 import jalview.util.MessageManager;
29 import java.util.ArrayList;
30 import java.util.Enumeration;
31 import java.util.HashSet;
32 import java.util.Hashtable;
33 import java.util.LinkedHashSet;
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 protected Alignment dataset;
50 protected List<SequenceI> sequences;
52 protected List<SequenceGroup> groups = java.util.Collections
53 .synchronizedList(new ArrayList<SequenceGroup>());
55 protected char gapCharacter = '-';
57 protected int type = NUCLEOTIDE;
59 public static final int PROTEIN = 0;
61 public static final int NUCLEOTIDE = 1;
63 public boolean hasRNAStructure = false;
66 public AlignmentAnnotation[] annotations;
68 HiddenSequences hiddenSequences = new HiddenSequences(this);
70 public Hashtable alignmentProperties;
72 private Set<AlignedCodonFrame> codonFrameList = new LinkedHashSet<AlignedCodonFrame>();
74 private void initAlignment(SequenceI[] seqs)
78 if (Comparison.isNucleotide(seqs))
87 sequences = java.util.Collections
88 .synchronizedList(new ArrayList<SequenceI>());
90 for (i = 0; i < seqs.length; i++)
92 sequences.add(seqs[i]);
98 * Make a 'copy' alignment - sequences have new copies of features and
99 * annotations, but share the original dataset sequences.
101 public Alignment(AlignmentI al)
103 SequenceI[] seqs = al.getSequencesArray();
104 for (int i = 0; i < seqs.length; i++)
106 seqs[i] = new Sequence(seqs[i]);
110 * Share the same dataset sequence mappings (if any). TODO: find a better
111 * place for these to live (alignment dataset?).
113 this.codonFrameList = ((Alignment) al).codonFrameList;
119 * Make an alignment from an array of Sequences.
124 public Alignment(SequenceI[] seqs)
130 * Make a new alignment from an array of SeqCigars
132 * no references in jalview?
139 public Alignment(SeqCigar[] alseqs)
141 SequenceI[] seqs = SeqCigar.createAlignmentSequences(alseqs,
142 gapCharacter, new ColumnSelection(), null);
147 * Make a new alignment from an CigarArray JBPNote - can only do this when
148 * compactAlignment does not contain hidden regions. JBPNote - must also check
149 * that compactAlignment resolves to a set of SeqCigars - or construct them
152 * @param compactAlignment
155 public static AlignmentI createAlignment(CigarArray compactAlignment)
157 throw new Error(MessageManager.getString("error.alignment_cigararray_not_implemented"));
158 // this(compactAlignment.refCigars);
162 public List<SequenceI> getSequences()
168 public List<SequenceI> getSequences(
169 Map<SequenceI, SequenceCollectionI> hiddenReps)
171 // TODO: in jalview 2.8 we don't do anything with hiddenreps - fix design to
177 public SequenceI[] getSequencesArray()
179 if (sequences == null)
183 synchronized (sequences)
185 return sequences.toArray(new SequenceI[sequences.size()]);
190 * Returns a map of lists of sequences keyed by sequence name.
195 public Map<String, List<SequenceI>> getSequencesByName()
197 return AlignmentUtils.getSequencesByName(this);
206 * @return DOCUMENT ME!
209 public SequenceI getSequenceAt(int i)
211 synchronized (sequences)
213 if (i > -1 && i < sequences.size())
215 return sequences.get(i);
222 * Adds a sequence to the alignment. Recalculates maxLength and size.
227 public void addSequence(SequenceI snew)
231 // maintain dataset integrity
232 if (snew.getDatasetSequence() != null)
234 getDataset().addSequence(snew.getDatasetSequence());
238 // derive new sequence
239 SequenceI adding = snew.deriveSequence();
240 getDataset().addSequence(adding.getDatasetSequence());
244 if (sequences == null)
246 initAlignment(new SequenceI[]
251 synchronized (sequences)
256 if (hiddenSequences != null)
258 hiddenSequences.adjustHeightSequenceAdded();
263 * Adds a sequence to the alignment. Recalculates maxLength and size.
268 public void setSequenceAt(int i, SequenceI snew)
270 synchronized (sequences)
273 sequences.set(i, snew);
280 * @return DOCUMENT ME!
283 public List<SequenceGroup> getGroups()
289 public void finalize()
291 if (getDataset() != null)
293 getDataset().removeAlignmentRef();
300 hiddenSequences = null;
304 * decrement the alignmentRefs counter by one and call finalize if it goes to
307 private void removeAlignmentRef()
309 if (--alignmentRefs == 0)
322 public void deleteSequence(SequenceI s)
324 deleteSequence(findIndex(s));
334 public void deleteSequence(int i)
336 if (i > -1 && i < getHeight())
338 synchronized (sequences)
341 hiddenSequences.adjustHeightSequenceDeleted(i);
349 * @see AlignmentI#findGroup(jalview.datamodel.SequenceI)
352 public SequenceGroup findGroup(SequenceI s)
354 synchronized (groups)
356 for (int i = 0; i < this.groups.size(); i++)
358 SequenceGroup sg = groups.get(i);
360 if (sg.getSequences(null).contains(s))
373 * AlignmentI#findAllGroups(jalview.datamodel.SequenceI)
376 public SequenceGroup[] findAllGroups(SequenceI s)
378 ArrayList<SequenceGroup> temp = new ArrayList<SequenceGroup>();
380 synchronized (groups)
382 int gSize = groups.size();
383 for (int i = 0; i < gSize; i++)
385 SequenceGroup sg = groups.get(i);
386 if (sg == null || sg.getSequences() == null)
388 this.deleteGroup(sg);
393 if (sg.getSequences().contains(s))
399 SequenceGroup[] ret = new SequenceGroup[temp.size()];
400 return temp.toArray(ret);
405 public void addGroup(SequenceGroup sg)
407 synchronized (groups)
409 if (!groups.contains(sg))
411 if (hiddenSequences.getSize() > 0)
413 int i, iSize = sg.getSize();
414 for (i = 0; i < iSize; i++)
416 if (!sequences.contains(sg.getSequenceAt(i)))
418 sg.deleteSequence(sg.getSequenceAt(i), false);
424 if (sg.getSize() < 1)
436 * remove any annotation that references gp
439 * (if null, removes all group associated annotation)
441 private void removeAnnotationForGroup(SequenceGroup gp)
443 if (annotations == null || annotations.length == 0)
447 // remove annotation very quickly
448 AlignmentAnnotation[] t, todelete = new AlignmentAnnotation[annotations.length], tokeep = new AlignmentAnnotation[annotations.length];
452 for (i = 0, p = 0, k = 0; i < annotations.length; i++)
454 if (annotations[i].groupRef != null)
456 todelete[p++] = annotations[i];
460 tokeep[k++] = annotations[i];
466 for (i = 0, p = 0, k = 0; i < annotations.length; i++)
468 if (annotations[i].groupRef == gp)
470 todelete[p++] = annotations[i];
474 tokeep[k++] = annotations[i];
480 // clear out the group associated annotation.
481 for (i = 0; i < p; i++)
483 unhookAnnotation(todelete[i]);
486 t = new AlignmentAnnotation[k];
487 for (i = 0; i < k; i++)
496 public void deleteAllGroups()
498 synchronized (groups)
500 if (annotations != null)
502 removeAnnotationForGroup(null);
504 for (SequenceGroup sg : groups)
514 public void deleteGroup(SequenceGroup g)
516 synchronized (groups)
518 if (groups.contains(g))
520 removeAnnotationForGroup(g);
529 public SequenceI findName(String name)
531 return findName(name, false);
537 * @see AlignmentI#findName(java.lang.String, boolean)
540 public SequenceI findName(String token, boolean b)
542 return findName(null, token, b);
548 * @see AlignmentI#findName(SequenceI, java.lang.String,
552 public SequenceI findName(SequenceI startAfter, String token, boolean b)
557 String sqname = null;
558 if (startAfter != null)
560 // try to find the sequence in the alignment
561 boolean matched = false;
562 while (i < sequences.size())
564 if (getSequenceAt(i++) == startAfter)
575 while (i < sequences.size())
577 sq = getSequenceAt(i);
578 sqname = sq.getName();
579 if (sqname.equals(token) // exact match
580 || (b && // allow imperfect matches - case varies
581 (sqname.equalsIgnoreCase(token))))
583 return getSequenceAt(i);
593 public SequenceI[] findSequenceMatch(String name)
595 Vector matches = new Vector();
598 while (i < sequences.size())
600 if (getSequenceAt(i).getName().equals(name))
602 matches.addElement(getSequenceAt(i));
607 SequenceI[] result = new SequenceI[matches.size()];
608 for (i = 0; i < result.length; i++)
610 result[i] = (SequenceI) matches.elementAt(i);
620 * @see AlignmentI#findIndex(jalview.datamodel.SequenceI)
623 public int findIndex(SequenceI s)
627 while (i < sequences.size())
629 if (s == getSequenceAt(i))
644 * AlignmentI#findIndex(jalview.datamodel.SearchResults)
647 public int findIndex(SearchResults results)
651 while (i < sequences.size())
653 if (results.involvesSequence(getSequenceAt(i)))
665 * @return DOCUMENT ME!
668 public int getHeight()
670 return sequences.size();
676 * @return DOCUMENT ME!
679 public int getWidth()
683 for (int i = 0; i < sequences.size(); i++)
685 if (getSequenceAt(i).getLength() > maxLength)
687 maxLength = getSequenceAt(i).getLength();
701 public void setGapCharacter(char gc)
704 synchronized (sequences)
706 for (SequenceI seq : sequences)
708 seq.setSequence(seq.getSequenceAsString().replace('.', gc)
709 .replace('-', gc).replace(' ', gc));
717 * @return DOCUMENT ME!
720 public char getGapCharacter()
728 * @see AlignmentI#isAligned()
731 public boolean isAligned()
733 return isAligned(false);
739 * @see AlignmentI#isAligned(boolean)
742 public boolean isAligned(boolean includeHidden)
744 int width = getWidth();
745 if (hiddenSequences == null || hiddenSequences.getSize() == 0)
747 includeHidden = true; // no hidden sequences to check against.
749 for (int i = 0; i < sequences.size(); i++)
751 if (includeHidden || !hiddenSequences.isHidden(getSequenceAt(i)))
753 if (getSequenceAt(i).getLength() != width)
764 * Delete all annotations, including auto-calculated if the flag is set true.
765 * Returns true if at least one annotation was deleted, else false.
767 * @param includingAutoCalculated
771 public boolean deleteAllAnnotations(boolean includingAutoCalculated)
773 boolean result = false;
774 for (AlignmentAnnotation alan : getAlignmentAnnotation())
776 if (!alan.autoCalculated || includingAutoCalculated)
778 deleteAnnotation(alan);
788 * @seeAlignmentI#deleteAnnotation(jalview.datamodel.
789 * AlignmentAnnotation)
792 public boolean deleteAnnotation(AlignmentAnnotation aa)
794 return deleteAnnotation(aa, true);
798 public boolean deleteAnnotation(AlignmentAnnotation aa, boolean unhook)
802 if (annotations != null)
804 aSize = annotations.length;
812 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize - 1];
814 boolean swap = false;
817 for (int i = 0; i < aSize; i++)
819 if (annotations[i] == aa)
824 if (tIndex < temp.length)
826 temp[tIndex++] = annotations[i];
835 unhookAnnotation(aa);
842 * remove any object references associated with this annotation
846 private void unhookAnnotation(AlignmentAnnotation aa)
848 if (aa.sequenceRef != null)
850 aa.sequenceRef.removeAlignmentAnnotation(aa);
852 if (aa.groupRef != null)
854 // probably need to do more here in the future (post 2.5.0)
862 * @seeAlignmentI#addAnnotation(jalview.datamodel.
863 * AlignmentAnnotation)
866 public void addAnnotation(AlignmentAnnotation aa)
868 addAnnotation(aa, -1);
874 * @seeAlignmentI#addAnnotation(jalview.datamodel.
875 * AlignmentAnnotation, int)
878 public void addAnnotation(AlignmentAnnotation aa, int pos)
880 if (aa.getRNAStruc() != null)
882 hasRNAStructure = true;
886 if (annotations != null)
888 aSize = annotations.length + 1;
891 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
893 if (pos == -1 || pos >= aSize)
895 temp[aSize - 1] = aa;
904 for (i = 0; i < (aSize - 1); i++, p++)
912 temp[p] = annotations[i];
921 public void setAnnotationIndex(AlignmentAnnotation aa, int index)
923 if (aa == null || annotations == null || annotations.length - 1 < index)
928 int aSize = annotations.length;
929 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
933 for (int i = 0; i < aSize; i++)
942 temp[i] = annotations[i];
946 temp[i] = annotations[i - 1];
955 * returns all annotation on the alignment
957 public AlignmentAnnotation[] getAlignmentAnnotation()
963 public void setNucleotide(boolean b)
976 public boolean isNucleotide()
978 if (type == NUCLEOTIDE)
989 public boolean hasRNAStructure()
991 // TODO can it happen that structure is removed from alignment?
992 return hasRNAStructure;
996 public void setDataset(Alignment data)
998 if (dataset == null && data == null)
1000 // Create a new dataset for this alignment.
1001 // Can only be done once, if dataset is not null
1002 // This will not be performed
1003 SequenceI[] seqs = new SequenceI[getHeight()];
1004 SequenceI currentSeq;
1005 for (int i = 0; i < getHeight(); i++)
1007 currentSeq = getSequenceAt(i);
1008 if (currentSeq.getDatasetSequence() != null)
1010 seqs[i] = currentSeq.getDatasetSequence();
1014 seqs[i] = currentSeq.createDatasetSequence();
1018 dataset = new Alignment(seqs);
1020 else if (dataset == null && data != null)
1023 for (int i = 0; i < getHeight(); i++)
1025 SequenceI currentSeq = getSequenceAt(i);
1026 SequenceI dsq = currentSeq.getDatasetSequence();
1029 dsq = currentSeq.createDatasetSequence();
1030 dataset.addSequence(dsq);
1034 while (dsq.getDatasetSequence() != null)
1036 dsq = dsq.getDatasetSequence();
1038 if (dataset.findIndex(dsq) == -1)
1040 dataset.addSequence(dsq);
1045 dataset.addAlignmentRef();
1049 * reference count for number of alignments referencing this one.
1051 int alignmentRefs = 0;
1054 * increase reference count to this alignment.
1056 private void addAlignmentRef()
1062 public Alignment getDataset()
1068 public boolean padGaps()
1070 boolean modified = false;
1072 // Remove excess gaps from the end of alignment
1076 for (int i = 0; i < sequences.size(); i++)
1078 current = getSequenceAt(i);
1079 for (int j = current.getLength(); j > maxLength; j--)
1082 && !Comparison.isGap(current.getCharAt(j)))
1093 for (int i = 0; i < sequences.size(); i++)
1095 current = getSequenceAt(i);
1096 cLength = current.getLength();
1098 if (cLength < maxLength)
1100 current.insertCharAt(cLength, maxLength - cLength, gapCharacter);
1103 else if (current.getLength() > maxLength)
1105 current.deleteChars(maxLength, current.getLength());
1112 * Justify the sequences to the left or right by deleting and inserting gaps
1113 * before the initial residue or after the terminal residue
1116 * true if alignment padded to right, false to justify to left
1117 * @return true if alignment was changed
1120 public boolean justify(boolean right)
1122 boolean modified = false;
1124 // Remove excess gaps from the end of alignment
1126 int ends[] = new int[sequences.size() * 2];
1128 for (int i = 0; i < sequences.size(); i++)
1130 current = getSequenceAt(i);
1131 // This should really be a sequence method
1132 ends[i * 2] = current.findIndex(current.getStart());
1133 ends[i * 2 + 1] = current.findIndex(current.getStart()
1134 + current.getLength());
1135 boolean hitres = false;
1136 for (int j = 0, rs = 0, ssiz = current.getLength(); j < ssiz; j++)
1138 if (!Comparison.isGap(current.getCharAt(j)))
1147 ends[i * 2 + 1] = j;
1148 if (j - ends[i * 2] > maxLength)
1150 maxLength = j - ends[i * 2];
1158 // now edit the flanking gaps to justify to either left or right
1159 int cLength, extent, diff;
1160 for (int i = 0; i < sequences.size(); i++)
1162 current = getSequenceAt(i);
1164 cLength = 1 + ends[i * 2 + 1] - ends[i * 2];
1165 diff = maxLength - cLength; // number of gaps to indent
1166 extent = current.getLength();
1170 if (extent > ends[i * 2 + 1])
1172 current.deleteChars(ends[i * 2 + 1] + 1, extent);
1175 if (ends[i * 2] > diff)
1177 current.deleteChars(0, ends[i * 2] - diff);
1182 if (ends[i * 2] < diff)
1184 current.insertCharAt(0, diff - ends[i * 2], gapCharacter);
1192 if (ends[i * 2] > 0)
1194 current.deleteChars(0, ends[i * 2]);
1196 ends[i * 2 + 1] -= ends[i * 2];
1197 extent -= ends[i * 2];
1199 if (extent > maxLength)
1201 current.deleteChars(maxLength + 1, extent);
1206 if (extent < maxLength)
1208 current.insertCharAt(extent, maxLength - extent, gapCharacter);
1218 public HiddenSequences getHiddenSequences()
1220 return hiddenSequences;
1224 public CigarArray getCompactAlignment()
1226 synchronized (sequences)
1228 SeqCigar alseqs[] = new SeqCigar[sequences.size()];
1230 for (SequenceI seq : sequences)
1232 alseqs[i++] = new SeqCigar(seq);
1234 CigarArray cal = new CigarArray(alseqs);
1235 cal.addOperation(CigarArray.M, getWidth());
1241 public void setProperty(Object key, Object value)
1243 if (alignmentProperties == null)
1245 alignmentProperties = new Hashtable();
1248 alignmentProperties.put(key, value);
1252 public Object getProperty(Object key)
1254 if (alignmentProperties != null)
1256 return alignmentProperties.get(key);
1265 public Hashtable getProperties()
1267 return alignmentProperties;
1274 * AlignmentI#addCodonFrame(jalview.datamodel.AlignedCodonFrame
1278 public void addCodonFrame(AlignedCodonFrame codons)
1282 codonFrameList.add(codons);
1290 * AlignmentI#getCodonFrame(jalview.datamodel.SequenceI)
1293 public List<AlignedCodonFrame> getCodonFrame(SequenceI seq)
1299 List<AlignedCodonFrame> cframes = new ArrayList<AlignedCodonFrame>();
1300 for (AlignedCodonFrame acf : codonFrameList)
1302 if (acf.involvesSequence(seq))
1311 * Sets the codon frame mappings (replacing any existing mappings).
1313 * @see AlignmentI#setCodonFrames()
1316 public void setCodonFrames(Set<AlignedCodonFrame> acfs)
1318 this.codonFrameList = acfs;
1322 * Returns the set of codon frame mappings. Any changes to the returned set
1323 * will affect the alignment.
1325 * @see AlignmentI#getCodonFrames()
1328 public Set<AlignedCodonFrame> getCodonFrames()
1330 return codonFrameList;
1336 * @seeAlignmentI#removeCodonFrame(jalview.datamodel.
1337 * AlignedCodonFrame)
1340 public boolean removeCodonFrame(AlignedCodonFrame codons)
1342 if (codons == null || codonFrameList == null)
1346 return codonFrameList.remove(codons);
1350 public void append(AlignmentI toappend)
1352 if (toappend == this)
1354 System.err.println("Self append may cause a deadlock.");
1356 // TODO test this method for a future 2.5 release
1357 // currently tested for use in jalview.gui.SequenceFetcher
1358 boolean samegap = toappend.getGapCharacter() == getGapCharacter();
1359 char oldc = toappend.getGapCharacter();
1360 boolean hashidden = toappend.getHiddenSequences() != null
1361 && toappend.getHiddenSequences().hiddenSequences != null;
1362 // get all sequences including any hidden ones
1363 List<SequenceI> sqs = (hashidden) ? toappend.getHiddenSequences()
1364 .getFullAlignment().getSequences() : toappend.getSequences();
1369 for (SequenceI addedsq : sqs)
1373 char[] oldseq = addedsq.getSequence();
1374 for (int c = 0; c < oldseq.length; c++)
1376 if (oldseq[c] == oldc)
1378 oldseq[c] = gapCharacter;
1382 addSequence(addedsq);
1386 AlignmentAnnotation[] alan = toappend.getAlignmentAnnotation();
1387 for (int a = 0; alan != null && a < alan.length; a++)
1389 addAnnotation(alan[a]);
1392 this.codonFrameList.addAll(toappend.getCodonFrames());
1394 List<SequenceGroup> sg = toappend.getGroups();
1397 for (SequenceGroup _sg : sg)
1402 if (toappend.getHiddenSequences() != null)
1404 HiddenSequences hs = toappend.getHiddenSequences();
1405 if (hiddenSequences == null)
1407 hiddenSequences = new HiddenSequences(this);
1409 if (hs.hiddenSequences != null)
1411 for (int s = 0; s < hs.hiddenSequences.length; s++)
1413 // hide the newly appended sequence in the alignment
1414 if (hs.hiddenSequences[s] != null)
1416 hiddenSequences.hideSequence(hs.hiddenSequences[s]);
1421 if (toappend.getProperties() != null)
1423 // we really can't do very much here - just try to concatenate strings
1424 // where property collisions occur.
1425 Enumeration key = toappend.getProperties().keys();
1426 while (key.hasMoreElements())
1428 Object k = key.nextElement();
1429 Object ourval = this.getProperty(k);
1430 Object toapprop = toappend.getProperty(k);
1433 if (ourval.getClass().equals(toapprop.getClass())
1434 && !ourval.equals(toapprop))
1436 if (ourval instanceof String)
1439 this.setProperty(k, ((String) ourval) + "; "
1440 + ((String) toapprop));
1444 if (ourval instanceof Vector)
1447 Enumeration theirv = ((Vector) toapprop).elements();
1448 while (theirv.hasMoreElements())
1450 ((Vector) ourval).addElement(theirv);
1458 // just add new property directly
1459 setProperty(k, toapprop);
1467 public AlignmentAnnotation findOrCreateAnnotation(String name,
1468 String calcId, boolean autoCalc, SequenceI seqRef,
1469 SequenceGroup groupRef)
1471 assert (name != null);
1472 if (annotations != null)
1474 for (AlignmentAnnotation annot : getAlignmentAnnotation())
1476 if (annot.autoCalculated == autoCalc && (name.equals(annot.label))
1477 && (calcId == null || annot.getCalcId().equals(calcId))
1478 && annot.sequenceRef == seqRef
1479 && annot.groupRef == groupRef)
1485 AlignmentAnnotation annot = new AlignmentAnnotation(name, name,
1486 new Annotation[1], 0f, 0f, AlignmentAnnotation.BAR_GRAPH);
1487 annot.hasText = false;
1488 annot.setCalcId(new String(calcId));
1489 annot.autoCalculated = autoCalc;
1492 annot.setSequenceRef(seqRef);
1494 annot.groupRef = groupRef;
1495 addAnnotation(annot);
1501 public Iterable<AlignmentAnnotation> findAnnotation(String calcId)
1503 ArrayList<AlignmentAnnotation> aa = new ArrayList<AlignmentAnnotation>();
1504 for (AlignmentAnnotation a : getAlignmentAnnotation())
1506 if (a.getCalcId() == calcId
1507 || (a.getCalcId() != null && calcId != null && a.getCalcId()
1517 * Returns an iterable collection of any annotations that match on given
1518 * sequence ref, calcId and label (ignoring null values).
1521 public Iterable<AlignmentAnnotation> findAnnotations(SequenceI seq,
1522 String calcId, String label)
1524 ArrayList<AlignmentAnnotation> aa = new ArrayList<AlignmentAnnotation>();
1525 for (AlignmentAnnotation ann : getAlignmentAnnotation())
1527 if (ann.getCalcId() != null && ann.getCalcId().equals(calcId)
1528 && ann.sequenceRef != null && ann.sequenceRef == seq
1529 && ann.label != null && ann.label.equals(label))
1538 public void moveSelectedSequencesByOne(SequenceGroup sg,
1539 Map<SequenceI, SequenceCollectionI> map, boolean up)
1541 synchronized (sequences)
1546 for (int i = 1, iSize = sequences.size(); i < iSize; i++)
1548 SequenceI seq = sequences.get(i);
1549 if (!sg.getSequences(map).contains(seq))
1554 SequenceI temp = sequences.get(i - 1);
1555 if (sg.getSequences(null).contains(temp))
1560 sequences.set(i, temp);
1561 sequences.set(i - 1, seq);
1566 for (int i = sequences.size() - 2; i > -1; i--)
1568 SequenceI seq = sequences.get(i);
1569 if (!sg.getSequences(map).contains(seq))
1574 SequenceI temp = sequences.get(i + 1);
1575 if (sg.getSequences(map).contains(temp))
1580 sequences.set(i, temp);
1581 sequences.set(i + 1, seq);
1589 public void validateAnnotation(AlignmentAnnotation alignmentAnnotation)
1591 alignmentAnnotation.validateRangeAndDisplay();
1592 if (isNucleotide() && alignmentAnnotation.isValidStruc())
1594 hasRNAStructure = true;
1599 private SequenceI seqrep=null;
1603 * @return the representative sequence for this group
1605 public SequenceI getSeqrep()
1611 * set the representative sequence for this group. Note - this affects the
1612 * interpretation of the Hidereps attribute.
1615 * the seqrep to set (null means no sequence representative)
1617 public void setSeqrep(SequenceI seqrep)
1619 this.seqrep = seqrep;
1624 * @return true if group has a sequence representative
1626 public boolean hasSeqrep()
1628 return seqrep != null;
1632 public int getEndRes()
1634 return getWidth() - 1;
1638 public int getStartRes()
1644 * In the case of AlignmentI - returns the dataset for the alignment, if set
1647 * @see jalview.datamodel.AnnotatedCollectionI#getContext()
1650 public AnnotatedCollectionI getContext()
1656 * Align this alignment like the given (mapped) one.
1659 public int alignAs(AlignmentI al)
1662 * Currently retains unmapped gaps (in introns), regaps mapped regions
1665 return alignAs(al, false, true);
1669 * Align this alignment 'the same as' the given one. Mapped sequences only are
1670 * realigned. If both of the same type (nucleotide/protein) then align both
1671 * identically. If this is nucleotide and the other is protein, make 3 gaps
1672 * for each gap in the protein sequences. If this is protein and the other is
1673 * nucleotide, insert a gap for each 3 gaps (or part thereof) between
1674 * nucleotide bases. If this is protein and the other is nucleotide, gaps
1675 * protein to match the relative ordering of codons in the nucleotide.
1677 * Parameters control whether gaps in exon (mapped) and intron (unmapped)
1678 * regions are preserved. Gaps that connect introns to exons are treated
1679 * conservatively, i.e. only preserved if both intron and exon gaps are
1683 * @param preserveMappedGaps
1684 * if true, gaps within and between mapped codons are preserved
1685 * @param preserveUnmappedGaps
1686 * if true, gaps within and between unmapped codons are preserved
1689 public int alignAs(AlignmentI al, boolean preserveMappedGaps,
1690 boolean preserveUnmappedGaps)
1692 // TODO should this method signature be the one in the interface?
1694 boolean thisIsNucleotide = this.isNucleotide();
1695 boolean thatIsProtein = !al.isNucleotide();
1696 if (!thatIsProtein && !thisIsNucleotide)
1698 return AlignmentUtils.alignProteinAsDna(this, al);
1701 char thisGapChar = this.getGapCharacter();
1702 String gap = thisIsNucleotide && thatIsProtein ? String
1704 { thisGapChar, thisGapChar, thisGapChar }) : String
1705 .valueOf(thisGapChar);
1707 // TODO handle intron regions? Needs a 'holistic' alignment of dna,
1708 // not just sequence by sequence. But how to 'gap' intron regions?
1711 * Get mappings from 'that' alignment's sequences to this.
1713 for (SequenceI alignTo : getSequences())
1715 count += AlignmentUtils.alignSequenceAs(alignTo, al, gap, preserveMappedGaps,
1716 preserveUnmappedGaps) ? 1 : 0;
1722 * Returns the set of distinct sequence names. No ordering is guaranteed.
1725 public Set<String> getSequenceNames()
1727 Set<String> names = new HashSet<String>();
1728 for (SequenceI seq : getSequences())
1730 names.add(seq.getName());
1736 * Returns the alignment in Fasta format. Behaviour of this method is not
1737 * guaranteed between versions.
1740 public String toString()
1742 SequenceI[] seq = this.getSequencesArray();
1746 * return "" + JSON.stringify(seq);
1749 return ((FastaFile) GenericFileAdapter.getFile("FastaFile")).print(seq);