/// <summary> /// Saves the database to a new directory with only the optimizations used /// in a given document. /// </summary> /// <param name="pathDestDir">The directory to save to</param> /// <param name="document">The document for which peptides are to be kept</param> /// <returns>The full path to the file saved</returns> public string PersistMinimized(string pathDestDir, SrmDocument document) { RequireUsable(); string persistPath = Path.Combine(pathDestDir, Path.GetFileName(PersistencePath) ?? string.Empty); // ReSharper using (var fs = new FileSaver(persistPath)) { var optDbMinimal = OptimizationDb.CreateOptimizationDb(fs.SafeName); // Calculate the minimal set of optimizations needed for this document var persistOptimizations = new List <DbOptimization>(); var dictOptimizations = new OptimizationDictionary(_database.GetOptimizations()); var persistedKeys = new HashSet <OptimizationKey>(); foreach (PeptideGroupDocNode seq in document.MoleculeGroups) { // Skip peptide groups with no transitions if (seq.TransitionCount == 0) { continue; } foreach (PeptideDocNode peptide in seq.Children) { foreach (TransitionGroupDocNode group in peptide.Children) { var modSeq = document.Settings.GetSourceTarget(peptide); var charge = group.PrecursorAdduct; foreach (TransitionDocNode transition in group.Children) { foreach (var optType in Enum.GetValues(typeof(OptimizationType)).Cast <OptimizationType>()) { var optimizationKey = new OptimizationKey(optType, modSeq, charge, transition.FragmentIonName, transition.Transition.Adduct); foreach (var dbOptimization in dictOptimizations.EntriesMatching(optimizationKey)) { if (persistedKeys.Add(dbOptimization.Key)) { persistOptimizations.Add(dbOptimization); } break; } } } } } } optDbMinimal.UpdateOptimizations(persistOptimizations, new DbOptimization[0]); fs.Commit(); } return(persistPath); }
public bool Equals(OptimizationDb other) { if (ReferenceEquals(null, other)) { return(false); } if (ReferenceEquals(this, other)) { return(true); } return(Equals(other._path, _path) && other._modifiedTime.Equals(_modifiedTime)); }
/// <summary> /// Saves the database to a new directory with only the optimizations used /// in a given document. /// </summary> /// <param name="pathDestDir">The directory to save to</param> /// <param name="document">The document for which peptides are to be kept</param> /// <returns>The full path to the file saved</returns> public string PersistMinimized(string pathDestDir, SrmDocument document) { RequireUsable(); string persistPath = Path.Combine(pathDestDir, Path.GetFileName(PersistencePath) ?? string.Empty); // ReSharper using (var fs = new FileSaver(persistPath)) { var optDbMinimal = OptimizationDb.CreateOptimizationDb(fs.SafeName); // Calculate the minimal set of optimizations needed for this document var persistOptimizations = new List <DbOptimization>(); var dictOptimizations = _database.GetOptimizations().ToDictionary(opt => opt.Key); foreach (PeptideGroupDocNode seq in document.MoleculeGroups) { // Skip peptide groups with no transitions if (seq.TransitionCount == 0) { continue; } foreach (PeptideDocNode peptide in seq.Children) { foreach (TransitionGroupDocNode group in peptide.Children) { string modSeq = document.Settings.GetSourceTextId(peptide); int charge = group.PrecursorCharge; foreach (TransitionDocNode transition in group.Children) { foreach (var optType in Enum.GetValues(typeof(OptimizationType)).Cast <OptimizationType>()) { var optimizationKey = new OptimizationKey(optType, modSeq, charge, transition.FragmentIonName, transition.Transition.Charge); DbOptimization dbOptimization; if (dictOptimizations.TryGetValue(optimizationKey, out dbOptimization)) { persistOptimizations.Add(new DbOptimization(dbOptimization.Key, dbOptimization.Value)); // Only add once dictOptimizations.Remove(optimizationKey); } } } } } } optDbMinimal.UpdateOptimizations(persistOptimizations, new DbOptimization[0]); fs.Commit(); } return(persistPath); }
public OptimizationLibrary Initialize(SrmDocument document, IProgressMonitor loadMonitor) { if (_database != null || IsNone) { return(this); } var database = OptimizationDb.GetOptimizationDb(DatabasePath, loadMonitor, document); // Check for the case where an exception was handled by the progress monitor if (database == null) { return(null); } return(ChangeDatabase(database)); }
public OptimizationLibrary ChangeDatabase(OptimizationDb database) { return(ChangeProp(ImClone(this), im => im._database = database)); }
public bool Equals(OptimizationDb other) { if (ReferenceEquals(null, other)) return false; if (ReferenceEquals(this, other)) return true; return Equals(other._path, _path) && other._modifiedTime.Equals(_modifiedTime); }
public static OptimizationDb ConvertFromOldFormat(string path, IProgressMonitor loadMonitor, ProgressStatus status, SrmDocument document) { // Try to open assuming old format (Id, PeptideModSeq, Charge, Mz, Value, Type) var precursors = new Dictionary <Target, HashSet <int> >(); // PeptideModSeq -> charges var optimizations = new List <Tuple <DbOptimization, double> >(); // DbOptimization, product m/z int maxCharge = 1; using (SQLiteConnection connection = new SQLiteConnection(@"Data Source = " + path)) using (SQLiteCommand command = new SQLiteCommand(connection)) { connection.Open(); command.CommandText = @"SELECT PeptideModSeq, Charge, Mz, Value, Type FROM OptimizationLibrary"; using (SQLiteDataReader reader = command.ExecuteReader()) { while (reader.Read()) { var type = (OptimizationType)reader[@"Type"]; var modifiedSequence = new Target(reader[@"PeptideModSeq"].ToString()); var charge = (int)reader[@"Charge"]; var productMz = (double)reader[@"Mz"]; var value = (double)reader[@"Value"]; optimizations.Add(new Tuple <DbOptimization, double>(new DbOptimization(type, modifiedSequence, Adduct.FromChargeProtonated(charge), string.Empty, Adduct.EMPTY, value), productMz)); if (!precursors.ContainsKey(modifiedSequence)) { precursors[modifiedSequence] = new HashSet <int>(); } precursors[modifiedSequence].Add(charge); if (charge > maxCharge) { maxCharge = charge; } } } } var peptideList = (from precursor in precursors from charge in precursor.Value select string.Format(@"{0}{1}", precursor.Key, Transition.GetChargeIndicator(Adduct.FromChargeProtonated(charge))) ).ToList(); var newDoc = new SrmDocument(document != null ? document.Settings : SrmSettingsList.GetDefault()); newDoc = newDoc.ChangeSettings(newDoc.Settings .ChangePeptideLibraries(libs => libs.ChangePick(PeptidePick.filter)) .ChangeTransitionFilter(filter => filter.ChangeFragmentRangeFirstName(@"ion 1") .ChangeFragmentRangeLastName(@"last ion") .ChangePeptideProductCharges(Enumerable.Range(1, maxCharge).Select(Adduct.FromChargeProtonated).ToList()) // TODO(bspratt) negative charge peptides .ChangePeptideIonTypes(new [] { IonType.y, IonType.b })) // TODO(bspratt) generalize to molecules? .ChangeTransitionLibraries(libs => libs.ChangePick(TransitionLibraryPick.none)) ); var matcher = new ModificationMatcher(); matcher.CreateMatches(newDoc.Settings, peptideList, Settings.Default.StaticModList, Settings.Default.HeavyModList); FastaImporter importer = new FastaImporter(newDoc, matcher); // ReSharper disable LocalizableElement string text = string.Format(">>{0}\r\n{1}", newDoc.GetPeptideGroupId(true), TextUtil.LineSeparate(peptideList)); // ReSharper restore LocalizableElement PeptideGroupDocNode imported = importer.Import(new StringReader(text), null, Helpers.CountLinesInString(text)).First(); int optimizationsUpdated = 0; foreach (PeptideDocNode nodePep in imported.Children) { foreach (var nodeGroup in nodePep.TransitionGroups) { var charge = nodeGroup.PrecursorAdduct; var libKeyToMatch = newDoc.Settings.GetSourceTarget(nodePep).GetLibKey(charge).LibraryKey; foreach (var nodeTran in nodeGroup.Transitions) { double productMz = nodeTran.Mz; foreach (var optimization in optimizations.Where(opt => string.IsNullOrEmpty(opt.Item1.FragmentIon) && opt.Item1.ProductAdduct.IsEmpty && Math.Abs(opt.Item2 - productMz) < 0.00001)) { var optLibKey = optimization.Item1.Target.GetLibKey(optimization.Item1.Adduct).LibraryKey; if (!LibKeyIndex.KeysMatch(optLibKey, libKeyToMatch)) { continue; } optimization.Item1.FragmentIon = nodeTran.FragmentIonName; optimization.Item1.ProductAdduct = nodeTran.Transition.Adduct; ++optimizationsUpdated; } } } } if (optimizations.Count > optimizationsUpdated) { throw new OptimizationsOpeningException(string.Format(Resources.OptimizationDb_ConvertFromOldFormat_Failed_to_convert__0__optimizations_to_new_format_, optimizations.Count - optimizationsUpdated)); } using (var fs = new FileSaver(path)) { OptimizationDb db = CreateOptimizationDb(fs.SafeName); db.UpdateOptimizations(optimizations.Select(opt => opt.Item1).ToArray(), new DbOptimization[0]); fs.Commit(); if (loadMonitor != null) { loadMonitor.UpdateProgress(status.ChangePercentComplete(100)); } return(GetOptimizationDb(fs.RealName, null, null)); } }
public OptimizationLibrary ChangeDatabase(OptimizationDb database) { return ChangeProp(ImClone(this), im => im._database = database); }