Ejemplo n.º 1
0
        public string ToString(MassType massType)
        {
            string str = Formula != null?
                         string.Format(@"{0:F04} - {1}", GetMass(massType), Formula) :
                             string.Format(@"{0:F04}", GetMass(massType));

            if (Charge != 0)
            {
                str += Transition.GetChargeIndicator(Adduct.FromCharge(Charge, Adduct.ADDUCT_TYPE.charge_only));
            }

            return(str);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Calculates the matching charge within a tolerance for a mass, assuming (de)protonation.
        /// </summary>
        /// <param name="mass">The mass to calculate charge for (actually massH if !IsCustomIon)</param>
        /// <param name="mz">The desired m/z value the charge should produce</param>
        /// <param name="tolerance">How far off the actual m/z is allowed to be</param>
        /// <param name="isCustomIon">Is this a custom ion formula?</param>
        /// <param name="minCharge">Minimum charge to consider</param>
        /// <param name="maxCharge">Maximum charge to consider</param>
        /// <param name="massShifts">Possible mass shifts that may have been applied to decoys</param>
        /// <param name="massShiftType"></param>
        /// <param name="massShift">Mass shift required to to achieve this charge state or zero</param>
        /// <param name="nearestCharge">closest matching charge, useful when return value is null</param>
        /// <returns>A matching charge or null, in which case the closest non-matching charge can be found in the nearestCharge value.</returns>
        public static Adduct CalcCharge(TypedMass mass, double mz, double tolerance, bool isCustomIon, int minCharge, int maxCharge,
                                        ICollection <int> massShifts, MassShiftType massShiftType, out int massShift, out int nearestCharge)
        {
            Assume.IsTrue(minCharge <= maxCharge);

            massShift = 0;

            nearestCharge = 0;
            double nearestDelta = double.MaxValue;

            for (int i = minCharge; i <= maxCharge; i++)
            {
                if (i != 0) // Avoid z=0 if we're entertaining negative charge states
                {
                    double calculatedMz = isCustomIon
                        ? Adduct.FromChargeProtonated(i).MzFromNeutralMass(mass)
                        : SequenceMassCalc.GetMZ(mass, i);
                    double delta           = mz - calculatedMz;
                    double deltaAbs        = Math.Abs(delta);
                    int    potentialShift  = (int)Math.Round(deltaAbs);
                    double fractionalDelta = deltaAbs - potentialShift;
                    if (MatchMz(fractionalDelta, tolerance) && MatchMassShift(potentialShift, massShifts, massShiftType))
                    {
                        massShift = potentialShift;
                        if (delta < 0)
                        {
                            massShift = -massShift;
                        }
                        var result = i;
                        nearestCharge = i;
                        return(Adduct.FromCharge(result, isCustomIon ? Adduct.ADDUCT_TYPE.non_proteomic : Adduct.ADDUCT_TYPE.proteomic));
                    }
                    if (deltaAbs < nearestDelta)
                    {
                        nearestDelta  = deltaAbs;
                        nearestCharge = i;
                    }
                    // If the charge is positive and the calculated m/z is smaller than the desired m/z
                    // increasing the charge further cannot possibly produce a match
                    if (massShiftType == MassShiftType.none && minCharge > 0 && delta > 0)
                    {
                        break;
                    }
                }
            }

            Debug.Assert(nearestCharge != 0);   // Could only happen if min > max

            return(Adduct.EMPTY);
        }
Ejemplo n.º 3
0
        public void ConvertSmallMolMzOnlyFrom37Test()
        {
            var docSmall = ResultsUtil.DeserializeDocument("SmallMoleculesMzOnly_3-7.sky", GetType());

            AssertEx.IsDocumentState(docSmall, 0, 4, 8, 13, 13);
            int iGroup  = 0;
            var iTran   = 0;
            var heavies = new HashSet <int> {
                2, 3, 5, 11, 13
            };                                                  // Indexes of nodes expected to be !isLight
            var mzPrecursorDeclared = new[]
            {
                "146.2",
                "155.2",
                "500.",
                "300.",
                "320.",
                "177.044724",
                "88.522088",
                "819.42",
                "1639.7",
                "351.217698",
                "355.242805",
                "335.001097",
                "339.247891"
            };

            foreach (var nodeGroup in docSmall.MoleculeTransitionGroups)
            {
                Adduct expectedPrecursorAdduct;
                switch (++iGroup)
                {
                case 6:
                    expectedPrecursorAdduct = Adduct.M_PLUS;
                    break;

                case 7:
                    expectedPrecursorAdduct = Adduct.M_PLUS_2;
                    break;

                case 8:
                    expectedPrecursorAdduct = Adduct.M_MINUS_2.ChangeIsotopeLabels(-.86055);
                    break;

                case 9:
                    expectedPrecursorAdduct = Adduct.M_MINUS;
                    break;

                case 10:
                    expectedPrecursorAdduct = Adduct.M_MINUS_H;
                    break;

                case 11:
                    expectedPrecursorAdduct = Adduct.FromString("[M4H2-H]", Adduct.ADDUCT_TYPE.non_proteomic, null);
                    break;

                case 12:
                    expectedPrecursorAdduct = Adduct.M_MINUS_H.ChangeIsotopeLabels(-.221687, 6);
                    break;

                case 13:
                    expectedPrecursorAdduct = Adduct.FromString("[M4H2-H]", Adduct.ADDUCT_TYPE.non_proteomic, null);
                    break;

                default:
                    // Check translation to adducts worked as expected
                    expectedPrecursorAdduct =
                        Adduct.FromCharge(nodeGroup.PrecursorCharge, Adduct.ADDUCT_TYPE.non_proteomic);
                    if (iGroup == 5)
                    {
                        expectedPrecursorAdduct = expectedPrecursorAdduct.ChangeIsotopeLabels(60.0);
                    }
                    break;
                }
                var prec = mzPrecursorDeclared[iGroup - 1].Split('.')[1].Length;
                Assert.AreEqual(double.Parse(mzPrecursorDeclared[iGroup - 1], CultureInfo.InvariantCulture), Math.Round(nodeGroup.PrecursorMz, prec), "mz iGroup=" + iGroup);
                Assert.AreEqual(expectedPrecursorAdduct, nodeGroup.PrecursorAdduct, "iGroup=" + iGroup);
                Assert.AreEqual(heavies.Contains(iGroup), !nodeGroup.IsLight, "iGroup=" + iGroup);
                // Most product m/z values should be single-digit precision (entered by a person)
                foreach (var nodeTran in nodeGroup.Transitions)
                {
                    if (++iTran < 7)
                    {
                        Assert.AreEqual(Math.Round(nodeTran.Mz, 1), nodeTran.Mz.Value);
                    }
                    var expectedTransitionAdduct = nodeTran.IsMs1 ? expectedPrecursorAdduct : Adduct.FromChargeNoMass(nodeTran.Transition.Charge);
                    Assert.AreEqual(expectedTransitionAdduct, nodeTran.Transition.Adduct, "iTran=" + iTran);
                }
            }

            AssertEx.Serializable(docSmall);
        }