X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fio%2FStockholmFile.java;fp=src%2Fjalview%2Fio%2FStockholmFile.java;h=a9b8d372c52b4ff8268745d2b005cc1c3949eac6;hb=ac1d5169b86af13d4f06152a09cb125309ddf76f;hp=71cc5c57b6089ee3585ffe80c9fe79ca0e1860b9;hpb=2bd43f464e725844b02a106c2d2f187c2c3655a6;p=jalview.git diff --git a/src/jalview/io/StockholmFile.java b/src/jalview/io/StockholmFile.java index 71cc5c5..a9b8d37 100644 --- a/src/jalview/io/StockholmFile.java +++ b/src/jalview/io/StockholmFile.java @@ -26,24 +26,21 @@ import jalview.datamodel.Sequence; import jalview.datamodel.SequenceFeature; import jalview.datamodel.SequenceI; -import java.io.*; -import java.util.*; - - -import javax.xml.parsers.ParserConfigurationException; - -import org.xml.sax.SAXException; +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Enumeration; +import java.util.Hashtable; +import java.util.StringTokenizer; +import java.util.Vector; import com.stevesoft.pat.Regex; -import fr.orsay.lri.varna.exceptions.ExceptionFileFormatOrSyntax; -import fr.orsay.lri.varna.exceptions.ExceptionLoadingFailed; -import fr.orsay.lri.varna.exceptions.ExceptionPermissionDenied; import fr.orsay.lri.varna.exceptions.ExceptionUnmatchedClosingParentheses; +import fr.orsay.lri.varna.factories.RNAFactory; import fr.orsay.lri.varna.models.rna.RNA; - - // import org.apache.log4j.*; /** @@ -62,18 +59,19 @@ public class StockholmFile extends AlignFile { // static Logger logger = Logger.getLogger("jalview.io.StockholmFile"); protected ArrayList result; + public String id; - + public StockholmFile() { } - public StockholmFile(String inFile, String type) throws Exception + public StockholmFile(String inFile, String type) throws IOException { super(inFile, type); } - public StockholmFile(FileParse source) throws Exception + public StockholmFile(FileParse source) throws IOException { super(source); } @@ -89,11 +87,9 @@ public class StockholmFile extends AlignFile * * @throws IOException * If there is an error with the input file - * @throws ExceptionUnmatchedClosingParentheses */ public void parse() throws IOException { - StringBuffer treeString = new StringBuffer(); String treeName = null; // --------------- Variable Definitions ------------------- @@ -544,8 +540,73 @@ public class StockholmFile extends AlignFile } } - protected static AlignmentAnnotation parseAnnotationRow(Vector annotation, - String label, String annots) + + /** + * Parse a file in Stockholm format into Jalview's data model using VARNA + * + * @throws IOException + * If there is an error with the input file + */ + public void parse_with_VARNA() throws IOException + { + FileReader fr = null; + fr = new FileReader(inFile); + + BufferedReader r = new BufferedReader(fr); + result = null; + try + { + result = RNAFactory.loadSecStrStockholm(r); + } catch (ExceptionUnmatchedClosingParentheses umcp) + { + errormessage = "Unmatched parentheses in annotation. Aborting (" + + umcp.getMessage() + ")"; + throw new IOException(umcp); + } + // DEBUG System.out.println("this is the secondary scructure:" + // +result.size()); + SequenceI[] seqs = new SequenceI[result.size()]; + for (int i = 0; i < result.size(); i++) + { + // DEBUG System.err.println("Processing i'th sequence in Stockholm file") + RNA current = result.get(i); + + String seq = current.getSeq(); + String rna = current.getStructDBN(true); + // DEBUG System.out.println(seq); + // DEBUG System.err.println(rna); + int begin = 0; + int end = seq.length() - 1; + id = safeName(getDataName()); + seqs[i] = new Sequence(id, seq, begin, end); + String[] annot = new String[rna.length()]; + Annotation[] ann = new Annotation[rna.length()]; + for (int j = 0; j < rna.length(); j++) + { + annot[j] = rna.substring(j, j + 1); + + } + + for (int k = 0; k < rna.length(); k++) + { + ann[k] = new Annotation(annot[k], "", + jalview.schemes.ResidueProperties.getRNASecStrucState( + annot[k]).charAt(0), 0f); + + } + AlignmentAnnotation align = new AlignmentAnnotation("Sec. str.", + current.getID(), ann); + + seqs[i].addAlignmentAnnotation(align); + seqs[i].setRNA(result.get(i)); + this.annotations.addElement(align); + } + this.setSeqs(seqs); + + } + + protected static AlignmentAnnotation parseAnnotationRow( + Vector annotation, String label, String annots) { String convert1, convert2 = null; @@ -553,7 +614,7 @@ public class StockholmFile extends AlignFile Regex openparen = new Regex("(<|\\[)", "("); Regex closeparen = new Regex("(>|\\])", ")"); - // Detect if file is RNA by looking for bracket types + // Detect if file is RNA by looking for bracket types Regex detectbrackets = new Regex("(<|>|\\[|\\]|\\(|\\))"); convert1 = openparen.replaceAll(annots); @@ -581,7 +642,7 @@ public class StockholmFile extends AlignFile if (detectbrackets.search(pos)) { ann.secondaryStructure = jalview.schemes.ResidueProperties - .getRNASecStrucState(pos).charAt(0); + .getRNASecStrucState(pos).charAt(0); } else { @@ -594,14 +655,14 @@ public class StockholmFile extends AlignFile ann.displayCharacter = ""; // null; // " "; } else - { + { ann.displayCharacter = " " + ann.displayCharacter; } } els[i] = ann; } - AlignmentAnnotation annot = null; + AlignmentAnnotation annot = null; Enumeration e = annotation.elements(); while (e.hasMoreElements()) { @@ -616,16 +677,16 @@ public class StockholmFile extends AlignFile annotation.addElement(annot); } else - { + { Annotation[] anns = new Annotation[annot.annotations.length + els.length]; - System.arraycopy(annot.annotations, 0, anns, 0, - annot.annotations.length); + System.arraycopy(annot.annotations, 0, anns, 0, + annot.annotations.length); System.arraycopy(els, 0, anns, annot.annotations.length, els.length); annot.annotations = anns; - //System.out.println("else: "); - } - return annot; + // System.out.println("else: "); + } + return annot; } public static String print(SequenceI[] s) @@ -674,41 +735,6 @@ public class StockholmFile extends AlignFile return id; } /** - * //ssline is complete secondary structure line private AlignmentAnnotation - * addHelices(Vector annotation, String label, String ssline) { - * - * // decide on secondary structure or not. Annotation[] els = new - * Annotation[ssline.length()]; for (int i = 0; i < ssline.length(); i++) { - * String pos = ssline.substring(i, i + 1); Annotation ann; ann = new - * Annotation(pos, "", ' ', 0f); // 0f is 'valid' null - will not - * - * ann.secondaryStructure = - * jalview.schemes.ResidueProperties.getRNAssState(pos).charAt(0); - * - * ann.displayCharacter = "x" + ann.displayCharacter; - * - * System.out.println(ann.displayCharacter); - * - * els[i] = ann; } AlignmentAnnotation helicesAnnot = null; Enumeration e = - * annotation.elements(); while (e.hasMoreElements()) { helicesAnnot = - * (AlignmentAnnotation) e.nextElement(); if (helicesAnnot.label.equals(type)) - * break; helicesAnnot = null; } if (helicesAnnot == null) { helicesAnnot = - * new AlignmentAnnotation(type, type, els); - * annotation.addElement(helicesAnnot); } else { Annotation[] anns = new - * Annotation[helicesAnnot.annotations.length + els.length]; - * System.arraycopy(helicesAnnot.annotations, 0, anns, 0, - * helicesAnnot.annotations.length); System.arraycopy(els, 0, anns, - * helicesAnnot.annotations.length, els.length); helicesAnnot.annotations = - * anns; } - * - * helicesAnnot.features = Rna.GetBasePairs(ssline); - * Rna.HelixMap(helicesAnnot.features); - * - * - * return helicesAnnot; } - * - */ - /** * make a friendly ID string. * * @param dataName @@ -717,13 +743,13 @@ public class StockholmFile extends AlignFile private String safeName(String dataName) { int b = 0; - while ((b = dataName.indexOf("/")) > -1 && b < dataName.length() ) - { - dataName = dataName.substring(b + 1).trim(); - + 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(); + int e = (dataName.length() - dataName.indexOf(".")) + 1; + dataName = dataName.substring(1, e).trim(); return dataName; } }