public static MidasLibrary Create(LibrarySpec libSpec) { using (var sessionFactory = SessionFactoryFactory.CreateSessionFactory(libSpec.FilePath, typeof(MidasLibrary), true)) using (var session = new SessionWithLock(sessionFactory.OpenSession(), new ReaderWriterLock(), true)) using (var transaction = session.BeginTransaction()) { session.Save(new DbLibInfo { SchemaVersion = SCHEMA_VERSION_CURRENT, Guid = Guid.NewGuid().ToString() }); transaction.Commit(); return(new MidasLibrary(libSpec)); } }
public static void SaveSnack(SnackViewModel snackViewModel) { var sessionFactory = SessionFactoryFactory.GetFactory(); using (var session = sessionFactory.OpenSession()) { using (var trans = session.BeginTransaction()) { var snackModel = SnackMap.VmToModel(snackViewModel); session.SaveOrUpdate(snackModel); trans.Commit(); } } }
public static IEnumerable <SnackViewModel> GetSnacks() { var sessionFactory = SessionFactoryFactory.GetFactory(); using (var session = sessionFactory.OpenSession()) { using (var trans = session.BeginTransaction()) { var snackVMs = session.CreateCriteria <Snack>().List <Snack>(); return(snackVMs.Select(SnackMap.ModelToVm)); } } }
public static ProteomeDb CreateProteomeDb(String path) { using (var sessionFactory = SessionFactoryFactory.CreateSessionFactory(path, TYPE_DB, true)) { using (var session = new SessionWithLock(sessionFactory.OpenSession(), new ReaderWriterLock(), true)) using (var transaction = session.BeginTransaction()) { session.Save(new DbVersionInfo { SchemaVersionMajor = SCHEMA_VERSION_MAJOR_CURRENT, SchemaVersionMinor = SCHEMA_VERSION_MINOR_CURRENT }); transaction.Commit(); } } return(OpenProteomeDb(path)); }
public static IrtDb CreateIrtDb(string path) { using (var sessionFactory = SessionFactoryFactory.CreateSessionFactory(path, typeof(IrtDb), true)) { using (var session = new SessionWithLock(sessionFactory.OpenSession(), new ReaderWriterLock(), true)) using (var transaction = session.BeginTransaction()) { session.Save(new DbVersionInfo { SchemaVersion = SCHEMA_VERSION_CURRENT }); transaction.Commit(); } } return(GetIrtDb(path, null)); }
protected override IDisposable Connect() { DbProviderFactory fact = new SQLiteFactory(); SQLiteConnection conn = (SQLiteConnection)fact.CreateConnection(); if (conn != null) { var connectionStringBuilder = SessionFactoryFactory.SQLiteConnectionStringBuilderFromFilePath(FilePath); connectionStringBuilder.Version = 3; conn.ConnectionString = connectionStringBuilder.ToString(); conn.Open(); } return(conn); }
private void OpenSessionFactory() { if (!ReferenceEquals(this, _owner)) { return; } if (SessionFactory != null) { throw new InvalidOperationException("SessionFactory is already open"); } if (Path.GetExtension(DatabasePath) == TpgLinkDef.Extension) { _tpgLinkDef = TpgLinkDef.Load(DatabasePath); _sessionFactory = SessionFactoryFactory.CreateSessionFactory(TpgLinkDef, 0); } else { _sessionFactory = SessionFactoryFactory.CreateSessionFactory(DatabasePath, 0); } }
public void RemoveResultsFiles(params string[] resultsFiles) { using (var sessionFactory = SessionFactoryFactory.CreateSessionFactory(FilePath, typeof(MidasLibrary), false)) using (var session = new SessionWithLock(sessionFactory.OpenSession(), new ReaderWriterLock(), true)) using (var transaction = session.BeginTransaction()) { foreach (var kvp in _spectra) { if (resultsFiles.Contains(kvp.Key.FilePath)) { foreach (var spectrum in kvp.Value) { session.Delete(spectrum); } session.Delete(kvp.Key); } } transaction.Commit(); } }
void testModelFile(TestModel testModel, string filename) { using (var sessionFactory = SessionFactoryFactory.CreateSessionFactory(filename)) using (var session = testModel.session = sessionFactory.OpenSession()) { Assert.AreEqual(SchemaUpdater.CurrentSchemaRevision, session.UniqueResult <int>("SELECT SchemaRevision FROM About")); testModel.TestOverallCounts(); testModel.TestSanity(); testModel.TestProteins(); testModel.TestPeptides(); testModel.TestPeptideInstances(); testModel.TestSpectrumSourceGroups(); testModel.TestSpectrumSources(); testModel.TestSpectra(); testModel.TestAnalyses(); testModel.TestPeptideSpectrumMatches(); testModel.TestModifications(); } }
public static void ComputeData(Stopwatch sw) { sw.Restart(); var sessFactFactory = new SessionFactoryFactory(false); var sessionFactory = sessFactFactory.GetSessionFactory(); var repository = new Repository(sessionFactory); repository.Clear(); AggregationServices services = new AggregationServices(repository); services.ReAggregateAllOperations(); sw.Stop(); Console.WriteLine("Profiles computed in: {0}", sw.ElapsedMilliseconds); sw.Restart(); services.ComputeBalancesForAllAccounts(); repository.Flush(); sw.Stop(); Console.WriteLine("Balances computed in: {0}", sw.ElapsedMilliseconds); }
public ISessionFactory CreateDatabase() { using (var connection = OpenConnection(GetConnectionStringNoDatabase())) { var cmd = connection.CreateCommand(); cmd.CommandText = "CREATE DATABASE " + Database; cmd.ExecuteNonQuery(); } using (var connection = OpenConnection(GetConnectionString())) { if (DatabaseTypeEnum == DatabaseTypeEnum.mysql) { var cmd = connection.CreateCommand(); cmd.CommandText = "SET storage_engine=INNODB"; cmd.ExecuteNonQuery(); } var configuration = SessionFactoryFactory.GetConfiguration(DatabaseTypeEnum, 0); var schemaExport = new SchemaExport(configuration); schemaExport.Execute(false, true, false, connection, null); } return(SessionFactoryFactory.CreateSessionFactory(this, SessionFactoryFlags.CreateSchema)); }
protected Workspace CreateWorkspace(string path, DbTracerDef dbTracerDef) { using (var sessionFactory = SessionFactoryFactory.CreateSessionFactory(path, SessionFactoryFlags.CreateSchema)) { using (var session = sessionFactory.OpenSession()) { var transaction = session.BeginTransaction(); var dbWorkspace = new DbWorkspace { ModificationCount = 1, TracerDefCount = dbTracerDef == null ? 0 : 1, SchemaVersion = WorkspaceUpgrader.CurrentVersion, }; session.Save(dbWorkspace); if (dbTracerDef != null) { dbTracerDef.Workspace = dbWorkspace; dbTracerDef.Name = "Tracer"; session.Save(dbTracerDef); } var modification = new DbModification { DeltaMass = 57.021461, Symbol = "C", Workspace = dbWorkspace }; session.Save(modification); transaction.Commit(); } } var workspace = new Workspace(path); workspace.SetTaskScheduler(TaskScheduler.Default); workspace.DatabasePoller.LoadAndMergeChanges(null); return(workspace); }
public static ISessionFactory CreateSessionFactory(String path, bool createSchema) { return(SessionFactoryFactory.CreateSessionFactory(path, typeof(BlibDb), createSchema)); }
public ISessionFactory OpenSessionFactory() { return(SessionFactoryFactory.CreateSessionFactory(this, 0)); }
public void TestSchemaUpdater() { Assert.AreEqual(15, SchemaUpdater.CurrentSchemaRevision); var testModel = new TestModel() { TestContext = TestContext }; TestModel.ClassInitialize(TestContext); testModel.TestInitialize(); testModelFile(testModel, "testModel.idpDB"); string filename = null; // test all revisions without a data filter applied // we don't need to test upgrade from 12 to 13; the extra table (PeptideModificationProbability) is ignored and necessary for the current NHibernate bindings // we don't need to test upgrade from 11 to 12; the changed table (XICMetrics) is ignored // we don't need to test upgrade from 10 to 11; the extra columns (GeneGroups, Genes) are ignored // we don't need to test upgrade from 9 to 10; the extra tables (XICMetrics, XICMetricsSettings) are ignored // we don't need to test upgrade from 8 to 9; the extra columns (GeneLevelFiltering, DistinctMatchFormat) are ignored // we don't need to test upgrade from 7 to 8; the extra columns are ignored // we don't need to test upgrade from 5 to 6; it simply forces reapplication of the basic filters // we don't need to test upgrade from 4 to 5; it's a simple null value fix filename = "testModel-v4.idpDB"; File.Copy("testModel.idpDB", filename, true); downgradeToRevision(filename, 4); testModelFile(testModel, filename); filename = "testModel-v3.idpDB"; File.Copy("testModel.idpDB", filename, true); downgradeToRevision(filename, 3); testModelFile(testModel, filename); filename = "testModel-v2.idpDB"; File.Copy("testModel.idpDB", filename, true); downgradeToRevision(filename, 2); testModelFile(testModel, filename); filename = "testModel-v1.idpDB"; File.Copy("testModel.idpDB", filename, true); downgradeToRevision(filename, 1); testModelFile(testModel, filename); filename = "testModel-v0.idpDB"; File.Copy("testModel.idpDB", filename, true); downgradeToRevision(filename, 0); testModelFile(testModel, filename); // test all revisions with a data filter applied (only check that the update worked this time) File.Copy("testModel.idpDB", "testFilter.idpDB", true); var dataFilter = new DataFilter { MaximumQValue = 1 }; using (var sessionFactory = SessionFactoryFactory.CreateSessionFactory("testFilter.idpDB")) using (var session = testModel.session = sessionFactory.OpenSession()) { dataFilter.ApplyBasicFilters(session); } filename = "testFilter-v4.idpDB"; File.Copy("testFilter.idpDB", filename, true); downgradeToRevision(filename, 4); testFilterFile(filename); filename = "testFilter-v3.idpDB"; File.Copy("testFilter.idpDB", filename, true); downgradeToRevision(filename, 3); testFilterFile(filename); filename = "testFilter-v2.idpDB"; File.Copy("testFilter.idpDB", filename, true); downgradeToRevision(filename, 2); testFilterFile(filename); filename = "testFilter-v1.idpDB"; File.Copy("testFilter.idpDB", filename, true); downgradeToRevision(filename, 1); testFilterFile(filename); filename = "testFilter-v0.idpDB"; File.Copy("testFilter.idpDB", filename, true); downgradeToRevision(filename, 0); testFilterFile(filename); }
public void TestSchemaUpdater() { Assert.AreEqual(18, SchemaUpdater.CurrentSchemaRevision); var testModel = new TestModel() { TestContext = TestContext }; TestModel.ClassInitialize(TestContext); testModel.TestInitialize(); testModelFile(testModel, "testModel.idpDB"); string filename = null; // test all revisions without a data filter applied // we don't need to test upgrade from 5 to 6; it simply forces reapplication of the basic filters // we don't need to test upgrade from 4 to 5; it's a simple null value fix filename = "testModel-v4.idpDB"; File.Copy("testModel.idpDB", filename, true); downgradeToRevision(filename, 4); testModelFile(testModel, filename); filename = "testModel-v3.idpDB"; File.Copy("testModel.idpDB", filename, true); downgradeToRevision(filename, 3); testModelFile(testModel, filename); filename = "testModel-v2.idpDB"; File.Copy("testModel.idpDB", filename, true); downgradeToRevision(filename, 2); testModelFile(testModel, filename); filename = "testModel-v1.idpDB"; File.Copy("testModel.idpDB", filename, true); downgradeToRevision(filename, 1); testModelFile(testModel, filename); filename = "testModel-v0.idpDB"; File.Copy("testModel.idpDB", filename, true); downgradeToRevision(filename, 0); testModelFile(testModel, filename); // test all revisions with a data filter applied (only check that the update worked this time) File.Copy("testModel.idpDB", "testFilter.idpDB", true); var dataFilter = new DataFilter { MaximumQValue = 1 }; using (var sessionFactory = SessionFactoryFactory.CreateSessionFactory("testFilter.idpDB")) using (var session = testModel.session = sessionFactory.OpenSession()) { dataFilter.ApplyBasicFilters(session); } filename = "testFilter-v4.idpDB"; File.Copy("testFilter.idpDB", filename, true); downgradeToRevision(filename, 4); testFilterFile(filename); filename = "testFilter-v3.idpDB"; File.Copy("testFilter.idpDB", filename, true); downgradeToRevision(filename, 3); testFilterFile(filename); filename = "testFilter-v2.idpDB"; File.Copy("testFilter.idpDB", filename, true); downgradeToRevision(filename, 2); testFilterFile(filename); filename = "testFilter-v1.idpDB"; File.Copy("testFilter.idpDB", filename, true); downgradeToRevision(filename, 1); testFilterFile(filename); filename = "testFilter-v0.idpDB"; File.Copy("testFilter.idpDB", filename, true); downgradeToRevision(filename, 0); testFilterFile(filename); }
public static void AddSpectra(MidasLibSpec libSpec, MsDataFilePath[] resultsFiles, SrmDocument doc, ILoadMonitor monitor, out List <MsDataFilePath> failedFiles) { // Get spectra from results files var newSpectra = new List <DbSpectrum>(); var progress = new ProgressStatus(string.Empty).ChangeMessage(Resources.MidasLibrary_AddSpectra_Reading_MIDAS_spectra); const int percentResultsFiles = 80; failedFiles = new List <MsDataFilePath>(); for (var i = 0; i < resultsFiles.Length; i++) { var resultsFile = resultsFiles[i]; try { monitor.UpdateProgress(progress.ChangePercentComplete(i * percentResultsFiles / resultsFiles.Length)); var filePath = resultsFile.GetFilePath(); if (File.Exists(filePath)) { var sampleIndex = resultsFile.GetSampleIndex(); using (var msd = new MsDataFileImpl(filePath, sampleIndex == -1 ? 0 : sampleIndex, resultsFile.GetLockMassParameters(), requireVendorCentroidedMS2: true)) { if (ChromatogramDataProvider.HasChromatogramData(msd) && SpectraChromDataProvider.HasSpectrumData(msd)) { var chromPrecursors = ReadChromPrecursorsFromMsd(msd, monitor).ToList(); newSpectra.AddRange(ReadDbSpectraFromMsd(msd, monitor)); MatchSpectraToChrom(newSpectra, chromPrecursors, monitor); } } MatchSpectraToPeptides(newSpectra, doc, monitor); } else { failedFiles.Add(resultsFile); } } catch (Exception x) { monitor.UpdateProgress(progress.ChangeErrorException(x)); failedFiles.Add(resultsFile); } if (monitor.IsCanceled) { monitor.UpdateProgress(progress.Cancel()); return; } } if (!newSpectra.Any()) { monitor.UpdateProgress(progress.Complete()); return; } progress = progress.ChangePercentComplete(percentResultsFiles); monitor.UpdateProgress(progress); // Add spectra to library var midasLib = !File.Exists(libSpec.FilePath) ? Create(libSpec) : Load(libSpec, monitor); if (midasLib == null) { monitor.UpdateProgress(progress.ChangeErrorException(new Exception(Resources.MidasLibrary_AddSpectra_Error_loading_MIDAS_library_for_adding_spectra_))); return; } progress = progress.ChangeMessage(Resources.MidasLibrary_AddSpectra_Adding_spectra_to_MIDAS_library); monitor.UpdateProgress(progress); var results = new Dictionary <string, DbResultsFile>(); if (midasLib._spectra != null) { foreach (var kvp in midasLib._spectra) { results[kvp.Key.FilePath] = kvp.Key; } } using (var sessionFactory = SessionFactoryFactory.CreateSessionFactory(libSpec.FilePath, typeof(MidasLibrary), false)) using (var session = new SessionWithLock(sessionFactory.OpenSession(), new ReaderWriterLock(), true)) using (var transaction = session.BeginTransaction()) { for (var i = 0; i < newSpectra.Count; i++) { if (monitor.IsCanceled) { transaction.Rollback(); monitor.UpdateProgress(progress.Cancel()); return; } var spectrum = newSpectra[i]; monitor.UpdateProgress(progress.ChangePercentComplete(percentResultsFiles + (int)(100.0 * i / newSpectra.Count))); DbResultsFile resultsFile; if (!results.TryGetValue(spectrum.ResultsFile.FilePath, out resultsFile)) { resultsFile = new DbResultsFile(spectrum.ResultsFile) { Id = null }; results[spectrum.ResultsFile.FilePath] = resultsFile; session.SaveOrUpdate(resultsFile); } else if (midasLib._spectra != null) { List <DbSpectrum> existingSpectra; if (midasLib._spectra.TryGetValue(resultsFile, out existingSpectra) && existingSpectra.Any(x => Equals(x.ResultsFile.FilePath, spectrum.ResultsFile.FilePath) && Equals(x.PrecursorMz, spectrum.PrecursorMz) && Equals(x.RetentionTime, spectrum.RetentionTime))) { // This spectrum already exists in the library continue; } } var spectrumNewDisconnected = new DbSpectrum(spectrum) { Id = null, ResultsFile = resultsFile }; session.SaveOrUpdate(spectrumNewDisconnected); } transaction.Commit(); monitor.UpdateProgress(progress.Complete()); } }
public void TestInitialize() { var sessionFactory = SessionFactoryFactory.CreateSessionFactory("testQueryReader.idpDB", false, false); session = sessionFactory.OpenSession(); }
private bool Load(IProgressMonitor monitor) { _spectra = null; if (FilePath == null) { return(false); } var info = new FileInfo(FilePath); if (!info.Exists || info.Length == 0) { return(false); } var progress = new ProgressStatus(string.Empty).ChangeMessage(Resources.MidasLibrary_Load_Loading_MIDAS_library); monitor.UpdateProgress(progress); var spectra = new Dictionary <DbResultsFile, List <DbSpectrum> >(); try { using (var sessionFactory = SessionFactoryFactory.CreateSessionFactory(FilePath, typeof(MidasLibrary), false)) using (var session = new SessionWithLock(sessionFactory.OpenSession(), new ReaderWriterLock(), false)) { var libInfo = session.CreateCriteria(typeof(DbLibInfo)).List <DbLibInfo>(); if (libInfo.Count != 1) { throw new Exception(Resources.MidasLibrary_Load_Error_reading_LibInfo_from_MIDAS_library); } SchemaVersion = libInfo[0].SchemaVersion; LibraryGuid = libInfo[0].Guid; var readSpectra = session.CreateCriteria(typeof(DbSpectrum)).List <DbSpectrum>(); progress = progress.ChangeSegments(0, readSpectra.Count); foreach (var spectrum in readSpectra) { if (monitor.IsCanceled) { monitor.UpdateProgress(progress.Cancel()); return(false); } progress = progress.NextSegment(); monitor.UpdateProgress(progress); List <DbSpectrum> list; if (!spectra.TryGetValue(spectrum.ResultsFile, out list)) { list = new List <DbSpectrum>(); spectra[spectrum.ResultsFile] = list; } list.Add(spectrum); } } } catch { monitor.UpdateProgress(progress.Cancel()); return(false); } _spectra = spectra; monitor.UpdateProgress(progress.Complete()); return(true); }
public void TestChromatogramGenerator() { String dbPath = Path.Combine(TestContext.TestDir, "test" + Guid.NewGuid() + ".tpg"); using (var sessionFactory = SessionFactoryFactory.CreateSessionFactory(dbPath, SessionFactoryFlags.CreateSchema)) { using (var session = sessionFactory.OpenSession()) { session.BeginTransaction(); DbWorkspace dbWorkspace = new DbWorkspace { TracerDefCount = 1, }; session.Save(dbWorkspace); DbTracerDef dbTracerDef = TracerDef.GetN15Enrichment(); dbTracerDef.Workspace = dbWorkspace; dbTracerDef.Name = "Tracer"; session.Save(dbTracerDef); session.Save(new DbSetting { Workspace = dbWorkspace, Name = SettingEnum.data_directory.ToString(), Value = GetDataDirectory() }); session.Transaction.Commit(); } } Workspace workspace = new Workspace(dbPath); workspace.SetTaskScheduler(TaskScheduler.Default); var dbMsDataFile = new DbMsDataFile { Name = "20090724_HT3_0", }; using (var session = workspace.OpenWriteSession()) { session.BeginTransaction(); session.Save(dbMsDataFile); session.Transaction.Commit(); } workspace.DatabasePoller.LoadAndMergeChanges(null); var msDataFile = workspace.MsDataFiles.FindByKey(dbMsDataFile.GetId()); Assert.IsTrue(MsDataFileUtil.InitMsDataFile(workspace, msDataFile)); DbPeptide dbPeptide; using (var session = workspace.OpenWriteSession()) { session.BeginTransaction(); dbPeptide = new DbPeptide { Protein = "TestProtein", Sequence = "YLAAYLLLVQGGNAAPSAADIK", FullSequence = "K.YLAAYLLLVQGGNAAPSAADIK.A", }; session.Save(dbPeptide); var searchResult = new DbPeptideSpectrumMatch { Peptide = dbPeptide, MsDataFile = session.Load <DbMsDataFile>(msDataFile.Id), PrecursorCharge = 3, RetentionTime = 20.557 * 60, }; session.Save(searchResult); session.Transaction.Commit(); } var peptide = new Peptide(workspace, dbPeptide); var peptideAnalysis = peptide.EnsurePeptideAnalysis(); peptideAnalysis.IncChromatogramRefCount(); var peptideFileAnalysis = PeptideFileAnalysis.EnsurePeptideFileAnalysis(peptideAnalysis, msDataFile); workspace.DatabasePoller.LoadAndMergeChanges(null); peptideAnalysis.IncChromatogramRefCount(); var chromatogramGenerator = new ChromatogramGenerator(workspace); chromatogramGenerator.Start(); while (peptideFileAnalysis.ChromatogramSet == null) { Thread.Sleep(100); } var chromatogramDatas = peptideFileAnalysis.GetChromatograms(); Assert.IsFalse(chromatogramDatas.Chromatograms.Count == 0); chromatogramGenerator.Stop(); while (chromatogramGenerator.IsThreadAlive) { Thread.Sleep(100); } }
public void TestImportExportIdpXml() { var testModel = new TestModel(); using (var sessionFactory = SessionFactoryFactory.CreateSessionFactory("testModel.idpDB")) using (var session = testModel.session = sessionFactory.OpenSession()) { var distinctAnalyses = testModel.session.Query <Analysis>(); foreach (var analysis in distinctAnalyses) { session.Save(new AnalysisParameter() { Analysis = analysis, Name = "ProteinDatabase", Value = "testImportExport.fasta" }); } /*IList<string> idpXmlPaths; * using (var exporter = new Exporter(session)) * { * idpXmlPaths = exporter.WriteIdpXml(true, true); * exporter.WriteSpectra(); * exporter.WriteProteins("testImportExport.fasta", false); * }*/ session.Close(); /*using (var parser = new Parser(".", qonverterSettingsHandler, false, idpXmlPaths.ToArray())) * { * // ReadXml should pick up mzML files in the same directory as the idpXMLs * parser.Start(); * }*/ //var merger = new Merger("testImportExport.idpDB", idpXmlPaths.Select(o => Path.ChangeExtension(o, ".idpDB"))); //merger.Start(); } using (var sessionFactory = SessionFactoryFactory.CreateSessionFactory("testImportExport.idpDB")) using (var session = testModel.session = sessionFactory.OpenSession()) { foreach (var analysis in session.Query <Analysis>()) { session.Delete(session.UniqueResult <AnalysisParameter>(o => o.Analysis.Id == analysis.Id && o.Name == "ProteinDatabase")); } session.Flush(); testModel.TestOverallCounts(); testModel.TestSanity(); testModel.TestProteins(); testModel.TestPeptides(); testModel.TestPeptideInstances(); testModel.TestSpectrumSourceGroups(); testModel.TestSpectrumSources(); testModel.TestSpectra(); testModel.TestAnalyses(); testModel.TestPeptideSpectrumMatches(); testModel.TestModifications(); // TODO: enable QonverterSettings round trip for idpXML? //testModel.TestQonverterSettings(); } }
// TODO(nicksh): 2013-10-03: Re-enable this test once it reliably passes //[TestMethod] public void TestPeakFinder() { String dbPath = Path.Combine(TestContext.TestDir, "PeakFinderTest.tpg"); using (var sessionFactory = SessionFactoryFactory.CreateSessionFactory(dbPath, SessionFactoryFlags.CreateSchema)) { using (var session = sessionFactory.OpenSession()) { session.BeginTransaction(); DbWorkspace dbWorkspace = new DbWorkspace { TracerDefCount = 1, SettingCount = 2, SchemaVersion = WorkspaceUpgrader.CurrentVersion }; session.Save(dbWorkspace); DbTracerDef dbTracerDef = TracerDef.GetD3LeuEnrichment(); dbTracerDef.Workspace = dbWorkspace; dbTracerDef.Name = "Tracer"; session.Save(dbTracerDef); session.Save(new DbSetting { Workspace = dbWorkspace, Name = SettingEnum.data_directory.ToString(), Value = GetDataDirectory() }); session.Save(new DbSetting { Workspace = dbWorkspace, Name = SettingEnum.mass_accuracy.ToString(), Value = "100000" }); session.Transaction.Commit(); } } Workspace workspace = new Workspace(dbPath); workspace.SetTaskScheduler(TaskScheduler.Default); workspace.DatabasePoller.LoadAndMergeChanges(null); foreach (var peakFinderPeptide in peptides) { AddPeptide(workspace, peakFinderPeptide); } while (true) { string statusMessage; int progress; workspace.ChromatogramGenerator.GetProgress(out statusMessage, out progress); if ("Idle" == statusMessage) { break; } Thread.Sleep(100); } workspace.DatabasePoller.LoadAndMergeChanges(null); var exceptions = new List <Exception>(); foreach (var peptide in peptides) { try { TestPeptide(workspace, peptide); } catch (Exception exception) { exceptions.Add(exception); Trace.TraceError("{0}:{1}", peptide.PeptideSequence, exception); } } CollectionAssert.AreEqual(new Exception[0], exceptions); }
public void TestStaticQonverter() { #region Example with forward and reverse proteins string[] testProteinSequences = new string[] { "PEPTIDERPEPTIDEK", "TIDERPEPTIDEKPEP", "DERPEPTIDEKPEPTI", "THEQUICKBRWNFXUMPSVERTHELZYDG", "KEDITPEPREDITPEP", "PEPKEDITPEPREDIT", "ITPEPKEDITPEPRED", "GDYZLEHTREVSPMUXFNWRBKCIUQEHT" }; const string decoyPrefix = "r-"; const int analysisCount = 2; const int sourceCount = 2; const int chargeCount = 2; File.Delete("testStaticQonversion.idpDB"); var sessionFactory = SessionFactoryFactory.CreateSessionFactory("testStaticQonversion.idpDB", new SessionFactoryConfig { CreateSchema = true }); var session = sessionFactory.OpenSession(); TestModel.CreateTestProteins(session, testProteinSequences); session.Clear(); // add decoy prefix to the second half of the proteins for (long i = testProteinSequences.LongLength / 2; i < testProteinSequences.LongLength; ++i) { var pro = session.Get <Protein>(i + 1); pro.Accession = decoyPrefix + pro.Accession; session.Update(pro); } #endregion #region Example PSMs // For each combination of 2 engines, 2 sources, and 2 charges, // we test qonversion on this scenario: // // Scan Score DecoyState Targets Decoys Ambiguous -> Expected QValue // 50 6 T 1 0 0 0 // 30 5 T 2 0 0 0 // 40 4 T 3 0 0 0 // 10 3 D 3 1 0 2/4 // 20 2 D 3 2 0 4/5 // 60 1 T 4 2 0 4/6 double q = PeptideSpectrumMatch.DefaultQValue; for (int analysis = 1; analysis <= analysisCount; ++analysis) { for (int source = 1; source <= sourceCount; ++source) { for (int charge = 1; charge <= chargeCount; ++charge) { List <SpectrumTuple> testPsmSummary = new List <SpectrumTuple>() { // Group Source Spectrum Analysis Score Q List of Peptide@Charge/ScoreDivider new SpectrumTuple("/", source, 5, analysis, 6, q, String.Format("TIDERPEPTIDEK@{0}/1 PEPTIDER@{0}/8", charge)), new SpectrumTuple("/", source, 3, analysis, 5, q, String.Format("TIDERPEPTIDEK@{0}/1 PEPTIDER@{0}/2", charge)), new SpectrumTuple("/", source, 4, analysis, 4, q, String.Format("EDITPEP@{0}/1 THEQUICKBR@{0}/4", charge)), new SpectrumTuple("/", source, 1, analysis, 3, q, String.Format("PEPTIDEKPEP@{0}/1 PEPTIDEK@{0}/2", charge)), new SpectrumTuple("/", source, 2, analysis, 2, q, String.Format("TIDER@{0}/1 PEPTIDER@{0}/2", charge)), new SpectrumTuple("/", source, 6, analysis, 1, q, String.Format("EDITPEPR@{0}/1 TIDERPEPTIDEK@{0}/3", charge)) }; TestModel.CreateTestData(session, testPsmSummary); } } } List <SpectrumTuple> testPsmSummaryWithoutScores = new List <SpectrumTuple>() { new SpectrumTuple("/", 1, 1, analysisCount + 1, null, 0.01, "TIDERPEPTIDEK@1/1 PEPTIDER@3/8"), new SpectrumTuple("/", 1, 2, analysisCount + 1, null, 0.02, "KEDITPEPR@2/1 PEPTIDEK@2/8"), new SpectrumTuple("/", 1, 3, analysisCount + 1, null, 0.03, "THEQUICKBR@3/1 THELZYDG@1/8"), }; TestModel.CreateTestData(session, testPsmSummaryWithoutScores); // force all peptide instances to fully specific foreach (PeptideInstance pi in session.Query <PeptideInstance>()) { pi.NTerminusIsSpecific = pi.CTerminusIsSpecific = true; session.Update(pi); } session.Flush(); session.Close(); // close the connection sessionFactory.Close(); #endregion var qonverter = new IDPicker.Qonverter() { SettingsByAnalysis = new Dictionary <int, Qonverter.Settings>() }; for (int i = 1; i <= analysisCount + 1; ++i) { qonverter.SettingsByAnalysis[i] = new Qonverter.Settings() { QonverterMethod = Qonverter.QonverterMethod.StaticWeighted, ChargeStateHandling = Qonverter.ChargeStateHandling.Ignore, TerminalSpecificityHandling = Qonverter.TerminalSpecificityHandling.Ignore, MassErrorHandling = Qonverter.MassErrorHandling.Ignore, MissedCleavagesHandling = Qonverter.MissedCleavagesHandling.Ignore, DecoyPrefix = decoyPrefix, ScoreInfoByName = new Dictionary <string, Qonverter.Settings.ScoreInfo>() { { "score1", new Qonverter.Settings.ScoreInfo() { Weight = 1 } }, { "score2", new Qonverter.Settings.ScoreInfo() { Weight = 0 } } } } } ; var progressTester = new QonversionProgressTest() { Qonverter = qonverter, ExpectedTotalAnalyses = analysisCount * sourceCount // engine 3 is ignored }; File.Copy("testStaticQonversion.idpDB", "testStaticQonversion2.idpDB", true); // test without progress monitor qonverter.Qonvert("testStaticQonversion2.idpDB"); qonverter.QonversionProgress += new IDPicker.Qonverter.QonversionProgressEventHandler(progressTester.qonverter_QonversionProgress); File.Copy("testStaticQonversion.idpDB", "testStaticQonversion3.idpDB", true); // test progress monitor with cancelling // (must before a full qonversion since the QValue test doesn't expect cancellation) progressTester.ExpectedQonvertedAnalyses = 0; progressTester.CancelAtCount = 4; qonverter.Qonvert("testStaticQonversion3.idpDB"); // test progress monitor without cancelling progressTester.ExpectedQonvertedAnalyses = 0; progressTester.CancelAtCount = int.MaxValue; qonverter.Qonvert("testStaticQonversion.idpDB"); sessionFactory = SessionFactoryFactory.CreateSessionFactory("testStaticQonversion.idpDB"); session = sessionFactory.OpenSession(); session.Clear(); #region QValue test Dictionary <long, double> expectedQValues = new Dictionary <long, double>() { // with high scoring decoys, Q values can spike and gradually go down again; // we squash these spikes such that Q value is monotonically increasing; // then we convert Q values to FDR scores using linear interpolation // targets decoys ambiguous unadjusted-Q-value adjusted-Q-value FDR-score { 4, 0 }, // 1 0 0 0 0 0 { 2, 0.2 }, // 2 0 0 0 0 0.2 { 3, 0.4 }, // 2 1 0 0.666 0.4 0.4 { 0, 0.488 }, // 3 1 0 0.5 0.4 0.488 { 1, 0.577 }, // 4 1 0 0.4 0.4 0.577 { 5, 0.666 }, // 4 2 0 0.666 0.666 0.666 }; for (long engine = 1; engine <= 2; ++engine) { for (long source = 1; source <= 2; ++source) { for (int charge = 1; charge <= 2; ++charge) { foreach (var itr in expectedQValues) { var topRankedMatch = session.CreateQuery("SELECT psm " + "FROM PeptideSpectrumMatch psm " + "WHERE psm.Analysis.id = ? " + " AND psm.Spectrum.Source.id = ? " + " AND psm.Spectrum.Index = ? " + " AND psm.Charge = ? " + " AND psm.Rank = 1" + "GROUP BY psm.Spectrum.id ") .SetParameter(0, engine) .SetParameter(1, source) .SetParameter(2, itr.Key) .SetParameter(3, charge) .List <PeptideSpectrumMatch>().First(); Assert.AreEqual(session.Get <Analysis>(engine), topRankedMatch.Analysis); Assert.AreEqual(session.Get <SpectrumSource>(source), topRankedMatch.Spectrum.Source); Assert.AreEqual(charge, topRankedMatch.Charge); Assert.AreEqual(itr.Value, topRankedMatch.QValue, 1e-3); } } } } var scorelessMatches = session.CreateQuery("SELECT psm " + "FROM PeptideSpectrumMatch psm " + "WHERE psm.Analysis.id = 3 " + " AND psm.Rank = 1") .List <PeptideSpectrumMatch>(); Assert.AreEqual(3, scorelessMatches.Count); Assert.AreEqual(0.01, scorelessMatches[0].QValue, 1e-12); Assert.AreEqual(0.02, scorelessMatches[1].QValue, 1e-12); Assert.AreEqual(0.03, scorelessMatches[2].QValue, 1e-12); #endregion }
public static ISessionFactory CreateSessionFactory_Redundant(String path, bool createSchema) { return(SessionFactoryFactory.CreateSessionFactory(path, typeof(BlibDb), @"mapping_redundant.xml", createSchema)); }
/// <summary> /// Maintains a single string globally for each path, in order to keep from /// having two threads accessing the same database at the same time. /// </summary> private static ISessionFactory GetSessionFactory(string path) { return(SessionFactoryFactory.CreateSessionFactory(path, typeof(IrtDb), false)); }
public void TestMergerModel() { #region Example proteins string[] testProteinSequences = new string[] { "PEPTIDERPEPTIDEKPEPTIDE", "TIDERPEPTIDEKPEP", "RPEPKTIDERPEPKTIDE", "EDITPEPKEDITPEPR", "PEPREDITPEPKEDIT", "EPPIERPETPDETKTDPEPIIRDE" }; #endregion #region Example PSMs List <SpectrumTuple> mergeSourcePsmSummary1 = new List <SpectrumTuple>() { // Group Source Spectrum Analysis Score Q List of Peptide@Charge/ScoreDivider new SpectrumTuple("/A/1", 1, 1, 1, 12, 0, "[C2H2O1]PEPTIDE@2/1 TIDERPEPTIDEK@4/2 EPPIER@1/3"), new SpectrumTuple("/A/1", 1, 2, 1, 23, 0, "PEPTIDER@2/1 PETPDETK@3/3 EDITPEPK@2/5"), new SpectrumTuple("/A/1", 1, 3, 1, 34, 0, "PEPTIDEK@2/1 TIDER@1/4 PETPDETK@2/8"), new SpectrumTuple("/A/1", 2, 1, 1, 43, 0, "PEPTIDE@2/1 E[H-2O-1]DIT[P1O4]PEPR@2/2 EPPIER@1/7"), new SpectrumTuple("/A/1", 2, 2, 1, 32, 0, "PEPTIDER@3/1 EDITPEPK@3/4 EDITPEPR@3/5"), new SpectrumTuple("/A/1", 2, 3, 1, 21, 0, "PEPT[P1O4]IDEK@3/1 TIDEK@1/7 PETPDETK@2/8"), new SpectrumTuple("/A/2", 3, 1, 1, 56, 0, "TIDEK@2/1 TIDE@1/2 P[P1O4]EPTIDE@3/3"), new SpectrumTuple("/A/2", 3, 2, 1, 45, 0, "TIDER@2/1 TIDERPEPTIDEK@4/3 PEPTIDEK@3/4"), new SpectrumTuple("/A/2", 3, 3, 1, 34, 0, "TIDE@1/1 PEPTIDEK@3/6 TIDEK@1/7"), new SpectrumTuple("/B/1", 4, 1, 1, 65, 0, "TIDERPEPTIDEK@4/1 PETPDETK@3/8 EDITPEPR@3/9"), new SpectrumTuple("/B/1", 4, 2, 1, 53, 0, "E[H-2O-1]DITPEPK@2/1 PEPTIDEK@3/2 PEPTIDE@2/3"), new SpectrumTuple("/B/1", 4, 3, 1, 42, 0, "EDIT@2/1 PEPTIDEK@3/3 EDITPEPR@2/4"), new SpectrumTuple("/B/2", 5, 1, 1, 20, 0, "EPPIER@2/1 TIDE@1/7 PEPTIDE@2/9"), new SpectrumTuple("/B/2", 5, 2, 1, 24, 0, "PETPDETK@2/1 PEPTIDEK@3/5 EDITPEPR@2/8"), new SpectrumTuple("/B/2", 5, 3, 1, 24, 0, "PETPDETK@3/1 EDIT@1/4 TIDER@2/6"), }; List <SpectrumTuple> mergeSourcePsmSummary2 = new List <SpectrumTuple>() { new SpectrumTuple("/A/1", 1, 1, 2, 120, 0, "TIDERPEPTIDEK@4/1 PEPTIDE@2/2 EPPIER@1/3"), new SpectrumTuple("/A/1", 1, 2, 2, 230, 0, "PEPTIDER@2/1 PETPDETK@3/3 EDITPEPK@2/5"), new SpectrumTuple("/A/1", 1, 3, 2, 340, 0, "PEPTIDEK@2/1 TIDER@1/4 PETPDETK@2/8"), new SpectrumTuple("/A/1", 2, 1, 2, 430, 0, "PEPTIDE@2/1 EDITPEPR@2/2 EPPIER@1/7"), new SpectrumTuple("/A/1", 2, 2, 2, 320, 0, "PEPTIDER@3/1 EDITPEPK@3/4 EDITPEPR@3/5"), new SpectrumTuple("/A/1", 2, 3, 2, 210, 0, "PEPT[P1O4]IDEK@3/1 TIDEK@1/7 PETPDETK@2/8"), new SpectrumTuple("/A/2", 3, 1, 2, 560, 0, "TIDEK@2/1 TIDE@1/2 PEPTIDE@3/3"), new SpectrumTuple("/A/2", 3, 2, 2, 450, 0, "TIDER@2/1 TIDERPEPTIDEK@4/3 PEPTIDEK@3/4"), new SpectrumTuple("/A/2", 3, 3, 2, 340, 0, "TIDE@1/1 PEPTIDEK@3/6 TIDEK@1/7"), new SpectrumTuple("/B/1", 4, 1, 2, 650, 0, "TIDERPEPTIDEK@4/1 PET[P1O4]PDETK@3/8 EDITPEPR@3/9"), new SpectrumTuple("/B/1", 4, 2, 2, 530, 0, "EDITPEPK@2/1 PEPTIDEK@3/2 PEPTIDE@2/3"), new SpectrumTuple("/B/1", 4, 3, 2, 420, 0, "EDIT@2/1 PEPTIDEK@3/3 EDITPEPR@2/4"), new SpectrumTuple("/B/2", 5, 1, 2, 200, 0, "E[H-2O-1]PPIER@2/1 TIDE@1/7 PEPTIDE@2/9"), new SpectrumTuple("/B/2", 5, 2, 2, 240, 0, "PEPTIDEK@2/1 PETPDETK@2/4 EDITPEPR@2/8"), new SpectrumTuple("/B/2", 5, 3, 2, 240, 0, "PETPDETK@3/1 EDIT@1/4 TIDER@2/6"), }; var qonverterSettings1 = new QonverterSettings() { QonverterMethod = Qonverter.QonverterMethod.StaticWeighted, DecoyPrefix = "quiRKy", RerankMatches = true, ScoreInfoByName = new Dictionary <string, Qonverter.Settings.ScoreInfo>() { { "score1", new Qonverter.Settings.ScoreInfo() { Weight = 1, Order = Qonverter.Settings.Order.Ascending, NormalizationMethod = Qonverter.Settings.NormalizationMethod.Linear } }, { "score2", new Qonverter.Settings.ScoreInfo() { Weight = 42, Order = Qonverter.Settings.Order.Descending, NormalizationMethod = Qonverter.Settings.NormalizationMethod.Quantile } } } }; var qonverterSettings2 = new QonverterSettings() { QonverterMethod = Qonverter.QonverterMethod.SVM, DecoyPrefix = "___---", RerankMatches = false, ScoreInfoByName = new Dictionary <string, Qonverter.Settings.ScoreInfo>() { { "foo", new Qonverter.Settings.ScoreInfo() { Weight = 7, Order = Qonverter.Settings.Order.Ascending, NormalizationMethod = Qonverter.Settings.NormalizationMethod.Off } }, { "bar", new Qonverter.Settings.ScoreInfo() { Weight = 11, Order = Qonverter.Settings.Order.Descending, NormalizationMethod = Qonverter.Settings.NormalizationMethod.Off } } } }; #endregion File.Delete("testMergeSource1.idpDB"); using (var sessionFactory = SessionFactoryFactory.CreateSessionFactory("testMergeSource1.idpDB", new SessionFactoryConfig { CreateSchema = true })) using (var session = sessionFactory.OpenSession()) { TestModel.CreateTestProteins(session, testProteinSequences); TestModel.CreateTestData(session, mergeSourcePsmSummary1); TestModel.AddSubsetPeakData(session); qonverterSettings1.Analysis = session.UniqueResult <Analysis>(o => o.Software.Name == "Engine 1"); session.Save(qonverterSettings1); session.Flush(); } File.Delete("testMergeSource2.idpDB"); using (var sessionFactory = SessionFactoryFactory.CreateSessionFactory("testMergeSource2.idpDB", new SessionFactoryConfig { CreateSchema = true })) using (var session = sessionFactory.OpenSession()) { TestModel.CreateTestProteins(session, testProteinSequences); TestModel.CreateTestData(session, mergeSourcePsmSummary2); TestModel.AddSubsetPeakData(session); // copy is required because session.Save() takes ownership of the instance var qonverterSettings2Copy = new QonverterSettings() { Analysis = session.UniqueResult <Analysis>(o => o.Software.Name == "Engine 2"), QonverterMethod = qonverterSettings2.QonverterMethod, DecoyPrefix = qonverterSettings2.DecoyPrefix, RerankMatches = qonverterSettings2.RerankMatches, ScoreInfoByName = qonverterSettings2.ScoreInfoByName }; session.Save(qonverterSettings2Copy); session.Flush(); } // create a new merged idpDB from two idpDB files File.Delete("testMerger.idpDB"); Merger.Merge("testMerger.idpDB", new string[] { "testMergeSource1.idpDB", "testMergeSource2.idpDB" }); var testModel = new TestModel(); // test that testMerger.idpDB passes the TestModel tests using (var sessionFactory = SessionFactoryFactory.CreateSessionFactory("testMerger.idpDB")) using (var session = testModel.session = sessionFactory.OpenSession()) { testModel.TestOverallCounts(); testModel.TestSanity(); testModel.TestProteins(); testModel.TestPeptides(); testModel.TestPeptideInstances(); testModel.TestSpectrumSourceGroups(); testModel.TestSpectrumSources(false); testModel.TestSpectra(false); testModel.TestAnalyses(); testModel.TestPeptideSpectrumMatches(); testModel.TestModifications(); testModel.TestQonverterSettings(); } // create an in-memory representation of testMergeSource2 var memoryFactory = SessionFactoryFactory.CreateSessionFactory(":memory:", new SessionFactoryConfig { CreateSchema = true }); var memoryConnection = SessionFactoryFactory.CreateFile(":memory:"); var memorySession = memoryFactory.OpenSession(memoryConnection); { TestModel.CreateTestProteins(memorySession, testProteinSequences); TestModel.CreateTestData(memorySession, mergeSourcePsmSummary2); TestModel.AddSubsetPeakData(memorySession); qonverterSettings2.Analysis = memorySession.UniqueResult <Analysis>(o => o.Software.Name == "Engine 2"); memorySession.Save(qonverterSettings2); memorySession.Flush(); } // merge the in-memory connection into the testMergeSource1 file /*Merger.Merge("testMergeSource1.idpDB", memoryConnection as System.Data.SQLite.SQLiteConnection); * * // testMergeSource1.idpDB should pass just like testMerger.idpDB * using (var sessionFactory = SessionFactoryFactory.CreateSessionFactory("testMergeSource1.idpDB")) * using (var session = testModel.session = sessionFactory.OpenSession()) * { * testModel.TestOverallCounts(); * testModel.TestSanity(); * testModel.TestProteins(); * testModel.TestPeptides(); * testModel.TestPeptideInstances(); * testModel.TestSpectrumSourceGroups(); * testModel.TestSpectrumSources(false); * testModel.TestSpectra(false); * testModel.TestAnalyses(); * testModel.TestPeptideSpectrumMatches(); * testModel.TestModifications(); * testModel.TestQonverterSettings(); * }*/ }