private void ExecuteBackground() { try { ProteinMatchTypes matchTypesRemaining = Settings.MatchTypes; using (var proteomeDb = ProteomeDb.OpenProteomeDb(Settings.ProteomeDbPath.FilePath, CancellationToken)) using (var session = proteomeDb.OpenStatelessSession(false)) { if (matchTypesRemaining.Contains(ProteinMatchType.sequence) && IsAminoAcidSequence(Settings.SearchText)) { AddProteinMatches(session, () => proteomeDb.GetDigestion() .GetProteinIdsThatMightHaveSequence(session, new[] { Settings.SearchText })); } matchTypesRemaining = matchTypesRemaining.Except(ProteinMatchType.sequence); if (DoMatching) { string pattern = @"%" + Settings.SearchText + @"%"; if (Settings.SearchText.Length < MIN_FREE_SEARCH_LENGTH) { // That could cast a pretty wide net - only match to beginning of keywords, and not to description or species pattern = Settings.SearchText + @"%"; matchTypesRemaining = matchTypesRemaining.Except(ProteinMatchType.description, ProteinMatchType.species); } var exprLike = new List <string>(); foreach (ProteinMatchType matchType in matchTypesRemaining) { exprLike.Add(String.Format(@"pn.{0} LIKE :expr", ProteinMatchTypeDbFieldName(matchType))); } String hql = @"SELECT distinct pn.Protein FROM " + typeof(DbProteinName) + @" pn " // ReSharper disable LocalizableElement + String.Format("\nWHERE {0}", String.Join(" OR ", exprLike)) + "\nORDER BY pn.IsPrimary DESC, pn.Name"; // ReSharper restore LocalizableElement IQuery query = session.CreateQuery(hql).SetParameter(@"expr", pattern); query.SetMaxResults(MaxResults); AddProteinMatches(session, () => { return(query.List <DbProtein>() .Where(dbProtein => null != dbProtein) .Select(dbProtein => dbProtein.Id.Value)); }); } } } catch (Exception exception) { if (CancellationToken.IsCancellationRequested) { return; } Trace.TraceError(@"Unhandled exception: {0}", exception); } }
/// <summary> /// Executes the query (which must be a query on the DbProtein table), and adds /// all of the rows in that query to the ProteinMatches. /// </summary> private void AddProteinMatches(IStatelessSession session, Func <IEnumerable <long> > proteinIdQuery) { Dictionary <long, ProteinMatch> newMatches = new Dictionary <long, ProteinMatch>(); lock (this) { if (_matches != null) { foreach (var entry in _matches) { newMatches.Add(entry.Key, entry.Value); } } } var newProteinIds = new List <long>(); CancellationToken.ThrowIfCancellationRequested(); foreach (long id in proteinIdQuery()) { CancellationToken.ThrowIfCancellationRequested(); if (newMatches.ContainsKey(id)) { continue; } newProteinIds.Add(id); } if (newProteinIds.Count == 0) { return; } using (var proteomeDb = ProteomeDb.OpenProteomeDb(Settings.ProteomeDbPath.FilePath, CancellationToken)) { // We fetched the protein ids. Now fetch all the protein rows themselves. var proteins = proteomeDb.GetProteinsWithIds(session, newProteinIds); foreach (var protein in proteins) { var proteinMatch = new ProteinMatch(Settings, protein); newMatches.Add(protein.Id, proteinMatch); } SetProteinMatches(newMatches); } }