2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 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.MessageManager;
27 import java.util.ArrayList;
28 import java.util.Enumeration;
29 import java.util.HashSet;
30 import java.util.Hashtable;
31 import java.util.LinkedHashSet;
32 import java.util.List;
35 import java.util.Vector;
38 * Data structure to hold and manipulate a multiple sequence alignment
44 public class Alignment implements AlignmentI
46 protected Alignment dataset;
48 protected List<SequenceI> sequences;
50 protected List<SequenceGroup> groups = java.util.Collections
51 .synchronizedList(new ArrayList<SequenceGroup>());
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;
64 public AlignmentAnnotation[] annotations;
66 HiddenSequences hiddenSequences = new HiddenSequences(this);
68 public Hashtable alignmentProperties;
70 private void initAlignment(SequenceI[] seqs)
74 if (jalview.util.Comparison.isNucleotide(seqs))
83 sequences = java.util.Collections
84 .synchronizedList(new ArrayList<SequenceI>());
86 for (i = 0; i < seqs.length; i++)
88 sequences.add(seqs[i]);
94 * Make a 'copy' alignment - sequences have new copies of features and
95 * annotations, but share the original dataset sequences.
97 public Alignment(AlignmentI al)
99 SequenceI[] seqs = al.getSequencesArray();
100 for (int i = 0; i < seqs.length; i++)
102 seqs[i] = new Sequence(seqs[i]);
108 * Make an alignment from an array of Sequences.
112 public Alignment(SequenceI[] seqs)
118 * Make a new alignment from an array of SeqCigars
123 public Alignment(SeqCigar[] alseqs)
125 SequenceI[] seqs = SeqCigar.createAlignmentSequences(alseqs,
126 gapCharacter, new ColumnSelection(), null);
131 * Make a new alignment from an CigarArray JBPNote - can only do this when
132 * compactAlignment does not contain hidden regions. JBPNote - must also check
133 * that compactAlignment resolves to a set of SeqCigars - or construct them
136 * @param compactAlignment
139 public static AlignmentI createAlignment(CigarArray compactAlignment)
141 throw new Error(MessageManager.getString("error.alignment_cigararray_not_implemented"));
142 // this(compactAlignment.refCigars);
148 * @return DOCUMENT ME!
151 public List<SequenceI> getSequences()
157 public List<SequenceI> getSequences(
158 Map<SequenceI, SequenceCollectionI> hiddenReps)
160 // TODO: in jalview 2.8 we don't do anything with hiddenreps - fix design to
166 public SequenceI[] getSequencesArray()
168 if (sequences == null)
172 synchronized (sequences)
174 return sequences.toArray(new SequenceI[sequences.size()]);
179 * Returns a map of lists of sequences keyed by sequence name.
184 public Map<String, List<SequenceI>> getSequencesByName()
186 return AlignmentUtils.getSequencesByName(this);
195 * @return DOCUMENT ME!
198 public SequenceI getSequenceAt(int i)
200 synchronized (sequences)
202 if (i > -1 && i < sequences.size())
204 return sequences.get(i);
211 * Adds a sequence to the alignment. Recalculates maxLength and size.
216 public void addSequence(SequenceI snew)
220 // maintain dataset integrity
221 if (snew.getDatasetSequence() != null)
223 getDataset().addSequence(snew.getDatasetSequence());
227 // derive new sequence
228 SequenceI adding = snew.deriveSequence();
229 getDataset().addSequence(adding.getDatasetSequence());
233 if (sequences == null)
235 initAlignment(new SequenceI[]
240 synchronized (sequences)
245 if (hiddenSequences != null)
247 hiddenSequences.adjustHeightSequenceAdded();
252 * Adds a sequence to the alignment. Recalculates maxLength and size.
257 public void setSequenceAt(int i, SequenceI snew)
259 synchronized (sequences)
262 sequences.set(i, snew);
269 * @return DOCUMENT ME!
272 public List<SequenceGroup> getGroups()
278 public void finalize()
280 if (getDataset() != null)
282 getDataset().removeAlignmentRef();
289 hiddenSequences = null;
293 * decrement the alignmentRefs counter by one and call finalize if it goes to
296 private void removeAlignmentRef()
298 if (--alignmentRefs == 0)
311 public void deleteSequence(SequenceI s)
313 deleteSequence(findIndex(s));
323 public void deleteSequence(int i)
325 if (i > -1 && i < getHeight())
327 synchronized (sequences)
330 hiddenSequences.adjustHeightSequenceDeleted(i);
338 * @see jalview.datamodel.AlignmentI#findGroup(jalview.datamodel.SequenceI)
341 public SequenceGroup findGroup(SequenceI s)
343 synchronized (groups)
345 for (int i = 0; i < this.groups.size(); i++)
347 SequenceGroup sg = groups.get(i);
349 if (sg.getSequences(null).contains(s))
362 * jalview.datamodel.AlignmentI#findAllGroups(jalview.datamodel.SequenceI)
365 public SequenceGroup[] findAllGroups(SequenceI s)
367 ArrayList<SequenceGroup> temp = new ArrayList<SequenceGroup>();
369 synchronized (groups)
371 int gSize = groups.size();
372 for (int i = 0; i < gSize; i++)
374 SequenceGroup sg = groups.get(i);
375 if (sg == null || sg.getSequences(null) == null)
377 this.deleteGroup(sg);
382 if (sg.getSequences(null).contains(s))
388 SequenceGroup[] ret = new SequenceGroup[temp.size()];
389 return temp.toArray(ret);
394 public void addGroup(SequenceGroup sg)
396 synchronized (groups)
398 if (!groups.contains(sg))
400 if (hiddenSequences.getSize() > 0)
402 int i, iSize = sg.getSize();
403 for (i = 0; i < iSize; i++)
405 if (!sequences.contains(sg.getSequenceAt(i)))
407 sg.deleteSequence(sg.getSequenceAt(i), false);
413 if (sg.getSize() < 1)
425 * remove any annotation that references gp
428 * (if null, removes all group associated annotation)
430 private void removeAnnotationForGroup(SequenceGroup gp)
432 if (annotations == null || annotations.length == 0)
436 // remove annotation very quickly
437 AlignmentAnnotation[] t, todelete = new AlignmentAnnotation[annotations.length], tokeep = new AlignmentAnnotation[annotations.length];
441 for (i = 0, p = 0, k = 0; i < annotations.length; i++)
443 if (annotations[i].groupRef != null)
445 todelete[p++] = annotations[i];
449 tokeep[k++] = annotations[i];
455 for (i = 0, p = 0, k = 0; i < annotations.length; i++)
457 if (annotations[i].groupRef == gp)
459 todelete[p++] = annotations[i];
463 tokeep[k++] = annotations[i];
469 // clear out the group associated annotation.
470 for (i = 0; i < p; i++)
472 unhookAnnotation(todelete[i]);
475 t = new AlignmentAnnotation[k];
476 for (i = 0; i < k; i++)
485 public void deleteAllGroups()
487 synchronized (groups)
489 if (annotations != null)
491 removeAnnotationForGroup(null);
493 for (SequenceGroup sg : groups)
503 public void deleteGroup(SequenceGroup g)
505 synchronized (groups)
507 if (groups.contains(g))
509 removeAnnotationForGroup(g);
518 public SequenceI findName(String name)
520 return findName(name, false);
526 * @see jalview.datamodel.AlignmentI#findName(java.lang.String, boolean)
529 public SequenceI findName(String token, boolean b)
531 return findName(null, token, b);
537 * @see jalview.datamodel.AlignmentI#findName(SequenceI, java.lang.String,
541 public SequenceI findName(SequenceI startAfter, String token, boolean b)
546 String sqname = null;
547 if (startAfter != null)
549 // try to find the sequence in the alignment
550 boolean matched = false;
551 while (i < sequences.size())
553 if (getSequenceAt(i++) == startAfter)
564 while (i < sequences.size())
566 sq = getSequenceAt(i);
567 sqname = sq.getName();
568 if (sqname.equals(token) // exact match
569 || (b && // allow imperfect matches - case varies
570 (sqname.equalsIgnoreCase(token))))
572 return getSequenceAt(i);
582 public SequenceI[] findSequenceMatch(String name)
584 Vector matches = new Vector();
587 while (i < sequences.size())
589 if (getSequenceAt(i).getName().equals(name))
591 matches.addElement(getSequenceAt(i));
596 SequenceI[] result = new SequenceI[matches.size()];
597 for (i = 0; i < result.length; i++)
599 result[i] = (SequenceI) matches.elementAt(i);
609 * @see jalview.datamodel.AlignmentI#findIndex(jalview.datamodel.SequenceI)
612 public int findIndex(SequenceI s)
616 while (i < sequences.size())
618 if (s == getSequenceAt(i))
633 * jalview.datamodel.AlignmentI#findIndex(jalview.datamodel.SearchResults)
636 public int findIndex(SearchResults results)
640 while (i < sequences.size())
642 if (results.involvesSequence(getSequenceAt(i)))
654 * @return DOCUMENT ME!
657 public int getHeight()
659 return sequences.size();
665 * @return DOCUMENT ME!
668 public int getWidth()
672 for (int i = 0; i < sequences.size(); i++)
674 if (getSequenceAt(i).getLength() > maxLength)
676 maxLength = getSequenceAt(i).getLength();
690 public void setGapCharacter(char gc)
693 synchronized (sequences)
695 for (SequenceI seq : sequences)
697 seq.setSequence(seq.getSequenceAsString().replace('.', gc)
698 .replace('-', gc).replace(' ', gc));
706 * @return DOCUMENT ME!
709 public char getGapCharacter()
717 * @see jalview.datamodel.AlignmentI#isAligned()
720 public boolean isAligned()
722 return isAligned(false);
728 * @see jalview.datamodel.AlignmentI#isAligned(boolean)
731 public boolean isAligned(boolean includeHidden)
733 int width = getWidth();
734 if (hiddenSequences == null || hiddenSequences.getSize() == 0)
736 includeHidden = true; // no hidden sequences to check against.
738 for (int i = 0; i < sequences.size(); i++)
740 if (includeHidden || !hiddenSequences.isHidden(getSequenceAt(i)))
742 if (getSequenceAt(i).getLength() != width)
755 * @seejalview.datamodel.AlignmentI#deleteAnnotation(jalview.datamodel.
756 * AlignmentAnnotation)
759 public boolean deleteAnnotation(AlignmentAnnotation aa)
761 return deleteAnnotation(aa, true);
765 public boolean deleteAnnotation(AlignmentAnnotation aa, boolean unhook)
769 if (annotations != null)
771 aSize = annotations.length;
779 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize - 1];
781 boolean swap = false;
784 for (int i = 0; i < aSize; i++)
786 if (annotations[i] == aa)
791 if (tIndex < temp.length)
793 temp[tIndex++] = annotations[i];
802 unhookAnnotation(aa);
809 * remove any object references associated with this annotation
813 private void unhookAnnotation(AlignmentAnnotation aa)
815 if (aa.sequenceRef != null)
817 aa.sequenceRef.removeAlignmentAnnotation(aa);
819 if (aa.groupRef != null)
821 // probably need to do more here in the future (post 2.5.0)
829 * @seejalview.datamodel.AlignmentI#addAnnotation(jalview.datamodel.
830 * AlignmentAnnotation)
833 public void addAnnotation(AlignmentAnnotation aa)
835 addAnnotation(aa, -1);
841 * @seejalview.datamodel.AlignmentI#addAnnotation(jalview.datamodel.
842 * AlignmentAnnotation, int)
845 public void addAnnotation(AlignmentAnnotation aa, int pos)
847 if (aa.getRNAStruc() != null)
849 hasRNAStructure = true;
853 if (annotations != null)
855 aSize = annotations.length + 1;
858 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
860 if (pos == -1 || pos >= aSize)
862 temp[aSize - 1] = aa;
871 for (i = 0; i < (aSize - 1); i++, p++)
879 temp[p] = annotations[i];
888 public void setAnnotationIndex(AlignmentAnnotation aa, int index)
890 if (aa == null || annotations == null || annotations.length - 1 < index)
895 int aSize = annotations.length;
896 AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize];
900 for (int i = 0; i < aSize; i++)
909 temp[i] = annotations[i];
913 temp[i] = annotations[i - 1];
922 * returns all annotation on the alignment
924 public AlignmentAnnotation[] getAlignmentAnnotation()
930 public void setNucleotide(boolean b)
943 public boolean isNucleotide()
945 if (type == NUCLEOTIDE)
956 public boolean hasRNAStructure()
958 // TODO can it happen that structure is removed from alignment?
959 return hasRNAStructure;
963 public void setDataset(Alignment data)
965 if (dataset == null && data == null)
967 // Create a new dataset for this alignment.
968 // Can only be done once, if dataset is not null
969 // This will not be performed
970 SequenceI[] seqs = new SequenceI[getHeight()];
971 SequenceI currentSeq;
972 for (int i = 0; i < getHeight(); i++)
974 currentSeq = getSequenceAt(i);
975 if (currentSeq.getDatasetSequence() != null)
977 seqs[i] = currentSeq.getDatasetSequence();
981 seqs[i] = currentSeq.createDatasetSequence();
985 dataset = new Alignment(seqs);
987 else if (dataset == null && data != null)
990 for (int i = 0; i < getHeight(); i++)
992 SequenceI currentSeq = getSequenceAt(i);
993 SequenceI dsq = currentSeq.getDatasetSequence();
996 dsq = currentSeq.createDatasetSequence();
997 dataset.addSequence(dsq);
1001 while (dsq.getDatasetSequence() != null)
1003 dsq = dsq.getDatasetSequence();
1005 if (dataset.findIndex(dsq) == -1)
1007 dataset.addSequence(dsq);
1012 dataset.addAlignmentRef();
1016 * reference count for number of alignments referencing this one.
1018 int alignmentRefs = 0;
1021 * increase reference count to this alignment.
1023 private void addAlignmentRef()
1029 public Alignment getDataset()
1035 public boolean padGaps()
1037 boolean modified = false;
1039 // Remove excess gaps from the end of alignment
1043 for (int i = 0; i < sequences.size(); i++)
1045 current = getSequenceAt(i);
1046 for (int j = current.getLength(); j > maxLength; j--)
1049 && !jalview.util.Comparison.isGap(current.getCharAt(j)))
1060 for (int i = 0; i < sequences.size(); i++)
1062 current = getSequenceAt(i);
1063 cLength = current.getLength();
1065 if (cLength < maxLength)
1067 current.insertCharAt(cLength, maxLength - cLength, gapCharacter);
1070 else if (current.getLength() > maxLength)
1072 current.deleteChars(maxLength, current.getLength());
1079 * Justify the sequences to the left or right by deleting and inserting gaps
1080 * before the initial residue or after the terminal residue
1083 * true if alignment padded to right, false to justify to left
1084 * @return true if alignment was changed
1087 public boolean justify(boolean right)
1089 boolean modified = false;
1091 // Remove excess gaps from the end of alignment
1093 int ends[] = new int[sequences.size() * 2];
1095 for (int i = 0; i < sequences.size(); i++)
1097 current = getSequenceAt(i);
1098 // This should really be a sequence method
1099 ends[i * 2] = current.findIndex(current.getStart());
1100 ends[i * 2 + 1] = current.findIndex(current.getStart()
1101 + current.getLength());
1102 boolean hitres = false;
1103 for (int j = 0, rs = 0, ssiz = current.getLength(); j < ssiz; j++)
1105 if (!jalview.util.Comparison.isGap(current.getCharAt(j)))
1114 ends[i * 2 + 1] = j;
1115 if (j - ends[i * 2] > maxLength)
1117 maxLength = j - ends[i * 2];
1125 // now edit the flanking gaps to justify to either left or right
1126 int cLength, extent, diff;
1127 for (int i = 0; i < sequences.size(); i++)
1129 current = getSequenceAt(i);
1131 cLength = 1 + ends[i * 2 + 1] - ends[i * 2];
1132 diff = maxLength - cLength; // number of gaps to indent
1133 extent = current.getLength();
1137 if (extent > ends[i * 2 + 1])
1139 current.deleteChars(ends[i * 2 + 1] + 1, extent);
1142 if (ends[i * 2] > diff)
1144 current.deleteChars(0, ends[i * 2] - diff);
1149 if (ends[i * 2] < diff)
1151 current.insertCharAt(0, diff - ends[i * 2], gapCharacter);
1159 if (ends[i * 2] > 0)
1161 current.deleteChars(0, ends[i * 2]);
1163 ends[i * 2 + 1] -= ends[i * 2];
1164 extent -= ends[i * 2];
1166 if (extent > maxLength)
1168 current.deleteChars(maxLength + 1, extent);
1173 if (extent < maxLength)
1175 current.insertCharAt(extent, maxLength - extent, gapCharacter);
1185 public HiddenSequences getHiddenSequences()
1187 return hiddenSequences;
1191 public CigarArray getCompactAlignment()
1193 synchronized (sequences)
1195 SeqCigar alseqs[] = new SeqCigar[sequences.size()];
1197 for (SequenceI seq : sequences)
1199 alseqs[i++] = new SeqCigar(seq);
1201 CigarArray cal = new CigarArray(alseqs);
1202 cal.addOperation(CigarArray.M, getWidth());
1208 public void setProperty(Object key, Object value)
1210 if (alignmentProperties == null)
1212 alignmentProperties = new Hashtable();
1215 alignmentProperties.put(key, value);
1219 public Object getProperty(Object key)
1221 if (alignmentProperties != null)
1223 return alignmentProperties.get(key);
1232 public Hashtable getProperties()
1234 return alignmentProperties;
1237 // AlignedCodonFrame[] codonFrameList = null;
1239 LinkedHashSet<AlignedCodonFrame> codonFrameList = new LinkedHashSet<AlignedCodonFrame>();
1245 * jalview.datamodel.AlignmentI#addCodonFrame(jalview.datamodel.AlignedCodonFrame
1249 public void addCodonFrame(AlignedCodonFrame codons)
1253 codonFrameList.add(codons);
1261 * jalview.datamodel.AlignmentI#getCodonFrame(jalview.datamodel.SequenceI)
1264 public List<AlignedCodonFrame> getCodonFrame(SequenceI seq)
1270 List<AlignedCodonFrame> cframes = new ArrayList<AlignedCodonFrame>();
1271 for (AlignedCodonFrame acf : codonFrameList)
1273 if (acf.involvesSequence(seq))
1284 * @see jalview.datamodel.AlignmentI#getCodonFrames()
1287 public Set<AlignedCodonFrame> getCodonFrames()
1289 return codonFrameList;
1295 * @seejalview.datamodel.AlignmentI#removeCodonFrame(jalview.datamodel.
1296 * AlignedCodonFrame)
1299 public boolean removeCodonFrame(AlignedCodonFrame codons)
1301 if (codons == null || codonFrameList == null)
1305 return codonFrameList.remove(codons);
1309 public void append(AlignmentI toappend)
1311 if (toappend == this)
1313 System.err.println("Self append may cause a deadlock.");
1315 // TODO test this method for a future 2.5 release
1316 // currently tested for use in jalview.gui.SequenceFetcher
1317 boolean samegap = toappend.getGapCharacter() == getGapCharacter();
1318 char oldc = toappend.getGapCharacter();
1319 boolean hashidden = toappend.getHiddenSequences() != null
1320 && toappend.getHiddenSequences().hiddenSequences != null;
1321 // get all sequences including any hidden ones
1322 List<SequenceI> sqs = (hashidden) ? toappend.getHiddenSequences()
1323 .getFullAlignment().getSequences() : toappend.getSequences();
1328 for (SequenceI addedsq : sqs)
1332 char[] oldseq = addedsq.getSequence();
1333 for (int c = 0; c < oldseq.length; c++)
1335 if (oldseq[c] == oldc)
1337 oldseq[c] = gapCharacter;
1341 addSequence(addedsq);
1345 AlignmentAnnotation[] alan = toappend.getAlignmentAnnotation();
1346 for (int a = 0; alan != null && a < alan.length; a++)
1348 addAnnotation(alan[a]);
1351 this.codonFrameList.addAll(toappend.getCodonFrames());
1353 List<SequenceGroup> sg = toappend.getGroups();
1356 for (SequenceGroup _sg : sg)
1361 if (toappend.getHiddenSequences() != null)
1363 HiddenSequences hs = toappend.getHiddenSequences();
1364 if (hiddenSequences == null)
1366 hiddenSequences = new HiddenSequences(this);
1368 if (hs.hiddenSequences != null)
1370 for (int s = 0; s < hs.hiddenSequences.length; s++)
1372 // hide the newly appended sequence in the alignment
1373 if (hs.hiddenSequences[s] != null)
1375 hiddenSequences.hideSequence(hs.hiddenSequences[s]);
1380 if (toappend.getProperties() != null)
1382 // we really can't do very much here - just try to concatenate strings
1383 // where property collisions occur.
1384 Enumeration key = toappend.getProperties().keys();
1385 while (key.hasMoreElements())
1387 Object k = key.nextElement();
1388 Object ourval = this.getProperty(k);
1389 Object toapprop = toappend.getProperty(k);
1392 if (ourval.getClass().equals(toapprop.getClass())
1393 && !ourval.equals(toapprop))
1395 if (ourval instanceof String)
1398 this.setProperty(k, ((String) ourval) + "; "
1399 + ((String) toapprop));
1403 if (ourval instanceof Vector)
1406 Enumeration theirv = ((Vector) toapprop).elements();
1407 while (theirv.hasMoreElements())
1409 ((Vector) ourval).addElement(theirv);
1417 // just add new property directly
1418 setProperty(k, toapprop);
1426 public AlignmentAnnotation findOrCreateAnnotation(String name,
1427 String calcId, boolean autoCalc, SequenceI seqRef,
1428 SequenceGroup groupRef)
1430 assert (name != null);
1431 if (annotations != null)
1433 for (AlignmentAnnotation annot : getAlignmentAnnotation())
1435 if (annot.autoCalculated == autoCalc && (name.equals(annot.label))
1436 && (calcId == null || annot.getCalcId().equals(calcId))
1437 && annot.sequenceRef == seqRef
1438 && annot.groupRef == groupRef)
1444 AlignmentAnnotation annot = new AlignmentAnnotation(name, name,
1445 new Annotation[1], 0f, 0f, AlignmentAnnotation.BAR_GRAPH);
1446 annot.hasText = false;
1447 annot.setCalcId(new String(calcId));
1448 annot.autoCalculated = autoCalc;
1451 annot.setSequenceRef(seqRef);
1453 annot.groupRef = groupRef;
1454 addAnnotation(annot);
1460 public Iterable<AlignmentAnnotation> findAnnotation(String calcId)
1462 ArrayList<AlignmentAnnotation> aa = new ArrayList<AlignmentAnnotation>();
1463 for (AlignmentAnnotation a : getAlignmentAnnotation())
1465 if (a.getCalcId() == calcId
1466 || (a.getCalcId() != null && calcId != null && a.getCalcId()
1476 * Returns an iterable collection of any annotations that match on given
1477 * sequence ref, calcId and label (ignoring null values).
1480 public Iterable<AlignmentAnnotation> findAnnotations(SequenceI seq,
1481 String calcId, String label)
1483 ArrayList<AlignmentAnnotation> aa = new ArrayList<AlignmentAnnotation>();
1484 for (AlignmentAnnotation ann : getAlignmentAnnotation())
1486 if (ann.getCalcId() != null && ann.getCalcId().equals(calcId)
1487 && ann.sequenceRef != null && ann.sequenceRef == seq
1488 && ann.label != null && ann.label.equals(label))
1497 public void moveSelectedSequencesByOne(SequenceGroup sg,
1498 Map<SequenceI, SequenceCollectionI> map, boolean up)
1500 synchronized (sequences)
1505 for (int i = 1, iSize = sequences.size(); i < iSize; i++)
1507 SequenceI seq = sequences.get(i);
1508 if (!sg.getSequences(map).contains(seq))
1513 SequenceI temp = sequences.get(i - 1);
1514 if (sg.getSequences(null).contains(temp))
1519 sequences.set(i, temp);
1520 sequences.set(i - 1, seq);
1525 for (int i = sequences.size() - 2; i > -1; i--)
1527 SequenceI seq = sequences.get(i);
1528 if (!sg.getSequences(map).contains(seq))
1533 SequenceI temp = sequences.get(i + 1);
1534 if (sg.getSequences(map).contains(temp))
1539 sequences.set(i, temp);
1540 sequences.set(i + 1, seq);
1548 public void validateAnnotation(AlignmentAnnotation alignmentAnnotation)
1550 alignmentAnnotation.validateRangeAndDisplay();
1551 if (isNucleotide() && alignmentAnnotation.isValidStruc())
1553 hasRNAStructure = true;
1558 public int getEndRes()
1560 return getWidth() - 1;
1564 public int getStartRes()
1570 * In the case of AlignmentI - returns the dataset for the alignment, if set
1573 * @see jalview.datamodel.AnnotatedCollectionI#getContext()
1576 public AnnotatedCollectionI getContext()
1582 * Align this alignment like the given (mapped) one.
1585 public int alignAs(AlignmentI al)
1588 * Currently retains unmapped gaps (in introns), regaps mapped regions
1591 return alignAs(al, false, true);
1595 * Align this alignment 'the same as' the given one. Mapped sequences only are
1596 * realigned. If both of the same type (nucleotide/protein) then align both
1597 * identically. If this is nucleotide and the other is protein, make 3 gaps
1598 * for each gap in the protein sequences. If this is protein and the other is
1599 * nucleotide, insert a gap for each 3 gaps (or part thereof) between
1600 * nucleotide bases. Does nothing if alignment of protein from cDNA is
1601 * requested (not yet implemented).
1603 * Parameters control whether gaps in exon (mapped) and intron (unmapped)
1604 * regions are preserved. Gaps that connect introns to exons are treated
1605 * conservatively, i.e. only preserved if both intron and exon gaps are
1609 * @param preserveMappedGaps
1610 * if true, gaps within and between mapped codons are preserved
1611 * @param preserveUnmappedGaps
1612 * if true, gaps within and between unmapped codons are preserved
1615 public int alignAs(AlignmentI al, boolean preserveMappedGaps,
1616 boolean preserveUnmappedGaps)
1618 // TODO should this method signature be the one in the interface?
1620 boolean thisIsNucleotide = this.isNucleotide();
1621 boolean thatIsProtein = !al.isNucleotide();
1622 if (!thatIsProtein && !thisIsNucleotide)
1625 .println("Alignment of protein from cDNA not yet implemented");
1627 // todo: build it - a variant of Dna.CdnaTranslate()
1630 char thisGapChar = this.getGapCharacter();
1631 String gap = thisIsNucleotide && thatIsProtein ? String
1633 { thisGapChar, thisGapChar, thisGapChar }) : String
1634 .valueOf(thisGapChar);
1637 * Get mappings from 'that' alignment's sequences to this.
1639 for (SequenceI alignTo : getSequences())
1641 count += AlignmentUtils.alignSequenceAs(alignTo, al, gap, preserveMappedGaps,
1642 preserveUnmappedGaps) ? 1 : 0;
1648 * Returns the alignment in Fasta format. Behaviour of this method is not
1649 * guaranteed between versions.
1652 public String toString()
1654 return new FastaFile().print(getSequencesArray());
1658 * Returns the set of distinct sequence names. No ordering is guaranteed.
1661 public Set<String> getSequenceNames()
1663 Set<String> names = new HashSet<String>();
1664 for (SequenceI seq : getSequences())
1666 names.add(seq.getName());