public IList <string> WriteSpectra(MSDataFile.WriteConfig config) { var outputPaths = new List <string>(); foreach (SpectrumSource ss in session.Query <SpectrumSource>()) { if (ss.Metadata == null) { continue; } string outputSuffix; switch (config.format) { case MSDataFile.Format.Format_mzML: outputSuffix = ".mzML"; break; case MSDataFile.Format.Format_mzXML: outputSuffix = ".mzXML"; break; case MSDataFile.Format.Format_MGF: outputSuffix = ".mgf"; break; case MSDataFile.Format.Format_MS2: outputSuffix = ".ms2"; break; default: config.format = MSDataFile.Format.Format_mzML; outputSuffix = ".mzML"; break; } MSDataFile.write(ss.Metadata, ss.Name + outputSuffix, config); outputPaths.Add(ss.Name + outputSuffix); } return(outputPaths); }
private void writeSingleSpectrum(string sourceFile, string nativeID, string outFileName) { MSDataFile.WriteConfig writeConfig = new MSDataFile.WriteConfig(); writeConfig.format = MSDataFile.Format.Format_MGF; writeConfig.precision = MSDataFile.Precision.Precision_32; //var predicate = new SpectrumList_FilterPredicate_IndexSet(); //predicate.indexSet.Add(nativeID); var predicate = new SpectrumList_FilterPredicate_NativeIDSet(); predicate.nativeIDSet.Add(nativeID); try { //Workspace.SetText("\r\nWriting selected spectrum to " + outFileName); MainForm.SetText(this, "Start writing selected spectrum to " + outFileName + "\r\n"); using (MSDataFile msFile = new MSDataFile(sourceFile)) { msFile.run.spectrumList = new SpectrumList_Filter(msFile.run.spectrumList, new SpectrumList_FilterAcceptSpectrum(predicate.accept)); msFile.write(outFileName, writeConfig); } MainForm.SetText(this, "Finished writing selected spectrum to " + outFileName + "\r\n"); } catch (Exception exc) { //throw new Exception("Error in writiing new spectra file", exc); //Workspace.SetText("\r\nError in writing new spectra file\r\n"); tbPepNovoResult.AppendText("\r\nError in writing new spectra file\r\n"); throw new Exception(exc.Message); } }
void processFile(string filename, Config config, ReaderList readers) { // read in data file using (var msdList = new MSDataList()) { string msg = String.Format("Opening file \"{0}\" for read...", filename); if (LogUpdate != null) { LogUpdate(msg, _info); } if (StatusUpdate != null) { StatusUpdate(msg, ProgressBarStyle.Marquee, _info); } readers.read(filename, msdList); foreach (var msd in msdList) { var outputFilename = config.outputFilename(filename, msd); if (filename == outputFilename) { throw new ArgumentException("Output filepath is the same as input filepath"); } if (StatusUpdate != null) { StatusUpdate("Waiting...", ProgressBarStyle.Marquee, _info); } // only one thread lock (calculateSHA1Mutex) { if (LogUpdate != null) { LogUpdate("Calculating SHA1 checksum...", _info); } if (StatusUpdate != null) { StatusUpdate("Calculating SHA1 checksum...", ProgressBarStyle.Marquee, _info); } MSDataFile.calculateSHA1Checksums(msd); } if (LogUpdate != null) { LogUpdate("Processing...", _info); } if (StatusUpdate != null) { StatusUpdate("Processing...", ProgressBarStyle.Marquee, _info); } SpectrumListFactory.wrap(msd, config.Filters); if ((msd.run.spectrumList == null) || msd.run.spectrumList.empty()) { if ((msd.run.chromatogramList != null) && !msd.run.chromatogramList.empty()) { msg = "Note: input contains only chromatogram data."; switch (config.WriteConfig.format) { case MSDataFile.Format.Format_MZ5: case MSDataFile.Format.Format_mzML: break; default: msg += " The selected output format can only represent spectra. Consider using mzML instead."; break; } } else { msg = "Note: input contains no spectra or chromatogram data."; } if (LogUpdate != null) { LogUpdate(msg, _info); } if (StatusUpdate != null) { StatusUpdate(msg, ProgressBarStyle.Continuous, _info); } } if (StatusUpdate != null && msd.run.spectrumList != null) { StatusUpdate(String.Format("Processing ({0} of {1})", DataGridViewProgressCell.MessageSpecialValue.CurrentValue, DataGridViewProgressCell.MessageSpecialValue.Maximum), ProgressBarStyle.Continuous, _info); } // write out the new data file var ilr = new IterationListenerRegistry(); ilr.addListener(this, 100); msg = String.Format("Writing \"{0}\"...", outputFilename); if (LogUpdate != null) { LogUpdate(msg, _info); } if (StatusUpdate != null) { StatusUpdate(msg, ProgressBarStyle.Continuous, _info); } MSDataFile.write(msd, outputFilename, config.WriteConfig, ilr); ilr.removeListener(this); } } }
void processFile(string filename, Config config, ReaderList readers, Map <string, int> usedOutputFilenames) { // read in data file using (var msdList = new MSDataList()) { string msg = String.Format("Opening file \"{0}\" for read...", filename); var stripCredentialsMatch = Regex.Match(filename, "https?://([^:]+:[^@]+@).*", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase); if (stripCredentialsMatch.Success) { msg = msg.Replace(stripCredentialsMatch.Groups[1].Value, ""); } LogUpdate?.Invoke(msg, _info); StatusUpdate?.Invoke(msg, ProgressBarStyle.Marquee, _info); readers.read(filename, msdList, config.ReaderConfig); foreach (var msd in msdList) { try { var outputFilename = config.outputFilename(filename, msd); string deduplicatedFilename = outputFilename; StatusUpdate?.Invoke("Waiting...", ProgressBarStyle.Marquee, _info); // only one thread lock (_calculateSHA1Mutex) { // if output name is same as input name, add a suffix if (filename == outputFilename) { ++usedOutputFilenames[outputFilename]; } if (usedOutputFilenames.Contains(deduplicatedFilename)) { deduplicatedFilename = deduplicatedFilename.Replace(Path.GetExtension(outputFilename), String.Format(" ({0}).{1}", usedOutputFilenames[outputFilename] + 1, Path.GetExtension(outputFilename))); } ++usedOutputFilenames[outputFilename]; LogUpdate?.Invoke("Calculating SHA1 checksum...", _info); StatusUpdate?.Invoke("Calculating SHA1 checksum...", ProgressBarStyle.Marquee, _info); MSDataFile.calculateSHA1Checksums(msd); } var ilr = new IterationListenerRegistry(); ilr.addListenerWithTimer(this, 1); LogUpdate?.Invoke("Processing...", _info); StatusUpdate?.Invoke("Processing...", ProgressBarStyle.Marquee, _info); SpectrumListFactory.wrap(msd, config.Filters, ilr); config.WriteConfig.useWorkerThreads = msd.run.spectrumList.benefitsFromWorkerThreads(); if ((msd.run.spectrumList == null) || msd.run.spectrumList.empty()) { if ((msd.run.chromatogramList != null) && !msd.run.chromatogramList.empty()) { msg = "Note: input contains only chromatogram data."; switch (config.WriteConfig.format) { case MSDataFile.Format.Format_MZ5: case MSDataFile.Format.Format_mzML: break; default: msg += " The selected output format can only represent spectra. Consider using mzML instead."; break; } } else { msg = "Note: input contains no spectra or chromatogram data."; } LogUpdate?.Invoke(msg, _info); StatusUpdate?.Invoke(msg, ProgressBarStyle.Continuous, _info); } if (StatusUpdate != null && msd.run.spectrumList != null) { StatusUpdate(String.Format("Processing ({0} of {1})", DataGridViewProgressCell.MessageSpecialValue.CurrentValue, DataGridViewProgressCell.MessageSpecialValue.Maximum), ProgressBarStyle.Continuous, _info); } // write out the new data file msg = String.Format("Writing \"{0}\"...", deduplicatedFilename); LogUpdate?.Invoke(msg, _info); StatusUpdate?.Invoke(msg, ProgressBarStyle.Continuous, _info); MSDataFile.write(msd, deduplicatedFilename, config.WriteConfig, ilr); ilr.removeListener(this); } finally { msd.Dispose(); } } } }
/// <summary> /// write out a subset of high quality spectra based on ScanRanker metrics file /// using ProteoWizard library /// </summary> public void Write() { foreach (FileInfo file in inFileList) { string fileBaseName = Path.GetFileNameWithoutExtension(file.FullName); string metricsFileName = fileBaseName + metricsFileSuffix + ".txt"; string outFileName = fileBaseName + outFileSuffix + "." + outFormat; Workspace.SetText("\r\nStart writing high quality spectra for file: " + file.Name + " ...\r\n\r\n"); if (File.Exists(outFileName)) { File.Delete(outFileName); } if (!File.Exists(metricsFileName)) { Workspace.SetText("\r\nError: Cannot find quality metrics file: " + metricsFileName + " in output directory!"); Workspace.ChangeButtonTo("Close"); return; } //List<int> allIndices = new List<int>(); //List<int> highQualIndices = new List<int>(); List <string> allIndices = new List <string>(); List <string> highQualIndices = new List <string>(); // read metrics file, split and get high quality spectra indecies from the second column try { Workspace.SetText("\r\nExtracting scan index from metrics file: " + metricsFileName); using (TextReader tr = File.OpenText(metricsFileName)) { tr.ReadLine(); // read the header line but do nothing, first three lines are header tr.ReadLine(); // read the header line but do nothing tr.ReadLine(); // read the header line but do nothing string line = string.Empty; while ((line = tr.ReadLine()) != null) { string[] items = line.Split('\t'); //int index = Convert.ToInt32(items[1]); //index string index = Convert.ToString(items[1]); //nativeID if (!allIndices.Exists(element => element == index)) // remove duplicate index { allIndices.Add(index); } } } } catch (Exception exc) { //throw new Exception("Error in reading metrics file for spectra removal\r\n",exc); Workspace.SetText("\r\nError in reading metrics file for spectra removal\r\n"); Workspace.ChangeButtonTo("Close"); throw new Exception(exc.Message); } // get indices for high quality spectra Workspace.SetText("\r\nGenerating indices of high quality spectra"); int numOutputSpectra = Convert.ToInt32(allIndices.Count * cutoff); highQualIndices = allIndices.GetRange(0, numOutputSpectra); //highQualIndices.Sort(); //var predicate = new SpectrumList_FilterPredicate_IndexSet(); //foreach (int i in highQualIndices) //{ // predicate.indexSet.Add(i); //} var predicate = new SpectrumList_FilterPredicate_NativeIDSet(); foreach (string i in highQualIndices) { predicate.nativeIDSet.Add(i); } //var sorterPredicate = new SpectrumList_SorterPredicate_IndexSet(); //foreach (int i in highQualIndices) //{ // sorterPredicate.indexSet.Add(i); //} //MSDataFile.WriteConfig writeConfig = new MSDataFile.WriteConfig(MSDataFile.Format.Format_mzXML); MSDataFile.WriteConfig writeConfig = new MSDataFile.WriteConfig(); if (outFormat.Equals("mzXML") || outFormat.Equals("mzxml")) { writeConfig.format = MSDataFile.Format.Format_mzXML; } else if (outFormat.Equals("mzML") || outFormat.Equals("mzml")) { writeConfig.format = MSDataFile.Format.Format_mzML; } else if (outFormat.Equals("MGF") || outFormat.Equals("mgf")) { writeConfig.format = MSDataFile.Format.Format_MGF; } else if (outFormat.Equals("MS2") || outFormat.Equals("ms2")) { writeConfig.format = MSDataFile.Format.Format_MS2; } else { MessageBox.Show("Plese select output format"); } writeConfig.precision = MSDataFile.Precision.Precision_32; try { Workspace.SetText("\r\nWriting high quality spectra to file: " + outFileName); using (var msFile = new MSDataFile(file.FullName)) { var indexSet = new string[highQualIndices.Count]; var sl = msFile.run.spectrumList; for (int i = 0; i < highQualIndices.Count; ++i) { string id = highQualIndices[i]; int index = sl.find(id); if (index == sl.size()) { throw new Exception("nativeID \"" + id + "\" not found in file"); } indexSet[i] = index.ToString(); } var indexSetString = String.Join(" ", indexSet); var predicate2 = new SpectrumList_FilterPredicate_IndexSet(indexSetString); var filter = new SpectrumList_Filter(msFile.run.spectrumList, predicate2); msFile.run.spectrumList = filter; //msFile.run.spectrumList = new SpectrumList_Sorter(msFile.run.spectrumList, new SpectrumList_Sorter_LessThan( sorterPredicate.lessThan )); //msFile.run.spectrumList = new SpectrumList_Sorter(msFile.run.spectrumList, new SpectrumList_Sorter_LessThan(lessThan)); //Workspace.SetText("\r\nFinished msFile.run.spectrumList"); msFile.write(outFileName, writeConfig); } } catch (Exception exc) { //throw new Exception("Error in writiing new spectra file", exc); Workspace.SetText("\r\nError in writing new spectra file\r\n"); Workspace.ChangeButtonTo("Close"); throw new Exception(exc.Message); } Workspace.SetText("\r\nFinished writing high quality spectra for file: " + file.Name + " \r\n\r\n"); }// end of foreach file Workspace.SetText("\r\nFinished writing high quality spectra!"); Workspace.ChangeButtonTo("Close"); } // end of write()
void processFile(string filename, Config config, ReaderList readers) { if (LogUpdate != null) { LogUpdate("Opening file...", _info); } if (StatusUpdate != null) { StatusUpdate("Opening file...", ProgressBarStyle.Marquee, _info); } // read in data file using (var msdList = new MSDataList()) { readers.read(filename, msdList); foreach (var msd in msdList) { var outputFilename = config.outputFilename(filename, msd); if (filename == outputFilename) { throw new ArgumentException("Output filepath is the same as input filepath"); } if (LogUpdate != null) { LogUpdate("Calculating SHA1 checksum...", _info); } if (StatusUpdate != null) { StatusUpdate("Calculating SHA1 checksum...", ProgressBarStyle.Marquee, _info); } // only one thread lock (calculateSHA1Mutex) MSDataFile.calculateSHA1Checksums(msd); if (LogUpdate != null) { LogUpdate("Processing...", _info); } if (StatusUpdate != null) { StatusUpdate("Processing...", ProgressBarStyle.Marquee, _info); } SpectrumListFactory.wrap(msd, config.Filters); if (StatusUpdate != null && msd.run.spectrumList != null) { StatusUpdate(String.Format("Processing ({0} of {1})", DataGridViewProgressCell.MessageSpecialValue.CurrentValue, DataGridViewProgressCell.MessageSpecialValue.Maximum), ProgressBarStyle.Continuous, _info); } // write out the new data file IterationListenerRegistry ilr = null; ilr = new IterationListenerRegistry(); ilr.addListener(this, 100); MSDataFile.write(msd, outputFilename, config.WriteConfig, ilr); } } }
private void writeUnidentifiedSpectra(string spectraFilename, string outFileSuffix, List <string> unidentifiedSpectra, float cutoff, string outFormat) { int numOutputSpectra = Convert.ToInt32(unidentifiedSpectra.Count * cutoff); //List<int> highQualIndices; List <string> highQualIndices; highQualIndices = unidentifiedSpectra.GetRange(0, numOutputSpectra); //var predicate = new SpectrumList_FilterPredicate_IndexSet(); //foreach (int i in highQualIndices) //{ // predicate.indexSet.Add(i); //} var predicate = new SpectrumList_FilterPredicate_NativeIDSet(); foreach (string i in highQualIndices) { predicate.nativeIDSet.Add(i); } //MSDataFile.WriteConfig writeConfig = new MSDataFile.WriteConfig(MSDataFile.Format.Format_mzXML); MSDataFile.WriteConfig writeConfig = new MSDataFile.WriteConfig(); if (outFormat.Equals("mzXML") || outFormat.Equals("mzxml")) { writeConfig.format = MSDataFile.Format.Format_mzXML; } else if (outFormat.Equals("mzML") || outFormat.Equals("mzml")) { writeConfig.format = MSDataFile.Format.Format_mzML; } else if (outFormat.Equals("MGF") || outFormat.Equals("mgf")) { writeConfig.format = MSDataFile.Format.Format_MGF; } else if (outFormat.Equals("MS2") || outFormat.Equals("ms2")) { writeConfig.format = MSDataFile.Format.Format_MS2; } else { MessageBox.Show("Plese select output format"); } writeConfig.precision = MSDataFile.Precision.Precision_32; try { string outFileName = Path.GetFileNameWithoutExtension(spectraFilename) + outFileSuffix + "-Top" + (recoveryCutoff * 100).ToString() + "PercUnidentSpec." + recoveryOutFormat; Workspace.SetText("\r\nWriting unidentified high quality spectra to file: " + outFileName); using (MSDataFile msFile = new MSDataFile(spectraFilename)) { msFile.run.spectrumList = new SpectrumList_Filter(msFile.run.spectrumList, new SpectrumList_FilterAcceptSpectrum(predicate.accept)); msFile.write(outFileName, writeConfig); } } catch (Exception exc) { //throw new Exception("Error in writiing new spectra file", exc); Workspace.SetText("\r\nError in writing new spectra file\r\n"); Workspace.ChangeButtonTo("Close"); throw new Exception(exc.Message); } }
/// <summary> /// write out a subset of high quality spectra based on ScanRanker metrics file /// using ProteoWizard library /// </summary> public void Write() { foreach (FileInfo file in inFileList) { string fileBaseName = Path.GetFileNameWithoutExtension(file.FullName); string metricsFileName = fileBaseName + metricsFileSuffix + ".txt"; string outFileName = fileBaseName + outFileSuffix + "." + outFormat; Workspace.SetText("\r\nStart writing high quality spectra for file: " + file.Name + " ...\r\n\r\n"); if (File.Exists(outFileName)) { File.Delete(outFileName); } if (!File.Exists(metricsFileName)) { Workspace.SetText("\r\nError: Cannot find quality metrics file: " + metricsFileName + " in output directory!"); Workspace.ChangeButtonTo("Close"); return; } //List<int> allIndices = new List<int>(); //List<int> highQualIndices = new List<int>(); List<string> allIndices = new List<string>(); List<string> highQualIndices = new List<string>(); // read metrics file, split and get high quality spectra indecies from the second column try { Workspace.SetText("\r\nExtracting scan index from metrics file: " + metricsFileName); using (TextReader tr = File.OpenText(metricsFileName)) { tr.ReadLine(); // read the header line but do nothing, first three lines are header tr.ReadLine(); // read the header line but do nothing tr.ReadLine(); // read the header line but do nothing string line = string.Empty; while ((line = tr.ReadLine()) != null) { string[] items = line.Split('\t'); //int index = Convert.ToInt32(items[1]); //index string index = Convert.ToString(items[1]); //nativeID if (!allIndices.Exists(element => element == index)) // remove duplicate index { allIndices.Add(index); } } } } catch (Exception exc) { //throw new Exception("Error in reading metrics file for spectra removal\r\n",exc); Workspace.SetText("\r\nError in reading metrics file for spectra removal\r\n"); Workspace.ChangeButtonTo("Close"); throw new Exception(exc.Message); } // get indices for high quality spectra Workspace.SetText("\r\nGenerating indices of high quality spectra"); int numOutputSpectra = Convert.ToInt32(allIndices.Count * cutoff); highQualIndices = allIndices.GetRange(0, numOutputSpectra); //highQualIndices.Sort(); //var predicate = new SpectrumList_FilterPredicate_IndexSet(); //foreach (int i in highQualIndices) //{ // predicate.indexSet.Add(i); //} var predicate = new SpectrumList_FilterPredicate_NativeIDSet(); foreach (string i in highQualIndices) { predicate.nativeIDSet.Add(i); } //var sorterPredicate = new SpectrumList_SorterPredicate_IndexSet(); //foreach (int i in highQualIndices) //{ // sorterPredicate.indexSet.Add(i); //} //MSDataFile.WriteConfig writeConfig = new MSDataFile.WriteConfig(MSDataFile.Format.Format_mzXML); MSDataFile.WriteConfig writeConfig = new MSDataFile.WriteConfig(); if (outFormat.Equals("mzXML") || outFormat.Equals("mzxml")) { writeConfig.format = MSDataFile.Format.Format_mzXML; } else if (outFormat.Equals("mzML") || outFormat.Equals("mzml")) { writeConfig.format = MSDataFile.Format.Format_mzML; } else if (outFormat.Equals("MGF") || outFormat.Equals("mgf")) { writeConfig.format = MSDataFile.Format.Format_MGF; } else if (outFormat.Equals("MS2") || outFormat.Equals("ms2")) { writeConfig.format = MSDataFile.Format.Format_MS2; } else { MessageBox.Show("Plese select output format"); } writeConfig.precision = MSDataFile.Precision.Precision_32; try { Workspace.SetText("\r\nWriting high quality spectra to file: " + outFileName); using (var msFile = new MSDataFile(file.FullName)) { var indexSet = new string[highQualIndices.Count]; var sl = msFile.run.spectrumList; for (int i=0; i < highQualIndices.Count; ++i) { string id = highQualIndices[i]; int index = sl.find(id); if (index == sl.size()) throw new Exception("nativeID \"" + id + "\" not found in file"); indexSet[i] = index.ToString(); } var indexSetString = String.Join(" ", indexSet); var predicate2 = new SpectrumList_FilterPredicate_IndexSet(indexSetString); var filter = new SpectrumList_Filter(msFile.run.spectrumList, predicate2); msFile.run.spectrumList = filter; //msFile.run.spectrumList = new SpectrumList_Sorter(msFile.run.spectrumList, new SpectrumList_Sorter_LessThan( sorterPredicate.lessThan )); //msFile.run.spectrumList = new SpectrumList_Sorter(msFile.run.spectrumList, new SpectrumList_Sorter_LessThan(lessThan)); //Workspace.SetText("\r\nFinished msFile.run.spectrumList"); msFile.write(outFileName, writeConfig); } } catch (Exception exc) { //throw new Exception("Error in writiing new spectra file", exc); Workspace.SetText("\r\nError in writing new spectra file\r\n"); Workspace.ChangeButtonTo("Close"); throw new Exception(exc.Message); } Workspace.SetText("\r\nFinished writing high quality spectra for file: " + file.Name + " \r\n\r\n"); }// end of foreach file Workspace.SetText("\r\nFinished writing high quality spectra!"); Workspace.ChangeButtonTo("Close"); } // end of write()
private void writeSingleSpectrum(string sourceFile, string nativeID, string outFileName) { MSDataFile.WriteConfig writeConfig = new MSDataFile.WriteConfig(); writeConfig.format = MSDataFile.Format.Format_MGF; writeConfig.precision = MSDataFile.Precision.Precision_32; //var predicate = new SpectrumList_FilterPredicate_IndexSet(); //predicate.indexSet.Add(nativeID); var predicate = new SpectrumList_FilterPredicate_NativeIDSet(); predicate.nativeIDSet.Add(nativeID); try { //Workspace.SetText("\r\nWriting selected spectrum to " + outFileName); MainForm.SetText(this, "Start writing selected spectrum to " + outFileName + "\r\n"); using (MSDataFile msFile = new MSDataFile(sourceFile)) { msFile.run.spectrumList = new SpectrumList_Filter(msFile.run.spectrumList, new SpectrumList_FilterAcceptSpectrum(predicate.accept)); msFile.write(outFileName, writeConfig); } MainForm.SetText(this, "Finished writing selected spectrum to " + outFileName + "\r\n"); } catch (Exception exc) { //throw new Exception("Error in writiing new spectra file", exc); //Workspace.SetText("\r\nError in writing new spectra file\r\n"); tbPepNovoResult.AppendText("\r\nError in writing new spectra file\r\n"); throw new Exception(exc.Message); } }