public FromMass ( double lowMass, double highMass, int maxNumberOfResults = int.MaxValue, bool sort = true ) : IEnumerable |
||
lowMass | double | |
highMass | double | |
maxNumberOfResults | int | |
sort | bool | |
Résultat | IEnumerable |
public IEnumerable<ChemicalFormula> FromMass(double lowMass, double highMass, int maxNumberOfResults = int.MaxValue, bool sort = true)
{
if (highMass <= lowMass)
{
throw new ArgumentException("The high mass must be greater than the low mass");
}
if (!MaximumFormula.Contains(MinimumFormula))
{
throw new ArgumentException("The maximum formula must include the minimum formula");
}
List<ChemicalFormula> returnFormulas = new List<ChemicalFormula>();
// The minimum formula required for any return formulas
double correctedLowMass = lowMass;
double correctedHighMass = highMass;
bool minFormulaExists = MinimumFormula.IsotopeCount != 0;
int[] minValues = null;
double minFormulaMass = 0;
if (minFormulaExists)
{
minValues = MinimumFormula.GetIsotopes();
minFormulaMass = MinimumFormula.MonoisotopicMass;
correctedLowMass -= minFormulaMass;
correctedHighMass -= minFormulaMass;
// Add the minimum formula itself if it is within the bounds
}
// The maximum formula allowed, represented in number of isotopes
int[] maxValues = MaximumFormula.GetIsotopes();
// The current formula represented in isotopes
int[] currentFormula = new int[maxValues.Length];
// A list of all the isotopes masses
double[] masses = new double[maxValues.Length];
int length = maxValues.Length;
for (int j = 0; j < length; j++)
{
if (minFormulaExists && j < minValues.Length)
maxValues[j] -= minValues[j];
if (maxValues[j] == 0)
continue;
masses[j] = PeriodicTable.GetIsotope(j).AtomicMass;
}
masses[0] = PeriodicTable.GetIsotope(0).AtomicMass;
GenerateFormulaHelper(correctedLowMass, correctedHighMass, masses, maxValues, length - 1, currentFormula, returnFormulas);
if (minFormulaExists)
{
foreach (ChemicalFormula formula in returnFormulas)
{
formula.Add(MinimumFormula);
}
if (minFormulaMass >= lowMass && minFormulaMass <= highMass)
{
returnFormulas.Add(new ChemicalFormula(MinimumFormula));
}
}
if (!sort)
return returnFormulas;
double meanValue = (highMass + lowMass)/2.0;
return returnFormulas.OrderBy(formula => Math.Abs(formula.MonoisotopicMass - meanValue)).Take(maxNumberOfResults);
}
ChemicalFormulaGenerator::FromMass ( IRange |
private static void ChemicalFormulaGeneratorExample() { //// Create a generator with formula bounds. In this case, every formula needs to have at least H2 but no more than C4H2 //ChemicalFormulaGenerator generator = new ChemicalFormulaGenerator(new ChemicalFormula("H2"), new ChemicalFormula("C4H2")); //var formulas = generator.FromMass(0, 100).ToList(); //foreach (var formula in formulas) //{ // WriteFormulaToConsole(formula); //} //Console.WriteLine("Unique Formulas: " + formulas.Count); // Create a generator with formula bounds. In this case, every formula needs to have at least H2 but no more than C4H2 ChemicalFormulaGenerator generator = new ChemicalFormulaGenerator(new ChemicalFormula("C10000N1000H1000O1000")); DoubleRange range = DoubleRange.FromPPM(new ChemicalFormula("C62H54N42O24").MonoisotopicMass, 0.1); double mass = range.Mean; int count = 0; foreach (var formula in generator.FromMass(range).Validate()) { Console.WriteLine("{0,-15} {1:F10} {2,-5:G5} ppm", formula, formula.MonoisotopicMass, Tolerance.GetTolerance(formula.MonoisotopicMass, mass, ToleranceUnit.PPM)); count++; } Console.WriteLine("Unique Formulas: " + count); }