/// <summary> /// Extract isomer ratios from a given spectrum (transformed into capacity vector) /// </summary> /// <param name="ratiosToFit"></param> /// <param name="nbProductsToKeep"></param> /// <param name="capacity"></param> /// <param name="tolerance"></param> /// <param name="PrecursorIntensityInCTrap"></param> /// <param name="PrecursorIntensity"></param> /// <param name="underFlow"></param> /// <param name="percentError"></param> /// <param name="ConSole"></param> /// <returns></returns> public static Dictionary <CharacterizedPrecursor, SolvedResult> SolveFromSpectrum(IEnumerable <CharacterizedPrecursor> ratiosToFit, int nbProductsToKeep, IEnumerable <MsMsPeak> capacity, MassTolerance tolerance, double PrecursorIntensityInCTrap, double PrecursorIntensity, out double underFlow, out double percentError, IConSol ConSole, string fileOut = null) { bool keepGoing = true; Dictionary <double, double> mixedSpectrum = new Dictionary <double, double>(); List <Dictionary <double, double> > unitSpectrum = new List <Dictionary <double, double> >(); foreach (CharacterizedPrecursor isomer in ratiosToFit) { foreach (double key in isomer.NormalizedFragments[nbProductsToKeep].Keys) { if (!mixedSpectrum.ContainsKey(key)) { double cumulIntensity = 0.0; foreach (MsMsPeak peak in capacity) { if (Math.Abs(Utilities.Numerics.CalculateMassError(peak.MZ, key, tolerance.Units)) <= tolerance.Value) { cumulIntensity += peak.Intensity; } } mixedSpectrum.Add(key, cumulIntensity);// / PrecursorIntensityInCTrap); } } if (isomer.NormalizedFragments.ContainsKey(nbProductsToKeep)) { if (isomer.FragmentNormalizor.ContainsKey(nbProductsToKeep)) { Dictionary <double, double> dic = new Dictionary <double, double>(); foreach (double key in isomer.NormalizedFragments[nbProductsToKeep].Keys) { dic.Add(key, isomer.NormalizedFragments[nbProductsToKeep][key] * isomer.FragmentNormalizor[nbProductsToKeep].InterpolateIntensity(PrecursorIntensityInCTrap)); } unitSpectrum.Add(dic); } else { unitSpectrum.Add(isomer.NormalizedFragments[nbProductsToKeep]); } } else { keepGoing = false; } } vsCSVWriter writerFrag = null; if (!string.IsNullOrEmpty(fileOut)) { writerFrag = new vsCSVWriter(fileOut); string line = "Fragments:"; foreach (double key in mixedSpectrum.Keys) { line += "," + key; } writerFrag.AddLine(line); line = "Mixed:"; foreach (double val in mixedSpectrum.Values) { line += "," + val; } writerFrag.AddLine(line); } //This nbProduct seems relevant, try to use isomer to get ratios for this spectrum if (keepGoing) { List <double> solution = new List <double>(); double stepSize = PrecursorIntensityInCTrap / 1000.0; if (stepSize < 1) { stepSize = 1; } double tmpUnderflow = 0; Utilities.Methods.GradientDescent.SolveMaxFlowStyle(unitSpectrum, mixedSpectrum, out solution, out tmpUnderflow, ConSole, stepSize); //Utilities.Methods.GradientDescent.SolveFromGradientDescent(unitSpectrum, mixedSpectrum, PrecursorIntensityInCTrap, out solution, out tmpUnderflow, ConSole); double sumOfIntensities = 0; foreach (double val in mixedSpectrum.Values) { sumOfIntensities += val; } underFlow = tmpUnderflow; List <SolvedResult> result = GetResultList(solution, underFlow, sumOfIntensities); Dictionary <CharacterizedPrecursor, SolvedResult> resultPerSample = new Dictionary <CharacterizedPrecursor, SolvedResult>(); int i = 0; foreach (CharacterizedPrecursor key in ratiosToFit) { resultPerSample.Add(key, result[i]); i++; } if (writerFrag != null) { foreach (CharacterizedPrecursor cPrec in ratiosToFit) { string line = cPrec.Peptide.Sequence; foreach (double key in mixedSpectrum.Keys) { line += "," + cPrec.NormalizedFragments[nbProductsToKeep][key] * resultPerSample[cPrec].NbFitTimes; } writerFrag.AddLine(line); } writerFrag.WriteToFile(); } percentError = (underFlow / sumOfIntensities); return(resultPerSample); } else { percentError = 1.0; underFlow = 0; return(new Dictionary <CharacterizedPrecursor, SolvedResult>()); } }
/// <summary> /// Compares individual items with their respective items from control file. /// </summary> /// <param name="items">Items to compare.</param> /// <param name="sampleToControlMaxFold">Maximum sample to control ratio.</param> /// <param name="controlToSampleMaxFold">Maximum control to sample ratio.</param> /// <param name="massTolerance">Mass tolerance used for peak consolidation.</param> /// <param name="inputFiles">Input files information sorted in the final view order.</param> protected override Dictionary <UnknownFeatureIonInstanceItem, Tuple <InControlStatus, double?> > CompareComponentItems( List <UnknownFeatureIonInstanceItem> items, double sampleToControlMaxFold, double controlToSampleMaxFold, MassTolerance massTolerance, List <InputFileInfo> inputFiles) { // init results dict var results = new Dictionary <UnknownFeatureIonInstanceItem, Tuple <InControlStatus, double?> >(); // make input files map var inputFilesMap = inputFiles.ToDictionary(key => key.FileID); // get all control items var controlItems = items.Where(w => inputFilesMap[w.FileID].IsReference).ToList(); // compare all items foreach (var item in items) { // init status and ratio InControlStatus status; double? ratio = null; // get input file info var inputFile = inputFilesMap[item.FileID]; // control item itself but NOT found if (inputFile.IsReference && item.Area.Equals(0)) { status = InControlStatus.NotInControlSelf; results.Add(item, Tuple.Create(status, ratio)); continue; } // control item itself if (inputFile.IsReference) { status = InControlStatus.InControlSelf; results.Add(item, Tuple.Create(status, ratio)); continue; } // no control file assigned if (inputFile.HasReference == false) { status = InControlStatus.NoControlAssigned; results.Add(item, Tuple.Create(status, ratio)); continue; } // get corresponding control item double maxDelta = massTolerance.GetToleranceInU(item.MolecularWeight); var controlItem = controlItems.FirstOrDefault(w => w.FileID == inputFile.ReferenceFileID && Math.Abs(w.MolecularWeight - item.MolecularWeight) <= maxDelta); // not found in control if (controlItem == null || controlItem.Area.Equals(0)) { status = InControlStatus.NotInControl; results.Add(item, Tuple.Create(status, ratio)); continue; } // calc sample to control area ratio ratio = item.Area / controlItem.Area; // get in-control status bool inControl = (sampleToControlMaxFold.Equals(0) || ratio <= sampleToControlMaxFold) && (controlToSampleMaxFold.Equals(0) || 1.0 / ratio <= controlToSampleMaxFold); status = (inControl) ? InControlStatus.InControl : InControlStatus.Outside; // store result results.Add(item, Tuple.Create(status, ratio)); } return(results); }
public FastOverlapDemultiplexer(MsDataFileImpl demuxProcessedFile) { _dataFile = demuxProcessedFile; _dataFile.EnableCaching(200); _fragmentTolerance = new MassTolerance(10); }
private static double ComputeMaxFlow(List <List <ProductMatch> > spikedMatches, List <MsMsPeak> mixedSpectrum, MassTolerance tolerance, ref List <List <double> > optimalSolutions, ref double percentError, ref List <long> average, IConSol ConSole) { //Create dictionnary of usefull peaks Dictionary <float, double> mixedFragDic = new Dictionary <float, double>(); foreach (List <ProductMatch> fragmentRatio in spikedMatches) { foreach (ProductMatch match in fragmentRatio) { if (!mixedFragDic.ContainsKey((float)match.theoMz)) { float closest = -1; foreach (float key in mixedFragDic.Keys) { if (Math.Abs(Utilities.Numerics.CalculateMassError(match.theoMz, key, tolerance.Units)) <= tolerance.Value) { closest = key; } } if (closest > 0) { //ConSole.WriteLine("Potential problem with selected fragment masses "); match.theoMz = closest; } else { mixedFragDic.Add((float)match.theoMz, 0); } } } } //Fill dictionnary with Intensities List <float> keys = new List <float>(mixedFragDic.Keys); foreach (MsMsPeak peak in mixedSpectrum) { foreach (float mz in keys) { if (Math.Abs(Utilities.Numerics.CalculateMassError(peak.MZ, mz, tolerance.Units)) <= tolerance.Value) { mixedFragDic[mz] += peak.Intensity; } } } List <long> localFlows = new List <long>(); foreach (List <ProductMatch> fragmentRatio in spikedMatches) { localFlows.Add(FindLocalMaximumFlow(fragmentRatio, mixedFragDic)); } Dictionary <float, double> virtualSpectrum = BuildVirtualSpectrum(spikedMatches, localFlows, mixedFragDic); double overError = MaxFlowHelper.ComputeOverflow(virtualSpectrum, mixedFragDic); double underError = MaxFlowHelper.ComputeUnderflow(virtualSpectrum, mixedFragDic); double[] bestIndexes = new double[spikedMatches.Count]; int iterSize = 1; double bestOverallError = double.MaxValue; List <long> bestLocalFlows = new List <long>(); Random rnd = new Random(); while (overError >= 1 && iterSize < 10000)//anything less than 1 is an acceptable solution { for (int index = 0; index < bestIndexes.Length; index++) { bestIndexes[index] = -1; } for (int i = 0; i < spikedMatches.Count; i++) { if (localFlows[i] > 0) { localFlows[i] -= iterSize; virtualSpectrum = BuildVirtualSpectrum(spikedMatches, localFlows, mixedFragDic); double tmpErrorMinus = MaxFlowHelper.ComputeUnderflow(virtualSpectrum, mixedFragDic); double tmpErrorPlus = MaxFlowHelper.ComputeOverflow(virtualSpectrum, mixedFragDic); double tmpFlowRate = Math.Abs(overError - tmpErrorPlus); double underDiff = Math.Abs(underError - tmpErrorMinus); if (underDiff >= 1) { tmpFlowRate /= underDiff; } bestIndexes[i] = tmpFlowRate; localFlows[i] += iterSize; } } //Pick pseudo randomly best index double worstFlowRate = 0.0; for (int index = 0; index < bestIndexes.Length; index++) { if (bestIndexes[index] > worstFlowRate) { worstFlowRate = bestIndexes[index]; } } if (worstFlowRate > 0) { int nbMatching = 0; for (int index = 0; index < bestIndexes.Length; index++) { if (bestIndexes[index] >= worstFlowRate) { nbMatching++; } } int iterChoice = rnd.Next(0, nbMatching - 1); int iterNb = 0; for (int index = 0; index < bestIndexes.Length; index++) { if (bestIndexes[index] >= worstFlowRate) { if (iterChoice == iterNb) { localFlows[index] -= iterSize; } iterNb++; } } iterSize = 1; } else { iterSize++; } virtualSpectrum = BuildVirtualSpectrum(spikedMatches, localFlows, mixedFragDic); overError = MaxFlowHelper.ComputeOverflow(virtualSpectrum, mixedFragDic); underError = MaxFlowHelper.ComputeUnderflow(virtualSpectrum, mixedFragDic); if (overError + underError < bestOverallError) { bestLocalFlows = new List <long>(localFlows); bestOverallError = overError + underError; } }//End of while overflow > 1 optimalSolutions.Clear(); List <double> newList = new List <double>(); foreach (long localFlow in localFlows) { newList.Add(localFlow); } optimalSolutions.Add(newList); newList = new List <double>(); foreach (long localFlow in bestLocalFlows) { newList.Add(localFlow); } optimalSolutions.Add(newList); //Compute average if (bestOverallError < double.MaxValue) { average.Clear(); for (int i = 0; i < optimalSolutions[0].Count; i++) { double sum = 0.0; foreach (List <double> solution in optimalSolutions) { sum += solution[i]; } double avg = sum / (double)optimalSolutions.Count; average.Add((long)avg); } } //Compute expected error in percentage double sumOfIntensities = 0; foreach (double val in mixedFragDic.Values) { sumOfIntensities += val; } percentError = underError / sumOfIntensities; virtualSpectrum = BuildVirtualSpectrum(spikedMatches, localFlows, mixedFragDic); return(MaxFlowHelper.ComputeUnderflow(virtualSpectrum, mixedFragDic)); }
public static Dictionary <CharacterizedPrecursor, SolvedResult> SolveFromSpectrumBKP(IEnumerable <CharacterizedPrecursor> ratiosToFit, int nbProductsToKeep, long precision, IEnumerable <MsMsPeak> capacity, MassTolerance tolerance, int returnType,//0 for max flow, 1 for best flow, 2 for average double PrecursorIntensityInCTrap, ref double overFlow, ref double underFlow, ref double errorInPercent, IConSol ConSole) { List <List <double> > solutions = new List <List <double> >(); List <long> average = new List <long>(); List <MsMsPeak> expandedCapacity = new List <MsMsPeak>(); double sumOfProducts = 0; foreach (MsMsPeak peak in capacity) { double intensityNormed = peak.Intensity / PrecursorIntensityInCTrap; expandedCapacity.Add(new MsMsPeak(peak.MZ, intensityNormed * precision, peak.Charge)); sumOfProducts += peak.Intensity; } List <List <ProductMatch> > tmpRatiosToFit = new List <List <ProductMatch> >(); //foreach (List<ProductMatch> list in ratiosToFit.Values) foreach (CharacterizedPrecursor prec in ratiosToFit) { List <ProductMatch> pms = new List <ProductMatch>(); foreach (ProductMatch pm in prec.Fragments[nbProductsToKeep]) { ProductMatch newPm = new ProductMatch(pm); newPm.obsIntensity = newPm.normalizedIntensity;// *PrecursorIntensityInCTrap; pms.Add(newPm); } tmpRatiosToFit.Add(pms); } double error = ComputeMaxFlow(tmpRatiosToFit, expandedCapacity, tolerance, ref solutions, ref errorInPercent, ref average, ConSole); double sumOfIntensities = 0; foreach (MsMsPeak peak in expandedCapacity) { sumOfIntensities += peak.Intensity; } overFlow = 0; underFlow = error; List <SolvedResult> result = null; switch (returnType) { case 0: result = GetResultList(solutions[0], precision, underFlow, sumOfIntensities); break; case 1: result = GetResultList(solutions[1], precision, underFlow, sumOfIntensities); break; case 2: List <double> tmpAverage = new List <double>(); foreach (double val in average) { tmpAverage.Add(val); } result = GetResultList(tmpAverage, precision, underFlow, sumOfIntensities); break; } Dictionary <CharacterizedPrecursor, SolvedResult> resultPerSample = new Dictionary <CharacterizedPrecursor, SolvedResult>(); int i = 0; foreach (CharacterizedPrecursor key in ratiosToFit) { resultPerSample.Add(key, result[i]); i++; } return(resultPerSample); }
public static Dictionary <PeptideSpectrumMatch, SolvedResult> SolveFromFragmentScore(int nbProductsToKeep, double precision, IEnumerable <MsMsPeak> capacity, MassTolerance tolerance, double PrecursorIntensityInCTrap, Query query, out double underFlow, out double percentError, IConSol ConSole) { List <Dictionary <double, double> > unitSpectrum = new List <Dictionary <double, double> >(); foreach (PeptideSpectrumMatch psm in query.psms) { double[] arrayFrag = psm.ComputeAACoverage(); Dictionary <double, double> individualSpectrum = new Dictionary <double, double>(); for (int i = 0; i < arrayFrag.Length; i++) { individualSpectrum.Add(i, arrayFrag[i]); } unitSpectrum.Add(individualSpectrum); } Dictionary <double, double> mixedSpectrum = new Dictionary <double, double>(); string seq = query.psms[0].Peptide.BaseSequence; for (int i = 0; i < seq.Length; i++) { mixedSpectrum.Add(i, precision); } List <double> solution = new List <double>(); double tmpUnderflow = 0; Utilities.Methods.GradientDescent.SolveMaxFlowStyle(unitSpectrum, mixedSpectrum, out solution, out tmpUnderflow, ConSole, 1); double sumOfIntensities = 0; foreach (double val in mixedSpectrum.Values) { sumOfIntensities += val; } underFlow = tmpUnderflow; List <SolvedResult> result = GetResultList(solution, precision, underFlow, sumOfIntensities); Dictionary <PeptideSpectrumMatch, SolvedResult> resultPerSample = new Dictionary <PeptideSpectrumMatch, SolvedResult>(); int k = 0; foreach (PeptideSpectrumMatch key in query.psms) { resultPerSample.Add(key, result[k]); k++; } percentError = (underFlow / sumOfIntensities); return(resultPerSample); }
public static Dictionary <PeptideSpectrumMatch, SolvedResult> SolveFromFragmentScoreTheoMZ(int nbProductsToKeep, double precision, IEnumerable <MsMsPeak> capacity, MassTolerance tolerance, double PrecursorIntensityInCTrap, Query query, out double underFlow, out double percentError, IConSol ConSole) { List <Dictionary <double, double> > unitSpectrum = new List <Dictionary <double, double> >(); foreach (PeptideSpectrumMatch psm in query.psms) { double[] arrayFrag = psm.ComputeAACoverage(); Dictionary <double, double> individualSpectrum = new Dictionary <double, double>(); foreach (ProductMatch match in psm.AllProductMatches) { if (!individualSpectrum.ContainsKey(match.theoMz)) { individualSpectrum.Add(match.theoMz, 0); } double addedFactor = individualSpectrum[match.theoMz]; if (match.Fragment == null) { addedFactor += arrayFrag[match.fragmentPos]; } else if (match.Fragment.IsReverse) { for (int i = match.fragmentPos - 1; i < arrayFrag.Length; i++) { addedFactor += arrayFrag[i]; } } else { for (int i = match.fragmentPos - 1; i >= 0; i--) { addedFactor += arrayFrag[i]; } } individualSpectrum[match.theoMz] += addedFactor; } double sum = 0.0; foreach (double val in individualSpectrum.Values) { sum += val; } foreach (double key in new List <double>(individualSpectrum.Keys)) { individualSpectrum[key] /= sum; } unitSpectrum.Add(individualSpectrum); } Dictionary <double, double> mixedSpectrum = new Dictionary <double, double>(); foreach (Dictionary <double, double> dic in unitSpectrum) { foreach (double key in dic.Keys) { if (!mixedSpectrum.ContainsKey(key)) { mixedSpectrum.Add(key, precision); } } } double nbKeys = (double)mixedSpectrum.Count; foreach (double key in new List <double>(mixedSpectrum.Keys)) { mixedSpectrum[key] /= nbKeys; } //string seq = query.psms[0].Peptide.BaseSequence; //for (int i = 0; i < seq.Length; i++) // mixedSpectrum.Add(i, precision); List <double> solution = new List <double>(); double tmpUnderflow = 0; Utilities.Methods.GradientDescent.SolveMaxFlowStyle(unitSpectrum, mixedSpectrum, out solution, out tmpUnderflow, ConSole, 1); double sumOfIntensities = 0; foreach (double val in mixedSpectrum.Values) { sumOfIntensities += val; } underFlow = tmpUnderflow; List <SolvedResult> result = GetResultList(solution, precision, underFlow, sumOfIntensities); Dictionary <PeptideSpectrumMatch, SolvedResult> resultPerSample = new Dictionary <PeptideSpectrumMatch, SolvedResult>(); int k = 0; foreach (PeptideSpectrumMatch key in query.psms) { resultPerSample.Add(key, result[k]); k++; } percentError = (underFlow / sumOfIntensities); return(resultPerSample); }
public static Dictionary <CharacterizedPrecursor, SolvedResult> SolveFromSpectrum(IEnumerable <CharacterizedPrecursor> ratiosToFit, int nbProductsToKeep, double precision, IEnumerable <MsMsPeak> capacity, MassTolerance tolerance, double PrecursorIntensityInCTrap, out double underFlow, out double percentError, IConSol ConSole) { bool keepGoing = true; Dictionary <double, double> mixedSpectrum = new Dictionary <double, double>(); List <Dictionary <double, double> > unitSpectrum = new List <Dictionary <double, double> >(); foreach (CharacterizedPrecursor isomer in ratiosToFit) { foreach (double key in isomer.NormalizedFragments[nbProductsToKeep].Keys) { if (!mixedSpectrum.ContainsKey(key)) { double cumulIntensity = 0.0; foreach (MsMsPeak peak in capacity) { if (Math.Abs(Proteomics.Utilities.Numerics.CalculateMassError(peak.MZ, key, tolerance.Units)) <= tolerance.Value) { cumulIntensity += peak.Intensity; } } mixedSpectrum.Add(key, cumulIntensity * precision / PrecursorIntensityInCTrap); } } if (isomer.NormalizedFragments.ContainsKey(nbProductsToKeep)) { unitSpectrum.Add(isomer.NormalizedFragments[nbProductsToKeep]); } else { keepGoing = false; } } if (keepGoing) { List <double> solution = new List <double>(); double tmpUnderflow = 0; Proteomics.Utilities.Methods.GradientDescent.SolveMaxFlowStyle(unitSpectrum, mixedSpectrum, out solution, out tmpUnderflow, ConSole); double sumOfIntensities = 0; foreach (double val in mixedSpectrum.Values) { sumOfIntensities += val; } underFlow = tmpUnderflow; List <SolvedResult> result = GetResultList(solution, precision, underFlow, sumOfIntensities); Dictionary <CharacterizedPrecursor, SolvedResult> resultPerSample = new Dictionary <CharacterizedPrecursor, SolvedResult>(); int i = 0; foreach (CharacterizedPrecursor key in ratiosToFit) { resultPerSample.Add(key, result[i]); i++; } percentError = (underFlow / sumOfIntensities); return(resultPerSample); } else { percentError = 1.0; underFlow = 0; return(new Dictionary <CharacterizedPrecursor, SolvedResult>()); } }