public static void SerializerDeserializerTest() { IList<CrossSectionWorkflowResult> results = new List<CrossSectionWorkflowResult>(); // create fake result 1 IdentifiedIsomerInfo holyGrail1 = new IdentifiedIsomerInfo(10, 250, 6, 10, 22, 4, null, 5, AnalysisStatus.Positive, new PeakScores(1,2,3), new MolecularTarget("C2H34", IonizationMethod.APCI, "Carbon"), 1, 2); PeakScores averageFeatureScores1 = new PeakScores(3, 4, 5); IImsTarget target1 = new MolecularTarget("C2H5OH", IonizationMethod.Deprotonated, "Ginger ale"); IImsTarget target2 = new MolecularTarget("C2H5OH", IonizationMethod.Sodiumated, "Volka"); CrossSectionWorkflowResult result1 = new CrossSectionWorkflowResult( "France", target1, AnalysisStatus.Positive, null,averageFeatureScores1, 4, "", ""); results.Add(result1); // Serialize fake result struct IFormatter formatter = new BinaryFormatter(); using (Stream stream = new FileStream("serialized_result.bin", FileMode.Create, FileAccess.Write, FileShare.None)) { formatter.Serialize(stream, results); } IList<CrossSectionWorkflowResult> newResults; // deserialize fake result struct using (Stream stream = new FileStream("serialized_result.bin", FileMode.Open, FileAccess.Read, FileShare.Read)) { newResults = (IList<CrossSectionWorkflowResult>)formatter.Deserialize(stream); } CrossSectionWorkflowResult result2 = newResults.First(); // Compare it var result = newResults.First(); Assert.AreEqual(result.AnalysisStatus, result1.AnalysisStatus); Assert.AreEqual(result.DatasetName, result2.DatasetName); Assert.AreEqual(result.AverageObservedPeakStatistics.IntensityScore, result2.AverageObservedPeakStatistics.IntensityScore); }
public bool Equals(MolecularTarget other) { return MoleculeUtil.AreCompositionsEqual(this.CompositionWithAdduct, other.CompositionWithAdduct) && this.ChargeState == other.ChargeState && this.correspondingChemical == other.correspondingChemical; }
public void TestTargetDetectionWithIsomersHard() { // BHC string formula3 = "C13H18ClNO"; MolecularTarget sample3 = new MolecularTarget(formula3, IonizationMethod.Protonated, "BHC"); string fileLocation3 = BHC; CrossSectionSearchParameters parameters3 = new CrossSectionSearchParameters(testDriftTubeLength); CrossSectionWorkfow workfow3 = new CrossSectionWorkfow(fileLocation3, "output", parameters3); CrossSectionWorkflowResult results3 = workfow3.RunCrossSectionWorkFlow(sample3, true); Assert.AreEqual(2, results3.IdentifiedIsomers.Count()); workfow3.Dispose(); }
public int TestTargetDetectionWithIsomersClean(string formula, IonizationMethod method, string descriptor, string fileLocation) { MolecularTarget sample1 = new MolecularTarget(formula, method, descriptor); CrossSectionSearchParameters parameters1 = new CrossSectionSearchParameters(CrossSectionSearchParameters.DefaultDriftTimeToleranceInMs, CrossSectionSearchParameters.DefaultMzWindowHalfWidthInPpm, CrossSectionSearchParameters.DefaultNumPointForSmoothing, CrossSectionSearchParameters.DefaultFeatureFilterLevel, CrossSectionSearchParameters.DefaultAbsoluteIntensityThreshold, CrossSectionSearchParameters.DefaultPeakShapeThreshold, CrossSectionSearchParameters.DefaultIsotopicThreshold, CrossSectionSearchParameters.DefaultMaxOutliers, CrossSectionSearchParameters.DefaultPeakDetectorSelection, FitlineEnum.OrdinaryLeastSquares, CrossSectionSearchParameters.DefaultMinR2, CrossSectionSearchParameters.DefaultRelativeIntensityPercentageThreshold, "png", CrossSectionSearchParameters.DefaultInsufficientFramesFraction, testDriftTubeLength, true); CrossSectionWorkfow workfow1 = new CrossSectionWorkfow(fileLocation, "output", parameters1); CrossSectionWorkflowResult results1 = workfow1.RunCrossSectionWorkFlow(sample1, true); int count = results1.IdentifiedIsomers.Count(); workfow1.Dispose(); return count; }
public void TestSingleMoleculeWithFormula() { // Nicotine // string formula = "C10H14N2"; // ImsTarget sample = new ImsTarget(1, Adduct.Protonated, formula); // Console.WriteLine("MZ: " + 221.0594); // string fileLocation = AcetamipridFile; // Acetamiprid // string formula = "C10H11ClN4"; // ImsTarget sample = new ImsTarget(1, Adduct.Deprotonated, formula); // string fileLocation = AcetamipridFile; // F1E // string formula = "C15H21Cl2FN2O3"; // MolecularTarget sample = new MolecularTarget(formula, IonizationMethod.Protonated); // string fileLocation = F1E; // BPS Negative // string formula = "C12H10O4S"; // MolecularTarget sample = new MolecularTarget(formula, IonizationMethod.prot); // string fileLocation = BPSNegative; // BPS Positive string formula = "C12H10O4S"; MolecularTarget sample = new MolecularTarget(formula, IonizationMethod.Protonated, "BPS"); string fileLocation = BPSPostive; CrossSectionSearchParameters parameters = new CrossSectionSearchParameters(testDriftTubeLength); CrossSectionWorkfow workfow = new CrossSectionWorkfow(fileLocation, "output", parameters); CrossSectionWorkflowResult result = workfow.RunCrossSectionWorkFlow(sample, true); Assert.AreEqual(1, result.IdentifiedIsomers.Count()); Assert.LessOrEqual(Math.Abs(result.IdentifiedIsomers.First().CrossSectionalArea - 166.3012), 0.5); workfow.Dispose(); }
public void TestSingleMoleculeMzOnly() { // Good BPS data double mz = 251.037804609; string uimfFile = BPSPostive; // Acetaminophen // double mz = 150.0555008; // string uimfFile = Acetaminophen; // Nicotinefnic // double mz = 161.10787; // string uimfFile = NicoFile; // Nico M+H // double mz = 161.10787; // string uimfFile = NicoFile; MolecularTarget target = new MolecularTarget(mz, IonizationMethod.Protonated, "Nicotine"); CrossSectionSearchParameters parameters = new CrossSectionSearchParameters(testDriftTubeLength); CrossSectionWorkfow workflow = new CrossSectionWorkfow(uimfFile, "output", parameters); workflow.RunCrossSectionWorkFlow(target); workflow.Dispose(); }
public void TestFileNotFound() { // Good BPS data double mz = 273.0192006876; string uimfFile = "blablabla"; // Acetaminophen // double mz = 150.0555008; // string uimfFile = Acetaminophen; // Nicotinefnic // double mz = 161.10787; // string uimfFile = NicoFile; // AcetamipridFile // double mz = 221.059395; // string uimfFile = AcetamipridFile; MolecularTarget target = new MolecularTarget(mz, IonizationMethod.Deprotonated, "BPS"); CrossSectionSearchParameters parameters = new CrossSectionSearchParameters(testDriftTubeLength); Assert.Throws<FileNotFoundException>(() => new CrossSectionWorkfow(uimfFile, "output", parameters)); }
/// <summary> /// The execute finder. /// </summary> /// <param name="options"> /// The options. /// </param> /// <returns> /// The <see cref="int"/>. /// </returns> private static int ExecuteFinder(FinderOptions options) { try { string uimfFile = options.InputPath; // get the UIMF file string datasetName = Path.GetFileNameWithoutExtension(uimfFile); string resultName = datasetName + "_" + "Result.bin"; string resultPath = Path.Combine(options.OutputPath, resultName); string outputDirectory = options.OutputPath; IList<string> targetList = options.TargetList; bool verbose = options.DetailedVerbose; if (outputDirectory == string.Empty) { outputDirectory = Directory.GetCurrentDirectory(); } if (!outputDirectory.EndsWith("\\")) { outputDirectory += "\\"; } if (!Directory.Exists(outputDirectory)) { try { Directory.CreateDirectory(outputDirectory); } catch (Exception) { Console.WriteLine("Failed to create directory."); throw; } } // Delete the result file if it already exists if (File.Exists(resultPath)) { File.Delete(resultPath); } // Load parameters double Mz; CrossSectionSearchParameters searchParameters = new CrossSectionSearchParameters( options.DriftTimeToleranceInMs, options.PrePpm, options.NumberOfPointsForSmoothing, options.FeatureFilterLevel, options.IntensityThreshold, options.PeakShapeScoreThreshold, options.IsotopicScoreThreshold, options.MaxOutliers, CrossSectionSearchParameters.DefaultPeakDetectorSelection, // No longer an option options.RobustRegression ? FitlineEnum.IterativelyBiSquareReweightedLeastSquares : FitlineEnum.OrdinaryLeastSquares, options.MinR2, options.RelativeIntensityPercentageThreshold, options.GraphicsFormat, options.InsufficientFramesFraction, options.DriftTubeLength, options.UseAverageTemperature); IFormatter formatter = new BinaryFormatter(); // If target cannot be constructed. Create a result. IList<IImsTarget> targets = new List<IImsTarget>(); IImsTarget currentTarget = null; IList<CrossSectionWorkflowResult> errorTargets = new List<CrossSectionWorkflowResult>(); foreach (string item in targetList) { foreach (string ionization in options.IonizationList) { try { // get the ionization method. IonizationMethod method = IonizationMethodUtilities.ParseIonizationMethod(ionization.Trim()); Tuple<string, string> target = ParseTargetToken(item, datasetName); string formula = target.Item2; string sampleClass = target.Item1; bool isDouble = Double.TryParse(formula, out Mz); if (!isDouble) { Regex rgx = new Regex("[^a-zA-Z0-9 -]"); formula = rgx.Replace(formula, ""); } if (!isDouble) { currentTarget = new MolecularTarget(formula, method, sampleClass); targets.Add(currentTarget); } else { currentTarget = new MolecularTarget(Mz, method, sampleClass); targets.Add(currentTarget); } } catch (Exception e) { // In case of error creating targets, create the target error result CrossSectionWorkflowResult informedResult = CrossSectionWorkflowResult.CreateErrorResult(currentTarget, datasetName, uimfFile, resultPath, ""); using (Stream stream = new FileStream("serialized_result.bin", FileMode.Create, FileAccess.Write, FileShare.None)) { formatter.Serialize(stream, informedResult); } throw e; } } } // Preprocessing Console.WriteLine("Start Preprocessing:"); BincCentricIndexing.IndexUimfFile(uimfFile); // Run algorithms in IMSInformed CrossSectionWorkfow workflow = new CrossSectionWorkfow(uimfFile, outputDirectory, searchParameters); IList<CrossSectionWorkflowResult> results = workflow.RunCrossSectionWorkFlow(targets, verbose); workflow.Dispose(); // Merge the target error result dictionary and other results foreach (var pair in errorTargets) { results.Add(pair); } // Serialize the result string binPath = Path.Combine(outputDirectory, resultName); using (Stream stream = new FileStream(binPath, FileMode.Create, FileAccess.Write, FileShare.None)) { formatter.Serialize(stream, results); } if (options.PauseWhenDone) { PauseProgram(); } // Define success foreach (CrossSectionWorkflowResult result in results) { if (!(result.AnalysisStatus == AnalysisStatus.Positive || result.AnalysisStatus == AnalysisStatus.Negative || result.AnalysisStatus == AnalysisStatus.NotSufficientPoints | result.AnalysisStatus == AnalysisStatus.Rejected)) { return 1; } } return 0; } catch (Exception e) { string uimfFile = options.InputPath; // get the UIMF file string datasetName = Path.GetFileNameWithoutExtension(uimfFile); string resultName = datasetName + "_Result.txt"; string resultPath = Path.Combine(options.OutputPath, resultName); string outputDirectory = options.OutputPath; if (outputDirectory == string.Empty) { outputDirectory = Directory.GetCurrentDirectory(); } if (!outputDirectory.EndsWith("\\")) { outputDirectory += "\\"; } if (!Directory.Exists(outputDirectory)) { try { Directory.CreateDirectory(outputDirectory); } catch (Exception) { Console.WriteLine("Failed to create directory."); throw; } } // Delete the result file if it already exists if (File.Exists(resultPath)) { File.Delete(resultPath); } Console.WriteLine(e.Message); using (StreamWriter errorFile = File.CreateText(resultPath)) { errorFile.Write(e.Message); errorFile.Write(e.StackTrace); } if (options.PauseWhenDone) { PauseProgram(); } return 1; } }
public void TestScoring() { string formula = "C9H13ClN6"; string fileLocation = Cae; MolecularTarget target = new MolecularTarget(formula, IonizationMethod.Protonated, "CAE"); Console.WriteLine("CompositionWithoutAdduct: " + target.CompositionWithoutAdduct); Console.WriteLine("Monoisotopic ViperCompatibleMass: " + target.MonoisotopicMass); CrossSectionSearchParameters parameters = new CrossSectionSearchParameters(driftTubeLength); var smoother = new SavitzkyGolaySmoother(parameters.NumPointForSmoothing, 2); CrossSectionWorkfow workflow = new CrossSectionWorkfow(fileLocation, "output", parameters); Console.WriteLine("Ionization method: " + target.Adduct); Console.WriteLine("Targeting centerMz: " + target.MassWithAdduct); // Generate Theoretical Isotopic Profile List<Peak> theoreticalIsotopicProfilePeakList = null; if (target.CompositionWithAdduct != null) { string empiricalFormula = target.CompositionWithAdduct.ToPlainString(); var theoreticalFeatureGenerator = new JoshTheorFeatureGenerator(); IsotopicProfile theoreticalIsotopicProfile = theoreticalFeatureGenerator.GenerateTheorProfile(empiricalFormula, 1); theoreticalIsotopicProfilePeakList = theoreticalIsotopicProfile.Peaklist.Cast<Peak>().ToList(); } // Generate VoltageSeparatedAccumulatedXICs var uimfReader = new DataReader(fileLocation); Console.WriteLine("Input file: {0}", fileLocation); VoltageSeparatedAccumulatedXiCs accumulatedXiCs = new VoltageSeparatedAccumulatedXiCs(uimfReader, target.MassWithAdduct, parameters.MzWindowHalfWidthInPpm, driftTubeLength); Console.WriteLine(); // For each voltage, find 2D XIC features foreach (VoltageGroup voltageGroup in accumulatedXiCs.Keys) { Console.WriteLine("Voltage group: {0} V, Frame {1}-{2}, {3:F2}K, {4:F2}Torr", voltageGroup.MeanVoltageInVolts, voltageGroup.FirstFrameNumber, voltageGroup.LastFrameNumber, voltageGroup.MeanTemperatureInKelvin, voltageGroup.MeanPressureInTorr); List<IntensityPoint> intensityPoints = accumulatedXiCs[voltageGroup].IntensityPoints; List<FeatureBlob> featureBlobs = PeakFinding.FindPeakUsingWatershed(intensityPoints, smoother, parameters.FeatureFilterLevel); List<StandardImsPeak> standardPeaks = featureBlobs.Select(featureBlob => new StandardImsPeak(featureBlob, uimfReader, voltageGroup, target.MassWithAdduct, parameters.MzWindowHalfWidthInPpm)).ToList(); // feature scorings and Target selection. double globalMaxIntensity = IMSUtil.MaxIntensityAfterFrameAccumulation(voltageGroup, uimfReader); // Check each XIC Peak found foreach (var featurePeak in standardPeaks) { // Evaluate feature scores. double intensityScore = FeatureScoreUtilities.IntensityScore(featurePeak, globalMaxIntensity); double isotopicScoreAngle = FeatureScoreUtilities.IsotopicProfileScore( featurePeak, workflow.uimfReader, target, theoreticalIsotopicProfilePeakList, voltageGroup, IsotopicScoreMethod.Angle, globalMaxIntensity, workflow.NumberOfScans); double isotopicScoreDistance = FeatureScoreUtilities.IsotopicProfileScore( featurePeak, workflow.uimfReader, target, theoreticalIsotopicProfilePeakList, voltageGroup, IsotopicScoreMethod.EuclideanDistance, globalMaxIntensity, workflow.NumberOfScans); double isotopicScorePerson = FeatureScoreUtilities.IsotopicProfileScore( featurePeak, workflow.uimfReader, target, theoreticalIsotopicProfilePeakList, voltageGroup, IsotopicScoreMethod.PearsonCorrelation, globalMaxIntensity, workflow.NumberOfScans); double isotopicScoreBhattacharyya = FeatureScoreUtilities.IsotopicProfileScore( featurePeak, workflow.uimfReader, target, theoreticalIsotopicProfilePeakList, voltageGroup, IsotopicScoreMethod.Bhattacharyya, globalMaxIntensity, workflow.NumberOfScans); double isotopicScoreDistanceAlternative = FeatureScoreUtilities.IsotopicProfileScore( featurePeak, workflow.uimfReader, target, theoreticalIsotopicProfilePeakList, voltageGroup, IsotopicScoreMethod.EuclideanDistanceAlternative, globalMaxIntensity, workflow.NumberOfScans); double peakShapeScore = FeatureScoreUtilities.PeakShapeScore(featurePeak, workflow.uimfReader, workflow.Parameters.MzWindowHalfWidthInPpm, workflow.Parameters.DriftTimeToleranceInMs, voltageGroup, globalMaxIntensity, workflow.NumberOfScans); // Report all features. Console.WriteLine(" feature found at scan number {0}", featurePeak.PeakApex.DriftTimeCenterInScanNumber); Console.WriteLine(" IntensityScore: {0}", intensityScore); Console.WriteLine(" peakShapeScore: {0}", peakShapeScore); Console.WriteLine(" isotopicScore - Angle: {0}", isotopicScoreAngle); Console.WriteLine(" isotopicScore - Distance: {0}", isotopicScoreDistance); Console.WriteLine(" isotopicScore - Distance2:{0}", isotopicScoreDistanceAlternative); Console.WriteLine(" isotopicScore - Pearson: {0}", isotopicScorePerson); Console.WriteLine(" isotopicScore - Bhattacharyya: {0}", isotopicScoreBhattacharyya); Console.WriteLine(); } Console.WriteLine(); } workflow.Dispose(); }
public void TestFormulaPerturbance() { List<Tuple<string, string>> formulas = new List<Tuple<string, string>>(); // truth formulas.Add(new Tuple<string, string>("True formula", "C12H10O4S")); formulas.Add(new Tuple<string, string>("1 extra H", "C12H11O4S")); formulas.Add(new Tuple<string, string>("2 extra H", "C12H12O4S")); formulas.Add(new Tuple<string, string>("3 extra H", "C12H13O4S")); formulas.Add(new Tuple<string, string>("3 extra H", "C12H14O4S")); formulas.Add(new Tuple<string, string>("4 extra H", "C12H15O4S")); formulas.Add(new Tuple<string, string>("5 extra H", "C12H16O4S")); formulas.Add(new Tuple<string, string>("1 less H", "C12H9O4S")); formulas.Add(new Tuple<string, string>("2 less H", "C12H8O4S")); formulas.Add(new Tuple<string, string>("3 less H", "C12H7O4S")); formulas.Add(new Tuple<string, string>("4 less H", "C12H6O4S")); Console.WriteLine("[Intensity], [Distance1], [Distance2], [Angle], [Pearson], [Bucha]"); string fileLocation = BPSNegative; CrossSectionSearchParameters parameters = new CrossSectionSearchParameters(driftTubeLength); CrossSectionWorkfow workflow = new CrossSectionWorkfow(fileLocation, "output", parameters); foreach (var form in formulas) { bool found = false; MolecularTarget target = new MolecularTarget(form.Item2, new IonizationAdduct(IonizationMethod.Deprotonated), form.Item1); Console.Write(form.Item1 + ": "); var smoother = new SavitzkyGolaySmoother(parameters.NumPointForSmoothing, 2); // Generate Theoretical Isotopic Profile List<Peak> theoreticalIsotopicProfilePeakList = null; if (target.CompositionWithAdduct != null) { string empiricalFormula = target.CompositionWithAdduct.ToPlainString(); var theoreticalFeatureGenerator = new JoshTheorFeatureGenerator(); IsotopicProfile theoreticalIsotopicProfile = theoreticalFeatureGenerator.GenerateTheorProfile(empiricalFormula, 1); theoreticalIsotopicProfilePeakList = theoreticalIsotopicProfile.Peaklist.Cast<Peak>().ToList(); } // Generate VoltageSeparatedAccumulatedXICs var uimfReader = new DataReader(fileLocation); VoltageSeparatedAccumulatedXiCs accumulatedXiCs = new VoltageSeparatedAccumulatedXiCs(uimfReader, target.MassWithAdduct, parameters.MzWindowHalfWidthInPpm, parameters.DriftTubeLengthInCm); var voltageGroup = accumulatedXiCs.Keys.First(); // Find peaks using multidimensional peak finder. List<IntensityPoint> intensityPoints = accumulatedXiCs[voltageGroup].IntensityPoints; List<FeatureBlob> featureBlobs = PeakFinding.FindPeakUsingWatershed(intensityPoints, smoother, parameters.FeatureFilterLevel); List<StandardImsPeak> standardPeaks = featureBlobs.Select(featureBlob => new StandardImsPeak(featureBlob, uimfReader, voltageGroup, target.MassWithAdduct, parameters.MzWindowHalfWidthInPpm)).ToList(); // feature scorings and Target selection. double globalMaxIntensity = IMSUtil.MaxIntensityAfterFrameAccumulation(voltageGroup, uimfReader); // Check each XIC Peak found foreach (var peak in standardPeaks) { // Evaluate feature scores. double intensityScore = FeatureScoreUtilities.IntensityScore(peak, globalMaxIntensity); double isotopicScoreAngle = FeatureScoreUtilities.IsotopicProfileScore( peak, workflow.uimfReader, target, theoreticalIsotopicProfilePeakList, voltageGroup, IsotopicScoreMethod.Angle, globalMaxIntensity, workflow.NumberOfScans); double isotopicScoreDistance = FeatureScoreUtilities.IsotopicProfileScore( peak, workflow.uimfReader, target, theoreticalIsotopicProfilePeakList, voltageGroup, IsotopicScoreMethod.EuclideanDistance, globalMaxIntensity, workflow.NumberOfScans); double isotopicScorePerson = FeatureScoreUtilities.IsotopicProfileScore( peak, workflow.uimfReader, target, theoreticalIsotopicProfilePeakList, voltageGroup, IsotopicScoreMethod.PearsonCorrelation, globalMaxIntensity, workflow.NumberOfScans); double isotopicScoreBhattacharyya = FeatureScoreUtilities.IsotopicProfileScore( peak, workflow.uimfReader, target, theoreticalIsotopicProfilePeakList, voltageGroup, IsotopicScoreMethod.Bhattacharyya, globalMaxIntensity, workflow.NumberOfScans); double isotopicScoreDistanceAlternative = FeatureScoreUtilities.IsotopicProfileScore( peak, workflow.uimfReader, target, theoreticalIsotopicProfilePeakList, voltageGroup, IsotopicScoreMethod.EuclideanDistanceAlternative, globalMaxIntensity, workflow.NumberOfScans); double peakShapeScore = FeatureScoreUtilities.PeakShapeScore(peak, workflow.uimfReader, workflow.Parameters.MzWindowHalfWidthInPpm, workflow.Parameters.DriftTimeToleranceInMs, voltageGroup, globalMaxIntensity, workflow.NumberOfScans); // Report all features. if (peak.PeakApex.DriftTimeCenterInScanNumber == 115) { Console.Write("{0:F4} ", intensityScore); found = true; } // Report all features. if (peak.PeakApex.DriftTimeCenterInScanNumber == 115) { Console.Write("{0:F4} ", isotopicScoreDistance); found = true; } // Report all features. if (peak.PeakApex.DriftTimeCenterInScanNumber == 115) { Console.Write("{0:F4} ", isotopicScoreDistanceAlternative); found = true; } // Report all features. if (peak.PeakApex.DriftTimeCenterInScanNumber == 115) { Console.Write("{0:F4} ", isotopicScoreAngle); found = true; } // Report all features. if (peak.PeakApex.DriftTimeCenterInScanNumber == 115) { Console.Write("{0:F4} ", isotopicScorePerson); found = true; } // Report all features. if (peak.PeakApex.DriftTimeCenterInScanNumber == 115) { Console.Write("{0:F4} ", isotopicScoreBhattacharyya); found = true; } } if (!found) { Console.Write("No features"); } Console.WriteLine(); } // Manually dispose so it doesn't interfere with other tests. workflow.Dispose(); }