private bool ImportTransitionList(CommandArgs commandArgs) { _out.WriteLine(Resources.CommandLine_ImportTransitionList_Importing_transiton_list__0____, Path.GetFileName(commandArgs.TransitionListPath)); IdentityPath selectPath; List<MeasuredRetentionTime> irtPeptides; List<SpectrumMzInfo> librarySpectra; List<TransitionImportErrorInfo> errorList; List<PeptideGroupDocNode> peptideGroups; var retentionTimeRegression = _doc.Settings.PeptideSettings.Prediction.RetentionTime; RCalcIrt calcIrt = retentionTimeRegression != null ? (retentionTimeRegression.Calculator as RCalcIrt) : null; var progressMonitor = new CommandProgressMonitor(_out, new ProgressStatus(string.Empty)); var inputs = new MassListInputs(commandArgs.TransitionListPath); var docNew = _doc.ImportMassList(inputs, progressMonitor, null, out selectPath, out irtPeptides, out librarySpectra, out errorList, out peptideGroups); // If nothing was imported (e.g. operation was canceled or zero error-free transitions) and also no errors, just return if (ReferenceEquals(docNew, _doc) && !errorList.Any()) return true; // Show the errors or as warnings, if error transitions are ignore if (errorList.Any()) { string messageFormat = !commandArgs.IsIgnoreTransitionErrors ? Resources.CommandLine_ImportTransitionList_Error___line__0___column__1____2_ : Resources.CommandLine_ImportTransitionList_Warning___line__0___column__1____2_; foreach (var errorMessage in errorList) { _out.WriteLine(messageFormat, errorMessage.Row, errorMessage.Column, errorMessage.ErrorMessage); } if (!commandArgs.IsIgnoreTransitionErrors) return false; } if (!commandArgs.IsTransitionListAssayLibrary) { _doc = docNew; return true; } if (irtPeptides.Count == 0 || librarySpectra.Count == 0) { if (irtPeptides.Any()) _out.WriteLine(Resources.CommandLine_ImportTransitionList_Error__Imported_assay_library__0__lacks_ion_abundance_values_); else if (librarySpectra.Any()) _out.WriteLine(Resources.CommandLine_ImportTransitionList_Error__Imported_assay_library__0__lacks_iRT_values_); else _out.WriteLine(Resources.CommandLine_ImportTransitionList_Error__Imported_assay_library__0__lacks_iRT_and_ion_abundance_values_); return false; } string destinationPath = commandArgs.SaveFile ?? commandArgs.SkylineFile; string documentLibrary = BiblioSpecLiteSpec.GetLibraryFileName(destinationPath); // ReSharper disable once AssignNullToNotNullAttribute string outputLibraryPath = Path.Combine(Path.GetDirectoryName(documentLibrary), Path.GetFileNameWithoutExtension(documentLibrary) + BiblioSpecLiteSpec.ASSAY_NAME + BiblioSpecLiteSpec.EXT); bool libraryExists = File.Exists(outputLibraryPath); string libraryName = Path.GetFileNameWithoutExtension(destinationPath) + BiblioSpecLiteSpec.ASSAY_NAME; int indexOldLibrary = docNew.Settings.PeptideSettings.Libraries.LibrarySpecs.IndexOf( spec => spec != null && spec.FilePath == outputLibraryPath); bool libraryLinkedToDoc = indexOldLibrary != -1; if (libraryExists && !libraryLinkedToDoc) { _out.WriteLine(Resources.CommandLine_ImportTransitionList_Error__There_is_an_existing_library_with_the_same_name__0__as_the_document_library_to_be_created_, libraryName); return false; } var dbIrtPeptides = irtPeptides.Select(rt => new DbIrtPeptide(rt.PeptideSequence, rt.RetentionTime, false, TimeSource.scan)).ToList(); var dbIrtPeptidesFilter = ImportAssayLibraryHelper.GetUnscoredIrtPeptides(dbIrtPeptides, calcIrt); // If there are no iRT peptides with different values than the database, don't import any iRT's bool checkPeptides = false; if (dbIrtPeptidesFilter.Any()) { if (calcIrt == null) { string irtDatabasePath = commandArgs.IrtDatabasePath; if (string.IsNullOrEmpty(irtDatabasePath)) irtDatabasePath = Path.ChangeExtension(destinationPath, IrtDb.EXT); if (!string.IsNullOrEmpty(commandArgs.IrtStandardsPath)) { _out.WriteLine(Resources.CommandLine_ImportTransitionList_Importing_iRT_transition_list__0_, commandArgs.IrtStandardsPath); var irtInputs = new MassListInputs(commandArgs.IrtStandardsPath); try { List<SpectrumMzInfo> irtLibrarySpectra; docNew = docNew.ImportMassList(irtInputs, null, out selectPath, out irtPeptides, out irtLibrarySpectra, out errorList); if (errorList.Any()) { throw new InvalidDataException(errorList[0].ErrorMessage); } librarySpectra.AddRange(irtLibrarySpectra); dbIrtPeptidesFilter.AddRange(irtPeptides.Select(rt => new DbIrtPeptide(rt.PeptideSequence, rt.RetentionTime, true, TimeSource.scan))); } catch (Exception x) { _out.WriteLine(Resources.CommandLine_Run_Error__Failed_importing_the_file__0____1_, commandArgs.IrtStandardsPath, x.Message); return false; } if (!CreateIrtDatabase(irtDatabasePath, commandArgs)) return false; } else if (!string.IsNullOrEmpty(commandArgs.IrtGroupName)) { var nodeGroupIrt = docNew.PeptideGroups.FirstOrDefault(nodeGroup => nodeGroup.Name == commandArgs.IrtGroupName); if (nodeGroupIrt == null) { _out.WriteLine(Resources.CommandLine_ImportTransitionList_Error__The_name__0__specified_with__1__was_not_found_in_the_imported_assay_library_, commandArgs.IrtGroupName, CommandArgs.ArgText(CommandArgs.ARG_IRT_STANDARDS_GROUP_NAME)); return false; } var irtPeptideSequences = new HashSet<string>(nodeGroupIrt.Peptides.Select(pep => pep.ModifiedSequence)); dbIrtPeptidesFilter.ForEach(pep => pep.Standard = irtPeptideSequences.Contains(pep.PeptideModSeq)); if (!CreateIrtDatabase(irtDatabasePath, commandArgs)) return false; } else if (!File.Exists(irtDatabasePath)) { _out.Write(Resources.CommandLine_ImportTransitionList_Error__To_create_the_iRT_database___0___for_this_assay_library__you_must_specify_the_iRT_standards_using_either_of_the_arguments__1__or__2_, irtDatabasePath, CommandArgs.ArgText(CommandArgs.ARG_IRT_STANDARDS_GROUP_NAME), CommandArgs.ArgText(CommandArgs.ARG_IRT_STANDARDS_FILE)); return false; } else { checkPeptides = true; } string irtCalcName = commandArgs.IrtCalcName ?? Path.GetFileNameWithoutExtension(destinationPath); calcIrt = new RCalcIrt(irtCalcName, irtDatabasePath); retentionTimeRegression = new RetentionTimeRegression(calcIrt.Name, calcIrt, null, null, 10, new List<MeasuredRetentionTime>()); docNew = docNew.ChangeSettings(docNew.Settings.ChangePeptidePrediction(prediction => prediction.ChangeRetentionTime(retentionTimeRegression))); } string dbPath = calcIrt.DatabasePath; IrtDb db = IrtDb.GetIrtDb(dbPath, null); if (checkPeptides) { var standards = docNew.Molecules.Where(m => db.IsStandard(m.RawTextId)).ToArray(); if (standards.Length != db.StandardPeptideCount) { _out.WriteLine(Resources.CommandLine_ImportTransitionList_Warning__The_document_is_missing_iRT_standards); foreach (var rawTextId in db.StandardPeptides.Where(s => !standards.Contains(nodePep => s == nodePep.RawTextId))) { _out.WriteLine(" " + rawTextId); // Not L10N } } } var oldPeptides = db.GetPeptides().ToList(); IList<DbIrtPeptide.Conflict> conflicts; dbIrtPeptidesFilter = DbIrtPeptide.MakeUnique(dbIrtPeptidesFilter); DbIrtPeptide.FindNonConflicts(oldPeptides, dbIrtPeptidesFilter, null, out conflicts); // Warn about peptides that are present in the import and already in the database foreach (var conflict in conflicts) { _out.WriteLine(Resources.CommandLine_ImportTransitionList_Warning__The_iRT_calculator_already_contains__0__with_the_value__1___Ignoring__2_, conflict.ExistingPeptide.PeptideModSeq, conflict.ExistingPeptide.Irt, conflict.NewPeptide.Irt); } _out.WriteLine(Resources.CommandLine_ImportTransitionList_Importing__0__iRT_values_into_the_iRT_calculator__1_, dbIrtPeptidesFilter.Count, calcIrt.Name); docNew = docNew.AddIrtPeptides(dbIrtPeptidesFilter, false, progressMonitor); if (docNew == null) return false; } librarySpectra = SpectrumMzInfo.RemoveDuplicateSpectra(librarySpectra); if (libraryLinkedToDoc) { string oldName = docNew.Settings.PeptideSettings.Libraries.LibrarySpecs[indexOldLibrary].Name; var libraryOld = docNew.Settings.PeptideSettings.Libraries.GetLibrary(oldName); var additionalSpectra = SpectrumMzInfo.GetInfoFromLibrary(libraryOld); additionalSpectra = SpectrumMzInfo.RemoveDuplicateSpectra(additionalSpectra); librarySpectra = SpectrumMzInfo.MergeWithOverwrite(librarySpectra, additionalSpectra); foreach (var stream in libraryOld.ReadStreams) stream.CloseStream(); } if (librarySpectra.Any()) { // Delete the existing library; either it's not tied to the document or we've already extracted the spectra _out.WriteLine(Resources.CommandLine_ImportTransitionList_Adding__0__spectra_to_the_library__1_, librarySpectra.Count, libraryName); if (libraryExists) { FileEx.SafeDelete(outputLibraryPath); FileEx.SafeDelete(Path.ChangeExtension(outputLibraryPath, BiblioSpecLiteSpec.EXT_REDUNDANT)); } using (var blibDb = BlibDb.CreateBlibDb(outputLibraryPath)) { var docLibrarySpec = new BiblioSpecLiteSpec(libraryName, outputLibraryPath); var docLibrary = blibDb.CreateLibraryFromSpectra(docLibrarySpec, librarySpectra, libraryName, progressMonitor); if (docLibrary == null) return false; var newSettings = docNew.Settings.ChangePeptideLibraries( libs => libs.ChangeLibrary(docLibrary, docLibrarySpec, indexOldLibrary)); docNew = docNew.ChangeSettings(newSettings, new SrmSettingsChangeMonitor(progressMonitor, Resources.SkylineWindow_ImportMassList_Finishing_up_import)); } } _doc = docNew; return true; }
public void OkDialog() { IrtType irtType = GetIrtType(); if (textCalculatorName.Text.Length == 0) { MessageDlg.Show(this, Resources.CreateIrtCalculatorDlg_OkDialog_Calculator_name_cannot_be_empty); return; } if (_existing.Select(spec => spec.Name).Contains(textCalculatorName.Text)) { var replaceResult = MultiButtonMsgDlg.Show(this, Resources.CreateIrtCalculatorDlg_OkDialog_A_calculator_with_that_name_already_exists___Do_you_want_to_replace_it_, MultiButtonMsgDlg.BUTTON_YES, MultiButtonMsgDlg.BUTTON_NO, false); if (replaceResult == DialogResult.No) return; } if (irtType == IrtType.existing) { try { if (!File.Exists(textOpenDatabase.Text)) { MessageDlg.Show(this, Resources.CreateIrtCalculatorDlg_OkDialog_iRT_database_field_must_contain_a_path_to_a_valid_file_); textOpenDatabase.Focus(); return; } var db = IrtDb.GetIrtDb(textOpenDatabase.Text, null); if (db == null) { throw new DatabaseOpeningException(string.Format(Resources.CreateIrtCalculatorDlg_OkDialog_Cannot_read_the_database_file__0_, textOpenDatabase.Text)); } } catch (Exception x) { MessageDlg.ShowWithException(this, string.Format(Resources.CreateIrtCalculatorDlg_OkDialog_Failed_to_open_the_database_file___0_, x.Message), x); return; } } else if (irtType == IrtType.separate_list) { if (textNewDatabase.Text.Length == 0) { MessageDlg.Show(this, Resources.CreateIrtCalculatorDlg_OkDialog_iRT_database_field_must_not_be_empty_); textNewDatabase.Focus(); return; } if (!CreateIrtDatabase(textNewDatabase.Text)) return; } else { if (textNewDatabaseProteins.Text.Length == 0) { MessageDlg.Show(this, Resources.CreateIrtCalculatorDlg_OkDialog_iRT_database_field_must_not_be_empty_); textNewDatabaseProteins.Focus(); return; } if (comboBoxProteins.SelectedIndex == -1) { MessageDlg.Show(this, Resources.CreateIrtCalculatorDlg_OkDialog_Please_select_a_protein_containing_the_list_of_standard_peptides_for_the_iRT_calculator_); comboBoxProteins.Focus(); return; } if (!CreateIrtDatabase(textNewDatabaseProteins.Text)) return; } // Make a version of the document with the new calculator in it var databaseFileName = irtType == IrtType.existing ? textOpenDatabase.Text : irtType == IrtType.separate_list ? textNewDatabase.Text : textNewDatabaseProteins.Text; var calculator = new RCalcIrt(textCalculatorName.Text, databaseFileName); // CONSIDER: Probably can't use just a static default like 10 below var retentionTimeRegression = new RetentionTimeRegression(calculator.Name, calculator, null, null, 10, new List<MeasuredRetentionTime>()); var docNew = Document.ChangeSettings(Document.Settings.ChangePeptidePrediction(prediction => prediction.ChangeRetentionTime(retentionTimeRegression))); // Import transition list of standards, if applicable if (irtType == IrtType.separate_list) { try { if (!File.Exists(textImportText.Text)) { MessageDlg.Show(this, Resources.CreateIrtCalculatorDlg_OkDialog_Transition_list_field_must_contain_a_path_to_a_valid_file_); return; } IdentityPath selectPath; List<MeasuredRetentionTime> irtPeptides; List<TransitionImportErrorInfo> errorList; var inputs = new MassListInputs(textImportText.Text); docNew = docNew.ImportMassList(inputs, null, out selectPath, out irtPeptides, out _librarySpectra, out errorList); if (errorList.Any()) { throw new InvalidDataException(errorList[0].ErrorMessage); } _dbIrtPeptides = irtPeptides.Select(rt => new DbIrtPeptide(rt.PeptideSequence, rt.RetentionTime, true, TimeSource.scan)).ToList(); IrtFile = textImportText.Text; } catch (Exception x) { MessageDlg.ShowWithException(this, string.Format(Resources.CreateIrtCalculatorDlg_OkDialog_Error_reading_iRT_standards_transition_list___0_, x.Message), x); return; } } else if (irtType == IrtType.protein) { PeptideGroupDocNode selectedGroup = comboBoxProteins.SelectedItem as PeptideGroupDocNode; // ReSharper disable PossibleNullReferenceException _irtPeptideSequences = new HashSet<string>(selectedGroup.Peptides.Select(pep => pep.ModifiedSequence)); // ReSharper restore PossibleNullReferenceException } Document = docNew; DialogResult = DialogResult.OK; }
private SrmDocument AddTransitionList(SrmDocument document, ref IdentityPath selectedPath) { if (tabControl1.SelectedTab != tabPageTransitionList) return document; if (IsMolecule) { // Save the current column order to settings var active = new List<string>(); for (int order = 0; order < gridViewTransitionList.Columns.Count; order++) { for (int gridcol = 0; gridcol < gridViewTransitionList.Columns.Count; gridcol++) { var dataGridViewColumn = gridViewTransitionList.Columns[gridcol]; if (dataGridViewColumn.DisplayIndex == order) { if (dataGridViewColumn.Visible) active.Add(dataGridViewColumn.Name); break; } } } Settings.Default.CustomMoleculeTransitionInsertColumnsList = active; // We will accept a completely empty product list as meaning // "these are all precursor transitions" var requireProductInfo = false; for (var i = 0; i < gridViewTransitionList.RowCount - 1; i++) { var row = gridViewTransitionList.Rows[i]; var productMz = row.Cells[INDEX_PRODUCT_MZ].Value; var productFormula = row.Cells[INDEX_PRODUCT_FORMULA].Value; var productCharge = row.Cells[INDEX_PRODUCT_CHARGE].Value; if ((productMz != null && productMz.ToString().Length > 0) || (productFormula != null && productFormula.ToString().Length > 0) || (productCharge != null && productCharge.ToString().Length > 0)) { requireProductInfo = true; // Product list is not completely empty break; } } // For each row in the grid, add to or begin MoleculeGroup|Molecule|TransitionList tree for(int i = 0; i < gridViewTransitionList.RowCount - 1; i ++) { DataGridViewRow row = gridViewTransitionList.Rows[i]; var precursor = ReadPrecursorOrProductColumns(document, row, true); // Get molecule values if (precursor == null) return null; if (requireProductInfo && ReadPrecursorOrProductColumns(document, row, false) == null) { return null; } var charge = precursor.Charge; var precursorMonoMz = BioMassCalc.CalculateIonMz(precursor.MonoMass, charge); var precursorAverageMz = BioMassCalc.CalculateIonMz(precursor.AverageMass, charge); // Preexisting molecule group? bool pepGroupFound = false; foreach (var pepGroup in document.MoleculeGroups) { var pathPepGroup = new IdentityPath(pepGroup.Id); if (Equals(pepGroup.Name, Convert.ToString(row.Cells[INDEX_MOLECULE_GROUP].Value))) { // Found a molecule group with the same name - can we find an existing transition group to which we can add a transition? pepGroupFound = true; bool pepFound = false; foreach (var pep in pepGroup.SmallMolecules) { var pepPath = new IdentityPath(pathPepGroup, pep.Id); var ionMonoMz = BioMassCalc.CalculateIonMz(pep.CustomIon.MonoisotopicMass, charge); var ionAverageMz = BioMassCalc.CalculateIonMz(pep.CustomIon.AverageMass, charge); // Match existing molecule if same name (if any) and same formula (if any) and similar m/z at the precursor charge // (we don't just check mass since we don't have a tolerance value for that) // Or same name If any) and identical formula when stripped of labels // Or same name, no formula, and different isotope labels if (Equals(pep.CustomIon.Name, precursor.Name) && ((Equals(pep.CustomIon.Formula, precursor.Formula) && Math.Abs(ionMonoMz - precursorMonoMz) <= document.Settings.TransitionSettings.Instrument.MzMatchTolerance && Math.Abs(ionAverageMz - precursorAverageMz) <= document.Settings.TransitionSettings.Instrument.MzMatchTolerance) || (!Equals(pep.CustomIon.Formula, precursor.Formula) && Equals(pep.CustomIon.UnlabeledFormula, BioMassCalc.MONOISOTOPIC.StripLabelsFromFormula(precursor.Formula))) || (string.IsNullOrEmpty(pep.CustomIon.Formula) && string.IsNullOrEmpty(precursor.Formula) && !pep.TransitionGroups.Any(t => Equals(t.TransitionGroup.LabelType, precursor.IsotopeLabelType??IsotopeLabelType.light))) )) { pepFound = true; bool tranGroupFound = false; foreach (var tranGroup in pep.TransitionGroups) { var pathGroup = new IdentityPath(pepPath, tranGroup.Id); if (Math.Abs(tranGroup.PrecursorMz - precursor.Mz) <= document.Settings.TransitionSettings.Instrument.MzMatchTolerance) { tranGroupFound = true; var tranFound = false; try { var tranNode = GetMoleculeTransition(document, row, pep.Peptide, tranGroup.TransitionGroup, requireProductInfo); if (tranNode == null) return null; foreach (var tran in tranGroup.Transitions) { if (Equals(tranNode.Transition.CustomIon,tran.Transition.CustomIon)) { tranFound = true; break; } } if (!tranFound) { document = (SrmDocument) document.Add(pathGroup, tranNode); } } catch (InvalidDataException e) { // Some error we didn't catch in the basic checks ShowTransitionError(new PasteError { Column = 0, Line = row.Index, Message = e.Message }); return null; } break; } } if (!tranGroupFound) { var node = GetMoleculeTransitionGroup(document, row, pep.Peptide, requireProductInfo); if (node == null) return null; document = (SrmDocument) document.Add(pepPath, node); } break; } } if (!pepFound) { var node = GetMoleculePeptide(document, row, pepGroup.PeptideGroup, requireProductInfo); if (node == null) return null; document = (SrmDocument) document.Add(pathPepGroup,node); } break; } } if (!pepGroupFound) { var node = GetMoleculePeptideGroup(document, row, requireProductInfo); if (node == null) return null; IdentityPath first; IdentityPath next; document = document.AddPeptideGroups(new[] {node}, false,null , out first,out next); } } } else { var backgroundProteome = GetBackgroundProteome(document); var sbTransitionList = new StringBuilder(); var dictNameSeq = new Dictionary<string, FastaSequence>(); // Add all existing FASTA sequences in the document to the name to seq dictionary // Including named peptide lists would cause the import code to give matching names // in this list new names (e.g. with 1, 2, 3 appended). In this code, the names // are intended to be merged. foreach (var nodePepGroup in document.Children.Cast<PeptideGroupDocNode>().Where(n => !n.IsPeptideList)) { if (!dictNameSeq.ContainsKey(nodePepGroup.Name)) dictNameSeq.Add(nodePepGroup.Name, (FastaSequence) nodePepGroup.PeptideGroup); } // Check for simple errors and build strings for import for (int i = 0; i < gridViewTransitionList.Rows.Count; i++) { var row = gridViewTransitionList.Rows[i]; var peptideSequence = Convert.ToString(row.Cells[colTransitionPeptide.Index].Value); var proteinName = Convert.ToString(row.Cells[colTransitionProteinName.Index].Value); var precursorMzText = Convert.ToString(row.Cells[colTransitionPrecursorMz.Index].Value); var productMzText = Convert.ToString(row.Cells[colTransitionProductMz.Index].Value); if (string.IsNullOrEmpty(peptideSequence) && string.IsNullOrEmpty(proteinName)) { continue; } if (string.IsNullOrEmpty(peptideSequence)) { ShowTransitionError(new PasteError { Column = colTransitionPeptide.Index, Line = i, Message = Resources.PasteDlg_ListPeptideSequences_The_peptide_sequence_cannot_be_blank }); return null; } if (!FastaSequence.IsExSequence(peptideSequence)) { ShowTransitionError(new PasteError { Column = colTransitionPeptide.Index, Line = i, Message = Resources.PasteDlg_ListPeptideSequences_This_peptide_sequence_contains_invalid_characters }); return null; } double mz; if (!double.TryParse(precursorMzText, out mz)) { ShowTransitionError(new PasteError { Column = colTransitionPrecursorMz.Index, Line = i, Message = Resources.PasteDlg_AddTransitionList_The_precursor_m_z_must_be_a_number_ }); return null; } if (!double.TryParse(productMzText, out mz)) { ShowTransitionError(new PasteError { Column = colTransitionProductMz.Index, Line = i, Message = Resources.PasteDlg_AddTransitionList_The_product_m_z_must_be_a_number_ }); return null; } const char sep = TRANSITION_LIST_SEPARATOR; // Add columns in order specified by TRANSITION_LIST_COL_INDICES sbTransitionList .Append(proteinName).Append(sep) .Append(peptideSequence).Append(sep) .Append(precursorMzText).Append(sep) .Append(productMzText).AppendLine(); // Build FASTA sequence text in cases where it is known if (!dictNameSeq.ContainsKey(proteinName)) { var fastaSeq = backgroundProteome.GetFastaSequence(proteinName); if (fastaSeq != null) dictNameSeq.Add(proteinName, fastaSeq); } } if (sbTransitionList.Length == 0) return document; // Do the actual import into PeptideGroupDocNodes IEnumerable<PeptideGroupDocNode> peptideGroupDocNodes; try { List<TransitionImportErrorInfo> errorList; List<MeasuredRetentionTime> irtPeptides; List<SpectrumMzInfo> librarySpectra; var inputs = new MassListInputs(sbTransitionList.ToString(), LocalizationHelper.CurrentCulture, TRANSITION_LIST_SEPARATOR); var importer = new MassListImporter(document, inputs); // TODO: support long-wait broker peptideGroupDocNodes = importer.Import(null, TRANSITION_LIST_COL_INDICES, dictNameSeq, out irtPeptides, out librarySpectra, out errorList); if (errorList.Any()) { var firstError = errorList[0]; if (firstError.Row.HasValue) { throw new LineColNumberedIoException(firstError.ErrorMessage, firstError.Row.Value, firstError.Column ?? -1); } else { throw new InvalidDataException(firstError.ErrorMessage); } } } catch (LineColNumberedIoException x) { var columns = new[] { colTransitionProteinName, colPeptideSequence, colTransitionPrecursorMz, colTransitionProductMz }; ShowTransitionError(new PasteError { Column = x.ColumnIndex >= 0 ? columns[x.ColumnIndex].Index : 0, Line = (int) x.LineNumber - 1, Message = x.PlainMessage }); return null; } catch (InvalidDataException x) { ShowTransitionError(new PasteError { Message = x.Message }); return null; } // Insert the resulting nodes into the document tree, merging when possible bool after = false; foreach (var nodePepGroup in peptideGroupDocNodes) { PeptideGroupDocNode nodePepGroupExist = FindPeptideGroupDocNode(document, nodePepGroup); if (nodePepGroupExist != null) { var nodePepGroupNew = nodePepGroupExist.Merge(nodePepGroup); if (!ReferenceEquals(nodePepGroupExist, nodePepGroupNew)) document = (SrmDocument) document.ReplaceChild(nodePepGroupNew); } else { // Add to the end, if no insert node var to = selectedPath; if (to == null || to.Depth < (int) SrmDocument.Level.MoleculeGroups) document = (SrmDocument) document.Add(nodePepGroup); else { Identity toId = selectedPath.GetIdentity((int) SrmDocument.Level.MoleculeGroups); document = (SrmDocument) document.Insert(toId, nodePepGroup, after); } selectedPath = new IdentityPath(nodePepGroup.Id); // All future insertions should be after, to avoid reversing the list after = true; } } } return document; }
public void VariableModImportListTest() { // TODO: Deal with multiple variable modifications that produce the same // precursor mass but different product ion masses SrmDocument document = new SrmDocument(SrmSettingsList.GetDefault()); var staticMods = new List<StaticMod>(document.Settings.PeptideSettings.Modifications.StaticModifications); staticMods.AddRange(new[] { new StaticMod("Met Sulfoxide", "M", null, true, "O", LabelAtoms.None, RelativeRT.Matching, null, null, new[] { new FragmentLoss("SOCH4") }), new StaticMod("Met Sulfone", "M", null, true, "O2", LabelAtoms.None, RelativeRT.Matching, null, null, null), new StaticMod("Phospho", "S,T,Y", null, true, "PO3H", LabelAtoms.None, RelativeRT.Matching, null, null, new[] {new FragmentLoss("PO4H3")}), new StaticMod("K(GlyGly)", "K", null, true, "N2H6C4O2", LabelAtoms.None, RelativeRT.Matching, null, null, null), }); document = document.ChangeSettings(document.Settings.ChangePeptideModifications(mods => mods.ChangeStaticModifications(staticMods))); IdentityPath pathTo; List<MeasuredRetentionTime> irtPeptides; List<SpectrumMzInfo> librarySpectra; List<TransitionImportErrorInfo> errorList; var inputsPhospho = new MassListInputs(TEXT_PHOSPHO_TRANSITION_LIST, CultureInfo.InvariantCulture, TextUtil.SEPARATOR_CSV); document.ImportMassList(inputsPhospho, null, out pathTo, out irtPeptides, out librarySpectra, out errorList); Assert.AreEqual(27, errorList.Count); AssertEx.AreComparableStrings(TextUtil.SpaceSeparate(Resources.MassListRowReader_CalcTransitionExplanations_The_product_m_z__0__is_out_of_range_for_the_instrument_settings__in_the_peptide_sequence__1_, Resources.MassListRowReader_CalcPrecursorExplanations_Check_the_Instrument_tab_in_the_Transition_Settings), errorList[0].ErrorMessage, 2); Assert.AreEqual(1, errorList[0].Column); Assert.AreEqual(40, errorList[0].Row); var docHighMax = document.ChangeSettings(document.Settings.ChangeTransitionInstrument(inst => inst.ChangeMaxMz(1800))); var docList = docHighMax.ImportMassList(inputsPhospho, null, out pathTo); AssertEx.Serializable(docList); AssertEx.IsDocumentState(docList, 3, 68, 134, 157, 481); foreach (var nodeTran in docList.PeptideTransitions) { var it = nodeTran.Transition.IonType; Assert.IsTrue(it == IonType.y || it == IonType.b || it == IonType.precursor, "Found unexpected non b, y or precursor ion type."); } var inputsMultiLoss = new MassListInputs(TEXT_PHOSPHO_MULTI_LOSS, CultureInfo.InvariantCulture, TextUtil.SEPARATOR_CSV); docHighMax.ImportMassList(inputsMultiLoss, null, out pathTo, out irtPeptides, out librarySpectra, out errorList); Assert.AreEqual(5, errorList.Count); AssertEx.AreComparableStrings(Resources.MassListRowReader_CalcTransitionExplanations_Product_m_z_value__0__in_peptide__1__has_no_matching_product_ion, errorList[0].ErrorMessage, 2); Assert.AreEqual(1, errorList[0].Column); Assert.AreEqual(2, errorList[0].Row); var docMultiLos = docHighMax.ChangeSettings(docHighMax.Settings.ChangePeptideModifications(mods => mods.ChangeMaxNeutralLosses(2))); docList = docMultiLos.ImportMassList(inputsMultiLoss, null, out pathTo); AssertEx.IsDocumentState(docList, 4, 4, 4, 12); }
private static SrmDocument InitWiffDocument(TestFilesDir testFilesDir) { const LabelAtoms labelAtoms = LabelAtoms.C13 | LabelAtoms.N15; List<StaticMod> heavyMods = new List<StaticMod> { new StaticMod("Heavy K", "K", ModTerminus.C, null, labelAtoms, null, null), new StaticMod("Heavy R", "R", ModTerminus.C, null, labelAtoms, null, null), }; SrmSettings settings = SrmSettingsList.GetDefault(); settings = settings.ChangePeptideModifications(mods => mods.ChangeHeavyModifications(heavyMods)); SrmDocument doc = new SrmDocument(settings); IdentityPath selectPath; string path = testFilesDir.GetTestPath("051309_transition list.csv"); // Product m/z out of range var docError = doc; List<MeasuredRetentionTime> irtPeptides; List<SpectrumMzInfo> librarySpectra; List<TransitionImportErrorInfo> errorList; var inputs = new MassListInputs(path) { FormatProvider = CultureInfo.InvariantCulture, Separator = TextUtil.SEPARATOR_CSV }; docError.ImportMassList(inputs, null, out selectPath, out irtPeptides, out librarySpectra, out errorList); Assert.AreEqual(errorList.Count, 1); AssertEx.AreComparableStrings(TextUtil.SpaceSeparate(Resources.MassListRowReader_CalcTransitionExplanations_The_product_m_z__0__is_out_of_range_for_the_instrument_settings__in_the_peptide_sequence__1_, Resources.MassListRowReader_CalcPrecursorExplanations_Check_the_Instrument_tab_in_the_Transition_Settings), errorList[0].ErrorMessage, 2); Assert.AreEqual(errorList[0].Column, 1); Assert.AreEqual(errorList[0].Row, 19); doc = doc.ChangeSettings(settings.ChangeTransitionInstrument(inst => inst.ChangeMaxMz(1800))); inputs = new MassListInputs(path) { FormatProvider = CultureInfo.InvariantCulture, Separator = TextUtil.SEPARATOR_CSV }; doc = doc.ImportMassList(inputs, null, out selectPath); AssertEx.IsDocumentState(doc, 2, 9, 9, 18, 54); return doc; }
private void ImportMassList(MassListInputs inputs, string description) { SrmTreeNode nodePaste = SequenceTree.SelectedNode as SrmTreeNode; IdentityPath insertPath = nodePaste != null ? nodePaste.Path : null; IdentityPath selectPath = null; List<MeasuredRetentionTime> irtPeptides = null; List<SpectrumMzInfo> librarySpectra = null; List<TransitionImportErrorInfo> errorList = null; List<PeptideGroupDocNode> peptideGroups = null; RetentionTimeRegression retentionTimeRegressionStore = null; var docCurrent = DocumentUI; SrmDocument docNew = null; var retentionTimeRegression = docCurrent.Settings.PeptideSettings.Prediction.RetentionTime; RCalcIrt calcIrt = retentionTimeRegression != null ? (retentionTimeRegression.Calculator as RCalcIrt) : null; using (var longWaitDlg = new LongWaitDlg(this) {Text = description}) { longWaitDlg.PerformWork(this, 1000, longWaitBroker => { docNew = docCurrent.ImportMassList(inputs, longWaitBroker, insertPath, out selectPath, out irtPeptides, out librarySpectra, out errorList, out peptideGroups); }); } bool isDocumentSame = ReferenceEquals(docNew, docCurrent); // If nothing was imported (e.g. operation was canceled or zero error-free transitions) and also no errors, just return if (isDocumentSame && !errorList.Any()) return; // Show the errors, giving the option to accept the transitions without errors, // if there are any if (errorList.Any()) { using (var errorDlg = new ImportTransitionListErrorDlg(errorList, isDocumentSame)) { if (errorDlg.ShowDialog(this) == DialogResult.Cancel || isDocumentSame) { return; } } } var dbIrtPeptides = irtPeptides.Select(rt => new DbIrtPeptide(rt.PeptideSequence, rt.RetentionTime, false, TimeSource.scan)).ToList(); var dbIrtPeptidesFilter = ImportAssayLibraryHelper.GetUnscoredIrtPeptides(dbIrtPeptides, calcIrt); bool overwriteExisting = false; MassListInputs irtInputs = null; // If there are no iRT peptides or none with different values than the database, don't import any iRT's if (dbIrtPeptidesFilter.Any()) { // Ask whether or not to include iRT peptides in the paste string useIrtMessage = calcIrt == null ? Resources.SkylineWindow_ImportMassList_The_transition_list_appears_to_contain_iRT_values__but_the_document_does_not_have_an_iRT_calculator___Create_a_new_calculator_and_add_these_iRT_values_ : Resources.SkylineWindow_ImportMassList_The_transition_list_appears_to_contain_iRT_library_values___Add_these_iRT_values_to_the_iRT_calculator_; string yesButton = calcIrt == null ? Resources.SkylineWindow_ImportMassList__Create___ : Resources.SkylineWindow_ImportMassList_Add; var useIrtResult = MultiButtonMsgDlg.Show(this, useIrtMessage, yesButton, Resources.SkylineWindow_ImportMassList__Skip, true); if (useIrtResult == DialogResult.Cancel) { return; } if (useIrtResult == DialogResult.Yes) { if (calcIrt == null) { // If there is no iRT calculator, ask the user to create one using (var dlg = new CreateIrtCalculatorDlg(docNew, DocumentFilePath, Settings.Default.RTScoreCalculatorList, peptideGroups)) { if (dlg.ShowDialog(this) != DialogResult.OK) { return; } docNew = dlg.Document; calcIrt = (RCalcIrt)docNew.Settings.PeptideSettings.Prediction.RetentionTime.Calculator; dlg.UpdateLists(librarySpectra, dbIrtPeptidesFilter); if (!string.IsNullOrEmpty(dlg.IrtFile)) irtInputs = new MassListInputs(dlg.IrtFile); } } string dbPath = calcIrt.DatabasePath; IrtDb db = File.Exists(dbPath) ? IrtDb.GetIrtDb(dbPath, null) : IrtDb.CreateIrtDb(dbPath); var oldPeptides = db.GetPeptides().ToList(); IList<DbIrtPeptide.Conflict> conflicts; dbIrtPeptidesFilter = DbIrtPeptide.MakeUnique(dbIrtPeptidesFilter); DbIrtPeptide.FindNonConflicts(oldPeptides, dbIrtPeptidesFilter, null, out conflicts); // Ask whether to keep or overwrite peptides that are present in the import and already in the database if (conflicts.Any()) { string messageOverwrite = string.Format(Resources.SkylineWindow_ImportMassList_The_iRT_calculator_already_contains__0__of_the_imported_peptides_, conflicts.Count); var overwriteResult = MultiButtonMsgDlg.Show(this, TextUtil.LineSeparate(messageOverwrite, conflicts.Count == 1 ? Resources.SkylineWindow_ImportMassList_Keep_the_existing_iRT_value_or_overwrite_with_the_imported_value_ : Resources.SkylineWindow_ImportMassList_Keep_the_existing_iRT_values_or_overwrite_with_imported_values_), Resources.SkylineWindow_ImportMassList__Keep, Resources.SkylineWindow_ImportMassList__Overwrite, true); if (overwriteResult == DialogResult.Cancel) { return; } overwriteExisting = overwriteResult == DialogResult.No; } using (var longWaitDlg = new LongWaitDlg(this) { Text = Resources.SkylineWindow_ImportMassList_Adding_iRT_values_}) { longWaitDlg.PerformWork(this, 100, progressMonitor => docNew = docNew.AddIrtPeptides(dbIrtPeptidesFilter, overwriteExisting, progressMonitor)); } if (docNew == null) return; retentionTimeRegressionStore = docNew.Settings.PeptideSettings.Prediction.RetentionTime; } } BiblioSpecLiteSpec docLibrarySpec = null; BiblioSpecLiteLibrary docLibrary = null; int indexOldLibrary = -1; if (librarySpectra.Any()) { string addLibraryMessage = Resources.SkylineWindow_ImportMassList_The_transition_list_appears_to_contain_spectral_library_intensities___Create_a_document_library_from_these_intensities_; var addLibraryResult = MultiButtonMsgDlg.Show(this, addLibraryMessage, Resources.SkylineWindow_ImportMassList__Create___, Resources.SkylineWindow_ImportMassList__Skip, true); if (addLibraryResult == DialogResult.Cancel) { return; } if (addLibraryResult == DialogResult.Yes) { // Can't name a library after the document if the document is unsaved // In this case, prompt to save if (DocumentFilePath == null) { string saveDocumentMessage = Resources.SkylineWindow_ImportMassList_You_must_save_the_Skyline_document_in_order_to_create_a_spectral_library_from_a_transition_list_; var saveDocumentResult = MultiButtonMsgDlg.Show(this, saveDocumentMessage, MultiButtonMsgDlg.BUTTON_OK); if (saveDocumentResult == DialogResult.Cancel) { return; } else if (!SaveDocumentAs()) { return; } } librarySpectra = SpectrumMzInfo.RemoveDuplicateSpectra(librarySpectra); string documentLibrary = BiblioSpecLiteSpec.GetLibraryFileName(DocumentFilePath); // ReSharper disable once AssignNullToNotNullAttribute string outputPath = Path.Combine(Path.GetDirectoryName(documentLibrary), Path.GetFileNameWithoutExtension(documentLibrary) + BiblioSpecLiteSpec.ASSAY_NAME + BiblioSpecLiteSpec.EXT); bool libraryExists = File.Exists(outputPath); string name = Path.GetFileNameWithoutExtension(DocumentFilePath) + BiblioSpecLiteSpec.ASSAY_NAME; indexOldLibrary = docNew.Settings.PeptideSettings.Libraries.LibrarySpecs.IndexOf(spec => spec != null && spec.FilePath == outputPath); bool libraryLinkedToDoc = indexOldLibrary != -1; if (libraryLinkedToDoc) { string oldName = docNew.Settings.PeptideSettings.Libraries.LibrarySpecs[indexOldLibrary].Name; var libraryOld = docNew.Settings.PeptideSettings.Libraries.GetLibrary(oldName); var additionalSpectra = SpectrumMzInfo.GetInfoFromLibrary(libraryOld); additionalSpectra = SpectrumMzInfo.RemoveDuplicateSpectra(additionalSpectra); librarySpectra = SpectrumMzInfo.MergeWithOverwrite(librarySpectra, additionalSpectra); foreach (var stream in libraryOld.ReadStreams) stream.CloseStream(); } if (libraryExists && !libraryLinkedToDoc) { string replaceLibraryMessage = string.Format(Resources.SkylineWindow_ImportMassList_There_is_an_existing_library_with_the_same_name__0__as_the_document_library_to_be_created___Overwrite_this_library_or_skip_import_of_library_intensities_, name); // If the document does not have an assay library linked to it, then ask if user wants to delete the one that we have found var replaceLibraryResult = MultiButtonMsgDlg.Show(this, replaceLibraryMessage, Resources.SkylineWindow_ImportMassList__Overwrite, Resources.SkylineWindow_ImportMassList__Skip, true); if (replaceLibraryResult == DialogResult.Cancel) return; if (replaceLibraryResult == DialogResult.No) librarySpectra.Clear(); } if (librarySpectra.Any()) { // Delete the existing library; either it's not tied to the document or we've already extracted the spectra if (libraryExists) { FileEx.SafeDelete(outputPath); FileEx.SafeDelete(Path.ChangeExtension(outputPath, BiblioSpecLiteSpec.EXT_REDUNDANT)); } using (var blibDb = BlibDb.CreateBlibDb(outputPath)) { docLibrarySpec = new BiblioSpecLiteSpec(name, outputPath); using (var longWaitDlg = new LongWaitDlg(this) { Text = Resources.SkylineWindow_ImportMassList_Creating_Spectral_Library }) { longWaitDlg.PerformWork(this, 1000, progressMonitor => { docLibrary = blibDb.CreateLibraryFromSpectra(docLibrarySpec, librarySpectra, name, progressMonitor); if (docLibrary == null) return; var newSettings = docNew.Settings.ChangePeptideLibraries(libs => libs.ChangeLibrary(docLibrary, docLibrarySpec, indexOldLibrary)); var status = new ProgressStatus(Resources.SkylineWindow_ImportMassList_Finishing_up_import); progressMonitor.UpdateProgress(status); progressMonitor.UpdateProgress(status.ChangePercentComplete(100)); docNew = docNew.ChangeSettings(newSettings); }); if (docLibrary == null) return; } } } } } ModifyDocument(description, doc => { if (ReferenceEquals(doc, docCurrent)) return docNew; try { // If the document was changed during the operation, try all the changes again // using the information given by the user. docCurrent = DocumentUI; doc = doc.ImportMassList(inputs, insertPath, out selectPath); if (irtInputs != null) { doc = doc.ImportMassList(irtInputs, null, out selectPath); } var newSettings = doc.Settings; if (retentionTimeRegressionStore != null) { newSettings = newSettings.ChangePeptidePrediction(prediction => prediction.ChangeRetentionTime(retentionTimeRegressionStore)); } if (docLibrarySpec != null) { newSettings = newSettings.ChangePeptideLibraries(libs => libs.ChangeLibrary(docLibrary, docLibrarySpec, indexOldLibrary)); } if (!ReferenceEquals(doc.Settings, newSettings)) doc = doc.ChangeSettings(newSettings); } catch (Exception x) { throw new InvalidDataException(string.Format(Resources.SkylineWindow_ImportMassList_Unexpected_document_change_during_operation___0_, x.Message, x)); } return doc; }); if (selectPath != null) SequenceTree.SelectedPath = selectPath; if (retentionTimeRegressionStore != null) { Settings.Default.RetentionTimeList.Add(retentionTimeRegressionStore); Settings.Default.RTScoreCalculatorList.Add(retentionTimeRegressionStore.Calculator); } if (docLibrarySpec != null) { Settings.Default.SpectralLibraryList.Insert(0, docLibrarySpec); } }
public MassListImporter(SrmDocument document, MassListInputs inputs) { Document = document; Inputs = inputs; }