public PeptideSubIndex(IEnumerable <IndexItem> items) { _entries = new Dictionary <string, ImmutableList <PeptideEntry> >(); var singletonIndexes = new Dictionary <int, ImmutableList <int> >(); var indexes = new Dictionary <ImmutableList <int>, ImmutableList <int> >(); var modIndexComparer = Comparer <IList <int> > .Create(CompareModificationIndexes); foreach (var group in items.Where(item => item.LibraryKey is PeptideLibraryKey) .ToLookup(item => ((PeptideLibraryKey)item.LibraryKey).UnmodifiedSequence)) { _entries.Add(group.Key, ImmutableList.ValueOf(group .Select(item => PeptideEntry.NewInstance(singletonIndexes, indexes, item)) .OrderBy(entry => entry.ModificationIndexes, modIndexComparer))); } Count = _entries.Values.Sum(list => list.Count); }
/// <summary> /// Returns the set of entries with modifications on the same amino acids. /// </summary> private IEnumerable <PeptideEntry> ModificationIndexMatches(PeptideLibraryKey peptideLibraryKey, [CanBeNull] out IList <KeyValuePair <int, string> > modifications) { modifications = null; ImmutableList <PeptideEntry> entries; if (!_entries.TryGetValue(peptideLibraryKey.UnmodifiedSequence, out entries)) { return(ImmutableList <PeptideEntry> .EMPTY); } modifications = peptideLibraryKey.GetModifications(); var peptideEntry = new PeptideEntry(new IndexItem(peptideLibraryKey, -1), modifications); var range = CollectionUtil.BinarySearch(entries, item => item.CompareModIndexes(peptideEntry)); return(Enumerable.Range(range.Start, range.Length) .Select(index => entries[index])); }
/// <summary> /// Constructs a new PeptideEntry, and reuses ImmutableList values from the /// passed in dictionaries to prevent redundant object creation. /// </summary> public static PeptideEntry NewInstance(IDictionary <int, ImmutableList <int> > singletonIndexCache, IDictionary <ImmutableList <int>, ImmutableList <int> > indexCache, IndexItem indexItem) { var peptideEntry = new PeptideEntry(indexItem, ((PeptideLibraryKey)indexItem.LibraryKey).GetModifications()); if (peptideEntry.ModificationIndexes.Count == 0) { return(peptideEntry); } ImmutableList <int> newIndexes; if (peptideEntry.ModificationIndexes.Count == 1) { if (singletonIndexCache.TryGetValue(peptideEntry.ModificationIndexes[0], out newIndexes)) { peptideEntry.ModificationIndexes = newIndexes; } else { singletonIndexCache.Add(peptideEntry.ModificationIndexes[0], peptideEntry.ModificationIndexes); } } else { if (indexCache.TryGetValue(peptideEntry.ModificationIndexes, out newIndexes)) { peptideEntry.ModificationIndexes = newIndexes; } else { indexCache.Add(peptideEntry.ModificationIndexes, peptideEntry.ModificationIndexes); } } return(peptideEntry); }
public int CompareModIndexes(PeptideEntry that) { return(CompareModificationIndexes(ModificationIndexes, that.ModificationIndexes)); }