示例#1
0
        private void AddSearchResults(IList <SearchResult> searchResults, Dictionary <string, DbPeptide> peptides, Dictionary <string, DbMsDataFile> dataFiles)
        {
            String baseMessage = _message;

            if (searchResults == null)
            {
                return;
            }
            var dbPeptideSearchResults = GetSearchResults();
            var modifiedPeptides       = new HashSet <DbPeptide>();
            var searchResultsToInsert  = new Dictionary <SearchResultKey, DbPeptideSpectrumMatch>();
            var searchResultsToUpdate  = new Dictionary <SearchResultKey, DbPeptideSpectrumMatch>();

            using (var session = Workspace.OpenWriteSession())
            {
                var statementBuilder = new SqlStatementBuilder(session.GetSessionImplementation().Factory.Dialect);
                var dbWorkspace      = Workspace.LoadDbWorkspace(session);
                session.BeginTransaction();
                var newPeptides = new Dictionary <String, Dictionary <String, object> >();

                foreach (var searchResult in searchResults)
                {
                    var trimmedSequence = Peptide.TrimSequence(searchResult.Sequence);
                    if (peptides.ContainsKey(trimmedSequence) || newPeptides.ContainsKey(trimmedSequence))
                    {
                        continue;
                    }
                    newPeptides.Add(trimmedSequence, new Dictionary <string, object>
                    {
                        { "Sequence", trimmedSequence },
                        { "FullSequence", searchResult.Sequence },
                        { "Version", 1 },
                        { "ValidationStatus", 0 }
                    });
                }
                if (newPeptides.Count > 0)
                {
                    long maxPeptideId     = (long?)session.CreateQuery("SELECT Max(T.Id) FROM DbPeptide T").UniqueResult() ?? 0;
                    var  insertStatements = new List <String>();
                    foreach (var dict in newPeptides.Values)
                    {
                        insertStatements.Add(statementBuilder.GetInsertStatement("DbPeptide", dict));
                    }
                    statementBuilder.ExecuteStatements(session, insertStatements);
                    var criteria = session.CreateCriteria(typeof(DbPeptide))
                                   .Add(Restrictions.Gt("Id", maxPeptideId));
                    foreach (DbPeptide dbPeptide in criteria.List())
                    {
                        peptides.Add(dbPeptide.Sequence, dbPeptide);
                    }
                }

                foreach (var searchResult in searchResults)
                {
                    var       trimmedSequence = Peptide.TrimSequence(searchResult.Sequence);
                    DbPeptide dbPeptide;
                    if (!peptides.TryGetValue(trimmedSequence, out dbPeptide))
                    {
                        // should not happen
                        continue;
                    }
                    if (searchResult.Filename == null)
                    {
                        continue;
                    }
                    DbMsDataFile dbMsDataFile;
                    if (!dataFiles.TryGetValue(searchResult.Filename, out dbMsDataFile))
                    {
                        dbMsDataFile = new DbMsDataFile
                        {
                            Name  = searchResult.Filename,
                            Label = searchResult.Filename,
                        };
                        session.Save(dbMsDataFile);
                        dataFiles.Add(dbMsDataFile.Name, dbMsDataFile);
                    }

                    var key = new SearchResultKey(dbMsDataFile, dbPeptide, searchResult.RetentionTime);
                    DbPeptideSpectrumMatch dbPeptideSpectrumMatch;
                    if (dbPeptideSearchResults.TryGetValue(key, out dbPeptideSpectrumMatch))
                    {
                        bool changed = false;
                        if (dbPeptideSpectrumMatch.ModifiedSequence == null && searchResult.ModifiedSequence != null)
                        {
                            dbPeptideSpectrumMatch.ModifiedSequence = searchResult.ModifiedSequence;
                            changed = true;
                        }
                        if (!dbPeptideSpectrumMatch.PrecursorMz.HasValue && searchResult.PrecursorMz.HasValue)
                        {
                            dbPeptideSpectrumMatch.PrecursorMz     = searchResult.PrecursorMz;
                            dbPeptideSpectrumMatch.PrecursorCharge = searchResult.PrecursorCharge;
                            changed = true;
                        }
                        if (!changed)
                        {
                            continue;
                        }
                        searchResultsToUpdate[key] = dbPeptideSpectrumMatch;
                    }
                    else
                    {
                        if (!searchResultsToInsert.TryGetValue(key, out dbPeptideSpectrumMatch))
                        {
                            dbPeptideSpectrumMatch = new DbPeptideSpectrumMatch
                            {
                                MsDataFile       = dbMsDataFile,
                                Peptide          = dbPeptide,
                                RetentionTime    = searchResult.RetentionTime,
                                PrecursorCharge  = searchResult.PrecursorCharge,
                                PrecursorMz      = searchResult.PrecursorMz,
                                ModifiedSequence = searchResult.ModifiedSequence,
                            };
                            searchResultsToInsert.Add(key, dbPeptideSpectrumMatch);
                            if (!newPeptides.ContainsKey(trimmedSequence))
                            {
                                modifiedPeptides.Add(dbPeptide);
                            }
                        }
                    }
                }
                var statements = new List <string>();
                foreach (var dbPeptideSearchResult in searchResultsToInsert.Values)
                {
                    statements.Add(statementBuilder.GetInsertStatement("DbPeptideSpectrumMatch",
                                                                       new Dictionary <string, object> {
                        { "MsDataFile", dbPeptideSearchResult.MsDataFile.Id },
                        { "Peptide", dbPeptideSearchResult.Peptide.Id },
                        { "RetentionTime", dbPeptideSearchResult.RetentionTime },
                        { "ModifiedSequence", dbPeptideSearchResult.ModifiedSequence },
                        { "PrecursorMz", dbPeptideSearchResult.PrecursorMz },
                        { "PrecursorCharge", dbPeptideSearchResult.PrecursorCharge },
                        { "Version", 1 },
                    }));
                }
                foreach (var dbPeptideSearchResult in searchResultsToUpdate.Values)
                {
                    statements.Add(statementBuilder.GetUpdateStatement("DbPeptideSearchResult",
                                                                       new Dictionary <string, object> {
                        { "RetentionTime", dbPeptideSearchResult.RetentionTime },
                        { "ModifiedSequence", dbPeptideSearchResult.ModifiedSequence },
                        { "PrecursorMz", dbPeptideSearchResult.PrecursorMz },
                        { "PrecursorCharge", dbPeptideSearchResult.PrecursorCharge },
                        { "Version", dbPeptideSearchResult.Version + 1 },
                    },
                                                                       new Dictionary <string, object> {
                        { "Id", dbPeptideSearchResult.Id.GetValueOrDefault() }
                    }
                                                                       ));
                }
                statementBuilder.ExecuteStatements(session, statements);
                UpdateProgress(baseMessage + "(Committing transaction)", 99);
                session.Transaction.Commit();
            }
        }