X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fdatamodel%2FAlignment.java;h=90bdcae2ee318ae29b290bbb5ea1e5a145b2389a;hb=d2177decb24a93432ad55175b2123741bb3d8ac7;hp=fd918fc709fffdf48390fb8a80f70c36d5614799;hpb=4956fb78f7222e84d0d7008a6b91146d6c61d151;p=jalview.git diff --git a/src/jalview/datamodel/Alignment.java b/src/jalview/datamodel/Alignment.java index fd918fc..90bdcae 100755 --- a/src/jalview/datamodel/Alignment.java +++ b/src/jalview/datamodel/Alignment.java @@ -24,6 +24,7 @@ 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; @@ -226,18 +227,21 @@ public class Alignment implements AlignmentI { 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) { @@ -287,13 +291,23 @@ public class Alignment implements AlignmentI } @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; @@ -302,14 +316,16 @@ public class Alignment implements AlignmentI } /** - * 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(); } } @@ -350,17 +366,18 @@ public class Alignment implements AlignmentI * @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; + } } } } @@ -645,7 +662,7 @@ public class Alignment implements AlignmentI * jalview.datamodel.AlignmentI#findIndex(jalview.datamodel.SearchResults) */ @Override - public int findIndex(SearchResults results) + public int findIndex(SearchResultsI results) { int i = 0; @@ -1039,6 +1056,7 @@ public class Alignment implements AlignmentI private void resolveAndAddDatasetSeq(SequenceI currentSeq, Set seqs, boolean createDatasetSequence) { + SequenceI alignedSeq = currentSeq; if (currentSeq.getDatasetSequence() != null) { currentSeq = currentSeq.getDatasetSequence(); @@ -1073,12 +1091,19 @@ public class Alignment implements AlignmentI { 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 + " is not a dataset sequence."); - // TODO: if this happens, could also rewrite the reference to - // point to new dataset sequence + 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 @@ -1100,7 +1125,7 @@ public class Alignment implements AlignmentI return; } // try to avoid using SequenceI.equals at this stage, it will be expensive - Set seqs = new jalview.util.LinkedIdentityHashSet(); + Set seqs = new LinkedIdentityHashSet(); for (int i = 0; i < getHeight(); i++) { @@ -1813,7 +1838,7 @@ public class Alignment implements AlignmentI @Override public String toString() { - return new FastaFile().print(getSequencesArray()); + return new FastaFile().print(getSequencesArray(), true); } /**