Esempio n. 1
0
        public void ParallelRun(string path, string outputDir, AveragineType type, ChargerType charger)
        {
            string file   = Path.GetFileNameWithoutExtension(path) + ".mzML";
            string output = Path.Combine(outputDir, file);

            MZMLProducer mZMLProducer = new MZMLProducer();
            var          model        = mZMLProducer.Produce(path, progressingCounter.Add, type, (mzMLWriter.ChargerType)charger);

            var serializer = new XmlSerializer(model.GetType());
            var encoding   = Encoding.GetEncoding("ISO-8859-1");
            XmlWriterSettings xmlWriterSettings = new XmlWriterSettings
            {
                Indent             = true,
                OmitXmlDeclaration = false,
                Encoding           = Encoding.UTF8
            };

            using (FileStream ostrm = new FileStream(output, FileMode.OpenOrCreate, FileAccess.Write))
            {
                using (StreamWriter writer = new StreamWriter(ostrm))
                {
                    using (XmlWriter xmlWriter = XmlWriter.Create(writer, xmlWriterSettings))
                    {
                        serializer.Serialize(xmlWriter, model);
                    }
                }
            }
            // update progress
            progress.Add();
        }
 private void SelectTypes(object sender, RoutedEventArgs e)
 {
     if (Peptides.IsChecked == true)
     {
         type = AveragineType.Peptide;
     }
     else if (Glycopeptides.IsChecked == true)
     {
         type = AveragineType.GlycoPeptide;
     }
     else if (Glycan.IsChecked == true)
     {
         type = AveragineType.Glycan;
     }
     else if (PermethylatedGlycan.IsChecked == true)
     {
         type = AveragineType.PermethylatedGlycan;
     }
 }
Esempio n. 3
0
        public Dictionary <Element, double> Composition(AveragineType type)
        {
            switch (type)
            {
            case AveragineType.Peptide:
                return(Peptide);

            case AveragineType.GlycoPeptide:
                return(Glycopeptide);

            case AveragineType.Glycan:
                return(Glycan);

            case AveragineType.PermethylatedGlycan:
                return(PermethylatedGlycan);

            default:
                break;
            }
            return(Peptide);
        }
Esempio n. 4
0
 public Averagine(AveragineType type = AveragineType.GlycoPeptide)
 {
     Type = type;
 }
Esempio n. 5
0
        public void ParallelRun(string path, string outputDir, AveragineType type, ChargerType chargerType)
        {
            string file   = Path.GetFileNameWithoutExtension(path) + ".mgf";
            string output = Path.Combine(outputDir, file);

            ThermoRawSpectrumReader reader  = new ThermoRawSpectrumReader();
            LocalMaximaPicking      picking = new LocalMaximaPicking(ms1PrcisionPPM);

            reader.Init(path);

            Dictionary <int, List <int> > scanGroup = new Dictionary <int, List <int> >();
            int current = -1;
            int start   = reader.GetFirstScan();
            int end     = reader.GetLastScan();

            for (int i = start; i < end; i++)
            {
                if (reader.GetMSnOrder(i) == 1)
                {
                    current      = i;
                    scanGroup[i] = new List <int>();
                }
                else if (reader.GetMSnOrder(i) == 2)
                {
                    scanGroup[current].Add(i);
                }
            }

            List <MS2Info> ms2Infos = new List <MS2Info>();

            Parallel.ForEach(scanGroup, (scanPair) =>
            {
                if (scanPair.Value.Count > 0)
                {
                    ISpectrum ms1 = reader.GetSpectrum(scanPair.Key);
                    foreach (int i in scanPair.Value)
                    {
                        double mz             = reader.GetPrecursorMass(i, reader.GetMSnOrder(i));
                        List <IPeak> ms1Peaks = FilterPeaks(ms1.GetPeaks(), mz, searchRange);

                        if (ms1Peaks.Count() == 0)
                        {
                            continue;
                        }

                        // insert pseudo peaks for large gap
                        List <IPeak> peaks = new List <IPeak>();
                        double precision   = 0.02;
                        double last        = ms1Peaks.First().GetMZ();
                        foreach (IPeak peak in ms1Peaks)
                        {
                            if (peak.GetMZ() - last > precision)
                            {
                                peaks.Add(new GeneralPeak(last + precision / 2, 0));
                                peaks.Add(new GeneralPeak(peak.GetMZ() - precision / 2, 0));
                            }
                            peaks.Add(peak);
                            last = peak.GetMZ();
                        }
                        List <IPeak> majorPeaks = picking.Process(peaks);
                        ICharger charger        = new Patterson();
                        if (chargerType == ChargerType.Fourier)
                        {
                            charger = new Fourier();
                        }
                        else if (chargerType == ChargerType.Combined)
                        {
                            charger = new PattersonFourierCombine();
                        }
                        int charge = charger.Charge(peaks, mz - searchRange, mz + searchRange);

                        // find evelope cluster
                        EnvelopeProcess envelope = new EnvelopeProcess();
                        var cluster = envelope.Cluster(majorPeaks, mz, charge);
                        if (cluster.Count == 0)
                        {
                            continue;
                        }

                        // find monopeak
                        Averagine averagine           = new Averagine(type);
                        BrainCSharp braincs           = new BrainCSharp();
                        MonoisotopicSearcher searcher = new MonoisotopicSearcher(averagine, braincs);
                        MonoisotopicScore result      = searcher.Search(mz, charge, cluster);
                        double precursorMZ            = result.GetMZ();

                        // write mgf
                        ISpectrum ms2      = reader.GetSpectrum(i);
                        IProcess processer = new WeightedAveraging(new LocalNeighborPicking());
                        ms2 = processer.Process(ms2);

                        MS2Info ms2Info = new MS2Info
                        {
                            PrecursorMZ     = result.GetMZ(),
                            PrecursorCharge = charge,
                            Scan            = ms2.GetScanNum(),
                            Retention       = ms2.GetRetention(),
                            Peaks           = ms2.GetPeaks()
                        };
                        lock (resultLock)
                        {
                            ms2Infos.Add(ms2Info);
                        }
                    }
                }
                readingProgress.Add(scanGroup.Count);
            });

            ms2Infos = ms2Infos.OrderBy(m => m.Scan).ToList();
            using (FileStream ostrm = new FileStream(output, FileMode.OpenOrCreate, FileAccess.Write))
            {
                using (StreamWriter writer = new StreamWriter(ostrm))
                {
                    foreach (MS2Info ms2 in ms2Infos)
                    {
                        WriteMGF(writer, path + ",SCANS=" + ms2.Scan.ToString() + ",PRECURSOR=" + ms2.PrecursorMZ, ms2.PrecursorMZ, ms2.PrecursorCharge,
                                 ms2.Scan, ms2.Retention * 60, reader.GetActivation(ms2.Scan), ms2.Peaks);
                        writer.Flush();
                    }
                }
            }

            // update progress
            progress.Add();
        }
        public Run Read(string path, AveragineType type, ChargerType charger, string defaultDataProcessingRef,
                        ProgressUpdate updater)
        {
            Run data = new Run();

            // init reader
            ThermoRawSpectrumReader reader  = new ThermoRawSpectrumReader();
            LocalMaximaPicking      picking = new LocalMaximaPicking(ms1PrcisionPPM);
            IProcess process = new WeightedAveraging(new LocalNeighborPicking());

            reader.Init(path);

            data.spectrumList = new SpectrumList();
            Dictionary <int, Spectrum> spectrumMap = new Dictionary <int, Spectrum>();

            int start = reader.GetFirstScan();
            int end   = reader.GetLastScan();
            Dictionary <int, List <int> > scanGroup = new Dictionary <int, List <int> >();
            int current = -1;

            for (int i = start; i < end; i++)
            {
                if (reader.GetMSnOrder(i) == 1)
                {
                    current      = i;
                    scanGroup[i] = new List <int>();
                }
                else if (reader.GetMSnOrder(i) == 2)
                {
                    scanGroup[current].Add(i);
                }
            }

            Parallel.ForEach(scanGroup, (scanPair) => {
                if (scanPair.Value.Count > 0)
                {
                    int parentScan          = scanPair.Key;
                    ISpectrum ms1           = null;
                    List <IPeak> majorPeaks = new List <IPeak>();
                    Spectrum ms1Spectrum    =
                        ThermoRawRunFactoryHelper.GetMS1Spectrum(ref reader, parentScan, ref ms1);
                    if (ms1Spectrum != null)
                    {
                        lock (resultLock)
                        {
                            spectrumMap[parentScan] = ms1Spectrum;
                        }
                    }

                    foreach (int scan in scanPair.Value)
                    {
                        Spectrum ms2Spectrum =
                            ThermoRawRunFactoryHelper.GetMS2Spectrum(ref reader, scan, type, charger, picking, process, ms1);
                        if (ms2Spectrum != null)
                        {
                            lock (resultLock)
                            {
                                spectrumMap[scan] = ms2Spectrum;
                            }
                        }
                    }
                }
                updater(scanGroup.Count);
            });

            List <Spectrum> spectrumList =
                spectrumMap.OrderBy(s => s.Key).Select(s => s.Value).ToList();

            data.spectrumList.spectrum = new Spectrum[spectrumList.Count];
            spectrumList = spectrumList.OrderBy(x => int.Parse(x.id.Substring(5))).ToList();
            for (int i = 0; i < spectrumList.Count; i++)
            {
                int scan = int.Parse(spectrumList[i].id.Substring(5));
                data.spectrumList.spectrum[i]       = spectrumList[i];
                data.spectrumList.spectrum[i].index = (scan - 1).ToString();
                data.spectrumList.spectrum[i].defaultArrayLength = spectrumList[i].defaultArrayLength;
            }
            data.spectrumList.count = spectrumList.Count.ToString();
            data.spectrumList.defaultDataProcessingRef = defaultDataProcessingRef;

            return(data);
        }
Esempio n. 7
0
        public MSmzML Produce(string path, ProgressUpdate updater, AveragineType type, ChargerType charger)
        {
            var model = new MSmzML();

            // cvList
            model.cvList = new CVList();
            CV[] cvArray = new CV[2];
            cvArray[0] = new CV()
            {
                id       = "MS",
                fullName = "Proteomics Standards Initiative Mass Spectrometry Ontology",
                version  = "2.26.0",
                URI      = "http://psidev.cvs.sourceforge.net/*checkout*/psidev/psi/psi-ms/mzML/controlledVocabulary/psi-ms.obo"
            };
            cvArray[1] = new CV
            {
                id       = "UO",
                fullName = "Unit Ontology",
                version  = "14:07:2009",
                URI      = "http://obo.cvs.sourceforge.net/*checkout*/obo/obo/ontology/phenotype/unit.obo"
            };
            model.cvList.cv    = cvArray;
            model.cvList.count = "2";

            // fileDescription
            model.fileDescription                        = new FileDescription();
            model.fileDescription.fileContent            = new FileContent();
            model.fileDescription.fileContent.cvParam    = new CVParam[2];
            model.fileDescription.fileContent.cvParam[0] = new CVParam
            {
                cvRef     = "MS",
                accession = "MS:1000580",
                name      = "MSn spectrum",
                value     = ""
            };
            model.fileDescription.fileContent.cvParam[1] = new CVParam
            {
                cvRef     = "MS",
                accession = "MS:1000127",
                name      = "centroid spectrum",
                value     = ""
            };
            model.fileDescription.sourceFileList            = new SourceFileList();
            model.fileDescription.sourceFileList.sourceFile = new SourceFile[1];
            string fileName = Path.GetFileName(path);

            model.fileDescription.sourceFileList.sourceFile[0] = new SourceFile
            {
                id       = fileName,
                name     = fileName,
                location = path
            };
            model.fileDescription.sourceFileList.count = "1";
            model.fileDescription.contact            = new Contact();
            model.fileDescription.contact.cvParam    = new CVParam[2];
            model.fileDescription.contact.cvParam[0] = new CVParam
            {
                cvRef     = "MS",
                accession = "MS:1000586",
                name      = "contact name",
                value     = contactPerson
            };
            model.fileDescription.contact.cvParam[1] = new CVParam
            {
                cvRef     = "MS",
                accession = "MS:1000589",
                name      = "contact email",
                value     = contactEmail
            };

            // softwareList
            model.softwareList             = new SoftwareList();
            model.softwareList.software    = new Software[1];
            model.softwareList.software[0] = new Software
            {
                id      = software,
                version = softwareVersion
            };
            model.softwareList.count = "1";

            // instrumentConfiguration
            string instrumentConfigurationID = "UNKNOWN";

            model.instrumentConfigurationList = new InstrumentConfigurationList();
            model.instrumentConfigurationList.instrumentConfiguration    = new InstrumentConfiguration[1];
            model.instrumentConfigurationList.instrumentConfiguration[0] = new InstrumentConfiguration()
            {
                id = instrumentConfigurationID
            };
            model.instrumentConfigurationList.count = "1";

            // data processing
            model.dataProcessingList = new DataProcessingList();
            model.dataProcessingList.dataProcessing       = new DataProcessing[1];
            model.dataProcessingList.dataProcessing[0]    = new DataProcessing();
            model.dataProcessingList.dataProcessing[0].id = dataProcessingID;
            model.dataProcessingList.dataProcessing[0].processingMethod    = new ProcessingMethod[1];
            model.dataProcessingList.dataProcessing[0].processingMethod[0] = new ProcessingMethod()
            {
                order       = "1",
                softwareRef = software
            };
            model.dataProcessingList.dataProcessing[0].processingMethod[0].cvParam    = new CVParam[2];
            model.dataProcessingList.dataProcessing[0].processingMethod[0].cvParam[0] = new CVParam()
            {
                cvRef     = "MS",
                accession = "MS:1000035",
                name      = "peak picking",
                value     = ""
            };
            model.dataProcessingList.dataProcessing[0].processingMethod[0].cvParam[1] = new CVParam()
            {
                cvRef     = "MS",
                accession = "MS:1000544",
                name      = "Conversion to mzML",
                value     = ""
            };
            model.dataProcessingList.count = "1";

            // spectrum data
            ThermoRawRunFactory factory = new ThermoRawRunFactory();

            model.run = factory.Read(path, type, charger,
                                     model.dataProcessingList.dataProcessing[0].id, updater);
            model.run.id = Guid.NewGuid().ToString();
            model.run.defaultInstrumentConfigurationRef = instrumentConfigurationID;
            return(model);
        }
        public static Spectrum GetMS2Spectrum(ref ThermoRawSpectrumReader reader,
                                              int scan, AveragineType type, ChargerType chargerType, LocalMaximaPicking picking, IProcess process,
                                              ISpectrum ms1)
        {
            // scan header
            Spectrum spectrum = new Spectrum
            {
                id = "scan=" + scan.ToString()
            };

            double dLowMass           = 0;
            double dHighMass          = 0;
            double dTIC               = 0;
            double dBasePeakMass      = 0;
            double dBasePeakIntensity = 0;

            reader.GetScanHeaderInfoForScanNum(scan, ref dLowMass, ref dHighMass,
                                               ref dTIC, ref dBasePeakMass, ref dBasePeakIntensity);
            SetScanHeader(spectrum, dLowMass, dHighMass, dTIC,
                          dBasePeakMass, dBasePeakIntensity);

            // binary data
            spectrum.binaryDataArrayList = new BinaryDataArrayList();
            SetBinaryDataArrayHeader(spectrum.binaryDataArrayList);

            spectrum.cvParam[0] = new Component.CVParam()
            {
                cvRef     = "MS",
                accession = "MS:1000511",
                name      = "ms level",
                value     = "2",
            };

            double       mz       = reader.GetPrecursorMass(scan, reader.GetMSnOrder(scan));
            List <IPeak> ms1Peaks = FilterPeaks(ms1.GetPeaks(), mz, searchRange);

            if (ms1Peaks.Count() == 0)
            {
                return(null);
            }

            // insert pseudo peaks for large gaps
            List <IPeak> peaks     = new List <IPeak>();
            double       precision = 0.02;
            double       last      = ms1Peaks.First().GetMZ();

            foreach (IPeak peak in ms1Peaks)
            {
                if (peak.GetMZ() - last > precision)
                {
                    peaks.Add(new GeneralPeak(last + precision / 2, 0));
                    peaks.Add(new GeneralPeak(peak.GetMZ() - precision / 2, 0));
                }
                peaks.Add(peak);
                last = peak.GetMZ();
            }
            List <IPeak> majorPeaks = picking.Process(peaks);
            ICharger     charger    = new Patterson();

            if (chargerType == ChargerType.Fourier)
            {
                charger = new Fourier();
            }
            else if (chargerType == ChargerType.Combined)
            {
                charger = new PattersonFourierCombine();
            }
            int charge = charger.Charge(peaks, mz - searchRange, mz + searchRange);

            // find evelope cluster
            EnvelopeProcess envelope = new EnvelopeProcess();
            var             cluster  = envelope.Cluster(majorPeaks, mz, charge);

            if (cluster.Count == 0)
            {
                return(null);
            }

            // find monopeak
            Averagine            averagine = new Averagine(type);
            BrainCSharp          braincs   = new BrainCSharp();
            MonoisotopicSearcher searcher  = new MonoisotopicSearcher(averagine, braincs);
            MonoisotopicScore    result    = searcher.Search(mz, charge, cluster);

            // process spectrum
            ISpectrum ms2 = reader.GetSpectrum(scan);

            List <IPeak> ms2Peaks = process.Process(ms2).GetPeaks();

            spectrum.binaryDataArrayList.binaryDataArray[0].binary =
                ms2Peaks.SelectMany(p => BitConverter.GetBytes(p.GetMZ())).ToArray();
            spectrum.binaryDataArrayList.binaryDataArray[1].binary =
                ms2Peaks.SelectMany(p => BitConverter.GetBytes(p.GetIntensity())).ToArray();
            spectrum.defaultArrayLength = ms2Peaks.Count.ToString();

            spectrum.precursorList = new PrecursorList
            {
                count     = "1",
                precursor = new Precursor[1]
            };
            for (int i = 0; i < spectrum.precursorList.precursor.Length; i++)
            {
                spectrum.precursorList.precursor[i] = new Precursor();
            }

            spectrum.precursorList.precursor[0].selectedIonList = new SelectedIonList
            {
                count       = "1",
                selectedIon = new SelectedIon[1]
            };
            for (int i = 0; i < spectrum.precursorList.precursor[0].selectedIonList.selectedIon.Length; i++)
            {
                spectrum.precursorList.precursor[0].selectedIonList.selectedIon[i] = new SelectedIon();
            }
            spectrum.precursorList.precursor[0].selectedIonList.selectedIon[0].cvParam    = new Component.CVParam[2];
            spectrum.precursorList.precursor[0].selectedIonList.selectedIon[0].cvParam[0] = new Component.CVParam()
            {
                cvRef         = "MS",
                accession     = "MS:1000744",
                name          = "selected ion m/z",
                value         = result.GetMZ().ToString(),
                unitCvRef     = "MS",
                unitAccession = "MS:1000040",
                unitName      = "m/z"
            };
            spectrum.precursorList.precursor[0].selectedIonList.selectedIon[0].cvParam[1] = new Component.CVParam()
            {
                cvRef     = "MS",
                accession = "MS:1000041",
                name      = "charge state",
                value     = charge.ToString()
            };
            spectrum.precursorList.precursor[0].activation = new Activation
            {
                cvParam = new Component.CVParam[1]
            };
            spectrum.precursorList.precursor[0].activation.cvParam[0] =
                ActivationCVParam(reader.GetActivation(scan));

            spectrum.binaryDataArrayList.binaryDataArray[0].encodedLength =
                Convert.ToBase64String(spectrum.binaryDataArrayList.binaryDataArray[0].binary).Length.ToString();
            spectrum.binaryDataArrayList.binaryDataArray[1].encodedLength =
                Convert.ToBase64String(spectrum.binaryDataArrayList.binaryDataArray[1].binary).Length.ToString();
            return(spectrum);
        }