+import jalview.analysis.AlignmentUtils;
+import jalview.datamodel.AlignedCodonFrame.SequenceToSequenceMapping;
+import jalview.io.FastaFile;
+import jalview.util.Comparison;
+import jalview.util.LinkedIdentityHashSet;
+import jalview.util.MessageManager;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.HashSet;
+import java.util.Hashtable;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.Vector;
+
+/**
+ * Data structure to hold and manipulate a multiple sequence alignment
+ */
+/**
+ * @author JimP
+ *
+ */
+public class Alignment implements AlignmentI
+{
+ private Alignment dataset;
+
+ protected List<SequenceI> sequences;
+
+ protected List<SequenceGroup> groups;
+
+ protected char gapCharacter = '-';
+
+ private boolean nucleotide = true;
+
+ public boolean hasRNAStructure = false;
+
+ public AlignmentAnnotation[] annotations;
+
+ HiddenSequences hiddenSequences;
+
+ public Hashtable alignmentProperties;
+
+ private List<AlignedCodonFrame> codonFrameList;
+
+ private void initAlignment(SequenceI[] seqs)
+ {
+ groups = Collections.synchronizedList(new ArrayList<SequenceGroup>());
+ hiddenSequences = new HiddenSequences(this);
+ codonFrameList = new ArrayList<AlignedCodonFrame>();
+
+ nucleotide = Comparison.isNucleotide(seqs);
+
+ sequences = Collections.synchronizedList(new ArrayList<SequenceI>());
+
+ for (int i = 0; i < seqs.length; i++)
+ {
+ sequences.add(seqs[i]);
+ }
+
+ }
+
+ /**
+ * Make a 'copy' alignment - sequences have new copies of features and
+ * annotations, but share the original dataset sequences.
+ */
+ public Alignment(AlignmentI al)
+ {
+ SequenceI[] seqs = al.getSequencesArray();
+ for (int i = 0; i < seqs.length; i++)
+ {
+ seqs[i] = new Sequence(seqs[i]);
+ }
+
+ initAlignment(seqs);
+
+ /*
+ * Share the same dataset sequence mappings (if any).
+ */
+ if (dataset == null && al.getDataset() == null)
+ {
+ this.setCodonFrames(al.getCodonFrames());
+ }
+ }
+
+ /**
+ * Make an alignment from an array of Sequences.
+ *
+ * @param sequences
+ */
+ public Alignment(SequenceI[] seqs)
+ {
+ initAlignment(seqs);
+ }
+
+ /**
+ * Make a new alignment from an array of SeqCigars
+ *
+ * @param seqs
+ * SeqCigar[]
+ */
+ public Alignment(SeqCigar[] alseqs)
+ {
+ SequenceI[] seqs = SeqCigar.createAlignmentSequences(alseqs,
+ gapCharacter, new ColumnSelection(), null);
+ initAlignment(seqs);
+ }
+
+ /**
+ * Make a new alignment from an CigarArray JBPNote - can only do this when
+ * compactAlignment does not contain hidden regions. JBPNote - must also check
+ * that compactAlignment resolves to a set of SeqCigars - or construct them
+ * appropriately.
+ *
+ * @param compactAlignment
+ * CigarArray
+ */
+ public static AlignmentI createAlignment(CigarArray compactAlignment)
+ {
+ throw new Error(
+ MessageManager
+ .getString("error.alignment_cigararray_not_implemented"));
+ // this(compactAlignment.refCigars);
+ }
+
+ @Override
+ public List<SequenceI> getSequences()
+ {
+ return sequences;
+ }
+
+ @Override
+ public List<SequenceI> getSequences(
+ Map<SequenceI, SequenceCollectionI> hiddenReps)
+ {
+ // TODO: in jalview 2.8 we don't do anything with hiddenreps - fix design to
+ // work on this.
+ return sequences;
+ }
+
+ @Override
+ public SequenceI[] getSequencesArray()
+ {
+ if (sequences == null)
+ {
+ return null;
+ }
+ synchronized (sequences)
+ {
+ return sequences.toArray(new SequenceI[sequences.size()]);
+ }
+ }
+
+ /**
+ * Returns a map of lists of sequences keyed by sequence name.
+ *
+ * @return
+ */
+ @Override
+ public Map<String, List<SequenceI>> getSequencesByName()
+ {
+ return AlignmentUtils.getSequencesByName(this);
+ }
+
+
+ @Override
+ public SequenceI getSequenceAt(int i)
+ {
+ synchronized (sequences)
+ {
+ if (i > -1 && i < sequences.size())
+ {
+ return sequences.get(i);
+ }
+ }
+ return null;
+ }
+
+ @Override
+ public SequenceI getSequenceAtAbsoluteIndex(int i)
+ {
+ SequenceI seq = null;
+ if (getHiddenSequences().getSize() > 0)
+ {
+ seq = getHiddenSequences().getHiddenSequence(i);
+ if (seq == null)
+ {
+ // didn't find the sequence in the hidden sequences, get it from the
+ // alignment
+ int index = getHiddenSequences().findIndexWithoutHiddenSeqs(i);
+ seq = getSequenceAt(index);
+ }
+ }
+ else
+ {
+ seq = getSequenceAt(i);
+ }
+ return seq;
+ }
+
+ /**
+ * Adds a sequence to the alignment. Recalculates maxLength and size. Note
+ * this currently does not recalculate whether or not the alignment is
+ * nucleotide, so mixed alignments may have undefined behaviour.
+ *
+ * @param snew
+ */
+ @Override
+ public void addSequence(SequenceI snew)
+ {
+ if (dataset != null)
+ {
+
+ // maintain dataset integrity
+ SequenceI dsseq = snew.getDatasetSequence();
+ if (dsseq == null)
+ {
+ // derive new sequence
+ SequenceI adding = snew.deriveSequence();
+ snew = adding;
+ dsseq = snew.getDatasetSequence();
+ }
+ if (getDataset().findIndex(dsseq) == -1)
+ {
+ getDataset().addSequence(dsseq);
+ }
+
+ }
+ if (sequences == null)
+ {
+ initAlignment(new SequenceI[] { snew });
+ }
+ else
+ {
+ synchronized (sequences)
+ {
+ sequences.add(snew);
+ }
+ }
+ if (hiddenSequences != null)
+ {
+ hiddenSequences.adjustHeightSequenceAdded();
+ }
+ }
+
+ @Override
+ public SequenceI replaceSequenceAt(int i, SequenceI snew)
+ {
+ synchronized (sequences)
+ {
+ if (sequences.size() > i)
+ {
+ return sequences.set(i, snew);
+
+ }
+ else
+ {
+ sequences.add(snew);
+ hiddenSequences.adjustHeightSequenceAdded();
+ }
+ return null;
+ }
+ }
+
+ /**
+ * DOCUMENT ME!
+ *
+ * @return DOCUMENT ME!
+ */
+ @Override
+ public List<SequenceGroup> getGroups()
+ {
+ return groups;
+ }
+
+ @Override
+ public void finalize() throws Throwable
+ {
+ if (getDataset() != null)
+ {
+ getDataset().removeAlignmentRef();
+ }
+
+ nullReferences();
+ super.finalize();
+ }
+
+ /**
+ * Defensively nulls out references in case this object is not garbage
+ * collected
+ */
+ void nullReferences()
+ {
+ dataset = null;
+ sequences = null;
+ groups = null;
+ annotations = null;
+ hiddenSequences = null;
+ }
+
+ /**
+ * decrement the alignmentRefs counter by one and null references if it goes
+ * to zero.
+ *
+ * @throws Throwable
+ */
+ private void removeAlignmentRef() throws Throwable
+ {
+ if (--alignmentRefs == 0)
+ {
+ nullReferences();
+ }
+ }
+
+ @Override
+ public void deleteSequence(SequenceI s)
+ {
+ synchronized (sequences)
+ {
+ deleteSequence(findIndex(s));
+ }
+ }
+
+ @Override
+ public void deleteSequence(int i)
+ {
+ synchronized (sequences)
+ {
+ if (i > -1 && i < getHeight())
+ {
+ sequences.remove(i);
+ hiddenSequences.adjustHeightSequenceDeleted(i);
+ }
+ }
+ }
+
+ @Override
+ public void deleteHiddenSequence(int i)
+ {
+ synchronized (sequences)
+ {
+ if (i > -1 && i < getHeight())
+ {
+ sequences.remove(i);
+ }
+ }
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see jalview.datamodel.AlignmentI#findGroup(jalview.datamodel.SequenceI)
+ */
+ @Override
+ public SequenceGroup findGroup(SequenceI seq, int position)
+ {
+ synchronized (groups)
+ {
+ for (SequenceGroup sg : groups)
+ {
+ if (sg.getSequences(null).contains(seq))
+ {
+ if (position >= sg.getStartRes() && position <= sg.getEndRes())
+ {
+ return sg;
+ }
+ }
+ }
+ }
+ return null;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * jalview.datamodel.AlignmentI#findAllGroups(jalview.datamodel.SequenceI)
+ */
+ @Override
+ public SequenceGroup[] findAllGroups(SequenceI s)
+ {
+ ArrayList<SequenceGroup> temp = new ArrayList<SequenceGroup>();
+
+ synchronized (groups)
+ {
+ int gSize = groups.size();
+ for (int i = 0; i < gSize; i++)
+ {
+ SequenceGroup sg = groups.get(i);
+ if (sg == null || sg.getSequences() == null)
+ {
+ this.deleteGroup(sg);
+ gSize--;
+ continue;
+ }
+
+ if (sg.getSequences().contains(s))
+ {
+ temp.add(sg);
+ }
+ }
+ }
+ SequenceGroup[] ret = new SequenceGroup[temp.size()];
+ return temp.toArray(ret);
+ }
+
+ /** */
+ @Override
+ public void addGroup(SequenceGroup sg)
+ {
+ synchronized (groups)
+ {
+ if (!groups.contains(sg))
+ {
+ if (hiddenSequences.getSize() > 0)
+ {
+ int i, iSize = sg.getSize();
+ for (i = 0; i < iSize; i++)
+ {
+ if (!sequences.contains(sg.getSequenceAt(i)))
+ {
+ sg.deleteSequence(sg.getSequenceAt(i), false);
+ iSize--;
+ i--;
+ }
+ }
+
+ if (sg.getSize() < 1)
+ {
+ return;
+ }
+ }
+ sg.setContext(this);
+ groups.add(sg);
+ }
+ }
+ }
+
+ /**
+ * remove any annotation that references gp
+ *
+ * @param gp
+ * (if null, removes all group associated annotation)
+ */
+ private void removeAnnotationForGroup(SequenceGroup gp)
+ {
+ if (annotations == null || annotations.length == 0)
+ {
+ return;
+ }
+ // remove annotation very quickly
+ AlignmentAnnotation[] t, todelete = new AlignmentAnnotation[annotations.length], tokeep = new AlignmentAnnotation[annotations.length];
+ int i, p, k;
+ if (gp == null)
+ {
+ for (i = 0, p = 0, k = 0; i < annotations.length; i++)
+ {
+ if (annotations[i].groupRef != null)
+ {
+ todelete[p++] = annotations[i];
+ }
+ else
+ {
+ tokeep[k++] = annotations[i];
+ }
+ }
+ }
+ else
+ {
+ for (i = 0, p = 0, k = 0; i < annotations.length; i++)
+ {
+ if (annotations[i].groupRef == gp)
+ {
+ todelete[p++] = annotations[i];
+ }
+ else
+ {
+ tokeep[k++] = annotations[i];
+ }
+ }
+ }
+ if (p > 0)
+ {
+ // clear out the group associated annotation.
+ for (i = 0; i < p; i++)
+ {
+ unhookAnnotation(todelete[i]);
+ todelete[i] = null;
+ }
+ t = new AlignmentAnnotation[k];
+ for (i = 0; i < k; i++)
+ {
+ t[i] = tokeep[i];
+ }
+ annotations = t;
+ }
+ }
+
+ @Override
+ public void deleteAllGroups()
+ {
+ synchronized (groups)
+ {
+ if (annotations != null)
+ {
+ removeAnnotationForGroup(null);
+ }
+ for (SequenceGroup sg : groups)
+ {
+ sg.setContext(null);
+ }
+ groups.clear();
+ }
+ }
+
+ /** */
+ @Override
+ public void deleteGroup(SequenceGroup g)
+ {
+ synchronized (groups)
+ {
+ if (groups.contains(g))
+ {
+ removeAnnotationForGroup(g);
+ groups.remove(g);
+ g.setContext(null);
+ }
+ }
+ }
+
+ /** */
+ @Override
+ public SequenceI findName(String name)
+ {
+ return findName(name, false);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see jalview.datamodel.AlignmentI#findName(java.lang.String, boolean)
+ */
+ @Override
+ public SequenceI findName(String token, boolean b)
+ {
+ return findName(null, token, b);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see jalview.datamodel.AlignmentI#findName(SequenceI, java.lang.String,
+ * boolean)
+ */
+ @Override
+ public SequenceI findName(SequenceI startAfter, String token, boolean b)
+ {
+
+ int i = 0;
+ SequenceI sq = null;
+ String sqname = null;
+ if (startAfter != null)
+ {
+ // try to find the sequence in the alignment
+ boolean matched = false;
+ while (i < sequences.size())
+ {
+ if (getSequenceAt(i++) == startAfter)
+ {
+ matched = true;
+ break;
+ }
+ }
+ if (!matched)
+ {
+ i = 0;
+ }
+ }
+ while (i < sequences.size())
+ {
+ sq = getSequenceAt(i);
+ sqname = sq.getName();
+ if (sqname.equals(token) // exact match
+ || (b && // allow imperfect matches - case varies
+ (sqname.equalsIgnoreCase(token))))
+ {
+ return getSequenceAt(i);
+ }
+
+ i++;
+ }
+
+ return null;
+ }
+
+ @Override
+ public SequenceI[] findSequenceMatch(String name)
+ {
+ Vector matches = new Vector();
+ int i = 0;
+
+ while (i < sequences.size())
+ {
+ if (getSequenceAt(i).getName().equals(name))
+ {
+ matches.addElement(getSequenceAt(i));
+ }
+ i++;
+ }
+
+ SequenceI[] result = new SequenceI[matches.size()];
+ for (i = 0; i < result.length; i++)
+ {
+ result[i] = (SequenceI) matches.elementAt(i);
+ }
+
+ return result;
+
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see jalview.datamodel.AlignmentI#findIndex(jalview.datamodel.SequenceI)
+ */
+ @Override
+ public int findIndex(SequenceI s)
+ {
+ int i = 0;
+
+ while (i < sequences.size())
+ {
+ if (s == getSequenceAt(i))
+ {
+ return i;
+ }
+
+ i++;
+ }
+
+ return -1;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * jalview.datamodel.AlignmentI#findIndex(jalview.datamodel.SearchResults)
+ */
+ @Override
+ public int findIndex(SearchResultsI results)
+ {
+ int i = 0;
+
+ while (i < sequences.size())
+ {
+ if (results.involvesSequence(getSequenceAt(i)))
+ {
+ return i;
+ }
+ i++;
+ }
+ return -1;
+ }