Skip to content

Commit

Permalink
Skyline: Clean-up "StaticMod.PrecisionRequired" changes (#2765)
Browse files Browse the repository at this point in the history
* Skyline: Clean-up "StaticMod.PrecisionRequired" changes
- These changes ended up not getting used and just add confusion to the code
- Revert UniModCompiler/InputFiles/unimod.xml to what was actually used to generate UniModData.cs
- Add a few changes to UniModTemplate.cs to make it match UniModData.cs
  • Loading branch information
brendanx67 authored Nov 1, 2023
1 parent e66e43d commit fafbb32
Show file tree
Hide file tree
Showing 8 changed files with 1,936 additions and 17,145 deletions.
16,641 changes: 788 additions & 15,853 deletions pwiz_tools/Skyline/Executables/UniModCompiler/InputFiles/unimod.xml

Large diffs are not rendered by default.

129 changes: 2 additions & 127 deletions pwiz_tools/Skyline/Executables/UniModCompiler/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
using System.Linq;
using System.IO;
using System.Xml.Serialization;
using pwiz.Common.Chemistry;

namespace UniModCompiler
{
Expand All @@ -45,9 +44,6 @@ class Program
private static List<Mod> _listedHiddenMods;
private static List<string> _impossibleMods;
private static Dictionary<string, ThreeLetterCodeUsed> _dictModNameToThreeLetterCode;
private static readonly MassDistribution EmptyMassDistribution = new MassDistribution(.001, .00001);
private static readonly IsotopeAbundances IsotopeAbundances = GetIsotopeAbundances();
private static Dictionary<Mod, int> _requiredPrecisions;

private class ThreeLetterCodeUsed
{
Expand Down Expand Up @@ -128,8 +124,6 @@ static void Main()
}
}

_requiredPrecisions = GetRequiredPrecisions(3);

_impossibleMods = new List<string>();

// Writing the output file.
Expand Down Expand Up @@ -381,7 +375,7 @@ private static IEnumerable<Mod> ReadListedMods(StreamReader reader)
}
return listedMods;
}

/// <summary>
/// Search for the listed modifications within the XML modifications, and write matches to the C# file.
/// </summary>
Expand Down Expand Up @@ -491,11 +485,7 @@ private static void WriteMods(StreamWriter writer, bool hidden, bool isotopic)
}
}
}
writer.Write("Hidden = {0}, ", hidden.ToString().ToLower());
int precisionRequired;
if (!_requiredPrecisions.TryGetValue(mod, out precisionRequired))
precisionRequired = 1;
writer.WriteLine("PrecisionRequired = {0}", precisionRequired);
writer.WriteLine("Hidden = {0}, ", hidden.ToString().ToLower());
writer.WriteLine(" },");


Expand All @@ -506,99 +496,6 @@ private static void WriteMods(StreamWriter writer, bool hidden, bool isotopic)
}
}

private static Dictionary<Mod, int> GetRequiredPrecisions(int decimalsToCheck)
{
var all = new List<Tuple<Mod, double>>();
foreach (var mod in _listedMods.Concat(_listedHiddenMods))
{
var dictMods = new List<mod_t>();
mod_t dictLookupMod;
if (_dictIsotopeMods.TryGetValue(mod.Title, out dictLookupMod))
dictMods.Add(dictLookupMod);
if (_dictStructuralMods.TryGetValue(mod.Title, out dictLookupMod))
dictMods.Add(dictLookupMod);
if (!dictMods.Any())
continue;

var curMod = mod;
all.AddRange(from dictMod in dictMods
select BuildFormula(dictMod.delta.element)
into skylineFormula where skylineFormula.Length > 0
select GetMassDistribution(skylineFormula)
into massDistribution
select massDistribution.MostAbundanceMass into monoMass
select Tuple.Create(curMod, monoMass));
}

all.Sort((x, y) => x.Item2.CompareTo(y.Item2));
var precisions = new Dictionary<Mod, int>();
for (var i = 0; i < all.Count; i++)
{
var current = all[i];
var requiredLower = RequiredPrecision(current, all.Take(i).Reverse(), decimalsToCheck);
var requiredUpper = RequiredPrecision(current, all.Skip(i + 1), decimalsToCheck);
precisions[current.Item1] = Math.Max(requiredLower, requiredUpper);
}

return precisions;
}

private static int RequiredPrecision(Tuple<Mod, double> cur, IEnumerable<Tuple<Mod, double>> check, int decimalsToCheck)
{
var protein = string.Join(string.Empty, cur.Item1.AAs).Equals("Protein");
// find first Mod in check with at least one matching AA
foreach (var other in check)
{
if (protein || !cur.Item1.AAs.Any() || !other.Item1.AAs.Any() || cur.Item1.AAs.Intersect(other.Item1.AAs).Any())
{
// find minimum amount of decimals needed to distinguish cur from other
for (var i = 1; i <= decimalsToCheck; i++)
{
if (!ValuesEqualWithPrecision(cur.Item2, other.Item2, i))
return i;
}
break; // not distinguishable with any amount of decimals up to limit
}
}
return 1;
}

private static bool ValuesEqualWithPrecision(double x, double y, int precision)
{
var formatString = "F0" + precision;
return Equals(x.ToString(formatString), y.ToString(formatString));
}

private static MassDistribution GetMassDistribution(string formula)
{
var md = EmptyMassDistribution;
var result = md;
Molecule moleculePlus;
Molecule moleculeMinus;
var indexMinus = formula.IndexOf("-", StringComparison.InvariantCulture);
if (indexMinus < 0)
{
moleculePlus = Molecule.Parse(formula.Trim());
moleculeMinus = Molecule.Empty;
}
else
{
moleculePlus = Molecule.Parse(formula.Substring(0, indexMinus).Trim());
moleculeMinus = Molecule.Parse(formula.Substring(indexMinus + 1).Trim());
}
var isotopeAbundances = IsotopeAbundances;
foreach (var element in moleculePlus)
{
result = result.Add(md.Add(isotopeAbundances[element.Key]).Multiply(element.Value));
}
foreach (var element in moleculeMinus)
{
result = result.Add(md.Add(isotopeAbundances[element.Key]).Multiply(-element.Value));
}
return result;

}

private static bool ListEquals(List<string> list1, List<string> list2)
{
for (int i = 0; i < list1.Count; i++)
Expand Down Expand Up @@ -779,28 +676,6 @@ private static void SequenceMassCalc()
AMINO_FORMULAS['y'] = AMINO_FORMULAS['Y'] = "C9H9O2N";
// ReSharper restore CharImplicitlyConvertedToNumeric
}

private static IsotopeAbundances GetIsotopeAbundances()
{
var isotopeAbundances = IsotopeAbundances.Default;
isotopeAbundances = isotopeAbundances.SetAbundances(
new Dictionary<string, MassDistribution>
{
{"H'", SingleMass(2.014101779)},
{"O\"", SingleMass(16.9991315)},
{"O'", SingleMass(17.9991604)},
{"N'", SingleMass(15.0001088984)},
{"C'", SingleMass(13.0033548378)},
{"Cl'", SingleMass(36.965902602)},
{"Br'", SingleMass(80.9162897)}
});
return isotopeAbundances;
}

private static MassDistribution SingleMass(double mass)
{
return EmptyMassDistribution.SetAbundance(mass, 1.0);
}
}

}
19 changes: 3 additions & 16 deletions pwiz_tools/Skyline/Executables/UniModCompiler/UniModTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// ReSharper disable NonLocalizedString
// ReSharper disable LocalizableElement
namespace pwiz.Skyline.Model.DocSettings
{
public static class UniModData
Expand All @@ -35,7 +35,7 @@ public static class UniModData
// Hardcoded Skyline Mods
new UniModModificationData
{
Name = "Amonia Loss (K, N, Q, R)",
Name = "Ammonia Loss (K, N, Q, R)",
AAs = "K, N, Q, R", LabelAtoms = LabelAtoms.None, Losses = new [] { new FragmentLoss("NH3"), },
Structural = true, Hidden = false,
},
Expand Down Expand Up @@ -86,20 +86,7 @@ public static class UniModData
Name = "Label:13C(6) (C-term R)",
AAs = "R", Terminus = ModTerminus.C, LabelAtoms = LabelAtoms.C13,
Structural = false, Hidden = false,
// },
// NOT YET - Brendan wants to see demonstrated user need 7-21-17
// new UniModModificationData
// {
// Name = "Label:37Cl",
// LabelAtoms = LabelAtoms.Cl37, // For small molecule use
// Structural = false, Hidden = false,
// },
// new UniModModificationData
// {
// Name = "Label:81Br",
// LabelAtoms = LabelAtoms.Br81, // For small molecule use
// Structural = false, Hidden = false,
}
}
};
}
}
11 changes: 3 additions & 8 deletions pwiz_tools/Skyline/Model/DocSettings/Modification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,14 @@ public StaticMod(string name, string aas, ModTerminus? term, LabelAtoms labelAto

public StaticMod(string name, string aas, ModTerminus? term,
string formula, LabelAtoms labelAtoms, double? monoMass, double? avgMass)
: this(name, aas, term, false, formula, labelAtoms, RelativeRT.Matching, monoMass, avgMass, null, null, null, null)
: this(name, aas, term, false, formula, labelAtoms, RelativeRT.Matching, monoMass, avgMass, null, null, null)
{

}

public StaticMod(string name, string aas, ModTerminus? term, bool isVariable, string formula,
LabelAtoms labelAtoms, RelativeRT relativeRT, double? monoMass, double? avgMass, IList<FragmentLoss> losses)
: this(name, aas, term, isVariable, formula, labelAtoms, relativeRT, monoMass, avgMass, losses, null, null, null)
: this(name, aas, term, isVariable, formula, labelAtoms, relativeRT, monoMass, avgMass, losses, null, null)
{

}
Expand All @@ -144,8 +144,7 @@ public StaticMod(string name,
double? avgMass,
IList<FragmentLoss> losses,
int? uniModId,
string shortName,
int? precisionRequired)
string shortName)
: base(name)
{
AAs = aas;
Expand All @@ -161,7 +160,6 @@ public StaticMod(string name,

UnimodId = uniModId;
ShortName = shortName;
_precisionRequired = precisionRequired;

Validate();
}
Expand Down Expand Up @@ -379,9 +377,6 @@ public bool HasLoss

public string ShortName { get; private set; }

private readonly int? _precisionRequired;
public int PrecisionRequired { get { return _precisionRequired ?? 1; }}

public CrosslinkerSettings CrosslinkerSettings { get; private set; }

public MoleculeMassOffset GetMoleculeMassOffset()
Expand Down
3 changes: 1 addition & 2 deletions pwiz_tools/Skyline/Model/DocSettings/UniMod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private static void AddMod(UniModModificationData data)
{
var newMod = new StaticMod(data.Name, data.AAs, data.Terminus, false, data.Formula, data.LabelAtoms,
RelativeRT.Matching, null, null, data.Losses, data.ID,
data.ShortName, data.PrecisionRequired);
data.ShortName);
if (data.ID.HasValue && data.ShortName != null)
{
int id;
Expand Down Expand Up @@ -216,7 +216,6 @@ public struct UniModModificationData
public int? ID { get; set; }
public bool Structural { get; set; }
public bool Hidden { get; set; }
public int? PrecisionRequired { get; set; }
public string ShortName { get; set; }
}

Expand Down
Loading

0 comments on commit fafbb32

Please sign in to comment.