public static SrmDocument ImportFasta(SrmDocument document, string fastaPath, IProgressMonitor monitor,
                                              IdentityPath to, out IdentityPath firstAdded, out IdentityPath nextAdd, out List <PeptideGroupDocNode> peptideGroupsNew)
        {
            var importer = new FastaImporter(document, false);

            using (TextReader reader = File.OpenText(fastaPath))
            {
                peptideGroupsNew = importer.Import(reader, monitor, Helpers.CountLinesInFile(fastaPath)).ToList();
                document         = document.AddPeptideGroups(peptideGroupsNew, false, to, out firstAdded, out nextAdd);
            }
            return(document);
        }
Exemple #2
0
        public static SrmDocument ImportFasta(SrmDocument document, string fastaPath, IProgressMonitor monitor,
                                              IdentityPath to, out IdentityPath firstAdded, out IdentityPath nextAdd, out int emptyProteinCount)
        {
            var importer = new FastaImporter(document, false);

            using (TextReader reader = File.OpenText(fastaPath))
            {
                document = document.AddPeptideGroups(importer.Import(reader, monitor, Helpers.CountLinesInFile(fastaPath)),
                                                     false, null, out firstAdded, out nextAdd);
            }
            emptyProteinCount = importer.EmptyPeptideGroupCount;
            return(document);
        }
        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<string, 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)) // Not L10N
            using (SQLiteCommand command = new SQLiteCommand(connection))
            {
                connection.Open();
                command.CommandText = "SELECT PeptideModSeq, Charge, Mz, Value, Type FROM OptimizationLibrary"; // Not L10N
                using (SQLiteDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        var type = (OptimizationType)reader["Type"]; // Not L10N
                        var modifiedSequence = reader["PeptideModSeq"].ToString(); // Not L10N
                        var charge = (int)reader["Charge"]; // Not L10N
                        var productMz = (double)reader["Mz"]; // Not L10N
                        var value = (double)reader["Value"]; // Not L10N
                        optimizations.Add(new Tuple<DbOptimization, double>(new DbOptimization(type, modifiedSequence, charge, string.Empty, -1, 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(charge)) // Not L10N
                               ).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") // Not L10N
                          .ChangeFragmentRangeLastName("last ion") // Not L10N
                          .ChangeProductCharges(Enumerable.Range(1, maxCharge).ToList())
                          .ChangeIonTypes(new []{ IonType.y, IonType.b }))
                .ChangeTransitionLibraries(libs => libs.ChangePick(TransitionLibraryPick.none))
                );
            var matcher = new ModificationMatcher { FormatProvider = NumberFormatInfo.InvariantInfo };
            matcher.CreateMatches(newDoc.Settings, peptideList, Settings.Default.StaticModList, Settings.Default.HeavyModList);
            FastaImporter importer = new FastaImporter(newDoc, matcher);
            string text = string.Format(">>{0}\r\n{1}", newDoc.GetPeptideGroupId(true), TextUtil.LineSeparate(peptideList)); // Not L10N
            PeptideGroupDocNode imported = importer.Import(new StringReader(text), null, Helpers.CountLinesInString(text)).First();

            int optimizationsUpdated = 0;
            foreach (PeptideDocNode nodePep in imported.Children)
            {
                string sequence = newDoc.Settings.GetSourceTextId(nodePep);
                foreach (var nodeGroup in nodePep.TransitionGroups)
                {
                    int charge = nodeGroup.PrecursorCharge;
                    foreach (var nodeTran in nodeGroup.Transitions)
                    {
                        double productMz = nodeTran.Mz;
                        foreach (var optimization in optimizations.Where(opt =>
                            string.IsNullOrEmpty(opt.Item1.FragmentIon) &&
                            opt.Item1.ProductCharge == -1 &&
                            opt.Item1.PeptideModSeq == sequence &&
                            opt.Item1.Charge == charge &&
                            Math.Abs(opt.Item2 - productMz) < 0.00001))
                        {
                            optimization.Item1.FragmentIon = nodeTran.FragmentIonName;
                            optimization.Item1.ProductCharge = nodeTran.Transition.Charge;
                            ++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);
            }
        }
Exemple #4
0
        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));
            }
        }