示例#1
0
        /// <summary>
        /// Look for the default .pbf testing file in the default test file folder,
        /// plus also in the UnitTest_Files directory in this project or solution
        /// </summary>
        /// <param name="createIfMissing">If true and the .mzML file is ofund, create the .pbf file</param>
        /// <returns>Path to the file if found, otherwise the default path on Proto-2</returns>
        public static string GetPbfTestFilePath(bool createIfMissing)
        {
            var methodName = MethodBase.GetCurrentMethod().Name;

            var pbfFilePath = Path.Combine(DEFAULT_SPEC_FILES_FOLDER, PBF_TEST_FILE);

            var pbfFileInfo = GetTestFile("PbfTestFilePath", pbfFilePath, false);

            if (pbfFileInfo != null)
            {
                // Check for a lock file, which would indicate another process is creating the .pbf file
                var existingLockFile = new FileInfo(Path.Combine(pbfFileInfo.FullName + ".lock"));
                WaitForLockFile(existingLockFile, false);

                pbfFileInfo.Refresh();
                if (pbfFileInfo.Exists)
                {
                    DeleteLockFile(existingLockFile);
                    return(pbfFileInfo.FullName);
                }
            }

            if (!createIfMissing)
            {
                // Not creating the file if missing; return the default path, despite the fact that the file does not exist
                // The calling method will discover that the file is missing and act accordingly
                return(pbfFilePath);
            }

            // Create the missing file

            var mzmlFilePath = Path.Combine(DEFAULT_SPEC_FILES_FOLDER, MZML_TEST_FILE);

            var mzmlFileInfo = GetTestFile("MzmlTestFilePath", mzmlFilePath, false);

            if (mzmlFileInfo?.DirectoryName == null)
            {
                // Unable to create the pbf file; return the default path, despite the fact that the file does not exist
                return(pbfFilePath);
            }

            ShowMessage(methodName, string.Format("Creating {0} using {1}", PBF_TEST_FILE, mzmlFileInfo.FullName));

            var lockFile       = new FileInfo(Path.Combine(mzmlFileInfo.DirectoryName, Path.GetFileNameWithoutExtension(mzmlFileInfo.Name) + ".pbf.lock"));
            var newPbfFilePath = Path.Combine(mzmlFileInfo.DirectoryName, PBF_TEST_FILE);

            try
            {
                // Create a new lock file so other processes know this thread is creating the .pbf file
                WaitForLockFile(lockFile, true);

                mLastStatus = string.Empty;
                var startTime = DateTime.UtcNow;

                var reader   = MassSpecDataReaderFactory.GetMassSpecDataReader(mzmlFileInfo.FullName);
                var progress = new Progress <ProgressData>(p =>
                {
                    p.UpdateFrequencySeconds = 2;
                    if (p.Percent < 100 && (p.Percent % 25).Equals(0) || p.ShouldUpdate())
                    {
                        var statusMessage = string.Format("{0}, {1:00.0}% complete                        ", p.Status, p.Percent);

                        if (string.Equals(mLastStatus, statusMessage))
                        {
                            return;
                        }

                        mLastStatus = statusMessage;
                        Console.Write("\r{0}, {1:00.0}% complete                        ", p.Status, p.Percent);
                    }
                });

                var run = new PbfLcMsRun(mzmlFileInfo.FullName, reader, newPbfFilePath, 0, 0, progress);
                Console.WriteLine();
                ShowMessage(methodName, string.Format("Created {0} in {1:F0} seconds", run.PbfFilePath, DateTime.UtcNow.Subtract(startTime).TotalSeconds));

                DeleteLockFile(lockFile);

                return(run.PbfFilePath);
            }
            catch (Exception ex)
            {
                ShowMessage(methodName, string.Format("Exception creating {0} using {1}: {2}", PBF_TEST_FILE, mzmlFileInfo.FullName, ex.Message));

                try
                {
                    var incompletePbfFile = new FileInfo(newPbfFilePath);
                    if (incompletePbfFile.Exists)
                    {
                        incompletePbfFile.Delete();
                    }

                    DeleteLockFile(lockFile);
                }
                catch
                {
                    // Ignore errors here
                }

                // Return the default path, despite the fact that the file does not exist
                return(pbfFilePath);
            }
        }
示例#2
0
        public static int Main(string[] args)
        {
            PbfGenInputParameters options;

            try
            {
                var osVersionInfo = new clsOSVersionInfo();
                if (osVersionInfo.GetOSVersion().ToLower().Contains("windows"))
                {
                    var handle = Process.GetCurrentProcess().MainWindowHandle;
                    SetConsoleMode(handle, EnableExtendedFlags);
                }

                var exeName = System.Reflection.Assembly.GetEntryAssembly().GetName().Name;

                var parser = new CommandLineParser <PbfGenInputParameters>(exeName, Version);
                parser.UsageExamples.Add($"Using -start and -end to limit the scan range to include in the .pbf file\n\t{exeName}.exe -s Dataset.raw -start 2000 -end 3000");

                var results = parser.ParseArgs(args);

                if (!results.Success)
                {
                    // Wait for 1.5 seconds
                    System.Threading.Thread.Sleep(1500);

                    return(-1);
                }

                if (!results.ParsedResults.Validate())
                {
                    parser.PrintHelp();

                    // Wait for 1.5 seconds
                    System.Threading.Thread.Sleep(1500);

                    return(-1);
                }

                options = results.ParsedResults;
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception while parsing the command line parameters: " + ex.Message);
                return(-5);
            }

#if (!DEBUG)
            try
#endif
            {
                var specFilePaths = new[] { options.SourcePath };
                if (Directory.Exists(options.SourcePath) && !MassSpecDataReaderFactory.IsADirectoryDataset(options.SourcePath))
                {
                    specFilePaths = Directory.GetFiles(options.SourcePath, "*.raw"); // TODO: Support folders with other formats in them too...
                }

                foreach (var rawFilePath in specFilePaths)
                {
                    var pbfFileName = MassSpecDataReaderFactory.ChangeExtension(rawFilePath, PbfLcMsRun.FileExtensionConst);
                    var pbfFilePath = Path.Combine(options.OutputDir, Path.GetFileName(pbfFileName));

                    if (File.Exists(pbfFilePath) && PbfLcMsRun.CheckFileFormatVersion(pbfFilePath, out var isCurrent) && isCurrent)
                    {
                        Console.WriteLine("{0} already exists.", pbfFilePath);
                        continue;
                    }

                    Console.WriteLine("Creating {0} from {1}", pbfFilePath, rawFilePath);

                    if (options.StartScan > 0 && options.EndScan > 0)
                    {
                        Console.WriteLine("Only including scans {0} to {1}", options.StartScan, options.EndScan);
                    }
                    else if (options.StartScan > 0)
                    {
                        Console.WriteLine("Only including scans {0} to the end", options.StartScan);
                    }
                    else if (options.EndScan > 0)
                    {
                        Console.WriteLine("Only including scans 1 to {0}", options.EndScan);
                    }

                    var reader   = MassSpecDataReaderFactory.GetMassSpecDataReader(rawFilePath);
                    var progress = new Progress <ProgressData>(p =>
                    {
                        p.UpdateFrequencySeconds = 2;
                        if ((p.Percent % 25).Equals(0) || p.ShouldUpdate())
                        {
                            Console.Write("\r{0}, {1:00.0}% complete                        ", p.Status, p.Percent);
                        }
                    });
                    var run = new PbfLcMsRun(rawFilePath, reader, pbfFilePath, 0, 0, progress, false, options.StartScan, options.EndScan);
                    Console.WriteLine();
                }

                Console.WriteLine("PbfFormatVersion: {0}", PbfLcMsRun.FileFormatId);
                return(0);
            }
#if (!DEBUG)
            catch (Exception ex)
            {
                // NOTE: The DMS Analysis Manager looks for this text; do not change it
                Console.WriteLine("Exception while processing: " + ex.Message);
                Console.WriteLine(ex.StackTrace);
                var errorCode = -Math.Abs(ex.Message.GetHashCode());
                if (errorCode == 0)
                {
                    return(-1);
                }
                else
                {
                    return(errorCode);
                }
            }
#endif
        }
示例#3
0
        public void TestReadingPbfFile()
        {
            var methodName = MethodBase.GetCurrentMethod().Name;

            Utils.ShowStarting(methodName);

            var pbfFilePath = Path.Combine(Utils.DEFAULT_TEST_FILE_FOLDER, @"TopDown\ProductionQCShew\QC_Shew_13_04_A_17Feb14_Samwise_13-07-28.pbf");

            if (!File.Exists(pbfFilePath))
            {
                Assert.Ignore(@"Skipping test {0} since file not found: {1}", methodName, pbfFilePath);
            }

            var pbfRun = new PbfLcMsRun(pbfFilePath);

            var checksum = pbfRun.PbfFileChecksum;

            var specFilePath = Path.ChangeExtension(pbfFilePath, "raw");

            if (!File.Exists(specFilePath))
            {
                Assert.Ignore(@"Skipping test {0} since file not found: {1}", methodName, specFilePath);
            }

            Console.WriteLine(@"Loading .pbf into memory");

            //var run = InMemoryLcMsRun.GetLcMsRun(specFilePath);
            var run = new InMemoryLcMsRun(MassSpecDataReaderFactory.GetMassSpecDataReader(specFilePath), 0, 0);

            Console.WriteLine(@"Comparing spectra between .pbf and in-memory spectra");

            // spectrum comparison
            //for (var scanNum = run.MinLcScan; scanNum <= run.MaxLcScan; scanNum++)
            foreach (var scanNum in run.AllScanNumbers)
            {
                var spec1 = run.GetSpectrum(scanNum);
                var spec2 = pbfRun.GetSpectrum(scanNum);

                Assert.IsTrue(spec1.Peaks.Length == spec2.Peaks.Length);
                for (var i = 0; i < spec1.Peaks.Length; i++)
                {
                    var p1 = spec1.Peaks[i];
                    var p2 = spec2.Peaks[i];

                    Assert.True(p1.Equals(p2));

                    Assert.True(Math.Abs(p1.Mz - p2.Mz) < 1e-8);
                    Assert.True(Math.Abs(p1.Intensity - p2.Intensity) < 0.001);
                }
            }

            Console.WriteLine(@"Comparing XICs");
            // chromatogram comparison
            const double targetMz  = 655.01;
            var          tolerance = new Tolerance(10);
            var          xic1      = run.GetFullPrecursorIonExtractedIonChromatogram(targetMz, tolerance);
            var          xic2      = pbfRun.GetFullPrecursorIonExtractedIonChromatogram(targetMz, tolerance);

            Assert.True(xic1.Count == xic2.Count);

            for (var i = 0; i < xic1.Count; i++)
            {
                if (!xic1[i].Equals(xic2[i]))
                {
                    Console.WriteLine(@"{0} {1} {2}", i, xic1[i], xic2[i]);
                }
                Assert.True(xic1[i].Equals(xic2[i]));
            }
            Console.WriteLine(@"Done");
        }
示例#4
0
        public static int Main(string[] args)
        {
            string specFilePath;
            string outputDir;

            try
            {
                var handle = Process.GetCurrentProcess().MainWindowHandle;
                SetConsoleMode(handle, EnableExtendedFlags);

                if (args.Length == 0)
                {
                    PrintUsageInfo();
                    return(-1);
                }

                var paramDic = new Dictionary <string, string>
                {
                    { "-s", null },
                    { "-o", null }
                };

                for (var i = 0; i < args.Length / 2; i++)
                {
                    var key   = args[2 * i];
                    var value = args[2 * i + 1];
                    if (!paramDic.ContainsKey(key))
                    {
                        PrintUsageInfo("Invalid parameter: " + key);
                        return(-1);
                    }
                    paramDic[key] = value;
                }

                // Parse command line parameters
                specFilePath = paramDic["-s"];

                if (specFilePath == null)
                {
                    PrintUsageInfo("Missing required parameter -s!");
                    return(-1);
                }

                // Check for folder-type datasets, and replace specFilePath with the directory name if it is.
                specFilePath = MassSpecDataReaderFactory.GetDatasetName(specFilePath);

                var isDirectoryDataset = MassSpecDataReaderFactory.IsADirectoryDataset(specFilePath);
                // True if specFilePath is a directory that is NOT a supported folder-type dataset.
                var specPathIsDirectory = Directory.Exists(specFilePath) && !isDirectoryDataset;

                if (!File.Exists(specFilePath) && !specPathIsDirectory && !isDirectoryDataset)
                {
                    PrintUsageInfo("File not found: " + specFilePath);
                    return(-1);
                }

                var types = MassSpecDataReaderFactory.MassSpecDataTypeFilterList;
                types.Remove(".pbf");

                if (!specPathIsDirectory && !(types.Select(ext => specFilePath.ToLower().EndsWith(ext)).Any()))
                {
                    PrintUsageInfo("Invalid file extension: (" + Path.GetExtension(specFilePath) + ") " + specFilePath);
                    return(-1);
                }

                // Must use "Path.GetFullPath" to return the absolute path when the source file is in the working directory
                // But, it could cause problems with too-long paths.
                outputDir = paramDic["-o"] ?? (specPathIsDirectory ? specFilePath : Path.GetDirectoryName(Path.GetFullPath(specFilePath)));
                if (outputDir == null)
                {
                    PrintUsageInfo("Invalid output file directory: " + specFilePath);
                    return(-1);
                }

                if (!Directory.Exists(outputDir))
                {
                    if (File.Exists(outputDir) && !File.GetAttributes(outputDir).HasFlag(FileAttributes.Directory))
                    {
                        PrintUsageInfo("OutputDir " + outputDir + " is not a directory!");
                        return(-1);
                    }
                    Directory.CreateDirectory(outputDir);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception while parsing the command line parameters: " + ex.Message);
                return(-5);
            }

#if (!DEBUG)
            try
            {
#endif
            string[] specFilePaths = new[] { specFilePath };
            if (Directory.Exists(specFilePath) && !MassSpecDataReaderFactory.IsADirectoryDataset(specFilePath))
            {
                specFilePaths = Directory.GetFiles(specFilePath, "*.raw");
            }

            foreach (var rawFilePath in specFilePaths)
            {
                var pbfFilePath = Path.Combine(outputDir, Path.GetFileNameWithoutExtension(rawFilePath) + PbfLcMsRun.FileExtension);

                bool isCurrent;
                if (File.Exists(pbfFilePath) && PbfLcMsRun.CheckFileFormatVersion(pbfFilePath, out isCurrent) && isCurrent)
                {
                    Console.WriteLine("{0} already exists.", pbfFilePath);
                    continue;
                }

                Console.WriteLine("Creating {0} from {1}", pbfFilePath, rawFilePath);
                IMassSpecDataReader reader = MassSpecDataReaderFactory.GetMassSpecDataReader(rawFilePath);
                var progress = new Progress <ProgressData>(p =>
                {
                    p.UpdateFrequencySeconds = 2;
                    if ((p.Percent % 25).Equals(0) || p.ShouldUpdate())
                    {
                        Console.Write("\r{0}, {1:00.0}% complete                        ", p.Status, p.Percent);
                    }
                });
                var run = new PbfLcMsRun(rawFilePath, reader, pbfFilePath, 0, 0, progress);
                Console.WriteLine();
            }


            Console.WriteLine("PbfFormatVersion: {0}", PbfLcMsRun.FileFormatId);
            return(0);

#if (!DEBUG)
        }

        catch (Exception ex)
        {
            // NOTE: The DMS Analysis Manager looks for this text; do not change it
            Console.WriteLine("Exception while processing: " + ex.Message);
            Console.WriteLine(ex.StackTrace);
            var errorCode = -Math.Abs(ex.Message.GetHashCode());
            if (errorCode == 0)
            {
                return(-1);
            }
            else
            {
                return(errorCode);
            }
        }
#endif
        }
        /// <summary>
        /// Process spectra and identifications.
        /// </summary>
        /// <param name="rawFilePath">Full file path to raw file.</param>
        /// <param name="idFilePath">Full file path to identification file.</param>
        /// <param name="cancellationToken">For notification of cancellation.</param>
        /// <param name="progress">Progress reporter.</param>
        /// <returns>List of processed IDs.</returns>
        public List <ProcessedResult> Process(string rawFilePath, string idFilePath, CancellationToken cancellationToken, IProgress <ProgressData> progress = null)
        {
            // Set up progress reporter
            progress = progress ?? new Progress <ProgressData>();

            var progressData = new ProgressData(progress);

            // Show initial loading message
            progressData.Report(0.1, "Loading...");

            // Read mzid file
            var mzidReader      = new SimpleMZIdentMLReader();
            var identifications = mzidReader.Read(idFilePath, cancellationToken);

            // Check to make sure raw and MZID file match.
            var rawFileName = Path.GetFileNameWithoutExtension(rawFilePath);

            var spectrumFileFromId = Path.GetFileNameWithoutExtension(identifications.SpectrumFile);
            var dtaIndex           = spectrumFileFromId.LastIndexOf("_dta");

            if (dtaIndex >= 0)
            {
                spectrumFileFromId = spectrumFileFromId.Substring(0, dtaIndex);
            }

            if (rawFileName != spectrumFileFromId)
            {
                throw new ArgumentException($"Mismatch between spectrum file ({rawFileName}) and id file ({spectrumFileFromId}).");
            }

            // Group IDs into a hash by scan number
            var idMap = identifications.Identifications.GroupBy(id => id.ScanNum).ToDictionary(scan => scan.Key, ids => ids);

            var processedResults = new ConcurrentBag <ProcessedResult>();

            // Load raw file
            using (var lcms = MassSpecDataReaderFactory.GetMassSpecDataReader(rawFilePath))
            {
                int count = 0;
                Parallel.ForEach(
                    lcms.ReadAllSpectra(),
                    spectrum =>
                {
                    if (cancellationToken.IsCancellationRequested)
                    {           // Cancel if necessary
                        return;
                    }

                    // Report completion percentage and current scan number
                    if (count % (int)Math.Max(0.01 * lcms.NumSpectra, 1) == 0)
                    {
                        progressData.Report(count, lcms.NumSpectra, $"{Math.Round(100.0*count / lcms.NumSpectra)}%");
                    }

                    Interlocked.Increment(ref count);

                    // Skip spectrum if it isn't MS2
                    var productSpectrum = spectrum as ProductSpectrum;
                    if (productSpectrum == null || !idMap.ContainsKey(spectrum.ScanNum))
                    {
                        return;
                    }

                    var specResults = idMap[spectrum.ScanNum];

                    var results = from specResult in specResults
                                  let sequence = specResult.Peptide.GetIpSequence()
                                                 let coverage = this.CalculateSequenceCoverage(productSpectrum, sequence, specResult.Charge)
                                                                select new ProcessedResult
                    {
                        ScanNum          = spectrum.ScanNum,
                        Sequence         = sequence,
                        Charge           = specResult.Charge,
                        PrecursorMz      = specResult.CalMz,
                        DeNovoScore      = specResult.DeNovoScore,
                        SpecEValue       = specResult.SpecEv,
                        EValue           = specResult.EValue,
                        QValue           = specResult.QValue,
                        PepQValue        = specResult.PepQValue,
                        FragMethod       = productSpectrum.ActivationMethod,
                        IsotopeError     = specResult.IsoError,
                        SequenceCoverage = Math.Round(coverage),
                    };

                    foreach (var result in results)
                    {
                        processedResults.Add(result);
                    }
                });
            }

            // Sort spectra by SpecEValue
            return(processedResults.OrderBy(pr => pr.SpecEValue).ToList());
        }