X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fdatamodel%2FAlignment.java;h=375e6b455e0a05a13b8f5673883bf626b969e2f1;hb=b122ba188c2ffcbb302809bafc02058e355643ee;hp=b6a5e0f8e0082881f626fa347246c8e78e4123d5;hpb=cfed9d8a9e10be992877d6097260758ee5563630;p=jalview.git diff --git a/src/jalview/datamodel/Alignment.java b/src/jalview/datamodel/Alignment.java index b6a5e0f..375e6b4 100755 --- a/src/jalview/datamodel/Alignment.java +++ b/src/jalview/datamodel/Alignment.java @@ -20,15 +20,10 @@ */ 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; +import java.util.Arrays; import java.util.BitSet; +import java.util.Collection; import java.util.Collections; import java.util.Enumeration; import java.util.HashSet; @@ -39,6 +34,13 @@ import java.util.Map; import java.util.Set; import java.util.Vector; +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; + /** * Data structure to hold and manipulate a multiple sequence alignment */ @@ -46,11 +48,11 @@ import java.util.Vector; * @author JimP * */ -public class Alignment implements AlignmentI +public class Alignment implements AlignmentI, AutoCloseable { private Alignment dataset; - protected List sequences; + private List sequences; protected List groups; @@ -194,11 +196,13 @@ public class Alignment implements AlignmentI { synchronized (sequences) { + if (i > -1 && i < sequences.size()) { return sequences.get(i); } } + return null; } @@ -300,15 +304,20 @@ public class Alignment implements AlignmentI } @Override - public void finalize() throws Throwable + public void close() { if (getDataset() != null) { - getDataset().removeAlignmentRef(); + try + { + getDataset().removeAlignmentRef(); + } catch (Throwable e) + { + e.printStackTrace(); + } } nullReferences(); - super.finalize(); } /** @@ -587,11 +596,12 @@ public class Alignment implements AlignmentI int i = 0; SequenceI sq = null; String sqname = null; + int nseq = sequences.size(); if (startAfter != null) { // try to find the sequence in the alignment boolean matched = false; - while (i < sequences.size()) + while (i < nseq) { if (getSequenceAt(i++) == startAfter) { @@ -604,7 +614,7 @@ public class Alignment implements AlignmentI i = 0; } } - while (i < sequences.size()) + while (i < nseq) { sq = getSequenceAt(i); sqname = sq.getName(); @@ -710,15 +720,22 @@ public class Alignment implements AlignmentI for (int i = 0; i < sequences.size(); i++) { - if (getSequenceAt(i).getLength() > maxLength) - { - maxLength = getSequenceAt(i).getLength(); - } + maxLength = Math.max(maxLength, getSequenceAt(i).getLength()); } - return maxLength; } + @Override + public int getVisibleWidth() + { + int w = getWidth(); + if (hiddenCols != null) + { + w -= hiddenCols.getSize(); + } + return w; + } + /** * DOCUMENT ME! * @@ -1179,7 +1196,8 @@ public class Alignment implements AlignmentI int maxLength = -1; SequenceI current; - for (int i = 0; i < sequences.size(); i++) + int nseq = sequences.size(); + for (int i = 0; i < nseq; i++) { current = getSequenceAt(i); for (int j = current.getLength(); j > maxLength; j--) @@ -1196,7 +1214,7 @@ public class Alignment implements AlignmentI maxLength++; int cLength; - for (int i = 0; i < sequences.size(); i++) + for (int i = 0; i < nseq; i++) { current = getSequenceAt(i); cLength = current.getLength(); @@ -1604,7 +1622,10 @@ public class Alignment implements AlignmentI AlignmentAnnotation annot = new AlignmentAnnotation(name, name, new Annotation[1], 0f, 0f, AlignmentAnnotation.BAR_GRAPH); annot.hasText = false; - annot.setCalcId(new String(calcId)); + if (calcId != null) + { + annot.setCalcId(new String(calcId)); + } annot.autoCalculated = autoCalc; if (seqRef != null) { @@ -1619,40 +1640,23 @@ public class Alignment implements AlignmentI @Override public Iterable findAnnotation(String calcId) { - List aa = new ArrayList<>(); AlignmentAnnotation[] alignmentAnnotation = getAlignmentAnnotation(); if (alignmentAnnotation != null) { - for (AlignmentAnnotation a : alignmentAnnotation) - { - if (a.getCalcId() == calcId || (a.getCalcId() != null - && calcId != null && a.getCalcId().equals(calcId))) - { - aa.add(a); - } - } + return AlignmentAnnotation.findAnnotation( + Arrays.asList(getAlignmentAnnotation()), calcId); } - return aa; + return Arrays.asList(new AlignmentAnnotation[] {}); } @Override public Iterable findAnnotations(SequenceI seq, String calcId, String label) { - ArrayList aa = new ArrayList<>(); - for (AlignmentAnnotation ann : getAlignmentAnnotation()) - { - if ((calcId == null || (ann.getCalcId() != null - && ann.getCalcId().equals(calcId))) - && (seq == null || (ann.sequenceRef != null - && ann.sequenceRef == seq)) - && (label == null - || (ann.label != null && ann.label.equals(label)))) - { - aa.add(ann); - } - } - return aa; + return annotations == null ? null + : AlignmentAnnotation.findAnnotations( + Arrays.asList(getAlignmentAnnotation()), seq, calcId, + label); } @Override @@ -1911,9 +1915,12 @@ public class Alignment implements AlignmentI } @Override - public void setHiddenColumns(HiddenColumns cols) + public boolean setHiddenColumns(HiddenColumns cols) { + boolean changed = cols == null ? hiddenCols != null + : !cols.equals(hiddenCols); hiddenCols = cols; + return changed; } @Override @@ -2028,4 +2035,90 @@ public class Alignment implements AlignmentI } } + //// + //// Contact Matrix Holder Boilerplate + //// + ContactMapHolder cmholder = new ContactMapHolder(); + + @Override + public Collection getContactMaps() + { + return cmholder.getContactMaps(); + } + + @Override + public ContactMatrixI getContactMatrixFor(AlignmentAnnotation _aa) + { + ContactMatrixI cm = cmholder.getContactMatrixFor(_aa); + if (cm == null && _aa.groupRef != null) + { + cm = _aa.groupRef.getContactMatrixFor(_aa); + } + if (cm == null && _aa.sequenceRef != null) + { + cm = _aa.sequenceRef.getContactMatrixFor(_aa); + if (cm == null && _aa.sequenceRef.getDatasetSequence()!=null) + { + // TODO fix up this logic and unify with getContactListFor + cm = _aa.sequenceRef.getDatasetSequence().getContactMatrixFor(_aa); + } + } + return cm; + } + + @Override + public ContactListI getContactListFor(AlignmentAnnotation _aa, int column) + { + if (_aa.annotations==null || column>=_aa.annotations.length || column<0) + { + return null; + } + ContactListI cl = cmholder.getContactListFor(_aa, column); + if (cl == null && _aa.groupRef != null) + { + cl = _aa.groupRef.getContactListFor(_aa, column); + } + if (cl == null && _aa.sequenceRef != null) + { + if (_aa.annotations[column] != null) + { + // sequence associated + cl = _aa.sequenceRef.getContactListFor(_aa, column); + if (cl == null && _aa.sequenceRef.getDatasetSequence() != null) + { + int spos = _aa.sequenceRef.findPosition(column); + if (spos >= _aa.sequenceRef.getStart() + && spos <= 1 + _aa.sequenceRef.getEnd()) + { + cl = _aa.sequenceRef.getDatasetSequence().getContactListFor(_aa, + spos - _aa.sequenceRef.getStart()); + } + } + } + } + return cl; + } + + @Override + public AlignmentAnnotation addContactList(ContactMatrixI cm) + { + AlignmentAnnotation aa = cmholder.addContactList(cm); + + Annotation _aa[] = new Annotation[getWidth()]; + for (int i = 0; i < _aa.length; _aa[i++] = new Annotation(0.0f)) + { + ; + } + aa.annotations = _aa; + addAnnotation(aa); + return aa; + } + + @Override + public void addContactListFor(AlignmentAnnotation annotation, + ContactMatrixI cm) + { + cmholder.addContactListFor(annotation, cm); + + } }