X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fgui%2FAppVarna.java;h=f529316506c171a8e1118f93a1e56905d48d47ed;hb=HEAD;hp=dcb2a7ddea8ca0519fadc574e3e07400c0df4d55;hpb=528dcd143968fc270232e4cec64a79b447aa5ec6;p=jalview.git diff --git a/src/jalview/gui/AppVarna.java b/src/jalview/gui/AppVarna.java index dcb2a7d..f529316 100644 --- a/src/jalview/gui/AppVarna.java +++ b/src/jalview/gui/AppVarna.java @@ -20,22 +20,6 @@ */ package jalview.gui; -import jalview.analysis.AlignSeq; -import jalview.datamodel.AlignmentAnnotation; -import jalview.datamodel.ColumnSelection; -import jalview.datamodel.RnaViewerModel; -import jalview.datamodel.SequenceGroup; -import jalview.datamodel.SequenceI; -import jalview.ext.varna.RnaModel; -import jalview.structure.SecondaryStructureListener; -import jalview.structure.SelectionListener; -import jalview.structure.SelectionSource; -import jalview.structure.StructureSelectionManager; -import jalview.structure.VamsasSource; -import jalview.util.Comparison; -import jalview.util.MessageManager; -import jalview.util.ShiftList; - import java.awt.BorderLayout; import java.awt.Color; import java.util.Collection; @@ -43,8 +27,6 @@ import java.util.Hashtable; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; -import java.util.regex.Matcher; -import java.util.regex.Pattern; import javax.swing.JInternalFrame; import javax.swing.JSplitPane; @@ -61,13 +43,29 @@ import fr.orsay.lri.varna.models.FullBackup; import fr.orsay.lri.varna.models.annotations.HighlightRegionAnnotation; import fr.orsay.lri.varna.models.rna.ModeleBase; import fr.orsay.lri.varna.models.rna.RNA; +import jalview.analysis.AlignSeq; +import jalview.datamodel.AlignmentAnnotation; +import jalview.datamodel.ColumnSelection; +import jalview.datamodel.HiddenColumns; +import jalview.datamodel.RnaViewerModel; +import jalview.datamodel.SequenceGroup; +import jalview.datamodel.SequenceI; +import jalview.ext.varna.RnaModel; +import jalview.structure.SecondaryStructureListener; +import jalview.structure.SelectionListener; +import jalview.structure.SelectionSource; +import jalview.structure.StructureSelectionManager; +import jalview.structure.VamsasSource; +import jalview.util.Comparison; +import jalview.util.MessageManager; +import jalview.util.ShiftList; -public class AppVarna extends JInternalFrame implements SelectionListener, - SecondaryStructureListener, InterfaceVARNASelectionListener, - VamsasSource +public class AppVarna extends JInternalFrame + implements SelectionListener, SecondaryStructureListener, + InterfaceVARNASelectionListener, VamsasSource { - private static final Pattern PAIRS_PATTERN = Pattern - .compile("[^([{<>}])]"); + private static final byte[] PAIRS = new byte[] { '(', ')', '[', ']', '{', + '}', '<', '>' }; private AppVarnaBinding vab; @@ -121,6 +119,15 @@ public class AppVarna extends JInternalFrame implements SelectionListener, } } + /** + * highlight a region from start to end (inclusive) on rna + * + * @param rna + * @param start + * - first base pair index (from 0) + * @param end + * - last base pair index (from 0) + */ public void highlightRegion(RNA rna, int start, int end) { clearLastSelection(); @@ -178,10 +185,12 @@ public class AppVarna extends JInternalFrame implements SelectionListener, { this(ap); - String sname = aa.sequenceRef == null ? "secondary structure (alignment)" + String sname = aa.sequenceRef == null + ? "secondary structure (alignment)" : seq.getName() + " structure"; String theTitle = sname - + (aa.sequenceRef == null ? " trimmed to " + seq.getName() : ""); + + (aa.sequenceRef == null ? " trimmed to " + seq.getName() + : ""); theTitle = MessageManager.formatMessage("label.varna_params", new String[] { theTitle }); @@ -192,12 +201,12 @@ public class AppVarna extends JInternalFrame implements SelectionListener, addModel(gappedModel, gappedTitle); String trimmedTitle = "trimmed " + sname; - RnaModel trimmedModel = new RnaModel(trimmedTitle, aa, seq, null, false); + RnaModel trimmedModel = new RnaModel(trimmedTitle, aa, seq, null, + false); addModel(trimmedModel, trimmedTitle); vab.setSelectedIndex(0); } - /** * Constructor that links the viewer to a parent panel (but has no structures * yet - use addModel to add them) @@ -206,6 +215,7 @@ public class AppVarna extends JInternalFrame implements SelectionListener, */ protected AppVarna(AlignmentPanel ap) { + this.setFrameIcon(null); this.ap = ap; this.viewId = System.currentTimeMillis() + "." + this.hashCode(); vab = new AppVarnaBinding(); @@ -260,13 +270,6 @@ public class AppVarna extends JInternalFrame implements SelectionListener, showPanel(true); } - public String replaceOddGaps(String oldStr) - { - Matcher matcher = PAIRS_PATTERN.matcher(oldStr); - String newStr = matcher.replaceAll("."); - return newStr; - } - /** * Constructs a new RNA model from the given one, without gaps. Also * calculates and saves a 'shift list' @@ -282,7 +285,8 @@ public class AppVarna extends JInternalFrame implements SelectionListener, RNA rnaTrim = new RNA(name); try { - rnaTrim.setRNA(rna.getSeq(), replaceOddGaps(rna.getStructDBN())); + String structDBN = rna.getStructDBN(true); + rnaTrim.setRNA(rna.getSeq(), replaceOddGaps(structDBN)); } catch (ExceptionUnmatchedClosingParentheses e2) { e2.printStackTrace(); @@ -293,7 +297,7 @@ public class AppVarna extends JInternalFrame implements SelectionListener, String seq = rnaTrim.getSeq(); StringBuilder struc = new StringBuilder(256); - struc.append(rnaTrim.getStructDBN()); + struc.append(rnaTrim.getStructDBN(true)); int ofstart = -1; int sleng = seq.length(); @@ -306,7 +310,7 @@ public class AppVarna extends JInternalFrame implements SelectionListener, ofstart = i; } /* - * mark base or base pair in the structure with * + * mark base or base & pair in the structure with * */ if (!rnaTrim.findPair(i).isEmpty()) { @@ -402,7 +406,8 @@ public class AppVarna extends JInternalFrame implements SelectionListener, RnaModel rnaModel = models.get(rna); if (rnaModel.seq == sequence) { - int highlightPos = rnaModel.gapped ? index : position - 1; + int highlightPos = rnaModel.gapped ? index + : position - sequence.getStart(); mouseOverHighlighter.highlightRegion(rna, highlightPos, highlightPos); vab.updateSelectedRNA(rna); } @@ -410,7 +415,7 @@ public class AppVarna extends JInternalFrame implements SelectionListener, @Override public void selection(SequenceGroup seqsel, ColumnSelection colsel, - SelectionSource source) + HiddenColumns hidden, SelectionSource source) { if (source != ap.av) { @@ -423,18 +428,31 @@ public class AppVarna extends JInternalFrame implements SelectionListener, { return; } - if (seqsel != null && seqsel.getSize() > 0) + + RnaModel rnaModel = models.get(rna); + + if (seqsel != null && seqsel.getSize() > 0 + && seqsel.contains(rnaModel.seq)) { int start = seqsel.getStartRes(), end = seqsel.getEndRes(); - ShiftList shift = offsets.get(rna); - if (shift != null) + if (rnaModel.gapped) { - start = shift.shift(start); - end = shift.shift(end); + ShiftList shift = offsets.get(rna); + if (shift != null) + { + start = shift.shift(start); + end = shift.shift(end); + } + } + else + { + start = rnaModel.seq.findPosition(start) - rnaModel.seq.getStart(); + end = rnaModel.seq.findPosition(end) - rnaModel.seq.getStart(); } + selectionHighlighter.highlightRegion(rna, start, end); - selectionHighlighter.getLastHighlight().setOutlineColor( - seqsel.getOutlineColour()); + selectionHighlighter.getLastHighlight() + .setOutlineColor(seqsel.getOutlineColour()); // TODO - translate column markings to positions on structure if present. vab.updateSelectedRNA(rna); } @@ -458,7 +476,8 @@ public class AppVarna extends JInternalFrame implements SelectionListener, if (shift != null) { int i = shift.shift(newBase.getIndex()); - // System.err.println("shifted "+(arg1.getIndex())+" to "+i); + // jalview.bin.Console.errPrintln("shifted "+(arg1.getIndex())+" to + // "+i); ssm.mouseOverVamsasSequence(seq, i, this); } else @@ -469,7 +488,8 @@ public class AppVarna extends JInternalFrame implements SelectionListener, } @Override - public void onSelectionChanged(BaseList arg0, BaseList arg1, BaseList arg2) + public void onSelectionChanged(BaseList arg0, BaseList arg1, + BaseList arg2) { // TODO translate selected regions in VARNA to a selection on the // alignpanel. @@ -586,7 +606,8 @@ public class AppVarna extends JInternalFrame implements SelectionListener, { if (!model.ann.isValidStruc()) { - throw new IllegalArgumentException("Invalid RNA structure annotation"); + throw new IllegalArgumentException( + "Invalid RNA structure annotation"); } /* @@ -631,11 +652,10 @@ public class AppVarna extends JInternalFrame implements SelectionListener, ShiftList offset = new ShiftList(); int ofstart = -1; int sleng = seq.getLength(); - char[] seqChars = seq.getSequence(); for (int i = 0; i < sleng; i++) { - if (Comparison.isGap(seqChars[i])) + if (Comparison.isGap(seq.getCharAt(i))) { if (ofstart == -1) { @@ -685,7 +705,6 @@ public class AppVarna extends JInternalFrame implements SelectionListener, vab.setSelectedIndex(selectedIndex); } - /** * Add a model with associated Varna session file * @@ -697,7 +716,8 @@ public class AppVarna extends JInternalFrame implements SelectionListener, { if (!model.ann.isValidStruc()) { - throw new IllegalArgumentException("Invalid RNA structure annotation"); + throw new IllegalArgumentException( + "Invalid RNA structure annotation"); } try @@ -723,4 +743,41 @@ public class AppVarna extends JInternalFrame implements SelectionListener, return null; } } + + /** + * Replace everything except RNA secondary structure characters with a period + * + * @param s + * @return + */ + public static String replaceOddGaps(String s) + { + if (s == null) + { + return null; + } + + // this is measured to be 10 times faster than a regex replace + boolean changed = false; + byte[] bytes = s.getBytes(); + for (int i = 0; i < bytes.length; i++) + { + boolean ok = false; + // todo check for ((b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z')) if + // wanted also + for (int j = 0; !ok && (j < PAIRS.length); j++) + { + if (bytes[i] == PAIRS[j]) + { + ok = true; + } + } + if (!ok) + { + bytes[i] = '.'; + changed = true; + } + } + return changed ? new String(bytes) : s; + } }