package jalview.datamodel;
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;
{
if (dataset != null)
{
+
// maintain dataset integrity
- if (snew.getDatasetSequence() != null)
- {
- getDataset().addSequence(snew.getDatasetSequence());
- }
- else
+ SequenceI dsseq = snew.getDatasetSequence();
+ if (dsseq == null)
{
// derive new sequence
SequenceI adding = snew.deriveSequence();
- getDataset().addSequence(adding.getDatasetSequence());
snew = adding;
+ dsseq = snew.getDatasetSequence();
+ }
+ if (getDataset().findIndex(dsseq) == -1)
+ {
+ getDataset().addSequence(dsseq);
}
+
}
if (sequences == null)
{
}
}
- /**
- * Adds a sequence to the alignment. Recalculates maxLength and size.
- *
- * @param snew
- */
@Override
- public void setSequenceAt(int i, SequenceI snew)
+ public SequenceI replaceSequenceAt(int i, SequenceI snew)
{
synchronized (sequences)
{
- deleteSequence(i);
- sequences.set(i, snew);
+ if (sequences.size() > i)
+ {
+ return sequences.set(i, snew);
+
+ }
+ else
+ {
+ sequences.add(snew);
+ hiddenSequences.adjustHeightSequenceAdded();
+ }
+ return null;
}
}
}
@Override
- public void finalize()
+ 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;
}
/**
- * decrement the alignmentRefs counter by one and call finalize if it goes to
- * zero.
+ * decrement the alignmentRefs counter by one and null references if it goes
+ * to zero.
+ *
+ * @throws Throwable
*/
- private void removeAlignmentRef()
+ private void removeAlignmentRef() throws Throwable
{
if (--alignmentRefs == 0)
{
- finalize();
+ nullReferences();
}
}
* @see jalview.datamodel.AlignmentI#findGroup(jalview.datamodel.SequenceI)
*/
@Override
- public SequenceGroup findGroup(SequenceI s)
+ public SequenceGroup findGroup(SequenceI seq, int position)
{
synchronized (groups)
{
- for (int i = 0; i < this.groups.size(); i++)
+ for (SequenceGroup sg : groups)
{
- SequenceGroup sg = groups.get(i);
-
- if (sg.getSequences(null).contains(s))
+ if (sg.getSequences(null).contains(seq))
{
- return sg;
+ if (position >= sg.getStartRes() && position <= sg.getEndRes())
+ {
+ return sg;
+ }
}
}
}
* jalview.datamodel.AlignmentI#findIndex(jalview.datamodel.SearchResults)
*/
@Override
- public int findIndex(SearchResults results)
+ public int findIndex(SearchResultsI results)
{
int i = 0;
}
/**
+ * add dataset sequences to seq for currentSeq and any sequences it references
+ */
+ private void resolveAndAddDatasetSeq(SequenceI currentSeq,
+ Set<SequenceI> seqs, boolean createDatasetSequence)
+ {
+ SequenceI alignedSeq = currentSeq;
+ if (currentSeq.getDatasetSequence() != null)
+ {
+ currentSeq = currentSeq.getDatasetSequence();
+ }
+ else
+ {
+ if (createDatasetSequence)
+ {
+ currentSeq = currentSeq.createDatasetSequence();
+ }
+ }
+ if (seqs.contains(currentSeq))
+ {
+ return;
+ }
+ List<SequenceI> toProcess = new ArrayList<SequenceI>();
+ toProcess.add(currentSeq);
+ while (toProcess.size() > 0)
+ {
+ // use a queue ?
+ SequenceI curDs = toProcess.remove(0);
+ if (seqs.contains(curDs))
+ {
+ continue;
+ }
+ seqs.add(curDs);
+ // iterate over database references, making sure we add forward referenced
+ // sequences
+ if (curDs.getDBRefs() != null)
+ {
+ for (DBRefEntry dbr : curDs.getDBRefs())
+ {
+ if (dbr.getMap() != null && dbr.getMap().getTo() != null)
+ {
+ if (dbr.getMap().getTo() == alignedSeq)
+ {
+ /*
+ * update mapping to be to the newly created dataset sequence
+ */
+ dbr.getMap().setTo(currentSeq);
+ }
+ if (dbr.getMap().getTo().getDatasetSequence() != null)
+ {
+ throw new Error(
+ "Implementation error: Map.getTo() for dbref " + dbr
+ + " from " + curDs.getName()
+ + " is not a dataset sequence.");
+ }
+ // we recurse to add all forward references to dataset sequences via
+ // DBRefs/etc
+ toProcess.add(dbr.getMap().getTo());
+ }
+ }
+ }
+ }
+ }
+
+ /**
* Creates a new dataset for this alignment. Can only be done once - if
* dataset is not null this will not be performed.
*/
{
return;
}
- SequenceI[] seqs = new SequenceI[getHeight()];
- SequenceI currentSeq;
+ // try to avoid using SequenceI.equals at this stage, it will be expensive
+ Set<SequenceI> seqs = new LinkedIdentityHashSet<SequenceI>();
+
for (int i = 0; i < getHeight(); i++)
{
- currentSeq = getSequenceAt(i);
- if (currentSeq.getDatasetSequence() != null)
- {
- seqs[i] = currentSeq.getDatasetSequence();
- }
- else
+ SequenceI currentSeq = getSequenceAt(i);
+ resolveAndAddDatasetSeq(currentSeq, seqs, true);
+ }
+
+ // verify all mappings are in dataset
+ for (AlignedCodonFrame cf : codonFrameList)
+ {
+ for (SequenceToSequenceMapping ssm : cf.getMappings())
{
- seqs[i] = currentSeq.createDatasetSequence();
+ if (!seqs.contains(ssm.getFromSeq()))
+ {
+ resolveAndAddDatasetSeq(ssm.getFromSeq(), seqs, false);
+ }
+ if (!seqs.contains(ssm.getMapping().getTo()))
+ {
+ resolveAndAddDatasetSeq(ssm.getMapping().getTo(), seqs, false);
+ }
}
}
-
- dataset = new Alignment(seqs);
+ // finally construct dataset
+ dataset = new Alignment(seqs.toArray(new SequenceI[seqs.size()]));
// move mappings to the dataset alignment
dataset.codonFrameList = this.codonFrameList;
this.codonFrameList = null;
@Override
public void append(AlignmentI toappend)
{
- if (toappend == this)
- {
- System.err.println("Self append may cause a deadlock.");
- }
- // TODO test this method for a future 2.5 release
+ // TODO JAL-1270 needs test coverage
// currently tested for use in jalview.gui.SequenceFetcher
boolean samegap = toappend.getGapCharacter() == getGapCharacter();
char oldc = toappend.getGapCharacter();
.getFullAlignment().getSequences() : toappend.getSequences();
if (sqs != null)
{
+ // avoid self append deadlock by
+ List<SequenceI> toappendsq = new ArrayList<SequenceI>();
synchronized (sqs)
{
for (SequenceI addedsq : sqs)
}
}
}
- addSequence(addedsq);
+ toappendsq.add(addedsq);
}
}
+ for (SequenceI addedsq : toappendsq)
+ {
+ addSequence(addedsq);
+ }
}
AlignmentAnnotation[] alan = toappend.getAlignmentAnnotation();
for (int a = 0; alan != null && a < alan.length; a++)
* Parameters control whether gaps in exon (mapped) and intron (unmapped)
* regions are preserved. Gaps that connect introns to exons are treated
* conservatively, i.e. only preserved if both intron and exon gaps are
- * preserved.
+ * preserved. TODO: check caveats below where the implementation fails
*
* @param al
+ * - must have same dataset, and sequences in al must have equivalent
+ * dataset sequence and start/end bounds under given mapping
* @param preserveMappedGaps
* if true, gaps within and between mapped codons are preserved
* @param preserveUnmappedGaps
boolean preserveUnmappedGaps)
{
// TODO should this method signature be the one in the interface?
+ // JBPComment - yes - neither flag is used, so should be deleted.
boolean thisIsNucleotide = this.isNucleotide();
boolean thatIsProtein = !al.isNucleotide();
if (!thatIsProtein && !thisIsNucleotide)
@Override
public String toString()
{
- return new FastaFile().print(getSequencesArray());
+ return new FastaFile().print(getSequencesArray(), true);
}
/**