X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fio%2FAnnotationFile.java;h=ce1afc274bf08f745c6fabf3882aa7ff8ec8279f;hb=1660a4fe03b8dda74b008ae4de2d804f8044a463;hp=7f71df18f9bb06686b879766426cde6203dd9dec;hpb=67b2671b1ded591a6f8eb7f13d319e58f993df77;p=jalview.git diff --git a/src/jalview/io/AnnotationFile.java b/src/jalview/io/AnnotationFile.java index 7f71df1..ce1afc2 100755 --- a/src/jalview/io/AnnotationFile.java +++ b/src/jalview/io/AnnotationFile.java @@ -60,8 +60,9 @@ public class AnnotationFile public ColumnSelection hiddencols; public Vector visibleGroups; + public Hashtable hiddenRepSeqs; - + public ViewDef(String viewname, HiddenSequences hidseqs, ColumnSelection hiddencols, Hashtable hiddenRepSeqs) { @@ -71,10 +72,18 @@ public class AnnotationFile this.hiddenRepSeqs = hiddenRepSeqs; } } - + /** + * Prepare an annotation file given a set of annotations, groups, alignment properties and views. + * @param annotations + * @param groups + * @param properties + * @param views + * @return annotation file + */ public String printAnnotations(AlignmentAnnotation[] annotations, Vector groups, Hashtable properties, ViewDef[] views) { + // TODO: resolve views issue : annotationFile could contain visible region, or full data + hidden region specifications for a view. if (annotations != null) { boolean oneColour = true; @@ -84,7 +93,7 @@ public class AnnotationFile StringBuffer colours = new StringBuffer(); StringBuffer graphLine = new StringBuffer(); - + StringBuffer rowprops = new StringBuffer(); Hashtable graphGroup = new Hashtable(); java.awt.Color color; @@ -116,19 +125,43 @@ public class AnnotationFile refSeq = row.sequenceRef; text.append("\nSEQUENCE_REF\t" + refSeq.getName() + "\n"); } + boolean hasGlyphs = row.hasIcons, hasLabels = row.hasText, hasValues = row.hasScore, hasText = false; + // lookahead to check what the annotation row object actually contains. + for (int j = 0; row.annotations != null + && j < row.annotations.length + && (!hasGlyphs || !hasLabels || !hasValues); j++) + { + if (row.annotations[j] != null) + { + hasLabels |= (row.annotations[j].displayCharacter != null + && row.annotations[j].displayCharacter.length() > 0 && !row.annotations[j].displayCharacter + .equals(" ")); + hasGlyphs |= (row.annotations[j].secondaryStructure != 0 && row.annotations[j].secondaryStructure != ' '); + hasValues |= (row.annotations[j].value != Float.NaN); // NaNs can't + // be + // rendered.. + hasText |= (row.annotations[j].description != null && row.annotations[j].description + .length() > 0); + } + } if (row.graph == AlignmentAnnotation.NO_GRAPH) { text.append("NO_GRAPH\t"); + hasValues = false; // only secondary structure + // hasLabels = false; // and annotation description string. } else { if (row.graph == AlignmentAnnotation.BAR_GRAPH) { text.append("BAR_GRAPH\t"); + hasGlyphs = false; // no secondary structure + } else if (row.graph == AlignmentAnnotation.LINE_GRAPH) { + hasGlyphs = false; // no secondary structure text.append("LINE_GRAPH\t"); } @@ -165,7 +198,6 @@ public class AnnotationFile { text.append(row.description + "\t"); } - for (int j = 0; row.annotations != null && j < row.annotations.length; j++) { @@ -178,33 +210,54 @@ public class AnnotationFile if (row.annotations[j] != null) { comma = ""; - if (row.annotations[j].secondaryStructure != ' ') + if (row.annotations[j].secondaryStructure != ' ') // could be also hasGlyphs || ... { + text.append(comma + row.annotations[j].secondaryStructure); comma = ","; } - if (row.annotations[j].displayCharacter != null - && row.annotations[j].displayCharacter.length() > 0 - && !row.annotations[j].displayCharacter.equals(" ")) + if (hasValues) { - text.append(comma + row.annotations[j].displayCharacter); + if (row.annotations[j].value != Float.NaN) + { + text.append(comma + row.annotations[j].value); + } + else + { + System.err.println("Skipping NaN - not valid value."); + text.append(comma + 0f);// row.annotations[j].value); + } comma = ","; } - - if (row.annotations[j] != null) + if (hasLabels) { - if (color != null && !color.equals(row.annotations[j].colour)) + // TODO: labels are emitted after values for bar graphs. + if // empty labels are allowed, so + (row.annotations[j].displayCharacter != null + && row.annotations[j].displayCharacter.length() > 0 + && !row.annotations[j].displayCharacter.equals(" ")) { - oneColour = false; + text.append(comma + row.annotations[j].displayCharacter); + comma = ","; } - - color = row.annotations[j].colour; - if (row.annotations[j].value != 0f - && row.annotations[j].value != Float.NaN) + } + if (hasText) + { + if (row.annotations[j].description != null + && row.annotations[j].description.length() > 0 + && !row.annotations[j].description + .equals(row.annotations[j].displayCharacter)) { - text.append(comma + row.annotations[j].value); + text.append(comma + row.annotations[j].description); + comma = ","; } } + if (color != null && !color.equals(row.annotations[j].colour)) + { + oneColour = false; + } + + color = row.annotations[j].colour; if (row.annotations[j].colour != null && row.annotations[j].colour != java.awt.Color.black) @@ -214,6 +267,7 @@ public class AnnotationFile + jalview.util.Format .getHexString(row.annotations[j].colour) + "]"); + comma = ","; } } text.append("|"); @@ -229,7 +283,14 @@ public class AnnotationFile colours.append("COLOUR\t" + row.label + "\t" + jalview.util.Format.getHexString(color) + "\n"); } - + if (row.scaleColLabel || row.showAllColLabels || row.centreColLabels) + { + rowprops.append("ROWPROPERTIES\t"+row.label); + rowprops.append("\tscaletofit="+row.scaleColLabel); + rowprops.append("\tshowalllabs="+row.showAllColLabels); + rowprops.append("\tcentrelabs="+row.centreColLabels); + rowprops.append("\n"); + } } text.append("\n"); @@ -245,6 +306,7 @@ public class AnnotationFile text.append(en.nextElement() + "\n"); } } + text.append(rowprops.toString()); } if (groups != null) @@ -271,22 +333,24 @@ public class AnnotationFile public void printGroups(Vector sequenceGroups) { SequenceGroup sg; - SequenceI seqrep=null; + SequenceI seqrep = null; for (int i = 0; i < sequenceGroups.size(); i++) { sg = (SequenceGroup) sequenceGroups.elementAt(i); if (!sg.hasSeqrep()) { text.append("SEQUENCE_GROUP\t" + sg.getName() + "\t" - + (sg.getStartRes() + 1) + "\t" + (sg.getEndRes() + 1) + "\t" - + "-1\t"); + + (sg.getStartRes() + 1) + "\t" + (sg.getEndRes() + 1) + + "\t" + "-1\t"); seqrep = null; - } else { + } + else + { seqrep = sg.getSeqrep(); - text.append("SEQUENCE_REF\t"+seqrep.getName()+"\n"); + text.append("SEQUENCE_REF\t" + seqrep.getName() + "\n"); text.append("SEQUENCE_GROUP\t" + sg.getName() + "\t" - + (seqrep.findPosition(sg.getStartRes())) + "\t" + (seqrep.findPosition(sg.getEndRes())) + "\t" - + "-1\t"); + + (seqrep.findPosition(sg.getStartRes())) + "\t" + + (seqrep.findPosition(sg.getEndRes())) + "\t" + "-1\t"); } for (int s = 0; s < sg.getSize(); s++) { @@ -319,7 +383,7 @@ public class AnnotationFile text.append("displayBoxes=" + sg.getDisplayBoxes() + "\t"); text.append("displayText=" + sg.getDisplayText() + "\t"); text.append("colourText=" + sg.getColourText() + "\t"); - text.append("showUnconserved="+sg.getShowunconserved()+"\t"); + text.append("showUnconserved=" + sg.getShowunconserved() + "\t"); if (sg.textColour != java.awt.Color.black) { text.append("textCol1=" @@ -332,7 +396,7 @@ public class AnnotationFile } if (sg.thresholdTextColour != 0) { - text.append("textColThreshold=" + sg.thresholdTextColour+"\t"); + text.append("textColThreshold=" + sg.thresholdTextColour + "\t"); } if (sg.idColour != null) { @@ -347,7 +411,7 @@ public class AnnotationFile { text.append("hidecols=true\t"); } - if (seqrep!=null) + if (seqrep != null) { // terminate the last line and clear the sequence ref for the group text.append("\nSEQUENCE_REF"); @@ -449,7 +513,11 @@ public class AnnotationFile combineAnnotations(al, st); continue; } - + else if (token.equalsIgnoreCase("ROWPROPERTIES")) + { + addRowProperties(al, st); + continue; + } else if (token.equalsIgnoreCase("GRAPHLINE")) { addLine(al, st); @@ -566,7 +634,7 @@ public class AnnotationFile } else { - annotations[index++] = parseAnnotation(token); + annotations[index++] = parseAnnotation(token, graphStyle); emptyColumn = false; } } @@ -589,7 +657,8 @@ public class AnnotationFile // TODO: verify that undo/redo with 1:many sequence associated // annotations can be undone correctly AlignmentAnnotation ann = new AlignmentAnnotation(annotation); - annotation.createSequenceMapping(referedSeq, refSeqIndex, false); + annotation + .createSequenceMapping(referedSeq, refSeqIndex, false); annotation.adjustForAlignment(); referedSeq.addAlignmentAnnotation(annotation); al.addAnnotation(annotation); @@ -620,12 +689,13 @@ public class AnnotationFile return true; } - Annotation parseAnnotation(String string) + Annotation parseAnnotation(String string, int graphStyle) { + boolean hasSymbols = (graphStyle == AlignmentAnnotation.NO_GRAPH); // don't do the glyph test if we don't want secondary structure String desc = null, displayChar = null; char ss = ' '; // secondaryStructure float value = 0; - boolean parsedValue = false; + boolean parsedValue = false, dcset = false; // find colour here java.awt.Color colour = null; @@ -636,25 +706,40 @@ public class AnnotationFile UserColourScheme ucs = new UserColourScheme(); colour = ucs.getColourFromString(string.substring(i + 1, j)); - + if ( i>0 && string.charAt(i-1)==',') + { + // clip the preceding comma as well + i--; + } string = string.substring(0, i) + string.substring(j + 1); } - StringTokenizer st = new StringTokenizer(string, ","); + StringTokenizer st = new StringTokenizer(string, ",", true); String token; + boolean seenContent=false; while (st.hasMoreTokens()) { token = st.nextToken().trim(); - if (token.length() == 0) + if (token.equals(",")) { + if (!seenContent && parsedValue && !dcset) + { + // allow the value below the bar/line to be empty + dcset = true; + displayChar = " "; + } + seenContent = false; continue; + } else { + seenContent = true; } if (!parsedValue) { try { - displayChar = token; + displayChar = token; + // foo value = new Float(token).floatValue(); parsedValue = true; continue; @@ -663,7 +748,7 @@ public class AnnotationFile } } - if (token.equals("H") || token.equals("E")) + if (hasSymbols && (token.equals("H") || token.equals("E") || token.equals(" "))) { // Either this character represents a helix or sheet // or an integer which can be displayed @@ -679,7 +764,10 @@ public class AnnotationFile } } - +// if (!dcset && string.charAt(string.length() - 1) == ',') +// { +// displayChar = " "; // empty display char symbol. +// } if (displayChar != null && displayChar.length() > 1 && desc != null && desc.length() == 1) { @@ -779,32 +867,40 @@ public class AnnotationFile annotation.setThreshold(new GraphLine(value, label, colour)); } + void addGroup(AlignmentI al, StringTokenizer st) { SequenceGroup sg = new SequenceGroup(); sg.setName(st.nextToken()); - String rng =""; - try { + String rng = ""; + try + { rng = st.nextToken(); - if (rng.length()>0 && !rng.startsWith("*")) + if (rng.length() > 0 && !rng.startsWith("*")) { sg.setStartRes(Integer.parseInt(rng) - 1); - } else { + } + else + { sg.setStartRes(0); } rng = st.nextToken(); - if (rng.length()>0 && !rng.startsWith("*")) + if (rng.length() > 0 && !rng.startsWith("*")) { sg.setEndRes(Integer.parseInt(rng) - 1); - } else { - sg.setEndRes(al.getWidth()-1); + } + else + { + sg.setEndRes(al.getWidth() - 1); } } catch (Exception e) { - System.err.println("Couldn't parse Group Start or End Field as '*' or a valid column or sequence index: '"+rng+"' - assuming alignment width for group."); + System.err + .println("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 sg.setStartRes(0); - sg.setEndRes(al.getWidth()-1); + sg.setEndRes(al.getWidth() - 1); } String index = st.nextToken(); @@ -866,6 +962,35 @@ public class AnnotationFile } } + void addRowProperties(AlignmentI al, StringTokenizer st) + { + String label = st.nextToken(),keyValue,key,value; + boolean scaletofit=false,centerlab=false,showalllabs=false; + while (st.hasMoreTokens()) { + keyValue=st.nextToken(); + key = keyValue.substring(0, keyValue.indexOf("=")); + value = keyValue.substring(keyValue.indexOf("=") + 1); + if (key.equalsIgnoreCase("scaletofit")) { + scaletofit = Boolean.valueOf(value).booleanValue(); + } + if (key.equalsIgnoreCase("showalllabs")) { + showalllabs = Boolean.valueOf(value).booleanValue(); + } + if (key.equalsIgnoreCase("centrelabs")) { + centerlab = Boolean.valueOf(value).booleanValue(); + } + AlignmentAnnotation[] alr = al.getAlignmentAnnotation(); + for (int i = 0; i < alr.length; i++) + { + if (alr[i].label.equalsIgnoreCase(label)) + { + alr[i].centreColLabels = centerlab; + alr[i].scaleColLabel = scaletofit; + alr[i].showAllColLabels = showalllabs; + } + } + } + } void addProperties(AlignmentI al, StringTokenizer st) {