|
| 1 | +package de.unijena.bioinf.ms.frontend.subtools.selftest; |
| 2 | + |
| 3 | +import de.unijena.bioinf.ChemistryBase.chem.MolecularFormula; |
| 4 | +import de.unijena.bioinf.ChemistryBase.chem.PrecursorIonType; |
| 5 | +import de.unijena.bioinf.ChemistryBase.jobs.SiriusJobs; |
| 6 | +import de.unijena.bioinf.ChemistryBase.ms.Ms2Experiment; |
| 7 | +import de.unijena.bioinf.ChemistryBase.ms.MutableMs2Experiment; |
| 8 | +import de.unijena.bioinf.ChemistryBase.ms.ft.FTree; |
| 9 | +import de.unijena.bioinf.ChemistryBase.ms.ft.FragmentAnnotation; |
| 10 | +import de.unijena.bioinf.ChemistryBase.ms.ft.Score; |
| 11 | +import de.unijena.bioinf.ChemistryBase.ms.utils.SimpleSpectrum; |
| 12 | +import de.unijena.bioinf.FragmentationTreeConstruction.computation.FasterTreeComputationInstance; |
| 13 | +import de.unijena.bioinf.FragmentationTreeConstruction.computation.FragmentationPatternAnalysis; |
| 14 | +import de.unijena.bioinf.FragmentationTreeConstruction.computation.tree.TreeBuilder; |
| 15 | +import de.unijena.bioinf.FragmentationTreeConstruction.computation.tree.TreeBuilderFactory; |
| 16 | +import de.unijena.bioinf.IsotopePatternAnalysis.ExtractedIsotopePattern; |
| 17 | +import de.unijena.bioinf.babelms.GenericParser; |
| 18 | +import de.unijena.bioinf.babelms.MsExperimentParser; |
| 19 | +import de.unijena.bioinf.jjobs.JJob; |
| 20 | +import de.unijena.bioinf.jjobs.JobManager; |
| 21 | +import de.unijena.bioinf.ms.frontend.core.ApplicationCore; |
| 22 | +import de.unijena.bioinf.ms.frontend.workflow.Workflow; |
| 23 | +import de.unijena.bioinf.sirius.Ms2Preprocessor; |
| 24 | +import de.unijena.bioinf.sirius.ProcessedInput; |
| 25 | +import de.unijena.bioinf.sirius.Sirius; |
| 26 | +import de.unijena.bioinf.sirius.annotations.DecompositionList; |
| 27 | +import it.unimi.dsi.fastutil.Pair; |
| 28 | +import lombok.extern.slf4j.Slf4j; |
| 29 | +import org.jetbrains.annotations.NotNull; |
| 30 | + |
| 31 | +import java.io.BufferedReader; |
| 32 | +import java.io.IOException; |
| 33 | +import java.io.InputStreamReader; |
| 34 | +import java.net.URISyntaxException; |
| 35 | +import java.net.URL; |
| 36 | +import java.util.ArrayList; |
| 37 | +import java.util.Arrays; |
| 38 | +import java.util.List; |
| 39 | + |
| 40 | +@Slf4j |
| 41 | +public class SelfTestWorkflow implements Workflow { |
| 42 | + Sirius sirius = ApplicationCore.SIRIUS_PROVIDER.sirius(); |
| 43 | + |
| 44 | + @Override |
| 45 | + public void run() { |
| 46 | + try { |
| 47 | + log.info("Checking ILP solvers using TreeBuilder."); |
| 48 | + for (TreeBuilderFactory.DefaultBuilder builderType : TreeBuilderFactory.DefaultBuilder.values()) { |
| 49 | + log.info("Checking solver: {}", builderType.name()); |
| 50 | + TreeBuilder builder = TreeBuilderFactory.getInstance().getTreeBuilder(builderType); |
| 51 | + log.info("Checking solver '{}{}", builderType.name(), builder != null ? "' SUCCESSFUL." : "' FAILED!"); |
| 52 | + } |
| 53 | + log.info("Checking ILP solvers SUCCESSFUL! At least one ILP solver is available."); |
| 54 | + |
| 55 | + |
| 56 | + log.info("Checking computing FragmentationTree..."); |
| 57 | + testTreeComputation(); |
| 58 | + log.info("FragmentationTree computation SUCCESSFUL!"); |
| 59 | + |
| 60 | + log.info("Loading Test Data..."); |
| 61 | + List<Pair<Ms2Experiment, MolecularFormula>> experiments = List.of( |
| 62 | + Pair.of(getStandardExample("/de.unijena.bioinf.ms.frontend.subtools.selftest/Kaempferol.ms"), MolecularFormula.parseOrThrow("C15H10O6")), |
| 63 | + Pair.of(getStandardExample("/de.unijena.bioinf.ms.frontend.subtools.selftest/Laudanosine.mgf"), MolecularFormula.parseOrThrow("C21H27NO4")), |
| 64 | + Pair.of(getStandardExample("/de.unijena.bioinf.ms.frontend.subtools.selftest/Valium.cef"), MolecularFormula.parseOrThrow("C16H13ClN2O"))); |
| 65 | + log.info("Loading Test Data DONE!"); |
| 66 | + |
| 67 | + for (Pair<Ms2Experiment, MolecularFormula> data : experiments) { |
| 68 | + testFormulaId(data.first(), data.second()); |
| 69 | + } |
| 70 | + } catch (Exception e) { |
| 71 | + log.error("Self-test failed!", e); |
| 72 | + System.exit(99); |
| 73 | + } |
| 74 | + |
| 75 | + |
| 76 | + |
| 77 | + } |
| 78 | + |
| 79 | + public static MutableMs2Experiment getStandardExample(String resourcePath) throws IOException, URISyntaxException { |
| 80 | + MsExperimentParser parser = new MsExperimentParser(); |
| 81 | + URL str = SelfTestOptions.class.getResource(resourcePath); |
| 82 | + BufferedReader buf = new BufferedReader(new InputStreamReader(str.openStream())); |
| 83 | + GenericParser<Ms2Experiment> p = parser.getParser(resourcePath); |
| 84 | + Ms2Experiment exp = p.parse(buf, str.toURI()); |
| 85 | + return (MutableMs2Experiment) exp; |
| 86 | + } |
| 87 | + |
| 88 | + |
| 89 | + public void testTreeComputation() { |
| 90 | + final Ms2Experiment experiment = new MutableMs2Experiment(sirius.getMs2Experiment(368.113616943359d, PrecursorIonType.getPrecursorIonType("[M+H]+"), |
| 91 | + new SimpleSpectrum( |
| 92 | + new double[]{368.113616943359d, 369.116424560547d, 370.118530273438d, 371.121459960938d}, |
| 93 | + new double[]{7.4298448E7d, 1.656387E7d, 2646426.25d, 296325.0625d } |
| 94 | + ), |
| 95 | + Arrays.asList( |
| 96 | + new SimpleSpectrum( |
| 97 | + new double[]{100.3957290649414,109.0745849609375,110.39244079589844,116.84909057617188,119.04886627197266,124.55211639404297,138.5615692138672,139.62106323242188,149.0595703125,159.04342651367188,174.05429077148438,175.0388641357422,175.82968139648438,176.07041931152344,179.0346221923828,188.0712432861328,189.0785675048828,189.7934112548828,190.08619689941406,190.37754821777344,191.0943603515625,193.0496368408203,193.2034149169922,203.97384643554688,211.36386108398438,223.0755157470703,232.05950927734375,235.07550048828125,246.56312561035156,249.05589294433594,251.07077026367188,261.0544738769531,263.0704650878906,265.0871276855469,267.0630798339844,277.0498962402344,279.0650634765625,281.0810546875,289.0498352050781,291.0655822753906,292.0743713378906,293.08099365234375,295.0606384277344,297.0776672363281,297.3970947265625,302.46124267578125,303.0662841796875,305.08026123046875,305.6075134277344,306.0570373535156,306.45819091796875,306.8253479003906,307.0602111816406,307.2962951660156,307.5075988769531,307.66400146484375,308.0629577636719,309.07525634765625,318.4222412109375,318.80999755859375,319.0596923828125,319.3089599609375,319.70037841796875,320.06549072265625,321.0744323730469,322.1073913574219,325.07000732421875,326.0737609863281,332.0915222167969,333.0755310058594,335.0784606933594,337.0704040527344,338.0983581542969,339.0876159667969,350.1022033691406,353.0880432128906,366.08294677734375,367.3216247558594,368.11273193359375,368.216552734375,368.8077087402344,369.1158142089844,369.42626953125,369.91241455078125,}, |
| 98 | + new double[]{4537.85205078125,4636.14208984375,4386.55224609375,4726.12646484375,9624.818359375,5201.5712890625,5279.810546875,4162.4697265625,177876.6875,6609.36865234375,10644.8896484375,13759.1005859375,4984.36572265625,151992.171875,12310.5283203125,25785.259765625,52822.25390625,14517.52734375,2420622.25,7693.88671875,55927.109375,12310.1162109375,5552.3896484375,5172.888671875,7269.5537109375,10368.4189453125,11730.9384765625,21044.888671875,4541.33251953125,11602.5078125,34444.68359375,46705.83984375,49155.7421875,27223.947265625,7353.82177734375,147195.140625,45302.55078125,62019.55859375,284616.4375,102046.0546875,35895.4609375,82949.75,69763.328125,8305.2294921875,6610.50537109375,7126.484375,47796.5703125,13488.21875,5194.052734375,7746.63916015625,36957.0703125,51496.2109375,7290191.5,30372.474609375,9119.17578125,28094.4296875,117144.4453125,70117.2421875,9554.611328125,11817.4228515625,1896174.375,8624.4208984375,8855.138671875,27861.388671875,37455.0078125,8466.224609375,333207.96875,6757.73486328125,70097.484375,37933.45703125,278163.8125,362786.59375,10204.8984375,17157.6484375,350741.3125,10007.03125,5503.0703125,6211.84912109375,1299435.75,6204.93994140625,10005.435546875,1837789.875,8296.6474609375,5625.576171875} |
| 99 | + )) |
| 100 | + )); |
| 101 | + |
| 102 | + final Ms2Preprocessor preprocessor = new Ms2Preprocessor(); |
| 103 | + final ProcessedInput processedInput = preprocessor.preprocess(experiment); |
| 104 | + sirius.getMs1Analyzer().computeAndScoreIsotopePattern(processedInput); |
| 105 | + final FragmentationPatternAnalysis analysis = sirius.getMs2Analyzer(); |
| 106 | + |
| 107 | + FasterTreeComputationInstance instance = new FasterTreeComputationInstance(analysis, processedInput); |
| 108 | + JobManager jobs = SiriusJobs.getGlobalJobManager(); |
| 109 | + jobs.submitJob(instance); |
| 110 | + FasterTreeComputationInstance.FinalResult finalResult = instance.takeResult(); |
| 111 | + final FTree top = finalResult.getResults().get(0); |
| 112 | + assert MolecularFormula.parseOrThrow("C20H17NO6").equals(top.getRoot().getFormula()); |
| 113 | + |
| 114 | + // test ms1 |
| 115 | + final FragmentAnnotation<Score> score = top.getFragmentAnnotationOrThrow(Score.class); |
| 116 | + assert (score.get(top.getRoot()).get("MS-Isotopes") > 0); |
| 117 | + |
| 118 | + // number of decompositions should be equal in MS1 and MSMS |
| 119 | + assert (processedInput.getAnnotationOrThrow(ExtractedIsotopePattern.class).getExplanations().size() == processedInput.getPeakAnnotationOrThrow(DecompositionList.class).get(processedInput.getParentPeak()).getDecompositions().size()); |
| 120 | + } |
| 121 | + |
| 122 | + public void testFormulaId(@NotNull Ms2Experiment experiment, @NotNull MolecularFormula tophit) { |
| 123 | + log.info("Testing formulaID for '{}'...", experiment.getSource().toString()); |
| 124 | + |
| 125 | + // for profiling |
| 126 | + final Ms2Preprocessor preprocessor = new Ms2Preprocessor(); |
| 127 | + final ProcessedInput processedInput = preprocessor.preprocess(experiment); |
| 128 | + sirius.getMs1Analyzer().computeAndScoreIsotopePattern(processedInput); |
| 129 | + final FragmentationPatternAnalysis analysis = sirius.getMs2Analyzer(); |
| 130 | + JobManager jobsManager = SiriusJobs.getGlobalJobManager(); |
| 131 | + TreeBuilder builder = TreeBuilderFactory.getInstance().getTreeBuilder("clp"); |
| 132 | + analysis.setTreeBuilder(builder); |
| 133 | + |
| 134 | + |
| 135 | + List<FasterTreeComputationInstance> jobs = new ArrayList<>(); |
| 136 | + for (int i = 0; i < 20; ++i) { |
| 137 | + FasterTreeComputationInstance instance = new FasterTreeComputationInstance(analysis, processedInput); |
| 138 | + jobs.add(jobsManager.submitJob(instance)); |
| 139 | + } |
| 140 | + |
| 141 | + jobs.forEach(JJob::takeResult); |
| 142 | + |
| 143 | + final FTree top = jobs.get(jobs.size() - 1).getResult().getResults().get(0); |
| 144 | + |
| 145 | + assert top.getRoot().getFormula().equals(tophit); |
| 146 | + log.info("Testing formulaID for '{}' SUCCESSFUL!", experiment.getSource().toString()); |
| 147 | + } |
| 148 | +} |
0 commit comments