From 566041d3e8ca1d8783b4572a695e83784c0b6bc7 Mon Sep 17 00:00:00 2001 From: gmungoc Date: Fri, 12 Aug 2016 11:33:55 +0100 Subject: [PATCH] JAL-1841 bug fixes / code tidy for rna SS rendering / parsing --- src/jalview/datamodel/AlignmentAnnotation.java | 12 +-- src/jalview/io/RnamlFile.java | 34 +++---- src/jalview/io/StockholmFile.java | 49 +++++----- src/jalview/renderer/AnnotationRenderer.java | 44 ++++----- src/jalview/schemes/ResidueProperties.java | 115 ------------------------ 5 files changed, 73 insertions(+), 181 deletions(-) diff --git a/src/jalview/datamodel/AlignmentAnnotation.java b/src/jalview/datamodel/AlignmentAnnotation.java index 7990a5c..a5a38df 100755 --- a/src/jalview/datamodel/AlignmentAnnotation.java +++ b/src/jalview/datamodel/AlignmentAnnotation.java @@ -24,11 +24,11 @@ import jalview.analysis.Rna; import jalview.analysis.SecStrConsensus.SimpleBP; import jalview.analysis.WUSSParseException; -import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.Iterator; +import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -79,7 +79,7 @@ public class AlignmentAnnotation /** Array of annotations placed in the current coordinate system */ public Annotation[] annotations; - public ArrayList bps = null; + public List bps = null; /** * RNA secondary structure contact positions @@ -102,8 +102,8 @@ public class AlignmentAnnotation { try { - _rnasecstr = Rna.GetBasePairs(RNAannot); - bps = Rna.GetModeleBP(RNAannot); + bps = Rna.getModeleBP(RNAannot); + _rnasecstr = Rna.getBasePairs(bps); invalidrnastruc = -1; } catch (WUSSParseException px) { @@ -272,7 +272,7 @@ public class AlignmentAnnotation // JBPNote: what does this do ? public void ConcenStru(CharSequence RNAannot) throws WUSSParseException { - bps = Rna.GetModeleBP(RNAannot); + bps = Rna.getModeleBP(RNAannot); } /** @@ -485,7 +485,7 @@ public class AlignmentAnnotation this(0, annotations.length); } - public AnnotCharSequence(int start, int end) + AnnotCharSequence(int start, int end) { offset = start; max = end; diff --git a/src/jalview/io/RnamlFile.java b/src/jalview/io/RnamlFile.java index 0941a6f..f48f825 100644 --- a/src/jalview/io/RnamlFile.java +++ b/src/jalview/io/RnamlFile.java @@ -20,7 +20,7 @@ */ package jalview.io; -import jalview.analysis.SecStrConsensus.SimpleBP; +import jalview.analysis.Rna; import jalview.datamodel.AlignmentAnnotation; import jalview.datamodel.Annotation; import jalview.datamodel.Sequence; @@ -32,6 +32,7 @@ import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; +import java.util.List; import com.stevesoft.pat.Regex; @@ -79,6 +80,7 @@ public class RnamlFile extends AlignFile * * @see jalview.io.AlignFile#parse() */ + @Override public void parse() throws IOException { if (System.getProperty("java.version").indexOf("1.6") > -1 @@ -134,10 +136,10 @@ public class RnamlFile extends AlignFile result = RNAFactory.loadSecStrRNAML(getReader()); - ArrayList allarray = new ArrayList(); - ArrayList> BP = new ArrayList(); - ArrayList strucinarray = new ArrayList(); - SequenceI[] seqs = new SequenceI[result.size()]; + // ArrayList allarray = new ArrayList(); + // ArrayList> BP = new ArrayList(); + // ArrayList strucinarray = new ArrayList(); + SequenceI[] sqs = new SequenceI[result.size()]; for (int i = 0; i < result.size(); i++) { @@ -157,9 +159,9 @@ public class RnamlFile extends AlignFile id += "." + i; } } - seqs[i] = new Sequence(id, seq, begin, end); + sqs[i] = new Sequence(id, seq, begin, end); - seqs[i].setEnd(seqs[i].findPosition(seqs[i].getLength())); + sqs[i].setEnd(sqs[i].findPosition(sqs[i].getLength())); String[] annot = new String[rna.length()]; Annotation[] ann = new Annotation[rna.length()]; @@ -170,9 +172,8 @@ public class RnamlFile extends AlignFile } for (int k = 0; k < rna.length(); k++) { - ann[k] = new Annotation(annot[k], "", - jalview.schemes.ResidueProperties.getRNASecStrucState( - annot[k]).charAt(0), 0f); + ann[k] = new Annotation(annot[k], "", Rna.getRNASecStrucState( + annot[k]).charAt(0), 0f); } AlignmentAnnotation align = new AlignmentAnnotation( @@ -181,17 +182,17 @@ public class RnamlFile extends AlignFile + current.getID() : "", ann); - seqs[i].addAlignmentAnnotation(align); - seqs[i].setRNA(result.get(i)); + sqs[i].addAlignmentAnnotation(align); + sqs[i].setRNA(result.get(i)); - allarray.add(strucinarray); + // allarray.add(strucinarray); annotations.addElement(align); - BP.add(align.bps); + // BP.add(align.bps); } - setSeqs(seqs); + setSeqs(sqs); } public static String print(SequenceI[] s) @@ -199,13 +200,14 @@ public class RnamlFile extends AlignFile return "not yet implemented"; } + @Override public String print() { System.out.print("print :"); return print(getSeqsAsArray()); } - public ArrayList getRNA() + public List getRNA() { return result; } diff --git a/src/jalview/io/StockholmFile.java b/src/jalview/io/StockholmFile.java index 23c4d21..a5cd401 100644 --- a/src/jalview/io/StockholmFile.java +++ b/src/jalview/io/StockholmFile.java @@ -23,6 +23,7 @@ */ package jalview.io; +import jalview.analysis.Rna; import jalview.datamodel.AlignmentAnnotation; import jalview.datamodel.AlignmentI; import jalview.datamodel.Annotation; @@ -31,6 +32,7 @@ import jalview.datamodel.Mapping; import jalview.datamodel.Sequence; import jalview.datamodel.SequenceFeature; import jalview.datamodel.SequenceI; +import jalview.schemes.ResidueProperties; import jalview.util.Format; import jalview.util.MessageManager; @@ -72,8 +74,12 @@ import fr.orsay.lri.varna.models.rna.RNA; */ public class StockholmFile extends AlignFile { - // static Logger logger = Logger.getLogger("jalview.io.StockholmFile"); - protected ArrayList result; + private static final Regex OPEN_PAREN = new Regex("(<|\\[)", "("); + + private static final Regex CLOSE_PAREN = new Regex("(>|\\])", ")"); + + private static final Regex DETECT_BRACKETS = new Regex( + "(<|>|\\[|\\]|\\(|\\))"); StringBuffer out; // output buffer @@ -101,6 +107,7 @@ public class StockholmFile extends AlignFile super(source); } + @Override public void initData() { super.initData(); @@ -118,7 +125,7 @@ public class StockholmFile extends AlignFile fr = new FileReader(inFile); BufferedReader r = new BufferedReader(fr); - result = null; + List result = null; try { result = RNAFactory.loadSecStrStockholm(r); @@ -155,9 +162,8 @@ public class StockholmFile extends AlignFile for (int k = 0; k < rna.length(); k++) { - ann[k] = new Annotation(annot[k], "", - jalview.schemes.ResidueProperties.getRNASecStrucState( - annot[k]).charAt(0), 0f); + ann[k] = new Annotation(annot[k], "", Rna.getRNASecStrucState( + annot[k]).charAt(0), 0f); } AlignmentAnnotation align = new AlignmentAnnotation("Sec. str.", @@ -178,6 +184,7 @@ public class StockholmFile extends AlignFile * @throws IOException * If there is an error with the input file */ + @Override public void parse() throws IOException { StringBuffer treeString = new StringBuffer(); @@ -788,19 +795,13 @@ public class StockholmFile extends AlignFile } protected static AlignmentAnnotation parseAnnotationRow( - Vector annotation, String label, String annots) + Vector annotation, String label, + String annots) { String convert1, convert2 = null; - // Convert all bracket types to parentheses - Regex openparen = new Regex("(<|\\[)", "("); - Regex closeparen = new Regex("(>|\\])", ")"); - - // Detect if file is RNA by looking for bracket types - Regex detectbrackets = new Regex("(<|>|\\[|\\]|\\(|\\))"); - - convert1 = openparen.replaceAll(annots); - convert2 = closeparen.replaceAll(convert1); + convert1 = OPEN_PAREN.replaceAll(annots); + convert2 = CLOSE_PAREN.replaceAll(convert1); annots = convert2; String type = label; @@ -827,15 +828,15 @@ public class StockholmFile extends AlignFile { // if (" .-_".indexOf(pos) == -1) { - if (detectbrackets.search(pos)) + if (DETECT_BRACKETS.search(pos)) { - ann.secondaryStructure = jalview.schemes.ResidueProperties - .getRNASecStrucState(pos).charAt(0); + ann.secondaryStructure = Rna.getRNASecStrucState( + pos).charAt(0); } else { - ann.secondaryStructure = jalview.schemes.ResidueProperties - .getDssp3state(pos).charAt(0); + ann.secondaryStructure = ResidueProperties.getDssp3state(pos) + .charAt(0); } if (ann.secondaryStructure == pos.charAt(0)) @@ -853,10 +854,10 @@ public class StockholmFile extends AlignFile els[i] = ann; } AlignmentAnnotation annot = null; - Enumeration e = annotation.elements(); + Enumeration e = annotation.elements(); while (e.hasMoreElements()) { - annot = (AlignmentAnnotation) e.nextElement(); + annot = e.nextElement(); if (annot.label.equals(type)) { break; @@ -1106,6 +1107,7 @@ public class StockholmFile extends AlignFile return seq; } + @Override public String print() { out = new StringBuffer(); @@ -1119,6 +1121,7 @@ public class StockholmFile extends AlignFile } private static Hashtable typeIds = null; + static { if (typeIds == null) diff --git a/src/jalview/renderer/AnnotationRenderer.java b/src/jalview/renderer/AnnotationRenderer.java index 75099c2..634b1e4 100644 --- a/src/jalview/renderer/AnnotationRenderer.java +++ b/src/jalview/renderer/AnnotationRenderer.java @@ -22,6 +22,7 @@ package jalview.renderer; import jalview.analysis.AAFrequency; import jalview.analysis.CodingUtils; +import jalview.analysis.Rna; import jalview.analysis.StructureFrequency; import jalview.api.AlignViewportI; import jalview.datamodel.AlignmentAnnotation; @@ -43,8 +44,6 @@ import java.awt.image.ImageObserver; import java.util.BitSet; import java.util.Hashtable; -import com.stevesoft.pat.Regex; - public class AnnotationRenderer { private static final int UPPER_TO_LOWER = 'a' - 'A'; // 32 @@ -75,7 +74,7 @@ public class AnnotationRenderer this.debugRedraw = debugRedraw; } - public void drawStemAnnot(Graphics g, Annotation[] row_annotations, + void drawStemAnnot(Graphics g, Annotation[] row_annotations, int lastSSX, int x, int y, int iconOffset, int startRes, int column, boolean validRes, boolean validEnd) { @@ -83,7 +82,6 @@ public class AnnotationRenderer int sCol = (lastSSX / charWidth) + startRes; int x1 = lastSSX; int x2 = (x * charWidth); - Regex closeparen = new Regex("(\\))"); char dc = (column == 0 || row_annotations[column - 1] == null) ? ' ' : row_annotations[column - 1].secondaryStructure; @@ -93,15 +91,17 @@ public class AnnotationRenderer boolean diffdownstream = !validRes || !validEnd || row_annotations[column] == null || dc != row_annotations[column].secondaryStructure; - // System.out.println("Column "+column+" diff up: "+diffupstream+" down:"+diffdownstream); - // If a closing base pair half of the stem, display a backward arrow - if (column > 0 && ResidueProperties.isCloseParenRNA(dc)) - { + if (column > 0 && Rna.isClosingParenthesis(dc)) + { if (diffupstream) // if (validRes && column>1 && row_annotations[column-2]!=null && // dc.equals(row_annotations[column-2].displayCharacter)) { + /* + * if new annotation with a closing base pair half of the stem, + * display a backward arrow + */ g.fillPolygon(new int[] { lastSSX + 5, lastSSX + 5, lastSSX }, new int[] { y + iconOffset, y + 14 + iconOffset, y + 8 + iconOffset }, 3); @@ -114,10 +114,13 @@ public class AnnotationRenderer } else { - // display a forward arrow if (diffdownstream) { + /* + * if annotation ending with an opeing base pair half of the stem, + * display a forward arrow + */ g.fillPolygon(new int[] { x2 - 5, x2 - 5, x2 }, new int[] { y + iconOffset, y + 14 + iconOffset, y + 8 + iconOffset }, 3); x2 -= 5; @@ -195,7 +198,7 @@ public class AnnotationRenderer */ private boolean canClip = false; - public void drawNotCanonicalAnnot(Graphics g, Color nonCanColor, + void drawNotCanonicalAnnot(Graphics g, Color nonCanColor, Annotation[] row_annotations, int lastSSX, int x, int y, int iconOffset, int startRes, int column, boolean validRes, boolean validEnd) @@ -206,7 +209,6 @@ public class AnnotationRenderer int sCol = (lastSSX / charWidth) + startRes; int x1 = lastSSX; int x2 = (x * charWidth); - Regex closeparen = new Regex("}|]|<|[a-z]"); String dc = (column == 0 || row_annotations[column - 1] == null) ? "" : row_annotations[column - 1].displayCharacter; @@ -218,8 +220,7 @@ public class AnnotationRenderer || !dc.equals(row_annotations[column].displayCharacter); // System.out.println("Column "+column+" diff up: "+diffupstream+" down:"+diffdownstream); // If a closing base pair half of the stem, display a backward arrow - if (column > 0 && closeparen.search(dc))// closeletter_b.search(dc)||closeletter_c.search(dc)||closeletter_d.search(dc)||closecrochet.search(dc)) - // ) + if (column > 0 && Rna.isClosingParenthesis(dc.charAt(0))) { if (diffupstream) @@ -321,7 +322,7 @@ public class AnnotationRenderer * @param column * @return */ - public int[] getProfileFor(AlignmentAnnotation aa, int column) + int[] getProfileFor(AlignmentAnnotation aa, int column) { // TODO : consider refactoring the global alignment calculation // properties/rendering attributes as a global 'alignment group' which holds @@ -1069,7 +1070,7 @@ public class AnnotationRenderer private Color sdNOTCANONICAL_COLOUR; - public void drawGlyphLine(Graphics g, Annotation[] row, int lastSSX, + void drawGlyphLine(Graphics g, Annotation[] row, int lastSSX, int x, int y, int iconOffset, int startRes, int column, boolean validRes, boolean validEnd) { @@ -1077,7 +1078,7 @@ public class AnnotationRenderer g.fillRect(lastSSX, y + 6 + iconOffset, (x * charWidth) - lastSSX, 2); } - public void drawSheetAnnot(Graphics g, Annotation[] row, + void drawSheetAnnot(Graphics g, Annotation[] row, int lastSSX, int x, int y, int iconOffset, int startRes, int column, boolean validRes, boolean validEnd) @@ -1101,7 +1102,7 @@ public class AnnotationRenderer } - public void drawHelixAnnot(Graphics g, Annotation[] row, int lastSSX, + void drawHelixAnnot(Graphics g, Annotation[] row, int lastSSX, int x, int y, int iconOffset, int startRes, int column, boolean validRes, boolean validEnd) { @@ -1161,7 +1162,7 @@ public class AnnotationRenderer g.fillRect(x1, y + 4 + iconOffset, x2 - x1, 8); } - public void drawLineGraph(Graphics g, AlignmentAnnotation _aa, + void drawLineGraph(Graphics g, AlignmentAnnotation _aa, Annotation[] aa_annotations, int sRes, int eRes, int y, float min, float max, int graphHeight) { @@ -1254,7 +1255,7 @@ public class AnnotationRenderer } } - public void drawBarGraph(Graphics g, AlignmentAnnotation _aa, + void drawBarGraph(Graphics g, AlignmentAnnotation _aa, Annotation[] aa_annotations, int sRes, int eRes, float min, float max, int y, boolean renderHistogram, boolean renderProfile, boolean normaliseProfile) @@ -1385,8 +1386,9 @@ public class AnnotationRenderer scl = htn * scale * profl[c++]; lm = ofont.getLineMetrics(dc, 0, 1, g.getFontMetrics() .getFontRenderContext()); - g.setFont(ofont.deriveFont(AffineTransform.getScaleInstance( - wdth, scl / lm.getAscent()))); + Font font = ofont.deriveFont(AffineTransform.getScaleInstance( + wdth, scl / lm.getAscent())); + g.setFont(font); lm = g.getFontMetrics().getLineMetrics(dc, 0, 1, g); // Debug - render boxes around characters diff --git a/src/jalview/schemes/ResidueProperties.java b/src/jalview/schemes/ResidueProperties.java index 2aa24a1..d0d26b0 100755 --- a/src/jalview/schemes/ResidueProperties.java +++ b/src/jalview/schemes/ResidueProperties.java @@ -1603,87 +1603,6 @@ public class ResidueProperties return ss.toString(); } - /** - * Used by getRNASecStrucState - * - */ - public static Hashtable toRNAssState; - - public static boolean RNAcloseParen[] = new boolean[255]; - static - { - toRNAssState = new Hashtable(); - toRNAssState.put(")", "("); - toRNAssState.put("(", "("); - toRNAssState.put("]", "["); - toRNAssState.put("[", "["); - toRNAssState.put("{", "{"); - toRNAssState.put("}", "{"); - toRNAssState.put(">", ">"); - toRNAssState.put("<", ">"); - toRNAssState.put("A", "A"); - toRNAssState.put("a", "A"); - toRNAssState.put("B", "B"); - toRNAssState.put("b", "B"); - toRNAssState.put("C", "C"); - toRNAssState.put("c", "C"); - toRNAssState.put("D", "D"); - toRNAssState.put("d", "D"); - toRNAssState.put("E", "E"); - toRNAssState.put("e", "E"); - toRNAssState.put("F", "F"); - toRNAssState.put("f", "F"); - toRNAssState.put("G", "G"); - toRNAssState.put("g", "G"); - toRNAssState.put("H", "H"); - toRNAssState.put("h", "H"); - toRNAssState.put("I", "I"); - toRNAssState.put("i", "I"); - toRNAssState.put("J", "J"); - toRNAssState.put("j", "J"); - toRNAssState.put("K", "K"); - toRNAssState.put("k", "K"); - toRNAssState.put("L", "L"); - toRNAssState.put("l", "L"); - toRNAssState.put("M", "M"); - toRNAssState.put("m", "M"); - toRNAssState.put("N", "N"); - toRNAssState.put("n", "N"); - toRNAssState.put("O", "O"); - toRNAssState.put("o", "O"); - toRNAssState.put("P", "P"); - toRNAssState.put("p", "P"); - toRNAssState.put("Q", "Q"); - toRNAssState.put("q", "Q"); - toRNAssState.put("R", "R"); - toRNAssState.put("r", "R"); - toRNAssState.put("S", "S"); - toRNAssState.put("s", "S"); - toRNAssState.put("T", "T"); - toRNAssState.put("t", "T"); - toRNAssState.put("U", "U"); - toRNAssState.put("u", "U"); - toRNAssState.put("V", "V"); - toRNAssState.put("v", "V"); - toRNAssState.put("W", "W"); - toRNAssState.put("w", "W"); - toRNAssState.put("X", "X"); - toRNAssState.put("x", "X"); - toRNAssState.put("Y", "Y"); - toRNAssState.put("y", "Y"); - toRNAssState.put("Z", "Z"); - toRNAssState.put("z", "Z"); - for (int p = 0; p < RNAcloseParen.length; p++) - { - RNAcloseParen[p] = false; - } - for (String k : toRNAssState.keySet()) - { - RNAcloseParen[k.charAt(0)] = k.charAt(0) != toRNAssState.get(k) - .charAt(0); - } - } - static { modifications.put("MSE", "MET"); // Selenomethionine @@ -3008,40 +2927,6 @@ public class ResidueProperties return canonical == null ? aa : canonical; } - /** - * translate to RNA secondary structure representation - * - * @param ssstring - * @return ssstring as a RNA-state secondary structure assignment. - */ - public static String getRNASecStrucState(String ssstring) - { - if (ssstring == null) - { - return null; - } - StringBuffer ss = new StringBuffer(); - for (int i = 0; i < ssstring.length(); i++) - { - String ssc = ssstring.substring(i, i + 1); - if (toRNAssState.containsKey(ssc)) - { - // valid ss character - so return it - ss.append(ssc); // (String) toRNAssState.get(ssc)); - } - else - { - ss.append(" "); - } - } - return ss.toString(); - } - - public static boolean isCloseParenRNA(char dc) - { - return RNAcloseParen[dc]; - } - // main method generates perl representation of residue property hash // / cut here public static void main(String[] args) -- 1.7.10.2