Esempio n. 1
0
        private void WriteResultsToFile(DatabaseSequenceSpectrumMatch[] matches, string outputFilePath, FastaDatabase database)
        {
            using (var writer = new StreamWriter(outputFilePath))
            {
                writer.WriteLine("Scan\tPre\tSequence\tPost\tModifications\tComposition\tProteinName\tProteinDesc" +
                                 "\tProteinLength\tStart\tEnd\tCharge\tMostAbundantIsotopeMz\tMass\t#MatchedFragments\tProbability\tSpecEValue\tEValue");

                foreach (var scanNum in _ms2ScanNums)
                {
                    var match = matches[scanNum];
                    if (match == null)
                    {
                        continue;
                    }

                    var sequence           = match.Sequence;
                    var offset             = match.Offset;
                    var start              = database.GetOneBasedPositionInProtein(offset) + 1 + match.NumNTermCleavages;
                    var end                = start + sequence.Length - 1;
                    var proteinName        = database.GetProteinName(match.Offset);
                    var protLength         = database.GetProteinLength(proteinName);
                    var ion                = match.Ion;
                    var proteinDescription = database.GetProteinDescription(match.Offset);
                    var probability        = CompositeScorer.GetProbability(match.Score);

                    // Note for DblToString(value, 9, true), by having "9" and "true",
                    // values between 100 and 999 Da will have 7 digits after the decimal place, and
                    // values between 1000 and 9999 will have 6 digits after the decimal place
                    writer.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}\t{8}\t{9}\t{10}\t{11}\t{12}\t{13}\t{14}\t{15}\t{16}\t{17}",
                                     scanNum,
                                     match.Pre,                                                                                              // Pre
                                     sequence,                                                                                               // Sequence
                                     match.Post,                                                                                             // Post
                                     match.ModificationText,                                                                                 // Modifications
                                     ion.Composition,                                                                                        // Composition
                                     proteinName,                                                                                            // ProteinName
                                     proteinDescription,                                                                                     // ProteinDescription
                                     protLength,                                                                                             // ProteinLength
                                     start,                                                                                                  // Start position in protein
                                     end,                                                                                                    // End position in protein
                                     ion.Charge,                                                                                             // precursorCharge
                                     StringUtilities.DblToString(ion.GetMostAbundantIsotopeMz(), 9, true),                                   // MostAbundantIsotopeMz
                                     StringUtilities.DblToString(ion.Composition.Mass, 9, true),                                             // Mass
                                     match.NumMatchedFragments,                                                                              // (Number of matched fragments)
                                     StringUtilities.DblToString(probability, 4),                                                            // Probability
                                     StringUtilities.DblToString(ExcelMinValue(match.SpecEvalue), 6, true, 0.001),                           // EValue; will be displayed using scientific notation if the value is less than 0.001
                                     StringUtilities.DblToString(ExcelMinValue(match.SpecEvalue * database.GetNumEntries()), 6, true, 0.001) // SpecEValue; will be displayed using scientific notation if the value is less than 0.001
                                     );
                }
            }
        }
Esempio n. 2
0
        private void GetMatchStatistics(ProductSpectrum ms2Spec, Sequence sequence, int parentIonCharge, StreamWriter writer)
        {
            if (ms2Spec == null)
            {
                return;
            }
            if (sequence == null)
            {
                return;
            }

            var BaseIonTypesCID  = new[] { BaseIonType.B, BaseIonType.Y };
            var BaseIonTypesETD  = new[] { BaseIonType.C, BaseIonType.Z };
            var tolerance        = new Tolerance(12);
            var MinProductCharge = 1;
            var MaxProductCharge = Math.Min(parentIonCharge + 2, 20);

            var baseIonTypes = ms2Spec.ActivationMethod != ActivationMethod.ETD ? BaseIonTypesCID : BaseIonTypesETD;
            var refIntensity = CompositeScorer.GetRefIntensity(ms2Spec.Peaks);

            var activationMethodFlag = ms2Spec.ActivationMethod == ActivationMethod.ETD ? 2 : 1;
            var cleavages            = sequence.GetInternalCleavages();
            var nComplementaryFrags  = 0;

            var prefixStat = new FragmentStat();
            var suffixStat = new FragmentStat();

            var minMz = ms2Spec.Peaks.First().Mz;
            var maxMz = ms2Spec.Peaks.Last().Mz;

            var cleavageIndex = 0;

            var preFixIonCheck = new bool[sequence.Count + 1];
            var sufFixIonCheck = new bool[sequence.Count + 1];

            foreach (var c in cleavages)
            {
                var prefixHit = false;
                var suffixHit = false;

                foreach (var baseIonType in baseIonTypes)
                {
                    var stat = baseIonType.IsPrefix ? prefixStat : suffixStat;

                    var fragmentComposition = baseIonType.IsPrefix
                                  ? c.PrefixComposition + baseIonType.OffsetComposition
                                  : c.SuffixComposition + baseIonType.OffsetComposition;

                    if (fragmentComposition.Mass < ms2Spec.Peaks[0].Mz)
                    {
                        continue;
                    }

                    var curFragMass = fragmentComposition.Mass;

                    /*var curObsIonCharge = 0;
                     * var curObsIonDist = 1.0d;
                     * var curObsIonCorr = 0d;
                     * var curObsIonIntensity = 0d;
                     * var curObsIonMassError = 0d;*/

                    var mostAbundantIsotopeIndex = fragmentComposition.GetMostAbundantIsotopeZeroBasedIndex();
                    var fragmentIonMostAbuMass   = fragmentComposition.Mass + Constants.C13MinusC12 * mostAbundantIsotopeIndex;

                    var maxCharge = (int)Math.Floor(fragmentIonMostAbuMass / (minMz - Constants.Proton));
                    var minCharge = (int)Math.Ceiling(fragmentIonMostAbuMass / (maxMz - Constants.Proton));
                    if (maxCharge < 1 || maxCharge > MaxProductCharge)
                    {
                        maxCharge = MaxProductCharge;
                    }
                    if (minCharge < 1 || minCharge < MinProductCharge)
                    {
                        minCharge = MinProductCharge;
                    }

                    //var ionMatch = false;
                    for (var charge = minCharge; charge <= maxCharge; charge++)
                    {
                        var ion = new Ion(fragmentComposition, charge);

                        var isotopePeaks = ms2Spec.GetAllIsotopePeaks(ion, tolerance, 0.1);

                        if (isotopePeaks == null)
                        {
                            continue;
                        }

                        var distCorr = CompositeScorer.GetDistCorr(ion, isotopePeaks);
                        if (distCorr.Item2 < 0.7 && distCorr.Item1 > 0.03)
                        {
                            continue;
                        }
                        var mostAbuPeak = isotopePeaks[mostAbundantIsotopeIndex];
                        var intScore    = mostAbuPeak.Intensity / refIntensity;

                        /*
                         * if (ionMatch == false || curObsIonIntensity < intScore)
                         * {
                         *  curObsIonCharge = charge;
                         *  curObsIonCorr = distCorr.Item2;
                         *  curObsIonDist = distCorr.Item1;
                         *  curObsIonIntensity = intScore;
                         *
                         *  var mostAbuPeakMz = Ion.GetIsotopeMz(curFragMass, charge, mostAbundantIsotopeIndex);
                         *  curObsIonMassError = (Math.Abs(mostAbuPeak.Mz - mostAbuPeakMz) / mostAbuPeakMz) * 1e6;
                         *
                         *  //var curObsIonMass = Ion.GetMonoIsotopicMass(mostAbuPeak.Mz, charge, mostAbundantIsotopeIndex);
                         *  //curObsIonMassError = (Math.Abs(curFragMass - curObsIonMass) / curFragMass) * 1e6;
                         * }
                         * ionMatch = true;
                         */
                        var mostAbuPeakMz      = Ion.GetIsotopeMz(curFragMass, charge, mostAbundantIsotopeIndex);
                        var curObsIonMassError = (Math.Abs(mostAbuPeak.Mz - mostAbuPeakMz) / mostAbuPeakMz) * 1e6;

                        stat.Count++;
                        stat.Intensity += Math.Min(intScore, 1.0);
                        stat.Corr      += distCorr.Item2;
                        stat.Dist      += distCorr.Item1;
                        stat.MassError += curObsIonMassError;

                        if (baseIonType.IsPrefix)
                        {
                            prefixHit = true;
                        }
                        else
                        {
                            suffixHit = true;
                        }
                    }

                    //if (!ionMatch) continue;
                }

                if (prefixHit)
                {
                    preFixIonCheck[cleavageIndex] = true;
                }
                if (suffixHit)
                {
                    sufFixIonCheck[cleavageIndex] = true;
                }

                if (prefixHit && suffixHit)
                {
                    nComplementaryFrags++;
                }
                cleavageIndex++;
            }

            var preContCount = 0;
            var sufContCount = 0;

            for (var i = 0; i < preFixIonCheck.Length - 1; i++)
            {
                if (preFixIonCheck[i] && preFixIonCheck[i + 1])
                {
                    preContCount++;
                }
                if (sufFixIonCheck[i] && sufFixIonCheck[i + 1])
                {
                    sufContCount++;
                }
            }

            writer.Write(activationMethodFlag);
            writer.Write("\t");
            writer.Write(sequence.Composition.Mass);
            writer.Write("\t");
            writer.Write(sequence.Count);
            writer.Write("\t");
            writer.Write(nComplementaryFrags);
            writer.Write("\t");

            writer.Write("{0}\t{1}\t{2}\t{3}\t{4}\t{5}", prefixStat.Count, preContCount, prefixStat.Intensity, prefixStat.Corr, prefixStat.Dist, prefixStat.MassError);
            writer.Write("\t");
            writer.Write("{0}\t{1}\t{2}\t{3}\t{4}\t{5}", suffixStat.Count, sufContCount, suffixStat.Intensity, suffixStat.Corr, suffixStat.Dist, suffixStat.MassError);
            writer.Write("\n");
        }