Example #1
0
 public void Validate()
 {
     if (!string.IsNullOrEmpty(Formula))
     {
         try
         {
             MonoisotopicMass = SequenceMassCalc.ParseModMass(BioMassCalc.MONOISOTOPIC, Formula);
             AverageMass      = SequenceMassCalc.ParseModMass(BioMassCalc.AVERAGE, Formula);
         }
         catch (ArgumentException x)
         {
             throw new InvalidDataException(x.Message, x);  // Pass original as inner exception
         }
     }
     if (AverageMass == 0 || MonoisotopicMass == 0)
     {
         throw new InvalidDataException(Resources.CustomIon_Validate_Custom_ions_must_specify_a_formula_or_valid_monoisotopic_and_average_masses_);
     }
     if (AverageMass > MAX_MASS || MonoisotopicMass > MAX_MASS)
     {
         throw new InvalidDataException(string.Format(Resources.CustomIon_Validate_The_mass_of_the_custom_ion_exceeeds_the_maximum_of__0_, MAX_MASS));
     }
     if (AverageMass < MIN_MASS || MonoisotopicMass < MIN_MASS)
     {
         throw new InvalidDataException(string.Format(Resources.CustomIon_Validate_The_mass_of_the_custom_ion_is_less_than_the_minimum_of__0__, MIN_MASS));
     }
 }
Example #2
0
        private void UpdateAverageAndMonoTextsForFormula()
        {
            string formula = textFormula.Text;

            if (string.IsNullOrEmpty(formula))
            {
                // Leave any precalculated masses in place for user convenience
                textMono.Enabled = textAverage.Enabled = true;
            }
            else
            {
                // Formula drives mass, no direct edit allowed
                textMono.Enabled = textAverage.Enabled = false;
                try
                {
                    var monoMass    = SequenceMassCalc.ParseModMass(BioMassCalc.MONOISOTOPIC, formula);
                    var averageMass = SequenceMassCalc.ParseModMass(BioMassCalc.AVERAGE, formula);
                    GetTextFromMass(monoMass);     // Just to see if it throws or not
                    GetTextFromMass(averageMass);  // Just to see if it throws or not
                    MonoMass              = monoMass;
                    AverageMass           = averageMass;
                    textFormula.ForeColor = Color.Black;
                }
                catch (ArgumentException)
                {
                    textFormula.ForeColor = Color.Red;
                    textMono.Text         = textAverage.Text = string.Empty;
                }
            }
        }
Example #3
0
 public static bool IsValidLibKey(string key)
 {
     try
     {
         SequenceMassCalc.ParseModMass(BioMassCalc.AVERAGE, key);
     }
     catch
     {
         return(false);
     }
     return(true);
 }
Example #4
0
        private MeasuredIon ValidateCustomIon(string name)
        {
            var    helper  = new MessageBoxHelper(this);
            string formula = _formulaBox.Formula.ToString(LocalizationHelper.CurrentCulture);
            var    charge  = ValidateCharge();

            if (!charge.HasValue)
            {
                return(null);
            }
            double monoMass;
            double avgMass;

            if (!string.IsNullOrEmpty(formula))
            {
                // Mass is specified by chemical formula
                try
                {
                    monoMass = SequenceMassCalc.ParseModMass(BioMassCalc.MONOISOTOPIC, formula);
                    avgMass  = SequenceMassCalc.ParseModMass(BioMassCalc.AVERAGE, formula);
                }
                catch (ArgumentException x)
                {
                    helper.ShowTextBoxError(_formulaBox, x.Message);
                    return(null);
                }
            }
            else if (_formulaBox.MonoMass != null ||
                     _formulaBox.AverageMass != null)
            {
                // Mass is specified by combination of mz and charge
                formula = null;
                if (!_formulaBox.ValidateMonoText(helper))
                {
                    return(null);
                }
                if (!_formulaBox.ValidateAverageText(helper))
                {
                    return(null);
                }
                _formulaBox.Charge = charge; // This provokes calculation of mass from displayed mz values
                monoMass           = _formulaBox.MonoMass.Value;
                avgMass            = _formulaBox.AverageMass.Value;
            }
            else
            {
                // User hasn't fully specified either way
                _formulaBox.ShowTextBoxErrorFormula(helper,
                                                    Resources.EditMeasuredIonDlg_OkDialog_Please_specify_a_formula_or_constant_masses);
                return(null);
            }
            if (MeasuredIon.MIN_REPORTER_MASS > monoMass || MeasuredIon.MIN_REPORTER_MASS > avgMass)
            {
                _formulaBox.ShowTextBoxErrorMonoMass(helper, string.Format(Resources.EditMeasuredIonDlg_OkDialog_Reporter_ion_masses_must_be_less_than_or_equal_to__0__,
                                                                           MeasuredIon.MAX_REPORTER_MASS));
                return(null);
            }
            if (monoMass > MeasuredIon.MAX_REPORTER_MASS || avgMass > MeasuredIon.MAX_REPORTER_MASS)
            {
                _formulaBox.ShowTextBoxErrorAverageMass(helper, string.Format(Resources.EditMeasuredIonDlg_OkDialog_Reporter_ion_masses_must_be_less_than_or_equal_to__0__,
                                                                              MeasuredIon.MAX_REPORTER_MASS));
                return(null);
            }

            return(new MeasuredIon(name, formula, monoMass, avgMass, charge.Value));
        }
Example #5
0
        public void TestUniMod()
        {
            // UpdateTestXML is used to update the test files if the modifications in UniMod.cs
            // have changed. Before UpdateTestXML is called, TestUniMod should pass with the new changes
            // to make sure we haven't lost/broken any modifications from earlier versions of UniMod.cs.
            // Note: The test will always run against the last build XML. Run twice when updating.
            UpdateTestXML();

            foreach (StaticMod mod in UniMod.DictUniModIds.Values)
            {
                // UniModCompiler should not set the masses.
                if (mod.Formula == null)
                {
                    Assert.IsNull(mod.MonoisotopicMass);
                    Assert.IsNull(mod.AverageMass);
                }
                else
                {
                    Assert.AreEqual(mod.MonoisotopicMass,
                                    SequenceMassCalc.ParseModMass(BioMassCalc.MONOISOTOPIC, mod.Formula));
                    Assert.AreEqual(mod.AverageMass,
                                    SequenceMassCalc.ParseModMass(BioMassCalc.AVERAGE, mod.Formula));
                }
                // Everything amino acid/terminus that is part of the modification should be present in
                // the name of the modification.
                var aasAndTermInName = mod.Name.Split(new[] { ' ' }, 2)[1];
                if (mod.Terminus != null)
                {
                    Assert.IsTrue(aasAndTermInName.Contains(mod.Terminus.Value.ToString()));
                }
                if (mod.AAs != null)
                {
                    foreach (char aa in mod.AminoAcids)
                    {
                        Assert.IsTrue(aasAndTermInName.Contains(aa));
                    }
                }
                // Should not have label atoms if no amino acids are listed.
                if (!Equals(mod.LabelAtoms, LabelAtoms.None))
                {
                    Assert.IsTrue(mod.AAs != null);
                }
            }

            // Testing ValidateID.
            var phospho = UniMod.DictStructuralModNames["Phospho (ST)"];

            Assert.IsTrue(UniMod.ValidateID(phospho.ChangeExplicit(true)));
            Assert.IsTrue(UniMod.ValidateID(phospho.ChangeVariable(true)));
            Assert.IsFalse(UniMod.ValidateID((StaticMod)phospho.ChangeName("Phospho")));

            StreamReader staticReader = new StreamReader(GetTestStream(STATIC_LIST_FILE));
            string       staticMods   = staticReader.ReadToEnd();

            staticReader.Close();
            AssertEx.DeserializeNoError <StaticModList>(staticMods, false);

            StreamReader heavyReader = new StreamReader(GetTestStream(HEAVY_LIST_FILE));
            string       heavyMods   = heavyReader.ReadToEnd();

            heavyReader.Close();
            AssertEx.DeserializeNoError <HeavyModList>(heavyMods, false);
        }
Example #6
0
        public void OkDialog()
        {
            var helper = new MessageBoxHelper(this);

            string name;

            if (!helper.ValidateNameTextBox(_editing ? (Control)textName : comboMod, out name))
            {
                return;
            }

            // Allow updating the original modification
            if (!_editing || !Equals(name, Modification.Name))
            {
                if (!ModNameAvailable(name))
                {
                    helper.ShowTextBoxError(_editing ? (Control)textName : comboMod,
                                            Resources.EditStaticModDlg_OkDialog_The_modification__0__already_exists, name);
                    return;
                }
            }

            string aas = comboAA.Text;

            if (string.IsNullOrEmpty(aas))
            {
                aas = null;
            }
            else
            {
                // Use the cleanest possible format.
                var sb = new StringBuilder();
                foreach (string aaPart in aas.Split(SEPARATOR_AA))
                {
                    string aa = aaPart.Trim();
                    if (aa.Length == 0)
                    {
                        continue;
                    }
                    if (sb.Length > 0)
                    {
                        sb.Append(", "); // Not L10N
                    }
                    sb.Append(aa);
                }
            }

            string      termString = comboTerm.SelectedItem.ToString();
            ModTerminus?term       = null;

            if (!string.IsNullOrEmpty(termString))
            {
                term = (ModTerminus)Enum.Parse(typeof(ModTerminus), termString);
            }

            if (cbVariableMod.Checked && aas == null && term == null)
            {
                MessageDlg.Show(this, Resources.EditStaticModDlg_OkDialog_Variable_modifications_must_specify_amino_acid_or_terminus);
                comboAA.Focus();
                return;
            }

            string     formula    = null;
            double?    monoMass   = null;
            double?    avgMass    = null;
            LabelAtoms labelAtoms = LabelAtoms.None;

            if (cbChemicalFormula.Checked)
            {
                formula = Formula;
            }
            else
            {
                labelAtoms = LabelAtoms;
            }

            // Get the losses to know whether any exist below
            IList <FragmentLoss> losses = null;

            if (listNeutralLosses.Items.Count > 0)
            {
                losses = Losses.ToArray();
            }

            if (!string.IsNullOrEmpty(formula))
            {
                try
                {
                    SequenceMassCalc.ParseModMass(BioMassCalc.MONOISOTOPIC, formula);
                }
                catch (ArgumentException x)
                {
                    _formulaBox.ShowTextBoxErrorFormula(helper, x.Message);
                    return;
                }
            }
            else if (labelAtoms == LabelAtoms.None)
            {
                formula = null;

                // Allow formula and both masses to be empty, if losses are present
                if (NotZero(_formulaBox.MonoMass) || NotZero(_formulaBox.AverageMass) || losses == null)
                {
                    // TODO: Maximum and minimum masses should be formalized and applied everywhere
                    double mass;
                    if (!_formulaBox.ValidateMonoText(helper, -1500, 5000, out mass))
                    {
                        return;
                    }
                    monoMass = mass;
                    if (!_formulaBox.ValidateAverageText(helper, -1500, 5000, out mass))
                    {
                        return;
                    }
                    avgMass = mass;
                }
                // Loss-only modifications may not be variable
                else if (cbVariableMod.Checked)
                {
                    MessageDlg.Show(this, Resources.EditStaticModDlg_OkDialog_The_variable_checkbox_only_applies_to_precursor_modification_Product_ion_losses_are_inherently_variable);
                    cbVariableMod.Focus();
                    return;
                }
            }
            else if (aas == null && term.HasValue)
            {
                MessageDlg.Show(this, Resources.EditStaticModDlg_OkDialog_Labeled_atoms_on_terminal_modification_are_not_valid);
                return;
            }

            RelativeRT relativeRT = RelativeRT.Matching;

            if (comboRelativeRT.Visible && comboRelativeRT.SelectedItem != null)
            {
                relativeRT = RelativeRTExtension.GetEnum(comboRelativeRT.SelectedItem.ToString());
            }

            // Store state of the chemical formula checkbox for next use.
            if (cbChemicalFormula.Visible)
            {
                Settings.Default.ShowHeavyFormula = _formulaBox.FormulaVisible;
            }

            var newMod = new StaticMod(name,
                                       aas,
                                       term,
                                       cbVariableMod.Checked,
                                       formula,
                                       labelAtoms,
                                       relativeRT,
                                       monoMass,
                                       avgMass,
                                       losses);

            foreach (StaticMod mod in _existing)
            {
                if (newMod.Equivalent(mod) && !(_editing && mod.Equals(_originalModification)))
                {
                    if (DialogResult.OK == MultiButtonMsgDlg.Show(
                            this,
                            TextUtil.LineSeparate(Resources.EditStaticModDlg_OkDialog_There_is_an_existing_modification_with_the_same_settings,
                                                  string.Format("'{0}'.", mod.Name), // Not L10N
                                                  string.Empty,
                                                  Resources.EditStaticModDlg_OkDialog_Continue),
                            MultiButtonMsgDlg.BUTTON_OK))
                    {
                        Modification = newMod;
                        DialogResult = DialogResult.OK;
                    }
                    return;
                }
            }

            var uniMod = UniMod.GetModification(name, IsStructural);

            // If the modification name is not found in Unimod, check if there exists a modification in Unimod that matches
            // the dialog modification, and prompt the user to to use the Unimod modification instead.
            if (uniMod == null)
            {
                var matchingMod = UniMod.FindMatchingStaticMod(newMod, IsStructural);
                if (matchingMod != null &&
                    (ModNameAvailable(matchingMod.Name) ||
                     (_editing && Equals(matchingMod.Name, Modification.Name))))
                {
                    var result = MultiButtonMsgDlg.Show(
                        this,
                        TextUtil.LineSeparate(Resources.EditStaticModDlg_OkDialog_There_is_a_Unimod_modification_with_the_same_settings,
                                              string.Empty,
                                              string.Format(Resources.EditStaticModDlg_OkDialog_Click__Unimod__to_use_the_name___0___, matchingMod.Name),
                                              string.Format(Resources.EditStaticModDlg_OkDialog_Click__Custom__to_use_the_name___0___, name)),
                        Resources.EditStaticModDlg_OkDialog_Unimod,
                        Resources.EditStaticModDlg_OkDialog_Custom,
                        true);
                    if (result == DialogResult.Yes)
                    {
                        newMod = matchingMod.MatchVariableAndLossInclusion(newMod);   // Unimod
                    }
                    if (result == DialogResult.Cancel)
                    {
                        return;
                    }
                }
            }
            else
            {
                // If the dialog modification matches the modification of the same name in Unimod,
                // use the UnimodId.
                if (newMod.Equivalent(uniMod))
                {
                    newMod = uniMod.MatchVariableAndLossInclusion(newMod);
                }
                else
                {
                    // Finally, if the modification name is found in Unimod, but the modification in Unimod does not
                    // match the dialog modification, prompt the user to use the Unimod modification definition instead.
                    if (DialogResult.OK != MultiButtonMsgDlg.Show(
                            this,
                            TextUtil.LineSeparate(string.Format(Resources.EditStaticModDlg_OkDialog_This_modification_does_not_match_the_Unimod_specifications_for___0___, name),
                                                  string.Empty,
                                                  Resources.EditStaticModDlg_OkDialog_Use_non_standard_settings_for_this_name),
                            MultiButtonMsgDlg.BUTTON_OK))
                    {
                        return;
                    }
                }
            }

            _modification = newMod;

            DialogResult = DialogResult.OK;
        }
        public void OkDialog()
        {
            var helper = new MessageBoxHelper(this);

            string formulaLoss = _formulaBox.Formula;
            double?monoLoss    = null;
            double?avgLoss     = null;

            if (!string.IsNullOrEmpty(formulaLoss))
            {
                try
                {
                    double massMono    = SequenceMassCalc.ParseModMass(BioMassCalc.MONOISOTOPIC, formulaLoss);
                    double massAverage = SequenceMassCalc.ParseModMass(BioMassCalc.AVERAGE, formulaLoss);
                    if (FragmentLoss.MIN_LOSS_MASS > massMono || FragmentLoss.MIN_LOSS_MASS > massAverage)
                    {
                        _formulaBox.ShowTextBoxErrorFormula(helper, string.Format(Resources.EditFragmentLossDlg_OkDialog_Neutral_loss_masses_must_be_greater_than_or_equal_to__0__,
                                                                                  FragmentLoss.MIN_LOSS_MASS));
                        return;
                    }
                    if (massMono > FragmentLoss.MAX_LOSS_MASS || massAverage > FragmentLoss.MAX_LOSS_MASS)
                    {
                        _formulaBox.ShowTextBoxErrorFormula(helper, string.Format(Resources.EditFragmentLossDlg_OkDialog_Neutral_loss_masses_must_be_less_than_or_equal_to__0__,
                                                                                  FragmentLoss.MAX_LOSS_MASS));
                        return;
                    }
                }
                catch (ArgumentException x)
                {
                    _formulaBox.ShowTextBoxErrorFormula(helper, x.Message);
                    return;
                }
            }
            else if (_formulaBox.MonoMass != null ||
                     _formulaBox.AverageMass != null)
            {
                formulaLoss = null;
                double mass;
                if (!_formulaBox.ValidateMonoText(helper, FragmentLoss.MIN_LOSS_MASS, FragmentLoss.MAX_LOSS_MASS, out mass))
                {
                    return;
                }
                monoLoss = mass;
                if (!_formulaBox.ValidateAverageText(helper, FragmentLoss.MIN_LOSS_MASS, FragmentLoss.MAX_LOSS_MASS, out mass))
                {
                    return;
                }
                avgLoss = mass;
            }
            else
            {
                _formulaBox.ShowTextBoxErrorFormula(helper, Resources.EditFragmentLossDlg_OkDialog_Please_specify_a_formula_or_constant_masses);
                return;
            }

            // Make sure the new loss does not already exist.
            var loss = new FragmentLoss(formulaLoss, monoLoss, avgLoss, Inclusion);

            if (_existing.Contains(loss))
            {
                MessageDlg.Show(this, string.Format(Resources.EditFragmentLossDlg_OkDialog_The_loss__0__already_exists, loss));
                return;
            }

            Loss = loss;

            DialogResult = DialogResult.OK;
        }