private static List<MSPeak> Deisotope(IEnumerable<MSPeak> peaks, int maxAbsoluteCharge, int polarity, MassTolerance isotopicMZTolerance)
{
List<MSPeak> new_peaks = new List<MSPeak>(peaks);
int p = new_peaks.Count - 1;
while(p >= 1)
{
int q = p - 1;
bool removed = false;
while(q >= 0)
{
if(new_peaks[p].MZ > (new_peaks[q].MZ + Constants.C12_C13_MASS_DIFFERENCE) + isotopicMZTolerance)
{
break;
}
if(new_peaks[p].Intensity < new_peaks[q].Intensity)
{
if(polarity == 0)
{
if(Math.Abs(MassTolerance.CalculateMassError(new_peaks[p].MZ, new_peaks[q].MZ + Constants.C12_C13_MASS_DIFFERENCE, isotopicMZTolerance.Units)) <= isotopicMZTolerance.Value)
{
new_peaks.RemoveAt(p);
removed = true;
break;
}
}
else
{
for(int c = polarity; polarity > 0 ? c <= maxAbsoluteCharge : c >= -maxAbsoluteCharge; c += polarity)
{
if(Math.Abs(MassTolerance.CalculateMassError(new_peaks[p].MZ, new_peaks[q].MZ + Constants.C12_C13_MASS_DIFFERENCE / Math.Abs(c), isotopicMZTolerance.Units)) <= isotopicMZTolerance.Value)
{
new_peaks.RemoveAt(p);
removed = true;
break;
}
}
}
if(removed)
{
break;
}
}
q--;
}
p--;
}
return new_peaks;
}