/// <summary> /// Write to the SQLite database /// </summary> /// <param name="database"></param> /// <param name="options"></param> /// <param name="path"></param> public void Write(TargetDatabase database, Options options, string path) { DatabaseFactory.DatabaseFile = path; var databaseDirectory = Path.GetDirectoryName(path); /********************************************************************************************** * TODO: Get the append capability working * Set to false to avoid problems. Setting it to true will append some items, but not others. **********************************************************************************************/ DatabaseFactory.ReadOrAppend = false; var sessionFactory = DatabaseFactory.CreateSessionFactory(options.DatabaseType); using (var session = sessionFactory.OpenStatelessSession()) { // populate the database using (var transaction = session.BeginTransaction()) { session.Insert(options); /* This section breaks up the Target object, pulling out the individual TargetDataSet, SequenceInfo, * and TargetPeptideInfo. These objects are then "reverse linked", so that each of these objects * relates to multiple evidences. This is because these objects need to know what they are related to. * Additionally, these objects are saved before the Evidences are, because these objects need to already * exist in order to properly generate the relation. * */ var currentTarget = 0; var currentEv = 0; var datasetCount = 0; var total = database.ConsensusTargets.Count; foreach (var consensusTarget in database.ConsensusTargets) { OnProgressChanged(new MtdbProgressChangedEventArgs(currentTarget, total, MtdbCreationProgressType.COMMIT.ToString())); consensusTarget.Id = ++currentTarget; foreach (var ptm in consensusTarget.Ptms) { ptm.Id = 0; } consensusTarget.Dataset = consensusTarget.Evidences[0].DataSet; consensusTarget.ModificationCount = consensusTarget.Evidences[0].ModificationCount; consensusTarget.ModificationDescription = consensusTarget.Evidences[0].ModificationDescription; consensusTarget.MultiProteinCount = consensusTarget.Evidences[0].MultiProteinCount; session.Insert(consensusTarget); foreach (var evidence in consensusTarget.Evidences) { if (!m_uniqueDataSets.ContainsKey(evidence.DataSet.Name)) { evidence.DataSet.Id = ++datasetCount; m_uniqueDataSets.Add(evidence.DataSet.Name, evidence.DataSet); var outputPath = databaseDirectory + evidence.DataSet.Name + "Alignment.tsv"; var datasetWriter = new StreamWriter(databaseDirectory + "\\" + evidence.DataSet.Name + "Alignment.tsv"); datasetWriter.WriteLine("GANET_Obs\tScan_Number"); m_alignmentWriters.Add(evidence.DataSet.Name, datasetWriter); session.Insert(evidence.DataSet); } Evidence writtenEvidence = new Evidence { Id = ++currentEv, Charge = evidence.Charge, ObservedNet = evidence.ObservedNet, NetShift = evidence.NetShift, Mz = evidence.Mz, Scan = evidence.Scan, DelM = evidence.DelM, DelMPpm = evidence.DelMPpm, DiscriminantValue = evidence.DiscriminantValue, SpecProb = evidence.SpecProb, DataSet = m_uniqueDataSets[evidence.DataSet.Name], Parent = consensusTarget }; m_alignmentWriters[evidence.DataSet.Name].WriteLine(string.Format("{0}\t{1}", writtenEvidence.ObservedNet, writtenEvidence.Scan)); if (writtenEvidence.DiscriminantValue > 0.0) { writtenEvidence.DiscriminantValue += 0.0; } session.Insert(writtenEvidence); } foreach (var protein in consensusTarget.Proteins) { if (!m_uniqueProteins.ContainsKey(protein.ProteinName)) { protein.Id = 0; m_uniqueProteins.Add(protein.ProteinName, protein); session.Insert(protein); } var cProt = m_uniqueProteins[protein.ProteinName]; var cPPair = new ConsensusProteinPair { Consensus = consensusTarget, Protein = cProt, CleavageState = (short)cProt.CleavageState, TerminusState = (short)cProt.TerminusState, ResidueStart = (short)cProt.ResidueStart, ResidueEnd = (short)cProt.ResidueEnd }; session.Insert(cPPair); consensusTarget.ConsensusProtein.Add(cPPair); } foreach (var ptm in consensusTarget.Ptms) { if (!m_uniquePtms.ContainsKey(ptm.Name)) { m_uniquePtms.Add(ptm.Name, ptm); session.Insert(ptm); } var cPtmPair = new ConsensusPtmPair { Location = ptm.Location, PostTranslationalModification = m_uniquePtms[ptm.Name], PtmId = m_uniquePtms[ptm.Name].Id, Target = consensusTarget, ConsensusId = consensusTarget.Id }; session.Insert(cPtmPair); } } OnProgressChanged(new MtdbProgressChangedEventArgs(currentTarget, total, MtdbCreationProgressType.COMMIT.ToString())); transaction.Commit(); session.Close(); } } foreach (var writer in m_alignmentWriters) { writer.Value.Close(); } }
private void ReadSqLite(string path) { // Don't read again if we just read the file if (path == m_lastReadFile) { return; } // Reset the data m_targetDb.ClearTargets(); m_lcmsDataDic.Clear(); //var sessionFactory = DatabaseReaderFactory.CreateSessionFactory(path); DatabaseFactory.DatabaseFile = path; DatabaseFactory.ReadOrAppend = true; var sessionFactory = DatabaseFactory.CreateSessionFactory(DatabaseType.SQLite); var readConsensus = new List <ConsensusTarget>(); var readPair = new List <ConsensusProteinPair>(); var readProt = new List <ProteinInformation>(); var readEvidence = new List <Evidence>(); var readPtms = new List <PostTranslationalModification>(); var readPtmPairs = new List <ConsensusPtmPair>(); var readOptions = new List <Options>(); var consensusDic = new Dictionary <int, ConsensusTarget>(); var consensusProtDic = new Dictionary <int, List <ConsensusProteinPair> >(); var consensusPtmDic = new Dictionary <int, List <ConsensusPtmPair> >(); var protDic = new Dictionary <int, ProteinInformation>(); var ptmDic = new Dictionary <int, PostTranslationalModification>(); using (var session = sessionFactory.OpenStatelessSession()) { using (var transact = session.BeginTransaction()) { session.CreateCriteria <ProteinInformation>().List(readProt); session.CreateCriteria <ConsensusTarget>().List(readConsensus); session.CreateCriteria <PostTranslationalModification>().List(readPtms); session.CreateCriteria <Options>().List(readOptions); session.CreateCriteria <ConsensusProteinPair>().List(readPair); session.CreateCriteria <ConsensusPtmPair>().List(readPtmPairs); session.CreateCriteria <Evidence>().List(readEvidence); transact.Commit(); } /* * using (var transact = session.BeginTransaction()) * { * session.CreateCriteria<ConsensusProteinPair>().List(readPair); * session.CreateCriteria<ConsensusPtmPair>().List(readPtmPairs); * session.CreateCriteria<Evidence>().List(readEvidence); * transact.Commit(); * } */ foreach (var consensus in readConsensus) { consensus.Ptms.Clear(); //consensus.Evidences.Clear(); consensus.Evidences = new List <Evidence>(); consensus.Sequence = consensus.CleanSequence; m_targetDb.AddConsensusTarget(consensus); consensusDic.Add(consensus.Id, consensus); } foreach (var pair in readPair) { if (!consensusProtDic.ContainsKey(pair.Consensus.Id)) { consensusProtDic.Add(pair.Consensus.Id, new List <ConsensusProteinPair>()); } consensusProtDic[pair.Consensus.Id].Add(pair); } foreach (var pair in readPtmPairs) { if (!consensusPtmDic.ContainsKey(pair.Target.Id)) { consensusPtmDic.Add(pair.Target.Id, new List <ConsensusPtmPair>()); } consensusPtmDic[pair.Target.Id].Add(pair); } foreach (var prot in readProt) { protDic.Add(prot.Id, prot); } foreach (var ptm in readPtms) { ptmDic.Add(ptm.Id, ptm); } foreach (var consensus in consensusPtmDic) { foreach (var pair in consensus.Value) { var ptm = new PostTranslationalModification { Mass = ptmDic[pair.PostTranslationalModification.Id].Mass, Name = ptmDic[pair.PostTranslationalModification.Id].Name, Formula = ptmDic[pair.PostTranslationalModification.Id].Formula, Location = pair.Location, Parent = consensusDic[pair.Target.Id] }; consensusDic[pair.Target.Id].Ptms.Add(ptm); } } foreach (var evidence in readEvidence) { foreach (var pair in consensusProtDic[evidence.Parent.Id]) { var prot = protDic[pair.Protein.Id]; prot.ResidueEnd = pair.ResidueEnd; prot.ResidueStart = pair.ResidueStart; prot.TerminusState = (clsPeptideCleavageStateCalculator.ePeptideTerminusStateConstants)pair.TerminusState; prot.CleavageState = (clsPeptideCleavageStateCalculator.ePeptideCleavageStateConstants)pair.CleavageState; //prot.Id = 0; evidence.AddProtein(prot); consensusDic[evidence.Parent.Id].AddProtein(prot); } evidence.MonoisotopicMass = consensusDic[evidence.Parent.Id].TheoreticalMonoIsotopicMass; evidence.Ptms = consensusDic[evidence.Parent.Id].Ptms; if (!m_lcmsDataDic.ContainsKey(evidence.DataSet.Name)) { var dataset = new LcmsDataSet(true); m_lcmsDataDic.Add(evidence.DataSet.Name, dataset); m_lcmsDataDic[evidence.DataSet.Name].Name = evidence.DataSet.Name; m_lcmsDataDic[evidence.DataSet.Name].Tool = evidence.DataSet.Tool; } m_lcmsDataDic[evidence.DataSet.Name].Evidences.Add(evidence); consensusDic[evidence.Parent.Id].AddEvidence(evidence); } } // Set the member variable to avoid double reads. m_lastReadFile = path; }