Beispiel #1
0
        public static int FindAdductDescription(string line, out Adduct adduct)
        {
            // Check for adduct description
            var chargePos = -1;

            adduct = Adduct.EMPTY;
            var adductStart = line.LastIndexOf('[');

            if (adductStart >= 0)
            {
                if (adductStart + 2 > line.Length || line[adductStart + 1] == '+')
                {
                    // It was probably a modification like "[+57]", and we're being called by StripChargeIndicators on a peptide
                    return(chargePos);
                }
                var adductText = line.Substring(adductStart);
                if (adductStart > 0 && line[adductStart - 1] == '(')
                {
                    adductText = adductText.TrimEnd(')', ' ');
                    adductStart--; // Consider adduct description as beginning at start of enclosing parens
                }
                if (!Adduct.TryParse(adductText, out adduct))
                {
                    // Whatever it was, it's not an adduct
                    return(chargePos);
                }
                chargePos = adductStart;
            }
            return(chargePos);
        }
        public static string ValidateAdduct(string adductText)
        {
            Adduct adduct;

            if (!Adduct.TryParse(adductText, out adduct))
            {
                return(Resources.ValidatingIonMobilityPeptide_ValidateAdduct_A_valid_adduct_description__e_g____M_H____must_be_provided_);
            }
            return(null);
        }
Beispiel #3
0
            public bool TryGetCharge(long linesRead, out Adduct charge)
            {
                string chargeString = GetField(Field.charge);

                if (chargeString == null)
                {
                    charge = Adduct.EMPTY;
                    return(false);
                }

                if (!Adduct.TryParse(chargeString, out charge, Adduct.ADDUCT_TYPE.proteomic)) // Read, for example, "2" as Adduct.DOUBLY_PROTONATED
                {
                    throw new IOException(string.Format(Resources.PeakBoundaryImporter_Import_The_value___0___on_line__1__is_not_a_valid_charge_state_, chargeString, linesRead));
                }
                return(true);
            }
Beispiel #4
0
        public static bool IsFormulaWithAdduct(string formula, out Molecule molecule, out Adduct adduct, out string neutralFormula)
        {
            molecule       = null;
            adduct         = Adduct.EMPTY;
            neutralFormula = null;
            if (string.IsNullOrEmpty(formula))
            {
                return(false);
            }
            // Does formula contain an adduct description?  If so, pull charge from that.
            var parts = formula.Split('[');

            if (parts.Length == 2 && parts[1].Count(c => c == ']') == 1)
            {
                neutralFormula = parts[0];
                var adductString = formula.Substring(neutralFormula.Length);
                if (Adduct.TryParse(adductString, out adduct))
                {
                    molecule = neutralFormula.Length > 0 ? ApplyAdductToFormula(neutralFormula, adduct) : Molecule.Empty;
                    return(true);
                }
            }
            return(false);
        }
Beispiel #5
0
        public static string ValidateMeasuredDriftTimeCellValues(string[] values)
        {
            Adduct tempAdduct;
            double tempDouble;

            if (values.Length < 3)
            {
                return(Resources.MeasuredDriftTimeTable_ValidateMeasuredDriftTimeCellValues_The_pasted_text_must_have_three_columns_);
            }

            // Parse sequence
            var sequence = values[EditDriftTimePredictorDlg.COLUMN_SEQUENCE];

            if (string.IsNullOrEmpty(sequence))
            {
                return(Resources.MeasuredDriftTimeTable_ValidateMeasuredDriftTimeCellValues_A_modified_peptide_sequence_is_required_for_each_entry_);
            }

            if (!FastaSequence.IsExSequence(sequence))
            {
                return(string.Format(Resources.MeasuredDriftTimeTable_ValidateMeasuredDriftTimeCellValues_The_sequence__0__is_not_a_valid_modified_peptide_sequence_, sequence));
            }

            try
            {
                values[EditDriftTimePredictorDlg.COLUMN_SEQUENCE] = SequenceMassCalc.NormalizeModifiedSequence(sequence);
            }
            catch (Exception x)
            {
                return(x.Message);
            }

            // Parse charge
            if ((!Adduct.TryParse(values[EditDriftTimePredictorDlg.COLUMN_CHARGE].Trim(), out tempAdduct)) || ValidateCharge(tempAdduct) != null)
            {
                return(string.Format(Resources.EditDriftTimePredictorDlg_ValidateCharge_The_entry__0__is_not_a_valid_charge__Precursor_charges_must_be_integer_values_between_1_and__1__,
                                     values[EditDriftTimePredictorDlg.COLUMN_CHARGE].Trim(), TransitionGroup.MAX_PRECURSOR_CHARGE));
            }

            // Parse drift time
            if (!double.TryParse(values[EditDriftTimePredictorDlg.COLUMN_ION_MOBILITY].Trim(), out tempDouble))
            {
                return(string.Format(Resources.MeasuredDriftTimeTable_ValidateMeasuredDriftTimeCellValues_The_value__0__is_not_a_valid_drift_time_, values[EditDriftTimePredictorDlg.COLUMN_ION_MOBILITY].Trim()));
            }

            if (values.Length > EditDriftTimePredictorDlg.COLUMN_CCS)
            {
                // Parse CCS, if any
                if (!string.IsNullOrEmpty(values[EditDriftTimePredictorDlg.COLUMN_CCS]) &&
                    !double.TryParse(values[EditDriftTimePredictorDlg.COLUMN_CCS].Trim(), out tempDouble))
                {
                    return(string.Format(Resources.MeasuredDriftTimeTable_ValidateMeasuredDriftTimeCellValues_The_value__0__is_not_a_valid_collisional_cross_section_, values[EditDriftTimePredictorDlg.COLUMN_CCS].Trim()));
                }
            }
            if (values.Length > EditDriftTimePredictorDlg.COLUMN_HIGH_ENERGY_OFFSET)
            {
                // Parse high energy offset, if any
                if (!string.IsNullOrEmpty(values[EditDriftTimePredictorDlg.COLUMN_HIGH_ENERGY_OFFSET]) &&
                    !double.TryParse(values[EditDriftTimePredictorDlg.COLUMN_HIGH_ENERGY_OFFSET].Trim(), out tempDouble))
                {
                    return(string.Format(Resources.MeasuredDriftTimeTable_ValidateMeasuredDriftTimeCellValues_The_value__0__is_not_a_valid_high_energy_offset_, values[EditDriftTimePredictorDlg.COLUMN_HIGH_ENERGY_OFFSET].Trim()));
                }
            }
            return(null);
        }
Beispiel #6
0
        private void UpdateAverageAndMonoTextsForFormula()
        {
            bool valid;

            try
            {
                var formula = Formula; // Get current formula and adduct

                var userinput = textFormula.Text.Trim();
                if (_editMode == EditMode.adduct_only)
                {
                    if (!string.IsNullOrEmpty(userinput) && !userinput.StartsWith(@"["))
                    {
                        // Assume they're trying to type an adduct
                        userinput = @"[" + userinput + @"]";
                    }
                    if (string.IsNullOrEmpty(NeutralFormula))
                    {
                        formula = null; // Parent molecule was described as mass only
                    }
                    else
                    {
                        formula = NeutralFormula + userinput; // Try to apply this new adduct to parent molecule
                    }
                }
                else
                {
                    formula = userinput;
                }
                string   neutralFormula;
                Molecule ion;
                Adduct   adduct;
                if (!IonInfo.IsFormulaWithAdduct(formula, out ion, out adduct, out neutralFormula))
                {
                    neutralFormula = formula;
                    if (!Adduct.TryParse(userinput, out adduct))
                    {
                        adduct = Adduct.EMPTY;
                    }
                }
                if (_editMode != EditMode.adduct_only)
                {
                    NeutralFormula = neutralFormula;
                }
                if (_editMode != EditMode.formula_only)
                {
                    Adduct = adduct;
                }
                // Update mass/mz displays
                if (string.IsNullOrEmpty(neutralFormula))
                {
                    if (!adduct.IsEmpty)
                    {
                        // No formula, but adduct changed
                        Adduct = adduct;
                        // ReSharper disable once PossibleNullReferenceException
                        GetTextFromMass(_neutralMonoMass, MassType.Monoisotopic); // Just to see if it throws or not
                        GetTextFromMass(_neutralAverageMass, MassType.Average);   // Just to see if it throws or not
                    }
                }
                else
                {
                    // Is there an isotopic label we should apply to get the mass?
                    if (IsotopeLabelsForMassCalc != null && (Adduct.IsEmpty || !Adduct.HasIsotopeLabels)) // If adduct declares an isotope, that takes precedence
                    {
                        neutralFormula = IsotopeLabelsForMassCalc.Aggregate(neutralFormula, (current, kvp) => current.Replace(kvp.Key, kvp.Value));
                    }
                    var monoMass    = SequenceMassCalc.FormulaMass(BioMassCalc.MONOISOTOPIC, neutralFormula, SequenceMassCalc.MassPrecision);
                    var averageMass = SequenceMassCalc.FormulaMass(BioMassCalc.AVERAGE, neutralFormula, SequenceMassCalc.MassPrecision);
                    GetTextFromMass(monoMass, MassType.Monoisotopic); // Just to see if it throws or not
                    GetTextFromMass(averageMass, MassType.Average);   // Just to see if it throws or not
                    MonoMass    = monoMass;
                    AverageMass = averageMass;
                }
                valid = true; // If we got here, formula parsed OK, or adduct did
                textFormula.ForeColor = Color.Black;
                if (_editMode == EditMode.adduct_only)
                {
                    textFormula.Text = userinput; // Enforce proper adduct formatting
                    if (adduct.IsEmpty)
                    {
                        valid = false; // Adduct did not parse
                    }
                }
                else if (_editMode == EditMode.formula_only)
                {
                    valid &= adduct.IsEmpty; // Should not have anything going on with adduct here
                }
            }
            catch (InvalidOperationException)
            {
                valid = false;
            }
            catch (ArgumentException)
            {
                valid = false;
            }
            if (valid)
            {
                textFormula.ForeColor = Color.Black;
            }
            else
            {
                textFormula.ForeColor = Color.Red;
                textMono.Text         = textAverage.Text = string.Empty;
            }

            // Allow direct editing of masses if direct editing of formula is allowed, but formula is empty
            MassEnabled = _editMode != EditMode.adduct_only && string.IsNullOrEmpty(_neutralFormula);
        }