Example #1
0
        public static void MatchAllFragments(Result rez)
        {
            Console.WriteLine("   ************************************************************************   ");
            Console.WriteLine("   ****************** Testing All Fragments on results ********************   ");
            rez.dbOptions.fragments = new Fragments();
            rez.dbOptions.fragments.Add(new FragmentA());
            rez.dbOptions.fragments.Add(new FragmentB());
            rez.dbOptions.fragments.Add(new FragmentC());
            rez.dbOptions.fragments.Add(new FragmentX());
            rez.dbOptions.fragments.Add(new FragmentY());
            rez.dbOptions.fragments.Add(new FragmentZ());
            rez.dbOptions.addFragmentMods = true;
            rez.dbOptions.addFragmentLoss = true;

            foreach (Precursor precursor in rez.matchedPrecursors)
            {
                PeptideSpectrumMatch psm    = precursor.OptimizedBestPsm();
                PeptideSpectrumMatch psmNew = new PeptideSpectrumMatch(psm.Query, psm.Peptide, rez.dbOptions);
                precursor.psms = new PeptideSpectrumMatches();// GraphML_List<PeptideSpectrumMatch>();
                precursor.psms.Add(psmNew);
            }
            rez.WriteInfoToCsv();
            rez.Export(0.05, "05_AllFragments", true);
            Console.WriteLine("   ****************** All fragments added to best psms ********************   ");
            Console.WriteLine("   ************************************************************************   ");
        }
Example #2
0
        public static AnnotatedSpectrum TestNeo4j(IConSol console)
        {
            DBOptions dbOptions     = GetDBOptions(false, false, console);
            Samples   ProjectRatios = new Samples(@"C:\_IRIC\DATA\NB\ProjectTest_MonoAce_Spiked_19Oct.csv", 0, dbOptions);//Group 2 (all)

            Result rez = Propheus.Start(dbOptions, ProjectRatios, false, false, false);

            Database.Neo4j.ResultsToNeo4j.Export(rez);
            PeptideSpectrumMatch bestPsm = null;

            foreach (Query query in rez.queries)
            {
                foreach (PeptideSpectrumMatch psm in query.psms)
                {
                    if (psm.Peptide.Sequence.CompareTo("GK(acetylation of K)GGK(propionylation of K)GLGK(propionylation of K)GGAK(propionylation of K)R") == 0 &&
                        (bestPsm == null || bestPsm.ProbabilityScore() < query.psms[0].ProbabilityScore()))
                    {
                        bestPsm = psm;
                    }
                }
            }
            AnnotatedSpectrum aSpec = new AnnotatedSpectrum(ProjectRatios[0], bestPsm.Query.spectrum, bestPsm.Peptide);

            return(aSpec);
        }
        void setData(object sender, DoWorkEventArgs e)
        {
            try
            {
                Map <int, List <PSMRow> > precursorMassErrorsByCharge = new Map <int, List <PSMRow> >();;
                IDictionary <int, int>    spectralCountByChargeState;
                lock (session)
                {
                    var randomIds = session.CreateQuery("SELECT psm.Id " + viewFilter.GetFilteredQueryString(DataFilter.FromPeptideSpectrumMatch))
                                    .List <long>()
                                    .Shuffle()
                                    .Take(50000)
                                    .OrderBy(o => o);
                    string randomIdSet = String.Join(",", randomIds.Select(o => o.ToString()).ToArray());
                    var    query       = session.CreateSQLQuery("SELECT psm.ObservedNeutralMass, psm.MonoisotopicMassError, psm.MolecularWeightError, s.ScanTimeInSeconds, psm.Charge " +
                                                                "FROM PeptideSpectrumMatch psm " +
                                                                "JOIN Spectrum s ON psm.Spectrum=s.Id " +
                                                                "WHERE psm.Id IN (" + randomIdSet + ") " +
                                                                "GROUP BY psm.Id");
                    query.List <object[]>().ForEach(o =>
                                                    precursorMassErrorsByCharge[Convert.ToInt32(o[4])].Add(new PSMRow
                    {
                        ObservedNeutralMass = Convert.ToDouble(o[0]),
                        MassError           = PeptideSpectrumMatch.GetSmallerMassError(Convert.ToDouble(o[1]), Convert.ToDouble(o[2])),
                        ScanTime            = Convert.ToDouble(o[3]) / 60, // convert to minutes
                        Charge = Convert.ToInt32(o[4])
                    }));

                    var query2 = session.CreateQuery("SELECT psm.Charge, COUNT(DISTINCT psm.Spectrum.id) " +
                                                     dataFilter.GetFilteredQueryString(DataFilter.FromPeptideSpectrumMatch) +
                                                     "GROUP BY psm.Charge");
                    spectralCountByChargeState = query2.List <object[]>().ToDictionary(o => Convert.ToInt32(o[0]), o => Convert.ToInt32(o[1]));
                }

                Map <int, List <List <PSMRow> > > clusteredPrecursorMassErrorsByCharge = clusterMassErrors(precursorMassErrorsByCharge);

                // convert to PPM if the user requested it
                if (!ppmMassErrorMenuItem.Text.Contains("PPM"))
                {
                    clusteredPrecursorMassErrorsByCharge.Values.ForEach(o3 => o3.ForEach(o2 => o2.ForEach(o => o.MassError = o.MassError / o.ObservedNeutralMass * 1e6)));
                }

                {
                    var csr = new ColorSymbolRotator();
                    precursorMassErrorForm.ZedGraphControl.GraphPane.CurveList.Clear();
                    foreach (var kvp in clusteredPrecursorMassErrorsByCharge)
                    {
                        bool firstCluster = true;
                        var  color        = csr.NextColor;
                        foreach (var precursorMassErrorCluster in kvp.Value)
                        {
                            var precursorMassErrors      = precursorMassErrorCluster;
                            var precursorMassErrorValues = precursorMassErrors.Select(o => o.MassError).ToArray();
                            var densityCurve             = new LineItem(firstCluster ? kvp.Key.ToString() : "", getDensityCurve(precursorMassErrorValues, 0), color, SymbolType.None);
                            var grassPlot = new LineItem("", precursorMassErrorValues, Enumerable.Repeat(0.0, precursorMassErrorValues.Length).ToArray(), color, SymbolType.VDash)
                            {
                                Tag = kvp.Key
                            };
                            densityCurve.Line.IsAntiAlias = true;
                            precursorMassErrorForm.ZedGraphControl.GraphPane.CurveList.Add(densityCurve);
                            precursorMassErrorForm.ZedGraphControl.GraphPane.CurveList.Add(grassPlot);
                            firstCluster = false;
                        }
                    }
                }

                {
                    var csr = new ColorSymbolRotator();
                    scanTimeDistributionForm.ZedGraphControl.GraphPane.CurveList.Clear();
                    foreach (var kvp in precursorMassErrorsByCharge)
                    {
                        var precursorMassErrors = kvp.Value;
                        var scanTimeValues      = precursorMassErrors.Select(o => o.ScanTime).ToList();
                        var color = csr.NextColor;
                        if (scanTimeValues.Max() > 0)
                        {
                            var densityCurve = new LineItem(kvp.Key.ToString(), getDensityCurve(scanTimeValues, 0), color, SymbolType.None);
                            var grassPlot    = new LineItem("", scanTimeValues.ToArray(), Enumerable.Repeat(0.0, scanTimeValues.Count).ToArray(), color, SymbolType.VDash);
                            densityCurve.Line.IsAntiAlias = true;
                            scanTimeDistributionForm.ZedGraphControl.GraphPane.CurveList.Add(densityCurve);
                            scanTimeDistributionForm.ZedGraphControl.GraphPane.CurveList.Add(grassPlot);
                        }
                    }

                    if (scanTimeDistributionForm.ZedGraphControl.GraphPane.CurveList.IsNullOrEmpty())
                    {
                        scanTimeDistributionForm.ZedGraphControl.GraphPane.GraphObjList.Add(
                            new TextObj("No scan time information present.\r\nUse File/Embed to import scan times from raw data.",
                                        0.5, 0.5, CoordType.ChartFraction, AlignH.Center, AlignV.Center)
                        {
                            FontSpec = new FontSpec("monospace", 18.0f, Color.Black, true, false, false)
                        });
                    }
                }

                var colorRotator = new ColorSymbolRotator();
                var colors       = new List <Color>();
                foreach (var kvp in spectralCountByChargeState)
                {
                    colors.Add(colorRotator.NextColor);
                }

                chargeStatesForm.ZedGraphControl.GraphPane.CurveList.Clear();
                chargeStatesForm.ZedGraphControl.GraphPane.BarSettings.MinClusterGap = 8;
                var barItem = chargeStatesForm.ZedGraphControl.GraphPane.AddBar("", spectralCountByChargeState.Keys.Select(o => (double)o).ToArray(), spectralCountByChargeState.Values.Select(o => (double)o).ToArray(), Color.White) as BarItem;
                for (int i = 0; i < barItem.Points.Count; ++i)
                {
                    barItem.Points[i].ColorValue = (double)i + 1;
                }
                barItem.Bar.Fill = new Fill(colors.ToArray())
                {
                    Type     = FillType.GradientByColorValue,
                    RangeMin = 1,
                    RangeMax = colors.Count
                };
                chargeStatesForm.ZedGraphControl.GraphPane.XAxis.Scale.TextLabels = spectralCountByChargeState.Keys.Select(o => o.ToString()).ToArray();
            }
            catch (Exception ex)
            {
                e.Result = ex;
            }
        }
Example #4
0
        public static void CreateTestData (NHibernate.ISession session, IList<SpectrumTuple> testPsmSummary)
        {
            var dbGroups = new Map<string, SpectrumSourceGroup>();
            foreach (var ssg in session.Query<SpectrumSourceGroup>())
                dbGroups[ssg.Name] = ssg;

            var dbSources = new Map<long, SpectrumSource>();
            foreach (var ss in session.Query<SpectrumSource>())
                dbSources[ss.Id.Value] = ss;

            var dbAnalyses = new Map<long, Analysis>();
            foreach (var a in session.Query<Analysis>())
                dbAnalyses[a.Id.Value] = a;

            var dbPeptides = new Map<string, Peptide>();
            foreach (var pep in session.Query<Peptide>())
                dbPeptides[pep.Sequence] = pep;

            var bulkInserter = new BulkInserter(session.Connection);

            long lastPsmId = session.CreateQuery("SELECT MAX(Id) FROM PeptideSpectrumMatch").UniqueResult<long?>().GetValueOrDefault();
            long lastModId = session.CreateQuery("SELECT MAX(Id) FROM Modification").UniqueResult<long?>().GetValueOrDefault();
            long lastPmId = session.CreateQuery("SELECT MAX(Id) FROM PeptideModification").UniqueResult<long?>().GetValueOrDefault();
            long lastGroupId = session.CreateQuery("SELECT MAX(Id) FROM SpectrumSourceGroup").UniqueResult<long?>().GetValueOrDefault();
            long lastSourceId = session.CreateQuery("SELECT MAX(Id) FROM SpectrumSource").UniqueResult<long?>().GetValueOrDefault();
            long lastSglId = session.CreateQuery("SELECT MAX(Id) FROM SpectrumSourceGroupLink").UniqueResult<long?>().GetValueOrDefault();

            foreach (SpectrumTuple row in testPsmSummary)
            {
                string groupName = row.Group;
                string sourceName = "Source " + row.Source;
                string analysisId = "Engine " + row.Analysis;
                string peptideTuples = row.PeptideTuples;

                SpectrumSourceGroup group = dbGroups[groupName];
                if (String.IsNullOrEmpty(group.Name))
                {
                    group.Id = ++lastGroupId;
                    group.Name = groupName;
                    bulkInserter.Add(group);
                }

                SpectrumSource source = dbSources[row.Source];
                if (String.IsNullOrEmpty(source.Name))
                {
                    source.Id = ++lastSourceId;
                    source.Name = sourceName;
                    source.Group = group;
                    source.Spectra = new List<Spectrum>();
                    bulkInserter.Add(source);

                    // add a source group link for the source's immediate group
                    bulkInserter.Add(new SpectrumSourceGroupLink() { Id = ++lastSglId, Group = group, Source = source });

                    #region add source group links for all of the immediate group's parent groups

                    if (groupName != "/")
                    {
                        string parentGroupName = groupName.Substring(0, groupName.LastIndexOf("/"));
                        while (true)
                        {
                            if (String.IsNullOrEmpty(parentGroupName))
                                parentGroupName = "/";

                            // add the parent group if it doesn't exist yet
                            SpectrumSourceGroup parentGroup = session.UniqueResult<SpectrumSourceGroup>(o => o.Name == parentGroupName);
                            if (parentGroup == null)
                            {
                                parentGroup = new SpectrumSourceGroup() { Id = ++lastGroupId, Name = parentGroupName };
                                bulkInserter.Add(parentGroup);
                            }

                            bulkInserter.Add(new SpectrumSourceGroupLink() { Id = ++lastSglId, Group = parentGroup, Source = source });

                            if (parentGroupName == "/")
                                break;
                            parentGroupName = parentGroupName.Substring(0, parentGroupName.LastIndexOf("/"));
                        }
                    }

                    #endregion
                }

                Spectrum spectrum = source.Spectra.SingleOrDefault(o => o.Source.Id == source.Id &&
                                                                        o.Index == row.Spectrum - 1);
                if (spectrum == null)
                {
                    spectrum = new Spectrum()
                                   {
                                       Id = source.Id * 10000 + row.Spectrum,
                                       Index = row.Spectrum - 1,
                                       NativeID = "scan=" + row.Spectrum,
                                       Source = source,
                                       PrecursorMZ = 42
                                   };
                    source.Spectra.Add(spectrum);
                    bulkInserter.Add(spectrum);
                }

                Analysis analysis = dbAnalyses[row.Analysis];
                if (String.IsNullOrEmpty(analysis.Name))
                {
                    analysis.Id = dbAnalyses.Max(o => o.Value.Id).GetValueOrDefault() + 1;
                    analysis.Name = analysisId + " 1.0";
                    analysis.Software = new AnalysisSoftware() {Name = analysisId, Version = "1.0"};
                    analysis.StartTime = DateTime.Today.AddHours(row.Analysis);
                    analysis.Type = AnalysisType.DatabaseSearch;

                    analysis.Parameters = new SortedSet<AnalysisParameter>()
                    {
                        new AnalysisParameter()
                        {
                            Id = analysis.Id * 10000,
                            Analysis = analysis,
                            Name = "Parameter 1",
                            Value = "Value 1"
                        }
                    };

                    bulkInserter.Add(analysis);
                }

                // make sure peptides are sorted by their score divider (which will determine rank)
                var peptideList = new SortedList<int, List<PeptideTuple>>();
                foreach (string tuple in peptideTuples.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
                {
                    var peptideTuple = new PeptideTuple()
                                           {
                                               Sequence = tuple.Split('@', '/')[0],
                                               Charge = Convert.ToInt32(tuple.Split('@', '/')[1]),
                                               ScoreDivider = Convert.ToInt32(tuple.Split('@', '/')[2])
                                           };
                    if (!peptideList.ContainsKey(peptideTuple.ScoreDivider))
                        peptideList[peptideTuple.ScoreDivider] = new List<PeptideTuple>();
                    peptideList[peptideTuple.ScoreDivider].Add(peptideTuple);
                }

                int rank = 1;
                int lastDivider = 1;
                foreach (var peptideTupleList in peptideList.Values)
                    foreach (var peptideTuple in peptideTupleList)
                    {
                        using (PwizPeptide pwizPeptide = new PwizPeptide(peptideTuple.Sequence, ModParsing.ModificationParsing_Auto, ModDelimiter.ModificationDelimiter_Brackets))
                        {
                            Peptide peptide = dbPeptides[pwizPeptide.sequence];
                            if (String.IsNullOrEmpty(peptide.Sequence))
                            {
                                peptide = new TestPeptide(pwizPeptide.sequence);
                                peptide.Id = dbPeptides.Max(o => o.Value.Id).GetValueOrDefault() + 1;
                                peptide.MonoisotopicMass = pwizPeptide.monoisotopicMass(false);
                                peptide.MolecularWeight = pwizPeptide.molecularWeight(false);
                                dbPeptides[pwizPeptide.sequence] = peptide;
                                bulkInserter.Add(peptide);
                                createTestPeptideInstances(session, bulkInserter, peptide);
                            }

                            double neutralPrecursorMass = (spectrum.PrecursorMZ*peptideTuple.Charge) - (peptideTuple.Charge*Proton.Mass);

                            var psm = new PeptideSpectrumMatch()
                                          {
                                              Id = ++lastPsmId,
                                              Peptide = peptide,
                                              Spectrum = spectrum,
                                              Analysis = analysis,
                                              ObservedNeutralMass = neutralPrecursorMass,
                                              MonoisotopicMassError = neutralPrecursorMass - pwizPeptide.monoisotopicMass(),
                                              MolecularWeightError = neutralPrecursorMass - pwizPeptide.molecularWeight(),
                                              Charge = peptideTuple.Charge,
                                              Rank = (peptideTuple.ScoreDivider == lastDivider ? rank : ++rank),
                                              QValue = (rank == 1 ? row.QValue : PeptideSpectrumMatch.DefaultQValue),
                                          };

                            if (row.Score != null)
                                psm.Scores = new Dictionary<string, double>()
                                                 {
                                                     {"score1", (double) row.Score/peptideTuple.ScoreDivider},
                                                     {"score2", 1/((double) row.Score/peptideTuple.ScoreDivider)}
                                                 };

                            bulkInserter.Add(psm);
                            lastDivider = peptideTuple.ScoreDivider;

                            // add PeptideModifications and Modifications
                            foreach (KeyValuePair<int, ModList> itr in pwizPeptide.modifications())
                            {
                                foreach (PwizMod pwizMod in itr.Value)
                                {
                                    Modification mod = session.UniqueResult<Modification>(o => o.Formula == pwizMod.formula());
                                    if (mod == null)
                                    {
                                        mod = new Modification()
                                                  {
                                                      Id = ++lastModId,
                                                      Formula = pwizMod.formula(),
                                                      MonoMassDelta = pwizMod.monoisotopicDeltaMass(),
                                                      AvgMassDelta = pwizMod.averageDeltaMass(),
                                                      Name = pwizMod.formula()
                                                  };
                                        bulkInserter.Add(mod);
                                    }

                                    bulkInserter.Add(new PeptideModification()
                                                         {
                                                             Id = ++lastPmId,
                                                             PeptideSpectrumMatch = psm,
                                                             Modification = mod,
                                                             Offset = itr.Key == ModMap.NTerminus() ? int.MinValue
                                                                    : itr.Key == ModMap.CTerminus() ? int.MaxValue
                                                                    : itr.Key
                                                         });
                                }
                            }
                        }
                    }
            }
            bulkInserter.Execute();
            bulkInserter.Reset("");
        }
Example #5
0
        public void Add (PeptideSpectrumMatch psm)
        {
            insertRow(Table.PeptideSpectrumMatch, new object[]
                                                        {
                                                            psm.Id, psm.Peptide.Id, psm.Spectrum.Id, psm.Analysis.Id,
                                                            psm.ObservedNeutralMass,
                                                            psm.MonoisotopicMassError, psm.MolecularWeightError,
                                                            psm.Rank, psm.QValue, psm.Charge
                                                        });

            if (psm.Scores != null)
                foreach (var score in psm.Scores)
                {
                    long scoreId;
                    if (!scoreIdByName.TryGetValue(score.Key, out scoreId))
                    {
                        if (scoreIdByName.Count > 0)
                            scoreIdByName[score.Key] = scoreId = scoreIdByName.Max(o => o.Value) + 1;
                        else
                            scoreIdByName[score.Key] = scoreId = 1;

                        insertRow(Table.PeptideSpectrumMatchScoreName, new object[] { scoreId, score.Key });
                    }
                    insertRow(Table.PeptideSpectrumMatchScore, new object[] {psm.Id, scoreId, score.Value});
                }
        }
        private void SetSpectra(PhosphoRSInput input)
        {
            _spectra = input.Spectra;

            _spectrumMap    = new Dictionary <int, Spectrum>();
            _peptideMap     = new Dictionary <int, Peptide>();
            _phosphoSiteMap = new Dictionary <int, List <int> >();

            _items = new List <Tuple <PeptideSpectrumMatch, List <Tuple <int, List <int> > > > >();

            var count = 0;

            foreach (var spectrum in input.Spectra)
            {
                var rank = 0;
                foreach (var peptide in spectrum.IdentifiedPhosphorPeptides)
                {
                    if (_options.MaxHitRank > 0 && rank >= _options.MaxHitRank)
                    {
                        break;
                    }
                    rank += 1;

                    var isoformMap = new List <Tuple <int, List <int> > >();

                    var aas = AminoAcidSequence.Create(
                        peptide.ID,
                        peptide.Sequence,
                        _aminoAcidModifications,
                        peptide.Modification
                        );

                    var psm = new PeptideSpectrumMatch(
                        peptide.ID,
                        spectrum.ActivationType,
                        spectrum.PrecursorCharge,
                        1,
                        spectrum.Peaks.ToArray(),
                        aas
                        );

                    var phosphoCount = peptide.Modification
                                       .Skip(2).SkipLast(2)
                                       .Where(c => c == _options.PhosphorylationSymbol)
                                       .Count();

                    if (phosphoCount == 0)
                    {
                        continue;
                    }

                    var targetSites = peptide.Sequence
                                      .Select((c, i) => _phosphoTargetAminoAcids.Contains(c) ? i : -1)
                                      .Where(i => i >= 0)
                                      .ToList();

                    targetSites.Combinations(phosphoCount).Take(_options.MaxIsoformCount).ForEach(sites =>
                    {
                        var list = sites.ToList();
                        isoformMap.Add(
                            new Tuple <int, List <int> >(
                                count,
                                list
                                )
                            );

                        _spectrumMap.Add(count, spectrum);
                        _peptideMap.Add(count, peptide);
                        _phosphoSiteMap.Add(count, list);
                        count += 1;
                    });

                    _items.Add(Tuple.Create <PeptideSpectrumMatch, List <Tuple <int, List <int> > > >(
                                   psm,
                                   isoformMap
                                   ));
                }
            }
        }