Ejemplo n.º 1
0
        public static void WriteSearchMGF(QcParameters qcParameters, RawDataCollection rawData, IRawDataPlus rawFile, bool fixedScans = false)
        {
            var pars = qcParameters.searchParameters;

            int[] scans = AdditionalMath.SelectRandomScans(scans: rawData.scanIndex.ScanEnumerators[MSOrderType.Ms2], num: pars.NumSpectra, fixedScans: fixedScans);
            MGF.WriteMGF(rawData, rawFile, qcParameters.QcSearchDataDirectory, pars.MgfMassCutoff, scans, pars.MgfIntensityCutoff);
        }
Ejemplo n.º 2
0
        public static List <int> GetChargeState(CentroidStreamData centroidData, double parentMass, int assignedCharge)
        {
            double[]   masses;
            double[]   intensities;
            int[]      allowedCharges       = new int[] { 2, 3, 4, 5 };
            List <int> possibleChargeStates = new List <int>();
            int        parentIndex;
            double     massDiff = 1.003356;

            (masses, intensities) = AdditionalMath.SubsetMsData(centroidData.Masses, centroidData.Intensities, parentMass - 0.1, parentMass + 1.2);

            if (masses.Length == 0)
            {
                possibleChargeStates.Add(assignedCharge);
                return(possibleChargeStates);
            }

            int currentIndex;

            if (masses.withinTolerance(parentMass, 10))
            {
                parentIndex = masses.indexOfClosest(parentMass);
            }
            else
            {
                possibleChargeStates.Add(assignedCharge);
                return(possibleChargeStates);
            }


            // needs more work. can try matching multiple isotopes, then if charge state is ambiguous take the one with more matches
            foreach (int charge in allowedCharges)
            {
                currentIndex = parentIndex + 1;
                double isotopeMass = parentMass + massDiff / charge;

                for (int i = currentIndex; i < masses.Length; i++)
                {
                    if (Math.Abs(masses[i] - isotopeMass) / isotopeMass * 1e6 < 6)
                    {
                        double ratio = intensities[i] / intensities[parentIndex];
                        if (ratio > 10 | ratio < 0.1)
                        {
                            // the peak is really big or small compared to the parent peak. it probably is not actually an isotope of it
                            continue;
                        }
                        possibleChargeStates.Add(charge);
                        break;
                    }
                }
            }

            if (possibleChargeStates.Count() == 0)
            {
                possibleChargeStates.Add(assignedCharge);
            }

            return(possibleChargeStates);
        }
Ejemplo n.º 3
0
        public static void WriteSearchMGF(WorkflowParameters parameters, CentroidStreamCollection centroids, SegmentScanCollection segments, RetentionTimeCollection retentionTimes,
                                          PrecursorMassCollection precursorMasses, PrecursorScanCollection precursorScans, TrailerExtraCollection trailerExtras, MethodDataContainer methodData,
                                          ScanIndex index, string rawFileName, bool fixedScans = false)
        {
            var pars = parameters.QcParams.SearchParameters;

            int[] scans = AdditionalMath.SelectRandomScans(scans: index.ScanEnumerators[MSOrderType.Ms2],
                                                           num: parameters.QcParams.NumberSpectra, fixedScans: parameters.QcParams.FixedScans);

            string mgfFile = ReadWrite.GetPathToFile(parameters.QcParams.QcSearchDataDirectory, rawFileName, ".mgf");

            MgfWriter.WriteMGF(rawFileName, centroids, segments, parameters, retentionTimes, precursorMasses, precursorScans,
                               trailerExtras, methodData, index, outputFile: mgfFile, scans: scans);
        }
Ejemplo n.º 4
0
        private double GetIsotopeProbability(int[] number, double[] means, double[] exps)
        {
            var prob = 1.0;

            for (var i = 1; i <= Math.Min(ProbC.Length - 1, number.Length); i++)
            {
                var mean = means[i];
                var exp  = exps[i];
                if (number[i - 1] == 0)
                {
                    prob *= exp;
                }
                else
                {
                    prob *=
                        (Math.Pow(mean, number[i - 1]) * exp / AdditionalMath.Factorial(number[i - 1]));
                }
            }
            return(prob);
        }
Ejemplo n.º 5
0
        public static void RunIdentipy(RawDataCollection rawData, IRawDataPlus rawFile, string QcDataDirectory, SearchParameters idpyPars, bool writeMGF = true)
        {
            int[]  scans;
            string pyExec, idpyScript, mgfFile;

            // get the path to the python executable and identipy start script if they have not been specified
            if (idpyPars.PythonExecutable == null & idpyPars.IdentipyScript == null)
            {
                string pyDir = GetPythonDir();
                pyExec     = GetPythonExec(pyDir);
                idpyScript = GetIdentipyExec(pyDir);
                Log.Information("Found python directory: {PyDir}", pyDir);
                Log.Information("Found python executable: {PyExec}", pyExec);
                Log.Information("Found identipy start script: {IdpyScript}", idpyScript);
            }
            else
            {
                pyExec     = idpyPars.PythonExecutable;
                idpyScript = idpyPars.IdentipyScript;
            }

            if (writeMGF)
            {
                // get a random subset of scans
                scans = AdditionalMath.SelectRandomScans(scans: rawData.scanIndex.ScanEnumerators[MSOrderType.Ms2], num: idpyPars.NumSpectra);

                // write them to a mgf file
                MGF.WriteMGF(rawData, rawFile, QcDataDirectory, scans: scans, cutoff: idpyPars.MgfMassCutoff, intensityCutoff: idpyPars.MgfIntensityCutoff);
            }

            // recreate the path to the mgf
            mgfFile = Path.Combine(QcDataDirectory, Path.GetFileName(rawData.rawFileName) + ".mgf");

            // run identipy, supresseing RunTimeWarnings
            //IdentipyExecute(idpyExec, idpyPars.FastaDatabase, idpyPars.FixedMods, idpyPars.VariableMods, mgfFile);
            Console.WriteLine("Starting Identipy");
            Log.Information("Starting Identipy");
            IdentipyExecute(pyExec, idpyScript, mgfFile, idpyPars, rawData.methodData.MassAnalyzers[MSOrderType.Ms2]);
        }
Ejemplo n.º 6
0
        private static (int Min, int Max) GetMinMaxHeight(SliceSettings settings, int imageHeight, int previousSlice)
        {
            if (settings.TrueSliceMaxHeight == 0)
            {
                var sliceStep       = imageHeight / settings.SliceCount;
                var nextSliceCenter = previousSlice + sliceStep;

                var min = AdditionalMath.Clamp(previousSlice, imageHeight,
                                               nextSliceCenter - settings.TrueSliceMaxDistance / 2);
                var max = AdditionalMath.Clamp(previousSlice, imageHeight,
                                               nextSliceCenter + settings.TrueSliceMaxDistance / 2);

                return(imageHeight - max < settings.TrueSliceMinHeight ? (imageHeight, imageHeight) : (min, max));
            }
            else
            {
                var min = AdditionalMath.Clamp(previousSlice, imageHeight, previousSlice + settings.TrueSliceMinHeight);
                var max = AdditionalMath.Clamp(previousSlice, imageHeight, previousSlice + settings.TrueSliceMaxHeight);

                return(imageHeight - max < settings.TrueSliceMinHeight ? (imageHeight, imageHeight) : (min, max));
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="centroidData"></param>
        /// <param name="parentMZ"></param>
        /// <param name="possibleChargeStates"></param>
        /// <param name="assignedChargeState"></param>
        /// <returns></returns>
        static (int charge, double mass) GetMonoIsotopicMassCharge(CentroidStreamData centroidData, double parentMZ, List <int> possibleChargeStates, int assignedChargeState)
        {
            Dictionary <(int charge, double monoIsoMass), (double distance, double correlation)> scores =
                new Dictionary <(int charge, double monoIsoMass), (double distance, double correlation)>();;
            List <double> possibleMonoIsoMasses;
            Averagine     averagine = new Averagine();

            double[] masses, intensities;
            (masses, intensities) = AdditionalMath.SubsetMsData(centroidData.Masses, centroidData.Intensities, parentMZ - 2.2, parentMZ + 5);

            if (masses.Length == 0)
            {
                return(assignedChargeState, parentMZ);
            }

            foreach (int charge in possibleChargeStates)
            {
                // populate a list of possible monoisotopic masses
                possibleMonoIsoMasses = new List <double>()
                {
                    parentMZ
                };
                for (int i = 1; i < 4; i++)
                {
                    double isotope = parentMZ - i * Masses.C13MinusC12 / charge;
                    if (masses.withinTolerance(isotope, 10))
                    {
                        possibleMonoIsoMasses.Add(isotope);
                    }
                    else
                    {
                        break;
                    }
                }

                if (possibleChargeStates.Count() == 1 & possibleMonoIsoMasses.Count() == 1)
                {
                    // only one possible monoisotopic mass was found, so just return that
                    return(possibleChargeStates.First(), parentMZ);
                }

                // now score
                foreach (double monoisoMZ in possibleMonoIsoMasses)
                {
                    // calculate the actual mass and intensity envelope of the potential monoisotopicMZ
                    double   monoIsotopeMass    = (monoisoMZ - Masses.Proton) * charge;
                    double[] isotopomerEnvelope = Averagine.GetIsotopomerEnvelope(monoIsotopeMass).Envelope;

                    // get out the observed intensities corresponding to the isotope envelope
                    List <double> observedIntensities = new List <double>();
                    int           currentIndex        = masses.ToList().IndexOf(monoisoMZ);

                    for (int i = 0; i < isotopomerEnvelope.Count(); i++)
                    {
                        if ((masses.withinTolerance(monoisoMZ + i * Masses.C13MinusC12 / charge, 10)))
                        {
                            if (currentIndex < intensities.Count())
                            {
                                int index = masses.indexOfClosest(monoisoMZ + i * Masses.C13MinusC12 / charge);
                                observedIntensities.Add(intensities[index]);
                            }
                            else
                            {
                                // we've run out of ions
                                double[] temp = new double[observedIntensities.Count()];
                                for (int j = 0; j < temp.Length; j++)
                                {
                                    temp[j] = isotopomerEnvelope[j];
                                }
                                isotopomerEnvelope = temp;
                            }
                        }
                        else
                        {
                            observedIntensities.Add(0);
                        }
                        currentIndex++;
                    }

                    // normalize the observed intensities
                    double maxInt = observedIntensities.Max();
                    for (int i = 0; i < observedIntensities.Count(); i++)
                    {
                        observedIntensities[i] = observedIntensities[i] / maxInt;
                    }

                    // calculate distance and correlation
                    double distance, correlation;
                    (distance, correlation) = FitScores.GetDistanceAndCorrelation(observedIntensities.ToArray(), isotopomerEnvelope);
                    if (distance < 0.3)
                    {
                        scores.Add((charge, monoisoMZ), (distance, correlation));
                    }
                }
            }

            double smallestDistance = 1;
            double massOut          = 0;
            int    chargeOut        = assignedChargeState;

            if (scores.Keys.Count == 0)
            {
                return(chargeOut, parentMZ);
            }

            foreach (var key in scores?.Keys)
            {
                if (scores[key].distance < smallestDistance)
                {
                    smallestDistance = scores[key].distance;
                    massOut          = key.monoIsoMass;
                    chargeOut        = key.charge;
                }
            }

            return(chargeOut, massOut);
        }