public void TestProcess()
    {
      var peaks = new PeakList<Peak>();
      peaks.Add(new Peak(1.0, 1.0));
      peaks.Add(new Peak(2.0, 1.0));

      var processor = new PeakListMinIonCountProcessor<Peak>(3);
      Assert.AreSame(null, processor.Process(peaks));

      peaks.Add(new Peak(3.0, 1.0));
      Assert.AreSame(peaks, processor.Process(peaks));
    }
    public void TestProcess()
    {
      var peaks = new PeakList<Peak>();
      peaks.Add(new Peak(1.0, 10.0));
      peaks.Add(new Peak(2.0, 100.0));

      var processor1 = new PeakListMinTotalIonIntensityProcessor<Peak>(50.0);
      Assert.AreSame(peaks, processor1.Process(peaks));

      var processor2 = new PeakListMinIonIntensityProcessor<Peak>(150.0);
      Assert.AreSame(null, processor2.Process(peaks));
    }
Beispiel #3
0
        public void TestProcess()
        {
            var peaks = new PeakList <Peak>();

            peaks.Add(new Peak(1.0, 1.0));
            peaks.Add(new Peak(2.0, 1.0));

            var processor = new PeakListMinIonCountProcessor <Peak>(3);

            Assert.AreSame(null, processor.Process(peaks));

            peaks.Add(new Peak(3.0, 1.0));
            Assert.AreSame(peaks, processor.Process(peaks));
        }
Beispiel #4
0
        public void TestProcess()
        {
            var peaks = new PeakList <Peak>();

            peaks.Add(new Peak(1.0, 10.0));
            peaks.Add(new Peak(2.0, 100.0));

            var processor1 = new PeakListMinTotalIonIntensityProcessor <Peak>(50.0);

            Assert.AreSame(peaks, processor1.Process(peaks));

            var processor2 = new PeakListMinIonIntensityProcessor <Peak>(150.0);

            Assert.AreSame(null, processor2.Process(peaks));
        }
Beispiel #5
0
        public static PeakList <Peak> ReadElementPeakList(XElement ions, bool isPeakInWindow)
        {
            var result = new PeakList <Peak>();

            foreach (var p in ions.FindElements("Peak"))
            {
                var mz        = double.Parse(p.FindAttribute("mz").Value);
                var intensity = double.Parse(p.FindAttribute("intensity").Value);
                var charge    = int.Parse(p.FindAttribute("charge").Value);
                var peak      = new Peak(mz, intensity, charge);
                if (isPeakInWindow)
                {
                    peak.Tag = int.Parse(p.FindAttribute("tag").Value);
                }
                result.Add(peak);
            }

            if (isPeakInWindow)
            {
                var precursor = ions.FindElement("Precursor");
                var dic       = precursor.ToDictionary();
                result.Precursor.MasterScan       = int.Parse(dic["MasterScan"].Value);
                result.Precursor.MonoIsotopicMass = double.Parse(dic["MonoIsotopicMass"].Value);
                result.Precursor.IsolationMass    = double.Parse(dic["IsolationMass"].Value);
                result.Precursor.IsolationWidth   = double.Parse(dic["IsolationWidth"].Value);
                result.Precursor.Charge           = int.Parse(dic["Charge"].Value);
                result.Precursor.Intensity        = double.Parse(dic["Intensity"].Value);
            }

            return(result);
        }
Beispiel #6
0
        public static PeakList <Peak> ReadElementPeakList(XmlReader reader, string pklName, bool isPeakInWindow)
        {
            var result = new PeakList <Peak>();

            reader.MoveToElement(pklName);
            reader.ReadStartElement();
            while (reader.LocalName.Equals("Peak"))
            {
                Peak p = new Peak();
                p.Mz        = reader.ReadAttributeAsDouble("mz");
                p.Intensity = reader.ReadAttributeAsDouble("intensity");
                p.Charge    = reader.ReadAttributeAsInt("charge");
                if (isPeakInWindow)
                {
                    p.Tag = reader.ReadAttributeAsInt("tag");
                }
                result.Add(p);
                reader.Read();
            }

            if (isPeakInWindow)
            {
                reader.MoveToElement("Precursor");
                reader.ReadStartElement("Precursor");
                result.Precursor.MasterScan       = reader.ReadElementAsInt("MasterScan");
                result.Precursor.MonoIsotopicMass = reader.ReadElementAsDouble("MonoIsotopicMass");
                result.Precursor.IsolationMass    = reader.ReadElementAsDouble("IsolationMass");
                result.Precursor.IsolationWidth   = reader.ReadElementAsDouble("IsolationWidth");
                result.Precursor.Charge           = reader.ReadElementAsInt("Charge");
                result.Precursor.Intensity        = reader.ReadElementAsDouble("Intensity");
            }

            return(result);
        }
        public PeakList <Peak> GetMassListFromScanNum(int scan, bool centroid)
        {
            var result = new PeakList <Peak>();

            if (IsBadDataScan(scan))
            {
                return(result);
            }

            object varMassList = null;
            object varFlags    = null;
            int    curScan     = scan;
            int    arraySize   = 0;

            int centroidResult = centroid ? 1 : 0;

            if (IsCentroidScanForScanNum(scan))
            {
                centroidResult = 0;
            }
            double centroidWidth = 0.0;

            try
            {
                this.rawFile.GetMassListFromScanNum(
                    ref curScan,
                    null,
                    0,
                    0,
                    0,
                    centroidResult,
                    ref centroidWidth,
                    ref varMassList,
                    ref varFlags,
                    ref arraySize);
            }
            catch
            {
                Console.WriteLine("reading from scan {0} of {1} error, skipped.", scan, FileName);
                WriteIgnoreScan(curScan);
                return(result);
            }

            CheckErrorCode("GetMassListFromScanNum  of " + scan.ToString() + " Error");

            if (varMassList == null)
            {
                return(result);
            }

            var datas = (double[, ])varMassList;

            for (int inx = 0; inx < arraySize; inx++)
            {
                result.Add(new Peak(datas[0, inx], datas[1, inx], 0));
            }

            return(result);
        }
        public void TestFindEnvelope()
        {
            var peaks = new PeakList <Peak>();

            peaks.Add(new Peak(301, 1, 1));
            peaks.Add(new Peak(302, 2, 1));
            peaks.Add(new Peak(303, 1, 1));
            peaks.Add(new Peak(304, 2, 1));

            var env = this.FindEnvelope(peaks, peaks[0], 0.1);

            Assert.AreEqual(3, env.Count);
            Assert.IsTrue(!env.Contains(peaks[3]));

            peaks.RemoveAt(0);
            env = this.FindEnvelope(peaks, peaks[0], 0.1);
            Assert.AreEqual(2, env.Count);
            Assert.IsTrue(!env.Contains(peaks[2]));
        }
Beispiel #9
0
        private PeakList <T> ReadNextPeakList(StreamReader filein, ref string lastline)
        {
            //Find the scan/retentionTime line (start with '>')
            if (lastline == null || lastline.Length == 0 || lastline[0] != '>')
            {
                while ((lastline = filein.ReadLine()) != null)
                {
                    if (lastline.Length > 0 && lastline[0] == '>')
                    {
                        break;
                    }
                }
                if (lastline == null)
                {
                    return(null);
                }
            }

            string[] parts  = Regex.Split(lastline.Substring(1), @"\s");
            var      result = new PeakList <T>();

            result.ScanTimes.Add(new ScanTime(int.Parse(parts[0]), MyConvert.ToDouble(parts[1])));
            if (parts.Length > 2)
            {
                result.MsLevel = int.Parse(parts[2]);
            }

            while ((lastline = filein.ReadLine()) != null)
            {
                if (lastline.Length == 0)
                {
                    continue;
                }

                if (lastline[0] == '>')
                {
                    break;
                }

                string[] peakParts = Regex.Split(lastline, @"\s");
                var      peak      = new T();
                peak.Mz        = MyConvert.ToDouble(peakParts[0]);
                peak.Intensity = MyConvert.ToDouble(peakParts[1]);
                if (peakParts.Length > 2)
                {
                    peak.Charge = int.Parse(peakParts[2]);
                }

                result.Add(peak);
            }

            return(result);
        }
Beispiel #10
0
        private void ReadPeakList(XElement pkl, string elementName, PeakList <Peak> peakList)
        {
            var pklNode = pkl.Element(elementName);

            foreach (var peak in pklNode.Elements())
            {
                double mz        = MyConvert.ToDouble(peak.Attribute("Mz").Value);
                double intensity = MyConvert.ToDouble(peak.Attribute("Intensity").Value);
                var    p         = new Peak(mz, intensity);
                peakList.Add(p);
            }
        }
        private void ReadPeakList(XmlHelper xmlHelper, XmlNode pkl, string elementName, PeakList <Peak> peakList)
        {
            XmlNode pklNode = xmlHelper.GetValidChild(pkl, elementName);

            foreach (XmlNode peak in pklNode.ChildNodes)
            {
                double mz        = MyConvert.ToDouble(peak.Attributes["Mz"].InnerText);
                double intensity = MyConvert.ToDouble(peak.Attributes["Intensity"].InnerText);
                var    p         = new Peak(mz, intensity);
                peakList.Add(p);
            }
        }
        public void TestDeisotopic()
        {
            var peaks = new PeakList <Peak>();

            peaks.Add(new Peak(101.0000, 10, 1));
            peaks.Add(new Peak(101.2000, 3, 0));
            peaks.Add(new Peak(102.0034, 20, 1));
            peaks.Add(new Peak(103.0067, 10, 1));
            peaks.Add(new Peak(104.0101, 20, 1));
            peaks.Add(new Peak(105.0134, 10, 1));

            //peaks.ForEach(m => Console.WriteLine(m));

            var cur = this.Process(peaks);

            Assert.AreEqual(4, cur.Count);

            //no charge
            Assert.AreEqual(101.2000, peaks[1].Mz, 0.1);

            //first envelope
            Assert.AreEqual(101.0000, peaks[0].Mz, 0.1);
            Assert.AreEqual(102.0034, peaks[2].Mz, 0.1);

            //second envelope
            Assert.AreEqual(104.0101, peaks[3].Mz, 0.1);
        }
        public PeakList <Peak> GetLabelData(int scan, double minMz, double maxMz)
        {
            var result = new PeakList <Peak>();

            if (IsBadDataScan(scan))
            {
                return(result);
            }

            object varLabels = null;
            object varFlags  = null;

            int curScan = scan;

            try
            {
                this.rawFile.GetLabelData(ref varLabels, ref varFlags, ref curScan);
            }
            catch
            {
                Console.WriteLine("reading from scan {0} of {1} error, skipped.", scan, FileName);
                WriteIgnoreScan(curScan);
                return(result);
            }

            CheckErrorCode("GetLabelData " + scan + " Error");

            var labels = (double[, ])varLabels;

            int dim = labels.GetLength(1);

            for (int inx = 0; inx < dim; inx++)
            {
                double mz = labels[0, inx];
                if (mz < minMz)
                {
                    continue;
                }

                if (mz > maxMz)
                {
                    break;
                }

                var    charge    = (int)labels[5, inx];
                double intensity = labels[1, inx];

                result.Add(new Peak(mz, intensity, charge));
            }

            return(result);
        }
        public override PeakList <Peak> GetPeakList(int scan)
        {
            double[][] pkl = GetPeakArray(scan);

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

            for (int i = 0; i < pkl[0].Length; i++)
            {
                result.Add(new Peak(pkl[0][i], pkl[1][i]));
            }

            return(result);
        }
Beispiel #15
0
        public override PeakList <Peak> GetPeakList(int scan)
        {
            double[] mzAndIntensity = GetMzAndIntensities(scan);

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

            for (int i = 0; i < mzAndIntensity.Length; i += 2)
            {
                result.Add(new Peak(mzAndIntensity[i], mzAndIntensity[i + 1]));
            }

            return(result);
        }
Beispiel #16
0
        private static PeakList <Peak> ElementToPeakList(XElement ele, string peakname, bool isPeakInWindow)
        {
            PeakList <Peak> result = new PeakList <Peak>();

            var rawpeaks = ele.Element(peakname);

            if (rawpeaks != null)
            {
                foreach (var peakEle in rawpeaks.Elements("Peak"))
                {
                    var peak = new Peak()
                    {
                        Mz        = MyConvert.ToDouble(peakEle.Attribute("mz").Value),
                        Intensity = MyConvert.ToDouble(peakEle.Attribute("intensity").Value)
                    };

                    var chargeTag = peakEle.Attribute("charge");
                    if (chargeTag != null)
                    {
                        peak.Charge = int.Parse(chargeTag.Value);
                    }

                    if (isPeakInWindow)
                    {
                        var attrTag = peakEle.Attribute("tag");
                        if (attrTag != null)
                        {
                            peak.Tag = int.Parse(attrTag.Value);
                        }
                    }

                    result.Add(peak);
                }

                if (isPeakInWindow)
                {
                    var precursor = rawpeaks.Element("Precursor");
                    if (precursor != null)
                    {
                        result.Precursor.MasterScan       = int.Parse(precursor.Element("MasterScan").Value);
                        result.Precursor.MonoIsotopicMass = MyConvert.ToDouble(precursor.Element("MonoIsotopicMass").Value);
                        result.Precursor.IsolationMass    = MyConvert.ToDouble(precursor.Element("IsolationMass").Value);
                        result.Precursor.IsolationWidth   = MyConvert.ToDouble(precursor.Element("IsolationWidth").Value);
                        result.Precursor.Charge           = int.Parse(precursor.Element("Charge").Value);
                        result.Precursor.Intensity        = MyConvert.ToDouble(precursor.Element("Intensity").Value);
                    }
                }
            }

            return(result);
        }
        public PeakList <Peak> GetLabelData(int scan)
        {
            var result = new PeakList <Peak>();

            if (IsBadDataScan(scan))
            {
                Console.WriteLine("scan {0} of {1} is defined as ignore.", scan, FileName);
                return(result);
            }

            object varLabels = null;
            object varFlags  = null;

            int curScan = scan;

            try
            {
                this.rawFile.GetLabelData(ref varLabels, ref varFlags, ref curScan);
            }
            catch
            {
                Console.WriteLine("reading from scan {0} of {1} error, skipped.", scan, FileName);
                WriteIgnoreScan(curScan);

                this.Open(this.FileName);
                return(result);
                //throw new RawReadException(scan);
            }

            CheckErrorCode("GetLabelData " + scan + " Error");

            var labels = (double[, ])varLabels;

            int dim = labels.GetLength(1);

            for (int inx = 0; inx < dim; inx++)
            {
                double mz        = labels[0, inx];
                double intensity = labels[1, inx];
                double noise     = labels[4, inx];
                var    charge    = (int)labels[5, inx];
                result.Add(new Peak(mz, intensity, charge)
                {
                    Noise = noise
                });
            }

            return(result);
        }
Beispiel #18
0
        public PeakList <T> Process(PeakList <T> t)
        {
            List <PeakList <T> > envelopes = t.GetEnvelopes(this.ppmTolerance);

            ((List <T>)t).Clear();

            foreach (var envelope in envelopes)
            {
                t.Add(envelope[0]);
            }

            t.Sort((m1, m2) => m1.Mz.CompareTo(m2.Mz));

            return(t);
        }
Beispiel #19
0
        public static PeakList <Peak> GetPeakList(this IMsdrDataReader reader, double retentionTime, IMsdrPeakFilter peakFilter)
        {
            PeakList <Peak> result = new PeakList <Peak>();

            IBDASpecData s = reader.GetSpectrum(retentionTime, MSScanType.All, IonPolarity.Mixed, IonizationMode.Unspecified, peakFilter, true);

            for (int i = 0; i < s.XArray.Length; i++)
            {
                result.Add(new Peak(s.XArray[i], s.YArray[i]));
            }

            if (s.MZOfInterest.Length > 0)
            {
                result.PrecursorMZ = s.MZOfInterest[0].Start;
            }

            result.ScanTimes.Add(new ScanTime(0, retentionTime));

            return(result);
        }
Beispiel #20
0
        public static PeakList <Peak> Base64ToPeakList(string peaksStr)
        {
            var result = new PeakList <Peak>();

            byte[] bytes = Convert.FromBase64String(peaksStr);
            for (int index = 0; index < bytes.Length; index += 8)
            {
                var mzBytes        = new byte[4];
                var intensityBytes = new byte[4];
                for (int i = 0; i < 4; i++)
                {
                    mzBytes[i]        = bytes[index + i];
                    intensityBytes[i] = bytes[index + 4 + i];
                }

                double mz        = GetDoubleFromGetNetworkModeBytes(mzBytes);
                double intensity = GetDoubleFromGetNetworkModeBytes(intensityBytes);
                var    p         = new Peak(mz, intensity);
                result.Add(p);
            }

            return(result);
        }
Beispiel #21
0
        public override PeakList <Peak> GetPeakList(int scan, double minMz, double maxMz)
        {
            double[] mzAndIntensity = GetMzAndIntensities(scan);

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

            for (int i = 0; i < mzAndIntensity.Length; i += 2)
            {
                if (mzAndIntensity[i] < minMz)
                {
                    continue;
                }

                if (mzAndIntensity[i] > maxMz)
                {
                    break;
                }

                result.Add(new Peak(mzAndIntensity[i], mzAndIntensity[i + 1]));
            }

            return(result);
        }
Beispiel #22
0
        /// <summary>
        /// 将可用的PeakList的强度合并为一个总的PeakList,用于线性回归计算。
        /// </summary>
        /// <returns>合并后的PeakList</returns>
        private PeakList <Peak> MergeEnvelopes()
        {
            var mergedPeaks = new PeakList <Peak>();

            var initPeaks = this.ObservedEnvelopes.Where(m => m.Enabled).ToList();

            foreach (var envelope in initPeaks)
            {
                int maxCount = Math.Min(6, envelope.Count);
                for (int i = 0; i < maxCount; i++)
                {
                    if (i >= mergedPeaks.Count)
                    {
                        mergedPeaks.Add(new Peak(envelope[i].Mz, envelope[i].Intensity));
                    }
                    else
                    {
                        mergedPeaks[i].Intensity = mergedPeaks[i].Intensity + envelope[i].Intensity;
                    }
                }
            }
            return(mergedPeaks);
        }
        public override PeakList <Peak> GetPeakList(int scan, double minMz, double maxMz)
        {
            double[][] pkl = GetPeakArray(scan);

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

            for (int i = 0; i < pkl[0].Length; i++)
            {
                if (pkl[0][i] < minMz)
                {
                    continue;
                }

                if (pkl[0][i] > maxMz)
                {
                    break;
                }

                result.Add(new Peak(pkl[0][i], pkl[1][i]));
            }

            return(result);
        }
        private int FindPeak <T>(List <T> peaks, int nextPeakIndex, Pair <double, double> nextMzRange, int charge) where T : IPeak
        {
            var candidates = new PeakList <T>();

            for (int i = nextPeakIndex; i < peaks.Count; i++)
            {
                if (peaks[i].Mz > nextMzRange.Second)
                {
                    break;
                }

                if (peaks[i].Mz < nextMzRange.First)
                {
                    continue;
                }

                //if peaks[i] has been assigned charge and the charge is not
                //equals the assumed charge, just ignore it.
                if (0 != peaks[i].Charge && charge != peaks[i].Charge)
                {
                    continue;
                }

                candidates.Add(peaks[i]);
            }

            if (candidates.Count > 0)
            {
                T peak = candidates.FindMaxIntensityPeak();
                return(peaks.IndexOf(peak));
            }
            else
            {
                return(-1);
            }
        }
Beispiel #25
0
        protected override PeakList <T> DoReadNextPeakList(out bool hasNext)
        {
            var result = new PeakList <T>();

            string        lastLine = "";
            List <string> headers  = ReadHeader(ref lastLine);
            Dictionary <string, string> annotations = ReadAnnotation(ref lastLine);
            List <string> peaks = ReadPeak(ref lastLine);

            hasNext = CheckHasNext(this.reader);

            foreach (string line in headers)
            {
                if (line.StartsWith(MascotGenericFormatConstants.MS_SCAN_TAG))
                {
                    ParseScan(result, line);
                }
                else if (line.StartsWith(MascotGenericFormatConstants.MSMS_SCAN_TAG))
                {
                    ParseScan(result, line);
                }
                else if (line.StartsWith(MascotGenericFormatConstants.FILENAME_TAG))
                {
                    result.Experimental = line.Substring(line.IndexOf(':') + 1).Trim();
                }
            }

            foreach (var de in annotations)
            {
                if (de.Key.Equals(MascotGenericFormatConstants.PEPMASS_TAG))
                {
                    string[] values = Regex.Split(de.Value, "\\s");
                    result.PrecursorMZ = MyConvert.ToDouble(values[0]);
                    if (values.Length > 1)
                    {
                        result.PrecursorIntensity = MyConvert.ToDouble(values[1]);
                    }
                }
                else if (de.Key.Equals(MascotGenericFormatConstants.CHARGE_TAG))
                {
                    string charge = de.Value;
                    if (charge.Contains("and"))
                    {
                        result.Annotations.Add(de.Key, de.Value);
                        continue;
                    }

                    if (charge.EndsWith("+"))
                    {
                        charge = charge.Substring(0, charge.Length - 1);
                    }
                    result.PrecursorCharge = int.Parse(charge);
                }
                else if (de.Key.Equals(MascotGenericFormatConstants.SCAN_TAG))
                {
                    ParseScan(result, de.Value);
                }
                else if (de.Key.Equals(MascotGenericFormatConstants.RETENTION_TIME_TAG))
                {
                    double rtInSecond = 0.0;
                    if (MyConvert.TryParse(de.Value, out rtInSecond))
                    {
                        var rtInMin = rtInSecond / 60.0;
                        if (result.ScanTimes.Count == 0)
                        {
                            result.ScanTimes.Add(new ScanTime(0, rtInMin));
                        }
                        else
                        {
                            result.ScanTimes[0].RetentionTime = rtInMin;
                        }
                    }
                }
                else
                {
                    result.Annotations.Add(de.Key, de.Value);
                }
            }

            foreach (string line in peaks)
            {
                string[] values = Regex.Split(line, "\\s");
                try
                {
                    var peak = new T()
                    {
                        Mz        = MyConvert.ToDouble(values[0]),
                        Intensity = MyConvert.ToDouble(values[1])
                    };
                    result.Add(peak);
                }
                catch (Exception)
                {
                    throw new Exception(string.Format("Format error, cannot read peak info from {0}", line));
                }
            }

            return(result);
        }
Beispiel #26
0
        public override IEnumerable <string> Process(string fileName)
        {
            var result = new FileInfo(targetDir + "\\" + Path.ChangeExtension(new FileInfo(fileName).Name, ".mgf")).FullName;

            int             charge = 1;
            PeakList <Peak> ms1    = new PeakList <Peak>();

            //读取ms1
            List <string> comments = new List <string>();

            comments.Add("COM=" + software);
            using (StreamReader sr = new StreamReader(fileName))
            {
                string line;
                while ((line = sr.ReadLine()) != null)
                {
                    if (line.StartsWith("COM="))
                    {
                        comments.Add(line);
                        continue;
                    }

                    if (line.StartsWith("BEGIN IONS"))
                    {
                        break;
                    }

                    comments.Add(line);
                    var parts = line.Split('\t');
                    if (parts.Length == 2)
                    {
                        try
                        {
                            ms1.Add(new Peak(double.Parse(parts[0]), double.Parse(parts[1])));
                        }
                        catch (Exception) { }
                    }
                }
            }

            MascotGenericFormatReader <Peak> reader = new MascotGenericFormatReader <Peak>();
            var pkls = reader.ReadFromFile(fileName);

            foreach (var pkl in pkls)
            {
                var mzTolerance = PrecursorUtils.ppm2mz(pkl.PrecursorMZ, precursorTolerance);
                var peak        = ms1.FindPeak(pkl.PrecursorMZ, mzTolerance).FindMaxIntensityPeak();

                if (peak == null)
                {
                    pkl.PrecursorIntensity = ms1.Min(m => m.Intensity);
                }
                else
                {
                    pkl.PrecursorIntensity = peak.Intensity;
                }
            }

            pkls.Sort((m1, m2) => m2.PrecursorIntensity.CompareTo(m1.PrecursorIntensity));

            for (int i = 0; i < pkls.Count; i++)
            {
                var    line = pkls[i].Annotations[MascotGenericFormatConstants.TITLE_TAG] as string;
                var    m    = reg.Match(line);
                string experimental;
                if (m.Success)
                {
                    experimental = m.Groups[1].Value;
                    var m2 = nameReg.Match(experimental);
                    if (m2.Success)
                    {
                        if (m2.Groups[2].Value.Length == 1)
                        {
                            experimental = m2.Groups[1].Value + "0" + m2.Groups[2].Value;
                        }
                    }
                }
                else
                {
                    experimental = pkls[i].Experimental;
                }
                pkls[i].Annotations[MascotGenericFormatConstants.TITLE_TAG] = string.Format("{0}.{1}.{2:0}.{3}.dta", experimental, i + 1, pkls[i].PrecursorIntensity, charge);
            }

            var writer = new MascotGenericFormatWriter <Peak>();

            writer.Comments.AddRange(comments);
            writer.WriteToFile(result, pkls);

            return(new string[] { result });
        }
Beispiel #27
0
    public void SetUp()
    {
      pl = new PeakList<Peak>();
      pl.Add(new Peak(430.0884, 43875.5, 1));

      pl.Add(new Peak(445.1205, 335180.5, 1));
      pl.Add(new Peak(446.1185, 51638.0, 1));
      pl.Add(new Peak(446.1255, 129918.6, 1));
      pl.Add(new Peak(447.1170, 30164.7, 1));

      pl.Add(new Peak(491.9578, 442529.3, 3));
      pl.Add(new Peak(492.2919, 206717.3, 3));
      pl.Add(new Peak(492.6270, 137434.5, 3));
      pl.Add(new Peak(492.9613, 26216.6, 3));

      pl.Add(new Peak(531.2642, 129351.8, 4));

      pl.Add(new Peak(631.2642, 129351.8, 0));
    }
Beispiel #28
0
    public void TestMergeWith()
    {
      PeakList<Peak> pl1 = new PeakList<Peak>();
      pl1.Add(new Peak(420, 10, 1));
      pl1.Add(new Peak(445, 10, 1));

      PeakList<Peak> pl2 = new PeakList<Peak>();
      pl2.Add(new Peak(420.004, 30, 2));
      pl2.Add(new Peak(445.004, 30, 1));

      pl1.MergeByMZFirst(pl2, 50);

      Assert.AreEqual(3, pl1.Count);
      Assert.AreEqual(420, pl1[0].Mz, 0.001);
      Assert.AreEqual(1, pl1[0].Charge);
      Assert.AreEqual(420.004, pl1[1].Mz, 0.001);
      Assert.AreEqual(2, pl1[1].Charge);
      Assert.AreEqual(445.003, pl1[2].Mz, 0.001);
      Assert.AreEqual(1, pl1[2].Charge);

      Assert.AreEqual(10, pl1[0].Intensity);
      Assert.AreEqual(30, pl1[1].Intensity);
      Assert.AreEqual(40, pl1[2].Intensity);
    }
        public PeakList <T> ReadFromFile(string dtaFilename)
        {
            var fi = new FileInfo(dtaFilename);

            if (!fi.Exists)
            {
                throw new FileNotFoundException("Cannot find the file " + dtaFilename);
            }

            var             result = new PeakList <T>();
            SequestFilename sf     = SequestFilename.Parse(fi.Name);

            result.Experimental = sf.Experimental;
            result.ScanTimes.Add(new ScanTime(sf.FirstScan, 0.0));
            if (sf.FirstScan != sf.LastScan)
            {
                result.ScanTimes.Add(new ScanTime(sf.LastScan, 0.0));
            }
            result.PrecursorCharge = sf.Charge;

            using (var filein = new StreamReader(new FileStream(dtaFilename, FileMode.Open, FileAccess.Read)))
            {
                string lastline;
                while ((lastline = filein.ReadLine()) != null)
                {
                    if (lastline.Trim().Length > 0)
                    {
                        break;
                    }
                }
                if (lastline == null)
                {
                    return(null);
                }

                string[] parts         = Regex.Split(lastline, @"\s+");
                double   precursorMass = MyConvert.ToDouble(parts[0]);
                result.PrecursorCharge = int.Parse(parts[1]);
                result.PrecursorMZ     = PrecursorUtils.MHToMz(precursorMass, result.PrecursorCharge, true);

                while ((lastline = filein.ReadLine()) != null)
                {
                    if (lastline.Length == 0)
                    {
                        continue;
                    }

                    if (lastline[0] == '>')
                    {
                        break;
                    }

                    string[] peakParts = Regex.Split(lastline, @"\s+");
                    var      peak      = new T();
                    peak.Mz        = MyConvert.ToDouble(peakParts[0]);
                    peak.Intensity = MyConvert.ToDouble(peakParts[1]);
                    if (peakParts.Length > 2)
                    {
                        peak.Charge = int.Parse(peakParts[2]);
                    }

                    result.Add(peak);
                }

                return(result);
            }
        }
        public void Deconvolute <T>(List <T> peaks) where T : IPeak
        {
            peaks.Sort((m1, m2) => m1.Mz.CompareTo(m2.Mz));

            var peakEnvelopeMap = new Dictionary <T, List <PeakList <T> > >();
            var envelopes       = new List <PeakList <T> >();

            for (int i = 0; i < peaks.Count - 1; i++)
            {
                double mzTolerance = PrecursorUtils.ppm2mz(peaks[i].Mz, this.ppmTolerance);

                if (peaks[i + 1].Mz - peaks[i].Mz > C_GAP + mzTolerance)
                {
                    continue;
                }

                var envelopeList = new List <PeakList <T> >();
                for (int charge = 1; charge <= this.maxCharge; charge++)
                {
                    //if peaks[i] has already been assigned a charge,
                    //only that charge will be considered
                    if (0 != peaks[i].Charge && charge != peaks[i].Charge)
                    {
                        continue;
                    }

                    double gap = this.mzGaps[charge - 1];

                    var curPeaks = new PeakList <T>();
                    curPeaks.Add(peaks[i]);

                    //just use precursor charge to record the assumed charge
                    curPeaks.PrecursorCharge = charge;

                    int nextPeakIndex = i + 1;
                    while (true)
                    {
                        double currMz        = curPeaks[curPeaks.Count - 1].Mz;
                        var    nextMzRange   = new Pair <double, double>(currMz + gap - mzTolerance, currMz + gap + mzTolerance);
                        int    findPeakIndex = FindPeak(peaks, nextPeakIndex, nextMzRange, charge);
                        if (-1 == findPeakIndex)
                        {
                            break;
                        }
                        curPeaks.Add(peaks[findPeakIndex]);
                        nextPeakIndex = findPeakIndex + 1;
                    }

                    if (charge > MIN_THREE_PEAKS_NEEDED_FOR_CHARGE_LARGER_THAN)
                    {
                        if (curPeaks.Count > 2)
                        {
                            envelopeList.Add(curPeaks);
                        }
                    }
                    else if (curPeaks.Count > 1)
                    {
                        envelopeList.Add(curPeaks);
                    }
                }

                if (envelopeList.Count > 0)
                {
                    peakEnvelopeMap.Add(peaks[i], envelopeList);
                    envelopes.AddRange(envelopeList);
                }
            }

            SortEnvelopesByCountChargeMz(envelopes);
            RemoveAllContainedEnvelopes(envelopes);

            while (envelopes.Count > 0)
            {
                SortEnvelopesByCountChargeMz(envelopes);

                if (1 >= envelopes[0].Count)
                {
                    break;
                }

                envelopes[0].SetFragmentIonCharge(envelopes[0].PrecursorCharge);

                foreach (T p in envelopes[0])
                {
                    for (int j = 1; j < envelopes.Count; j++)
                    {
                        if (envelopes[j].Contains(p))
                        {
                            if (envelopes[j].PrecursorCharge != envelopes[0].PrecursorCharge)
                            {
                                envelopes[j].Remove(p);
                            }
                        }
                    }
                }
                envelopes.RemoveAt(0);

                for (int iEnv = envelopes.Count - 1; iEnv >= 0; iEnv--)
                {
                    if (1 >= envelopes[iEnv].Count)
                    {
                        envelopes.RemoveAt(iEnv);
                    }
                }
            }
        }
        public List <PeakList <Peak> > ReadFromFile(string fileName)
        {
            List <PeakList <Peak> > result = new List <PeakList <Peak> >();

            using (StreamReader sr = new StreamReader(fileName))
            {
                Progress.SetRange(0, sr.BaseStream.Length);
                string line;
                Dictionary <string, string> headers = new Dictionary <string, string>();
                List <string> peaks = new List <string>();
                while ((line = sr.ReadLine()) != null)
                {
                    if (line.Trim().Equals("peaklist start"))
                    {
                        Progress.SetPosition(StreamUtils.GetCharpos(sr));

                        headers.Clear();
                        peaks.Clear();

                        bool inHeader = true;
                        while ((line = sr.ReadLine()) != null)
                        {
                            var tline = line.Trim();
                            if (tline.Equals("peaklist end"))
                            {
                                break;
                            }

                            if (tline.Length == 0)
                            {
                                continue;
                            }

                            if (!inHeader)
                            {
                                peaks.Add(tline);
                            }
                            else if (Char.IsLetter(tline[0]))
                            {
                                var pos   = tline.IndexOf('=');
                                var key   = tline.Substring(0, pos);
                                var value = tline.Substring(pos + 1);
                                headers[key] = value;
                            }
                            else
                            {
                                inHeader = false;
                                peaks.Add(tline);
                            }
                        }

                        if (headers.Count > 0 && peaks.Count > 0)
                        {
                            PeakList <Peak> pkl = new PeakList <Peak>();
                            pkl.PrecursorMZ     = MyConvert.ToDouble(headers["mz"]);
                            pkl.PrecursorCharge = Convert.ToInt32(headers["charge"]);
                            pkl.MsLevel         = 2;
                            pkl.ScanMode        = headers["fragmentation"];
                            SequestFilename sf = parser.GetValue(headers["header"]);
                            pkl.ScanTimes.Add(new ScanTime(sf.FirstScan, 0.0));
                            pkl.Experimental = sf.Experimental;

                            result.Add(pkl);

                            foreach (var l in peaks)
                            {
                                var p = l.Split('\t');
                                if (p.Length > 1)
                                {
                                    pkl.Add(new Peak(MyConvert.ToDouble(p[0]), MyConvert.ToDouble(p[1])));
                                }
                            }
                        }
                    }
                }
            }

            return(result);
        }