Esempio n. 1
0
 /// <summary>
 /// Mass-to-charge ratio of a precursor ion selected for fragmentation. [PSI:PI]
 /// </summary>
 public static SelectedIon SetSelectedIonMz(
     this SelectedIon si, double mz)
 {
     if (mz < 0)
     {
         throw new ArgumentOutOfRangeException("mz");
     }
     return(si.SetCvParam(SelectedIonMz, mz).PSIMS_Mz());
 }
Esempio n. 2
0
        /// <summary>
        /// Mass-to-charge ratio of a precursor ion selected for fragmentation. [PSI:PI]
        /// </summary>
        public static bool TryGetSelectedIonMz(
            this SelectedIon si, out double mz)
        {
            CvParam p;

            if (si.TryGetParam(SelectedIonMz, out p))
            {
                mz = p.GetDouble();
                return(true);
            }
            else
            {
                mz = default(double);
                return(false);
            }
        }
Esempio n. 3
0
        private MzLite.Model.MassSpectrum ReadMassSpectrum(int scanNo)
        {
            RaiseDisposed();

            try
            {
                string       spectrumID = GetSpectrumID(scanNo);
                MassSpectrum spectrum   = new MassSpectrum(spectrumID);

                // spectrum

                int msLevel = GetMSLevel(rawFile, scanNo);
                spectrum.SetMsLevel(msLevel);

                if (IsCentroidSpectrum(rawFile, scanNo))
                {
                    spectrum.SetCentroidSpectrum();
                }
                else
                {
                    spectrum.SetProfileSpectrum();
                }

                // scan

                Scan scan = new Scan();
                scan.SetFilterString(GetFilterString(rawFile, scanNo))
                .SetScanStartTime(GetRetentionTime(rawFile, scanNo));
                //.UO_Minute();

                spectrum.Scans.Add(scan);

                // precursor

                if (msLevel > 1)
                {
                    Precursor precursor = new Precursor();

                    double isoWidth    = GetIsolationWindowWidth(rawFile, scanNo, msLevel) * 0.5d;
                    double targetMz    = GetIsolationWindowTargetMz(rawFile, scanNo, msLevel);
                    double precursorMz = GetPrecursorMz(rawFile, scanNo, msLevel);
                    int    chargeState = GetChargeState(rawFile, scanNo);

                    precursor.IsolationWindow
                    .SetIsolationWindowTargetMz(targetMz)
                    .SetIsolationWindowUpperOffset(isoWidth)
                    .SetIsolationWindowLowerOffset(isoWidth);

                    SelectedIon selectedIon = new SelectedIon();

                    selectedIon
                    .SetSelectedIonMz(precursorMz)
                    .SetChargeState(chargeState);

                    precursor.SelectedIons.Add(selectedIon);

                    spectrum.Precursors.Add(precursor);
                }

                return(spectrum);
            }
            catch (Exception ex)
            {
                throw new MzLiteIOException(ex.Message, ex);
            }
        }
Esempio n. 4
0
        private static MzLite.Model.MassSpectrum GetSpectrum(
            Batch batch,
            MassSpectrometerSample sample,
            MSExperiment msExp,
            int sampleIndex,
            int experimentIndex,
            int scanIndex)
        {
            MassSpectrumInfo wiffSpectrum = msExp.GetMassSpectrumInfo(scanIndex);

            MzLite.Model.MassSpectrum mzLiteSpectrum = new Model.MassSpectrum(ToSpectrumID(sampleIndex, experimentIndex, scanIndex));

            // spectrum

            mzLiteSpectrum.SetMsLevel(wiffSpectrum.MSLevel);

            if (wiffSpectrum.CentroidMode)
            {
                mzLiteSpectrum.SetCentroidSpectrum();
            }
            else
            {
                mzLiteSpectrum.SetProfileSpectrum();
            }

            // scan

            Scan scan = new Scan();

            scan.SetScanStartTime(wiffSpectrum.StartRT)
            .UO_Minute();

            mzLiteSpectrum.Scans.Add(scan);

            // precursor

            if (wiffSpectrum.IsProductSpectrum)
            {
                Precursor precursor = new Precursor();

                double isoWidth;
                double targetMz;

                if (GetIsolationWindow(wiffSpectrum.Experiment, out isoWidth, out targetMz))
                {
                    precursor.IsolationWindow
                    .SetIsolationWindowTargetMz(targetMz)
                    .SetIsolationWindowUpperOffset(isoWidth)
                    .SetIsolationWindowLowerOffset(isoWidth);
                }

                SelectedIon selectedIon = new SelectedIon();

                selectedIon.SetSelectedIonMz(wiffSpectrum.ParentMZ)
                .SetChargeState(wiffSpectrum.ParentChargeState);

                precursor.SelectedIons.Add(selectedIon);

                precursor.Activation
                .SetCollisionEnergy(wiffSpectrum.CollisionEnergy);

                mzLiteSpectrum.Precursors.Add(precursor);
            }

            return(mzLiteSpectrum);
        }
Esempio n. 5
0
        /// <summary>
        /// Handle a single selectedIonList element and child nodes
        /// Called by ReadPrecursor (xml hierarchy)
        /// </summary>
        /// <param name="reader">XmlReader that is only valid for the scope of the single selectedIonList element</param>
        /// <returns></returns>
        private List<SelectedIon> ReadSelectedIonList(XmlReader reader)
        {
            reader.MoveToContent();
            //int count = Convert.ToInt32(reader.GetAttribute("count"));
            var ions = new List<SelectedIon>();
            reader.ReadStartElement("selectedIonList"); // Throws exception if we are not at the "selectedIonList" tag.
            while (reader.ReadState == ReadState.Interactive)
            {
                // Handle exiting out properly at EndElement tags
                if (reader.NodeType != XmlNodeType.Element)
                {
                    reader.Read();
                    continue;
                }

                switch (reader.Name)
                {
                    case "selectedIon":
                        // Schema requirements: one to many instances of this element
                        var ion = new SelectedIon();
                        var innerReader = reader.ReadSubtree();
                        innerReader.MoveToContent();
                        innerReader.ReadStartElement("selectedIon"); // Throws exception if we are not at the "selectedIon" tag.
                        while (innerReader.ReadState == ReadState.Interactive)
                        {
                            // Handle exiting out properly at EndElement tags
                            if (innerReader.NodeType != XmlNodeType.Element)
                            {
                                innerReader.Read();
                                continue;
                            }
                            switch (innerReader.Name)
                            {
                                case "referenceableParamGroupRef":
                                    // Schema requirements: zero to many instances of this element
                                    innerReader.Skip();
                                    break;
                                case "cvParam":
                                    // Schema requirements: zero to many instances of this element
                                    /* MUST supply a *child* term of MS:1000455 (ion selection attribute) one or more times
                                     *   e.g.: MS:1000041 (charge state)
                                     *   e.g.: MS:1000042 (intensity)
                                     *   e.g.: MS:1000633 (possible charge state)
                                     *   e.g.: MS:1000744 (selected ion m/z)
                                     */
                                    switch (innerReader.GetAttribute("accession"))
                                    {
                                        case "MS:1000041":
                                            // name="charge state"
                                            ion.Charge = (int)Convert.ToDouble(innerReader.GetAttribute("value"));
                                            break;
                                        case "MS:1000744":
                                            // name="selected ion m/z"
                                            ion.SelectedIonMz = Convert.ToDouble(innerReader.GetAttribute("value"));
                                            break;
                                    }
                                    innerReader.Read(); // Consume the cvParam element (no child nodes)
                                    break;
                                case "userParam":
                                    // Schema requirements: zero to many instances of this element
                                    if (innerReader.GetAttribute("name") == "old charge state")
                                    {
                                        ion.OldCharge = (int) Convert.ToDouble(innerReader.GetAttribute("value"));
                                    }
                                    innerReader.Read();
                                    break;
                                default:
                                    innerReader.Skip();
                                    break;
                            }
                        }
                        innerReader.Close();
                        ions.Add(ion);
                        reader.Read(); // "selectedIon" might not have child nodes
                        // We will either consume the EndElement, or the same element that was passed to ReadSpectrum (in case of no child nodes)
                        break;
                    default:
                        reader.Skip();
                        break;
                }
            }
            reader.Close();
            return ions;
        }
Esempio n. 6
0
        /// <summary>
        /// Handle a single spectrum element and child nodes
        /// Called by ReadSpectrumList (xml hierarchy)
        /// </summary>
        /// <param name="reader">XmlReader that is only valid for the scope of the single spectrum element</param>
        /// <param name="includePeaks">Whether to read binary data arrays</param>
        private Spectrum ReadSpectrum(XmlReader reader, bool includePeaks = true)
        {
            reader.MoveToContent();
            string index = reader.GetAttribute("index");
            //Console.WriteLine("Reading spectrum indexed by " + index);
            // This is correct for Thermo files converted by msConvert, but need to implement for others as well
            string spectrumId = reader.GetAttribute("id"); // Native ID in mzML_1.1.0; unique identifier in mzML_1.0.0, often same as nativeID
            string nativeId = spectrumId;
            if (_version == MzML_Version.mzML1_0_0)
            {
                nativeId = reader.GetAttribute("nativeID"); // Native ID in mzML_1.0.0
            }

            int scanNum = -1;
            // If a random access reader, there is already a scan number stored, based on the order of the index. Use it instead.
            if (_randomAccess)
            {
                scanNum = (int) (_spectrumOffsets.NativeToIdMap[nativeId]);
            }
            else
            {
                scanNum = (int)(_artificialScanNum++);
                // Interpret the NativeID (if the format has an interpreter) and use it instead of the artificial number.
                // TODO: Better handling than the artificial ID for other nativeIDs (ones currently not supported)
                int num = 0;
                if (NativeIdConversion.TryGetScanNumberInt(nativeId, out num))
                {
                    scanNum = num;
                }
            }

            int defaultArraySize = Convert.ToInt32(reader.GetAttribute("defaultArrayLength"));
            reader.ReadStartElement("spectrum"); // Throws exception if we are not at the "spectrum" tag.
            bool is_ms_ms = false;
            int msLevel = 0;
            bool centroided = false;
            double tic = 0;
            List<Precursor> precursors = new List<Precursor>();
            List<ScanData> scans = new List<ScanData>();
            List<BinaryDataArray> bdas = new List<BinaryDataArray>();
            while (reader.ReadState == ReadState.Interactive)
            {
                // Handle exiting out properly at EndElement tags
                if (reader.NodeType != XmlNodeType.Element)
                {
                    reader.Read();
                    continue;
                }
                //////////////////////////////////////////////////////////////////////////////////////
                /// 
                /// MS1 Spectra: only need Spectrum data: scanNum, MSLevel, ElutionTime, mzArray, IntensityArray
                /// 
                /// MS2 Spectra: use ProductSpectrum; adds ActivationMethod and IsolationWindow
                /// 
                //////////////////////////////////////////////////////////////////////////////////////
                switch (reader.Name)
                {
                    case "referenceableParamGroupRef":
                        // Schema requirements: zero to many instances of this element
                        reader.Skip();
                        break;
                    case "cvParam":
                        // Schema requirements: zero to many instances of this element
                        /* MAY supply a *child* term of MS:1000465 (scan polarity) only once
                         *   e.g.: MS:1000129 (negative scan)
                         *   e.g.: MS:1000130 (positive scan)
                         * MUST supply a *child* term of MS:1000559 (spectrum type) only once
                         *   e.g.: MS:1000322 (charge inversion mass spectrum)
                         *   e.g.: MS:1000325 (constant neutral gain spectrum)
                         *   e.g.: MS:1000326 (constant neutral loss spectrum)
                         *   e.g.: MS:1000328 (e/2 mass spectrum)
                         *   e.g.: MS:1000341 (precursor ion spectrum)
                         *   e.g.: MS:1000579 (MS1 spectrum)
                         *   e.g.: MS:1000580 (MSn spectrum)
                         *   e.g.: MS:1000581 (CRM spectrum)
                         *   e.g.: MS:1000582 (SIM spectrum)
                         *   e.g.: MS:1000583 (SRM spectrum)
                         *   e.g.: MS:1000620 (PDA spectrum)
                         *   e.g.: MS:1000627 (selected ion current chromatogram)
                         *   e.g.: MS:1000789 (enhanced multiply charged spectrum)
                         *   e.g.: MS:1000790 (time-delayed fragmentation spectrum)
                         *   et al.
                         * MUST supply term MS:1000525 (spectrum representation) or any of its children only once
                         *   e.g.: MS:1000127 (centroid spectrum)
                         *   e.g.: MS:1000128 (profile spectrum)
                         * MAY supply a *child* term of MS:1000499 (spectrum attribute) one or more times
                         *   e.g.: MS:1000285 (total ion current)
                         *   e.g.: MS:1000497 (zoom scan)
                         *   e.g.: MS:1000504 (base peak m/z)
                         *   e.g.: MS:1000505 (base peak intensity)
                         *   e.g.: MS:1000511 (ms level)
                         *   e.g.: MS:1000527 (highest observed m/z)
                         *   e.g.: MS:1000528 (lowest observed m/z)
                         *   e.g.: MS:1000618 (highest observed wavelength)
                         *   e.g.: MS:1000619 (lowest observed wavelength)
                         *   e.g.: MS:1000796 (spectrum title)
                         *   et al.
                         */
                        switch (reader.GetAttribute("accession"))
                        {
                            case "MS:1000127":
                                // name="centroid spectrum"
                                centroided = true;
                                break;
                            case "MS:1000128":
                                // name="profile spectrum"
                                centroided = false;
                                break;
                            case "MS:1000511":
                                // name="ms level"
                                msLevel = Convert.ToInt32(reader.GetAttribute("value"));
                                break;
                            case "MS:1000579":
                                // name="MS1 spectrum"
                                is_ms_ms = false;
                                break;
                            case "MS:1000580":
                                // name="MSn spectrum"
                                is_ms_ms = true;
                                break;
                            case "MS:1000285":
                                // name="total ion current"
                                tic = Convert.ToDouble(reader.GetAttribute("value"));
                                break;
                        }
                        reader.Read(); // Consume the cvParam element (no child nodes)
                        break;
                    case "userParam":
                        // Schema requirements: zero to many instances of this element
                        reader.Skip();
                        break;
                    case "spectrumDescription": // mzML_1.0.0 compatibility
                        // Schema requirements: one instance of this element
                        ReadSpectrumDescription(reader.ReadSubtree(), ref scans, ref precursors, out centroided);
                        reader.ReadEndElement(); // "spectrumDescription" must have child nodes
                        break;
                    case "scanList":
                        // Schema requirements: zero to one instances of this element
                        scans.AddRange(ReadScanList(reader.ReadSubtree()));
                        reader.ReadEndElement(); // "scanList" must have child nodes
                        break;
                    case "precursorList":
                        // Schema requirements: zero to one instances of this element
                        precursors.AddRange(ReadPrecursorList(reader.ReadSubtree()));
                        reader.ReadEndElement(); // "precursorList" must have child nodes
                        break;
                    case "productList":
                        // Schema requirements: zero to one instances of this element
                        reader.Skip();
                        break;
                    case "binaryDataArrayList":
                        // Schema requirements: zero to one instances of this element
                        if (includePeaks)
                        {
                            bdas.AddRange(ReadBinaryDataArrayList(reader.ReadSubtree(), defaultArraySize));
                            reader.ReadEndElement(); // "binaryDataArrayList" must have child nodes
                        }
                        else
                        {
                            reader.Skip();
                        }
                        break;
                    default:
                        reader.Skip();
                        break;
                }
            }
            reader.Close();
            // Process the spectrum data
            ScanData scan = new ScanData();
            Spectrum spectrum;
            BinaryDataArray mzs = new BinaryDataArray();
            BinaryDataArray intensities = new BinaryDataArray();
            foreach (var bda in bdas)
            {
                if (bda.ArrayType == ArrayType.m_z_array)
                {
                    mzs = bda;
                }
                else if (bda.ArrayType == ArrayType.intensity_array)
                {
                    intensities = bda;
                }
            }
            
            if (!centroided && includePeaks)
            {
                // Centroid spectrum
                // ProteoWizard
                var centroider = new Centroider(mzs.Data, intensities.Data);
                double[] centroidedMzs, centroidedIntensities;
                centroider.GetCentroidedData(out centroidedMzs, out centroidedIntensities);
                mzs.Data = centroidedMzs;
                intensities.Data = centroidedIntensities;
            }
            if (scans.Count == 1)
            {
                scan = scans[0];
            }
            else if (scans.Count > 1)
            {
                // TODO: Should do something else to appropriately handle combinations...
                scan = scans[0];
            }

            if (is_ms_ms)
            {
                Precursor precursor = new Precursor();
                if (precursors.Count == 1)
                {
                    precursor = precursors[0];
                }
                else if (precursors.Count > 1)
                {
                    // TODO: Should do something else to appropriately handle multiple precursors...
                    precursor = precursors[0];
                }
                SelectedIon ion = new SelectedIon();
                if (precursor.Ions.Count == 1)
                {
                    ion = precursor.Ions[0];
                }
                else if (precursor.Ions.Count > 1)
                {
                    // TODO: Should do something else to appropriately handle multiple selected ions...
                    ion = precursor.Ions[0];
                }

                var pspectrum = new ProductSpectrum(mzs.Data, intensities.Data, scanNum);
                pspectrum.ActivationMethod = precursor.Activation;
                // Select mz value to use based on presence of a Thermo-specific user param.
                // The user param has a slightly higher precision, if that matters.
                double mz = scan.MonoisotopicMz == 0.0 ? ion.SelectedIonMz : scan.MonoisotopicMz;
                pspectrum.IsolationWindow = new IsolationWindow(precursor.IsolationWindowTargetMz, precursor.IsolationWindowLowerOffset, precursor.IsolationWindowUpperOffset, mz, ion.Charge);
                //pspectrum.IsolationWindow.OldCharge = ion.OldCharge;
                //pspectrum.IsolationWindow.SelectedIonMz = ion.SelectedIonMz;
                spectrum = pspectrum;
            }
            else
            {
                spectrum = new Spectrum(mzs.Data, intensities.Data, scanNum);
            }
            spectrum.MsLevel = msLevel;
            spectrum.ElutionTime = scan.StartTime;
            spectrum.NativeId = nativeId;
            spectrum.TotalIonCurrent = tic;
            
            return spectrum;
        }
Esempio n. 7
0
 /// <summary>
 /// The charge state of the ion, single or multiple and positive or negatively charged. [PSI:MS]
 /// </summary>
 public static SelectedIon SetChargeState(
     this SelectedIon si, int state)
 {
     return(si.SetCvParam(ChargeState, state).NoUnit());
 }
Esempio n. 8
0
        private MassSpectrum ReadMassSpectrum(UInt64 spectrumId)
        {
            BafSqlSpectrum bafSpec = linq2BafSql.GetBafSqlSpectrum(this.linq2BafSql.Core, spectrumId);

            if (bafSpec == null)
            {
                throw new MzLiteIOException("No spectrum found for id: " + spectrumId);
            }

            MassSpectrum ms = new MassSpectrum(spectrumId.ToString());

            // determine ms level
            BafSqlAcquisitionKey aqKey   = linq2BafSql.GetBafSqlAcquisitionKey(this.linq2BafSql.Core, bafSpec.AcquisitionKey);
            Nullable <int>       msLevel = null;

            if (aqKey != null && aqKey.MsLevel.HasValue)
            {
                // bruker starts ms level by 0, must be added by 1
                msLevel = aqKey.MsLevel.Value + 1;
                ms.SetMsLevel(msLevel.Value);
            }

            // determine type of spectrum and read peak data
            // if profile data available we prefer to get profile data otherwise centroided data (line spectra)
            if (bafSpec.ProfileMzId.HasValue && bafSpec.ProfileIntensityId.HasValue)
            {
                ms.SetProfileSpectrum();
            }
            else if (bafSpec.LineMzId.HasValue && bafSpec.LineIntensityId.HasValue)
            {
                ms.SetCentroidSpectrum();
            }

            if (msLevel == 1)
            {
                ms.SetMS1Spectrum();
            }
            else if (msLevel > 1)
            {
                ms.SetMSnSpectrum();
            }

            // scan
            if (bafSpec.Rt.HasValue)
            {
                Scan scan = new Scan();
                scan.SetScanStartTime(bafSpec.Rt.Value).UO_Second();
                ms.Scans.Add(scan);
            }

            // precursor
            if (msLevel > 1)
            {
                SpectrumVariableCollection spectrumVariables = SpectrumVariableCollection.ReadSpectrumVariables(linq2BafSql, bafSpec.Id);

                Precursor precursor = new Precursor();

                decimal value;

                if (spectrumVariables.TryGetValue("Collision_Energy_Act", supportedVariables, out value))
                {
                    precursor.Activation.SetCollisionEnergy(Decimal.ToDouble(value));
                }
                if (spectrumVariables.TryGetValue("MSMS_IsolationMass_Act", supportedVariables, out value))
                {
                    precursor.IsolationWindow.SetIsolationWindowTargetMz(Decimal.ToDouble(value));
                }
                if (spectrumVariables.TryGetValue("Quadrupole_IsolationResolution_Act", supportedVariables, out value))
                {
                    double width = Decimal.ToDouble(value) * 0.5d;
                    precursor.IsolationWindow.SetIsolationWindowUpperOffset(width);
                    precursor.IsolationWindow.SetIsolationWindowLowerOffset(width);
                }

                Nullable <int> charge = null;

                if (spectrumVariables.TryGetValue("MSMS_PreCursorChargeState", supportedVariables, out value))
                {
                    charge = Decimal.ToInt32(value);
                }

                IEnumerable <BafSqlStep> ions = linq2BafSql.GetBafSqlSteps(this.linq2BafSql.Core, bafSpec.Id);

                foreach (BafSqlStep ion in ions)
                {
                    if (ion.Mass.HasValue)
                    {
                        SelectedIon selectedIon = new SelectedIon();
                        precursor.SelectedIons.Add(selectedIon);
                        selectedIon.SetSelectedIonMz(ion.Mass.Value);

                        selectedIon.SetUserParam("Number", ion.Number.Value);
                        selectedIon.SetUserParam("IsolationType", ion.IsolationType.Value);
                        selectedIon.SetUserParam("ReactionType", ion.ReactionType.Value);
                        selectedIon.SetUserParam("MsLevel", ion.MsLevel.Value);

                        if (charge.HasValue)
                        {
                            selectedIon.SetChargeState(charge.Value);
                        }
                    }
                }

                // set parent spectrum as reference
                if (bafSpec.Parent.HasValue)
                {
                    precursor.SpectrumReference = new SpectrumReference(bafSpec.Parent.ToString());
                }

                ms.Precursors.Add(precursor);
            }

            return(ms);
        }
Esempio n. 9
0
 private void WriteSelectedIon(SelectedIon ion)
 {
     writer.WriteStartElement("selectedIon");
     WriteParamGroup(ion);
     writer.WriteEndElement();
 }