Ejemplo n.º 1
0
        /// <summary>
        /// Saves the database to a new directory with only the charged peptides used
        /// in a given document.
        /// </summary>
        /// <param name="pathDestDir">The directory to save to</param>
        /// <param name="document">The document for which charged peptides are to be kept</param>
        /// <param name="smallMoleculeConversionMap">Used for changing charge,modifedSeq to adduct,molecule in small molecule conversion</param>
        /// <returns>The full path to the file saved</returns>
        public override string PersistMinimized(string pathDestDir,
                                                SrmDocument document, IDictionary <LibKey, LibKey> smallMoleculeConversionMap)
        {
            RequireUsable();

            var fname = Path.GetFileName(PersistencePath);

            if (smallMoleculeConversionMap != null && fname != null &&
                !fname.Contains(BiblioSpecLiteSpec.DotConvertedToSmallMolecules))
            {
                fname = fname.Replace(IonMobilityDb.EXT, BiblioSpecLiteSpec.DotConvertedToSmallMolecules + IonMobilityDb.EXT);
            }
            string persistPath = Path.Combine(pathDestDir, fname ?? String.Empty);

            using (var fs = new FileSaver(persistPath))
            {
                var ionMobilityDbMinimal = IonMobilityDb.CreateIonMobilityDb(fs.SafeName);

                // Calculate the minimal set of peptides needed for this document
                var dbPeptides      = _database.GetPeptides().ToList();
                var persistPeptides = new List <ValidatingIonMobilityPeptide>();

                var dictPeptides = dbPeptides.ToDictionary(pep => pep.GetLibKey());
                foreach (var pair in document.MoleculePrecursorPairs)
                {
                    var test =
                        new ValidatingIonMobilityPeptide(pair.NodePep.ModifiedTarget, pair.NodeGroup.PrecursorAdduct, 0, 0);
                    var key = test.GetLibKey();
                    DbIonMobilityPeptide dbPeptide;
                    if (dictPeptides.TryGetValue(key, out dbPeptide))
                    {
                        if (smallMoleculeConversionMap != null)
                        {
                            // We are in the midst of converting a document to small molecules for test purposes
                            LibKey smallMolInfo;
                            if (smallMoleculeConversionMap.TryGetValue(new LibKey(pair.NodePep.ModifiedSequence, pair.NodeGroup.PrecursorCharge), out smallMolInfo))
                            {
                                var precursorAdduct         = smallMolInfo.Adduct;
                                var smallMoleculeAttributes = smallMolInfo.SmallMoleculeLibraryAttributes;
                                dbPeptide = new DbIonMobilityPeptide(smallMoleculeAttributes, precursorAdduct, dbPeptide.CollisionalCrossSection, dbPeptide.HighEnergyDriftTimeOffsetMsec);
                            }
                            else
                            {
                                // Not being converted
                                Assume.IsTrue(pair.NodeGroup.Peptide.IsDecoy);
                                continue;
                            }
                        }
                        persistPeptides.Add(new ValidatingIonMobilityPeptide(dbPeptide));
                        // Only add once
                        dictPeptides.Remove(key);
                    }
                }

                ionMobilityDbMinimal.UpdatePeptides(persistPeptides, new ValidatingIonMobilityPeptide[0]);
                fs.Commit();
            }

            return(persistPath);
        }
Ejemplo n.º 2
0
        public void TestLibIonMobilityInfo()
        {
            const double HIGH_ENERGY_DRIFT_TIME_OFFSET_MSEC = -.01;
            var dbIon1 = new DbIonMobilityPeptide("JKLMN", 1.2, HIGH_ENERGY_DRIFT_TIME_OFFSET_MSEC) { Id = 12345 };
            var dbIon2 = new DbIonMobilityPeptide(dbIon1);
            DbIonMobilityPeptide dbIon3 = null;
            Assert.AreEqual(dbIon1.GetHashCode(), dbIon2.GetHashCode());
            Assert.IsFalse(dbIon1.Equals(null));
            Assert.IsTrue(dbIon1.Equals(dbIon2 as object));
            // ReSharper disable once ExpressionIsAlwaysNull
            Assert.IsFalse(dbIon1.Equals(dbIon3 as object));
            Assert.IsTrue(dbIon1.Equals(dbIon1));
            Assert.IsTrue(dbIon1.Equals(dbIon1 as object));
            Assert.IsTrue(dbIon1.Equals(dbIon2));
            dbIon1.CollisionalCrossSection = 1.3;
            dbIon1.PeptideModSeq = "foo";
            Assert.AreNotEqual(dbIon1.CollisionalCrossSection, dbIon2.CollisionalCrossSection);
            Assert.AreNotEqual(dbIon1.Sequence, dbIon2.Sequence);
            Assert.AreNotEqual(dbIon1.PeptideModSeq, dbIon2.PeptideModSeq);

            var dictCCS1 = new Dictionary<LibKey, IonMobilityInfo[]>();
            var ccs1 = new List<IonMobilityInfo> { new IonMobilityInfo(1, true, HIGH_ENERGY_DRIFT_TIME_OFFSET_MSEC), new IonMobilityInfo(2, true, HIGH_ENERGY_DRIFT_TIME_OFFSET_MSEC) }; // Collisional cross sections
            var ccs2 = new List<IonMobilityInfo> { new IonMobilityInfo(3, true, HIGH_ENERGY_DRIFT_TIME_OFFSET_MSEC), new IonMobilityInfo(4, true, HIGH_ENERGY_DRIFT_TIME_OFFSET_MSEC) }; // Collisional cross sections
            const string seq1 = "JKLM";
            const string seq2 = "KLMN";
            dictCCS1.Add(new LibKey(seq1,1),ccs1.ToArray());
            dictCCS1.Add(new LibKey(seq2,1),ccs2.ToArray());
            var lib = new List<LibraryIonMobilityInfo> { new LibraryIonMobilityInfo("test", dictCCS1) };

            var peptideTimes = CollisionalCrossSectionGridViewDriver.ConvertDriftTimesToCollisionalCrossSections(null,
                lib, 1, null);
            var validatingIonMobilityPeptides = peptideTimes as ValidatingIonMobilityPeptide[] ?? peptideTimes.ToArray();
            Assert.AreEqual(2, validatingIonMobilityPeptides.Count());
            Assert.AreEqual(1.5, validatingIonMobilityPeptides[0].CollisionalCrossSection);
            Assert.AreEqual(3.5, validatingIonMobilityPeptides[1].CollisionalCrossSection);
            Assert.AreEqual(HIGH_ENERGY_DRIFT_TIME_OFFSET_MSEC, validatingIonMobilityPeptides[1].HighEnergyDriftTimeOffsetMsec);

            var dictCCS2 = new Dictionary<LibKey, IonMobilityInfo[]>();
            var ccs3 = new List<IonMobilityInfo> { new IonMobilityInfo(4, false, HIGH_ENERGY_DRIFT_TIME_OFFSET_MSEC), new IonMobilityInfo(5, false, HIGH_ENERGY_DRIFT_TIME_OFFSET_MSEC) }; // Drift times
            const string seq3 = "KLMNJ";
            dictCCS2.Add(new LibKey(seq3, 1), ccs3.ToArray());
            lib.Add(new LibraryIonMobilityInfo("test2", dictCCS2));
            List<LibraryIonMobilityInfo> lib1 = lib;
            AssertEx.ThrowsException<Exception>(() => CollisionalCrossSectionGridViewDriver.ConvertDriftTimesToCollisionalCrossSections(null,
                lib1, 2, null),
                String.Format(
                        Resources.CollisionalCrossSectionGridViewDriver_ProcessIonMobilityValues_Cannot_import_measured_drift_time_for_sequence__0___no_collisional_cross_section_conversion_parameters_were_provided_for_charge_state__1__,
                        seq3, 1));

            var regressions = new Dictionary<int, RegressionLine> {{1, new RegressionLine(2, 1)}};
            lib = new List<LibraryIonMobilityInfo> { new LibraryIonMobilityInfo("test", dictCCS2) };
            peptideTimes = CollisionalCrossSectionGridViewDriver.ConvertDriftTimesToCollisionalCrossSections(null,
                            lib, 1, regressions);
            validatingIonMobilityPeptides = peptideTimes as ValidatingIonMobilityPeptide[] ?? peptideTimes.ToArray();
            Assert.AreEqual(1, validatingIonMobilityPeptides.Count());
            Assert.AreEqual(1.75, validatingIonMobilityPeptides[0].CollisionalCrossSection);
        }
Ejemplo n.º 3
0
        public IonMobilityDb UpdatePeptides(IList <ValidatingIonMobilityPeptide> newPeptides, IList <ValidatingIonMobilityPeptide> oldPeptides)
        {
            var dictOld = new Dictionary <String, ValidatingIonMobilityPeptide>();

            foreach (var ionMobilityPeptide in oldPeptides)  // Not using ToDict in case of duplicate entries
            {
                ValidatingIonMobilityPeptide pep;
                if (!dictOld.TryGetValue(ionMobilityPeptide.Sequence, out pep))
                {
                    dictOld[ionMobilityPeptide.Sequence] = ionMobilityPeptide;
                }
            }
            var dictNew = new Dictionary <String, ValidatingIonMobilityPeptide>();

            foreach (var ionMobilityPeptide in newPeptides)  // Not using ToDict in case of duplicate entries
            {
                ValidatingIonMobilityPeptide pep;
                if (!dictNew.TryGetValue(ionMobilityPeptide.Sequence, out pep))
                {
                    dictNew[ionMobilityPeptide.Sequence] = ionMobilityPeptide;
                }
            }

            using (var session = OpenWriteSession())
                using (var transaction = session.BeginTransaction())
                {
                    // Remove peptides that are no longer in the list
                    foreach (var peptideOld in oldPeptides)
                    {
                        ValidatingIonMobilityPeptide pep;
                        if (!dictNew.TryGetValue(peptideOld.Sequence, out pep))
                        {
                            session.Delete(peptideOld);
                        }
                    }

                    // Add or update peptides that have changed from the old list
                    foreach (var peptideNew in newPeptides)
                    {
                        ValidatingIonMobilityPeptide pep;
                        if (!dictOld.TryGetValue(peptideNew.Sequence, out pep))
                        {
                            // Create a new instance, because not doing this causes a BindingSource leak
                            var peptideNewDisconnected = new DbIonMobilityPeptide(peptideNew);
                            session.SaveOrUpdate(peptideNewDisconnected);
                        }
                    }

                    transaction.Commit();
                }

            return(ChangeProp(ImClone(this), im => im.LoadPeptides(newPeptides)));
        }
Ejemplo n.º 4
0
 public virtual bool Equals(DbIonMobilityPeptide other)
 {
     if (ReferenceEquals(null, other))
     {
         return(false);
     }
     if (ReferenceEquals(this, other))
     {
         return(true);
     }
     return(base.Equals(other) &&
            Equals(other.PeptideModSeq, PeptideModSeq) &&
            other.HighEnergyDriftTimeOffsetMsec.Equals(other.HighEnergyDriftTimeOffsetMsec) &&
            other.CollisionalCrossSection.Equals(CollisionalCrossSection));
 }
Ejemplo n.º 5
0
        private void LoadPeptides(IEnumerable <DbIonMobilityPeptide> peptides)
        {
            var dictLibrary = new Dictionary <LibKey, DbIonMobilityPeptide>();

            foreach (var pep in peptides)
            {
                var dict = dictLibrary;
                try
                {
                    DbIonMobilityPeptide ignored;
                    var adduct = pep.GetPrecursorAdduct();
                    if (adduct.IsEmpty)
                    {
                        // Older formats didn't consider charge to be a factor is CCS, so just fake up M+H, M+2H and M+3H
                        for (int z = 1; z <= 3; z++)
                        {
                            var newPep = new DbIonMobilityPeptide(pep.GetNormalizedModifiedSequence(),
                                                                  Adduct.FromChargeProtonated(z), pep.CollisionalCrossSection, pep.HighEnergyDriftTimeOffsetMsec);
                            var key = newPep.GetLibKey();
                            if (!dict.TryGetValue(key, out ignored))
                            {
                                dict.Add(key, newPep);
                            }
                        }
                    }
                    else
                    {
                        var key = pep.GetLibKey();
                        if (!dict.TryGetValue(key, out ignored))
                        {
                            dict.Add(key, pep);
                        }
                    }
                }
                catch (ArgumentException)
                {
                }
            }

            DictLibrary = dictLibrary;
        }
Ejemplo n.º 6
0
        public IonMobilityDb UpdatePeptides(IList<ValidatingIonMobilityPeptide> newPeptides, IList<ValidatingIonMobilityPeptide> oldPeptides)
        {
            var dictOld = new Dictionary<String, ValidatingIonMobilityPeptide>();
            foreach (var ionMobilityPeptide in oldPeptides)  // Not using ToDict in case of duplicate entries
            {
                ValidatingIonMobilityPeptide pep;
                if (!dictOld.TryGetValue(ionMobilityPeptide.Sequence, out pep))
                    dictOld[ionMobilityPeptide.Sequence] = ionMobilityPeptide;
            }
            var dictNew = new Dictionary<String, ValidatingIonMobilityPeptide>();
            foreach (var ionMobilityPeptide in newPeptides)  // Not using ToDict in case of duplicate entries
            {
                ValidatingIonMobilityPeptide pep;
                if (!dictNew.TryGetValue(ionMobilityPeptide.Sequence, out pep))
                    dictNew[ionMobilityPeptide.Sequence] = ionMobilityPeptide;
            }

            using (var session = OpenWriteSession())
            using (var transaction = session.BeginTransaction())
            {
                // Remove peptides that are no longer in the list
                foreach (var peptideOld in oldPeptides)
                {
                    ValidatingIonMobilityPeptide pep;
                    if (!dictNew.TryGetValue(peptideOld.Sequence, out pep))
                        session.Delete(peptideOld);
                }

                // Add or update peptides that have changed from the old list
                foreach (var peptideNew in newPeptides)
                {
                    ValidatingIonMobilityPeptide pep;
                    if (!dictOld.TryGetValue(peptideNew.Sequence, out pep))
                    {
                        // Create a new instance, because not doing this causes a BindingSource leak
                        var peptideNewDisconnected = new DbIonMobilityPeptide(peptideNew);
                        session.SaveOrUpdate(peptideNewDisconnected);
                    }
                }

                transaction.Commit();
            }

            return ChangeProp(ImClone(this), im => im.LoadPeptides(newPeptides));
        }
Ejemplo n.º 7
0
 public DbIonMobilityPeptide(DbIonMobilityPeptide other)
     : this(other.ModifiedTarget, other.CollisionalCrossSection, other.HighEnergyDriftTimeOffsetMsec,
            other._adduct)
 {
     Id = other.Id;
 }
Ejemplo n.º 8
0
 public virtual bool Equals(DbIonMobilityPeptide other)
 {
     if (ReferenceEquals(null, other)) return false;
     if (ReferenceEquals(this, other)) return true;
     return base.Equals(other) &&
            Equals(other.PeptideModSeq, PeptideModSeq) &&
            other.HighEnergyDriftTimeOffsetMsec.Equals(other.HighEnergyDriftTimeOffsetMsec) &&
            other.CollisionalCrossSection.Equals(CollisionalCrossSection);
 }
Ejemplo n.º 9
0
 public DbIonMobilityPeptide(DbIonMobilityPeptide other)
     : this(other.PeptideModSeq, other.CollisionalCrossSection, other.HighEnergyDriftTimeOffsetMsec)
 {
     Id = other.Id;
 }
Ejemplo n.º 10
0
 public DbIonMobilityPeptide(DbIonMobilityPeptide other)
     : this(other.PeptideModSeq, other.CollisionalCrossSection, other.HighEnergyDriftTimeOffsetMsec)
 {
     Id = other.Id;
 }