X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fio%2FStockholmFile.java;h=f7a2df257a2f911580651ae4f7a48153fee5a8ce;hb=1beac3545a78d4c5c3274dbb53296708d693efe0;hp=ead653f875b5d9668d9751d160d19ba0356b9540;hpb=a30d9ff391d01301e9733d386d2346bfc3e953d3;p=jalview.git diff --git a/src/jalview/io/StockholmFile.java b/src/jalview/io/StockholmFile.java index ead653f..f7a2df2 100644 --- a/src/jalview/io/StockholmFile.java +++ b/src/jalview/io/StockholmFile.java @@ -74,23 +74,38 @@ import fr.orsay.lri.varna.models.rna.RNA; */ public class StockholmFile extends AlignFile { + private static final String ANNOTATION = "annotation"; + + private static final char UNDERSCORE = '_'; + private static final Regex OPEN_PAREN = new Regex("(<|\\[)", "("); private static final Regex CLOSE_PAREN = new Regex("(>|\\])", ")"); + // private static final Regex OPEN_PAREN = new Regex("(<|\\[)", "("); + // private static final Regex CLOSE_PAREN = new Regex("(>|\\])", ")"); + public static final Regex DETECT_BRACKETS = new Regex( "(<|>|\\[|\\]|\\(|\\)|\\{|\\})"); + // WUSS extended symbols. Avoid ambiguity with protein SS annotations by using NOT_RNASS first. + public static final String RNASS_BRACKETS = "<>[](){}AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz"; + + // use the following regex to decide an annotations (whole) line is NOT an RNA + // SS (it contains only E,H,e,h and other non-brace/non-alpha chars) + private static final Regex NOT_RNASS = new Regex( + "^[^<>[\\](){}A-DF-Za-df-z]*$"); + StringBuffer out; // output buffer - AlignmentI al; + private AlignmentI al; public StockholmFile() { } /** - * Creates a new StockholmFile object for output. + * Creates a new StockholmFile object for output */ public StockholmFile(AlignmentI al) { @@ -163,8 +178,8 @@ public class StockholmFile extends AlignFile for (int k = 0; k < rna.length(); k++) { - ann[k] = new Annotation(annot[k], "", Rna.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.", @@ -207,9 +222,8 @@ public class StockholmFile extends AlignFile r = new Regex("# STOCKHOLM ([\\d\\.]+)"); if (!r.search(nextLine())) { - throw new IOException( - MessageManager - .getString("exception.stockholm_invalid_format")); + throw new IOException(MessageManager + .getString("exception.stockholm_invalid_format")); } else { @@ -218,7 +232,7 @@ public class StockholmFile extends AlignFile // logger.debug("Stockholm version: " + version); } - // We define some Regexes here that will be used regularily later + // We define some Regexes here that will be used regularly later rend = new Regex("^\\s*\\/\\/"); // Find the end of an alignment p = new Regex("(\\S+)\\/(\\d+)\\-(\\d+)"); // split sequence id in // id/from/to @@ -392,7 +406,7 @@ public class StockholmFile extends AlignFile while (j.hasMoreElements()) { String desc = j.nextElement().toString(); - if ("annotations".equals(desc) && annotsAdded) + if (ANNOTATION.equals(desc) && annotsAdded) { // don't add features if we already added an annotation row continue; @@ -412,7 +426,7 @@ public class StockholmFile extends AlignFile int new_pos = posmap[k]; // look up nearest seqeunce // position to this column SequenceFeature feat = new SequenceFeature(type, desc, - new_pos, new_pos, 0f, null); + new_pos, new_pos, null); seqO.addSequenceFeature(feat); } @@ -439,8 +453,8 @@ public class StockholmFile extends AlignFile { // logger.error("Could not parse sequence line: " + line); throw new IOException(MessageManager.formatMessage( - "exception.couldnt_parse_sequence_line", - new String[] { line })); + "exception.couldnt_parse_sequence_line", new String[] + { line })); } String ns = seqs.get(x.stringMatched(1)); if (ns == null) @@ -626,16 +640,17 @@ public class StockholmFile extends AlignFile if (features.containsKey(this.id2type(type))) { // logger.debug("Found content for " + this.id2type(type)); - content = (Hashtable) features.get(this.id2type(type)); + content = (Hashtable) features + .get(this.id2type(type)); } else { // logger.debug("Creating new content holder for " + // this.id2type(type)); content = new Hashtable(); - features.put(this.id2type(type), content); + features.put(id2type(type), content); } - String ns = (String) content.get("annotation"); + String ns = (String) content.get(ANNOTATION); if (ns == null) { @@ -643,7 +658,7 @@ public class StockholmFile extends AlignFile } // finally, append the annotation line ns += seq; - content.put("annotation", ns); + content.put(ANNOTATION, ns); // // end of wrapped annotation block. // // Now a new row is created with the current set of data @@ -671,8 +686,8 @@ public class StockholmFile extends AlignFile // } else { - System.err - .println("Warning - couldn't parse sequence annotation row line:\n" + System.err.println( + "Warning - couldn't parse sequence annotation row line:\n" + line); // throw new IOException("Error parsing " + line); } @@ -680,8 +695,8 @@ public class StockholmFile extends AlignFile else { throw new IOException(MessageManager.formatMessage( - "exception.unknown_annotation_detected", new String[] { - annType, annContent })); + "exception.unknown_annotation_detected", new String[] + { annType, annContent })); } } } @@ -794,8 +809,10 @@ public class StockholmFile extends AlignFile { for (DBRefEntry d : dbrs) { - jalview.util.MapList mp = new jalview.util.MapList(new int[] { - seqO.getStart(), seqO.getEnd() }, new int[] { st, en }, 1, 1); + jalview.util.MapList mp = new jalview.util.MapList( + new int[] + { seqO.getStart(), seqO.getEnd() }, new int[] { st, en }, 1, + 1); jalview.datamodel.Mapping mping = new Mapping(mp); d.setMap(mping); } @@ -806,23 +823,29 @@ public class StockholmFile extends AlignFile Vector annotation, String label, String annots) { - String convert1, convert2 = null; - - // convert1 = OPEN_PAREN.replaceAll(annots); - // convert2 = CLOSE_PAREN.replaceAll(convert1); + String convert1, convert2 = null; + // String convert1 = OPEN_PAREN.replaceAll(annots); + // String convert2 = CLOSE_PAREN.replaceAll(convert1); // annots = convert2; String type = label; if (label.contains("_cons")) { - type = (label.indexOf("_cons") == label.length() - 5) ? label - .substring(0, label.length() - 5) : label; + type = (label.indexOf("_cons") == label.length() - 5) + ? label.substring(0, label.length() - 5) + : label; } boolean ss = false, posterior = false; type = id2type(type); + + boolean isrnass = false; + if (type.equalsIgnoreCase("secondary structure")) { ss = true; + isrnass = !NOT_RNASS.search(annots); // sorry about the double negative + // here (it's easier for dealing with + // other non-alpha-non-brace chars) } if (type.equalsIgnoreCase("posterior probability")) { @@ -833,6 +856,10 @@ public class StockholmFile extends AlignFile for (int i = 0; i < annots.length(); i++) { String pos = annots.substring(i, i + 1); + if (UNDERSCORE == pos.charAt(0)) + { + pos = " "; + } Annotation ann; ann = new Annotation(pos, "", ' ', 0f); // 0f is 'valid' null - will not // be written out @@ -840,7 +867,7 @@ public class StockholmFile extends AlignFile { // if (" .-_".indexOf(pos) == -1) { - if (DETECT_BRACKETS.search(pos)) + if (isrnass && RNASS_BRACKETS.indexOf(pos) >= 0) { ann.secondaryStructure = Rna.getRNASecStrucState(pos).charAt(0); ann.displayCharacter = "" + pos.charAt(0); @@ -850,14 +877,14 @@ public class StockholmFile extends AlignFile ann.secondaryStructure = ResidueProperties.getDssp3state(pos) .charAt(0); - if (ann.secondaryStructure == pos.charAt(0)) - { - ann.displayCharacter = ""; // null; // " "; - } - else - { - ann.displayCharacter = " " + ann.displayCharacter; - } + if (ann.secondaryStructure == pos.charAt(0)) + { + ann.displayCharacter = ""; // null; // " "; + } + else + { + ann.displayCharacter = " " + ann.displayCharacter; + } } } @@ -928,10 +955,7 @@ public class StockholmFile extends AlignFile while ((in < s.length) && (s[in] != null)) { String tmp = printId(s[in], jvSuffix); - if (s[in].getSequence().length > max) - { - max = s[in].getSequence().length; - } + max = Math.max(max, s[in].getLength()); if (tmp.length() > maxid) { @@ -980,8 +1004,8 @@ public class StockholmFile extends AlignFile { Object idd = en.nextElement(); String type = (String) dataRef.remove(idd); - out.append(new Format("%-" + (maxid - 2) + "s").form("#=GS " - + idd.toString() + " ")); + out.append(new Format("%-" + (maxid - 2) + "s") + .form("#=GS " + idd.toString() + " ")); if (type.contains("PFAM") || type.contains("RFAM")) { @@ -1002,41 +1026,40 @@ public class StockholmFile extends AlignFile if (alAnot != null) { Annotation[] ann; + for (int j = 0; j < alAnot.length; j++) { - - String key = type2id(alAnot[j].label); - boolean isrna = alAnot[j].isValidStruc(); - - if (isrna) - { - // hardwire to secondary structure if there is RNA secondary - // structure on the annotation - key = "SS"; - } - if (key == null) + if (alAnot[j].annotations != null) { + String key = type2id(alAnot[j].label); + boolean isrna = alAnot[j].isValidStruc(); - continue; - } + if (isrna) + { + // hardwire to secondary structure if there is RNA secondary + // structure on the annotation + key = "SS"; + } + if (key == null) + { - // out.append("#=GR "); - out.append(new Format("%-" + maxid + "s").form("#=GR " - + printId(s[i], jvSuffix) + " " + key + " ")); - ann = alAnot[j].annotations; - String seq = ""; - for (int k = 0; k < ann.length; k++) - { - char character = outputCharacter(key, k, isrna, ann, s[i]); - if (character == ' ') + continue; + } + + // out.append("#=GR "); + out.append(new Format("%-" + maxid + "s").form( + "#=GR " + printId(s[i], jvSuffix) + " " + key + " ")); + ann = alAnot[j].annotations; + String seq = ""; + for (int k = 0; k < ann.length; k++) { - character = '.'; + seq += outputCharacter(key, k, isrna, ann, s[i]); } - seq += character; + out.append(seq); + out.append(newline); } - out.append(seq); - out.append(newline); } + } out.append(new Format("%-" + maxid + "s") @@ -1082,8 +1105,8 @@ public class StockholmFile extends AlignFile } label = label.replace(" ", "_"); - out.append(new Format("%-" + maxid + "s").form("#=GC " + label - + " ")); + out.append( + new Format("%-" + maxid + "s").form("#=GC " + label + " ")); boolean isrna = aa.isValidStruc(); for (int j = 0; j < aa.annotations.length; j++) { @@ -1100,6 +1123,7 @@ public class StockholmFile extends AlignFile return out.toString(); } + /** * add an annotation character to the output row * @@ -1115,23 +1139,39 @@ public class StockholmFile extends AlignFile { char seq = ' '; Annotation annot = ann[k]; - String ch = (annot == null) ? ((sequenceI == null) ? "-" : Character - .toString(sequenceI.getCharAt(k))) : annot.displayCharacter; + String ch = (annot == null) + ? ((sequenceI == null) ? "-" + : Character.toString(sequenceI.getCharAt(k))) + : (annot.displayCharacter == null + ? String.valueOf(annot.secondaryStructure) + : annot.displayCharacter); + if (ch == null) + { + ch = " "; + } if (key != null && key.equals("SS")) { + char ssannotchar = ' '; + boolean charset = false; if (annot == null) { // sensible gap character - return ' '; + ssannotchar = ' '; + charset = true; } else { // valid secondary structure AND no alternative label (e.g. ' B') if (annot.secondaryStructure > ' ' && ch.length() < 2) { - return annot.secondaryStructure; + ssannotchar = annot.secondaryStructure; + charset = true; } } + if (charset) + { + return (ssannotchar == ' ' && isrna) ? '.' : ssannotchar; + } } if (ch.length() == 0) @@ -1146,9 +1186,31 @@ public class StockholmFile extends AlignFile { seq = ch.charAt(1); } - return seq; + + return (seq == ' ' && key != null && key.equals("SS") && isrna) ? '.' + : seq; } + /** + * make a friendly ID string. + * + * @param dataName + * @return truncated dataName to after last '/' + */ + private String safeName(String dataName) + { + int b = 0; + while ((b = dataName.indexOf("/")) > -1 && b < dataName.length()) + { + dataName = dataName.substring(b + 1).trim(); + + } + int e = (dataName.length() - dataName.indexOf(".")) + 1; + dataName = dataName.substring(1, e).trim(); + return dataName; + } + + public String print() { out = new StringBuffer(); @@ -1156,6 +1218,7 @@ public class StockholmFile extends AlignFile out.append(newline); print(getSeqsAsArray(), false); + out.append("//"); out.append(newline); return out.toString(); } @@ -1185,15 +1248,15 @@ public class StockholmFile extends AlignFile } } - + protected static String id2type(String id) { if (typeIds.containsKey(id)) { return (String) typeIds.get(id); } - System.err.println("Warning : Unknown Stockholm annotation type code " - + id); + System.err.println( + "Warning : Unknown Stockholm annotation type code " + id); return id; } @@ -1214,27 +1277,8 @@ public class StockholmFile extends AlignFile { return key; } - System.err.println("Warning : Unknown Stockholm annotation type: " - + type); + System.err.println( + "Warning : Unknown Stockholm annotation type: " + type); return key; } - - /** - * make a friendly ID string. - * - * @param dataName - * @return truncated dataName to after last '/' - */ - private String safeName(String dataName) - { - int b = 0; - while ((b = dataName.indexOf("/")) > -1 && b < dataName.length()) - { - dataName = dataName.substring(b + 1).trim(); - - } - int e = (dataName.length() - dataName.indexOf(".")) + 1; - dataName = dataName.substring(1, e).trim(); - return dataName; - } }