X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fanalysis%2FAlignmentUtils.java;fp=src%2Fjalview%2Fanalysis%2FAlignmentUtils.java;h=6f0125d97eb5e287aae5f90fc8d9a359375bd1d1;hb=f18077aa57dea5481ae537162871045f8557e2f0;hp=9202c0d08a969e698565c30e27d1f89db4793552;hpb=bb074f6d0c036c1b7754d83fe4dbd9f00e95fe72;p=jalview.git diff --git a/src/jalview/analysis/AlignmentUtils.java b/src/jalview/analysis/AlignmentUtils.java index 9202c0d..6f0125d 100644 --- a/src/jalview/analysis/AlignmentUtils.java +++ b/src/jalview/analysis/AlignmentUtils.java @@ -20,19 +20,9 @@ */ package jalview.analysis; -import jalview.datamodel.AlignedCodon; -import jalview.datamodel.AlignedCodonFrame; -import jalview.datamodel.AlignmentAnnotation; -import jalview.datamodel.AlignmentI; -import jalview.datamodel.Mapping; -import jalview.datamodel.SearchResults; -import jalview.datamodel.Sequence; -import jalview.datamodel.SequenceI; -import jalview.schemes.ResidueProperties; -import jalview.util.MapList; - import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; @@ -43,6 +33,18 @@ import java.util.Map.Entry; import java.util.Set; import java.util.TreeMap; +import jalview.datamodel.AlignedCodon; +import jalview.datamodel.AlignedCodonFrame; +import jalview.datamodel.AlignmentAnnotation; +import jalview.datamodel.AlignmentI; +import jalview.datamodel.Mapping; +import jalview.datamodel.SearchResults; +import jalview.datamodel.Sequence; +import jalview.datamodel.SequenceGroup; +import jalview.datamodel.SequenceI; +import jalview.schemes.ResidueProperties; +import jalview.util.MapList; + /** * grab bag of useful alignment manipulation operations Expect these to be * refactored elsewhere at some point. @@ -1056,4 +1058,158 @@ public class AlignmentUtils */ return mapProteinToCdna(proteinDs, dnaDs) != null; } + + /** + * Finds any reference annotations associated with the sequences in + * sequenceScope, that are not already added to the alignment, and adds them + * to the 'candidates' map. Also populates a lookup table of annotation + * labels, keyed by calcId, for use in constructing tooltips or the like. + * + * @param sequenceScope + * the sequences to scan for reference annotations + * @param labelForCalcId + * (optional) map to populate with label for calcId + * @param candidates + * map to populate with annotations for sequence + * @param al + * the alignment to check for presence of annotations + */ + public static void findAddableReferenceAnnotations( + List sequenceScope, Map labelForCalcId, + final Map> candidates, + AlignmentI al) + { + if (sequenceScope == null) + { + return; + } + + /* + * For each sequence in scope, make a list of any annotations on the + * underlying dataset sequence which are not already on the alignment. + * + * Add to a map of { alignmentSequence, } + */ + for (SequenceI seq : sequenceScope) + { + SequenceI dataset = seq.getDatasetSequence(); + if (dataset == null) + { + continue; + } + AlignmentAnnotation[] datasetAnnotations = dataset.getAnnotation(); + if (datasetAnnotations == null) + { + continue; + } + final List result = new ArrayList(); + for (AlignmentAnnotation dsann : datasetAnnotations) + { + /* + * Find matching annotations on the alignment. If none is found, then + * add this annotation to the list of 'addable' annotations for this + * sequence. + */ + final Iterable matchedAlignmentAnnotations = al + .findAnnotations(seq, dsann.getCalcId(), + dsann.label); + if (!matchedAlignmentAnnotations.iterator().hasNext()) + { + result.add(dsann); + if (labelForCalcId != null) + { + labelForCalcId.put(dsann.getCalcId(), dsann.label); + } + } + } + /* + * Save any addable annotations for this sequence + */ + if (!result.isEmpty()) + { + candidates.put(seq, result); + } + } + } + + /** + * Adds annotations to the top of the alignment annotations, in the same order + * as their related sequences. + * + * @param annotations + * the annotations to add + * @param alignment + * the alignment to add them to + * @param selectionGroup + * current selection group (or null if none) + */ + public static void addReferenceAnnotations( + Map> annotations, + final AlignmentI alignment, final SequenceGroup selectionGroup) + { + for (SequenceI seq : annotations.keySet()) + { + for (AlignmentAnnotation ann : annotations.get(seq)) + { + AlignmentAnnotation copyAnn = new AlignmentAnnotation(ann); + int startRes = 0; + int endRes = ann.annotations.length; + if (selectionGroup != null) + { + startRes = selectionGroup.getStartRes(); + endRes = selectionGroup.getEndRes(); + } + copyAnn.restrict(startRes, endRes); + + /* + * Add to the sequence (sets copyAnn.datasetSequence), unless the + * original annotation is already on the sequence. + */ + if (!seq.hasAnnotation(ann)) + { + seq.addAlignmentAnnotation(copyAnn); + } + // adjust for gaps + copyAnn.adjustForAlignment(); + // add to the alignment and set visible + alignment.addAnnotation(copyAnn); + copyAnn.visible = true; + } + } + } + + /** + * Set visibility of alignment annotations of specified types (labels), for + * specified sequences. This supports controls like + * "Show all secondary structure", "Hide all Temp factor", etc. + * + * @al the alignment to scan for annotations + * @param types + * the types (labels) of annotations to be updated + * @param forSequences + * if not null, only annotations linked to one of these sequences are + * in scope for update; if null, acts on all sequence annotations + * @param anyType + * if this flag is true, 'types' is ignored (label not checked) + * @param doShow + * if true, set visibility on, else set off + */ + public static void showOrHideSequenceAnnotations(AlignmentI al, + Collection types, List forSequences, + boolean anyType, boolean doShow) + { + for (AlignmentAnnotation aa : al + .getAlignmentAnnotation()) + { + if (anyType || types.contains(aa.label)) + { + if ((aa.sequenceRef != null) + && (forSequences == null || forSequences + .contains(aa.sequenceRef))) + { + aa.visible = doShow; + } + } + } + } }