From 191ed00f52901ed7ee6d1137637e66c999913be8 Mon Sep 17 00:00:00 2001 From: Daniel Barton Date: Wed, 21 Aug 2013 11:49:43 +0100 Subject: [PATCH 1/1] RNAStruct replaced by RNAStructScoreManager. Why does webservice getAnnotation method still return a ScoreManager Not an RNAStructScoreManager?!? --- datamodel/compbio/data/sequence/RNAStruct.java | 119 --- .../compbio/data/sequence/RNAStructReader.java | 584 ++++++------- .../data/sequence/RNAStructScoreManager.java | 131 +++ runner/compbio/runner/structure/RNAalifold.java | 193 ++--- .../structure/RNAalifoldParametersTester.java | 895 ++++++++++---------- .../compbio/runner/structure/RNAalifoldTester.java | 237 +++--- testsrc/compbio/ws/client/TestRNAalifoldWS.java | 227 ++--- webservices/compbio/ws/server/RNAalifoldWS.java | 149 ++-- 8 files changed, 1262 insertions(+), 1273 deletions(-) delete mode 100644 datamodel/compbio/data/sequence/RNAStruct.java create mode 100644 datamodel/compbio/data/sequence/RNAStructScoreManager.java diff --git a/datamodel/compbio/data/sequence/RNAStruct.java b/datamodel/compbio/data/sequence/RNAStruct.java deleted file mode 100644 index 65bca75..0000000 --- a/datamodel/compbio/data/sequence/RNAStruct.java +++ /dev/null @@ -1,119 +0,0 @@ -package compbio.data.sequence; - -import java.util.List; -import java.util.Iterator; -import java.util.Set; -import java.util.TreeSet; -import java.util.List; -import java.util.ArrayList; - -import javax.xml.bind.annotation.XmlAccessType; -import javax.xml.bind.annotation.XmlAccessorType; - -import compbio.util.annotation.Immutable; -import compbio.util.SysPrefs; -import compbio.data.sequence.Score; - -/* - * RNA secondary structure - * A class which is essentially just a Pair(List, List>) - * For the purpose of creating and retrieving data from ScoreManager Objects - * which are being used to store RNA folding output - */ - -@XmlAccessorType(XmlAccessType.FIELD) -public final class RNAStruct { - - private List structs = new ArrayList(); - private List> data = new ArrayList>(); - - - public RNAStruct() { - // default JaxB Constructor - } - - public RNAStruct(List structs, List> data) { - assert(structs.size() == data.size()); - this.structs = structs; - this.data = data; - } - - public List getStructs() { - return structs; - } - - public List> getData() { - return data; - } - - // Send this data Structure back to something approximating the stdoutFile - // with extra information from alifold.out - @Override - public String toString() { - String out = ""; - // The first objects hold the Consensus Alignment and the alifold.out info - out += structs.get(0) + SysPrefs.newlinechar; - - // Now the rest of the structures with energies/frequencies - for (int i = 1; i < structs.size(); i++) { - out = out + structs.get(i).toString(); - - if (data.get(i).first().getScores().size() > 0) { - List scores = data.get(i).first().getScores(); - if (scores.size() >= 3) { - out = out + " (" + scores.get(0).toString() + " = " - + scores.get(1).toString() + " + " + scores.get(2).toString() - + ")" + SysPrefs.newlinechar; - } - else if (data.get(i).first().getMethod().equals("alifoldMEA")) { - out = out + " { " + scores.get(0).toString() + " MEA=" - + scores.get(1).toString() + "}" + SysPrefs.newlinechar; - } - else if (scores.size() >= 2) { - out = out + " [" + scores.get(0).toString() + ", " - + scores.get(1).toString() + "]" + SysPrefs.newlinechar; - - } - } else out += SysPrefs.newlinechar; - } - if (data.get(0).first().getScores().size() > 0) { - Iterator iScores = data.get(0).iterator(); - out += "Base Pairings followed by probability" + SysPrefs.newlinechar; - for (int i = 0; i < data.get(0).size(); i++) { - Score s = iScores.next(); - Range r = s.getRanges().first(); - Float score = s.getScores().get(0); - out += String.format("%4d %4d %.1f%n", r.getFrom(), r.getTo(), - score); - } - } - - return out; - } - - @Override - public boolean equals(Object obj) { - if (obj == null) { - return false; - } - if (!(obj instanceof RNAStruct)) { - return false; - } - RNAStruct other = (RNAStruct) obj; - if (structs == null) { - if (other.structs != null) { - return false; - } - } else if (!structs.equals(other.structs)) - return false; - if (data == null) { - if (other.data != null) - return false; - } else if (!data.equals(other.data)) - return false; - - return true; - } -} - - diff --git a/datamodel/compbio/data/sequence/RNAStructReader.java b/datamodel/compbio/data/sequence/RNAStructReader.java index cfc83ff..7ebba4c 100644 --- a/datamodel/compbio/data/sequence/RNAStructReader.java +++ b/datamodel/compbio/data/sequence/RNAStructReader.java @@ -1,292 +1,292 @@ -package compbio.data.sequence; - -import java.io.BufferedReader; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.Scanner; -import java.util.TreeSet; -import java.util.regex.Pattern; - -import org.apache.log4j.Logger; - -import compbio.runner.structure.RNAalifold; - -// Utility class for reading alifold output - -public class RNAStructReader { - - private static Logger log = Logger.getLogger(RNAStructReader.class); - - // Whitespace patterns - static String s = "[+\\s=]+"; - static String bracket = "\\(|\\)|\\{|\\}|\\[|\\]"; - static String notData = "[\\s=+]+"; - - // RNAOut data type patterns - static String seqP = "[_\\-a-zA-Z]{2,}"; // Has to match --mis output aswell (not just ACGU_) - static String structP = "[\\.)({}\\[\\],]{2,}"; - static String floatP = "-?\\d+\\.\\d*(e[+\\-]\\d+)?"; - static String energyP = "-?[0-9]*\\.?[0-9]{2}"; - static String freqP = "^-?\\d\\.\\d{6,}(e[+\\-]\\d+)?$"; - - // alifold out line patterns - static String ps = "\\s*"; - static String alignmentP = "^"+seqP+ps+"$"; - static String stdStructP = "^"+structP+s+"\\("+ps+floatP+s+floatP+s+floatP+ps+"\\)"+ps+"$"; - static String justStructP = "^"+structP+ps+"$"; - static String stochBTStructP = "^"+structP+s+floatP+s+floatP+ps+"$"; - static String PStructP = "^"+structP+s+"\\["+ps+floatP+ps+"\\]"+ps+"$"; - static String centStructP = "^"+structP+s+floatP+ps+"\\{"+ps+floatP+s+floatP+ps+"\\}"+ps+"$"; - static String MEAStructP = "^"+structP+s+"\\{"+ps+floatP+s+"MEA="+floatP+ps+"\\}"+ps+"$"; - static String freeEnergyP = "^"+ps+"free energy of ensemble"+ps+"="+ps+floatP+ps+"kcal/mol"+ps+"$"; - static String ensembleFreqP = "^"+ps+"frequency of mfe structure in ensemble "+floatP+ps+"$"; - - public static ScoreManager readRNAStructStream(InputStream stdout) - throws IOException { - - String error = "Error in parsing alifold stdout file: "; - // The Lists required to construct a ScoreManager Using the new constructor - List structs = new ArrayList(); - List> data = new ArrayList>(); - - // Allocate necessry data structures for creating Score objects - ArrayList scores = new ArrayList(); - - BufferedReader reader = new BufferedReader(new InputStreamReader(stdout)); - // The first 2 lines of the alifold stdout file are always the same format - String fline = reader.readLine(); - assert (Pattern.matches(AlifoldLine.alignment.regex, fline)) : - error + "Sequence Alignment Expected"; - structs.add(fline.trim()); - data.add(newEmptyScore(AlifoldResult.alifoldSeq)); - - fline = reader.readLine(); - assert (Pattern.matches(AlifoldLine.stdStruct.regex, fline)) : - error + "Consensus Structure and Energy Expected"; - Scanner sc = new Scanner(fline); - structs.add(sc.next()); - for (int i = 0; i < 3; i++) { - scores.add(Float.parseFloat(sc.findInLine(floatP))); - } - data.add(newSetScore(AlifoldResult.alifold, scores)); - - // Now the alifold stdout file formats diverge based on arguments - fline = reader.readLine(); - String sline; - Scanner nsc = null; - while ( fline != null) { - scores.clear(); - AlifoldLine ftype = identifyLine(fline); - sline = reader.readLine(); // Look ahead - sc = new Scanner(fline); - if (sline != null) nsc = new Scanner(sline); - - if (ftype.equals(AlifoldLine.PStruct)) { - // The -p or --MEA option is specified - // The next line should always be frequency of mfe structure - assert ( sline != null && Pattern.matches(AlifoldLine.ensembleFreq.regex, sline)) : - error + "Expected frequency of mfe structure"; - structs.add(sc.next()); - scores.add(Float.parseFloat(sc.findInLine(floatP))); - scores.add(Float.parseFloat(nsc.findInLine(floatP))); - data.add(newSetScore(AlifoldResult.alifoldP, scores)); - // Jump line - sline = reader.readLine(); - } - else if (ftype.equals(AlifoldLine.centStruct)) { - structs.add(sc.next()); - for (int i = 0; i < 3; i++) { - scores.add(Float.parseFloat(sc.findInLine(floatP))); - } - data.add(newSetScore(AlifoldResult.alifoldCentroid, scores)); - } - else if (ftype.equals(AlifoldLine.MEAStruct)) { - structs.add(sc.next()); - for (int i = 0; i < 2; i++) { - scores.add(Float.parseFloat(sc.findInLine(floatP))); - } - data.add(newSetScore(AlifoldResult.alifoldMEA, scores)); - } - else if (ftype.equals(AlifoldLine.justStruct)) { - structs.add(sc.next()); - data.add(newEmptyScore(AlifoldResult.alifoldStochBT)); - } - else if (ftype.equals(AlifoldLine.stochBTStruct)) { - structs.add(sc.next()); - scores.add(sc.nextFloat()); - scores.add(sc.nextFloat()); - data.add(newSetScore(AlifoldResult.alifoldStochBT, scores)); - } - else if (ftype.equals(AlifoldLine.freeEnergy)) { - assert (sline != null - && Pattern.matches(AlifoldLine.ensembleFreq.regex, sline)) : - error + "Found 'freeEnergy' line on its own"; - structs.add("Free energy of ensemble (kcal/mol) followed by " - + "frequency of mfe structure in ensemble"); - scores.add(Float.parseFloat(sc.findInLine(floatP))); - scores.add(Float.parseFloat(nsc.findInLine(floatP))); - data.add(newSetScore(AlifoldResult.alifoldMetadata, scores)); - // jump line - sline = reader.readLine(); - } - - - assert(!ftype.equals(AlifoldLine.ensembleFreq)) : - error + "Wasn't expecting 'frequency of mfe structure'!"; - assert(!ftype.equals(AlifoldLine.stdStruct)) : - error + "'Standard output' line at a place other than line 2!"; - assert(!ftype.equals(AlifoldLine.alignment)) : - error + "Wasn't expecting an alignment sequence!"; - assert(!ftype.equals(AlifoldLine.OTHER)) : - error + "Wasn't expecting this whatever it is: " + fline; - if (Pattern.matches("^\\s*$", fline)) { - log.warn("While parsing alifold stdout: A line is either empty or" - + " contains only whitespace"); - } - - fline = sline; - } - - sc.close(); - if (nsc != null) nsc.close(); - - return new ScoreManager(new RNAStruct(structs, data)); - } - - // Just for the purpose of creating nee TreeSet objects of length one - // for adding to a 'data' list to make a ScoreManager - private static TreeSet newSetScore(Enum res, List scores) { - // first convert List to float[] - float[] scoresf = new float[scores.size()]; - Float f; - for (int i = 0; i < scoresf.length; i++) { - f = scores.get(i); - scoresf[i] = ( f != null ? f : Float.NaN); - } - return new TreeSet(Arrays.asList(new Score(res, scoresf))); - } - - // A method just for the purpose of neatly creating Almost Empty score objects - // that can't be null - public static TreeSet newEmptyScore(Enum res) { - return new TreeSet(Arrays.asList(new Score(res, new float[0]))); - } - - public static ScoreManager readRNAStructStream(InputStream stdout, - InputStream alifold) throws IOException { - - // The Lists required to construct a ScoreManager Using the new constructor - List structs; - List> data; - - // Get a ScoreManager that takes the std output but ignores alifold.out (-p) - ScoreManager stdSM = readRNAStructStream(stdout); - - // Unpack this into the structs and data lists - structs = stdSM.asRNAStruct().getStructs(); - data = stdSM.asRNAStruct().getData(); - - // Now parse alifold.out - Scanner sc = new Scanner(alifold); - sc.useDelimiter("[\\s%]+"); - - // jump two lines to the data - sc.nextLine(); sc.nextLine(); - - // Read the first, second and fourth columns. Ignoring everything else. - // Allocate necessry data structures for creating Score objects - ArrayList scores = new ArrayList(); - List rangeHolder = new ArrayList(); - String s = "null"; - while (true) { - s = sc.next(); - if (java.util.regex.Pattern.matches("^[\\.)(]{2,}$", s)) break; - if (!sc.hasNextLine()) break; - int t = sc.nextInt(); - rangeHolder.add(new Range(Integer.parseInt(s), t)); - sc.next(); - scores.add(sc.nextFloat()); - sc.nextLine(); - } - sc.close(); - - // Update the first ScoreHolder TreeSet element - assert (rangeHolder.size() == scores.size()); - TreeSet sHolder = new TreeSet(); - for (int i = 0; i < rangeHolder.size(); i++) { - ArrayList singleS = new ArrayList(Arrays.asList(scores.get(i))); - TreeSet singleR = new TreeSet(Arrays.asList(rangeHolder.get(i))); - sHolder.add(new Score(AlifoldResult.alifoldSeq, singleS, singleR)); - } - - data.set(0, sHolder); - - return new ScoreManager(new RNAStruct(structs, data)); - } - - private static RNAOut identify(String token) { - if (Pattern.matches(seqP, token)) { - return RNAOut.SEQ; - } else if (Pattern.matches(structP, token)) { - return RNAOut.STRUCT; - } else if (Pattern.matches(energyP, token)) { - return RNAOut.ENERGY; - } else if (Pattern.matches(freqP, token)) { - return RNAOut.FREQ; - } - - return RNAOut.OTHER; - } - - private static AlifoldLine identifyLine(String line) { - - for (AlifoldLine il : AlifoldLine.values()) { - if (Pattern.matches(il.regex, line)) return il; - } - return AlifoldLine.OTHER; - } - - static enum AlifoldLine { - stdStruct (stdStructP), - justStruct (justStructP), - stochBTStruct (stochBTStructP), - PStruct (PStructP), - centStruct (centStructP), - MEAStruct (MEAStructP), - freeEnergy (freeEnergyP), - ensembleFreq (ensembleFreqP), - alignment (alignmentP), - OTHER (".*"); - - String regex; - AlifoldLine(String regex) { this.regex = regex; } - - } - - //The types of data in an RNAalifold stdout file - static enum RNAOut { - SEQ, STRUCT, ENERGY, FREQ, OTHER - } - - //Something to put in the Score objects of the alifold result which gives information - //about what kind of sequence it is holding in its String Id. - static enum AlifoldResult { - alifold, alifoldP, alifoldMEA, alifoldCentroid, alifoldStochBT, alifoldSeq, alifoldMetadata - } - - - - // Print the full regex Strings for testing - public static void main(String[] args) { - for (AlifoldLine l : AlifoldLine.values()) { - System.out.println(l.toString() + ": " + l.regex.replace("^","").replace("$","")); - } - } - - - -} +package compbio.data.sequence; + +import java.io.BufferedReader; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Scanner; +import java.util.TreeSet; +import java.util.regex.Pattern; + +import org.apache.log4j.Logger; + +import compbio.runner.structure.RNAalifold; + +// Utility class for reading alifold output + +public class RNAStructReader { + + private static Logger log = Logger.getLogger(RNAStructReader.class); + + // Whitespace patterns + static String s = "[+\\s=]+"; + static String bracket = "\\(|\\)|\\{|\\}|\\[|\\]"; + static String notData = "[\\s=+]+"; + + // RNAOut data type patterns + static String seqP = "[_\\-a-zA-Z]{2,}"; // Has to match --mis output aswell (not just ACGU_) + static String structP = "[\\.)({}\\[\\],]{2,}"; + static String floatP = "-?\\d+\\.\\d*(e[+\\-]\\d+)?"; + static String energyP = "-?[0-9]*\\.?[0-9]{2}"; + static String freqP = "^-?\\d\\.\\d{6,}(e[+\\-]\\d+)?$"; + + // alifold out line patterns + static String ps = "\\s*"; + static String alignmentP = "^"+seqP+ps+"$"; + static String stdStructP = "^"+structP+s+"\\("+ps+floatP+s+floatP+s+floatP+ps+"\\)"+ps+"$"; + static String justStructP = "^"+structP+ps+"$"; + static String stochBTStructP = "^"+structP+s+floatP+s+floatP+ps+"$"; + static String PStructP = "^"+structP+s+"\\["+ps+floatP+ps+"\\]"+ps+"$"; + static String centStructP = "^"+structP+s+floatP+ps+"\\{"+ps+floatP+s+floatP+ps+"\\}"+ps+"$"; + static String MEAStructP = "^"+structP+s+"\\{"+ps+floatP+s+"MEA="+floatP+ps+"\\}"+ps+"$"; + static String freeEnergyP = "^"+ps+"free energy of ensemble"+ps+"="+ps+floatP+ps+"kcal/mol"+ps+"$"; + static String ensembleFreqP = "^"+ps+"frequency of mfe structure in ensemble "+floatP+ps+"$"; + + public static RNAStructScoreManager readRNAStructStream(InputStream stdout) + throws IOException { + + String error = "Error in parsing alifold stdout file: "; + // The Lists required to construct a ScoreManager Using the new constructor + List structs = new ArrayList(); + List> data = new ArrayList>(); + + // Allocate necessry data structures for creating Score objects + ArrayList scores = new ArrayList(); + + BufferedReader reader = new BufferedReader(new InputStreamReader(stdout)); + // The first 2 lines of the alifold stdout file are always the same format + String fline = reader.readLine(); + assert (Pattern.matches(AlifoldLine.alignment.regex, fline)) : + error + "Sequence Alignment Expected"; + structs.add(fline.trim()); + data.add(newEmptyScore(AlifoldResult.alifoldSeq)); + + fline = reader.readLine(); + assert (Pattern.matches(AlifoldLine.stdStruct.regex, fline)) : + error + "Consensus Structure and Energy Expected"; + Scanner sc = new Scanner(fline); + structs.add(sc.next()); + for (int i = 0; i < 3; i++) { + scores.add(Float.parseFloat(sc.findInLine(floatP))); + } + data.add(newSetScore(AlifoldResult.alifold, scores)); + + // Now the alifold stdout file formats diverge based on arguments + fline = reader.readLine(); + String sline; + Scanner nsc = null; + while ( fline != null) { + scores.clear(); + AlifoldLine ftype = identifyLine(fline); + sline = reader.readLine(); // Look ahead + sc = new Scanner(fline); + if (sline != null) nsc = new Scanner(sline); + + if (ftype.equals(AlifoldLine.PStruct)) { + // The -p or --MEA option is specified + // The next line should always be frequency of mfe structure + assert ( sline != null && Pattern.matches(AlifoldLine.ensembleFreq.regex, sline)) : + error + "Expected frequency of mfe structure"; + structs.add(sc.next()); + scores.add(Float.parseFloat(sc.findInLine(floatP))); + scores.add(Float.parseFloat(nsc.findInLine(floatP))); + data.add(newSetScore(AlifoldResult.alifoldP, scores)); + // Jump line + sline = reader.readLine(); + } + else if (ftype.equals(AlifoldLine.centStruct)) { + structs.add(sc.next()); + for (int i = 0; i < 3; i++) { + scores.add(Float.parseFloat(sc.findInLine(floatP))); + } + data.add(newSetScore(AlifoldResult.alifoldCentroid, scores)); + } + else if (ftype.equals(AlifoldLine.MEAStruct)) { + structs.add(sc.next()); + for (int i = 0; i < 2; i++) { + scores.add(Float.parseFloat(sc.findInLine(floatP))); + } + data.add(newSetScore(AlifoldResult.alifoldMEA, scores)); + } + else if (ftype.equals(AlifoldLine.justStruct)) { + structs.add(sc.next()); + data.add(newEmptyScore(AlifoldResult.alifoldStochBT)); + } + else if (ftype.equals(AlifoldLine.stochBTStruct)) { + structs.add(sc.next()); + scores.add(sc.nextFloat()); + scores.add(sc.nextFloat()); + data.add(newSetScore(AlifoldResult.alifoldStochBT, scores)); + } + else if (ftype.equals(AlifoldLine.freeEnergy)) { + assert (sline != null + && Pattern.matches(AlifoldLine.ensembleFreq.regex, sline)) : + error + "Found 'freeEnergy' line on its own"; + structs.add("Free energy of ensemble (kcal/mol) followed by " + + "frequency of mfe structure in ensemble"); + scores.add(Float.parseFloat(sc.findInLine(floatP))); + scores.add(Float.parseFloat(nsc.findInLine(floatP))); + data.add(newSetScore(AlifoldResult.alifoldMetadata, scores)); + // jump line + sline = reader.readLine(); + } + + + assert(!ftype.equals(AlifoldLine.ensembleFreq)) : + error + "Wasn't expecting 'frequency of mfe structure'!"; + assert(!ftype.equals(AlifoldLine.stdStruct)) : + error + "'Standard output' line at a place other than line 2!"; + assert(!ftype.equals(AlifoldLine.alignment)) : + error + "Wasn't expecting an alignment sequence!"; + assert(!ftype.equals(AlifoldLine.OTHER)) : + error + "Wasn't expecting this whatever it is: " + fline; + if (Pattern.matches("^\\s*$", fline)) { + log.warn("While parsing alifold stdout: A line is either empty or" + + " contains only whitespace"); + } + + fline = sline; + } + + sc.close(); + if (nsc != null) nsc.close(); + + return new RNAStructScoreManager(structs, data); + } + + // Just for the purpose of creating nee TreeSet objects of length one + // for adding to a 'data' list to make a ScoreManager + private static TreeSet newSetScore(Enum res, List scores) { + // first convert List to float[] + float[] scoresf = new float[scores.size()]; + Float f; + for (int i = 0; i < scoresf.length; i++) { + f = scores.get(i); + scoresf[i] = ( f != null ? f : Float.NaN); + } + return new TreeSet(Arrays.asList(new Score(res, scoresf))); + } + + // A method just for the purpose of neatly creating Almost Empty score objects + // that can't be null + public static TreeSet newEmptyScore(Enum res) { + return new TreeSet(Arrays.asList(new Score(res, new float[0]))); + } + + public static RNAStructScoreManager readRNAStructStream(InputStream stdout, + InputStream alifold) throws IOException { + + // The Lists required to construct a ScoreManager Using the new constructor + List structs; + List> data; + + // Get a ScoreManager that takes the std output but ignores alifold.out (-p) + RNAStructScoreManager stdSM = readRNAStructStream(stdout); + + // Unpack this into the structs and data lists + structs = stdSM.getStructs(); + data = stdSM.getData(); + + // Now parse alifold.out + Scanner sc = new Scanner(alifold); + sc.useDelimiter("[\\s%]+"); + + // jump two lines to the data + sc.nextLine(); sc.nextLine(); + + // Read the first, second and fourth columns. Ignoring everything else. + // Allocate necessry data structures for creating Score objects + ArrayList scores = new ArrayList(); + List rangeHolder = new ArrayList(); + String s = "null"; + while (true) { + s = sc.next(); + if (java.util.regex.Pattern.matches("^[\\.)(]{2,}$", s)) break; + if (!sc.hasNextLine()) break; + int t = sc.nextInt(); + rangeHolder.add(new Range(Integer.parseInt(s), t)); + sc.next(); + scores.add(sc.nextFloat()); + sc.nextLine(); + } + sc.close(); + + // Update the first ScoreHolder TreeSet element + assert (rangeHolder.size() == scores.size()); + TreeSet sHolder = new TreeSet(); + for (int i = 0; i < rangeHolder.size(); i++) { + ArrayList singleS = new ArrayList(Arrays.asList(scores.get(i))); + TreeSet singleR = new TreeSet(Arrays.asList(rangeHolder.get(i))); + sHolder.add(new Score(AlifoldResult.alifoldSeq, singleS, singleR)); + } + + data.set(0, sHolder); + + return new RNAStructScoreManager(structs, data); + } + + private static RNAOut identify(String token) { + if (Pattern.matches(seqP, token)) { + return RNAOut.SEQ; + } else if (Pattern.matches(structP, token)) { + return RNAOut.STRUCT; + } else if (Pattern.matches(energyP, token)) { + return RNAOut.ENERGY; + } else if (Pattern.matches(freqP, token)) { + return RNAOut.FREQ; + } + + return RNAOut.OTHER; + } + + private static AlifoldLine identifyLine(String line) { + + for (AlifoldLine il : AlifoldLine.values()) { + if (Pattern.matches(il.regex, line)) return il; + } + return AlifoldLine.OTHER; + } + + static enum AlifoldLine { + stdStruct (stdStructP), + justStruct (justStructP), + stochBTStruct (stochBTStructP), + PStruct (PStructP), + centStruct (centStructP), + MEAStruct (MEAStructP), + freeEnergy (freeEnergyP), + ensembleFreq (ensembleFreqP), + alignment (alignmentP), + OTHER (".*"); + + String regex; + AlifoldLine(String regex) { this.regex = regex; } + + } + + //The types of data in an RNAalifold stdout file + static enum RNAOut { + SEQ, STRUCT, ENERGY, FREQ, OTHER + } + + //Something to put in the Score objects of the alifold result which gives information + //about what kind of sequence it is holding in its String Id. + static enum AlifoldResult { + alifold, alifoldP, alifoldMEA, alifoldCentroid, alifoldStochBT, alifoldSeq, alifoldMetadata + } + + + + // Print the full regex Strings for testing + public static void main(String[] args) { + for (AlifoldLine l : AlifoldLine.values()) { + System.out.println(l.toString() + ": " + l.regex.replace("^","").replace("$","")); + } + } + + + +} diff --git a/datamodel/compbio/data/sequence/RNAStructScoreManager.java b/datamodel/compbio/data/sequence/RNAStructScoreManager.java new file mode 100644 index 0000000..3fb856c --- /dev/null +++ b/datamodel/compbio/data/sequence/RNAStructScoreManager.java @@ -0,0 +1,131 @@ +package compbio.data.sequence; + + +import java.io.IOException; +import java.io.Writer; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.TreeMap; +import java.util.TreeSet; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlTransient; + +import compbio.data.sequence.ScoreManager.ScoreHolder; +import compbio.util.SysPrefs; + +@XmlAccessorType(XmlAccessType.FIELD) +public class RNAStructScoreManager extends ScoreManager { + + + private RNAStructScoreManager() { + //Default JAXB constructor + } + + + public RNAStructScoreManager(List structs, List> data) { + assert(structs.size() == data.size()); + + List seqScores = new ArrayList(); + + for (int i = 0; i < structs.size(); i++) { + seqScores.add(new ScoreHolder(structs.get(i), data.get(i))); + } + this.seqScores = seqScores; + } + + // I put this in purely because it mirrors a method in ScoreManager, not because I need it :/ + public static RNAStructScoreManager newInstance(List structs, List> data) { + return new RNAStructScoreManager(structs, data); + } + + public List getStructs() { + List structs = new ArrayList(); + for (ScoreHolder sch : this.seqScores) { + structs.add(sch.id); + } + return structs; + } + + public List> getData() { + List> data = new ArrayList>(); + for (ScoreHolder sch : this.seqScores) { + data.add(sch.scores); + } + return data; + } + + // Send this data Structure back to something approximating the stdoutFile + // with extra information from alifold.out + @Override + public String toString() { + String out = ""; + // The first objects hold the Consensus Alignment and the alifold.out info + out += this.getStructs().get(0) + SysPrefs.newlinechar; + + // Now the rest of the structures with energies/frequencies + for (int i = 1; i < this.getStructs().size(); i++) { + out = out + this.getStructs().get(i).toString(); + + if (this.getData().get(i).first().getScores().size() > 0) { + List scores = this.getData().get(i).first().getScores(); + if (scores.size() >= 3) { + out = out + " (" + scores.get(0).toString() + " = " + + scores.get(1).toString() + " + " + scores.get(2).toString() + + ")" + SysPrefs.newlinechar; + } + else if (this.getData().get(i).first().getMethod().equals("alifoldMEA")) { + out = out + " { " + scores.get(0).toString() + " MEA=" + + scores.get(1).toString() + "}" + SysPrefs.newlinechar; + } + else if (scores.size() >= 2) { + out = out + " [" + scores.get(0).toString() + ", " + + scores.get(1).toString() + "]" + SysPrefs.newlinechar; + + } + } else out += SysPrefs.newlinechar; + } + if (this.getData().get(0).first().getScores().size() > 0) { + Iterator iScores = this.getData().get(0).iterator(); + out += "Base Pairings followed by probability" + SysPrefs.newlinechar; + for (int i = 0; i < this.getData().get(0).size(); i++) { + Score s = iScores.next(); + Range r = s.getRanges().first(); + Float score = s.getScores().get(0); + out += String.format("%4d %4d %.1f%n", r.getFrom(), r.getTo(), + score); + } + } + + + return out; + } + + @Override + public boolean equals(Object obj) { + if (obj == null) { + return false; + } + if (!(obj instanceof RNAStructScoreManager)) { + return false; + } + RNAStructScoreManager other = (RNAStructScoreManager) obj; + if (this.getStructs() == null) { + if (other.getStructs() != null) { + return false; + } + } else if (!this.getStructs().equals(other.getStructs())) + return false; + if (this.getData() == null) { + if (other.getData() != null) + return false; + } else if (!this.getData().equals(other.getData())) + return false; + + return true; + } +} diff --git a/runner/compbio/runner/structure/RNAalifold.java b/runner/compbio/runner/structure/RNAalifold.java index 0e9e20f..c6c79ec 100644 --- a/runner/compbio/runner/structure/RNAalifold.java +++ b/runner/compbio/runner/structure/RNAalifold.java @@ -1,118 +1,75 @@ -package compbio.runner.structure; - - -import java.io.FileNotFoundException; -import java.io.IOException; -import java.util.Arrays; -import java.util.List; - - - - - -import org.apache.log4j.Logger; - -import compbio.data.sequence.ScoreManager; -import compbio.data.sequence.RNAStruct; -import compbio.data.sequence.UnknownFileFormatException; -import compbio.engine.client.PipedExecutable; -import compbio.engine.client.SkeletalExecutable; -import compbio.metadata.ResultNotAvailableException; -import compbio.runner.Util; - - -import compbio.engine.client.CommandBuilder; - -public class RNAalifold extends SkeletalExecutable - implements PipedExecutable { - - - private static Logger log = Logger.getLogger(RNAalifold.class); - - // May not be necessary as defult is "" but still dont know - // How to deal with different key value separators for different params - public static final String KEY_VALUE_SEPARATOR = " "; - - public RNAalifold() { - super(KEY_VALUE_SEPARATOR); - } - - @Override - public RNAalifold setOutput(String outFile) { - super.setOutput(outFile); - return this; - } - - @Override - public RNAalifold setInput(String inFile) { - cbuilder.setLast(inFile); - super.setInput(inFile); - return this; - } - - @SuppressWarnings("unchecked") - @Override - public Class getType() { - return (Class) this.getClass(); - } - - @SuppressWarnings("unchecked") - @Override - // PlaceHolder method - public ScoreManager getResults(String workDirectory) - throws ResultNotAvailableException { - try { - return Util.readRNAStruct(workDirectory, getOutput()); - - } catch (FileNotFoundException e) { - log.error(e.getMessage(), e.getCause()); - throw new ResultNotAvailableException(e); - } catch (IOException e) { - log.error(e.getMessage(), e.getCause()); - throw new ResultNotAvailableException(e); - } - } - - - // the new currently used methods for reading are found in - // - compbio.data.sequence.SequenceUtil and - // - compbio.runner.Util - - // Simple and generic methods for reading a whole file -// private static String readRNAStruct(String workDirectory, -// String structFile) throws IOException, FileNotFoundException { -// assert !compbio.util.Util.isEmpty(workDirectory); -// assert !compbio.util.Util.isEmpty(structFile); -// File sfile = new File(compbio.engine.client.Util.getFullPath( -// workDirectory, structFile)); -// log.trace("RNAALIFOLD OUTPUT FILE PATH: " + sfile.getAbsolutePath()); -// if(!(sfile.exists() && sfile.length() > 0)) { -// throw new FileNotFoundException("Result for the jobId " -// + workDirectory + "with file name " + structFile -// + " is not found!"); -// } -// return readFile(sfile); -// } -// -// private static BufferedReader input; -// public static String readFile(File inputFile) throws -// FileNotFoundException, IOException { -// -// input = new BufferedReader(new FileReader(inputFile)); -// -// String file = new String(); -// String line = new String(); -// -// while (true) { -// line = input.readLine(); -// -// if (line != null) { -// file = file + line + "\r\n"; -// } else break; -// } -// // Close file -// input.close(); -// return file; -// } - -} +package compbio.runner.structure; + + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.Arrays; +import java.util.List; + + + + + +import org.apache.log4j.Logger; + +import compbio.data.sequence.ScoreManager; +import compbio.data.sequence.RNAStructScoreManager; +import compbio.data.sequence.UnknownFileFormatException; +import compbio.engine.client.PipedExecutable; +import compbio.engine.client.SkeletalExecutable; +import compbio.metadata.ResultNotAvailableException; +import compbio.runner.Util; + + +import compbio.engine.client.CommandBuilder; + +public class RNAalifold extends SkeletalExecutable + implements PipedExecutable { + + + private static Logger log = Logger.getLogger(RNAalifold.class); + + // May not be necessary as defult is "" but still dont know + // How to deal with different key value separators for different params + public static final String KEY_VALUE_SEPARATOR = " "; + + public RNAalifold() { + super(KEY_VALUE_SEPARATOR); + } + + @Override + public RNAalifold setOutput(String outFile) { + super.setOutput(outFile); + return this; + } + + @Override + public RNAalifold setInput(String inFile) { + cbuilder.setLast(inFile); + super.setInput(inFile); + return this; + } + + @SuppressWarnings("unchecked") + @Override + public Class getType() { + return (Class) this.getClass(); + } + + @SuppressWarnings("unchecked") + @Override + public RNAStructScoreManager getResults(String workDirectory) + throws ResultNotAvailableException { + try { + return Util.readRNAStruct(workDirectory, getOutput()); + + } catch (FileNotFoundException e) { + log.error(e.getMessage(), e.getCause()); + throw new ResultNotAvailableException(e); + } catch (IOException e) { + log.error(e.getMessage(), e.getCause()); + throw new ResultNotAvailableException(e); + } + } + +} diff --git a/testsrc/compbio/runner/structure/RNAalifoldParametersTester.java b/testsrc/compbio/runner/structure/RNAalifoldParametersTester.java index 2168a6c..38fbe8b 100644 --- a/testsrc/compbio/runner/structure/RNAalifoldParametersTester.java +++ b/testsrc/compbio/runner/structure/RNAalifoldParametersTester.java @@ -1,447 +1,448 @@ -package compbio.runner.structure; - -import static org.testng.Assert.assertFalse; -import static org.testng.Assert.assertNotNull; -import static org.testng.Assert.assertTrue; -import static org.testng.Assert.fail; - -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Map; - -import javax.xml.bind.JAXBException; -import javax.xml.bind.ValidationException; - -import org.apache.log4j.Level; -import org.apache.log4j.Logger; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.Test; - -import compbio.data.sequence.ScoreManager; -import compbio.engine.Configurator; -import compbio.engine.client.ConfiguredExecutable; -import compbio.engine.client.Executable.ExecProvider; -import compbio.engine.conf.RunnerConfigMarshaller; -import compbio.engine.local.ExecutableWrapper; -import compbio.engine.local.LocalRunner; -import compbio.metadata.AllTestSuit; -import compbio.metadata.JobExecutionException; -import compbio.metadata.JobSubmissionException; -import compbio.metadata.Option; -import compbio.metadata.Parameter; -import compbio.metadata.Preset; -import compbio.metadata.PresetManager; -import compbio.metadata.ResultNotAvailableException; -import compbio.metadata.RunnerConfig; -import compbio.runner.OptionCombinator; -import compbio.runner.structure.RNAalifold; -import compbio.util.FileUtil; - - -public class RNAalifoldParametersTester { - - - // should be: AllTestSuit.TEST_DATA_PATH + "RNAalifoldParameters.xml" - static final String rnaalifoldConfigFile = - AllTestSuit.TEST_DATA_PATH + "RNAalifoldParameters.xml"; - public static String test_outfile = "rnaalifold.out.txt"; - - private static Logger log = Logger - .getLogger(AllTestSuit.RUNNER_TEST_LOGGER); - - static { - log.setLevel(Level.INFO); - } - - RunnerConfig rnaalifoldConfig = null; - OptionCombinator rnaalifoldOpc = null; - - @BeforeMethod(groups = { AllTestSuit.test_group_runner }) - @SuppressWarnings("unchecked") - public void setup() { - try { - RunnerConfigMarshaller rnaalifoldmarsh = - new RunnerConfigMarshaller(RunnerConfig.class); - rnaalifoldConfig = rnaalifoldmarsh.read(new FileInputStream(new File( - rnaalifoldConfigFile)), RunnerConfig.class); - // set Name value separator - rnaalifoldConfig.setPrmSeparator(" "); - rnaalifoldOpc = new OptionCombinator(rnaalifoldConfig); - - - - } catch (JAXBException e) { - e.printStackTrace(); - fail(e.getLocalizedMessage()); - } catch (FileNotFoundException e) { - e.printStackTrace(); - fail(e.getLocalizedMessage()); - } - } - - @Test - public void testConfiguration() { - try { - this.rnaalifoldConfig.validate(); - } catch (ValidationException e) { - e.printStackTrace(); - fail(e.getLocalizedMessage()); - } catch (IllegalStateException e) { - e.printStackTrace(); - fail(e.getLocalizedMessage()); - } - } - - @Test(groups = { AllTestSuit.test_group_runner }) - public void testDefaultParameters() { - RNAalifold rnaalifold = new RNAalifold(); - rnaalifold.setInput(AllTestSuit.test_input_aln).setOutput(test_outfile); - - try { - // For local execution use relavive - ConfiguredExecutable confRNAalifold = Configurator - .configureExecutable(rnaalifold); - LocalRunner lr = new LocalRunner(confRNAalifold); - lr.executeJob(); - confRNAalifold = (ConfiguredExecutable) lr.waitForResult(); - assertNotNull(confRNAalifold.getResults()); - } catch (JobSubmissionException e) { - e.printStackTrace(); - fail(e.getLocalizedMessage()); - } catch (JobExecutionException e) { - e.printStackTrace(); - fail(e.getLocalizedMessage()); - } catch (ResultNotAvailableException e) { - e.printStackTrace(); - fail(e.getLocalizedMessage()); - } - } - - - - @Test - public void testOptions() { - // populate list of incompatable pairs by their names. todo - List> failPairs = new ArrayList>(); - - - // test the parameters without -g option - test(removeParam(rnaalifoldOpc.getAllOptions(), "G-Quadruplex")); - // now test without -c option - test(removeParam(rnaalifoldOpc.getAllOptions(), "Circular")); - } - - - // Prints all the incompatible option pairs - // (-c, -g) and (-p, -p0) - - @Test - public void testOptionPairs() throws ResultNotAvailableException { - List> pair = new ArrayList>(); - List> options = rnaalifoldOpc.getAllOptions(); - List> failedOptionPairs = new ArrayList>(); - - boolean failed = true; - for (int i = 0; i args = rnaalifoldOpc.argumentsToCommandString(pair); - try { - failed = singleRun(args); - } catch (ResultNotAvailableException e) { - System.out.println("Results not available: " + e.getMessage()); - failed = true; - } catch (JobSubmissionException e) { - e.printStackTrace(); - fail(e.getLocalizedMessage()); - } catch (JobExecutionException e) { - e.printStackTrace(); - fail(e.getLocalizedMessage()); - } catch (IOException e) { - e.printStackTrace(); - fail(e.getLocalizedMessage()); - } - if (failed == true) { - failedOptionPairs.add(args); - } - } - } - } - System.out.println("failedOptionPairs: " + failedOptionPairs); - - } - - // tests for incompatible Pairs of Parameters - // there are none however the --betascale parameter requires -p - - @Test - public void testParameterPairs() throws ResultNotAvailableException { - List> pair = new ArrayList>(); - List> Parameters = rnaalifoldOpc.getAllParameters(); - List> failedParameterPairs = new ArrayList>(); - - - boolean failed = true; - for (int i = 0; i args = rnaalifoldOpc.argumentsToCommandString(pair); - args.add("-p"); // --betascale requires -p - try { - failed = singleRun(args); - } catch (ResultNotAvailableException e) { - System.out.println("Results not available: " + e.getMessage()); - failed = true; - } catch (JobSubmissionException e) { - e.printStackTrace(); - fail(e.getLocalizedMessage()); - } catch (JobExecutionException e) { - e.printStackTrace(); - fail(e.getLocalizedMessage()); - } catch (IOException e) { - e.printStackTrace(); - fail(e.getLocalizedMessage()); - } - if (failed == true) { - failedParameterPairs.add(args); - } - } - } - } - System.out.println("failedParameterPairs: " + failedParameterPairs); - - } - - - // removes an argument from the list by name - public > List removeParam(List optionList, String name) { - List newL = new ArrayList(); - for (int i = 0; i < optionList.size(); i++) { - System.out.println(name.equals(optionList.get(i).getName())); - if (!name.equals(optionList.get(i).getName())) { - - newL.add(optionList.get(i)); - } - - } - return newL; - } - - public > List removeParams(List optionList, List names) { - for (int i = 0; i < names.size(); i++) { - optionList = removeParam(optionList, names.get(i)); - } - return optionList; - } - - - @Test(groups = { AllTestSuit.test_group_runner }) - public void testParameters() { - List> params = rnaalifoldOpc.getAllParameters(); - System.out.println("param list: " + params); - Collections.shuffle(params); - // test with -p for betascale option - List precursor = new ArrayList(); - precursor.add("-p"); - - test(params, precursor); - } - - // incompatible pairs of arguments are - /* - * the -c and -g options - * the -d2 option and the -d option - * the -p and -p0 option - */ - - @Test(groups = { AllTestSuit.test_group_runner }) - public void testArguments() { - List> options = rnaalifoldOpc.getAllOptions(); - options.addAll(rnaalifoldOpc.getAllParameters()); - Collections.shuffle(options); - test(options); - } - - // This test supercedes the testParameterPair() and testOptionPair() - // tests by testing all pairs of arguments - - - @Test - public void testAllPairs() throws ResultNotAvailableException { - List> pair = new ArrayList>(); - List> options = rnaalifoldOpc.getAllOptions(); - - // take out -p options so it can be added to all commands later - // options = removeParam(options, "Partition Function"); - - options.addAll(rnaalifoldOpc.getAllParameters()); - List> failedOptionPairs = new ArrayList>(); - - boolean failed = true; - for (int i = 0; i args = rnaalifoldOpc.argumentsToCommandString(pair); - // add -p - // args.add("-p"); - try { - failed = singleRun(args); - } catch (ResultNotAvailableException e) { - System.out.println("Results not available: " + e.getMessage()); - failed = true; - } catch (JobSubmissionException e) { - e.printStackTrace(); - fail(e.getLocalizedMessage()); - } catch (JobExecutionException e) { - e.printStackTrace(); - fail(e.getLocalizedMessage()); - } catch (IOException e) { - e.printStackTrace(); - fail(e.getLocalizedMessage()); - } - if (failed == true) { - failedOptionPairs.add(args); - } - } - } - } - System.out.println("failedOptionPairs: " + failedOptionPairs); - } - - /* - * This test method stolen from the other parameter testing classes - * the only purpose of the Collections.shuffle(params) and the for loop - * is to test giving the executable the parameters in different orders - * which leads to a lot of (unnecessary?) tests with an argument list - * as long as this one. - * - */ - - - void test(List> params) { - for (int i = 0; i < params.size(); i++) { - List args = rnaalifoldOpc.argumentsToCommandString(params); - singleTest(args); - Collections.shuffle(params); - } - log.info("NUMBER OF COMBINATION TESTED: " + params.size()); - } - - // because some parameters presuppose the -p option - - void test(List> params, List precursor) { - - for (int i = 0; i < params.size(); i++) { - List args = rnaalifoldOpc.argumentsToCommandString(params); - args.addAll(precursor); - singleTest(args); - Collections.shuffle(params); - } - log.info("NUMBER OF COMBINATION TESTED: " + params.size()); - } - @Test - void singleParamTest() { - //List> params = rnaalifoldOpc.getAllParameters(); - //System.out.println("special: params: " + params); - - //List args = rnaalifoldOpc.argumentsToCommandString(params); - List args = new ArrayList(); - //args.add("-T 37"); args.add("-S 1.07"); args.add("--stochBT_en 10"); - // replace "=" with " " to fail test - args.add("--MEA=1"); - args.add("-p"); - singleTest(args); - - } - - void singleTest(List params) { - try { - log.info("Using arguments: " + params); - RNAalifold rnaalifold = new RNAalifold(); - rnaalifold.setInput(AllTestSuit.test_input_aln).setOutput(test_outfile); - - ConfiguredExecutable confRNAalifold = Configurator - .configureExecutable(rnaalifold, ExecProvider.Local); - // Add options to the executable - confRNAalifold.addParameters(params); - LocalRunner lr = new LocalRunner(confRNAalifold); - lr.executeJob(); - confRNAalifold = (ConfiguredExecutable) lr.waitForResult(); - assertNotNull(confRNAalifold.getResults(), "results is null"); - - System.out.println("Results: \n" - + ((ScoreManager) confRNAalifold.getResults()).asRNAStruct().toString()); - - File errors = new File(confRNAalifold.getWorkDirectory(), - ExecutableWrapper.PROC_ERR_FILE); - if (errors.length() != 0) { - log.error("PROBLEMS:\n " + FileUtil.readFileToString(errors)); - } - assertTrue(errors.length() == 0, "Run with arguments : " + params - + " FAILED!"); - Collections.shuffle(params); - } catch (JobSubmissionException e) { - e.printStackTrace(); - fail(e.getLocalizedMessage()); - } catch (JobExecutionException e) { - e.printStackTrace(); - fail(e.getLocalizedMessage()); - } catch (IOException e) { - e.printStackTrace(); - fail(e.getLocalizedMessage()); - } catch (ResultNotAvailableException e) { - e.printStackTrace(); - fail(e.getLocalizedMessage()); - } - } - - /* A version of singleTest that continues running instead of calling - * fail() when it encounters a problem - * - * Used to identify incompatible options and parameters - * returns -1 on failure - * - * Bad Progamming practice? - */ - - - boolean singleRun(List params) throws JobSubmissionException, - JobExecutionException, IOException, ResultNotAvailableException { - boolean fail = true; - log.info("Using arguments: " + params); - RNAalifold rnaalifold = new RNAalifold(); - rnaalifold.setInput(AllTestSuit.test_input_aln).setOutput(test_outfile); - - ConfiguredExecutable confRNAalifold = Configurator - .configureExecutable(rnaalifold, ExecProvider.Local); - // Add options to the executable - confRNAalifold.addParameters(params); - LocalRunner lr = new LocalRunner(confRNAalifold); - lr.executeJob(); - confRNAalifold = (ConfiguredExecutable) lr.waitForResult(); - - System.out.println("Results: \n" - + ((ScoreManager) confRNAalifold.getResults()).asRNAStruct().toString()); - if (confRNAalifold.getResults() != null) fail = false; - File errors = new File(confRNAalifold.getWorkDirectory(), - ExecutableWrapper.PROC_ERR_FILE); - if (errors.length() != 0) { - log.error("PROBLEMS:\n " + FileUtil.readFileToString(errors)); - } - assertTrue(errors.length() == 0, "Run with arguments : " + params - + " FAILED!"); - Collections.shuffle(params); - return fail; - } -} +package compbio.runner.structure; + +import static org.testng.Assert.assertFalse; +import static org.testng.Assert.assertNotNull; +import static org.testng.Assert.assertTrue; +import static org.testng.Assert.fail; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; + +import javax.xml.bind.JAXBException; +import javax.xml.bind.ValidationException; + +import org.apache.log4j.Level; +import org.apache.log4j.Logger; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import compbio.data.sequence.RNAStructScoreManager; +import compbio.data.sequence.ScoreManager; +import compbio.engine.Configurator; +import compbio.engine.client.ConfiguredExecutable; +import compbio.engine.client.Executable.ExecProvider; +import compbio.engine.conf.RunnerConfigMarshaller; +import compbio.engine.local.ExecutableWrapper; +import compbio.engine.local.LocalRunner; +import compbio.metadata.AllTestSuit; +import compbio.metadata.JobExecutionException; +import compbio.metadata.JobSubmissionException; +import compbio.metadata.Option; +import compbio.metadata.Parameter; +import compbio.metadata.Preset; +import compbio.metadata.PresetManager; +import compbio.metadata.ResultNotAvailableException; +import compbio.metadata.RunnerConfig; +import compbio.runner.OptionCombinator; +import compbio.runner.structure.RNAalifold; +import compbio.util.FileUtil; + + +public class RNAalifoldParametersTester { + + + // should be: AllTestSuit.TEST_DATA_PATH + "RNAalifoldParameters.xml" + static final String rnaalifoldConfigFile = + AllTestSuit.TEST_DATA_PATH + "RNAalifoldParameters.xml"; + public static String test_outfile = "rnaalifold.out.txt"; + + private static Logger log = Logger + .getLogger(AllTestSuit.RUNNER_TEST_LOGGER); + + static { + log.setLevel(Level.INFO); + } + + RunnerConfig rnaalifoldConfig = null; + OptionCombinator rnaalifoldOpc = null; + + @BeforeMethod(groups = { AllTestSuit.test_group_runner }) + @SuppressWarnings("unchecked") + public void setup() { + try { + RunnerConfigMarshaller rnaalifoldmarsh = + new RunnerConfigMarshaller(RunnerConfig.class); + rnaalifoldConfig = rnaalifoldmarsh.read(new FileInputStream(new File( + rnaalifoldConfigFile)), RunnerConfig.class); + // set Name value separator + rnaalifoldConfig.setPrmSeparator(" "); + rnaalifoldOpc = new OptionCombinator(rnaalifoldConfig); + + + + } catch (JAXBException e) { + e.printStackTrace(); + fail(e.getLocalizedMessage()); + } catch (FileNotFoundException e) { + e.printStackTrace(); + fail(e.getLocalizedMessage()); + } + } + + @Test + public void testConfiguration() { + try { + this.rnaalifoldConfig.validate(); + } catch (ValidationException e) { + e.printStackTrace(); + fail(e.getLocalizedMessage()); + } catch (IllegalStateException e) { + e.printStackTrace(); + fail(e.getLocalizedMessage()); + } + } + + @Test(groups = { AllTestSuit.test_group_runner }) + public void testDefaultParameters() { + RNAalifold rnaalifold = new RNAalifold(); + rnaalifold.setInput(AllTestSuit.test_input_aln).setOutput(test_outfile); + + try { + // For local execution use relavive + ConfiguredExecutable confRNAalifold = Configurator + .configureExecutable(rnaalifold); + LocalRunner lr = new LocalRunner(confRNAalifold); + lr.executeJob(); + confRNAalifold = (ConfiguredExecutable) lr.waitForResult(); + assertNotNull(confRNAalifold.getResults()); + } catch (JobSubmissionException e) { + e.printStackTrace(); + fail(e.getLocalizedMessage()); + } catch (JobExecutionException e) { + e.printStackTrace(); + fail(e.getLocalizedMessage()); + } catch (ResultNotAvailableException e) { + e.printStackTrace(); + fail(e.getLocalizedMessage()); + } + } + + + + @Test + public void testOptions() { + // populate list of incompatable pairs by their names. todo + List> failPairs = new ArrayList>(); + + + // test the parameters without -g option + test(removeParam(rnaalifoldOpc.getAllOptions(), "G-Quadruplex")); + // now test without -c option + test(removeParam(rnaalifoldOpc.getAllOptions(), "Circular")); + } + + + // Prints all the incompatible option pairs + // (-c, -g) and (-p, -p0) + + @Test + public void testOptionPairs() throws ResultNotAvailableException { + List> pair = new ArrayList>(); + List> options = rnaalifoldOpc.getAllOptions(); + List> failedOptionPairs = new ArrayList>(); + + boolean failed = true; + for (int i = 0; i args = rnaalifoldOpc.argumentsToCommandString(pair); + try { + failed = singleRun(args); + } catch (ResultNotAvailableException e) { + System.out.println("Results not available: " + e.getMessage()); + failed = true; + } catch (JobSubmissionException e) { + e.printStackTrace(); + fail(e.getLocalizedMessage()); + } catch (JobExecutionException e) { + e.printStackTrace(); + fail(e.getLocalizedMessage()); + } catch (IOException e) { + e.printStackTrace(); + fail(e.getLocalizedMessage()); + } + if (failed == true) { + failedOptionPairs.add(args); + } + } + } + } + System.out.println("failedOptionPairs: " + failedOptionPairs); + + } + + // tests for incompatible Pairs of Parameters + // there are none however the --betascale parameter requires -p + + @Test + public void testParameterPairs() throws ResultNotAvailableException { + List> pair = new ArrayList>(); + List> Parameters = rnaalifoldOpc.getAllParameters(); + List> failedParameterPairs = new ArrayList>(); + + + boolean failed = true; + for (int i = 0; i args = rnaalifoldOpc.argumentsToCommandString(pair); + args.add("-p"); // --betascale requires -p + try { + failed = singleRun(args); + } catch (ResultNotAvailableException e) { + System.out.println("Results not available: " + e.getMessage()); + failed = true; + } catch (JobSubmissionException e) { + e.printStackTrace(); + fail(e.getLocalizedMessage()); + } catch (JobExecutionException e) { + e.printStackTrace(); + fail(e.getLocalizedMessage()); + } catch (IOException e) { + e.printStackTrace(); + fail(e.getLocalizedMessage()); + } + if (failed == true) { + failedParameterPairs.add(args); + } + } + } + } + System.out.println("failedParameterPairs: " + failedParameterPairs); + + } + + + // removes an argument from the list by name + public > List removeParam(List optionList, String name) { + List newL = new ArrayList(); + for (int i = 0; i < optionList.size(); i++) { + System.out.println(name.equals(optionList.get(i).getName())); + if (!name.equals(optionList.get(i).getName())) { + + newL.add(optionList.get(i)); + } + + } + return newL; + } + + public > List removeParams(List optionList, List names) { + for (int i = 0; i < names.size(); i++) { + optionList = removeParam(optionList, names.get(i)); + } + return optionList; + } + + + @Test(groups = { AllTestSuit.test_group_runner }) + public void testParameters() { + List> params = rnaalifoldOpc.getAllParameters(); + System.out.println("param list: " + params); + Collections.shuffle(params); + // test with -p for betascale option + List precursor = new ArrayList(); + precursor.add("-p"); + + test(params, precursor); + } + + // incompatible pairs of arguments are + /* + * the -c and -g options + * the -d2 option and the -d option + * the -p and -p0 option + */ + + @Test(groups = { AllTestSuit.test_group_runner }) + public void testArguments() { + List> options = rnaalifoldOpc.getAllOptions(); + options.addAll(rnaalifoldOpc.getAllParameters()); + Collections.shuffle(options); + test(options); + } + + // This test supercedes the testParameterPair() and testOptionPair() + // tests by testing all pairs of arguments + + + @Test + public void testAllPairs() throws ResultNotAvailableException { + List> pair = new ArrayList>(); + List> options = rnaalifoldOpc.getAllOptions(); + + // take out -p options so it can be added to all commands later + // options = removeParam(options, "Partition Function"); + + options.addAll(rnaalifoldOpc.getAllParameters()); + List> failedOptionPairs = new ArrayList>(); + + boolean failed = true; + for (int i = 0; i args = rnaalifoldOpc.argumentsToCommandString(pair); + // add -p + // args.add("-p"); + try { + failed = singleRun(args); + } catch (ResultNotAvailableException e) { + System.out.println("Results not available: " + e.getMessage()); + failed = true; + } catch (JobSubmissionException e) { + e.printStackTrace(); + fail(e.getLocalizedMessage()); + } catch (JobExecutionException e) { + e.printStackTrace(); + fail(e.getLocalizedMessage()); + } catch (IOException e) { + e.printStackTrace(); + fail(e.getLocalizedMessage()); + } + if (failed == true) { + failedOptionPairs.add(args); + } + } + } + } + System.out.println("failedOptionPairs: " + failedOptionPairs); + } + + /* + * This test method stolen from the other parameter testing classes + * the only purpose of the Collections.shuffle(params) and the for loop + * is to test giving the executable the parameters in different orders + * which leads to a lot of (unnecessary?) tests with an argument list + * as long as this one. + * + */ + + + void test(List> params) { + for (int i = 0; i < params.size(); i++) { + List args = rnaalifoldOpc.argumentsToCommandString(params); + singleTest(args); + Collections.shuffle(params); + } + log.info("NUMBER OF COMBINATION TESTED: " + params.size()); + } + + // because some parameters presuppose the -p option + + void test(List> params, List precursor) { + + for (int i = 0; i < params.size(); i++) { + List args = rnaalifoldOpc.argumentsToCommandString(params); + args.addAll(precursor); + singleTest(args); + Collections.shuffle(params); + } + log.info("NUMBER OF COMBINATION TESTED: " + params.size()); + } + @Test + void singleParamTest() { + //List> params = rnaalifoldOpc.getAllParameters(); + //System.out.println("special: params: " + params); + + //List args = rnaalifoldOpc.argumentsToCommandString(params); + List args = new ArrayList(); + //args.add("-T 37"); args.add("-S 1.07"); args.add("--stochBT_en 10"); + // replace "=" with " " to fail test + args.add("--MEA=1"); + args.add("-p"); + singleTest(args); + + } + + void singleTest(List params) { + try { + log.info("Using arguments: " + params); + RNAalifold rnaalifold = new RNAalifold(); + rnaalifold.setInput(AllTestSuit.test_input_aln).setOutput(test_outfile); + + ConfiguredExecutable confRNAalifold = Configurator + .configureExecutable(rnaalifold, ExecProvider.Local); + // Add options to the executable + confRNAalifold.addParameters(params); + LocalRunner lr = new LocalRunner(confRNAalifold); + lr.executeJob(); + confRNAalifold = (ConfiguredExecutable) lr.waitForResult(); + assertNotNull(confRNAalifold.getResults(), "results is null"); + + System.out.println("Results: \n" + + ((RNAStructScoreManager) confRNAalifold.getResults()).toString()); + + File errors = new File(confRNAalifold.getWorkDirectory(), + ExecutableWrapper.PROC_ERR_FILE); + if (errors.length() != 0) { + log.error("PROBLEMS:\n " + FileUtil.readFileToString(errors)); + } + assertTrue(errors.length() == 0, "Run with arguments : " + params + + " FAILED!"); + Collections.shuffle(params); + } catch (JobSubmissionException e) { + e.printStackTrace(); + fail(e.getLocalizedMessage()); + } catch (JobExecutionException e) { + e.printStackTrace(); + fail(e.getLocalizedMessage()); + } catch (IOException e) { + e.printStackTrace(); + fail(e.getLocalizedMessage()); + } catch (ResultNotAvailableException e) { + e.printStackTrace(); + fail(e.getLocalizedMessage()); + } + } + + /* A version of singleTest that continues running instead of calling + * fail() when it encounters a problem + * + * Used to identify incompatible options and parameters + * returns -1 on failure + * + * Bad Progamming practice? + */ + + + boolean singleRun(List params) throws JobSubmissionException, + JobExecutionException, IOException, ResultNotAvailableException { + boolean fail = true; + log.info("Using arguments: " + params); + RNAalifold rnaalifold = new RNAalifold(); + rnaalifold.setInput(AllTestSuit.test_input_aln).setOutput(test_outfile); + + ConfiguredExecutable confRNAalifold = Configurator + .configureExecutable(rnaalifold, ExecProvider.Local); + // Add options to the executable + confRNAalifold.addParameters(params); + LocalRunner lr = new LocalRunner(confRNAalifold); + lr.executeJob(); + confRNAalifold = (ConfiguredExecutable) lr.waitForResult(); + + System.out.println("Results: \n" + + ((RNAStructScoreManager) confRNAalifold.getResults()).toString()); + if (confRNAalifold.getResults() != null) fail = false; + File errors = new File(confRNAalifold.getWorkDirectory(), + ExecutableWrapper.PROC_ERR_FILE); + if (errors.length() != 0) { + log.error("PROBLEMS:\n " + FileUtil.readFileToString(errors)); + } + assertTrue(errors.length() == 0, "Run with arguments : " + params + + " FAILED!"); + Collections.shuffle(params); + return fail; + } +} diff --git a/testsrc/compbio/runner/structure/RNAalifoldTester.java b/testsrc/compbio/runner/structure/RNAalifoldTester.java index 4f5b508..0249215 100644 --- a/testsrc/compbio/runner/structure/RNAalifoldTester.java +++ b/testsrc/compbio/runner/structure/RNAalifoldTester.java @@ -1,118 +1,119 @@ -package compbio.runner.structure; - - -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertFalse; -import static org.testng.Assert.assertNotNull; -import static org.testng.Assert.assertNull; -import static org.testng.Assert.assertTrue; -import static org.testng.Assert.fail; - -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.text.ParseException; - -import javax.xml.bind.ValidationException; - -import org.apache.log4j.*; -import org.ggf.drmaa.DrmaaException; -import org.ggf.drmaa.JobInfo; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.Test; - -import compbio.data.sequence.Score; -import compbio.data.sequence.ScoreManager; -import compbio.data.sequence.ScoreManager.ScoreHolder; -import compbio.data.sequence.SequenceUtil; -import compbio.engine.AsyncExecutor; -import compbio.engine.Configurator; -import compbio.engine.SyncExecutor; -import compbio.engine.client.ConfExecutable; -import compbio.engine.client.ConfiguredExecutable; -import compbio.engine.client.Executable; -import compbio.engine.client.RunConfiguration; -import compbio.engine.cluster.drmaa.ClusterUtil; -import compbio.engine.cluster.drmaa.JobRunner; -import compbio.engine.cluster.drmaa.StatisticManager; -import compbio.engine.local.AsyncLocalRunner; -import compbio.engine.local.LocalExecutorService; -import compbio.engine.local.LocalRunner; -import compbio.metadata.AllTestSuit; -import compbio.metadata.JobExecutionException; -import compbio.metadata.JobStatus; -import compbio.metadata.JobSubmissionException; -import compbio.metadata.LimitsManager; -import compbio.metadata.PresetManager; -import compbio.metadata.ResultNotAvailableException; -import compbio.metadata.RunnerConfig; -import compbio.runner.msa.ClustalW; -import compbio.runner.structure.RNAalifold; - -public class RNAalifoldTester { - - private static Logger log = Logger - .getLogger(AllTestSuit.RUNNER_TEST_LOGGER); - - - private RNAalifold rnaalifold; - - static final String rnaalifoldConfigFile = AllTestSuit.TEST_DATA_PATH - + "RNAalifoldParameters.xml"; - public static String test_outfile = "rnaalifold.out"; - - - @Test(groups = { AllTestSuit.test_group_runner }) - public void testRunLocally() { - RNAalifold rnaalifold = new RNAalifold(); - rnaalifold.setInput(AllTestSuit.test_input_aln).setOutput(test_outfile); - try{ - - ConfiguredExecutable confRNAalifold = Configurator - .configureExecutable(rnaalifold, Executable.ExecProvider.Local); - LocalRunner lr = new LocalRunner(confRNAalifold); - lr.executeJob(); - confRNAalifold = (ConfiguredExecutable) lr.waitForResult(); - - System.out.println("TEST"); - System.out.println(((ScoreManager) confRNAalifold.getResults()).asRNAStruct().toString()); - - assertNotNull(confRNAalifold.getResults()); - } catch (JobSubmissionException e) { - e.printStackTrace(); - fail(e.getLocalizedMessage()); - } catch (JobExecutionException e) { - e.printStackTrace(); - fail(e.getLocalizedMessage()); - } catch (ResultNotAvailableException e) { - e.printStackTrace(); - fail(e.getLocalizedMessage()); - } - } - - - public static void main(String[] args) throws JobSubmissionException, - JobExecutionException, InterruptedException, ResultNotAvailableException { - - - log.warn("Logger test :- Run RNAalifold.main()"); - - RNAalifold rnaalifold = new RNAalifold(); - rnaalifold.setInput(AllTestSuit.test_input_aln).setOutput("test_outfile.txt"); - - ConfiguredExecutable confRNAalifold = Configurator - .configureExecutable(rnaalifold); - AsyncExecutor lr = new AsyncLocalRunner(); - lr.submitJob(confRNAalifold); - - - System.out.println(((ScoreManager) confRNAalifold.getResults()).asRNAStruct().toString()); - - - Thread.sleep(3000); - LocalExecutorService.shutDown(); - - } - -} +package compbio.runner.structure; + + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertFalse; +import static org.testng.Assert.assertNotNull; +import static org.testng.Assert.assertNull; +import static org.testng.Assert.assertTrue; +import static org.testng.Assert.fail; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.text.ParseException; + +import javax.xml.bind.ValidationException; + +import org.apache.log4j.*; +import org.ggf.drmaa.DrmaaException; +import org.ggf.drmaa.JobInfo; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import compbio.data.sequence.RNAStructScoreManager; +import compbio.data.sequence.Score; +import compbio.data.sequence.ScoreManager; +import compbio.data.sequence.ScoreManager.ScoreHolder; +import compbio.data.sequence.SequenceUtil; +import compbio.engine.AsyncExecutor; +import compbio.engine.Configurator; +import compbio.engine.SyncExecutor; +import compbio.engine.client.ConfExecutable; +import compbio.engine.client.ConfiguredExecutable; +import compbio.engine.client.Executable; +import compbio.engine.client.RunConfiguration; +import compbio.engine.cluster.drmaa.ClusterUtil; +import compbio.engine.cluster.drmaa.JobRunner; +import compbio.engine.cluster.drmaa.StatisticManager; +import compbio.engine.local.AsyncLocalRunner; +import compbio.engine.local.LocalExecutorService; +import compbio.engine.local.LocalRunner; +import compbio.metadata.AllTestSuit; +import compbio.metadata.JobExecutionException; +import compbio.metadata.JobStatus; +import compbio.metadata.JobSubmissionException; +import compbio.metadata.LimitsManager; +import compbio.metadata.PresetManager; +import compbio.metadata.ResultNotAvailableException; +import compbio.metadata.RunnerConfig; +import compbio.runner.msa.ClustalW; +import compbio.runner.structure.RNAalifold; + +public class RNAalifoldTester { + + private static Logger log = Logger + .getLogger(AllTestSuit.RUNNER_TEST_LOGGER); + + + private RNAalifold rnaalifold; + + static final String rnaalifoldConfigFile = AllTestSuit.TEST_DATA_PATH + + "RNAalifoldParameters.xml"; + public static String test_outfile = "rnaalifold.out"; + + + @Test(groups = { AllTestSuit.test_group_runner }) + public void testRunLocally() { + RNAalifold rnaalifold = new RNAalifold(); + rnaalifold.setInput(AllTestSuit.test_input_aln).setOutput(test_outfile); + try{ + + ConfiguredExecutable confRNAalifold = Configurator + .configureExecutable(rnaalifold, Executable.ExecProvider.Local); + LocalRunner lr = new LocalRunner(confRNAalifold); + lr.executeJob(); + confRNAalifold = (ConfiguredExecutable) lr.waitForResult(); + + System.out.println("TEST"); + System.out.println(((RNAStructScoreManager) confRNAalifold.getResults()).toString()); + + assertNotNull(confRNAalifold.getResults()); + } catch (JobSubmissionException e) { + e.printStackTrace(); + fail(e.getLocalizedMessage()); + } catch (JobExecutionException e) { + e.printStackTrace(); + fail(e.getLocalizedMessage()); + } catch (ResultNotAvailableException e) { + e.printStackTrace(); + fail(e.getLocalizedMessage()); + } + } + + + public static void main(String[] args) throws JobSubmissionException, + JobExecutionException, InterruptedException, ResultNotAvailableException { + + + log.warn("Logger test :- Run RNAalifold.main()"); + + RNAalifold rnaalifold = new RNAalifold(); + rnaalifold.setInput(AllTestSuit.test_input_aln).setOutput("test_outfile.txt"); + + ConfiguredExecutable confRNAalifold = Configurator + .configureExecutable(rnaalifold); + AsyncExecutor lr = new AsyncLocalRunner(); + lr.submitJob(confRNAalifold); + + + System.out.println(((RNAStructScoreManager) confRNAalifold.getResults()).toString()); + + + Thread.sleep(3000); + LocalExecutorService.shutDown(); + + } + +} diff --git a/testsrc/compbio/ws/client/TestRNAalifoldWS.java b/testsrc/compbio/ws/client/TestRNAalifoldWS.java index c5f3f6d..7a844ea 100644 --- a/testsrc/compbio/ws/client/TestRNAalifoldWS.java +++ b/testsrc/compbio/ws/client/TestRNAalifoldWS.java @@ -1,113 +1,114 @@ - package compbio.ws.client; - -import compbio.metadata.AllTestSuit; -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertNotNull; -import static org.testng.Assert.fail; - -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.net.ConnectException; -import java.util.ArrayList; -import java.util.List; - -import javax.xml.ws.WebServiceException; - -import org.testng.annotations.BeforeTest; -import org.testng.annotations.Test; - -import compbio.data.msa.FoldWS; -import compbio.data.msa.JABAService; -import compbio.data.msa.SequenceAnnotation; -import compbio.data.sequence.Alignment; -import compbio.data.sequence.RNAStruct; -import compbio.data.sequence.ClustalAlignmentUtil; -import compbio.data.sequence.ConservationMethod; -import compbio.data.sequence.FastaSequence; -import compbio.data.sequence.ScoreManager; -import compbio.data.sequence.SequenceUtil; -import compbio.data.sequence.UnknownFileFormatException; -import compbio.metadata.AllTestSuit; -import compbio.metadata.JobSubmissionException; -import compbio.metadata.LimitExceededException; -import compbio.metadata.Option; -import compbio.metadata.PresetManager; -import compbio.metadata.ResultNotAvailableException; -import compbio.metadata.RunnerConfig; -import compbio.metadata.UnsupportedRuntimeException; -import compbio.metadata.WrongParameterException; -import compbio.runner.conservation.AACon; -import compbio.util.SysPrefs; -import compbio.ws.server.RNAalifoldWS; - - -public class TestRNAalifoldWS { - - SequenceAnnotation foldws; - - @BeforeTest(groups = {AllTestSuit.test_group_webservices}) - void initConnection() { - - try { - JABAService client = Jws2Client.connect( - "http://localhost:8080/jabaws", Services.RNAalifoldWS); - foldws = (SequenceAnnotation) client; - } catch (ConnectException e) { - e.printStackTrace(); - fail(e.getMessage()); - } catch (WebServiceException e) { - e.printStackTrace(); - fail(e.getMessage()); - } - } - - - @Test(groups = {AllTestSuit.test_group_webservices}) - public void testFold() throws FileNotFoundException, IOException, - UnknownFileFormatException { - -// String CURRENT_DIRECTORY = SysPrefs.getCurrentDirectory() -// + File.separator; - - Alignment aln = ClustalAlignmentUtil.readClustalFile(new FileInputStream( - AllTestSuit.test_input_aln)); - - List fsl = aln.getSequences(); - - try { - List> options = new ArrayList>(); - options.add(foldws.getRunnerOptions().getArgumentByOptionName("--mis")); - options.add(foldws.getRunnerOptions().getArgumentByOptionName("-p")); - options.add(foldws.getRunnerOptions().getArgumentByOptionName("--MEA")); - - System.out.println("TestRNAalifoldWS: print options: " + options.toString()); - - String jobId = foldws.customAnalize(fsl, options); - System.out.println("J: " + jobId); - ScoreManager result = foldws.getAnnotation(jobId); - System.out.println("fold results: \n" + result.asRNAStruct().toString()); - assertNotNull(result); - - } catch (UnsupportedRuntimeException e) { - e.printStackTrace(); - fail(e.getMessage()); - } catch (LimitExceededException e) { - e.printStackTrace(); - fail(e.getMessage()); - } catch (JobSubmissionException e) { - e.printStackTrace(); - fail(e.getMessage()); - } catch (ResultNotAvailableException e) { - e.printStackTrace(); - fail(e.getMessage()); - } catch (WrongParameterException e) { - e.printStackTrace(); - fail(e.getMessage()); - } - } -} - - - + package compbio.ws.client; + +import static org.testng.Assert.assertNotNull; +import static org.testng.Assert.fail; + +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.io.Writer; +import java.net.ConnectException; +import java.util.ArrayList; +import java.util.List; + +import javax.xml.ws.WebServiceException; + +import org.testng.annotations.BeforeTest; +import org.testng.annotations.Test; + +import compbio.data.msa.JABAService; +import compbio.data.msa.SequenceAnnotation; +import compbio.data.sequence.Alignment; +import compbio.data.sequence.ClustalAlignmentUtil; +import compbio.data.sequence.FastaSequence; +import compbio.data.sequence.RNAStructScoreManager; +import compbio.data.sequence.ScoreManager; +import compbio.data.sequence.UnknownFileFormatException; +import compbio.metadata.AllTestSuit; +import compbio.metadata.JobSubmissionException; +import compbio.metadata.LimitExceededException; +import compbio.metadata.Option; +import compbio.metadata.ResultNotAvailableException; +import compbio.metadata.UnsupportedRuntimeException; +import compbio.metadata.WrongParameterException; +import compbio.runner.structure.RNAalifold; +import compbio.ws.server.RNAalifoldWS; + + +public class TestRNAalifoldWS { + +// SequenceAnnotation foldws; + RNAalifoldWS foldws; + + @BeforeTest(groups = {AllTestSuit.test_group_webservices}) + void initConnection() { + + try { + JABAService client = Jws2Client.connect( + "http://localhost:8080/jabaws", Services.RNAalifoldWS); + foldws = (RNAalifoldWS) client; + } catch (ConnectException e) { + e.printStackTrace(); + fail(e.getMessage()); + } catch (WebServiceException e) { + e.printStackTrace(); + fail(e.getMessage()); + } + } + + + @Test(groups = {AllTestSuit.test_group_webservices}) + public void testFold() throws FileNotFoundException, IOException, + UnknownFileFormatException { + +// String CURRENT_DIRECTORY = SysPrefs.getCurrentDirectory() +// + File.separator; + + Alignment aln = ClustalAlignmentUtil.readClustalFile(new FileInputStream( + AllTestSuit.test_input_aln)); + + List fsl = aln.getSequences(); + + try { + List> options = new ArrayList>(); + options.add(foldws.getRunnerOptions().getArgumentByOptionName("--mis")); + options.add(foldws.getRunnerOptions().getArgumentByOptionName("-p")); + options.add(foldws.getRunnerOptions().getArgumentByOptionName("--MEA")); + +// System.out.println("TestRNAalifoldWS: print options: " + options.toString()); + + String jobId = foldws.customAnalize(fsl, options); + System.out.println("J: " + jobId); + + Writer stdout = new OutputStreamWriter(System.out); + + System.out.println("results class: " + foldws.getAnnotation(jobId).getClass().getName()); + RNAStructScoreManager result = (RNAStructScoreManager)foldws.getAnnotation(jobId); + + + result.writeOut(stdout); + System.out.println("fold results: \n" + result.toString()); + assertNotNull(result); + + } catch (UnsupportedRuntimeException e) { + e.printStackTrace(); + fail(e.getMessage()); + } catch (LimitExceededException e) { + e.printStackTrace(); + fail(e.getMessage()); + } catch (JobSubmissionException e) { + e.printStackTrace(); + fail(e.getMessage()); + } catch (ResultNotAvailableException e) { + e.printStackTrace(); + fail(e.getMessage()); + } catch (WrongParameterException e) { + e.printStackTrace(); + fail(e.getMessage()); + } + } +} + + + diff --git a/webservices/compbio/ws/server/RNAalifoldWS.java b/webservices/compbio/ws/server/RNAalifoldWS.java index 34f0aaf..beca1dc 100644 --- a/webservices/compbio/ws/server/RNAalifoldWS.java +++ b/webservices/compbio/ws/server/RNAalifoldWS.java @@ -1,66 +1,83 @@ -package compbio.ws.server; - -import java.io.File; -import java.util.List; - -import javax.jws.WebParam; -import javax.jws.WebService; - -import org.apache.log4j.Logger; - -import compbio.data.msa.FoldWS; -import compbio.data.msa.JABAService; -import compbio.data.msa.JManagement; -import compbio.data.msa.Metadata; -import compbio.data.msa.SequenceAnnotation; -import compbio.data.sequence.Alignment; -import compbio.data.sequence.RNAStruct; -import compbio.data.sequence.FastaSequence; -import compbio.engine.AsyncExecutor; -import compbio.engine.Configurator; -import compbio.engine.client.ConfiguredExecutable; -import compbio.engine.client.SkeletalExecutable; -import compbio.metadata.ChunkHolder; -import compbio.metadata.JobStatus; -import compbio.metadata.JobSubmissionException; -import compbio.metadata.Limit; -import compbio.metadata.LimitExceededException; -import compbio.metadata.LimitsManager; -import compbio.metadata.Option; -import compbio.metadata.Preset; -import compbio.metadata.PresetManager; -import compbio.metadata.ResultNotAvailableException; -import compbio.metadata.RunnerConfig; -import compbio.metadata.UnsupportedRuntimeException; -import compbio.metadata.WrongParameterException; -import compbio.runner.Util; -import compbio.runner.disorder.GlobPlot; -import compbio.runner.structure.RNAalifold; - -@WebService(endpointInterface = "compbio.data.msa.SequenceAnnotation", targetNamespace = JABAService.V2_SERVICE_NAMESPACE, serviceName = "RNAalifoldWS") -public class RNAalifoldWS extends SequenceAnnotationService - implements - SequenceAnnotation { - - private static Logger log = Logger.getLogger(ClustalWS.class); - - public RNAalifoldWS() { - super (new RNAalifold(), log); - } - - - /* - * No presets are supported, thus the result of this call will be as simple - * call to analize without parameters - */ - @Override - public String presetAnalize(List sequences, - Preset preset) throws UnsupportedRuntimeException, - LimitExceededException, JobSubmissionException, - WrongParameterException { - - return analize(sequences); - } -} - - +package compbio.ws.server; + +import java.util.List; + +import javax.jws.WebService; + +import org.apache.log4j.Logger; + +import compbio.data.msa.JABAService; +import compbio.data.msa.SequenceAnnotation; +import compbio.data.sequence.FastaSequence; +import compbio.data.sequence.RNAStructScoreManager; +import compbio.engine.client.ConfiguredExecutable; +import compbio.metadata.JobSubmissionException; +import compbio.metadata.LimitExceededException; +import compbio.metadata.Option; +import compbio.metadata.Preset; +import compbio.metadata.ResultNotAvailableException; +import compbio.metadata.UnsupportedRuntimeException; +import compbio.metadata.WrongParameterException; +import compbio.runner.conservation.AACon; +import compbio.runner.structure.RNAalifold; + +@WebService(endpointInterface = "compbio.data.msa.SequenceAnnotation", targetNamespace = JABAService.V2_SERVICE_NAMESPACE, serviceName = "RNAalifoldWS") +public class RNAalifoldWS extends SequenceAnnotationService + implements + SequenceAnnotation { + + private static Logger log = Logger.getLogger(RNAalifoldWS.class); + + public RNAalifoldWS() { + super (new RNAalifold(), log); + } + + + // for testing + @Override + public RNAStructScoreManager getAnnotation(String jobId) + throws ResultNotAvailableException { + return WSUtil.getAnnotation(jobId, log); + } + + @Override + public String analize(List sequences) + throws UnsupportedRuntimeException, LimitExceededException, + JobSubmissionException { + WSUtil.validateFastaInput(sequences); + ConfiguredExecutable confRNAalifold = init(sequences); + return WSUtil.fold(sequences, confRNAalifold, log, "analize", + getLimit("")); + } + + @Override + public String customAnalize(List sequences, + List> options) throws UnsupportedRuntimeException, + LimitExceededException, JobSubmissionException, + WrongParameterException { + WSUtil.validateFastaInput(sequences); + ConfiguredExecutable confRNAalifold = init(sequences); + + List params = WSUtil.getCommands(options, + AACon.KEY_VALUE_SEPARATOR); + confRNAalifold.addParameters(params); + return WSUtil.fold(sequences, confRNAalifold, log, "customAnalize", + getLimit("")); + } + + + /* + * No presets are supported, thus the result of this call will be as simple + * call to analize without parameters + */ + @Override + public String presetAnalize(List sequences, + Preset preset) throws UnsupportedRuntimeException, + LimitExceededException, JobSubmissionException, + WrongParameterException { + + return analize(sequences); + } +} + + -- 1.7.10.2