Exemplo n.º 1
0
        private void DoCreateAnalyses(char[] excludeAas, int minTracerCount)
        {
            var  peptides          = new List <DbPeptide>();
            bool includeMissingMs2 = cbxIncludeMissingMS2.Checked;

            _statusMessage = "Listing peptides";
            using (var session = Workspace.OpenWriteSession())
            {
                session.CreateCriteria(typeof(DbPeptide)).List(peptides);
                for (int i = 0; i < peptides.Count(); i++)
                {
                    _statusMessage = "Peptide " + (i + 1) + "/" + peptides.Count;
                    _progress      = 100 * i / peptides.Count;
                    if (_cancelled)
                    {
                        break;
                    }
                    var peptide = peptides[i];
                    if (peptide.Sequence.IndexOfAny(excludeAas) >= 0)
                    {
                        continue;
                    }
                    if (Workspace.GetMaxTracerCount(peptide.Sequence) < minTracerCount)
                    {
                        continue;
                    }
                    var dbPeptideAnalysis = (DbPeptideAnalysis)session.CreateCriteria(typeof(DbPeptideAnalysis))
                                            .Add(Restrictions.Eq("Peptide", peptide)).UniqueResult();
                    if (dbPeptideAnalysis == null)
                    {
                        dbPeptideAnalysis = Peptide.CreateDbPeptideAnalysis(session, peptide);
                    }
                    var idList = new List <long>();
                    if (dbPeptideAnalysis.Id.HasValue)
                    {
                        var dataFileIdQuery = session.CreateQuery("SELECT A.MsDataFile.Id FROM " + typeof(DbPeptideFileAnalysis) +
                                                                  " A WHERE A.PeptideAnalysis.Id = :peptideAnalysisId")
                                              .SetParameter("peptideAnalysisId", dbPeptideAnalysis.Id);
                        dataFileIdQuery.List(idList);
                    }
                    var existingDataFileIds  = new HashSet <long>(idList);
                    var psmTimesByDataFileId = peptide.PsmTimesByDataFileId(session);
                    if (psmTimesByDataFileId.Count == 0)
                    {
                        continue;
                    }
                    session.BeginTransaction();
                    bool newAnalysis;
                    if (dbPeptideAnalysis.Id.HasValue)
                    {
                        newAnalysis = false;
                    }
                    else
                    {
                        session.Save(dbPeptideAnalysis);
                        newAnalysis = true;
                    }
                    var sqlStatementBuilder = new SqlStatementBuilder(session.GetSessionImplementation().Factory.Dialect);
                    var insertStatements    = new List <string>();
                    IList <MsDataFile> dataFiles;
                    if (includeMissingMs2)
                    {
                        dataFiles = Workspace.MsDataFiles
                                    .Where(msDataFile => !existingDataFileIds.Contains(msDataFile.Id))
                                    .ToArray();
                    }
                    else
                    {
                        dataFiles = new List <MsDataFile>();
                        foreach (var grouping in psmTimesByDataFileId)
                        {
                            MsDataFile dataFile;
                            if (existingDataFileIds.Contains(grouping.Key) || !Workspace.MsDataFiles.TryGetValue(grouping.Key, out dataFile))
                            {
                                continue;
                            }
                            dataFiles.Add(dataFile);
                        }
                    }
                    foreach (var msDataFile in dataFiles)
                    {
                        var dbPeptideFileAnalysis = PeptideFileAnalysis.CreatePeptideFileAnalysis(session, msDataFile, dbPeptideAnalysis, psmTimesByDataFileId);
                        insertStatements.Add(sqlStatementBuilder.GetInsertStatement("DbPeptideFileAnalysis",
                                                                                    new Dictionary <string, object>
                        {
                            { "ChromatogramEndTime", dbPeptideFileAnalysis.ChromatogramEndTime },
                            { "ChromatogramStartTime", dbPeptideFileAnalysis.ChromatogramStartTime },
                            { "MsDataFile", msDataFile.Id },
                            { "PeptideAnalysis", dbPeptideAnalysis.Id },
                            { "AutoFindPeak", 1 },
                            { "Version", 1 },
                            { "PeakCount", 0 },
                            { "PsmCount", dbPeptideFileAnalysis.PsmCount },
                            { "ValidationStatus", 0 },
                        }
                                                                                    ));
                        dbPeptideAnalysis.FileAnalysisCount++;
                        existingDataFileIds.Add(msDataFile.Id);
                    }
                    sqlStatementBuilder.ExecuteStatements(session, insertStatements);
                    session.Update(dbPeptideAnalysis);
                    if (!newAnalysis)
                    {
                        session.Save(new DbChangeLog(Workspace, dbPeptideAnalysis));
                    }
                    else
                    {
                        var dbWorkspace = Workspace.LoadDbWorkspace(session);
                        session.Update(dbWorkspace);
                    }
                    session.Transaction.Commit();
                }
            }
        }
Exemplo n.º 2
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();
            }
        }