Exemplo n.º 1
0
 public void AddPSM(PSM psm)
 {
     try
     {
         Psms.Add(psm.FilenameID, psm);
     }
     catch (Exception e)
     {
         throw new ArgumentException(string.Format("A PSM with the same id ({0}) has already been entered for this file ({1})", psm.FilenameID, FilePath), e);
     }
 }
Exemplo n.º 2
0
 public void AddPSM(PSM psm)
 {
     try
     {
         Psms.Add(psm.FilenameID, psm);
     }
     catch (Exception e)
     {
         throw new ArgumentException(string.Format("A PSM with the same id ({0}) has already been entered for this file ({1})", psm.FilenameID, FilePath), e);
     }
 }
Exemplo n.º 3
0
        private IEnumerable<QuantFile> LoadFiles(IEnumerable<string> filePaths, bool ms3Quant = false)
        {
            MSDataFile.CacheScans = false;
            //int largestQuantPeak = 0;
            int i = 0;
            foreach (TagInformation tag in UsedTags.Values)
            {
                tag.UniqueTagNumber = i++;
                tag.TotalSignal = 0;
                tag.NormalizedTotalSignal = 0;
            }
            int largestQuantPeak = i-1;
            //int largestQuantPeak = UsedTags.Values.Select(tag => tag.UniqueTagNumber).Concat(new[] {0}).Max();

            foreach (string filePath in filePaths)
            {
                Log("Processing file:\t" + filePath);
                OnUpdateLog("Processing File "+filePath+"...");
                QuantFile quantFile = new QuantFile(filePath);
                StreamReader basestreamReader = new StreamReader(filePath);
                int oldProgress = -1;
                using (CsvReader reader = new CsvReader(basestreamReader, true))
                {
                    while (reader.ReadNextRecord()) // go through csv and raw file to extract the info we want
                    {
                        int scanNumber = int.Parse(reader["Spectrum number"]);
                        string filenameID = reader["Filename/id"];
                        string rawFileName = filenameID.Split('.')[0];
                        bool isDecoy = reader["DEFLINE"].StartsWith("DECOY_");
                        ThermoRawFile rawFile;
                        if (!RawFiles.TryGetValue(rawFileName, out rawFile))
                        {
                            throw new ArgumentException("Cannot find this raw file: " + rawFileName + ".raw");
                        }
                        if (!rawFile.IsOpen)
                        {
                            rawFile.Open();
                        }

                        int progress = (int) (100*(double)basestreamReader.BaseStream.Position/basestreamReader.BaseStream.Length);
                        if (progress != oldProgress)
                        {
                            OnProgressUpdate(progress);
                            oldProgress = progress;
                        }

                    //// Set default fragmentation to CAD / HCD
                        //FragmentationMethod ScanFragMethod = filenameID.Contains(".ETD.")
                        //    ? FragmentationMethod.ETD
                        //    : FragmentationMethod.CAD;

                        //if (ScanFragMethod == FragmentationMethod.ETD)
                        //{
                        //    ScanFragMethod = FragmentationMethod.CAD;
                        //    scanNumber += ETDQuantPosition;
                        //}

                        // Get the scan object for the sequence ms2 scan
                        MsnDataScan quantitationMsnScan = rawFile[scanNumber] as MsnDataScan;

                        double purity = 1;
                        if (CalculatePurity)
                        {
                            double mz = quantitationMsnScan.PrecursorMz;
                            int charge = quantitationMsnScan.PrecursorCharge;
                            DoubleRange isolationRange = MzRange.FromDa(mz, PurityWindowInTh);

                            MSDataScan parentScan = rawFile[quantitationMsnScan.ParentScanNumber];
                            purity = DeterminePurity(parentScan, mz, charge, isolationRange);
                        }

                        if (quantitationMsnScan == null)
                        {
                            throw new ArgumentException("Spectrum Number " + scanNumber + " is not a valid MS2 scan from: "+rawFile.FilePath);
                        }

                        if (MS3Quant)
                        {
                            quantitationMsnScan = null;
                            // Look forward to find associated MS3 quant scan (based on parent scan number)
                            int ms3ScanNumber = scanNumber + 1;
                            while (ms3ScanNumber < rawFile.LastSpectrumNumber)
                            {
                                if (rawFile.GetParentSpectrumNumber(ms3ScanNumber) == scanNumber)
                                {
                                    quantitationMsnScan = rawFile[ms3ScanNumber] as MsnDataScan;
                                    break;
                                }
                                ms3ScanNumber++;
                            }
                            if (quantitationMsnScan == null)
                            {
                                throw new ArgumentException("Cannot find a MS3 spectrum associated with spectrum number " + scanNumber);
                            }
                        }

                        Tolerance Tolerance = quantitationMsnScan.MzAnalyzer == MZAnalyzerType.IonTrap2D ? ItMassTolerance : FtMassTolerance;
                        bool isETD = quantitationMsnScan.DissociationType == DissociationType.ETD;

                        double injectionTime = quantitationMsnScan.InjectionTime;
                        var massSpectrum = quantitationMsnScan.MassSpectrum;
                        double noise = 0;
                        if (NoisebandCap)
                        {
                            // Noise is pretty constant over a small region, find the noise of the center of all isobaric tags
                            MassRange range = new MassRange(UsedTags.Keys[0], UsedTags.Keys[UsedTags.Count - 1]);

                            ThermoSpectrum thermoSpectrum = massSpectrum as ThermoSpectrum;
                            if (thermoSpectrum != null)
                            {
                                var peak = thermoSpectrum.GetClosestPeak(range);
                                if (peak != null)
                                {
                                    noise = peak.Noise;
                                }
                                else
                                {
                                    throw new ArgumentException("Spectrum (#" + quantitationMsnScan.SpectrumNumber + ") has no m/z peaks");
                                }
                            }
                            else
                            {
                                throw new ArgumentException("Spectrum (#" + quantitationMsnScan.SpectrumNumber+") , or they are low-resolution data without noise information");
                            }

                            //ThermoLabeledPeak peak = massSpectrum.GetClosestPeak(range) as ThermoLabeledPeak;

                            //if (peak != null)
                            //{
                            //    noise = peak.Noise;
                            //}
                            //else
                            //{
                            //    peak = massSpectrum as ThermoLabeledPeak;
                            //    if (peak == null)
                            //    {
                            //        throw new ArgumentException("Either the spectrum (#" + quantitationMsnScan.SpectrumNumber+") has no m/z peaks, or they are low-resolution data without noise information");
                            //    }
                            //    noise = peak.Noise;
                            //}
                        }

                        //Dictionary<TagInformation, QuantPeak> peaks = new Dictionary<TagInformation, QuantPeak>();
                        QuantPeak[] peaks = new QuantPeak[largestQuantPeak+1];
                        // Read in the peak data
                        foreach (TagInformation tag in UsedTags.Values)
                        {
                            double tagMz = isETD
                                ? tag.MassEtd
                                : tag.MassCAD;

                            var peak = massSpectrum.GetClosestPeak(Tolerance.GetRange(tagMz));

                            QuantPeak qPeak = new QuantPeak(tag, peak, injectionTime, quantitationMsnScan, noise, peak == null && NoisebandCap);

                            peaks[tag.UniqueTagNumber] = qPeak;
                        }

                        PurityCorrect(peaks, isDecoy);

                        PSM psm = new PSM(filenameID, scanNumber, peaks, purity);
                        quantFile.AddPSM(psm);
                    }
                }

                // Dispose of all raw files
                foreach (ThermoRawFile rawFile in RawFiles.Values)
                {
                    rawFile.Dispose();
                }
                OnUpdateLog("PSMs loaded " + quantFile.Psms.Count);
                Log("PSMs Loaded:\t" + quantFile.Psms.Count );
                yield return quantFile;
            }
        }
Exemplo n.º 4
0
 public void AddPSM(PSM psm)
 {
     Psms.Add(psm.FilenameID, psm);
 }
Exemplo n.º 5
0
        private IEnumerable <QuantFile> LoadFiles(IEnumerable <string> filePaths, bool ms3Quant = false)
        {
            MSDataFile.CacheScans = false;
            //int largestQuantPeak = 0;
            int i = 0;

            foreach (TagInformation tag in UsedTags.Values)
            {
                tag.UniqueTagNumber       = i++;
                tag.TotalSignal           = 0;
                tag.NormalizedTotalSignal = 0;
            }
            int largestQuantPeak = i - 1;

            //int largestQuantPeak = UsedTags.Values.Select(tag => tag.UniqueTagNumber).Concat(new[] {0}).Max();

            foreach (string filePath in filePaths)
            {
                Log("Processing file:\t" + filePath);
                OnUpdateLog("Processing File " + filePath + "...");
                QuantFile    quantFile        = new QuantFile(filePath);
                StreamReader basestreamReader = new StreamReader(filePath);
                int          oldProgress      = -1;
                using (CsvReader reader = new CsvReader(basestreamReader, true))
                {
                    while (reader.ReadNextRecord()) // go through csv and raw file to extract the info we want
                    {
                        int           scanNumber  = int.Parse(reader["Spectrum number"]);
                        string        filenameID  = reader["Filename/id"];
                        string        rawFileName = filenameID.Split('.')[0];
                        bool          isDecoy     = reader["DEFLINE"].StartsWith("DECOY_");
                        ThermoRawFile rawFile;
                        if (!RawFiles.TryGetValue(rawFileName, out rawFile))
                        {
                            throw new ArgumentException("Cannot find this raw file: " + rawFileName + ".raw");
                        }
                        if (!rawFile.IsOpen)
                        {
                            rawFile.Open();
                        }

                        int progress = (int)(100 * (double)basestreamReader.BaseStream.Position / basestreamReader.BaseStream.Length);
                        if (progress != oldProgress)
                        {
                            OnProgressUpdate(progress);
                            oldProgress = progress;
                        }

                        //// Set default fragmentation to CAD / HCD
                        //FragmentationMethod ScanFragMethod = filenameID.Contains(".ETD.")
                        //    ? FragmentationMethod.ETD
                        //    : FragmentationMethod.CAD;

                        //if (ScanFragMethod == FragmentationMethod.ETD)
                        //{
                        //    ScanFragMethod = FragmentationMethod.CAD;
                        //    scanNumber += ETDQuantPosition;
                        //}

                        // Get the scan object for the sequence ms2 scan
                        MsnDataScan quantitationMsnScan = rawFile[scanNumber] as MsnDataScan;

                        double purity = 1;
                        if (CalculatePurity)
                        {
                            double      mz             = quantitationMsnScan.PrecursorMz;
                            int         charge         = quantitationMsnScan.PrecursorCharge;
                            DoubleRange isolationRange = MzRange.FromDa(mz, PurityWindowInTh);

                            MSDataScan parentScan = rawFile[quantitationMsnScan.ParentScanNumber];
                            purity = DeterminePurity(parentScan, mz, charge, isolationRange);
                        }

                        if (quantitationMsnScan == null)
                        {
                            OnUpdateLog("Spectrum Number " + scanNumber + " is not a valid MS2 scan from: " + rawFile.FilePath + ". Skipping PSM...");
                            continue;
                        }

                        if (MS3Quant)
                        {
                            quantitationMsnScan = null;
                            // Look forward to find associated MS3 quant scan (based on parent scan number)
                            int ms3ScanNumber = scanNumber + 1;
                            while (ms3ScanNumber < rawFile.LastSpectrumNumber)
                            {
                                if (rawFile.GetParentSpectrumNumber(ms3ScanNumber) == scanNumber)
                                {
                                    quantitationMsnScan = rawFile[ms3ScanNumber] as MsnDataScan;
                                    break;
                                }
                                ms3ScanNumber++;
                            }

                            if (quantitationMsnScan == null)
                            {
                                OnUpdateLog("Cannot find a MS3 spectrum associated with spectrum number " + scanNumber + ". Skipping PSM...");
                                continue;
                            }
                        }

                        Tolerance Tolerance = quantitationMsnScan.MzAnalyzer == MZAnalyzerType.IonTrap2D ? ItMassTolerance : FtMassTolerance;
                        bool      isETD     = quantitationMsnScan.DissociationType == DissociationType.ETD;

                        double injectionTime = quantitationMsnScan.InjectionTime;
                        //var massSpectrum = quantitationMsnScan.MassSpectrum;
                        var thermoSpectrum = rawFile.GetLabeledSpectrum(quantitationMsnScan.SpectrumNumber);

                        double noise = 0;
                        if (NoisebandCap)
                        {
                            // Noise is pretty constant over a small region, find the noise of the center of all isobaric tags
                            MassRange range = new MassRange(UsedTags.Keys[0], UsedTags.Keys[UsedTags.Count - 1]);

                            if (thermoSpectrum != null)
                            {
                                var peak = thermoSpectrum.GetClosestPeak(range.Mean, 500);
                                if (peak != null)
                                {
                                    noise = peak.Noise;
                                }
                                else
                                {
                                    OnUpdateLog("Spectrum (#" + quantitationMsnScan.SpectrumNumber + ") has no m/z peaks. Skipping PSM...");
                                    continue;
                                }
                            }
                            else
                            {
                                OnUpdateLog("Spectrum (#" + quantitationMsnScan.SpectrumNumber + ") is low-resolution data without noise information. Skipping PSM...");
                                continue;
                            }
                        }

                        //Dictionary<TagInformation, QuantPeak> peaks = new Dictionary<TagInformation, QuantPeak>();
                        QuantPeak[] peaks = new QuantPeak[largestQuantPeak + 1];
                        // Read in the peak data
                        foreach (TagInformation tag in UsedTags.Values)
                        {
                            double tagMz = isETD
                                ? tag.MassEtd
                                : tag.MassCAD;

                            var peak = thermoSpectrum.GetClosestPeak(Tolerance.GetRange(tagMz));

                            QuantPeak qPeak = new QuantPeak(tag, peak, injectionTime, quantitationMsnScan, noise, peak == null && NoisebandCap);

                            peaks[tag.UniqueTagNumber] = qPeak;
                        }

                        PurityCorrect(peaks, isDecoy);

                        PSM psm = new PSM(filenameID, scanNumber, peaks, purity);
                        quantFile.AddPSM(psm);
                    }
                }

                // Dispose of all raw files
                foreach (ThermoRawFile rawFile in RawFiles.Values)
                {
                    rawFile.Dispose();
                }
                OnUpdateLog("PSMs loaded " + quantFile.Psms.Count);
                Log("PSMs Loaded:\t" + quantFile.Psms.Count);
                yield return(quantFile);
            }
        }
Exemplo n.º 6
0
        private void WriteOutputFiles(IEnumerable <QuantFile> quantFiles)
        {
            OnUpdateLog("Writing output files...");
            foreach (QuantFile file in quantFiles)
            {
                string filePath = file.FilePath;
                string fileName = Path.GetFileNameWithoutExtension(filePath);

                string outputFilePath = Path.Combine(OutputDirectory, fileName + "_quant.csv");
                OnUpdateLog("Writing " + outputFilePath + "...");
                using (CsvReader reader = new CsvReader(new StreamReader(filePath), true))
                {
                    using (StreamWriter writer = new StreamWriter(outputFilePath))
                    {
                        StringBuilder sb            = new StringBuilder();
                        string[]      headerColumns = reader.GetFieldHeaders();
                        int           headerCount   = headerColumns.Length;
                        string        header        = string.Join(",", headerColumns);
                        sb.Append(header);

                        if (CalculatePurity)
                        {
                            sb.Append(",Precursor Purity (%)");
                        }

                        // Raw Intensities
                        foreach (TagInformation tag in UsedTags.Values)
                        {
                            sb.AppendFormat(",{0} ({1} NL)", tag.SampleName, tag.TagName);
                        }

                        // Denormalized Intensities
                        foreach (TagInformation tag in UsedTags.Values)
                        {
                            sb.AppendFormat(",{0} ({1} dNL)", tag.SampleName, tag.TagName);
                        }

                        // Purity Corrected Intensities
                        foreach (TagInformation tag in UsedTags.Values)
                        {
                            sb.AppendFormat(",{0} ({1} PC)", tag.SampleName, tag.TagName);
                        }

                        // Purity Corrected Normalized Intensities
                        foreach (TagInformation tag in UsedTags.Values)
                        {
                            sb.AppendFormat(",{0} ({1} PCN)", tag.SampleName, tag.TagName);
                        }


                        // Number of Channels Detected
                        sb.Append(",Channels Detected");

                        writer.WriteLine(sb.ToString());

                        while (reader.ReadNextRecord())
                        {
                            sb.Clear();

                            string[] inputData = new string[headerCount];
                            reader.CopyCurrentRecordTo(inputData);
                            for (int i = 0; i < inputData.Length; i++)
                            {
                                string data = inputData[i];
                                if (data.Contains('"'))
                                {
                                    data = data.Replace("\"", "\"\"");
                                }

                                if (data.Contains(','))
                                {
                                    sb.Append('"');
                                    sb.Append(data);
                                    sb.Append('"');
                                }
                                else
                                {
                                    sb.Append(data);
                                }

                                sb.Append(',');
                            }
                            sb.Remove(sb.Length - 1, 1);
                            //sb.Append(string.Join(",", inputData));

                            //int scanNumber = int.Parse(reader["Spectrum number"]);
                            string fileId = reader["Filename/id"];

                            PSM psm = file[fileId];

                            List <QuantPeak> peaks =
                                (from TagInformation tag in UsedTags.Values select psm[tag]).ToList();

                            // Print out precrusor purity if requested
                            if (CalculatePurity)
                            {
                                sb.Append(',');
                                sb.Append(psm.Purity * 100);
                            }

                            // Raw Intensities
                            foreach (QuantPeak peak in peaks)
                            {
                                sb.Append(',');
                                sb.Append(peak.RawIntensity);
                            }

                            // Denormalized Intensities
                            foreach (QuantPeak peak in peaks)
                            {
                                sb.Append(',');
                                sb.Append(peak.DeNormalizedIntensity);
                            }

                            // Purity Corrected Intensities
                            foreach (QuantPeak peak in peaks)
                            {
                                sb.Append(',');
                                sb.Append(peak.PurityCorrectedIntensity);
                            }

                            // Purity Corrected Normalized Intensities
                            foreach (QuantPeak peak in peaks)
                            {
                                sb.Append(',');
                                sb.Append(peak.PurityCorrectedNormalizedIntensity);
                            }

                            // Number of Channels Detected (positive raw intensity)
                            int channelsDetected = peaks.Count(p => p.RawIntensity > 0);
                            sb.Append(',');
                            sb.Append(channelsDetected);

                            writer.WriteLine(sb.ToString());
                        }
                    }
                }
            }
        }