/// <summary> /// Subtract Operation /// </summary> /// <param name="result">ResultResampling Class</param> /// <returns>ResultSpectrumCalculation</returns> private SpectrumCalculationResult OperationSubtract(ResamplingResult result) { if (!ValidateResult(result)) { return(null); } SpectrumCalculationResult spectrumResult = new SpectrumCalculationResult(); ClrDataPoints basePts = result.GetResultData(0); List <ClrDataPoints> subtract = new List <ClrDataPoints>(); for (int i = 1; i < result.GetXYDataNum(); i++) { ClrDataPoints temp = new ClrDataPoints(); for (uint j = 0; j < basePts.getLength(); j++) { if (_stopFlag == true) { return(null); } temp.addPoint(basePts.getX(j), result.GetResultData(i).getY(j) - basePts.getY(j)); } subtract.Add(temp); } for (int i = 0; i < subtract.Count; i++) { if (_stopFlag == true) { return(null); } spectrumResult.AddResult(subtract[i], _specList[i + 1], SPECTRUMNAME_SUBTRACTED); } return(spectrumResult); }
/// <summary> /// Create Remove Contaminant Peak Result. /// </summary> /// <param name="scanNumbers">Scan Numbers</param> /// <param name="tempDir">Temporal Directory</param> /// <param name="ms1List">MS1 List</param> /// <returns>Remove Contaminant Peak Result</returns> private RemoveResult CreateRemoveResult( int scanNumbers, string tempDir, List <SpectrumWrapper> ms1List) { RemoveResult result = new RemoveResult(); for (int i = 0; i < scanNumbers; i++) { if (UpdateProgress != null) { UpdateProgress( PROGRESS_REMOVECONTAMINANT_PHASE.READ_FROM_TEMPORARY, i, scanNumbers); } using (StreamReader sr = new StreamReader(tempDir + @"\Scan" + i.ToString() + ".csv")) { ClrDataPoints pts = new ClrDataPoints(); while (!sr.EndOfStream) { string[] splitLine = sr.ReadLine().Split(','); double x = double.Parse(splitLine[0]); double y = double.Parse(splitLine[1]); pts.addPoint(x, y); } result.AddResult(pts, ms1List[i]); } } return(result); }
/// <summary> /// Divide Operation /// </summary> /// <param name="result">ResultResampling Class</param> /// <returns>ResultSpectrumCalculation</returns> private SpectrumCalculationResult OperationDivide(ResamplingResult result) { if (!ValidateResult(result)) { return(null); } SpectrumCalculationResult spectrumResult = new SpectrumCalculationResult(); ClrDataPoints basePts = new ClrDataPoints(); ClrDataPoints firstPts = result.GetResultData(0); double threshold = firstPts.getMaxY() * Math.Pow(10, -6); for (uint i = 0; i < firstPts.getLength(); i++) { if (_stopFlag == true) { return(null); } double value = Math.Max(firstPts.getY(i), threshold); basePts.addPoint(firstPts.getX(i), value); } List <ClrDataPoints> subtract = new List <ClrDataPoints>(); for (int i = 1; i < result.GetXYDataNum(); i++) { ClrDataPoints temp = new ClrDataPoints(); for (uint j = 0; j < basePts.getLength(); j++) { if (_stopFlag == true) { return(null); } temp.addPoint(basePts.getX(j), result.GetResultData(i).getY(j) / basePts.getY(j)); } subtract.Add(temp); } //2013/11/26 - del //spectrumResult.AddResult(basePts, _specList[0], SPECTRUMNAME_DIVIDED); //enddel for (int i = 0; i < subtract.Count; i++) { if (_stopFlag == true) { return(null); } spectrumResult.AddResult(subtract[i], _specList[i + 1], SPECTRUMNAME_DIVIDED); } return(spectrumResult); }
/// <summary> /// Calculate Sum of Data Points /// </summary> /// <param name="result">ResultResampling Class</param> /// <returns>ClrDataPoints</returns> private ClrDataPoints CalculateDataPointsSum(ResamplingResult result) { ClrDataPoints sum = result.GetResultData(0); ClrDataPoints temp; for (int i = 1; i < result.GetXYDataNum(); i++) { temp = sum; sum = new ClrDataPoints(); ClrDataPoints data = result.GetResultData(i); for (uint j = 0; j < data.getLength(); j++) { if (_stopFlag == true) { return(null); } sum.addPoint(data.getX(j), data.getY(j) + temp.getY(j)); } } return(sum); }
/// <summary> /// Average Operation /// </summary> /// <param name="result">ResultResampling Class</param> /// <returns>ResultSpectrumCalculation</returns> private SpectrumCalculationResult OperationAverage(ResamplingResult result) { if (!ValidateResult(result)) { return(null); } SpectrumCalculationResult spectrumResult = new SpectrumCalculationResult(); ClrDataPoints sum = CalculateDataPointsSum(result); ClrDataPoints average = new ClrDataPoints(); for (uint i = 0; i < sum.getLength(); i++) { if (_stopFlag == true) { return(null); } average.addPoint( sum.getX(i), sum.getY(i) / result.GetXYDataNum()); } spectrumResult.AddResult(average, _specList[0], SPECTRUMNAME_AVERAGED); return(spectrumResult); }
/// <summary> /// add xyData to XYDataWrapper list and RT data list /// x arrengement number and y arrengement number have to be same /// </summary> /// <param name="x">x data arrengement</param> /// <param name="y">y data arrengement</param> /// <returns>ture:success/false:fail</returns> public bool AddData(double[] x, double[] y) { ClrDataPoints xyData = new ClrDataPoints(); // Resampling Results are same number if (x.Length != y.Length) { return(false); } if (_xyDataList.Count > 0) { if (x.Length != _xyDataList[0].getLength()) { return(false); } } for (int n = 0; n < x.Length; n++) { xyData.addPoint(x[n], y[n]); } _xyDataList.Add(xyData); return(true); }
private bool CreateBaselineSubtractedXIC( PluginFunctionItemWrapper pfiw, List <ClrDataPoints> resamplingList, List <SpectrumWrapper> ms1List, SettingParameterValuesWrapper spvw, ref ClrDataPoints[] subtractLowerXICList, ref ClrDataPoints[] subtractHigherXICList, ref ClrDataPoints subtractHighestXICList ) { uint resamplingLength = resamplingList[0].getLength(); int lowerIndex = (resamplingLength < int.MaxValue) ? (int)resamplingLength : int.MaxValue; int higherIndex = (0 < (int)(resamplingLength - lowerIndex))? (int)(resamplingLength - lowerIndex) : 0; subtractLowerXICList = new ClrDataPoints[lowerIndex]; subtractHigherXICList = new ClrDataPoints[higherIndex]; for (uint i = 0; i < resamplingLength; i++) { //Update message. if (UpdateProgress != null) { UpdateProgress( PROGRESS_REMOVECONTAMINANT_PHASE.REMOVE_CONTAMINANT_PEAK, (int)i, (int)resamplingLength); } //Cancel. if (StopFlag) { break; } //Create XIC. ClrDataPoints pts = new ClrDataPoints(); for (int j = 0; j < resamplingList.Count; j++) { //Cancel. if (StopFlag) { break; } pts.addPoint(ms1List[j].getRt(), resamplingList[j].getY(i)); } //Cancel. if (StopFlag) { pts.clearPoints(); pts.Dispose(); break; } //Baseline Subtraction ClrParameters param = new ClrParameters(); ClrDataPoints baseline = new ClrDataPoints(); ClrPluginCallTool.setXYData(param, pts); ClrPluginCallTool.setBaseline(param, baseline); ClrPluginCallTool.setSettingValues(param, spvw); pfiw.getCall().invoke(param); //Cancel. if (StopFlag) { param.Dispose(); baseline.clearPoints(); baseline.Dispose(); } //Resampling baseline. ClrDataPoints subtracted = new ClrDataPoints(); ResamplingParameter resamplingParam = new ResamplingParameter(); ResamplingCalculation resamplingCalc = new ResamplingCalculation(resamplingParam); resamplingParam.ResamplingMode = ResamplingParameter.SamplingMode.FIRST; resamplingParam.Interpolation = _resamplingParam.Interpolation; resamplingParam.AddXYData(pts); resamplingParam.AddXYData(baseline); resamplingCalc.Execute(); ClrDataPoints resampledBaseline = resamplingCalc.Result.getResultData(1); for (uint j = 0; j < resampledBaseline.getLength(); j++) { double intensity = Math.Max(pts.getY(j) - resampledBaseline.getY(j), 0); subtracted.addPoint(pts.getX(j), intensity); } // Countermeasure to OutOfBoundsException. if (i < int.MaxValue) { subtractLowerXICList[i] = subtracted; } else if (int.MaxValue < i && i < (uint)int.MaxValue * 2) { subtractHigherXICList[i - int.MaxValue] = subtracted; } else { subtractHighestXICList = subtracted; } resamplingParam.ClearXYDataList(); resamplingCalc.Result.ClearXYDataList(); } return(!StopFlag); }
/// <summary> /// Execute Remove Contaminant Peak. /// </summary> /// <exception cref="ArgumentException"></exception> /// <exception cref="ArgumentNullException"></exception> public void Execute() { //Update Message if (UpdateProgress != null) { UpdateProgress( PROGRESS_REMOVECONTAMINANT_PHASE.PREPARE, (int)0, (int)1); } //Create Temporary Folder. string tempDir = CreateTempDir(); if (tempDir == null) { return; } //Initialize. StopFlag = false; Result = null; ResultRemove result = new ResultRemove(); //Get root node ClrMsDataVariant msObj = new ClrMsDataVariant(_clrVariant); DataGroupNodeWrapper dgnw = msObj.getSample().getRootDataGroupNode(); //Get all MS1 Spectrums. List <SpectrumWrapper> ms1List = new List <SpectrumWrapper>(); ReadSpectrum(dgnw, ref ms1List); //If count of MS1 Spectrums is 0, quit remove contaminant peak. if (ms1List.Count <= 0) { //Update Message if (UpdateProgress != null) { UpdateProgress( PROGRESS_REMOVECONTAMINANT_PHASE.CANCELLED, (int)1, (int)1); } msObj.Dispose(); return; } else { foreach (SpectrumWrapper sw in ms1List) { _resamplingParam.AddXYData(sw.getXYData()); } } //Get wrappers SettingParameterValuesWrapper spvw = new SettingParameterValuesWrapper(); PluginFunctionItemWrapper pfiw = PluginManagerWrapper.getInstance().getFunctionItem( FUNCTIONTYPE_BASELINE, _baselineParam.Algorithm, null); //Convert parameters to string for SettingParameterValuesWrapper. string paramString = _baselineParam.ABCWindowWidth.ToString() + "," + _baselineParam.NoiseFactor.ToString() + "," + _baselineParam.SmoothingWidth.ToString(); pfiw.getSettingsPage().setParameterString(spvw, paramString); if (StopFlag) { _resamplingParam.ClearXYDataList(); return; } _resampling.UpdateResamplingProgress += UpdateSpecCalcResamplingProgress; //Resampling. _resampling.Execute(); if (StopFlag) { _resamplingParam.ClearXYDataList(); return; } List <ClrDataPoints> resamplingList = _resampling.Result.getAllResultData(); _resampling.UpdateResamplingProgress -= UpdateSpecCalcResamplingProgress; //Dispose. _resamplingParam.ClearXYDataList(); //if resampling result is empty, quit remove contaminant peak. if (resamplingList.Count == 0) { _resampling.Result.Dispose(); return; } //Garbage collect. GC.Collect(); // Countermeasure to OutOfBoundsException. // Max of List and Array Index is "int". // Max Index of XYDataPoints is bigger than maximum integer. // Maximum uint = (Maximum int) * 2 + 1. ClrDataPoints[] subtractLowerXICList = null; ClrDataPoints[] subtractHigherXICList = null; ClrDataPoints subtractHighestXICList = null; bool complete = true; // Create Baseline subtracted XIC. if (CreateBaselineSubtractedXIC( pfiw, resamplingList, ms1List, spvw, ref subtractLowerXICList, ref subtractHigherXICList, ref subtractHighestXICList)) { //Create XIC Complete. uint count = (uint)(subtractHighestXICList == null ? 0 : 1); count += (uint)subtractLowerXICList.Length; count += (uint)subtractHigherXICList.Length; //Replace intensity of resampled spectrum for (int i = 0; i < resamplingList.Count; i++) { if (UpdateProgress != null) { UpdateProgress( PROGRESS_REMOVECONTAMINANT_PHASE.WRITE_TO_TEMPORARY, i, resamplingList.Count); } if (StopFlag) { complete = false; break; } try { using (StreamWriter sw = new StreamWriter(tempDir + @"\Scan" + i.ToString() + ".csv")) { for (uint j = 0; j < subtractLowerXICList.Length; j++) { if (j <= int.MaxValue) { sw.WriteLine(resamplingList[i].getX(j).ToString() + "," + subtractLowerXICList[(int)j].getY((uint)i).ToString()); } else if (j == uint.MaxValue) { sw.WriteLine(resamplingList[i].getX(j).ToString() + "," + subtractHighestXICList.getY((uint)i).ToString()); } else { sw.WriteLine(resamplingList[i].getX(j).ToString() + "," + subtractHigherXICList[(int)(j - int.MaxValue)].getY((uint)i).ToString()); } } } } catch { complete = false; break; } } } else { complete = false; } int scanNumbers = resamplingList.Count; foreach (ClrDataPoints pts in subtractLowerXICList) { if (pts == null) { break; } pts.clearPoints(); pts.Dispose(); } foreach (ClrDataPoints pts in subtractHigherXICList) { pts.clearPoints(); pts.Dispose(); } if (complete == false) { return; } foreach (ClrDataPoints pts in resamplingList) { pts.Dispose(); } if (subtractHighestXICList != null) { subtractHighestXICList.clearPoints(); subtractHighestXICList.Dispose(); } spvw.clear(); spvw.Dispose(); //Garbage collect. GC.Collect(); if (StopFlag || !complete) { return; } for (int i = 0; i < scanNumbers; i++) { if (UpdateProgress != null) { UpdateProgress( PROGRESS_REMOVECONTAMINANT_PHASE.READ_FROM_TEMPORARY, i, scanNumbers); } using (StreamReader sr = new StreamReader(tempDir + @"\Scan" + i.ToString() + ".csv")) { ClrDataPoints pts = new ClrDataPoints(); while (!sr.EndOfStream) { string[] splitLine = sr.ReadLine().Split(','); double x = double.Parse(splitLine[0]); double y = double.Parse(splitLine[1]); pts.addPoint(x, y); } result.AddResult(pts, ms1List[i]); } } if (UpdateProgress != null) { UpdateProgress( PROGRESS_REMOVECONTAMINANT_PHASE.COMPLETE, 1, 1); } Result = result; GC.Collect(); return; }