示例#1
0
 public virtual bool EqualsIgnoreId(DbPrecursorAndIonMobility other)
 {
     return(DbPrecursorIon.EqualsIgnoreId(other.DbPrecursorIon) &&
            CollisionalCrossSectionSqA.Equals(other.CollisionalCrossSectionSqA) &&
            IonMobility.Equals(other.IonMobility) &&
            HighEnergyIonMobilityOffset.Equals(other.HighEnergyIonMobilityOffset) &&
            IonMobilityUnits == other.IonMobilityUnits);
 }
示例#2
0
 public DbPrecursorAndIonMobility(DbPrecursorIon precursor, double?collisionalCrossSection, double?ionMobility, eIonMobilityUnits units,
                                  double?highEnergyOffset)
 {
     DbPrecursorIon             = precursor;
     CollisionalCrossSectionSqA = collisionalCrossSection ?? 0;
     IonMobility                 = ionMobility ?? 0;
     IonMobilityUnits            = units;
     HighEnergyIonMobilityOffset = highEnergyOffset ?? 0;
 }
示例#3
0
 public override int GetHashCode()
 {
     unchecked
     {
         int hashCode = base.GetHashCode();
         hashCode = (hashCode * 397) ^ DbPrecursorIon.GetHashCode();
         hashCode = (hashCode * 397) ^ CollisionalCrossSectionSqA.GetHashCode();
         hashCode = (hashCode * 397) ^ IonMobility.GetHashCode();
         hashCode = (hashCode * 397) ^ HighEnergyIonMobilityOffset.GetHashCode();
         hashCode = (hashCode * 397) ^ (int)IonMobilityUnits;
         return(hashCode);
     }
 }
示例#4
0
        public IonMobilityDb UpdateIonMobilities(IList <DbPrecursorAndIonMobility> newMobilities)
        {
            var changeRecord = IonMobilityLibraryChange.NONE;

            using (var session = OpenWriteSession())
            {
                var oldMoleculesSet  = session.CreateCriteria <DbMolecule>().List <DbMolecule>();
                var oldPrecursorsSet = session.CreateCriteria <DbPrecursorIon>().List <DbPrecursorIon>();
                var oldMobilitiesSet = session.CreateCriteria <DbPrecursorAndIonMobility>().List <DbPrecursorAndIonMobility>();
                var nDeleted         = 0;
                var nAdded           = 0;

                // Remove items that are no longer in the list
                foreach (var mobilityOld in oldMobilitiesSet)
                {
                    if (!newMobilities.Any(m => m.EqualsIgnoreId(mobilityOld)))
                    {
                        session.Delete(mobilityOld);
                        if (!newMobilities.Any(m => Equals(m.DbPrecursorIon, mobilityOld.DbPrecursorIon)))
                        {
                            session.Delete(mobilityOld.DbPrecursorIon);
                            if (!newMobilities.Any(m => Equals(m.DbPrecursorIon.DbMolecule, mobilityOld.DbPrecursorIon.DbMolecule)))
                            {
                                session.Delete(mobilityOld.DbPrecursorIon.DbMolecule);
                                nDeleted++;
                            }
                        }
                    }
                }

                // Add or update items that have changed from the old list
                var newMobilitiesSet = new List <DbPrecursorAndIonMobility>();
                var newMoleculesSet  = new List <DbMolecule>();
                var newPrecursorsSet = new List <DbPrecursorIon>();
                foreach (var itemNew in newMobilities)
                {
                    // Create a new instance, because not doing this causes a BindingSource leak
                    // Also we want to create a non-redundant set
                    if (!newMobilitiesSet.Any(m => m.EqualsIgnoreId(itemNew)))
                    {
                        newMobilitiesSet.Add(new DbPrecursorAndIonMobility(itemNew));
                        nAdded++;
                    }
                    if (!newPrecursorsSet.Any(m => m.EqualsIgnoreId(itemNew.DbPrecursorIon)))
                    {
                        newPrecursorsSet.Add(new DbPrecursorIon(itemNew.DbPrecursorIon));
                    }
                    if (!newMoleculesSet.Any(m => m.EqualsIgnoreId(itemNew.DbPrecursorIon.DbMolecule)))
                    {
                        newMoleculesSet.Add(new DbMolecule(itemNew.DbPrecursorIon.DbMolecule));
                    }
                }
                // Update the molecules table
                using (var transaction = session.BeginTransaction())
                {
                    foreach (var molecule in newMoleculesSet)
                    {
                        if (oldMoleculesSet.Any(m => m.EqualsIgnoreId(molecule)))
                        {
                            session.SaveOrUpdate(molecule);
                        }
                        else
                        {
                            session.Save(molecule);
                        }
                    }

                    transaction.Commit();
                }

                // Read them back to get their assigned IDs
                oldMoleculesSet = session.CreateCriteria <DbMolecule>().List <DbMolecule>();

                // Update the precursors table
                using (var transaction = session.BeginTransaction())
                {
                    foreach (var precursor in newPrecursorsSet)
                    {
                        var dbMoleculeWithId        = oldMoleculesSet.FirstOrDefault(m => m.EqualsIgnoreId(precursor.DbMolecule));
                        var precursorWithMoleculeId =
                            new DbPrecursorIon(dbMoleculeWithId, precursor.GetPrecursorAdduct());
                        if (oldPrecursorsSet.Any(p => p.EqualsIgnoreId(precursor)))
                        {
                            session.SaveOrUpdate(precursorWithMoleculeId);
                        }
                        else
                        {
                            session.Save(precursorWithMoleculeId);
                        }
                    }

                    transaction.Commit();
                }

                // Read them back to get their assigned IDs
                oldPrecursorsSet = session.CreateCriteria <DbPrecursorIon>().List <DbPrecursorIon>();

                // Update the mobilities table
                using (var transaction = session.BeginTransaction())
                {
                    foreach (var mobility in newMobilitiesSet)
                    {
                        var dbPrecursorIonWithId    = oldPrecursorsSet.FirstOrDefault(p => p.EqualsIgnoreId(mobility.DbPrecursorIon));
                        var mobilityWithPrecursorId = new DbPrecursorAndIonMobility(dbPrecursorIonWithId,
                                                                                    mobility.CollisionalCrossSectionSqA, mobility.IonMobilityNullable,
                                                                                    mobility.IonMobilityUnits, mobility.HighEnergyIonMobilityOffset);
                        if (oldMobilitiesSet.Any(m => m.EqualsIgnoreId(mobility)))
                        {
                            session.SaveOrUpdate(mobilityWithPrecursorId);
                        }
                        else
                        {
                            session.Save(mobilityWithPrecursorId);
                        }
                    }

                    transaction.Commit();
                }

                if (nAdded > 0 || nDeleted > 0)
                {
                    var libInfo = session.CreateCriteria <DbLibInfo>().UniqueResult <DbLibInfo>();

                    libInfo.MajorVersion = libInfo.MajorVersion + 1;
                    libInfo.MinorVersion = DbLibInfo.SCHEMA_VERSION_CURRENT;

                    // Make sure LSID has version number consistent with libInfo.MajorVersion
                    var parts = libInfo.LibLSID.Split(':');
                    parts[parts.Length - 1] = string.Format(@"{0}.{1}", libInfo.MajorVersion, libInfo.MinorVersion);
                    libInfo.LibLSID         = string.Join(@":", parts);
                    libInfo.CreateTime      = new TimeStampISO8601().ToString(); // Update timestamp
                    session.Flush();

                    changeRecord = new IonMobilityLibraryChange() // For audit logging
                    {
                        LSID         = libInfo.LibLSID,
                        CreationTime = libInfo.CreateTime,
                        Added        = nAdded,
                        Deleted      = nDeleted
                    };
                }
            }

            var result = ChangeProp(ImClone(this), im => im.LoadIonMobilities());

            return(ChangeProp(ImClone(result), im => im.LastChange = changeRecord)); // High level change description for audit logging
        }