private List <double> getPeakStatistics() { IList <object[]> queryRows; lock (session) { var randomIds = session.CreateQuery("SELECT psm.Id " + viewFilter.GetFilteredQueryString(DataFilter.FromPeptideSpectrumMatch)) .List <long>() .Shuffle() .Take(1000) .OrderBy(o => o); string randomIdSet = String.Join(",", randomIds.Select(o => o.ToString()).ToArray()); queryRows = session.CreateQuery("SELECT psm.Spectrum.Source.Name, psm.Spectrum, psm, DISTINCT_GROUP_CONCAT(pm.Offset || ':' || mod.MonoMassDelta), psm.Peptide.Sequence " + "FROM PeptideSpectrumMatch psm " + "LEFT JOIN psm.Modifications pm " + "LEFT JOIN pm.Modification mod " + "WHERE psm.Id IN (" + randomIdSet + ") " + "GROUP BY psm.Spectrum.id ") .List <object[]>(); } var spectrumRows = queryRows.Select(o => new SpectrumRow(o)).OrderBy(o => o.SourceName); precursorScatterPlot.Clear(); chargeReducedScatterPlot.Clear(); int spectraCount = 0; string spectrumListFilters = String.Empty; Invoke(new MethodInvoker(() => { spectrumListFilters = spectrumFiltersTextBox.Text; zedGraphControl.MasterPane.AxisChange(); zedGraphControl.Refresh(); })); var points = new PointPairList(); string currentSourceName = null; string currentSourcePath = null; msdata.MSData msd = null; lock (owner) foreach (var row in spectrumRows) { if (row.SourceName != currentSourceName) { currentSourceName = row.SourceName; currentSourcePath = IDPickerForm.LocateSpectrumSource(currentSourceName, session.Connection.GetDataSource()); msd = new pwiz.CLI.msdata.MSDataFile(currentSourcePath); //var param = session.Query<AnalysisParameter>().Where(o => o.Name == "SpectrumListFilters").Min(o => o.Value); //string spectrumListFilters = String.IsNullOrEmpty(param) ? String.Empty : param; SpectrumListFactory.wrap(msd, spectrumListFilters.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)); } string label = String.Format("{0}/{1}\n{2}", row.SourceName, msdata.id.abbreviate(row.Spectrum.NativeID), row.ModifiedSequence); var spectrumList = msd.run.spectrumList; ++spectraCount; var pwizPeptide = new proteome.Peptide(row.ModifiedSequence, proteome.ModificationParsing.ModificationParsing_Auto, proteome.ModificationDelimiter.ModificationDelimiter_Brackets); var fragmentation = pwizPeptide.fragmentation(true, true); var pwizSpectrum = spectrumList.spectrum(spectrumList.find(row.Spectrum.NativeID), true); var pointMap = new seems.PointMap(new ZedGraph.PointPairList(pwizSpectrum.getMZArray().data, pwizSpectrum.getIntensityArray().data)); double tic = pointMap.Values.Sum(); double precursorMz = row.Spectrum.PrecursorMZ; double chargeReducedPrecursorMz = precursorMz * row.PeptideSpectrumMatch.Charge; bool plotMatchedPeaks = true; bool removeMatchedPeaks = false; double tolerance = 0.03; seems.PointMap.Enumerator itr; IonSeries[] ionSeries = Enum.GetValues(typeof(IonSeries)).Cast <IonSeries>().Where(o => o != IonSeries.Count).ToArray(); for (int z = 1; z <= 1; ++z) { for (int length = 1, end = pwizPeptide.sequence.Length; length <= end; ++length) { string NTermFragment = row.ModifiedSequence.Substring(0, length); string CTermFragment = row.ModifiedSequence.Substring(row.ModifiedSequence.Length - length); foreach (IonSeries series in ionSeries) { if ((series == IonSeries.c || series == IonSeries.cMinus1 || series == IonSeries.x) && length == pwizPeptide.sequence.Length) { continue; } itr = pointMap.FindNear(fragmentMass(fragmentation, series, length, z), tolerance); if (itr != null && itr.IsValid) { if (plotMatchedPeaks) { precursorScatterPlot.AddPoint(new PointPair(itr.Current.Key - precursorMz, itr.Current.Value / tic, (int)series, String.Format("{0} {1}\n{2} {3} {4} {5}", label, precursorMz, NTermFragment, itr.Current.Key, IonSeriesLabels[(int)series], length))); chargeReducedScatterPlot.AddPoint(new PointPair(itr.Current.Key - chargeReducedPrecursorMz, itr.Current.Value / tic, (int)series, String.Format("{0} {1}\n{2} {3} {4} {5}", label, chargeReducedPrecursorMz, NTermFragment, itr.Current.Key, IonSeriesLabels[(int)series], length))); } if (removeMatchedPeaks) { pointMap.Remove(itr); } } } } } foreach (var pair in pointMap) { precursorScatterPlot.AddPoint(new PointPair(pair.Key - precursorMz, pair.Value / tic, 0, label)); chargeReducedScatterPlot.AddPoint(new PointPair(pair.Key - chargeReducedPrecursorMz, pair.Value / tic, 0, label)); } if ((spectraCount % 100) == 0) { Invoke(new MethodInvoker(() => { zedGraphControl.MasterPane.AxisChange(); zedGraphControl.Refresh(); })); } } Invoke(new MethodInvoker(() => { if (!lockZoomCheckBox.Checked) { zedGraphControl.ZoomOutAll(zedGraphControl.GraphPane); } zedGraphControl.MasterPane.AxisChange(); zedGraphControl.Refresh(); })); return(new List <double>()); //percentTicBySpectrumByFragmentType[1]; }
private void getFragmentationStatistics() { IList <object[]> queryRows; lock (session) { var randomIds = session.CreateQuery("SELECT psm.Id " + viewFilter.GetFilteredQueryString(DataFilter.FromPeptideSpectrumMatch)) .List <long>() .Shuffle() .Take(1000) .OrderBy(o => o); string randomIdSet = String.Join(",", randomIds.Select(o => o.ToString()).ToArray()); queryRows = session.CreateQuery("SELECT psm.Spectrum.Source.Name, psm.Spectrum, psm, DISTINCT_GROUP_CONCAT(pm.Offset || ':' || mod.MonoMassDelta), psm.Peptide.Sequence " + "FROM PeptideSpectrumMatch psm " + "LEFT JOIN psm.Modifications pm " + "LEFT JOIN pm.Modification mod " + "WHERE psm.Id IN (" + randomIdSet + ") " + "GROUP BY psm.Spectrum.id ") .List <object[]>(); } var spectrumRows = queryRows.Select(o => new SpectrumRow(o)).OrderBy(o => o.SourceName); var percentTicBySpectrumByFragmentType = new List <PointPairList>(); var percentPeakCountBySpectrumByFragmentType = new List <PointPairList>(); var meanMzErrorBySpectrumByFragmentType = new List <PointPairList>(); var percentTicListByFragmentType = new List <List <double> >(); var percentPeakCountListByFragmentType = new List <List <double> >(); var meanMzErrorListByFragmentType = new List <List <double> >(); foreach (var graphControl in graphControls) { graphControl.MasterPane.PaneList.ForEach(o => o.CurveList.ForEach(c => c.Clear())); } for (int i = 0; i < (int)IonSeries.Count; ++i) { percentTicBySpectrumByFragmentType.Add(percentTicGraphControl.MasterPane.PaneList[i + 1].CurveList[3].Points as PointPairList); percentPeakCountBySpectrumByFragmentType.Add(percentPeakCountGraphControl.MasterPane.PaneList[i + 1].CurveList[3].Points as PointPairList); meanMzErrorBySpectrumByFragmentType.Add(meanMzErrorGraphControl.MasterPane.PaneList[i + 1].CurveList[3].Points as PointPairList); percentTicListByFragmentType.Add(new List <double>()); percentPeakCountListByFragmentType.Add(new List <double>()); meanMzErrorListByFragmentType.Add(new List <double>()); } int spectraCount = 0; maxPercentTic = 10; maxPercentPeakCount = 10; maxMeanMzError = 0.1; var tolerance = fragmentTolerance; string spectrumListFilters = String.Empty; Invoke(new MethodInvoker(() => { tolerance.value = Convert.ToDouble(fragmentToleranceTextBox.Text); tolerance.units = (MZTolerance.Units)fragmentToleranceUnitsComboBox.SelectedIndex; meanMzErrorGraphControl.GraphPane.YAxis.Title.Text = "Mean m/z error (" + tolerance.units.ToString() + ")"; spectrumListFilters = spectrumFiltersTextBox.Text; setAutomaticScales(); })); var points = new PointPairList(); string currentSourceName = null; string currentSourcePath = null; msdata.MSData msd = null; lock (owner) foreach (var row in spectrumRows) { if (row.SourceName != currentSourceName) { currentSourceName = row.SourceName; currentSourcePath = IDPickerForm.LocateSpectrumSource(currentSourceName, session.Connection.GetDataSource()); if (String.IsNullOrEmpty(currentSourcePath)) { throw new FileNotFoundException("source file not found"); } msd = new pwiz.CLI.msdata.MSDataFile(currentSourcePath); //var param = session.Query<AnalysisParameter>().Where(o => o.Name == "SpectrumListFilters").Min(o => o.Value); //string spectrumListFilters = String.IsNullOrEmpty(param) ? String.Empty : param; SpectrumListFactory.wrap(msd, spectrumListFilters.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)); } string spectrumId = String.Format("{0}/{1}", row.SourceName, msdata.id.abbreviate(row.Spectrum.NativeID)); var spectrumList = msd.run.spectrumList; ++spectraCount; var pwizPeptide = new proteome.Peptide(row.ModifiedSequence, proteome.ModificationParsing.ModificationParsing_Auto, proteome.ModificationDelimiter.ModificationDelimiter_Brackets); var fragmentation = pwizPeptide.fragmentation(true, true); var pwizSpectrum = spectrumList.spectrum(spectrumList.find(row.Spectrum.NativeID), true); var pointMap = new seems.PointMap(new ZedGraph.PointPairList(pwizSpectrum.getMZArray().data, pwizSpectrum.getIntensityArray().data)); double tic = pointMap.Values.Sum(); var percentTicByFragmentType = new List <double>(Enumerable.Repeat(0.0, (int)IonSeries.Count)); var percentPeakCountByFragmentType = new List <double>(Enumerable.Repeat(0.0, (int)IonSeries.Count)); var matchCountByFragmentType = new List <int>(Enumerable.Repeat(0, (int)IonSeries.Count)); var meanMzErrorByFragmentType = new List <double>(Enumerable.Repeat(Double.NaN, (int)IonSeries.Count)); seems.PointMap.Enumerator itr; double expected; IonSeries[] ionSeries = Enum.GetValues(typeof(IonSeries)).Cast <IonSeries>().Where(o => o != IonSeries.Count).ToArray(); for (int z = 1; z <= 1; ++z) { for (int length = 1, end = pwizPeptide.sequence.Length; length <= end; ++length) { foreach (IonSeries series in ionSeries) { if ((series == IonSeries.c || series == IonSeries.x) && length == pwizPeptide.sequence.Length) { continue; } expected = fragmentMass(fragmentation, series, length, z); itr = pointMap.FindNear(expected, expected - (expected - tolerance)); if (itr != null && itr.IsValid) { percentTicByFragmentType[(int)series] += itr.Current.Value; ++percentPeakCountByFragmentType[(int)series]; ++matchCountByFragmentType[(int)series]; if (Double.IsNaN(meanMzErrorByFragmentType[(int)series])) { meanMzErrorByFragmentType[(int)series] = 0; } meanMzErrorByFragmentType[(int)series] += mzError(itr.Current.Key, expected); } } } } var rng = new Random(); for (int i = 0; i < percentTicBySpectrumByFragmentType.Count; ++i) { // convert sum to mean if (percentPeakCountByFragmentType[i] > 0) { meanMzErrorByFragmentType[i] /= matchCountByFragmentType[i]; } // convert to percentages percentTicByFragmentType[i] /= tic / 100; percentPeakCountByFragmentType[i] /= pointMap.Count / 100.0; maxPercentTic = Math.Max(maxPercentTic, percentTicByFragmentType[i]); maxPercentPeakCount = Math.Max(maxPercentPeakCount, percentPeakCountByFragmentType[i]); double jitter = (rng.NextDouble() - 0.5); percentTicBySpectrumByFragmentType[i].Add(jitter, percentTicByFragmentType[i], String.Format("{0}: {1:G4}% ({2} matches)", spectrumId, percentTicByFragmentType[i], matchCountByFragmentType[i])); percentPeakCountBySpectrumByFragmentType[i].Add(jitter, percentPeakCountByFragmentType[i], String.Format("{0}: {1:G4}% ({2} matches)", spectrumId, percentPeakCountByFragmentType[i], matchCountByFragmentType[i])); percentTicListByFragmentType[i].Add(percentTicByFragmentType[i]); percentPeakCountListByFragmentType[i].Add(percentPeakCountByFragmentType[i]); if (!Double.IsNaN(meanMzErrorByFragmentType[i])) { maxMeanMzError = Math.Max(maxMeanMzError, Math.Abs(meanMzErrorByFragmentType[i])); meanMzErrorBySpectrumByFragmentType[i].Add(jitter, meanMzErrorByFragmentType[i], String.Format("{0}: {1:G4}%", spectrumId, meanMzErrorByFragmentType[i])); meanMzErrorListByFragmentType[i].Add(meanMzErrorByFragmentType[i]); } } if ((spectraCount % 100) == 0) { setAutomaticScales(); } }// for each spectrum row Invoke(new MethodInvoker(() => { for (int i = 0; i < percentTicBySpectrumByFragmentType.Count; ++i) { if (percentTicListByFragmentType[i].Count < 5) { continue; } percentTicListByFragmentType[i].Sort(); percentPeakCountListByFragmentType[i].Sort(); addSixNumberSummary(percentTicGraphControl.MasterPane.PaneList[i + 1], percentTicListByFragmentType[i]); addSixNumberSummary(percentPeakCountGraphControl.MasterPane.PaneList[i + 1], percentPeakCountListByFragmentType[i]); if (meanMzErrorListByFragmentType[i].Count < 5) { continue; } meanMzErrorListByFragmentType[i].Sort(); addSixNumberSummary(meanMzErrorGraphControl.MasterPane.PaneList[i + 1], meanMzErrorListByFragmentType[i]); } })); }
public SpectrumSource(pwiz.CLI.msdata.MSData msd) { Metadata = msd; }
public static void AddSubsetPeakData (NHibernate.ISession session) { session.Transaction.Begin(); session.Clear(); foreach (SpectrumSource source in session.Query<SpectrumSource>()) { var subsetPeakData = new msdata.MSData(); subsetPeakData.id = subsetPeakData.run.id = source.Name; var spectrumList = new msdata.SpectrumListSimple(); subsetPeakData.run.spectrumList = spectrumList; foreach (Spectrum spectrum in source.Spectra.OrderBy(o => o.Index)) { var spectrumData = new msdata.Spectrum(); spectrumData.id = spectrum.NativeID; spectrumData.index = spectrum.Index; spectrumData.setMZIntensityArrays(new List<double>() { 100, 200, 300, 400, 500 }, new List<double>() { 10, 20, 30, 40, 50 }); spectrumList.spectra.Add(spectrumData); } session.Evict(source); var newSource = new SpectrumSource(subsetPeakData) { Id = source.Id, Group = source.Group, Name = source.Name, URL = source.URL }; session.Update(newSource); } session.Transaction.Commit(); }