|
| 1 | +package tools; |
| 2 | + |
| 3 | +import ru.ifmo.genetics.Runner; |
| 4 | +import ru.ifmo.genetics.statistics.Timer; |
| 5 | +import ru.ifmo.genetics.utils.tool.ExecutionFailedException; |
| 6 | +import ru.ifmo.genetics.utils.tool.Parameter; |
| 7 | +import ru.ifmo.genetics.utils.tool.Tool; |
| 8 | +import ru.ifmo.genetics.utils.tool.inputParameterBuilder.BoolParameterBuilder; |
| 9 | +import ru.ifmo.genetics.utils.tool.inputParameterBuilder.DoubleParameterBuilder; |
| 10 | +import ru.ifmo.genetics.utils.tool.inputParameterBuilder.FileMVParameterBuilder; |
| 11 | +import ru.ifmo.genetics.utils.tool.inputParameterBuilder.IntParameterBuilder; |
| 12 | + |
| 13 | +import java.io.File; |
| 14 | +import java.io.FileNotFoundException; |
| 15 | +import java.io.PrintWriter; |
| 16 | +import java.text.SimpleDateFormat; |
| 17 | + |
| 18 | +public class StatsFeaturesBuilderMain extends Tool { |
| 19 | + public static final String NAME = "stats-features"; |
| 20 | + public static final String DESCRIPTION = "Builds statistically significant features for group of metagenomic samples"; |
| 21 | + |
| 22 | + |
| 23 | + static { |
| 24 | + forceParameter.set(true); |
| 25 | + launchOptions.remove(forceParameter); |
| 26 | + } |
| 27 | + |
| 28 | + // input parameters |
| 29 | + public final Parameter<Integer> k = addParameter(new IntParameterBuilder("k") |
| 30 | + .mandatory() |
| 31 | + .withShortOpt("k") |
| 32 | + .withDescription("k-mer size (in nucleotides, maximum 31 due to realization details)") |
| 33 | + .withDescriptionShort("k-mer size") |
| 34 | + .withDescriptionRu("Длина k-мера при построении графа де Брейна") |
| 35 | + .withDescriptionRuShort("Длина k-мера") |
| 36 | + .create()); |
| 37 | + |
| 38 | + public final Parameter<File[]> positiveFiles = addParameter(new FileMVParameterBuilder("positiveReads") |
| 39 | + .withShortOpt("pos") |
| 40 | + .mandatory() |
| 41 | + .withDescription("list of reads files from positive group. FASTQ, FASTA") |
| 42 | + .create()); |
| 43 | + |
| 44 | + public final Parameter<File[]> negativeFiles = addParameter(new FileMVParameterBuilder("negativeReads") |
| 45 | + .withShortOpt("neg") |
| 46 | + .mandatory() |
| 47 | + .withDescription("list of reads files from negative group. FASTQ, FASTA") |
| 48 | + .create()); |
| 49 | + |
| 50 | + public final Parameter<Double> PValueChi2 = addParameter(new DoubleParameterBuilder("p-value-chi2") |
| 51 | + .withShortOpt("pchi2") |
| 52 | + .withDescription("p-value for chi-squared test") |
| 53 | + .withDefaultValue(0.05) |
| 54 | + .create()); |
| 55 | + |
| 56 | + public final Parameter<Double> PValueMW = addParameter(new DoubleParameterBuilder("p-value-mw") |
| 57 | + .withShortOpt("pmw") |
| 58 | + .withDescription("p-value for Mann-Whitney test") |
| 59 | + .withDefaultValue(0.05) |
| 60 | + .create()); |
| 61 | + |
| 62 | + public final Parameter<Boolean> splitComponents = addParameter(new BoolParameterBuilder("split") |
| 63 | + .withDescription("Save each component in separate file?") |
| 64 | + .withDefaultValue(false) |
| 65 | + .create()); |
| 66 | + |
| 67 | + public final Parameter<Integer> maximalBadFrequency = addParameter(new IntParameterBuilder("maximal-bad-frequency") |
| 68 | + .important() |
| 69 | + .withShortOpt("b") |
| 70 | + .withDescription("maximal frequency for a k-mer to be assumed erroneous") |
| 71 | + .withDefaultValue(1) |
| 72 | + .withDescriptionShort("Maximal bad frequency") |
| 73 | + .withDescriptionRu("Максимальная частота ошибочного k-мера") |
| 74 | + .withDescriptionRuShort("Максимальная частота ошибочного k-мера") |
| 75 | + .create()); |
| 76 | + |
| 77 | + |
| 78 | + public final File[] outputDescFiles = new File[]{ |
| 79 | + new File("output_description.txt"), |
| 80 | + workDir.append("output_description.txt").get()}; |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | + // adding sub tools |
| 85 | + public KmersCounterPositiveNegative kmersCounterPosNeg = new KmersCounterPositiveNegative(); |
| 86 | + { |
| 87 | + setFix(kmersCounterPosNeg.k, k); |
| 88 | + setFix(kmersCounterPosNeg.positiveFiles, positiveFiles); |
| 89 | + setFix(kmersCounterPosNeg.negativeFiles, negativeFiles); |
| 90 | + setFix(kmersCounterPosNeg.maximalBadFrequency, maximalBadFrequency); |
| 91 | + setFixDefault(kmersCounterPosNeg.outputDir); |
| 92 | + kmersCounterPosNeg.outputDescFiles = outputDescFiles; |
| 93 | + addSubTool(kmersCounterPosNeg); |
| 94 | + } |
| 95 | + |
| 96 | + public StatsKmersFinder statsKmers = new StatsKmersFinder(); |
| 97 | + { |
| 98 | + setFix(statsKmers.Afiles, kmersCounterPosNeg.resultingPositiveKmerFiles); |
| 99 | + setFix(statsKmers.Bfiles, kmersCounterPosNeg.resultingNegativeKmerFiles); |
| 100 | + setFix(statsKmers.PValueChi2, PValueChi2); |
| 101 | + setFix(statsKmers.PValueMW, PValueMW); |
| 102 | + setFix(statsKmers.maximalBadFrequency, maximalBadFrequency); |
| 103 | + setFixDefault(statsKmers.outputDir); |
| 104 | + addSubTool(statsKmers); |
| 105 | + } |
| 106 | + |
| 107 | + |
| 108 | + public ComponentExtractorMain compExtractor = new ComponentExtractorMain(); |
| 109 | + { |
| 110 | + setFix(compExtractor.k, k); |
| 111 | + setFix(compExtractor.inputFiles, kmersCounterPosNeg.resultingPositiveKmerFiles); |
| 112 | + setFix(compExtractor.pivotFiles, statsKmers.resultingKmerFiles); |
| 113 | + addSubTool(compExtractor); |
| 114 | + } |
| 115 | + |
| 116 | + public FeaturesCalculatorMain featuresCalculator = new FeaturesCalculatorMain(); |
| 117 | + { |
| 118 | + setFix(featuresCalculator.k, k); |
| 119 | + setFix(featuresCalculator.kmersFiles, kmersCounterPosNeg.resultingPositiveKmerFiles); |
| 120 | + setFix(featuresCalculator.componentsFile, compExtractor.componentsFile); |
| 121 | + setFix(featuresCalculator.selectedKmers, statsKmers.resultingKmerFiles); |
| 122 | + addSubTool(featuresCalculator); |
| 123 | + } |
| 124 | + |
| 125 | + public ComponentsToSequences comp2seq = new ComponentsToSequences(); |
| 126 | + { |
| 127 | + setFix(comp2seq.k, k); |
| 128 | + setFix(comp2seq.componentsFile, compExtractor.componentsFile); |
| 129 | + setFix(comp2seq.splitComponents, splitComponents); |
| 130 | + addSubTool(comp2seq); |
| 131 | + } |
| 132 | + |
| 133 | + |
| 134 | + private Timer t; |
| 135 | + |
| 136 | + @Override |
| 137 | + protected void runImpl() throws ExecutionFailedException { |
| 138 | + // preparing |
| 139 | + t = new Timer(); |
| 140 | + |
| 141 | + info("Found " + positiveFiles.get().length + " samples in positive class and " |
| 142 | + + negativeFiles.get().length + " samples in negative class to process"); |
| 143 | + if (positiveFiles.get().length == 0 || negativeFiles.get().length == 0) { |
| 144 | + throw new ExecutionFailedException("No libraries to process!!! Can't continue the calculations."); |
| 145 | + } |
| 146 | + |
| 147 | + |
| 148 | + outputDescFiles[1] = workDir.append("output_description.txt").get(); // updating workdir |
| 149 | + createOutputDescFiles(); |
| 150 | + |
| 151 | + // running steps |
| 152 | + addStep(kmersCounterPosNeg); |
| 153 | + addStep(statsKmers); |
| 154 | + addStep(compExtractor); |
| 155 | + addStep(featuresCalculator); |
| 156 | + addStep(comp2seq); |
| 157 | + } |
| 158 | + |
| 159 | + private void createOutputDescFiles() { |
| 160 | + for (File f : outputDescFiles) { |
| 161 | + try { |
| 162 | + PrintWriter out = new PrintWriter(f); |
| 163 | + out.println("# Output files' description for run started at " + |
| 164 | + new SimpleDateFormat("dd-MMM-yyyy (EEE) HH:mm:ss").format(startDate)); |
| 165 | + out.println(); |
| 166 | + for (File ff : getLogFiles()) { |
| 167 | + out.println(ff); |
| 168 | + } |
| 169 | + out.println(" Identical files with run log"); |
| 170 | + |
| 171 | + out.println(); |
| 172 | + for (File ff : outputDescFiles) { |
| 173 | + out.println(ff); |
| 174 | + } |
| 175 | + out.println(" Identical files with output files' description"); |
| 176 | + out.close(); |
| 177 | + } catch (FileNotFoundException e) { |
| 178 | + warn("Can't create file " + f + ", skipping"); |
| 179 | + debug(e.getClass().getName() + ": " + e.getMessage()); |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + |
| 185 | + @Override |
| 186 | + protected void cleanImpl() { |
| 187 | + debug("Statistically significant features builder has finished! Time = " + t); |
| 188 | + } |
| 189 | + |
| 190 | + |
| 191 | + @Override |
| 192 | + public void mainImpl(String[] args) { |
| 193 | + if (Runner.containsOption(args, Runner.getOptKeys(continueParameter)) || |
| 194 | + Runner.containsOption(args, Runner.getOptKeys(Tool.startParameter))) { |
| 195 | + forceParameter.set(false); |
| 196 | + } |
| 197 | + super.mainImpl(args); |
| 198 | + } |
| 199 | + |
| 200 | + public static void main(String[] args) { |
| 201 | + new StatsFeaturesBuilderMain().mainImpl(args); |
| 202 | + } |
| 203 | + |
| 204 | + public StatsFeaturesBuilderMain() { |
| 205 | + super(NAME, DESCRIPTION); |
| 206 | + } |
| 207 | +} |
0 commit comments