X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fanalysis%2FAlignmentUtils.java;h=1158c53bb37dec432977f5bf1182587fe9d9ae3b;hb=b15a4dfe51ded457b2850f14c19b5245aadb918e;hp=201f88b251154217f055ef5c23181c1ad4457591;hpb=4b7d3640209c4434d569c746672cf9eed4250ace;p=jalview.git diff --git a/src/jalview/analysis/AlignmentUtils.java b/src/jalview/analysis/AlignmentUtils.java index 201f88b..1158c53 100644 --- a/src/jalview/analysis/AlignmentUtils.java +++ b/src/jalview/analysis/AlignmentUtils.java @@ -1504,7 +1504,8 @@ public class AlignmentUtils /** * Adds annotations to the top of the alignment annotations, in the same order - * as their related sequences. + * as their related sequences. If you already have an annotation and want to + * add it to a sequence in an alignment use {@code addReferenceAnnotationTo} * * @param annotations * the annotations to add @@ -1521,38 +1522,60 @@ public class AlignmentUtils { 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 + 0); + addReferenceAnnotationTo(alignment, seq, ann, selectionGroup); + } + } + } - /* - * Add to the sequence (sets copyAnn.datasetSequence), unless the - * original annotation is already on the sequence. - */ - if (!seq.hasAnnotation(ann)) - { - ContactMatrixI cm = seq.getDatasetSequence() - .getContactMatrixFor(ann); - if (cm != null) - { - seq.addContactListFor(copyAnn, cm); - } - seq.addAlignmentAnnotation(copyAnn); - } - // adjust for gaps - copyAnn.adjustForAlignment(); - // add to the alignment and set visible - alignment.addAnnotation(copyAnn); - copyAnn.visible = true; + /** + * Make a copy of a reference annotation {@code ann} and add it to an + * alignment sequence {@code seq} in {@code alignment}, optionally limited to + * the extent of {@code selectionGroup} + * + * @param alignment + * @param seq + * @param ann + * @param selectionGroup + * - may be null + * @return annotation added to {@code seq and {@code alignment} + */ + public static AlignmentAnnotation addReferenceAnnotationTo( + final AlignmentI alignment, final SequenceI seq, + final AlignmentAnnotation ann, final SequenceGroup selectionGroup) + { + AlignmentAnnotation copyAnn = new AlignmentAnnotation(ann); + int startRes = 0; + int endRes = ann.annotations.length; + if (selectionGroup != null) + { + startRes = -1 + Math.min(seq.getEnd(), Math.max(seq.getStart(), + seq.findPosition(selectionGroup.getStartRes()))); + endRes = -1 + Math.min(seq.getEnd(), + seq.findPosition(selectionGroup.getEndRes())); + + } + copyAnn.restrict(startRes, endRes + 0); + + /* + * Add to the sequence (sets copyAnn.datasetSequence), unless the + * original annotation is already on the sequence. + */ + if (!seq.hasAnnotation(ann)) + { + ContactMatrixI cm = seq.getDatasetSequence().getContactMatrixFor(ann); + if (cm != null) + { + seq.addContactListFor(copyAnn, cm); } + seq.addAlignmentAnnotation(copyAnn); } + // adjust for gaps + copyAnn.adjustForAlignment(); + // add to the alignment and set visible + alignment.addAnnotation(copyAnn); + copyAnn.visible = true; + return copyAnn; } /**