X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fio%2FAnnotationFile.java;h=7e4ee16768623e1c8fc24783193f6e3635ad5fc4;hb=HEAD;hp=6b02022c61ac1ec1b9bc33577e0200889da48b7a;hpb=c945c9641de88cad01b09b3db19e45667dfe751e;p=jalview.git diff --git a/src/jalview/io/AnnotationFile.java b/src/jalview/io/AnnotationFile.java index 6b02022..7e4ee16 100755 --- a/src/jalview/io/AnnotationFile.java +++ b/src/jalview/io/AnnotationFile.java @@ -20,6 +20,18 @@ */ package jalview.io; +import java.awt.Color; +import java.io.BufferedReader; +import java.util.ArrayList; +import java.util.BitSet; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.Hashtable; +import java.util.List; +import java.util.Map; +import java.util.StringTokenizer; +import java.util.Vector; + import jalview.analysis.Conservation; import jalview.api.AlignViewportI; import jalview.datamodel.AlignmentAnnotation; @@ -35,51 +47,32 @@ import jalview.schemes.ColourSchemeI; import jalview.schemes.ColourSchemeProperty; import jalview.util.ColorUtils; -import java.awt.Color; -import java.io.BufferedReader; -import java.io.FileReader; -import java.io.InputStreamReader; -import java.io.StringReader; -import java.net.URL; -import java.util.ArrayList; -import java.util.BitSet; -import java.util.Enumeration; -import java.util.Hashtable; -import java.util.List; -import java.util.Map; -import java.util.StringTokenizer; -import java.util.Vector; - public class AnnotationFile { - public AnnotationFile() - { - init(); - } + private static final String GRAPHLINE = "GRAPHLINE"; + + private static final String COMBINE = "COMBINE"; - /** - * character used to write newlines - */ protected String newline = System.getProperty("line.separator"); + private StringBuffer text; + + private SequenceI refSeq = null; + + private String refSeqId = null; + + private long nlinesread = 0; + + private String lastread = ""; + /** - * set new line string and reset the output buffer - * - * @param nl + * Constructor */ - public void setNewlineString(String nl) + public AnnotationFile() { - newline = nl; init(); } - public String getNewlineString() - { - return newline; - } - - StringBuffer text; - private void init() { text = new StringBuffer("JALVIEW_ANNOTATION" + newline + "# Created: " @@ -274,7 +267,7 @@ public class AnnotationFile if (row.graphGroup > -1) { graphGroupSeen.set(row.graphGroup); - Integer key = new Integer(row.graphGroup); + Integer key = Integer.valueOf(row.graphGroup); if (graphGroup.containsKey(key)) { graphGroup.put(key, graphGroup.get(key) + "\t" + row.label); @@ -324,7 +317,8 @@ public class AnnotationFile } else { - // System.err.println("Skipping NaN - not valid value."); + // jalview.bin.Console.errPrintln("Skipping NaN - not valid + // value."); text.append(comma + 0f);// row.annotations[j].value); } comma = ","; @@ -642,10 +636,6 @@ public class AnnotationFile } } - SequenceI refSeq = null; - - String refSeqId = null; - public boolean annotateAlignmentView(AlignViewportI viewport, Object file, DataSourceType protocol) { @@ -682,52 +672,26 @@ public class AnnotationFile BufferedReader in = null; try { - if (sourceType == DataSourceType.FILE) - { - in = FileLoader.getBuffereReader(file); - } - else if (sourceType == DataSourceType.URL) - { - URL url = new URL(file.toString()); - in = new BufferedReader(new InputStreamReader(url.openStream())); - } - else if (sourceType == DataSourceType.PASTE) - { - in = new BufferedReader(new StringReader(file.toString())); - } - else if (sourceType == DataSourceType.CLASSLOADER) - { - java.io.InputStream is = getClass().getResourceAsStream("/" + file); - if (is != null) - { - in = new BufferedReader(new java.io.InputStreamReader(is)); - } - } + in = new FileParse().getBufferedReader(file, sourceType); if (in != null) { return parseAnnotationFrom(al, hidden, in); } - } catch (Exception ex) { ex.printStackTrace(); - System.out.println("Problem reading annotation file: " + ex); + jalview.bin.Console + .outPrintln("Problem reading annotation file: " + ex); if (nlinesread > 0) { - System.out.println("Last read line " + nlinesread + ": '" + lastread - + "' (first 80 chars) ..."); + jalview.bin.Console.outPrintln("Last read line " + nlinesread + + ": '" + lastread + "' (first 80 chars) ..."); } return false; } return false; } - long nlinesread = 0; - - String lastread = ""; - - private static String GRAPHLINE = "GRAPHLINE", COMBINE = "COMBINE"; - public boolean parseAnnotationFrom(AlignmentI al, HiddenColumns hidden, BufferedReader in) throws Exception { @@ -762,7 +726,7 @@ public class AnnotationFile autoAnnotsKey(aa[aai], aa[aai].sequenceRef, (aa[aai].groupRef == null ? null : aa[aai].groupRef.getName())), - new Integer(1)); + Integer.valueOf(1)); } } } @@ -859,7 +823,7 @@ public class AnnotationFile if (refSeqIndex < 1) { refSeqIndex = 1; - System.out.println( + jalview.bin.Console.outPrintln( "WARNING: SEQUENCE_REF index must be > 0 in AnnotationFile"); } } catch (Exception ex) @@ -961,7 +925,7 @@ public class AnnotationFile { if (hidden == null) { - System.err.println( + jalview.bin.Console.errPrintln( "Cannot process HIDE_INSERTIONS without an alignment view: Ignoring line: " + line); } @@ -1113,7 +1077,7 @@ public class AnnotationFile { // TODO: specify and implement duplication of alignment annotation // for multiple group references. - System.err.println( + jalview.bin.Console.errPrintln( "Ignoring 1:many group reference mappings for group name '" + groupRef + "'"); } @@ -1271,7 +1235,7 @@ public class AnnotationFile { displayChar = token; // foo - value = new Float(token).floatValue(); + value = Float.valueOf(token).floatValue(); parsedValue = true; continue; } catch (NumberFormatException ex) @@ -1406,7 +1370,7 @@ public class AnnotationFile } else { - System.err.println( + jalview.bin.Console.errPrintln( "Couldn't combine annotations. None are added to alignment yet!"); } } @@ -1423,8 +1387,8 @@ public class AnnotationFile value = Float.valueOf(nextToken); } catch (NumberFormatException e) { - System.err.println("line " + nlinesread + ": Threshold '" + nextToken - + "' invalid, setting to zero"); + jalview.bin.Console.errPrintln("line " + nlinesread + ": Threshold '" + + nextToken + "' invalid, setting to zero"); } String label = st.hasMoreTokens() ? st.nextToken() : null; Color colour = null; @@ -1473,7 +1437,7 @@ public class AnnotationFile } } catch (Exception e) { - System.err.println( + jalview.bin.Console.errPrintln( "Couldn't parse Group Start or End Field as '*' or a valid column or sequence index: '" + rng + "' - assuming alignment width for group."); // assume group is full width @@ -1588,108 +1552,120 @@ public class AnnotationFile } String name = st.nextToken(); - SequenceGroup sg = null; - for (SequenceGroup _sg : al.getGroups()) + + Map properties = new HashMap<>(); + while (st.hasMoreTokens()) { - if ((sg = _sg).getName().equals(name)) - { - break; - } - else + String keyValue = st.nextToken(); + String key = keyValue.substring(0, keyValue.indexOf("=")); + String value = keyValue.substring(keyValue.indexOf("=") + 1); + properties.put(key, value); + } + + for (SequenceGroup sg : al.getGroups()) + { + if (sg.getName().equals(name)) { - sg = null; + addProperties(sg, properties, al); } } + } - if (sg != null) + /** + * Helper method that applies any specified properties to a SequenceGroup + * + * @param sg + * @param properties + * @param al + */ + private void addProperties(SequenceGroup sg, + Map properties, AlignmentI al) + { + ColourSchemeI def = sg.getColourScheme(); + for (String key : properties.keySet()) { - String keyValue, key, value; - ColourSchemeI def = sg.getColourScheme(); - while (st.hasMoreTokens()) + String value = properties.get(key); + if (key.equalsIgnoreCase("description")) { - keyValue = st.nextToken(); - key = keyValue.substring(0, keyValue.indexOf("=")); - value = keyValue.substring(keyValue.indexOf("=") + 1); - - if (key.equalsIgnoreCase("description")) - { - sg.setDescription(value); - } - else if (key.equalsIgnoreCase("colour")) - { - sg.cs.setColourScheme( - ColourSchemeProperty.getColourScheme(al, value)); - } - else if (key.equalsIgnoreCase("pidThreshold")) - { - sg.cs.setThreshold(Integer.parseInt(value), true); + sg.setDescription(value); + } + else if (key.equalsIgnoreCase("colour")) + { + // TODO need to notify colourscheme of view reference once it is + // available + sg.cs.setColourScheme( + ColourSchemeProperty.getColourScheme(null, al, value)); + } + else if (key.equalsIgnoreCase("pidThreshold")) + { + sg.cs.setThreshold(Integer.parseInt(value), true); - } - else if (key.equalsIgnoreCase("consThreshold")) - { - sg.cs.setConservationInc(Integer.parseInt(value)); - Conservation c = new Conservation("Group", sg.getSequences(null), - sg.getStartRes(), sg.getEndRes() + 1); + } + else if (key.equalsIgnoreCase("consThreshold")) + { + sg.cs.setConservationInc(Integer.parseInt(value)); + Conservation c = new Conservation("Group", sg.getSequences(null), + sg.getStartRes(), sg.getEndRes() + 1); - c.calculate(); - c.verdict(false, 25); // TODO: refer to conservation percent threshold + c.calculate(); + c.verdict(false, 25); // TODO: refer to conservation percent threshold - sg.cs.setConservation(c); + sg.cs.setConservation(c); - } - else if (key.equalsIgnoreCase("outlineColour")) - { - sg.setOutlineColour(ColorUtils.parseColourString(value)); - } - else if (key.equalsIgnoreCase("displayBoxes")) - { - sg.setDisplayBoxes(Boolean.valueOf(value).booleanValue()); - } - else if (key.equalsIgnoreCase("showUnconserved")) - { - sg.setShowNonconserved(Boolean.valueOf(value).booleanValue()); - } - else if (key.equalsIgnoreCase("displayText")) - { - sg.setDisplayText(Boolean.valueOf(value).booleanValue()); - } - else if (key.equalsIgnoreCase("colourText")) - { - sg.setColourText(Boolean.valueOf(value).booleanValue()); - } - else if (key.equalsIgnoreCase("textCol1")) - { - sg.textColour = ColorUtils.parseColourString(value); - } - else if (key.equalsIgnoreCase("textCol2")) - { - sg.textColour2 = ColorUtils.parseColourString(value); - } - else if (key.equalsIgnoreCase("textColThreshold")) - { - sg.thresholdTextColour = Integer.parseInt(value); - } - else if (key.equalsIgnoreCase("idColour")) - { - Color idColour = ColorUtils.parseColourString(value); - sg.setIdColour(idColour == null ? Color.black : idColour); - } - else if (key.equalsIgnoreCase("hide")) - { - // see bug https://mantis.lifesci.dundee.ac.uk/view.php?id=25847 - sg.setHidereps(true); - } - else if (key.equalsIgnoreCase("hidecols")) - { - // see bug https://mantis.lifesci.dundee.ac.uk/view.php?id=25847 - sg.setHideCols(true); - } - sg.recalcConservation(); } - if (sg.getColourScheme() == null) + else if (key.equalsIgnoreCase("outlineColour")) + { + sg.setOutlineColour(ColorUtils.parseColourString(value)); + } + else if (key.equalsIgnoreCase("displayBoxes")) + { + sg.setDisplayBoxes(Boolean.valueOf(value).booleanValue()); + } + else if (key.equalsIgnoreCase("showUnconserved")) + { + sg.setShowNonconserved(Boolean.valueOf(value).booleanValue()); + } + else if (key.equalsIgnoreCase("displayText")) + { + sg.setDisplayText(Boolean.valueOf(value).booleanValue()); + } + else if (key.equalsIgnoreCase("colourText")) + { + sg.setColourText(Boolean.valueOf(value).booleanValue()); + } + else if (key.equalsIgnoreCase("textCol1")) + { + sg.textColour = ColorUtils.parseColourString(value); + } + else if (key.equalsIgnoreCase("textCol2")) + { + sg.textColour2 = ColorUtils.parseColourString(value); + } + else if (key.equalsIgnoreCase("textColThreshold")) + { + sg.thresholdTextColour = Integer.parseInt(value); + } + else if (key.equalsIgnoreCase("idColour")) + { + Color idColour = ColorUtils.parseColourString(value); + sg.setIdColour(idColour == null ? Color.black : idColour); + } + else if (key.equalsIgnoreCase("hide")) + { + // see bug https://mantis.lifesci.dundee.ac.uk/view.php?id=25847 + sg.setHidereps(true); + } + else if (key.equalsIgnoreCase("hidecols")) { - sg.setColourScheme(def); + // see bug https://mantis.lifesci.dundee.ac.uk/view.php?id=25847 + sg.setHideCols(true); } + sg.recalcConservation(); + } + + if (sg.getColourScheme() == null) + { + sg.setColourScheme(def); } }