protected List <PeakList <Peak> > ReadTandemMassFromRaw(FileInfo rawFilename, List <int> ignoreScans)
        {
            string experimental = FileUtils.ChangeExtension(rawFilename.Name, "");

            var result = new List <PeakList <Peak> >();

            bool bReadAgain = false;

            rawReader.Open(rawFilename.FullName);
            try
            {
                int firstSpectrumNumber = rawReader.GetFirstSpectrumNumber();
                int lastSpectrumNumber  = rawReader.GetLastSpectrumNumber();

                Progress.SetRange(firstSpectrumNumber, lastSpectrumNumber);
                Progress.Begin();
                try
                {
                    for (int scan = firstSpectrumNumber; scan <= lastSpectrumNumber; scan++)
                    {
                        if (Progress.IsCancellationPending())
                        {
                            throw new UserTerminatedException();
                        }

                        if (ignoreScans.Contains(scan))
                        {
                            continue;
                        }

                        Progress.SetPosition(scan);

                        int msLevel = rawReader.GetMsLevel(scan);

                        if (msLevel > 1)
                        {
                            PeakList <Peak> pkl;
                            try
                            {
                                pkl = rawReader.GetPeakList(scan);
                            }
                            catch (RawReadException ex)
                            {
                                ignoreScans.Add(ex.Scan);
                                File.WriteAllLines(GetIgnoreScanFile(rawFilename), (from i in ignoreScans
                                                                                    let s = i.ToString()
                                                                                            select s).ToArray());
                                bReadAgain = true;
                                break;
                            }
                            pkl.Precursor    = rawReader.GetPrecursorPeakWithMasterScan(scan);
                            pkl.MsLevel      = msLevel;
                            pkl.Experimental = experimental;
                            pkl.ScanTimes.Add(new ScanTime(scan, rawReader.ScanToRetentionTime(scan)));
                            pkl.ScanMode = rawReader.GetScanMode(scan);

                            if (pkl.PrecursorCharge == 0)
                            {
                                pkl.PrecursorCharge = PrecursorUtils.GuessPrecursorCharge(pkl, pkl.PrecursorMZ);
                            }

                            PeakList <Peak> pklProcessed = this.pklProcessor.Process(pkl);
                            if (null != pklProcessed && pklProcessed.Count > 0)
                            {
                                result.Add(pklProcessed);
                            }
                        }
                    }
                }
                finally
                {
                    Progress.End();
                }
            }
            finally
            {
                rawReader.Close();
            }

            if (bReadAgain)
            {
                return(ReadTandemMassFromRaw(rawFilename, ignoreScans));
            }
            else
            {
                return(result);
            }
        }
        private int DoWritePeakList(IRawFile2 rawReader, int scan, string fileName, List <string> returnFiles, string experimental, int lastSpectrumNumber, List <int> ignoreScans, ref bool bReadAgain)
        {
            var result = scan + 1;

            if (ignoreScans.Contains(scan))
            {
                return(result);
            }

            SetPosition(scan);

            int msLevel = rawReader.GetMsLevel(scan);

            if (!DoAcceptMsLevel(msLevel))
            {
                return(result);
            }

            //Console.WriteLine("Reading scan {0}", scan);

            PeakList <Peak> pkl;

            try
            {
                pkl = rawReader.GetPeakList(scan);
            }
            catch (RawReadException ex)
            {
                ignoreScans.Add(ex.Scan);
                File.WriteAllLines(GetIgnoreScanFile(fileName), (from i in ignoreScans
                                                                 let s = i.ToString()
                                                                         select s).ToArray());
                bReadAgain = true;
                return(result);
            }

            pkl.MsLevel      = msLevel;
            pkl.Experimental = experimental;
            pkl.ScanTimes.Add(new ScanTime(scan, rawReader.ScanToRetentionTime(scan)));

            pkl.ScanMode = rawReader.GetScanMode(scan);

            PeakList <Peak> pklProcessed;

            if (msLevel > 1)
            {
                pkl.Precursor = new PrecursorPeak(rawReader.GetPrecursorPeak(scan));

                if (pkl.PrecursorCharge == 0)
                {
                    pkl.PrecursorCharge = PrecursorUtils.GuessPrecursorCharge(pkl, pkl.PrecursorMZ);
                }

                if (options.ExtractRawMS3 && pkl.MsLevel == 3)
                {
                    pklProcessed = pkl;
                }
                else
                {
                    pklProcessed = this.PeakListProcessor.Process(pkl);
                }
            }
            else
            {
                pklProcessed = pkl;
            }

            if (null != pklProcessed && pklProcessed.Count > 0)
            {
                DoWritePeakList(rawReader, pklProcessed, fileName, returnFiles);

                if (!options.MzXmlNestedScan)
                {
                    var intent = GetScanIntent(pkl.MsLevel);
                    sw.WriteLine(intent + "</scan>");
                }

                while (result < lastSpectrumNumber && rawReader.GetMsLevel(result) > msLevel)
                {
                    result = DoWritePeakList(rawReader, result, fileName, returnFiles, experimental, lastSpectrumNumber, ignoreScans, ref bReadAgain);
                    if (bReadAgain)
                    {
                        return(result);
                    }
                }

                if (options.MzXmlNestedScan)
                {
                    var intent = GetScanIntent(pkl.MsLevel);
                    sw.WriteLine(intent + "</scan>");
                }
            }

            return(result);
        }
        protected override void DoInitialize(IRawFile2 rawFile, string rawFileName)
        {
            var resultFile = GetResultFile(rawFile, rawFileName);

            var scans = GetOutputScans(rawFile);

            this.scanIndeies = new List <Pair <int, long> >();

            var utf8WithoutBom = new System.Text.UTF8Encoding(false);

            this.sw         = new StreamWriter(resultFile, false, utf8WithoutBom);
            this.sw.NewLine = "\n";

            /*
             * xml header and namespace info
             */
            sw.WriteLine(@"<?xml version=""1.0"" encoding=""ISO-8859-1""?>");
            sw.WriteLine(@"<mzXML xmlns=""http://sashimi.sourceforge.net/schema_revision/mzXML_3.1""");
            sw.WriteLine(@" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance""");
            sw.WriteLine(@" xsi:schemaLocation=""http://sashimi.sourceforge.net/schema_revision/mzXML_3.1 http://sashimi.sourceforge.net/schema_revision/mzXML_3.1/mzXML_idx_3.1.xsd"" >");

            int    firstScan = rawFile.GetFirstSpectrumNumber();
            int    lastScan  = rawFile.GetLastSpectrumNumber();
            double startTime = rawFile.ScanToRetentionTime(firstScan) * 60;
            double endTime   = rawFile.ScanToRetentionTime(lastScan) * 60;

            /*
             * begin msRun
             */
            sw.WriteLine(MyConvert.Format(@" <msRun scanCount=""{0}"" startTime=""PT{1:G6}S"" endTime=""PT{2:G6}S"" >", scans.Count, startTime, endTime));

            string sha1;

            try
            {
                sha1 = HashUtils.GetSHA1Hash(rawFileName).ToLower();
            }
            catch (Exception ex)
            {
                throw new Exception("Exception when calculating sha1 value of file " + rawFileName, ex);
            }

            /*
             * parent (raw input) file
             */
            //sw.WriteLine(@"  <parentFile fileName=""file://DATA-PC/L/5168TP_mouse_phos_TMT/raw/6.raw"" fileType=""RAWData"" fileSha1=""f6db29dd3e26dec7eb16422138f0b2b06f02ba06"" />");
            sw.WriteLine(@"  <parentFile fileName=""file://{0}"" fileType=""RAWData"" fileSha1=""{1}""/>", rawFileName.Replace("\\", "/"), sha1);

            if (rawFile is RawFileImpl)
            {
                var impl = rawFile as RawFileImpl;

                /*
                 * mass spec instrument section
                 */
                // get the instrument model
                string instModel = impl.GetInstModel();

                // get acquisition software version
                string instSoftVersion = impl.GetInstSoftwareVersion();

                sw.WriteLine(@"  <msInstrument>");
                sw.WriteLine(@"   <msManufacturer category=""msManufacturer"" value=""Thermo Finnigan"" />");
                sw.WriteLine(@"   <msModel category=""msModel"" value=""{0}"" />", instModel);
                sw.WriteLine(@"   <msIonisation category=""msIonisation"" value=""NSI"" />");
                sw.WriteLine(@"   <msMassAnalyzer category=""msMassAnalyzer"" value=""unknown"" />");
                sw.WriteLine(@"   <msDetector category=""msDetector"" value=""unknown"" />");
                sw.WriteLine(@"   <software type=""acquisition"" name=""Xcalibur"" version=""{0}"" />", instSoftVersion);
                sw.WriteLine(@"  </msInstrument>");
            }

            /*
             * data processing info
             */
            sw.WriteLine(@"  <dataProcessing>");
            sw.WriteLine(@"   <software type=""conversion"" name=""{0}"" version=""{1}"" />", this.DataProcessingSoftware, this.DataProcessingSoftwareVersion);
            foreach (string dataProcessingOperationName in this.dataProcessingOperations.Keys)
            {
                sw.WriteLine(MyConvert.Format(@"    <processingOperation name=""{0}"" value=""{1}""/>", dataProcessingOperationName, this.dataProcessingOperations[dataProcessingOperationName]));
            }
            sw.WriteLine("  </dataProcessing>");
        }
 public double ScanToRetentionTime(int scan)
 {
     return(rawFile.ScanToRetentionTime(scan));
 }