|
| 1 | +package com.thealgorithms.dynamicprogramming; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +import es.uma.eda.AlgorithmUtil; |
| 7 | +import es.uma.eda.problem.combinatorial.sequence.SequenceUtil; |
| 8 | +import es.uma.eda.problem.combinatorial.sequence.folding.RNASecondaryStructurePredictor; |
| 9 | + |
| 10 | +/** |
| 11 | + * Main class for testing RNA folding algorithms |
| 12 | + * @author ccottap |
| 13 | + * |
| 14 | + */ |
| 15 | +public class TestRNAFolding { |
| 16 | + |
| 17 | + /** |
| 18 | + * Filename for storing statistics |
| 19 | + */ |
| 20 | + private final static String outputFilename = "folding.txt"; |
| 21 | + /** |
| 22 | + * how much the size of sequences is scaled-up on each step |
| 23 | + */ |
| 24 | + private final static double scaleFactor = 1.5; |
| 25 | + |
| 26 | + /** |
| 27 | + * Finds the best folding of an RNA sequence |
| 28 | + * @param args command-line arguments (to read data from file or generate at random) |
| 29 | + * @throws IOException if data cannot be read from file or statistics cannot be written to file |
| 30 | + */ |
| 31 | + public static void main(String[] args) throws IOException { |
| 32 | + if (args.length<1) { |
| 33 | + System.out.println("java TestRNAFolding (-s|-f|-r) <arguments>"); |
| 34 | + System.out.println("\t-s: folds a string given as parameter"); |
| 35 | + System.out.println("\t-f: performs a batch test from a file"); |
| 36 | + System.out.println("\t-r: measures time"); |
| 37 | + } |
| 38 | + else { |
| 39 | + List<String> sequences = null; |
| 40 | + RNASecondaryStructurePredictor predictor = new Nussinov(); |
| 41 | + |
| 42 | + switch (args[0]) { |
| 43 | + case "-s": |
| 44 | + if (args.length < 3) { |
| 45 | + System.out.println("java TestRNAFolding -s <string> <min-loop-size>"); |
| 46 | + System.exit(-1); |
| 47 | + } |
| 48 | + else { |
| 49 | + String f = predictor.run(args[1], Integer.parseInt(args[2])); |
| 50 | + System.out.println("Folding " + args[1] + "..."); |
| 51 | + System.out.println("Result:\n\t" + (int)predictor.evaluate(args[1],f) + " base pairs\n\t" + args[1] + "\n\t" + f); |
| 52 | + } |
| 53 | + break; |
| 54 | + |
| 55 | + case "-f": |
| 56 | + if (args.length < 3) { |
| 57 | + System.out.println("java TestRNAFolding -f <filename> <min-loop-size>"); |
| 58 | + System.exit(-1); |
| 59 | + } |
| 60 | + else { |
| 61 | + sequences = SequenceUtil.readSequencesFromFile(args[1]); |
| 62 | + predictor.setVerbosity(false); |
| 63 | + for (String s: sequences) { |
| 64 | + String f = predictor.run(s, Integer.parseInt(args[2])); |
| 65 | + System.out.println((int)predictor.evaluate(s, f)); |
| 66 | + } |
| 67 | + } |
| 68 | + break; |
| 69 | + |
| 70 | + case "-r": |
| 71 | + if (args.length < 5) { |
| 72 | + System.out.println("java TestRNAFolding -r <initial-lenght> <doublings> <tests-per-length> <min-loop-size>"); |
| 73 | + System.exit(-1); |
| 74 | + } |
| 75 | + else { |
| 76 | + int initialLength = Integer.parseUnsignedInt(args[1]); |
| 77 | + int doublings = Integer.parseUnsignedInt(args[2]); |
| 78 | + int testsPerLength = Integer.parseUnsignedInt(args[3]); |
| 79 | + int minLoopSize = Integer.parseUnsignedInt(args[4]); |
| 80 | + double[][] statistics = runTimer(predictor, initialLength, doublings, testsPerLength, minLoopSize); |
| 81 | + AlgorithmUtil.writeStats(outputFilename, statistics); |
| 82 | + } |
| 83 | + break; |
| 84 | + |
| 85 | + default: |
| 86 | + System.out.println("Wrong argument: " + args[0]); |
| 87 | + System.exit(-1); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Performs a series of timed experiments with sequences of different sizes |
| 95 | + * @param predictor the structure prediction algorithm |
| 96 | + * @param initialLength initial length of sequences |
| 97 | + * @param doublings number of times the sequence length is doubled |
| 98 | + * @param testsPerLength number of tests per sequence length |
| 99 | + * @param minLoopSize minimum number of nucleotides enclosed in loops |
| 100 | + * @return a matrix with computational times (one row per size, one column per test). |
| 101 | + */ |
| 102 | + private static double[][] runTimer(RNASecondaryStructurePredictor predictor , int initialLength, int doublings, int testsPerLength, int minLoopSize) { |
| 103 | + final String alphabet = "ACGU"; |
| 104 | + double[][] statistics = new double[doublings][testsPerLength+1]; |
| 105 | + double interval; |
| 106 | + |
| 107 | + predictor.setVerbosity(false); |
| 108 | + for (int l = initialLength, dup = 0; dup < doublings; l *= scaleFactor, dup++) { |
| 109 | + statistics[dup][0] = l; |
| 110 | + System.out.println("Trying sequences of length " + l); |
| 111 | + for (int j=0; j<testsPerLength; j++) { |
| 112 | + String s = SequenceUtil.randomString(l, alphabet); |
| 113 | + predictor.run(s, minLoopSize); |
| 114 | + interval = predictor.getTime(); |
| 115 | + statistics[dup][j+1] = interval; |
| 116 | + System.out.println(predictor.getName() + " took " + interval + "s"); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + return statistics; |
| 121 | + |
| 122 | + } |
| 123 | + |
| 124 | +} |
0 commit comments