private void addGroupToolStripMenuItem_Click(object sender, EventArgs e) { var selNode = (tlvBranch)tlvGroupedFiles.GetModelObject(_clickedItem.Index); _numberNewNodes++; var ssg = new SpectrumSourceGroup(); ssg.Name = (string.Format("{0}/New Group({1})", selNode.Text, _numberNewNodes).Replace(@"//", @"/")); var newNode = new tlvBranch { Text = Path.GetFileName(ssg.Name), cms = cmRightClickGroupNode, Parent = selNode, Children = new List <tlvBranch>(), Data = ssg }; selNode.Children.Add(newNode); OrganizeNode(selNode); tlvGroupedFiles.RefreshObject(selNode); if (selNode.Children.Count == 1) { tlvGroupedFiles.Expand(selNode); } tlvGroupedFiles.EditSubItem(tlvGroupedFiles.ModelToItem(newNode), 0); }
public static void CreateTestData (NHibernate.ISession session, IList<SpectrumTuple> testPsmSummary) { var dbGroups = new Map<string, SpectrumSourceGroup>(); foreach (var ssg in session.Query<SpectrumSourceGroup>()) dbGroups[ssg.Name] = ssg; var dbSources = new Map<long, SpectrumSource>(); foreach (var ss in session.Query<SpectrumSource>()) dbSources[ss.Id.Value] = ss; var dbAnalyses = new Map<long, Analysis>(); foreach (var a in session.Query<Analysis>()) dbAnalyses[a.Id.Value] = a; var dbPeptides = new Map<string, Peptide>(); foreach (var pep in session.Query<Peptide>()) dbPeptides[pep.Sequence] = pep; var bulkInserter = new BulkInserter(session.Connection); long lastPsmId = session.CreateQuery("SELECT MAX(Id) FROM PeptideSpectrumMatch").UniqueResult<long?>().GetValueOrDefault(); long lastModId = session.CreateQuery("SELECT MAX(Id) FROM Modification").UniqueResult<long?>().GetValueOrDefault(); long lastPmId = session.CreateQuery("SELECT MAX(Id) FROM PeptideModification").UniqueResult<long?>().GetValueOrDefault(); long lastGroupId = session.CreateQuery("SELECT MAX(Id) FROM SpectrumSourceGroup").UniqueResult<long?>().GetValueOrDefault(); long lastSourceId = session.CreateQuery("SELECT MAX(Id) FROM SpectrumSource").UniqueResult<long?>().GetValueOrDefault(); long lastSglId = session.CreateQuery("SELECT MAX(Id) FROM SpectrumSourceGroupLink").UniqueResult<long?>().GetValueOrDefault(); foreach (SpectrumTuple row in testPsmSummary) { string groupName = row.Group; string sourceName = "Source " + row.Source; string analysisId = "Engine " + row.Analysis; string peptideTuples = row.PeptideTuples; SpectrumSourceGroup group = dbGroups[groupName]; if (String.IsNullOrEmpty(group.Name)) { group.Id = ++lastGroupId; group.Name = groupName; bulkInserter.Add(group); } SpectrumSource source = dbSources[row.Source]; if (String.IsNullOrEmpty(source.Name)) { source.Id = ++lastSourceId; source.Name = sourceName; source.Group = group; source.Spectra = new List<Spectrum>(); bulkInserter.Add(source); // add a source group link for the source's immediate group bulkInserter.Add(new SpectrumSourceGroupLink() { Id = ++lastSglId, Group = group, Source = source }); #region add source group links for all of the immediate group's parent groups if (groupName != "/") { string parentGroupName = groupName.Substring(0, groupName.LastIndexOf("/")); while (true) { if (String.IsNullOrEmpty(parentGroupName)) parentGroupName = "/"; // add the parent group if it doesn't exist yet SpectrumSourceGroup parentGroup = session.UniqueResult<SpectrumSourceGroup>(o => o.Name == parentGroupName); if (parentGroup == null) { parentGroup = new SpectrumSourceGroup() { Id = ++lastGroupId, Name = parentGroupName }; bulkInserter.Add(parentGroup); } bulkInserter.Add(new SpectrumSourceGroupLink() { Id = ++lastSglId, Group = parentGroup, Source = source }); if (parentGroupName == "/") break; parentGroupName = parentGroupName.Substring(0, parentGroupName.LastIndexOf("/")); } } #endregion } Spectrum spectrum = source.Spectra.SingleOrDefault(o => o.Source.Id == source.Id && o.Index == row.Spectrum - 1); if (spectrum == null) { spectrum = new Spectrum() { Id = source.Id * 10000 + row.Spectrum, Index = row.Spectrum - 1, NativeID = "scan=" + row.Spectrum, Source = source, PrecursorMZ = 42 }; source.Spectra.Add(spectrum); bulkInserter.Add(spectrum); } Analysis analysis = dbAnalyses[row.Analysis]; if (String.IsNullOrEmpty(analysis.Name)) { analysis.Id = dbAnalyses.Max(o => o.Value.Id).GetValueOrDefault() + 1; analysis.Name = analysisId + " 1.0"; analysis.Software = new AnalysisSoftware() {Name = analysisId, Version = "1.0"}; analysis.StartTime = DateTime.Today.AddHours(row.Analysis); analysis.Type = AnalysisType.DatabaseSearch; analysis.Parameters = new SortedSet<AnalysisParameter>() { new AnalysisParameter() { Id = analysis.Id * 10000, Analysis = analysis, Name = "Parameter 1", Value = "Value 1" } }; bulkInserter.Add(analysis); } // make sure peptides are sorted by their score divider (which will determine rank) var peptideList = new SortedList<int, List<PeptideTuple>>(); foreach (string tuple in peptideTuples.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)) { var peptideTuple = new PeptideTuple() { Sequence = tuple.Split('@', '/')[0], Charge = Convert.ToInt32(tuple.Split('@', '/')[1]), ScoreDivider = Convert.ToInt32(tuple.Split('@', '/')[2]) }; if (!peptideList.ContainsKey(peptideTuple.ScoreDivider)) peptideList[peptideTuple.ScoreDivider] = new List<PeptideTuple>(); peptideList[peptideTuple.ScoreDivider].Add(peptideTuple); } int rank = 1; int lastDivider = 1; foreach (var peptideTupleList in peptideList.Values) foreach (var peptideTuple in peptideTupleList) { using (PwizPeptide pwizPeptide = new PwizPeptide(peptideTuple.Sequence, ModParsing.ModificationParsing_Auto, ModDelimiter.ModificationDelimiter_Brackets)) { Peptide peptide = dbPeptides[pwizPeptide.sequence]; if (String.IsNullOrEmpty(peptide.Sequence)) { peptide = new TestPeptide(pwizPeptide.sequence); peptide.Id = dbPeptides.Max(o => o.Value.Id).GetValueOrDefault() + 1; peptide.MonoisotopicMass = pwizPeptide.monoisotopicMass(false); peptide.MolecularWeight = pwizPeptide.molecularWeight(false); dbPeptides[pwizPeptide.sequence] = peptide; bulkInserter.Add(peptide); createTestPeptideInstances(session, bulkInserter, peptide); } double neutralPrecursorMass = (spectrum.PrecursorMZ*peptideTuple.Charge) - (peptideTuple.Charge*Proton.Mass); var psm = new PeptideSpectrumMatch() { Id = ++lastPsmId, Peptide = peptide, Spectrum = spectrum, Analysis = analysis, ObservedNeutralMass = neutralPrecursorMass, MonoisotopicMassError = neutralPrecursorMass - pwizPeptide.monoisotopicMass(), MolecularWeightError = neutralPrecursorMass - pwizPeptide.molecularWeight(), Charge = peptideTuple.Charge, Rank = (peptideTuple.ScoreDivider == lastDivider ? rank : ++rank), QValue = (rank == 1 ? row.QValue : PeptideSpectrumMatch.DefaultQValue), }; if (row.Score != null) psm.Scores = new Dictionary<string, double>() { {"score1", (double) row.Score/peptideTuple.ScoreDivider}, {"score2", 1/((double) row.Score/peptideTuple.ScoreDivider)} }; bulkInserter.Add(psm); lastDivider = peptideTuple.ScoreDivider; // add PeptideModifications and Modifications foreach (KeyValuePair<int, ModList> itr in pwizPeptide.modifications()) { foreach (PwizMod pwizMod in itr.Value) { Modification mod = session.UniqueResult<Modification>(o => o.Formula == pwizMod.formula()); if (mod == null) { mod = new Modification() { Id = ++lastModId, Formula = pwizMod.formula(), MonoMassDelta = pwizMod.monoisotopicDeltaMass(), AvgMassDelta = pwizMod.averageDeltaMass(), Name = pwizMod.formula() }; bulkInserter.Add(mod); } bulkInserter.Add(new PeptideModification() { Id = ++lastPmId, PeptideSpectrumMatch = psm, Modification = mod, Offset = itr.Key == ModMap.NTerminus() ? int.MinValue : itr.Key == ModMap.CTerminus() ? int.MaxValue : itr.Key }); } } } } } bulkInserter.Execute(); bulkInserter.Reset(""); }
private List<SpectrumSourceGroup> applyAssemblyText(ISession session, string filepath) { var spectrumSources = session.Query<SpectrumSource>().ToList(); var sourcesByGroup = new Map<string, List<SpectrumSource>>(); var alreadyGroupedSources = new Set<string>(); var sourceGroups = new List<SpectrumSourceGroup>(); // open the assembly.txt file using (var assembleTxtFile = File.OpenText(filepath)) { string line; while ((line = assembleTxtFile.ReadLine()) != null) { if (line.Length == 0) continue; try { Regex groupFilemaskPair = new Regex("((\"(.+)\")|(\\S+))\\s+((\"(.+)\")|(\\S+))"); Match lineMatch = groupFilemaskPair.Match(line); string group = lineMatch.Groups[3].ToString() + lineMatch.Groups[4].ToString(); string filemask = lineMatch.Groups[7].ToString() + lineMatch.Groups[8].ToString(); // for wildcards, use old style behavior if (filemask.IndexOfAny("*?".ToCharArray()) > -1) { if (!Path.IsPathRooted(filemask)) filemask = Path.Combine(Path.GetDirectoryName(filepath), filemask); if (!sourcesByGroup.Contains(group)) sourcesByGroup[group] = new List<SpectrumSource>(); if (!Directory.Exists(Path.GetDirectoryName(filemask))) continue; var files = Directory.GetFiles(Path.GetDirectoryName(filemask), Path.GetFileName(filemask)); var sourceNames = files.Select(o => Path.GetFileNameWithoutExtension(o)); foreach (string sourceName in sourceNames) { var spectrumSource = spectrumSources.SingleOrDefault(o => o.Name == sourceName); if (spectrumSource == null) continue; var insertResult = alreadyGroupedSources.Insert(sourceName); if (insertResult.WasInserted) sourcesByGroup[group].Add(spectrumSource); } } else { // otherwise, match directly to source names string sourceName = Path.GetFileNameWithoutExtension(filemask); var spectrumSource = spectrumSources.SingleOrDefault(o => o.Name == sourceName); if (spectrumSource == null) continue; var insertResult = alreadyGroupedSources.Insert(sourceName); if (insertResult.WasInserted) sourcesByGroup[group].Add(spectrumSource); } } catch (Exception ex) { Program.HandleException(new Exception("Error reading assembly text from \"" + filepath + "\": " + ex.Message, ex)); } } } // remove existing groups RemoveGroupNode(_rootNode, false); sourceGroups.Add(new SpectrumSourceGroup { Name = "/" }); // build new group hierarchy foreach (var itr in sourcesByGroup) { if (itr.Value.IsNullOrEmpty()) continue; var ssg = new SpectrumSourceGroup { Name = itr.Key }; if (!alreadyGroupedSources.Contains(ssg.Name)) sourceGroups.Add(ssg); // decompose group path into segments, e.g. /foo/bar/ -> {foo, bar} IEnumerable<string> segments = ssg.Name.Split("/".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); segments = segments.Take(segments.Count() - 1); // ignore the last segment var parentNode = _rootNode; foreach(string segment in segments) { var segmentNode = parentNode.Children.FirstOrDefault(o => o.Text == segment); if (segmentNode == null) { var segmentGroup = new SpectrumSourceGroup { Name = (parentNode.Text + "/").Replace("//", "/") + segment }; if (!alreadyGroupedSources.Contains(segmentGroup.Name)) sourceGroups.Add(segmentGroup); segmentNode = new tlvBranch { Text = segment, cms = cmRightClickGroupNode, Parent = parentNode, Children = new List<tlvBranch>(), Data = segmentGroup }; parentNode.Children.Add(segmentNode); } parentNode = segmentNode; } // parentNode is now the immediate parent of the current group var groupNode = new tlvBranch { Text = Path.GetFileName(ssg.Name), cms = cmRightClickGroupNode, Parent = parentNode, Children = new List<tlvBranch>(), Data = ssg }; foreach (var source in itr.Value) { var sourceNode = new tlvBranch { Text = Path.GetFileName(source.Name), Parent = groupNode, Data = source, cms = cmRightClickFileNode }; groupNode.Children.Add(sourceNode); lvNonGroupedFiles.Items.RemoveByKey(sourceNode.Text); } parentNode.Children.Add(groupNode); } tlvGroupedFiles.RefreshObject(_rootNode); tlvGroupedFiles.ExpandAll(); return sourceGroups.ToList(); }
private void addGroupToolStripMenuItem_Click(object sender, EventArgs e) { var selNode = (tlvBranch)tlvGroupedFiles.GetModelObject(_clickedItem.Index); _numberNewNodes++; var ssg = new SpectrumSourceGroup(); ssg.Name = (string.Format("{0}/New Group({1})", selNode.Text, _numberNewNodes).Replace(@"//",@"/")); var newNode = new tlvBranch { Text = Path.GetFileName(ssg.Name), cms = cmRightClickGroupNode, Parent = selNode, Children = new List<tlvBranch>(), Data = ssg }; selNode.Children.Add(newNode); OrganizeNode(selNode); tlvGroupedFiles.RefreshObject(selNode); if (selNode.Children.Count == 1) tlvGroupedFiles.Expand(selNode); tlvGroupedFiles.EditSubItem(tlvGroupedFiles.ModelToItem(newNode),0); }
private List <SpectrumSourceGroup> applyAssemblyText(ISession session, string filepath) { var spectrumSources = session.Query <SpectrumSource>().ToList(); var sourcesByGroup = new Map <string, List <SpectrumSource> >(); var alreadyGroupedSources = new Set <string>(); var sourceGroups = new List <SpectrumSourceGroup>(); // open the assembly.txt file using (var assembleTxtFile = File.OpenText(filepath)) { string line; while ((line = assembleTxtFile.ReadLine()) != null) { if (line.Length == 0) { continue; } try { Regex groupFilemaskPair = new Regex("((\"(.+)\")|(\\S+))\\s+((\"(.+)\")|(\\S+))"); Match lineMatch = groupFilemaskPair.Match(line); string group = lineMatch.Groups[3].ToString() + lineMatch.Groups[4].ToString(); string filemask = lineMatch.Groups[7].ToString() + lineMatch.Groups[8].ToString(); group = group.TrimEnd(' ', '/'); // for wildcards, use old style behavior if (filemask.IndexOfAny("*?".ToCharArray()) > -1) { if (!Path.IsPathRooted(filemask)) { filemask = Path.Combine(Path.GetDirectoryName(filepath), filemask); } if (!sourcesByGroup.Contains(group)) { sourcesByGroup[group] = new List <SpectrumSource>(); } if (!Directory.Exists(Path.GetDirectoryName(filemask))) { continue; } var files = Directory.GetFiles(Path.GetDirectoryName(filemask), Path.GetFileName(filemask)); var sourceNames = files.Select(o => Path.GetFileNameWithoutExtension(o)); foreach (string sourceName in sourceNames) { var spectrumSource = spectrumSources.SingleOrDefault(o => o.Name == sourceName); if (spectrumSource == null) { continue; } var insertResult = alreadyGroupedSources.Insert(sourceName); if (insertResult.WasInserted) { sourcesByGroup[group].Add(spectrumSource); } } } else { // otherwise, match directly to source names string sourceName = Path.GetFileNameWithoutExtension(filemask); var spectrumSource = spectrumSources.SingleOrDefault(o => o.Name == sourceName); if (spectrumSource == null) { continue; } var insertResult = alreadyGroupedSources.Insert(sourceName); if (insertResult.WasInserted) { sourcesByGroup[group].Add(spectrumSource); } } } catch (Exception ex) { Program.HandleException(new Exception("Error reading assembly text from \"" + filepath + "\": " + ex.Message, ex)); } } } // remove existing groups RemoveGroupNode(_rootNode, false); sourceGroups.Add(new SpectrumSourceGroup { Name = "/" }); // build new group hierarchy foreach (var itr in sourcesByGroup) { if (itr.Value.IsNullOrEmpty()) { continue; } var ssg = new SpectrumSourceGroup { Name = itr.Key }; if (!alreadyGroupedSources.Contains(ssg.Name)) { sourceGroups.Add(ssg); } // decompose group path into segments, e.g. /foo/bar/ -> {foo, bar} IEnumerable <string> segments = ssg.Name.Split("/".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); segments = segments.Take(segments.Count() - 1); // ignore the last segment var parentNode = _rootNode; foreach (string segment in segments) { var segmentNode = parentNode.Children.FirstOrDefault(o => o.Text == segment); if (segmentNode == null) { var segmentGroup = new SpectrumSourceGroup { Name = (parentNode.Text + "/").Replace("//", "/") + segment }; if (!alreadyGroupedSources.Contains(segmentGroup.Name)) { sourceGroups.Add(segmentGroup); } segmentNode = new tlvBranch { Text = segment, cms = cmRightClickGroupNode, Parent = parentNode, Children = new List <tlvBranch>(), Data = segmentGroup }; parentNode.Children.Add(segmentNode); } parentNode = segmentNode; } // parentNode is now the immediate parent of the current group var groupNode = new tlvBranch { Text = Path.GetFileName(ssg.Name), cms = cmRightClickGroupNode, Parent = parentNode, Children = new List <tlvBranch>(), Data = ssg }; foreach (var source in itr.Value) { var sourceNode = new tlvBranch { Text = Path.GetFileName(source.Name), Parent = groupNode, Data = source, cms = cmRightClickFileNode }; groupNode.Children.Add(sourceNode); lvNonGroupedFiles.Items.RemoveByKey(sourceNode.Text); } parentNode.Children.Add(groupNode); } tlvGroupedFiles.RefreshObject(_rootNode); tlvGroupedFiles.ExpandAll(); return(sourceGroups.ToList()); }
public void Add (SpectrumSourceGroup ssg) { insertRow(Table.SpectrumSourceGroup, new object[] {ssg.Id, ssg.Name}); }