/// <summary>
 /// Gets a value indicating whether the specified point belongs to the collection.
 /// </summary>
 /// <param name="pts">The instance to which the method applies.</param>
 /// <param name="pt">The point to search.</param>
 /// <param name="tol">The tolerance to use in comparisons.</param>
 /// <returns>true if the point is found; otherwise, false.</returns>
 public static bool Contains(this Point3dCollection pts, Point3d pt, Tolerance tol)
 {
     for (int i = 0; i < pts.Count; i++)
     {
         if (pt.IsEqualTo(pts[i], tol))
             return true;
     }
     return false;
 }
Пример #2
0
        public override PeptideSpectralMatch Search(IMassSpectrum massSpectrum, Peptide peptide, FragmentTypes fragmentTypes, Tolerance productMassTolerance)
        {
            double[] eMasses = massSpectrum.MassSpectrum.GetMasses();
            double[] eIntenisties = massSpectrum.MassSpectrum.GetIntensities();
            double tic = massSpectrum.MassSpectrum.GetTotalIonCurrent();

            PeptideSpectralMatch psm = new PeptideSpectralMatch(DefaultPsmScoreType) {Peptide = peptide};
            double[] tMasses = peptide.Fragment(fragmentTypes).Select(frag => Mass.MzFromMass(frag.MonoisotopicMass, 1)).OrderBy(val => val).ToArray();
            double score = Search(eMasses, eIntenisties, tMasses, productMassTolerance, tic);
            psm.Score = score;

            return psm;
        }
Пример #3
0
 public IEnumerable<Fragment> MatchFragments(IEnumerable<Fragment> fragments, Tolerance tolerance, double percentCutoff, params int[] chargeStates)
 {
     double basePeakInt = Spectrum.GetBasePeakIntensity();
     double lowThreshold = basePeakInt*percentCutoff;
     double summedIntensity = 0;
     double totalIntensity = Spectrum.GetTotalIonCurrent();
     foreach (Fragment fragment in fragments)
     {
         foreach (int chargeState in chargeStates)
         {
             double mz = fragment.ToMz(chargeState);
             var peak = Spectrum.GetClosestPeak(tolerance.GetRange(mz));
             if (peak != null && peak.Intensity >= lowThreshold)
             {
                 Add(new FragmentSpectralMatch(Spectrum, fragment, tolerance, chargeState));
                 yield return fragment;
                 summedIntensity += peak.Intensity;
             }
         }
     }
     PercentTIC = 100.0*summedIntensity/totalIntensity;
 }
Пример #4
0
        public override SortedMaxSizedContainer<PeptideSpectralMatch> Search(IMassSpectrum spectrum, IEnumerable<Peptide> peptides, FragmentTypes fragmentTypes, Tolerance productMassTolerance)
        {
            SortedMaxSizedContainer<PeptideSpectralMatch> results = new SortedMaxSizedContainer<PeptideSpectralMatch>(MaxMatchesPerSpectrum);

            double[] eMasses = spectrum.MassSpectrum.GetMasses();
            double[] eIntenisties = spectrum.MassSpectrum.GetIntensities();
            double tic = spectrum.MassSpectrum.TotalIonCurrent;

            foreach (var peptide in peptides)
            {
                PeptideSpectralMatch psm = new PeptideSpectralMatch(DefaultPsmScoreType) {Peptide = peptide};
                double[] tMasses =
                    peptide.Fragment(fragmentTypes)
                            .Select(frag => Mass.MzFromMass(frag.MonoisotopicMass, 1))
                            .OrderBy(val => val)
                            .ToArray();
                double score = Search(eMasses, eIntenisties, tMasses, productMassTolerance, tic);
                psm.Score = score;
                results.Add(psm);
            }

            return results;
        }
Пример #5
0
 public bool initXtalTolerance(string configurationField)
 {
     try
     {
         if (configurationField == "loPPM")
         {
             this.XtalTol = Tolerance.loPPM;
         }
         else if (configurationField == "hiPPM")
         {
             this.XtalTol = Tolerance.hiPPM;
         }
         else
         {
             return false;
         }
         return true;
     }
     catch
     {
         return false;
     }
 }
 /// <summary>
 /// Removes duplicated points in the collection.
 /// </summary>
 /// <param name="pts">The instance to which the method applies.</param>
 /// <param name="tol">The tolerance to use in comparisons.</param>
 public static void RemoveDuplicate(this Point3dCollection pts, Tolerance tol)
 {
     List<Point3d> ptlst = new List<Point3d>();
     for (int i = 0; i < pts.Count; i++)
     {
         ptlst.Add(pts[i]);
     }
     ptlst.Sort((p1, p2) => p1.X.CompareTo(p2.X));
     for (int i = 0; i < ptlst.Count - 1; i++)
     {
         for (int j = i + 1; j < ptlst.Count; )
         {
             if ((ptlst[j].X - ptlst[i].X) > tol.EqualPoint)
                 break;
             if (ptlst[i].IsEqualTo(ptlst[j], tol))
             {
                 pts.Remove(ptlst[j]);
                 ptlst.RemoveAt(j);
             }
             else
                 j++;
         }
     }
 }
Пример #7
0
        public void GeneratePrmInfo(string resultFilePath, string outputFilePath)
        {
            Console.Write("Processing {0}", Path.GetFileName(resultFilePath));
            Console.Out.Flush();

            var rawFilePath =
                @"D:\Research\Data\EDRN\DDA\raw\" + Path.GetFileNameWithoutExtension(resultFilePath) + ".raw";
            var reader = new XCaliburReader(rawFilePath);
            var run    = InMemoryLcMsRun.GetLcMsRun(rawFilePath);

            var tolerance = new Tolerance(10, ToleranceUnit.Ppm);

            const string spikedInPeptideFile = @"D:\Research\Data\EDRN\SpikedPeptides.txt";
            var          spikedInPeptides    = File.ReadAllLines(spikedInPeptideFile);
            var          spikedInPepSet      = new HashSet <string>();

            foreach (var p in spikedInPeptides)
            {
                spikedInPepSet.Add(p);
            }
//            const string resultFilePath = @"D:\Research\Data\EDRN\DDA\Frac7_NTT2.tsv";
            //const string resultFilePath = @"D:\Research\Data\EDRN\DDA\Heavy\342865_EDRN_Serum_07_DDA_1_12Nov13_Samwise_13-07-28.tsv";
//            const string resultFilePath = @"D:\Research\Data\EDRN\DDA\NTT1_NoMod\342865_EDRN_Serum_07_DDA_1_12Nov13_Samwise_13-07-28.tsv";
            const double qValueThreshold = 0.01;

            var pepSet = new HashSet <string>();
            MsGfPlusHeaderInformation headerInfo = null;

            //var prefix = new HashSet<string>();
            //var suffix = new HashSet<string>();
            var numPeptides = 0;

            var prevScanNum = -1;

            using (var writer = new StreamWriter(outputFilePath))
            {
                writer.WriteLine("Peptide\tCharge\tMonoMz\tMostAbundantMz\tMs2ScanNum\tRtMs2\tRtApex\tRtStart\tRtEnd\tSpecEValue\tPepQValue");
                foreach (var line in File.ReadLines(resultFilePath))
                {
                    if (line.StartsWith("#"))
                    {
                        headerInfo = new MsGfPlusHeaderInformation(line);
                        continue;
                    }

                    var match = new MsGfMatch(line, headerInfo);

                    if (match.ScanNum == prevScanNum)
                    {
                        continue;
                    }
                    prevScanNum = match.ScanNum;

                    if (!match.IsValid || match.Protein.StartsWith(FastaDatabase.DecoyProteinPrefix))
                    {
                        continue;
                    }
                    if (match.PepQValue > qValueThreshold)
                    {
                        continue;
                    }
                    var peptide = match.Peptide.Replace("C+57.021", "C").Replace("K+8.014", "K").Replace("R+10.008", "R");

                    if (pepSet.Contains(peptide))
                    {
                        continue;
                    }
                    pepSet.Add(peptide);

                    if (spikedInPepSet.Contains(peptide))
                    {
                        var ion = new Ion(match.Formula, match.Charge);
                        var mostAbundantIonMz = ion.GetMostAbundantIsotopeMz();
                        var xic = run.GetPrecursorExtractedIonChromatogram(mostAbundantIonMz, tolerance, match.ScanNum);
                        if (xic.Count == 0)
                        {
                            continue;
                        }
                        var minScan = xic.Min().ScanNum;
                        var maxScan = xic.Max().ScanNum;
                        writer.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}\t{8}\t{9}\t{10}",
                                         peptide,
                                         match.Charge,
                                         ion.GetMonoIsotopicMz(),
                                         mostAbundantIonMz,
                                         match.ScanNum,
                                         reader.RtFromScanNum(match.ScanNum),
                                         reader.RtFromScanNum(xic.GetApexScanNum()), // Rt apex
                                         reader.RtFromScanNum(minScan),              // Rt start
                                         reader.RtFromScanNum(maxScan),              // Rt end
                                         match.SpecEValue,
                                         match.PepQValue);
                        ++numPeptides;
                    }
                    //else
                    //{
                    //    foreach (var spikedInPeptide in spikedInPeptides)
                    //    {
                    //        if (spikedInPeptide.StartsWith(peptide)) prefix.Add(spikedInPeptide + "\t" + peptide + "\t" + match.ScanNum);
                    //        else if (spikedInPeptide.EndsWith(peptide)) suffix.Add(spikedInPeptide + "\t" + peptide + "\t" + match.ScanNum);
                    //    }
                    //}
                }
            }

            //Console.WriteLine("*********Prefix");
            //foreach(var p in prefix) Console.WriteLine(p);

            //Console.WriteLine("*********Suffix");
            //foreach (var p in suffix) Console.WriteLine(p);

            Console.WriteLine("\t{0}", numPeptides);
        }
Пример #8
0
 public LeaderSkillStatus(string leaderSkillId, string name, string description, LeaderSkillType type, float hpFollowingPercent, float upPercent, Tolerance tolerance)
 {
     float[] array = new float[2];
     array[0]         = 1f;
     this._floatValue = array;
     this._isHaving   = true;
     base..ctor();
     this._leaderSkillId = leaderSkillId;
     this._name          = name;
     this._description   = description;
     this._type          = type;
     this._floatValue[0] = Mathf.Clamp01(hpFollowingPercent);
     this._floatValue[1] = Mathf.Clamp01(upPercent);
     this._addTolerances = this.addTolerances;
     this._isHaving      = (name != null);
 }
Пример #9
0
        public void ExtractLcMsFeaturesForTrainingSet()
        {
            var methodName = MethodBase.GetCurrentMethod().Name;

            TestUtils.ShowStarting(methodName);

            const string idFileFolder = @"D:\MassSpecFiles\training\FilteredIdResult";

            if (!Directory.Exists(idFileFolder))
            {
                Assert.Ignore(@"Skipping test {0} since folder not found: {1}", methodName, idFileFolder);
            }

            var tolerance  = new Tolerance(10);
            var tolerance2 = new Tolerance(20);
            var id         = 1;


            for (var d = 0; d < TrainSetFileLists.Length; d++)
            {
                var dataset            = TrainSetFileLists[d];
                var dataname           = Path.GetFileNameWithoutExtension(dataset);
                var filtedIdResultFile = string.Format(@"{0}\{1}.trainset.tsv", idFileFolder, Path.GetFileNameWithoutExtension(dataset));
                var featureResult      = string.Format(@"{0}\{1}.ms1ft", idFileFolder, Path.GetFileNameWithoutExtension(dataset));

                if (!File.Exists(dataset))
                {
                    Console.WriteLine(@"Warning: Skipping since file not found: {0}", dataset);
                    continue;
                }
                if (!File.Exists(filtedIdResultFile))
                {
                    Console.WriteLine(@"Warning: Skipping since file not found: {0}", filtedIdResultFile);
                    continue;
                }


                var run = PbfLcMsRun.GetLcMsRun(dataset);


                var targetStatWriter = new StreamWriter(string.Format(@"D:\MassSpecFiles\training\statistics\{0}.tsv", Path.GetFileNameWithoutExtension(dataset)));
                var decoyStatWriter  = new StreamWriter(string.Format(@"D:\MassSpecFiles\training\statistics\{0}_decoy.tsv", Path.GetFileNameWithoutExtension(dataset)));
                var writer           = new StreamWriter(featureResult);

                writer.Write("Ms2MinScan\tMs2MaxScan\tMs2MinCharge\tMs2MaxCharge\tMs2Mass\t");
                writer.Write("Mass\tMinScan\tMaxScan\tMinCharge\tMaxCharge\tMinTime\tMaxTime\tElution\tGood\n");
                var tsvParser = new TsvFileParser(filtedIdResultFile);

                var featureFinder = new LcMsPeakMatrix(run);


                for (var i = 0; i < tsvParser.NumData; i++)
                {
                    var minScan   = int.Parse(tsvParser.GetData("MinScan")[i]);
                    var maxScan   = int.Parse(tsvParser.GetData("MaxScan")[i]);
                    var minCharge = int.Parse(tsvParser.GetData("MinCharge")[i]);
                    var maxCharge = int.Parse(tsvParser.GetData("MaxCharge")[i]);
                    var mass      = double.Parse(tsvParser.GetData("Mass")[i]);

                    writer.Write(minScan);
                    writer.Write("\t");
                    writer.Write(maxScan);
                    writer.Write("\t");
                    writer.Write(minCharge);
                    writer.Write("\t");
                    writer.Write(maxCharge);
                    writer.Write("\t");
                    writer.Write(mass);
                    writer.Write("\t");

                    var binNum = featureFinder.Comparer.GetBinNumber(mass);

                    var binMass = featureFinder.Comparer.GetMzAverage(binNum);

                    var             binNumList     = (mass < binMass) ? new int[] { binNum, binNum - 1, binNum + 1 } : new int[] { binNum, binNum + 1, binNum - 1 };
                    LcMsPeakCluster refinedFeature = null;

                    foreach (var bi in binNumList)
                    {
                        var tempList = new List <LcMsPeakCluster>();
                        var features = featureFinder.FindFeatures(bi);
                        var massTh   = (mass < 2000) ? tolerance2.GetToleranceAsTh(mass) : tolerance.GetToleranceAsTh(mass);
                        foreach (var feature in features)
                        {
                            if (Math.Abs(mass - feature.Mass) < massTh)
                            {
                                tempList.Add(feature);
                            }
                        }

                        //var nHits = 0;
                        var highestAbu = 0d;
                        //var scans = Enumerable.Range(minScan, maxScan - minScan + 1);
                        foreach (var feature in tempList)
                        {
                            //var scans2 = Enumerable.Range(feature.MinScanNum, feature.MaxScanNum - feature.MinScanNum + 1);
                            //var hitScans = scans.Intersect(scans2).Count();
                            if (feature.MinScanNum < 0.5 * (minScan + maxScan) &&
                                0.5 * (minScan + maxScan) < feature.MaxScanNum)
                            {
                                if (feature.Abundance > highestAbu)
                                {
                                    refinedFeature = feature;
                                    highestAbu     = feature.Abundance;
                                }
                            }

                            /*if (hitScans > 0)
                             * {
                             *  refinedFeature = feature;
                             *  nHits = hitScans;
                             * }*/
                        }

                        if (refinedFeature != null)
                        {
                            break;
                        }
                    }

                    if (refinedFeature != null)
                    {
                        writer.Write(refinedFeature.Mass);
                        writer.Write("\t");
                        writer.Write(refinedFeature.MinScanNum);
                        writer.Write("\t");
                        writer.Write(refinedFeature.MaxScanNum);
                        writer.Write("\t");
                        writer.Write(refinedFeature.MinCharge);
                        writer.Write("\t");
                        writer.Write(refinedFeature.MaxCharge);
                        writer.Write("\t");
                        writer.Write(refinedFeature.MinElutionTime);
                        writer.Write("\t");
                        writer.Write(refinedFeature.MaxElutionTime);
                        writer.Write("\t");
                        writer.Write(refinedFeature.MaxElutionTime - refinedFeature.MinElutionTime);
                        writer.Write("\t");

                        var good = (refinedFeature.MinScanNum <= minScan && refinedFeature.MaxScanNum >= maxScan);
                        writer.Write(good ? 1 : 0);
                        writer.Write("\n");
                        //writer.Write(0); writer.Write("\t");
                        //writer.Write(0); writer.Write("\n");

                        OutputEnvelopPeakStat(id, refinedFeature, targetStatWriter);

                        var chargeRange = featureFinder.GetDetectableMinMaxCharge(refinedFeature.RepresentativeMass, run.MinMs1Mz, run.MaxMs1Mz);
                        refinedFeature.UpdateWithDecoyScore(featureFinder.Ms1Spectra, chargeRange.Item1, chargeRange.Item2);
                        OutputEnvelopPeakStat(id, refinedFeature, decoyStatWriter);
                        id++;
                    }
                    else
                    {
                        writer.Write(0);
                        writer.Write("\t");
                        writer.Write(0);
                        writer.Write("\t");
                        writer.Write(0);
                        writer.Write("\t");
                        writer.Write(0);
                        writer.Write("\t");
                        writer.Write(0);
                        writer.Write("\t");
                        writer.Write(0);
                        writer.Write("\t");
                        writer.Write(0);
                        writer.Write("\t");
                        writer.Write(0);
                        writer.Write("\t");
                        writer.Write(0);
                        writer.Write("\n");
                    }
                    //var feature = featureFinder.FindLcMsPeakCluster(mass, (int) scan, (int) charge);
                }
                writer.Close();
                targetStatWriter.Close();
                decoyStatWriter.Close();
                Console.WriteLine(dataname);
            }
        }
Пример #10
0
        /// <summary>
        /// Creates a new Polyline that is the result of projecting the Polyline parallel to 'direction' onto 'plane' and returns it.
        /// </summary>
        /// <param name="pline">The polyline to project.</param>
        /// <param name="plane">The plane onto which the curve is to be projected.</param>
        /// <param name="direction">Direction (in WCS coordinates) of the projection.</param>
        /// <returns>The projected Polyline.</returns>
        public static Polyline GetProjectedPolyline(this Polyline pline, Plane plane, Vector3d direction)
        {
            Tolerance tol = new Tolerance(1e-9, 1e-9);
            if (plane.Normal.IsPerpendicularTo(direction, tol))
                return null;

            if (pline.Normal.IsPerpendicularTo(direction, tol))
            {
                Plane dirPlane = new Plane(Point3d.Origin, direction);
                if (!pline.IsWriteEnabled) pline.UpgradeOpen();
                pline.TransformBy(Matrix3d.WorldToPlane(dirPlane));
                Extents3d extents = pline.GeometricExtents;
                pline.TransformBy(Matrix3d.PlaneToWorld(dirPlane));
                return GeomExt.ProjectExtents(extents, plane, direction, dirPlane);
            }

            return GeomExt.ProjectPolyline(pline, plane, direction);
        }
Пример #11
0
 static PointSampler()
 {
     Tolerance = new Tolerance(1E-6, 1E-6);
 }
Пример #12
0
 public IcBottomUpRescorer(string specFilePath, string icResultFilePath, string outputFilePath, AminoAcidSet aaSet, Tolerance tolerance)
 {
     _run = InMemoryLcMsRun.GetLcMsRun(specFilePath, 1.4826, 0.0);
     Rescore(icResultFilePath, outputFilePath);
 }
Пример #13
0
 public Curve3d GetGeCurve(Tolerance tolerance)
 {
     createInstance();
     Curve3d GetGeCur = BaseCurve.GetGeCurve(tolerance);
     tr.Dispose();
     return GetGeCur;
 }
Пример #14
0
 public MassRange(double meanMass, Tolerance toleranceWidth)
     : base(meanMass, toleranceWidth)
 {
 }
Пример #15
0
 public MzRange(double meanMZ, Tolerance tolerance)
     : base(meanMZ, tolerance)
 {
 }
        protected bool CompareColor(ScreenshotColor refColor, ScreenshotColor actualColor,
            Tolerance tolerance)
        {
            // Simply compare the discrepancy of the R, G and B values
            // of each color.
            for (int i = 0; i < 3; i++)
            {
                byte bRef = refColor.GetValueFromIndex(i);
                byte bAct = actualColor.GetValueFromIndex(i);

                if (!(Math.Abs(bRef - bAct) <= tolerance.GetValueFromIndex(i)))
                    return false;
            }

            return true;
        }
Пример #17
0
 /// <summary>
 /// Gets a value indicating whether the specified point is on the segment defined by two points.
 /// </summary>
 /// <param name="pt">The instance to which the method applies.</param>
 /// <param name="p1">The segment start point.</param>
 /// <param name="p2">The segment end point.</param>
 /// <param name="tol">The tolerance used in comparisons.</param>
 /// <returns>true if the point is on the segment; otherwise, false.</returns>
 public static bool IsBetween(this Point3d pt, Point3d p1, Point3d p2, Tolerance tol)
 {
     return p1.GetVectorTo(pt).GetNormal(tol).Equals(pt.GetVectorTo(p2).GetNormal(tol));
 }
Пример #18
0
 /// <summary>
 /// Проверка дублирования вхождений блоков
 /// </summary>
 /// <param name="blk1"></param>
 /// <param name="blk2"></param>
 /// <returns></returns>
 public static bool IsDuplicate(this BlockReference blk1, BlockReference blk2)
 {
     Tolerance tol = new Tolerance(1, 1);
     return
         blk1.OwnerId == blk2.OwnerId &&
         blk1.Name == blk2.Name &&
         Math.Round(blk1.Rotation, 1) == Math.Round(blk2.Rotation, 1) &&
         blk1.Position.IsEqualTo(blk2.Position, tol) &&
         blk1.ScaleFactors.IsEqualTo(blk2.ScaleFactors, tol);
 }
Пример #19
0
 public void SetFromGeCurve(Curve3d geCurve, Vector3d __unnamed001, Tolerance tolerance)
 {
     createInstance();
     BaseCurve.SetFromGeCurve(geCurve, __unnamed001, tolerance);
     tr.Dispose();
 }
Пример #20
0
 public void SetFromGeCurve(Curve3d geCurve, Tolerance tolerance)
 {
     createInstance();
     BaseCurve.SetFromGeCurve(geCurve, tolerance);
     tr.Dispose();
 }
        /// <summary>
        /// Gets a value indicating whether the specified point belongs to the collection.
        /// </summary>
        /// <param name="pts">The instance to which the method applies.</param>
        /// <param name="pt">The point to search.</param>
        /// <param name="tol">The tolerance to use in comparisons.</param>
        /// <returns>true if the point is found; otherwise, false.</returns>
        public static bool Contains([NotNull] this Point2dCollection pts, Point2d pt, Tolerance tol)
        {
            foreach (var t in pts)
            {
                if (pt.IsEqualTo(t, tol))
                {
                    return(true);
                }
            }

            return(false);
        }
Пример #22
0
        public void MassToleranceImplicitValue()
        {
            var tol = new Tolerance("10 ppm");

            Assert.AreEqual(10, tol.Value);
        }
Пример #23
0
        public Xic GetProductExtractedIonChromatogram(double productIonMz, double precursorIonMz, Tolerance tolerance, int minScanNum, int maxScanNum)
        {
            var productXic = new Xic();

            for (var scanNum = minScanNum; scanNum <= maxScanNum; scanNum++)
            {
                if (GetMsLevel(scanNum) == 1)
                {
                    continue;
                }

                var productSpec = _scanNumSpecMap[scanNum] as ProductSpectrum;
                if (productSpec == null)
                {
                    continue;
                }

                if (!productSpec.IsolationWindow.Contains(precursorIonMz))
                {
                    continue;
                }

                var peak = productSpec.FindPeak(productIonMz, tolerance);
                if (peak != null)
                {
                    productXic.Add(new XicPoint(scanNum, peak.Mz, peak.Intensity));
                }
            }

            return(productXic);
        }
Пример #24
0
        /// <summary>
        /// Прополка полилинии
        /// </summary>        
        public static void Wedding(this Polyline pl, Tolerance tolerance)
        {
            var count = pl.NumberOfVertices;
            for (int i = 1; i < count; i++)
            {
                int iPrew;
                int iCur;
                int iNext;
                iPrew = i-1;
                iCur = i;
                iNext = i + 1;
                if (iNext == count)
                {
                    break;
                }
                var prew = pl.GetPoint2dAt(iPrew);
                var cur = pl.GetPoint2dAt(iCur);
                var next = pl.GetPoint2dAt(iNext);
                if (IsPointsOnSomeLine(prew, cur, next, tolerance))
                {
                    pl.RemoveVertexAt(i);
                    i--;
                    count--;
                }
            }

            count = pl.NumberOfVertices;

            // Если начальная точка совпадает с конечной, то проверка сегменов до и после
            if (count > 3)
            {
                Point2d fp = pl.GetPoint2dAt(0);
                Point2d lp = pl.GetPoint2dAt(count - 1);
                Point2d next = pl.GetPoint2dAt(1);
                if (fp.IsEqualTo(lp, tolerance))
                {
                    var cur = fp;
                    var prew = pl.GetPoint2dAt(count - 2);
                    if (IsPointsOnSomeLine(prew, cur, next, tolerance))
                    {
                        pl.RemoveVertexAt(count - 1);
                        pl.RemoveVertexAt(0);
                        if (!pl.Closed)
                        {
                            pl.Closed = true;
                        }
                    }
                }
                else if (pl.Closed)
                {
                    var prew = lp;
                    var cur = fp;
                    if (IsPointsOnSomeLine(prew, cur, next, tolerance))
                    {
                        pl.RemoveVertexAt(0);
                    }
                }
            }
        }
Пример #25
0
        public virtual SortedMaxSizedContainer<PeptideSpectralMatch> Search(IMassSpectrum massSpectrum, IEnumerable<Peptide> peptides, FragmentTypes fragmentTypes, Tolerance productMassTolerance)
        {
            SortedMaxSizedContainer<PeptideSpectralMatch> results = new SortedMaxSizedContainer<PeptideSpectralMatch>(MaxMatchesPerSpectrum);

            foreach (var peptide in peptides)
            {
                results.Add(Search(massSpectrum, peptide, fragmentTypes, productMassTolerance));
            }

            return results;
        }
Пример #26
0
 private static bool IsPointsOnSomeLine(Point2d pt1, Point2d pt2, Point2d pt3, Tolerance tolerance)
 {
     var vec1 = pt2 - pt1;
     var vec2 = pt3 - pt1;
     return vec1.IsParallelTo(vec2, tolerance);
 }
Пример #27
0
        public void MassToleranceImplicitPlusMinus5()
        {
            var tol = new Tolerance("10 ppm");

            Assert.AreEqual(tol.Type, ToleranceType.FullWidth);
        }
Пример #28
0
 /// <summary>
 ///     TODO
 /// </summary>
 /// <param name="axis"></param>
 /// <param name="tol"></param>
 /// <returns></returns>
 public bool IsParallelTo(Axis axis, Tolerance tol)
 {
     return(Start.GetVectorTo(End).IsParallelTo(axis.Start.GetVectorTo(axis.End), tol));
 }
Пример #29
0
        public void MassToleranceConstructorDaValue()
        {
            var tol = new Tolerance(ToleranceUnit.DA, 10);

            Assert.AreEqual(10, tol.Value);
        }
Пример #30
0
        public void FindMissingLcMsFeatures()
        {
            var mspfFolder  = @"D:\MassSpecFiles\CompRef_Kelleher\Study3";
            var ms1ftFolder = @"D:\MassSpecFiles\CompRef_Kelleher\Study3";

            const int Nfraction1 = 3;
            const int Nfraction2 = 5;

            for (var frac1 = 1; frac1 <= Nfraction1; frac1++)
            {
                for (var frac2 = 1; frac2 <= Nfraction2; frac2++)
                {
                    var datasets = GetDataSetNamesStudy3(frac1, frac2);
                    //var outFilePath = string.Format(@"D:\MassSpecFiles\CompRef_Kelleher\study3_GFrep{0}_Gfrac{1}.tsv", frac1.ToString("D2"), frac2.ToString("D2"));
                    var nDataset   = datasets.Count;
                    var prsmReader = new ProteinSpectrumMatchReader();
                    var tolerance  = new Tolerance(12);

                    for (var i = 0; i < nDataset; i++)
                    {
                        var rawFile   = string.Format(@"{0}\{1}.pbf", PbfPath, datasets[i]);
                        var mspFile   = string.Format(@"{0}\{1}_IcTda.tsv", mspfFolder, datasets[i]);
                        var ms1FtFile = string.Format(@"{0}\{1}.ms1ft", ms1ftFolder, datasets[i]);
                        var outPath   = string.Format(@"{0}\{1}.seqtag.ms1ft", ms1ftFolder, datasets[i]);

                        if (File.Exists(outPath))
                        {
                            continue;
                        }

                        var run              = PbfLcMsRun.GetLcMsRun(rawFile);
                        var features         = LcMsFeatureAlignment.LoadProMexResult(i, ms1FtFile, run);
                        var prsmList         = prsmReader.LoadIdentificationResult(mspFile, ProteinSpectrumMatch.SearchTool.MsPathFinder);
                        var prsmFeatureMatch = new bool[prsmList.Count];

                        for (var j = 0; j < features.Count; j++)
                        {
                            //features[j].ProteinSpectrumMatches = new ProteinSpectrumMatchSet(i);
                            var massTol = tolerance.GetToleranceAsTh(features[j].Mass);
                            for (var k = 0; k < prsmList.Count; k++)
                            {
                                var match = prsmList[k];
                                if (features[j].MinScanNum < match.ScanNum && match.ScanNum < features[j].MaxScanNum && Math.Abs(features[j].Mass - match.Mass) < massTol)
                                {
                                    features[j].ProteinSpectrumMatches.Add(match);
                                    prsmFeatureMatch[k] = true;
                                }
                            }
                        }

                        var missingPrsm = new List <ProteinSpectrumMatch>();
                        for (var k = 0; k < prsmList.Count; k++)
                        {
                            if (!prsmFeatureMatch[k])
                            {
                                missingPrsm.Add(prsmList[k]);
                            }
                        }

                        FeatureFind(missingPrsm, run, outPath);
                        Console.WriteLine(outPath);
                    }
                }
            }
        }
Пример #31
0
        public bool?Equal(object x, object y, ref Tolerance tolerance, ComparisonState state)
        {
            if (!(x is Stream) || !(y is Stream))
            {
                return(null);
            }

            Stream xStream = (Stream)x;
            Stream yStream = (Stream)y;

            if (xStream == yStream)
            {
                return(true);
            }

            if (!xStream.CanRead)
            {
                throw new ArgumentException("Stream is not readable", "expected");
            }
            if (!yStream.CanRead)
            {
                throw new ArgumentException("Stream is not readable", "actual");
            }
            if (!xStream.CanSeek)
            {
                throw new ArgumentException("Stream is not seekable", "expected");
            }
            if (!yStream.CanSeek)
            {
                throw new ArgumentException("Stream is not seekable", "actual");
            }

            if (xStream.Length != yStream.Length)
            {
                return(false);
            }

            byte[] bufferExpected = new byte[BUFFER_SIZE];
            byte[] bufferActual   = new byte[BUFFER_SIZE];

            BinaryReader binaryReaderExpected = new BinaryReader(xStream);
            BinaryReader binaryReaderActual   = new BinaryReader(yStream);

            long expectedPosition = xStream.Position;
            long actualPosition   = yStream.Position;

            try
            {
                binaryReaderExpected.BaseStream.Seek(0, SeekOrigin.Begin);
                binaryReaderActual.BaseStream.Seek(0, SeekOrigin.Begin);

                for (long readByte = 0; readByte < xStream.Length; readByte += BUFFER_SIZE)
                {
                    binaryReaderExpected.Read(bufferExpected, 0, BUFFER_SIZE);
                    binaryReaderActual.Read(bufferActual, 0, BUFFER_SIZE);

                    for (int count = 0; count < BUFFER_SIZE; ++count)
                    {
                        if (bufferExpected[count] != bufferActual[count])
                        {
                            NUnitEqualityComparer.FailurePoint fp = new NUnitEqualityComparer.FailurePoint();
                            fp.Position        = readByte + count;
                            fp.ExpectedHasData = true;
                            fp.ExpectedValue   = bufferExpected[count];
                            fp.ActualHasData   = true;
                            fp.ActualValue     = bufferActual[count];
                            _equalityComparer.FailurePoints.Insert(0, fp);
                            return(false);
                        }
                    }
                }
            }
            finally
            {
                xStream.Position = expectedPosition;
                yStream.Position = actualPosition;
            }

            return(true);
        }
Пример #32
0
        //private double Search(ref double[] eMasses, ref double[] eIntenisties, double[] tMasses, double productTolerance, double tic, ref Dictionary<double, double> scores)
        //{
        //    double score = 0.0;
        //    int eLength = eMasses.Length;
        //    int tLength = tMasses.Length;
        //    int e = 0;
        //    foreach (double t in tMasses)
        //    {
        //        double storedScore;
        //        if (scores.TryGetValue(t, out storedScore))
        //        {
        //            score += storedScore;
        //            continue;
        //        }
        //        double minMZ = t - productTolerance;
        //        double maxMZ = t + productTolerance;
        //        while (e < eLength && eMasses[e] < minMZ)
        //            e++;
        //        if (e >= eLength)
        //            break;
        //        if (eMasses[e] > maxMZ)
        //            continue;
        //        double intensities = 0;
        //        int index = e; // switch variables to keep e the same for the next loop around
        //        do
        //        {
        //            intensities += eIntenisties[index];
        //            index++;
        //        } while (index < eLength && eMasses[index] < maxMZ);
        //        storedScore = 1 + intensities/tic;
        //        score += storedScore;
        //        scores[t] = storedScore;
        //    }
        //    return score;
        //}
        /// <summary>
        /// The main searching algorithm of Morpheus
        /// </summary>
        /// <param name="eMasses">The experimental masses</param>
        /// <param name="eIntenisties">The experimental intensities</param>
        /// <param name="tMasses">The theoretical masses</param>
        /// <param name="productTolerance">The product mass tolerance</param>
        /// <param name="tic">The total ion current of the experimental peaks</param>
        /// <returns></returns>
        private double Search(double[] eMasses, double[] eIntenisties, double[] tMasses, Tolerance productTolerance, double tic)
        {
            double score = 0.0;
            double intensities = 0.0;
            int eLength = eMasses.Length;
            int tLength = tMasses.Length;
            int e = 0;

            bool forceCheck = productTolerance.GetMinimumValue(tMasses[tLength - 1]) >= eMasses[eLength - 1];
            if (forceCheck)
            {
                foreach (double t in tMasses)
                {
                    IRange<double> range = productTolerance.GetRange(t);
                    double minMZ = range.Minimum;
                    double maxMZ = range.Maximum;

                    while (e < eLength && eMasses[e] < minMZ)
                        e++;

                    if (e >= eLength)
                        break;

                    if (eMasses[e] > maxMZ)
                        continue;

                    score++;

                    int index = e; // switch variables to keep e the same for the next loop around
                    do
                    {
                        intensities += eIntenisties[index];
                        index++;
                    } while (index < eLength && eMasses[index] < maxMZ);
                }
            }
            else
            {
                foreach (double t in tMasses)
                {
                    IRange<double> range = productTolerance.GetRange(t);
                    double minMZ = range.Minimum;
                    double maxMZ = range.Maximum;

                    while (eMasses[e] < minMZ)
                        e++;

                    if (eMasses[e] > maxMZ)
                        continue;

                    score++;

                    int index = e; // switch variables to keep e the same for the next loop around
                    do
                    {
                        intensities += eIntenisties[index];
                        index++;
                    } while (index < eLength && eMasses[index] < maxMZ);
                }
            }
            return score + intensities/tic;
        }
Пример #33
0
        public void AlignFeatures(List <string> datasets, string mspfFolder, string ms1ftFolder, string outFilePath)
        {
            var nDataset   = datasets.Count;
            var prsmReader = new ProteinSpectrumMatchReader();
            var tolerance  = new Tolerance(12);
            var alignment  = new LcMsFeatureAlignment(new AnalysisCompRef.CompRefFeatureComparer(tolerance));

            for (var i = 0; i < nDataset; i++)
            {
                var rawFile    = string.Format(@"{0}\{1}.pbf", PbfPath, datasets[i]);
                var mspFile    = string.Format(@"{0}\{1}_IcTda.tsv", mspfFolder, datasets[i]);
                var ms1FtFile  = string.Format(@"{0}\{1}.ms1ft", ms1ftFolder, datasets[i]);
                var ms1FtFile2 = string.Format(@"{0}\{1}.seqtag.ms1ft", ms1ftFolder, datasets[i]);

                var run       = PbfLcMsRun.GetLcMsRun(rawFile);
                var features  = LcMsFeatureAlignment.LoadProMexResult(i, ms1FtFile, run);
                var features2 = LcMsFeatureAlignment.LoadProMexResult(i, ms1FtFile2, run);
                features.AddRange(features2);

                if (File.Exists(mspFile))
                {
                    var prsmList = prsmReader.LoadIdentificationResult(mspFile, ProteinSpectrumMatch.SearchTool.MsPathFinder);
                    //var prsmFeatureMatch = new bool[prsmList.Count];

                    for (var j = 0; j < prsmList.Count; j++)
                    {
                        var match = prsmList[j];
                        match.ProteinId = match.ProteinName;
                    }

                    // tag features by PrSMs
                    for (var j = 0; j < features.Count; j++)
                    {
                        //features[j].ProteinSpectrumMatches = new ProteinSpectrumMatchSet(i);
                        var massTol = tolerance.GetToleranceAsTh(features[j].Mass);
                        for (var k = 0; k < prsmList.Count; k++)
                        {
                            var match = prsmList[k];
                            if (features[j].MinScanNum < match.ScanNum && match.ScanNum < features[j].MaxScanNum && Math.Abs(features[j].Mass - match.Mass) < massTol)
                            {
                                features[j].ProteinSpectrumMatches.Add(match);
                                //prsmFeatureMatch[k] = true;
                            }
                        }
                    }
                }

                alignment.AddDataSet(i, features, run);
            }

            alignment.AlignFeatures();

            Console.WriteLine("{0} alignments ", alignment.CountAlignedFeatures);

            for (var i = 0; i < nDataset; i++)
            {
                alignment.FillMissingFeatures(i);
                Console.WriteLine("{0} has been processed", datasets[i]);
            }

            AnalysisCompRef.OutputCrossTabWithId(outFilePath, alignment, datasets.ToArray());
        }
Пример #34
0
 /// <summary>
 /// Perform the comparison
 /// </summary>
 protected override bool PerformComparison(ComparisonAdapter comparer, object actual, object expected, Tolerance tolerance)
 {
     return(comparer.Compare(actual, tolerance.ApplyToValue(expected).UpperBound) <= 0);
 }
Пример #35
0
        public IEnumerable <Tuple <List <IMzPeak>, int> > Deconvolute(MzRange theRange, int maxAssumedChargeState, Tolerance massTolerance, double intensityRatio)
        {
            var isolatedMassesAndCharges = new List <Tuple <List <IMzPeak>, int> >();

            foreach (var peak in Extract(theRange))
            {
                // Always assume the current peak is a monoisotopic peak!

                List <IMzPeak> bestListOfPeaks = new List <IMzPeak>();
                int            bestChargeState = 1;
                for (int chargeState = 1; chargeState <= maxAssumedChargeState; chargeState++)
                {
                    var listOfPeaksForThisChargeState = new List <IMzPeak> {
                        peak
                    };
                    var mMass = peak.Mz.ToMass(chargeState);
                    for (int mm = 1; mm <= mms.Length; mm++)
                    {
                        double diffToNextMmPeak = mms[mm - 1];
                        double theorMass        = mMass + diffToNextMmPeak;
                        var    closestpeak      = GetClosestPeak(theorMass.ToMz(chargeState));
                        if (massTolerance.Within(closestpeak.Mz.ToMass(chargeState), theorMass) && SatisfiesRatios(mMass, mm, peak, closestpeak, intensityRatio))
                        {
                            // Found a match to an isotope peak for this charge state!
                            listOfPeaksForThisChargeState.Add(closestpeak);
                        }
                        else
                        {
                            break;
                        }
                    }
                    if (listOfPeaksForThisChargeState.Count >= bestListOfPeaks.Count)
                    {
                        bestListOfPeaks = listOfPeaksForThisChargeState;
                        bestChargeState = chargeState;
                    }
                }
                if (bestListOfPeaks.Count >= 2)
                {
                    isolatedMassesAndCharges.Add(new Tuple <List <IMzPeak>, int>(bestListOfPeaks, bestChargeState));
                }
            }

            List <double> seen = new List <double>();

            while (isolatedMassesAndCharges.Any())
            {
                // Pick longest
                var longest = isolatedMassesAndCharges.OrderByDescending(b => b.Item1.Count).First();
                yield return(longest);

                isolatedMassesAndCharges.Remove(longest);
                isolatedMassesAndCharges.RemoveAll(b => b.Item1.Intersect(longest.Item1).Any());
            }
        }
        /// <summary>
        /// Reads RawData into structured data.
        /// </summary>
        public void StructureData(CFGData cfg)
        {
            this.Name = cfg.Name;

            this.Strength = cfg.Attributes[0];
            this.Intelligence = cfg.Attributes[1];
            this.Willpower = cfg.Attributes[2];
            this.Agility = cfg.Attributes[3];
            this.Endurance = cfg.Attributes[4];
            this.Personality = cfg.Attributes[5];
            this.Speed = cfg.Attributes[6];
            this.Luck = cfg.Attributes[7];

            this.PrimarySkill1 = (Skills)cfg.PrimarySkill1;
            this.PrimarySkill2 = (Skills)cfg.PrimarySkill2;
            this.PrimarySkill3 = (Skills)cfg.PrimarySkill3;
            this.MajorSkill1 = (Skills)cfg.MajorSkill1;
            this.MajorSkill2 = (Skills)cfg.MajorSkill2;
            this.MajorSkill3 = (Skills)cfg.MajorSkill3;
            this.MinorSkill1 = (Skills)cfg.MinorSkill1;
            this.MinorSkill2 = (Skills)cfg.MinorSkill2;
            this.MinorSkill3 = (Skills)cfg.MinorSkill3;
            this.MinorSkill4 = (Skills)cfg.MinorSkill4;
            this.MinorSkill5 = (Skills)cfg.MinorSkill5;
            this.MinorSkill6 = (Skills)cfg.MinorSkill6;

            this.HitPointsPerLevelOrMonsterLevel = cfg.HitPointsPerLevelOrMonsterLevel;

            float value = (cfg.AdvancementMultiplier >> 16) + ((cfg.AdvancementMultiplier & 0xffff)) / 65536f;
            try
            {
                this.AdvancementMultiplier = float.Parse(string.Format("{0:0.00}", value), NumberStyles.Float, CultureInfo.InvariantCulture);
            }
            catch
            {
                this.AdvancementMultiplier = value;
            }

            this.Paralysis = GetTolerance(cfg, EffectFlags.Paralysis);
            this.Magic = GetTolerance(cfg, EffectFlags.Magic);
            this.Poison = GetTolerance(cfg, EffectFlags.Poison);
            this.Fire = GetTolerance(cfg, EffectFlags.Fire);
            this.Frost = GetTolerance(cfg, EffectFlags.Frost);
            this.Shock = GetTolerance(cfg, EffectFlags.Shock);
            this.Disease = GetTolerance(cfg, EffectFlags.Disease);

            this.ForbiddenMaterials = (MaterialFlags)cfg.ForbiddenMaterialsFlags;
            this.ForbiddenShields = (ShieldFlags)((cfg.WeaponArmorShieldsBitfield >> 9) & 0x0f);
            this.ForbiddenArmors = (ArmorFlags)((cfg.WeaponArmorShieldsBitfield >> 6) & 0x07);
            this.ForbiddenProficiencies = (ProficiencyFlags)(cfg.WeaponArmorShieldsBitfield & 0x3f);
            this.ExpertProficiencies = (ProficiencyFlags)((cfg.WeaponArmorShieldsBitfield >> 16) & 0x3f);

            this.ShortBlades = GetProficiency(ProficiencyFlags.ShortBlades);
            this.LongBlades = GetProficiency(ProficiencyFlags.LongBlades);
            this.HandToHand = GetProficiency(ProficiencyFlags.HandToHand);
            this.Axes = GetProficiency(ProficiencyFlags.Axes);
            this.BluntWeapons = GetProficiency(ProficiencyFlags.BluntWeapons);
            this.MissileWeapons = GetProficiency(ProficiencyFlags.MissileWeapons);

            this.SpellPointMultiplier = GetSpellPointMultiplier(cfg);
            this.SpellPointMultiplierValue = GetSpellPointMultiplierValue(this.SpellPointMultiplier);

            this.DarknessPoweredMagery = (DarknessMageryFlags)((cfg.AbilityFlagsAndSpellPointsBitfield & 0x300) >> 8);
            this.LightPoweredMagery = (LightMageryFlags)((cfg.AbilityFlagsAndSpellPointsBitfield & 0x00C0) >> 6);

            this.SpellAbsorption = (SpellAbsorptionFlags)cfg.SpellAbsorptionFlags;

            this.NoRegenSpellPoints = HasSpecialAbility(cfg, SpecialAbilityFlags.NoRegenSpellPoints);

            this.AcuteHearing = HasSpecialAbility(cfg, SpecialAbilityFlags.AcuteHearing);
            this.Athleticism = HasSpecialAbility(cfg, SpecialAbilityFlags.Athleticism);
            this.AdrenalineRush = HasSpecialAbility(cfg, SpecialAbilityFlags.AdrenalineRush);

            this.Regeneration = (RegenerationFlags)cfg.Regeneration;
            this.RapidHealing = (RapidHealingFlags)cfg.RapidHealing;

            this.DamageFromSunlight = HasSpecialAbility(cfg, SpecialAbilityFlags.SunDamage);
            this.DamageFromHolyPlaces = HasSpecialAbility(cfg, SpecialAbilityFlags.HolyDamage);

            this.UndeadAttackModifier = GetAttackModifier(cfg, EnemyGroups.Undead);
            this.DaedraAttackModifier = GetAttackModifier(cfg, EnemyGroups.Daedra);
            this.HumanoidAttackModifier = GetAttackModifier(cfg, EnemyGroups.Humanoid);
            this.AnimalsAttackModifier = GetAttackModifier(cfg, EnemyGroups.Animals);
        }
        public CompositeScorerBasedOnDeconvolutedSpectrum(DeconvolutedSpectrum deconvolutedSpectrum, ProductSpectrum spec, Tolerance productTolerance, IMassBinning comparer)
            : base(deconvolutedSpectrum, productTolerance)
        {
            ReferencePeakIntensity = GetRefIntensity(spec.Peaks);
            _comparer         = comparer;
            _massBinToPeakMap = new Dictionary <int, DeconvolutedPeak>();

            foreach (var p in deconvolutedSpectrum.Peaks)
            {
                var mass      = p.Mz;
                var deltaMass = productTolerance.GetToleranceAsDa(mass, 1);
                var minMass   = mass - deltaMass;
                var maxMass   = mass + deltaMass;

                var binNum = comparer.GetBinNumber(mass);

                if (binNum < 0)
                {
                    binNum = comparer.GetBinNumber(minMass);
                    if (binNum < 0)
                    {
                        binNum = comparer.GetBinNumber(maxMass);
                    }
                }

                // filter out
                if (binNum < 0)
                {
                    continue;
                }

                UpdateDeconvPeak(binNum, p as DeconvolutedPeak);
                // going up
                for (var nextBinNum = binNum + 1; nextBinNum < comparer.NumberOfBins; nextBinNum++)
                {
                    var nextBinMass = comparer.GetMassStart(nextBinNum);
                    if (minMass < nextBinMass && nextBinMass < maxMass)
                    {
                        UpdateDeconvPeak(nextBinNum, p as DeconvolutedPeak);                                                 //_ionMassChkBins[nextBinNum] = true;
                    }
                    else
                    {
                        break;
                    }
                }

                // going down
                for (var prevBinNum = binNum - 1; prevBinNum < comparer.NumberOfBins; prevBinNum--)
                {
                    var prevBinMass = comparer.GetMassEnd(prevBinNum);
                    if (minMass < prevBinMass && prevBinMass < maxMass)
                    {
                        UpdateDeconvPeak(prevBinNum, p as DeconvolutedPeak);                                                 //_ionMassChkBins[prevBinNum] = true;
                    }
                    else
                    {
                        break;
                    }
                }
            }
        }
Пример #38
0
        public void FilteringEfficiencyQcShew()
        {
            var methodName = MethodBase.GetCurrentMethod().Name;

            Utils.ShowStarting(methodName);

            var sw = new System.Diagnostics.Stopwatch();

            sw.Start();
            const string rawFilePath = @"C:\cygwin\home\kims336\Data\TopDownQCShew\raw\QC_ShewIntact_2ug_3k_CID_4Apr14_Bane_PL011402.raw";

            if (!File.Exists(rawFilePath))
            {
                Assert.Ignore(@"Skipping test {0} since file not found: {1}", methodName, rawFilePath);
            }

            var run = InMemoryLcMsRun.GetLcMsRun(rawFilePath, 1.4826, 1.4826);

            sw.Stop();

            Console.WriteLine(@"Reading run: {0:f4} sec", sw.Elapsed.TotalSeconds);

            const int minPrecursorCharge = 3;
            const int maxPrecursorCharge = 30;
            const int tolerancePpm       = 10;
            var       tolerance          = new Tolerance(tolerancePpm);

            sw.Reset();
            sw.Start();
            var ms1BasedFilter = new Ms1IsotopeAndChargeCorrFilter(run, new Tolerance(10.0), minPrecursorCharge, maxPrecursorCharge, 3000, 50000, 0.7, 0.7, 0.7, 40);

            //var ms1BasedFilter = new Ms1IsotopeCorrFilter(run, minPrecursorCharge, maxPrecursorCharge, 15, 0.5, 40);

            sw.Stop();

            Console.WriteLine(@"Ms1 filter: {0:f4} sec", sw.Elapsed.TotalSeconds);

            ISequenceFilter ms1Filter = ms1BasedFilter;

            sw.Reset();
            sw.Start();
            const double minProteinMass = 3000.0;
            const double maxProteinMass = 30000.0;
            var          minBinNum      = ProductScorerBasedOnDeconvolutedSpectra.GetBinNumber(minProteinMass);
            var          maxBinNum      = ProductScorerBasedOnDeconvolutedSpectra.GetBinNumber(maxProteinMass);
            var          numComparisons = 0L;

            for (var binNum = minBinNum; binNum <= maxBinNum; binNum++)
            {
                var mass = ProductScorerBasedOnDeconvolutedSpectra.GetMz(binNum);
                numComparisons += ms1Filter.GetMatchingMs2ScanNums(mass).Count();
            }
            sw.Stop();

            Console.WriteLine(@"Calculating #matches per bin: {0:f4} sec", sw.Elapsed.TotalSeconds);

            //const string prot =
            //    "ADVFHLGLTKAMLDGATLAIVPGDPERVKRIAELMDNATFLASHREYTSYLAYADGKPVVICSTGIGGPSTSIAVEELAQLGVNTFLRVGTTGAIQPHVNVGDVIVTQASVRLDGASLHFAPMEFPAVANFECTTAMVAACRDAGVEPHIGVTASSDTFYPGQERYDTVTGRVTRRFAGSMKEWQDMGVLNYEMESATLFTMCATQGWRAACVAGVIVNRTQQEIPDEATMKKTEVSAVSIVVAAAKKLLA";
            //var protMass = (new AminoAcidSet().GetComposition(prot) + Composition.H2O).Mass;
            //Console.WriteLine("************ScanNums: " + string.Join("\t", ms1Filter.GetMatchingMs2ScanNums(protMass)));

            const string resultFilePath = @"C:\cygwin\home\kims336\Data\TopDownQCShew\MSAlign\NoMod.tsv";

            if (!File.Exists(resultFilePath))
            {
                Assert.Ignore(@"Skipping test {0} since file not found: {1}", methodName, resultFilePath);
            }

            var tsvReader = new TsvFileParser(resultFilePath);
            var scanNums  = tsvReader.GetData("Scan(s)");
            var charges   = tsvReader.GetData("Charge");
            var scores    = tsvReader.GetData("E-value");
            var sequences = tsvReader.GetData("Peptide");

            //const string resultFilePath = @"C:\cygwin\home\kims336\Data\TopDownQCShew\raw\QC_ShewIntact_2ug_3k_CID_4Apr14_Bane_PL011402_N30_C30.tsv";
            //var tsvReader = new TsvFileParser(resultFilePath);
            //var scanNums = tsvReader.GetData("ScanNum");
            //var charges = tsvReader.GetData("Charge");
            //var scores = tsvReader.GetData("Score");
            //var sequences = tsvReader.GetData("Sequence");

            var aaSet = new AminoAcidSet();

            var seqSet             = new HashSet <string>();
            var allSeqSet          = new HashSet <string>();
            var numUnfilteredSpecs = 0;
            var totalSpecs         = 0;

            for (var i = 0; i < scores.Count; i++)
            {
                var score = Convert.ToDouble(scores[i]);
                if (score > 1E-4)
                {
                    continue;
                }
                //if (score < 10) continue;

                var scanNum = Convert.ToInt32(scanNums[i]);
                var charge  = Convert.ToInt32(charges[i]);

                var sequence = SimpleStringProcessing.GetStringBetweenDots(sequences[i]);
                if (sequence == null || sequence.Contains("("))
                {
                    continue;
                }
                //var sequence = sequences[i];
                var composition = aaSet.GetComposition(sequence) + Composition.H2O;

                var precursorIon = new Ion(composition, charge);
                var isValid      = run.GetSpectrum(scanNum) is ProductSpectrum spec && spec.IsolationWindow.Contains(precursorIon.GetMostAbundantIsotopeMz());
                if (!isValid)
                {
                    continue;
                }
                ++totalSpecs;

                var precursorScanNum = run.GetPrecursorScanNum(scanNum);
                var precursorSpec    = run.GetSpectrum(precursorScanNum);
                var corr1            = precursorSpec.GetCorrScore(precursorIon, tolerance, 0.1);

                var nextScanNum = run.GetNextScanNum(scanNum, 1);
                var nextSpec    = run.GetSpectrum(nextScanNum);
                var corr2       = nextSpec.GetCorrScore(precursorIon, tolerance, 0.1);

                var corr3 = ms1Filter.GetMatchingMs2ScanNums(composition.Mass).Contains(scanNum) ? 1 : 0;
                if (corr3 == 1)
                {
                    numUnfilteredSpecs++;
                    seqSet.Add(sequences[i]);
                }
                allSeqSet.Add(sequences[i]);

                var corrMax = new[] { corr1, corr2, corr3 }.Max();

                Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}", scanNum, precursorScanNum, corr1, nextScanNum, corr2, corr3, corrMax);
            }

            Console.WriteLine("TotalNumComparisons: {0}", numComparisons);
            Console.WriteLine("AverageNumComparisons: {0:f2}", numComparisons / (double)(maxBinNum - minBinNum + 1));
            Console.WriteLine("SuccessRate: {0:f2} {1} / {2}", numUnfilteredSpecs / (double)totalSpecs, numUnfilteredSpecs, totalSpecs);
            Console.WriteLine("NumUniqueSequences: {0:f2}, {1} / {2}", seqSet.Count / (double)allSeqSet.Count, seqSet.Count, allSeqSet.Count);

            Console.WriteLine(@"Elapsed Time: {0:f4} sec", sw.Elapsed.TotalSeconds);
        }
Пример #39
0
        protected override MyTaskResults RunSpecific(string OutputFolder, List <DbForTask> dbFilenameList, List <string> currentRawFileList, string taskId, FileSpecificParameters[] fileSettingsList)
        {
            LoadModifications(taskId, out var variableModifications, out var fixedModifications, out var localizeableModificationTypes);

            // TODO: print error messages loading GPTMD mods
            List <Modification> gptmdModifications       = GlobalVariables.AllModsKnown.OfType <Modification>().Where(b => GptmdParameters.ListOfModsGptmd.Contains((b.ModificationType, b.IdWithMotif))).ToList();
            IEnumerable <Tuple <double, double> > combos = LoadCombos(gptmdModifications).ToList();

            // load proteins
            List <Protein> proteinList = LoadProteins(taskId, dbFilenameList, true, DecoyType.Reverse, localizeableModificationTypes, CommonParameters);

            List <PeptideSpectralMatch> allPsms = new List <PeptideSpectralMatch>();

            var numRawFiles = currentRawFileList.Count;

            // write prose settings
            ProseCreatedWhileRunning.Append("The following G-PTM-D settings were used: "); ProseCreatedWhileRunning.Append("protease = " + CommonParameters.DigestionParams.Protease + "; ");
            ProseCreatedWhileRunning.Append("maximum missed cleavages = " + CommonParameters.DigestionParams.MaxMissedCleavages + "; ");
            ProseCreatedWhileRunning.Append("minimum peptide length = " + CommonParameters.DigestionParams.MinPeptideLength + "; ");
            ProseCreatedWhileRunning.Append(CommonParameters.DigestionParams.MaxPeptideLength == int.MaxValue ?
                                            "maximum peptide length = unspecified; " :
                                            "maximum peptide length = " + CommonParameters.DigestionParams.MaxPeptideLength + "; ");
            ProseCreatedWhileRunning.Append("initiator methionine behavior = " + CommonParameters.DigestionParams.InitiatorMethionineBehavior + "; ");
            ProseCreatedWhileRunning.Append("max modification isoforms = " + CommonParameters.DigestionParams.MaxModificationIsoforms + "; ");
            ProseCreatedWhileRunning.Append("fixed modifications = " + string.Join(", ", fixedModifications.Select(m => m.IdWithMotif)) + "; ");
            ProseCreatedWhileRunning.Append("variable modifications = " + string.Join(", ", variableModifications.Select(m => m.IdWithMotif)) + "; ");
            ProseCreatedWhileRunning.Append("G-PTM-D modifications count = " + gptmdModifications.Count + "; ");

            // temporary search type for writing prose
            // the actual search type is technically file-specific but we don't allow file-specific notches, so it's safe to do this
            MassDiffAcceptor tempSearchMode = new DotMassDiffAcceptor("", GetAcceptableMassShifts(fixedModifications, variableModifications, gptmdModifications, combos), CommonParameters.PrecursorMassTolerance);

            ProseCreatedWhileRunning.Append("precursor mass tolerance(s) = {" + tempSearchMode.ToProseString() + "}; ");

            ProseCreatedWhileRunning.Append("product mass tolerance = " + CommonParameters.ProductMassTolerance + ". ");
            ProseCreatedWhileRunning.Append("The combined search database contained " + proteinList.Count(p => !p.IsDecoy) + " non-decoy protein entries including " + proteinList.Where(p => p.IsContaminant).Count() + " contaminant sequences. ");

            // start the G-PTM-D task
            Status("Running G-PTM-D...", new List <string> {
                taskId
            });
            MyTaskResults = new MyTaskResults(this)
            {
                NewDatabases = new List <DbForTask>()
            };
            var fileSpecificCommonParams = fileSettingsList.Select(b => SetAllFileSpecificCommonParams(CommonParameters, b));
            HashSet <DigestionParams> ListOfDigestionParams = new HashSet <DigestionParams>(fileSpecificCommonParams.Select(p => p.DigestionParams));

            MyFileManager myFileManager = new MyFileManager(true);

            object lock1 = new object();
            object lock2 = new object();

            for (int spectraFileIndex = 0; spectraFileIndex < currentRawFileList.Count; spectraFileIndex++)
            {
                // Stop if canceled
                if (GlobalVariables.StopLoops)
                {
                    break;
                }

                var origDataFile = currentRawFileList[spectraFileIndex];

                // mark the file as in-progress
                StartingDataFile(origDataFile, new List <string> {
                    taskId, "Individual Spectra Files", origDataFile
                });

                CommonParameters combinedParams = SetAllFileSpecificCommonParams(CommonParameters, fileSettingsList[spectraFileIndex]);
                MassDiffAcceptor searchMode     = new DotMassDiffAcceptor("", GetAcceptableMassShifts(fixedModifications, variableModifications, gptmdModifications, combos), combinedParams.PrecursorMassTolerance);

                NewCollection(Path.GetFileName(origDataFile), new List <string> {
                    taskId, "Individual Spectra Files", origDataFile
                });

                Status("Loading spectra file...", new List <string> {
                    taskId, "Individual Spectra Files", origDataFile
                });
                MsDataFile myMsDataFile = myFileManager.LoadFile(origDataFile, combinedParams);
                Status("Getting ms2 scans...", new List <string> {
                    taskId, "Individual Spectra Files", origDataFile
                });
                Ms2ScanWithSpecificMass[] arrayOfMs2ScansSortedByMass = GetMs2Scans(myMsDataFile, origDataFile, combinedParams).OrderBy(b => b.PrecursorMass).ToArray();
                myFileManager.DoneWithFile(origDataFile);
                PeptideSpectralMatch[] allPsmsArray = new PeptideSpectralMatch[arrayOfMs2ScansSortedByMass.Length];
                new ClassicSearchEngine(allPsmsArray, arrayOfMs2ScansSortedByMass, variableModifications, fixedModifications, null, proteinList, searchMode, combinedParams, new List <string> {
                    taskId, "Individual Spectra Files", origDataFile
                }).Run();
                allPsms.AddRange(allPsmsArray.Where(p => p != null));
                FinishedDataFile(origDataFile, new List <string> {
                    taskId, "Individual Spectra Files", origDataFile
                });
                ReportProgress(new ProgressEventArgs(100, "Done!", new List <string> {
                    taskId, "Individual Spectra Files", origDataFile
                }));
            }
            ReportProgress(new ProgressEventArgs(100, "Done!", new List <string> {
                taskId, "Individual Spectra Files"
            }));

            allPsms = allPsms.OrderByDescending(b => b.Score)
                      .ThenBy(b => b.PeptideMonisotopicMass.HasValue ? Math.Abs(b.ScanPrecursorMass - b.PeptideMonisotopicMass.Value) : double.MaxValue)
                      .GroupBy(b => new Tuple <string, int, double?>(b.FullFilePath, b.ScanNumber, b.PeptideMonisotopicMass))
                      .Select(b => b.First()).ToList();

            new FdrAnalysisEngine(allPsms, tempSearchMode.NumNotches, CommonParameters, new List <string> {
                taskId
            }).Run();

            var writtenFile = Path.Combine(OutputFolder, "GPTMD_Candidates.psmtsv");

            WritePsmsToTsv(allPsms, writtenFile, new Dictionary <string, int>());
            FinishedWritingFile(writtenFile, new List <string> {
                taskId
            });

            // get file-specific precursor mass tolerances for the GPTMD engine
            var filePathToPrecursorMassTolerance = new Dictionary <string, Tolerance>();

            for (int i = 0; i < currentRawFileList.Count; i++)
            {
                string    filePath      = currentRawFileList[i];
                Tolerance fileTolerance = CommonParameters.PrecursorMassTolerance;
                if (fileSettingsList[i] != null && fileSettingsList[i].PrecursorMassTolerance != null)
                {
                    fileTolerance = fileSettingsList[i].PrecursorMassTolerance;
                }
                filePathToPrecursorMassTolerance.Add(filePath, fileTolerance);
            }

            // run GPTMD engine
            var gptmdResults = (GptmdResults) new GptmdEngine(allPsms, gptmdModifications, combos, filePathToPrecursorMassTolerance, CommonParameters, new List <string> {
                taskId
            }).Run();

            // Stop if canceled
            if (GlobalVariables.StopLoops)
            {
                return(MyTaskResults);
            }

            // write GPTMD databases
            if (dbFilenameList.Any(b => !b.IsContaminant))
            {
                List <string> databaseNames = new List <string>();
                foreach (var nonContaminantDb in dbFilenameList.Where(p => !p.IsContaminant))
                {
                    var  dbName       = Path.GetFileNameWithoutExtension(nonContaminantDb.FilePath);
                    var  theExtension = Path.GetExtension(nonContaminantDb.FilePath).ToLowerInvariant();
                    bool compressed   = theExtension.EndsWith("gz");
                    databaseNames.Add(compressed ? Path.GetFileNameWithoutExtension(dbName) : dbName);
                }
                string outputXMLdbFullName = Path.Combine(OutputFolder, string.Join("-", databaseNames) + "GPTMD.xml");

                var newModsActuallyWritten = ProteinDbWriter.WriteXmlDatabase(gptmdResults.Mods, proteinList.Where(b => !b.IsDecoy && !b.IsContaminant).ToList(), outputXMLdbFullName);

                FinishedWritingFile(outputXMLdbFullName, new List <string> {
                    taskId
                });

                MyTaskResults.NewDatabases.Add(new DbForTask(outputXMLdbFullName, false));
                MyTaskResults.AddNiceText("Modifications added: " + newModsActuallyWritten.Select(b => b.Value).Sum());
                MyTaskResults.AddNiceText("Mods types and counts:");
                MyTaskResults.AddNiceText(string.Join(Environment.NewLine, newModsActuallyWritten.OrderByDescending(b => b.Value).Select(b => "\t" + b.Key + "\t" + b.Value)));
            }
            if (dbFilenameList.Any(b => b.IsContaminant))
            {
                // do NOT use this code (Path.GetFilenameWithoutExtension) because GPTMD on .xml.gz will result in .xml.xml file type being written
                //string outputXMLdbFullNameContaminants = Path.Combine(OutputFolder, string.Join("-", dbFilenameList.Where(b => b.IsContaminant).Select(b => Path.GetFileNameWithoutExtension(b.FilePath))) + "GPTMD.xml");
                List <string> databaseNames = new List <string>();
                foreach (var contaminantDb in dbFilenameList.Where(p => p.IsContaminant))
                {
                    var dbName          = Path.GetFileName(contaminantDb.FilePath);
                    int indexOfFirstDot = dbName.IndexOf(".");
                    databaseNames.Add(dbName.Substring(0, indexOfFirstDot));
                }
                string outputXMLdbFullNameContaminants = Path.Combine(OutputFolder, string.Join("-", databaseNames) + "GPTMD.xml");

                var newModsActuallyWritten = ProteinDbWriter.WriteXmlDatabase(gptmdResults.Mods, proteinList.Where(b => !b.IsDecoy && b.IsContaminant).ToList(), outputXMLdbFullNameContaminants);

                FinishedWritingFile(outputXMLdbFullNameContaminants, new List <string> {
                    taskId
                });

                MyTaskResults.NewDatabases.Add(new DbForTask(outputXMLdbFullNameContaminants, true));
                MyTaskResults.AddNiceText("Contaminant modifications added: " + newModsActuallyWritten.Select(b => b.Value).Sum());
                MyTaskResults.AddNiceText("Mods types and counts:");
                MyTaskResults.AddNiceText(string.Join(Environment.NewLine, newModsActuallyWritten.OrderByDescending(b => b.Value).Select(b => "\t" + b.Key + "\t" + b.Value)));
            }
            return(MyTaskResults);
        }
Пример #40
0
        public void FilteringEfficiency()
        {
            var methodName = MethodBase.GetCurrentMethod().Name;

            Utils.ShowStarting(methodName);

            var sw = new System.Diagnostics.Stopwatch();

            sw.Start();
            const string rawFilePath = @"C:\cygwin\home\kims336\Data\TopDown\raw\SBEP_STM_001_02272012_Aragon.raw";

            if (!File.Exists(rawFilePath))
            {
                Assert.Ignore(@"Skipping test {0} since file not found: {1}", methodName, rawFilePath);
            }

            var run = InMemoryLcMsRun.GetLcMsRun(rawFilePath, 1.4826, 1.4826);

            sw.Stop();

            Console.WriteLine(@"Reading run: {0:f4} sec", sw.Elapsed.TotalSeconds);

            const int minPrecursorCharge = 3;
            const int maxPrecursorCharge = 30;
            const int tolerancePpm       = 10;
            var       tolerance          = new Tolerance(tolerancePpm);

            sw.Reset();
            sw.Start();
            //var ms1BasedFilter = new Ms1BasedFilter(run, minPrecursorCharge, maxPrecursorCharge, tolerancePpm);
            //
            //var ms1BasedFilter = new Ms1IsotopeTopKFilter(run, minPrecursorCharge, maxPrecursorCharge, tolerancePpm, 20);
            //var ms1BasedFilter = new ProductScorerBasedOnDeconvolutedSpectra(run,
            //    minPrecursorCharge, maxPrecursorCharge,
            //    0, 0,
            //    600.0, 1800.0, new Tolerance(tolerancePpm), null);
            //ms1BasedFilter.CachePrecursorMatchesBinCentric();
            var ms1BasedFilter = new Ms1IsotopeAndChargeCorrFilter(run, new Tolerance(10.0), minPrecursorCharge, maxPrecursorCharge, 3000, 50000, 0.5, 0.5, 0.5, 40);

            //var ms1BasedFilter = new Ms1IsotopeCorrFilter(run, minPrecursorCharge, maxPrecursorCharge, 15, 0.5, 40);

            sw.Stop();

            Console.WriteLine(@"Ms1 filter: {0:f4} sec", sw.Elapsed.TotalSeconds);

            ISequenceFilter ms1Filter = ms1BasedFilter;

            sw.Reset();
            sw.Start();
            const double minProteinMass = 3000.0;
            const double maxProteinMass = 30000.0;
            var          minBinNum      = ProductScorerBasedOnDeconvolutedSpectra.GetBinNumber(minProteinMass);
            var          maxBinNum      = ProductScorerBasedOnDeconvolutedSpectra.GetBinNumber(maxProteinMass);
            var          numComparisons = 0L;

            for (var binNum = minBinNum; binNum <= maxBinNum; binNum++)
            {
                var mass = ProductScorerBasedOnDeconvolutedSpectra.GetMz(binNum);
                numComparisons += ms1Filter.GetMatchingMs2ScanNums(mass).Count();
            }
            sw.Stop();

            Console.WriteLine(@"Calculating #matches per bin: {0:f4} sec", sw.Elapsed.TotalSeconds);

            const string resultFilePath = @"C:\cygwin\home\kims336\Data\TopDown\raw\SBEP_STM_001_02272012_Aragon_4PTMs.icresult";

            if (!File.Exists(resultFilePath))
            {
                Assert.Ignore(@"Skipping test {0} since file not found: {1}", methodName, resultFilePath);
            }

            var tsvReader    = new TsvFileParser(resultFilePath);
            var compositions = tsvReader.GetData("Composition");
            var scanNums     = tsvReader.GetData("ScanNum");
            var charges      = tsvReader.GetData("Charge");
            var scores       = tsvReader.GetData("Score");
            var qvalues      = tsvReader.GetData("QValue");
            var sequences    = tsvReader.GetData("Sequence");

            var sequenceCount = new Dictionary <string, int>();

            for (var i = 0; i < compositions.Count; i++)
            {
                if (qvalues != null)
                {
                    var qValue = Convert.ToDouble(qvalues[i]);
                    if (qValue > 0.01)
                    {
                        continue;
                    }
                }
                else
                {
                    var score = Convert.ToDouble(scores[i]);
                    if (score < 13)
                    {
                        continue;
                    }
                }
                var scanNum      = Convert.ToInt32(scanNums[i]);
                var charge       = Convert.ToInt32(charges[i]);
                var composition  = Composition.Parse(compositions[i]);
                var precursorIon = new Ion(composition, charge);
                var isValid      = run.GetSpectrum(scanNum) is ProductSpectrum spec && spec.IsolationWindow.Contains(precursorIon.GetMostAbundantIsotopeMz());
                if (!isValid)
                {
                    continue;
                }

                var sequence = sequences[i];
                if (sequenceCount.TryGetValue(sequence, out var count))
                {
                    sequenceCount[sequence] = count + 1;
                }
                else
                {
                    sequenceCount[sequence] = 1;
                }
            }
            //var sequences = tsvReader.GetData("Annotation");

            var seqSet             = new HashSet <string>();
            var allSeqSet          = new HashSet <string>();
            var numUnfilteredSpecs = 0;
            var totalSpecs         = 0;

            for (var i = 0; i < compositions.Count; i++)
            {
                if (qvalues != null)
                {
                    var qValue = Convert.ToDouble(qvalues[i]);
                    if (qValue > 0.01)
                    {
                        continue;
                    }
                }
                else
                {
                    var score = Convert.ToDouble(scores[i]);
                    if (score < 13)
                    {
                        continue;
                    }
                }
                var scanNum      = Convert.ToInt32(scanNums[i]);
                var charge       = Convert.ToInt32(charges[i]);
                var composition  = Composition.Parse(compositions[i]);
                var precursorIon = new Ion(composition, charge);
                var isValid      = run.GetSpectrum(scanNum) is ProductSpectrum spec && spec.IsolationWindow.Contains(precursorIon.GetMostAbundantIsotopeMz());
                if (!isValid)
                {
                    continue;
                }

                ++totalSpecs;

                var precursorScanNum = run.GetPrecursorScanNum(scanNum);
                var precursorSpec    = run.GetSpectrum(precursorScanNum);
                var corr1            = precursorSpec.GetCorrScore(precursorIon, tolerance, 0.1);

                var nextScanNum = run.GetNextScanNum(scanNum, 1);
                var nextSpec    = run.GetSpectrum(nextScanNum);
                var corr2       = nextSpec.GetCorrScore(precursorIon, tolerance, 0.1);

                var corr3 = ms1Filter.GetMatchingMs2ScanNums(composition.Mass).Contains(scanNum) ? 1 : 0;
                if (corr3 == 1)
                {
                    numUnfilteredSpecs++;
                    seqSet.Add(sequences[i]);
                }
                allSeqSet.Add(sequences[i]);

                //var xic = run.GetFullPrecursorIonExtractedIonChromatogram(precursorIon.GetMostAbundantIsotopeMz(), tolerance);
                ////xic.Display();
                //var apexScanNum = xic.GetNearestApexScanNum(run.GetPrecursorScanNum(scanNum), false);
                //var apexSpec = run.GetSpectrum(apexScanNum);
                //var corr3 = apexSpec.GetCorrScore(precursorIon, tolerance, 0.1);

                var corrMax = new[] { corr1, corr2, corr3 }.Max();

                Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}", scanNum, precursorScanNum, corr1, nextScanNum, corr2, corr3, corrMax, sequenceCount[sequences[i]]);
            }

            Console.WriteLine("TotalNumComparisons: {0}", numComparisons);
            Console.WriteLine("AverageNumComparisons: {0:f2}", numComparisons / (double)(maxBinNum - minBinNum + 1));
            Console.WriteLine("SuccessRate: {0:f2} {1} / {2}", numUnfilteredSpecs / (double)totalSpecs, numUnfilteredSpecs, totalSpecs);
            Console.WriteLine("NumUniqueSequences: {0:f2}, {1} / {2}", seqSet.Count / (double)allSeqSet.Count, seqSet.Count, allSeqSet.Count);

            Console.WriteLine(@"Elapsed Time: {0:f4} sec", sw.Elapsed.TotalSeconds);
        }
Пример #41
0
        public static void MatchIons(IMsDataScan <IMzSpectrum <IMzPeak> > thisScan, Tolerance productMassTolerance, double[] sortedTheoreticalProductMassesForThisPeptide, List <double> matchedIonMassesList, List <double> productMassErrorDa, List <double> productMassErrorPpm, double precursorMass, List <DissociationType> dissociationTypes, bool addCompIons)
        {
            var TotalProductsHere = sortedTheoreticalProductMassesForThisPeptide.Length;

            if (TotalProductsHere == 0)
            {
                return;
            }

            int    currentTheoreticalIndex = -1;
            double currentTheoreticalMass;

            do
            {
                currentTheoreticalIndex++;
                currentTheoreticalMass = sortedTheoreticalProductMassesForThisPeptide[currentTheoreticalIndex];
            } while (double.IsNaN(currentTheoreticalMass) && currentTheoreticalIndex < sortedTheoreticalProductMassesForThisPeptide.Length - 1);

            if (double.IsNaN(currentTheoreticalMass))
            {
                return;
            }

            double currentTheoreticalMz = currentTheoreticalMass + Constants.protonMass;
            int    testTheoreticalIndex;
            double testTheoreticalMass;
            double testTheoreticalMz;

            // speed optimizations
            double[] experimental_mzs         = thisScan.MassSpectrum.XArray;
            double[] experimental_intensities = thisScan.MassSpectrum.YArray;
            int      numExperimentalPeaks     = experimental_mzs.Length;

            // Loop over all experimental indices
            for (int experimentalIndex = 0; experimentalIndex < numExperimentalPeaks; experimentalIndex++)
            {
                double currentExperimentalMz = experimental_mzs[experimentalIndex];
                // If found match
                if (productMassTolerance.Within(currentExperimentalMz, currentTheoreticalMz))
                {
                    matchedIonMassesList.Add(currentTheoreticalMass);
                    double currentExperimentalMass = currentExperimentalMz - Constants.protonMass;
                    productMassErrorDa.Add(currentExperimentalMass - currentTheoreticalMass);
                    productMassErrorPpm.Add((currentExperimentalMass - currentTheoreticalMass) * 1000000 / currentTheoreticalMass);

                    currentTheoreticalIndex++;
                    if (currentTheoreticalIndex == TotalProductsHere)
                    {
                        break;
                    }
                    currentTheoreticalMass = sortedTheoreticalProductMassesForThisPeptide[currentTheoreticalIndex];
                    currentTheoreticalMz   = currentTheoreticalMass + Constants.protonMass;
                }
                // Else if for sure did not reach the next theoretical yet
                else if (currentExperimentalMz > currentTheoreticalMz)
                {
                    // Move on to next index and never come back!
                    currentTheoreticalIndex++;
                    if (currentTheoreticalIndex == TotalProductsHere)
                    {
                        break;
                    }
                    currentTheoreticalMass = sortedTheoreticalProductMassesForThisPeptide[currentTheoreticalIndex];
                    currentTheoreticalMz   = currentTheoreticalMass + Constants.protonMass;

                    // Start with the current ones
                    testTheoreticalIndex = currentTheoreticalIndex;
                    testTheoreticalMass  = currentTheoreticalMass;
                    testTheoreticalMz    = currentTheoreticalMz;
                    // Mark the skipped theoreticals as not found. The last one is not for sure, might be flipped!
                    while (currentExperimentalMz > testTheoreticalMz)
                    {
                        // Store old info for possible reuse
                        currentTheoreticalMz    = testTheoreticalMz;
                        currentTheoreticalMass  = testTheoreticalMass;
                        currentTheoreticalIndex = testTheoreticalIndex;

                        // Update test stuff!
                        testTheoreticalIndex++;
                        if (testTheoreticalIndex == TotalProductsHere)
                        {
                            break;
                        }
                        testTheoreticalMass = sortedTheoreticalProductMassesForThisPeptide[testTheoreticalIndex];
                        testTheoreticalMz   = testTheoreticalMass + Constants.protonMass;
                    }
                    experimentalIndex--;
                }
            }
            if (addCompIons)
            {
                double[] complementaryMasses      = new double[numExperimentalPeaks];
                double[] complementaryIntensities = new double[numExperimentalPeaks];

                foreach (DissociationType dissociationType in dissociationTypes)
                {
                    if (complementaryIonConversionDictionary.TryGetValue(dissociationType, out double protonMassShift))
                    {
                        currentTheoreticalIndex = -1;
                        do
                        {
                            currentTheoreticalIndex++;
                            currentTheoreticalMass = sortedTheoreticalProductMassesForThisPeptide[currentTheoreticalIndex];
                        } while (double.IsNaN(currentTheoreticalMass) && currentTheoreticalIndex < sortedTheoreticalProductMassesForThisPeptide.Length - 1);

                        double massShiftForComplementaryConversion = precursorMass + protonMassShift; //mass shift needed to reobtain the original product ion for calculating tolerance
                        for (int i = numExperimentalPeaks - 1; i >= 0; i--)
                        {
                            complementaryMasses[numExperimentalPeaks - i - 1]      = massShiftForComplementaryConversion - experimental_mzs[i];
                            complementaryIntensities[numExperimentalPeaks - i - 1] = experimental_intensities[i];
                        }

                        // Loop over all experimental indices
                        for (int experimentalIndex = 0; experimentalIndex < numExperimentalPeaks; experimentalIndex++)
                        {
                            double currentExperimentalMass  = complementaryMasses[experimentalIndex];
                            double originalExperimentalMass = massShiftForComplementaryConversion - currentExperimentalMass;
                            double minBoundary = currentExperimentalMass - originalExperimentalMass + productMassTolerance.GetMinimumValue(originalExperimentalMass);
                            double maxBoundary = currentExperimentalMass - originalExperimentalMass + productMassTolerance.GetMaximumValue(originalExperimentalMass);
                            // If found match
                            if (minBoundary < currentTheoreticalMass && maxBoundary > currentTheoreticalMass)
                            {
                                matchedIonMassesList.Add(currentTheoreticalMass);
                                productMassErrorDa.Add(currentExperimentalMass - currentTheoreticalMass);
                                productMassErrorPpm.Add((currentExperimentalMass - currentTheoreticalMass) * 1000000 / currentTheoreticalMass);

                                currentTheoreticalIndex++;
                                if (currentTheoreticalIndex == TotalProductsHere)
                                {
                                    break;
                                }
                                currentTheoreticalMass = sortedTheoreticalProductMassesForThisPeptide[currentTheoreticalIndex];
                            }
                            // Else if for sure passed a theoretical
                            else if (currentExperimentalMass > currentTheoreticalMass)
                            {
                                // Move on to next index and never come back!
                                currentTheoreticalIndex++;
                                if (currentTheoreticalIndex == TotalProductsHere)
                                {
                                    break;
                                }
                                currentTheoreticalMass = sortedTheoreticalProductMassesForThisPeptide[currentTheoreticalIndex];

                                // Start with the current ones
                                testTheoreticalIndex = currentTheoreticalIndex;
                                testTheoreticalMass  = currentTheoreticalMass;
                                // Mark the skipped theoreticals as not found. The last one is not for sure, might be flipped!
                                while (currentExperimentalMass > testTheoreticalMass)
                                {
                                    // Store old info for possible reuse
                                    currentTheoreticalMass  = testTheoreticalMass;
                                    currentTheoreticalIndex = testTheoreticalIndex;

                                    // Update test stuff!
                                    testTheoreticalIndex++;
                                    if (testTheoreticalIndex == TotalProductsHere)
                                    {
                                        break;
                                    }
                                    testTheoreticalMass = sortedTheoreticalProductMassesForThisPeptide[testTheoreticalIndex];
                                }
                                experimentalIndex--;
                            }
                        }
                    }
                    else
                    {
                        throw new NotImplementedException();
                    }
                }
            }
        }
Пример #42
0
        public void TestMs1Filtering()
        {
            var methodName = MethodBase.GetCurrentMethod().Name;

            Utils.ShowStarting(methodName);

            const string resultFilePath =
                //    @"C:\cygwin\home\kims336\Data\TopDown\raw\CorrMatches_N30\SBEP_STM_001_02272012_Aragon.tsv";
                @"C:\cygwin\home\kims336\Data\TopDown\raw\CorrMatches_N30\SBEP_STM_001_02272012_Aragon.decoy.icresult";

            if (!File.Exists(resultFilePath))
            {
                Assert.Ignore(@"Skipping test {0} since file not found: {1}", methodName, resultFilePath);
            }

            const string rawFilePath = @"C:\cygwin\home\kims336\Data\TopDown\raw\DataFiles\SBEP_STM_001_02272012_Aragon.raw";

            if (!File.Exists(rawFilePath))
            {
                Assert.Ignore(@"Skipping test {0} since file not found: {1}", methodName, rawFilePath);
            }

            var run = InMemoryLcMsRun.GetLcMsRun(rawFilePath, 1.4826, 1.4826);

            //const int minPrecursorCharge = 3;
            //const int maxPrecursorCharge = 30;
            //const int tolerancePpm = 15;
            var tolerance = new Tolerance(15);

            //var ms1BasedFilter = new Ms1IsotopeCorrFilter(run, minPrecursorCharge, maxPrecursorCharge, tolerancePpm, 0.7, 40);
            ////var ms1BasedFilter = new Ms1IsotopeTopKFilter(run, minPrecursorCharge, maxPrecursorCharge, tolerancePpm, 20);
            //ISequenceFilter ms1Filter = ms1BasedFilter;

            var tsvReader    = new TsvFileParser(resultFilePath);
            var compositions = tsvReader.GetData("Composition");
            var scanNums     = tsvReader.GetData("ScanNum");
            var charges      = tsvReader.GetData("Charge");
            var qValues      = tsvReader.GetData("QValue");
            var scores       = tsvReader.GetData("Score");

            //var sequences = tsvReader.GetData("Annotation");

            //var hist = new int[11];

            Console.WriteLine("ScanNum\tScore\tPrecursor\tNext\tSum\tNextIsotope\tLessCharge\tMoreCharge\tMax\tNumXicPeaks");
            for (var i = 0; i < compositions.Count; i++)
            {
                if (qValues != null)
                {
                    var qValue = Convert.ToDouble(qValues[i]);
                    if (qValue > 0.01)
                    {
                        continue;
                    }
                }

                var scanNum     = Convert.ToInt32(scanNums[i]);
                var composition = Composition.Parse(compositions[i]);
                var charge      = Convert.ToInt32(charges[i]);

                var precursorIon = new Ion(composition, charge);
                var isValid      = run.GetSpectrum(scanNum) is ProductSpectrum spec && spec.IsolationWindow.Contains(precursorIon.GetMostAbundantIsotopeMz());
                if (!isValid)
                {
                    continue;
                }

                var score = Convert.ToDouble(scores[i]);

                var precursorScanNum = run.GetPrecursorScanNum(scanNum);
                var precursorSpec    = run.GetSpectrum(precursorScanNum);
                var preIsotopeCorr   = precursorSpec.GetCorrScore(precursorIon, tolerance, 0.1);

                var nextScanNum     = run.GetNextScanNum(scanNum, 1);
                var nextSpec        = run.GetSpectrum(nextScanNum);
                var nextIsotopeCorr = nextSpec.GetCorrScore(precursorIon, tolerance, 0.1);

                var xicMostAbundant = run.GetPrecursorExtractedIonChromatogram(precursorIon.GetMostAbundantIsotopeMz(), tolerance, scanNum);

                var apexScanNum = xicMostAbundant.GetApexScanNum();
                if (apexScanNum < run.MinLcScan)
                {
                    apexScanNum = scanNum;
                }
                //var sumSpec = run.GetSummedMs1Spectrum(apexScanNum);
                //                var apexIsotopeCorr = sumSpec.GetCorrScore(precursorIon, tolerance, 0.1);
                //                var corr3 = ms1Filter.GetMatchingMs2ScanNums(composition.Mass).Contains(scanNum) ? 1 : 0;

                var xicNextIsotope = run.GetPrecursorExtractedIonChromatogram(precursorIon.GetMostAbundantIsotopeMz() + Constants.C13MinusC12 / charge, tolerance, scanNum);

                var plusOneIsotopeCorr = xicMostAbundant.GetCorrelation(xicNextIsotope);

                var precursorIonChargeMinusOne = new Ion(composition, charge - 1);
                var xicChargeMinusOne          = run.GetPrecursorExtractedIonChromatogram(precursorIonChargeMinusOne.GetMostAbundantIsotopeMz(), tolerance, scanNum);
                var chargeMinusOneCorr         = xicMostAbundant.GetCorrelation(xicChargeMinusOne);

                var precursorIonChargePlusOne = new Ion(composition, charge + 1);
                var xicChargePlusOne          = run.GetPrecursorExtractedIonChromatogram(precursorIonChargePlusOne.GetMostAbundantIsotopeMz(), tolerance, scanNum);
                var chargePlusOneCorr         = xicMostAbundant.GetCorrelation(xicChargePlusOne);

                //var max = new[] {preIsotopeCorr, nextIsotopeCorr, apexIsotopeCorr, plusOneIsotopeCorr, chargeMinusOneCorr, chargePlusOneCorr}.Max();
                //Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}\t{8}\t{9}",
                //    scanNum, score, preIsotopeCorr, nextIsotopeCorr, apexIsotopeCorr, plusOneIsotopeCorr, chargeMinusOneCorr, chargePlusOneCorr, max, xicMostAbundant.Count);
            }

            //Console.WriteLine("Histogram");
            //for (var i = 0; i < hist.Length; i++)
            //{
            //    Console.WriteLine("{0:f1}\t{1}", i / 10.0, hist[i]);
            //}
        }
 /// <summary>
 /// Gets a value indicating whether the specified point is on the segment defined by two points.
 /// </summary>
 /// <param name="pt">The instance to which the method applies.</param>
 /// <param name="p1">The segment start point.</param>
 /// <param name="p2">The segment end point.</param>
 /// <param name="tol">The tolerance used in comparisons.</param>
 /// <returns>true if the point is on the segment; otherwise, false.</returns>
 public static bool IsBetween(this Point2d pt, Point2d p1, Point2d p2, Tolerance tol)
 {
     return(p1.GetVectorTo(pt).GetNormal(tol).Equals(pt.GetVectorTo(p2).GetNormal(tol)));
 }
Пример #44
0
        //Calculate score based on All possible Products Masses for inter- or intra- crosslinks and deadend.
        public static void XlLocalization(Ms2ScanWithSpecificMass theScan, PsmCross psmCross, double modMass,
                                          CrosslinkerTypeClass crosslinker, List <ProductType> lp, Tolerance fragmentTolerance, bool Charge_2_3, bool Charge_2_3_PrimeFragment, List <int> linkPos)
        {
            var pmmhList = PsmCross.XlCalculateTotalProductMasses(psmCross, modMass, crosslinker, lp, Charge_2_3, Charge_2_3_PrimeFragment, linkPos);

            List <double>         scoreList = new List <double>();
            List <MatchedIonInfo> miil      = new List <MatchedIonInfo>();

            foreach (var pmm in pmmhList)
            {
                var    matchedIonMassesListPositiveIsMatch = new MatchedIonInfo(pmm.ProductMz.Length);
                double pmmScore = PsmCross.XlMatchIons(theScan.TheScan, fragmentTolerance, pmm.ProductMz, pmm.ProductName, matchedIonMassesListPositiveIsMatch);
                miil.Add(matchedIonMassesListPositiveIsMatch);
                scoreList.Add(pmmScore);
            }

            psmCross.XLBestScore    = scoreList.Max();
            psmCross.MatchedIonInfo = miil[scoreList.IndexOf(scoreList.Max())];
            psmCross.XlPos          = pmmhList[scoreList.IndexOf(scoreList.Max())].XlPos + 1;
            if (crosslinker.Cleavable)
            {
                psmCross.ParentIonMaxIntensityRanks = new List <int>();
                if (psmCross.MatchedIonInfo.MatchedIonName.Any(p => p != null && p.Contains("PepS")))
                {
                    psmCross.ParentIonExist    += "PepS";
                    psmCross.ParentIonExistNum += 1;
                }
                if (psmCross.MatchedIonInfo.MatchedIonName.Any(p => p != null && p.Contains("PepL")))
                {
                    psmCross.ParentIonExist    += "PepL";
                    psmCross.ParentIonExistNum += 1;
                }
                //if (psmCross.MatchedIonInfo.MatchedIonName.Any(p => p != null && p.Equals("PepS2")))
                //{
                //    psmCross.ParentIonExist += "PepS2";
                //    psmCross.ParentIonExistNum += 1;
                //}
                //if (psmCross.MatchedIonInfo.MatchedIonName.Any(p => p != null && p.Equals("PepL2")))
                //{
                //    psmCross.ParentIonExist += "PepL2";
                //    psmCross.ParentIonExistNum += 1;
                //}
                for (int i = 0; i < psmCross.MatchedIonInfo.MatchedIonName.Length; i++)
                {
                    if (psmCross.MatchedIonInfo.MatchedIonName[i] != null)
                    {
                        if (psmCross.MatchedIonInfo.MatchedIonName[i].Contains("Pep"))
                        {
                            psmCross.ParentIonMaxIntensityRanks.Add(psmCross.MatchedIonInfo.MatchedIonIntensityRank[i]);
                        }
                    }
                }
            }
            if (Charge_2_3 || Charge_2_3_PrimeFragment)
            {
                int Charge2IonExist = 0;
                for (int i = 0; i < psmCross.MatchedIonInfo.MatchedIonName.Length; i++)
                {
                    if (psmCross.MatchedIonInfo.MatchedIonName[i] != null && (psmCross.MatchedIonInfo.MatchedIonName[i].Contains("t2") || psmCross.MatchedIonInfo.MatchedIonName[i].Contains("t3")))
                    {
                        Charge2IonExist++;
                    }
                }
                psmCross.Charge2IonExist = Charge2IonExist;
            }
        }
Пример #45
0
        public void TestFeatureAlignment()
        {
            const string outFilePath = @"\\protoapps\UserData\Jungkap\Lewy\aligned\promex_crosstab_temp.tsv";

            //CPTAC_Intact_CR32A_24Aug15_Bane_15-02-06-RZ
            var prsmReader = new ProteinSpectrumMatchReader();
            var tolerance  = new Tolerance(10);
            var alignment  = new LcMsFeatureAlignment(new AnalysisCompRef.CompRefFeatureComparer(tolerance));

            for (var i = 0; i < DATASET_COUNT; i++)
            {
                var rawFile   = string.Format(@"{0}\{1}.pbf", PbfPath, GetDataSetNames(i));
                var mspFile   = string.Format(@"{0}\{1}_IcTda.tsv", MsPfFolder, GetDataSetNames(i));
                var mspFile2  = string.Format(@"{0}\{1}_IcTda.tsv", MsPfFolder2, GetDataSetNames(i));
                var ms1FtFile = string.Format(@"{0}\{1}.ms1ft", Ms1FtFolder, GetDataSetNames(i));

                if (!File.Exists(rawFile))
                {
                    Console.WriteLine("Skipping test since file not found: " + rawFile);
                    continue;
                }

                if (!File.Exists(ms1FtFile))
                {
                    Console.WriteLine("Skipping test since file not found: " + ms1FtFile);
                    continue;
                }

                if (!File.Exists(mspFile))
                {
                    Console.WriteLine("Skipping test since file not found: " + mspFile);
                    continue;
                }

                if (!File.Exists(mspFile2))
                {
                    Console.WriteLine("Skipping test since file not found: " + mspFile2);
                    continue;
                }

                Console.WriteLine(rawFile);

                var run       = PbfLcMsRun.GetLcMsRun(rawFile);
                var prsmList1 = prsmReader.LoadIdentificationResult(mspFile, ProteinSpectrumMatch.SearchTool.MsPathFinder);
                var prsmList2 = prsmReader.LoadIdentificationResult(mspFile2, ProteinSpectrumMatch.SearchTool.MsPathFinder);
                prsmList1.AddRange(prsmList2);

                var prsmList = MergePrsm(prsmList1);
                var features = LcMsFeatureAlignment.LoadProMexResult(i, ms1FtFile, run);

                foreach (var match in prsmList)
                {
                    match.ProteinId = match.ProteinName;
                }

                // tag features by PrSMs
                foreach (var feature in features)
                {
                    //features[j].ProteinSpectrumMatches = new ProteinSpectrumMatchSet(i);
                    var massTol = tolerance.GetToleranceAsMz(feature.Mass);
                    foreach (var match in prsmList)
                    {
                        if (feature.MinScanNum < match.ScanNum && match.ScanNum < feature.MaxScanNum && Math.Abs(feature.Mass - match.Mass) < massTol)
                        {
                            feature.ProteinSpectrumMatches.Add(match);
                        }
                    }
                }

                alignment.AddDataSet(i, features, run);
            }

            if (alignment.CountDatasets == 0)
            {
                Assert.Ignore("Skipping test since input data files were found");
            }

            alignment.AlignFeatures();

            Console.WriteLine("{0} alignments ", alignment.CountAlignedFeatures);

            for (var i = 0; i < DATASET_COUNT; i++)
            {
                alignment.FillMissingFeatures(i);
                Console.WriteLine("{0} has been processed", GetDataSetNames(i));
            }

            OutputCrossTabWithId(outFilePath, alignment);
        }
Пример #46
0
        //Calculate score based on All possible Products Masses for inter- or intra- crosslinks and deadend.
        public static void XlLocalizationForLoopCrosslink(Ms2ScanWithSpecificMass theScan, PsmCross psmCross, double modMass, CrosslinkerTypeClass crosslinker, List <ProductType> lp, Tolerance fragmentTolerance, List <int> linkPos)
        {
            var pmmhList = PsmCross.XlCalculateTotalProductMassesForLoopCrosslink(psmCross, modMass, crosslinker, lp, linkPos);

            List <double>         scoreList = new List <double>();
            List <MatchedIonInfo> miil      = new List <MatchedIonInfo>();

            foreach (var pmm in pmmhList)
            {
                var    matchedIonMassesListPositiveIsMatch = new MatchedIonInfo(pmm.ProductMz.Length);
                double pmmScore = PsmCross.XlMatchIons(theScan.TheScan, fragmentTolerance, pmm.ProductMz, pmm.ProductName, matchedIonMassesListPositiveIsMatch);
                miil.Add(matchedIonMassesListPositiveIsMatch);
                scoreList.Add(pmmScore);
            }

            psmCross.XLBestScore    = scoreList.Max();
            psmCross.MatchedIonInfo = miil[scoreList.IndexOf(scoreList.Max())];
            psmCross.XlPos          = pmmhList[scoreList.IndexOf(scoreList.Max())].XlPos + 1;
            psmCross.XlPos2         = pmmhList[scoreList.IndexOf(scoreList.Max())].XlPos2 + 1;
        }
Пример #47
0
 public void SetUp()
 {
     tenPercent    = new Tolerance(10.0).Percent;
     zeroTolerance = new Tolerance(0);
 }
Пример #48
0
        //Calculate score based on Product Masses.
        public static double XlMatchIons(IMsDataScan <IMzSpectrum <IMzPeak> > thisScan, Tolerance productMassTolerance, double[] sorted_theoretical_product_masses_for_this_peptide, string[] sorted_theoretical_product_name_for_this_peptide, MatchedIonInfo matchedIonMassesListPositiveIsMatch)
        {
            var TotalProductsHere = sorted_theoretical_product_masses_for_this_peptide.Length;

            if (TotalProductsHere == 0)
            {
                return(0);
            }
            int    MatchingProductsHere  = 0;
            double MatchingIntensityHere = 0;

            // speed optimizations
            double[] experimental_mzs              = thisScan.MassSpectrum.XArray;
            double[] experimental_intensities      = thisScan.MassSpectrum.YArray;
            int[]    experimental_intensities_rank = GenerateIntensityRanks(experimental_mzs, experimental_intensities);
            int      num_experimental_peaks        = experimental_mzs.Length;

            int    currentTheoreticalIndex = -1;
            double currentTheoreticalMass;

            do
            {
                currentTheoreticalIndex++;
                currentTheoreticalMass = sorted_theoretical_product_masses_for_this_peptide[currentTheoreticalIndex];
            } while (double.IsNaN(currentTheoreticalMass) && currentTheoreticalIndex < sorted_theoretical_product_masses_for_this_peptide.Length - 1);

            if (double.IsNaN(currentTheoreticalMass))
            {
                return(0);
            }

            double currentTheoreticalMz = currentTheoreticalMass + Constants.protonMass;

            int    testTheoreticalIndex;
            double testTheoreticalMZ;
            double testTheoreticalMass;

            // Loop over all experimenal indices
            for (int experimentalIndex = 0; experimentalIndex < num_experimental_peaks; experimentalIndex++)
            {
                double currentExperimentalMZ = experimental_mzs[experimentalIndex];
                // If found match
                if (productMassTolerance.Within(currentExperimentalMZ, currentTheoreticalMz))
                {
                    MatchingProductsHere++;
                    MatchingIntensityHere += experimental_intensities[experimentalIndex];
                    matchedIonMassesListPositiveIsMatch.MatchedIonMz[currentTheoreticalIndex]            = currentTheoreticalMass;
                    matchedIonMassesListPositiveIsMatch.MatchedIonIntensity[currentTheoreticalIndex]     = experimental_intensities[experimentalIndex];
                    matchedIonMassesListPositiveIsMatch.MatchedIonName[currentTheoreticalIndex]          = sorted_theoretical_product_name_for_this_peptide[currentTheoreticalIndex];
                    matchedIonMassesListPositiveIsMatch.MatchedIonIntensityRank[currentTheoreticalIndex] = experimental_intensities_rank[experimentalIndex];
                    currentTheoreticalIndex++;
                    if (currentTheoreticalIndex == TotalProductsHere)
                    {
                        break;
                    }
                    currentTheoreticalMass = sorted_theoretical_product_masses_for_this_peptide[currentTheoreticalIndex];
                    currentTheoreticalMz   = currentTheoreticalMass + Constants.protonMass;
                }
                // Else if for sure did not reach the next theoretical yet, move to next experimental
                else if (currentExperimentalMZ < currentTheoreticalMz)
                {
                    continue;
                }
                // Else if for sure passed a theoretical
                else
                {
                    // Mark the theoretical as missed
                    matchedIonMassesListPositiveIsMatch.MatchedIonMz[currentTheoreticalIndex] = -currentTheoreticalMass;

                    // Move on to next index and never come back!
                    currentTheoreticalIndex++;
                    if (currentTheoreticalIndex == TotalProductsHere)
                    {
                        break;
                    }
                    currentTheoreticalMass = sorted_theoretical_product_masses_for_this_peptide[currentTheoreticalIndex];
                    currentTheoreticalMz   = currentTheoreticalMass + Constants.protonMass;

                    // Start with the current ones
                    testTheoreticalIndex = currentTheoreticalIndex;
                    testTheoreticalMZ    = currentTheoreticalMz;
                    testTheoreticalMass  = currentTheoreticalMass;
                    // Mark the skipped theoreticals as not found. The last one is not for sure, might be flipped!
                    while (currentExperimentalMZ > testTheoreticalMZ)
                    {
                        matchedIonMassesListPositiveIsMatch.MatchedIonMz[testTheoreticalIndex] = -currentTheoreticalMass;
                        // Store old info for possible reuse
                        currentTheoreticalMass  = testTheoreticalMass;
                        currentTheoreticalMz    = testTheoreticalMZ;
                        currentTheoreticalIndex = testTheoreticalIndex;

                        // Update test stuff!
                        testTheoreticalIndex++;
                        if (testTheoreticalIndex == TotalProductsHere)
                        {
                            break;
                        }
                        testTheoreticalMass = sorted_theoretical_product_masses_for_this_peptide[testTheoreticalIndex];
                        testTheoreticalMZ   = testTheoreticalMass + Constants.protonMass;
                    }

                    experimentalIndex--;
                }
            }
            return(MatchingProductsHere + MatchingIntensityHere / thisScan.TotalIonCurrent);
        }
Пример #49
0
 public abstract PeptideSpectralMatch Search(IMassSpectrum massSpectrum, Peptide peptide, FragmentTypes fragmentTypes, Tolerance productMassTolerance);
Пример #50
0
 public LcMsFeatureMergeComparer(Tolerance tolerance)
 {
     _tolerance = tolerance;
 }
Пример #51
0
 public void SetUp()
 {
     tolerance = Tolerance.Empty;
     comparer = new NUnitComparer();
 }
Пример #52
0
        public void TestPearsonCorrelation(int precursor, string commonName, string composition, Tolerance tolerance)
        {
            var rawFilePath = @"\\proto-2\UnitTest_Files\Liquid\PearsonCorrelationTests\OMICS_IM102_691_1d_Lipid_pooled_POS_150mm_17Apr15_Polaroid_14-12-16.raw";

            var lcmsRun = PbfLcMsRun.GetLcMsRun(rawFilePath);

            var spectrum = lcmsRun.GetSpectrum(precursor);

            var parsedComposition     = Composition.ParseFromPlainString(composition);
            var correlationCalculator = new PearsonCorrelationFitUtil();
            //var target = new LipidTarget(commonName, LipidClass.DG, FragmentationMode.Positive, parsedComposition, new List<AcylChain>(), Adduct.Hydrogen);
            var spectrumSearchResult = new SpectrumSearchResult(null, null, spectrum, null, null, new Xic(), lcmsRun)
            {
                PrecursorTolerance = tolerance
            };
            var correlation = correlationCalculator.GetFitScore(spectrumSearchResult, parsedComposition);

            Console.WriteLine("The Pearson correlation is: " + correlation);
        }
Пример #53
0
        public void MassToleranceImplicitPlusMinus4()
        {
            var tol = new Tolerance("± 10 ppm"); // alt-code 241

            Assert.AreEqual(tol.Type, ToleranceType.PlusAndMinus);
        }
Пример #54
0
        public void TestPearsonCorrelationWholeFile(string directoryPath)
        {
            var dirFiles = Directory.GetFiles(directoryPath);

            var correlationCalculator = new PearsonCorrelationFitUtil();

            foreach (var pathToResults in dirFiles.Where(path => path.EndsWith(".txt")))
            {
                var datasetName = Path.GetFileNameWithoutExtension(pathToResults);
                var pathToRaw   = GetRawFilePath(directoryPath, datasetName);
                if (string.IsNullOrEmpty(pathToRaw))
                {
                    continue;
                }

                var lcmsRun        = PbfLcMsRun.GetLcMsRun(pathToRaw);
                var tolerance      = new Tolerance(30, ToleranceUnit.Ppm);
                var rawFileName    = Path.GetFileName(pathToRaw);
                var datasetDirPath = Path.GetDirectoryName(pathToResults);
                var outputFileName = string.Format("{0}_training.tsv", datasetName);
                var outputPath     = Path.Combine(datasetDirPath, outputFileName);
                using (var writer = new StreamWriter(outputPath))
                    using (var reader = new StreamReader(new FileStream(pathToResults, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)))
                    {
                        var lineCount     = 0;
                        var headerToIndex = new Dictionary <string, int>();
                        while (!reader.EndOfStream)
                        {
                            var line = reader.ReadLine();
                            if (string.IsNullOrWhiteSpace(line))
                            {
                                continue;
                            }

                            var pieces = line.Split('\t').ToArray();

                            if (lineCount++ == 0)
                            { // First line
                                writer.Write("Raw File\t");
                                for (var i = 0; i < pieces.Length; i++)
                                {
                                    headerToIndex.Add(pieces[i], i);
                                    writer.Write("{0}\t", pieces[i]);
                                }

                                writer.WriteLine("Fit Score\tFit M-1 Score");

                                continue;
                            }

                            var precursor  = Convert.ToInt32(pieces[headerToIndex["Precursor Scan"]]);
                            var commonName = pieces[headerToIndex["Common Name"]];
                            var adduct     = pieces[headerToIndex["Adduct"]];
                            var spectrum   = lcmsRun.GetSpectrum(precursor);
                            if (spectrum == null)
                            {
                                Console.WriteLine("Invalid scan number: {0}", precursor);
                                continue;
                            }

                            var lipid = new Lipid {
                                AdductFull = adduct, CommonName = commonName
                            };
                            var lipidTarget          = lipid.CreateLipidTarget();
                            var spectrumSearchResult = new SpectrumSearchResult(null, null, spectrum, null, null, new Xic(), lcmsRun)
                            {
                                PrecursorTolerance = tolerance
                            };
                            var fitScore       = correlationCalculator.GetFitScore(spectrumSearchResult, lipidTarget.Composition);
                            var fitMinus1Score = correlationCalculator.GetFitMinus1Score(spectrumSearchResult, lipidTarget.Composition);

                            writer.Write(rawFileName + "\t");
                            writer.Write(line);

                            writer.WriteLine("{0}\t{1}", fitScore, fitMinus1Score);
                        }
                    }
            }
        }
Пример #55
0
        public void MassToleranceImplicitType()
        {
            var tol = new Tolerance("10 ppm");

            Assert.AreEqual(ToleranceUnit.PPM, tol.Unit);
        }
Пример #56
0
        public void PearsonCorrelationFileCombiner(string directoryPath)
        {
            var dirFiles = Directory.GetFiles(directoryPath);

            var correlationCalculator = new PearsonCorrelationFitUtil();
            var cosineCalculator      = new CosineFitUtil();

            // Each dictionary corresponds to a dataset, each dictionary key corresponds to the TSV header.
            var results = new List <Dictionary <string, List <string> > >();
            var headers = new HashSet <string>();

            foreach (var pathToResults in dirFiles.Where(path => path.EndsWith(".txt")))
            {
                var datasetName = Path.GetFileNameWithoutExtension(pathToResults);
                var pathToRaw   = GetRawFilePath(directoryPath, datasetName);
                var rawName     = Path.GetFileName(pathToRaw);
                if (string.IsNullOrEmpty(pathToRaw))
                {
                    continue;
                }

                var lcmsRun   = PbfLcMsRun.GetLcMsRun(pathToRaw);
                var tolerance = new Tolerance(30, ToleranceUnit.Ppm);
                using (var reader = new StreamReader(new FileStream(pathToResults, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)))
                {
                    results.Add(new Dictionary <string, List <string> >()); // Add dictionary for new dataset.
                    var datasetResults = results.Last();                    // Results for the current dataset.
                    var lineCount      = 0;
                    var headerToIndex  = new Dictionary <string, int>();
                    while (!reader.EndOfStream)
                    {
                        var line = reader.ReadLine();
                        if (string.IsNullOrWhiteSpace(line))
                        {
                            continue;
                        }

                        var pieces = line.Split('\t').ToArray();

                        if (lineCount++ == 0)
                        {   // First line
                            for (var i = 0; i < pieces.Length; i++)
                            {
                                var header = pieces[i];
                                headerToIndex.Add(header, i);
                                datasetResults.Add(header, new List <string>());
                            }

                            datasetResults.Add("Raw File", new List <string>());
                            datasetResults.Add("Pearson Corr Score", new List <string>());
                            datasetResults.Add("Pearson Corr M-1 Score", new List <string>());
                            datasetResults.Add("Cosine Score", new List <string>());
                            datasetResults.Add("Cosine M-1 Score", new List <string>());
                            headers.UnionWith(datasetResults.Keys);
                            continue;
                        }

                        var precursor  = Convert.ToInt32(pieces[headerToIndex["Precursor Scan"]]);
                        var commonName = pieces[headerToIndex["Common Name"]];
                        var adduct     = pieces[headerToIndex["Adduct"]];
                        var spectrum   = lcmsRun.GetSpectrum(precursor);
                        if (spectrum == null)
                        {
                            Console.WriteLine("Invalid scan number: {0}", precursor);
                            continue;
                        }

                        var lipid = new Lipid {
                            AdductFull = adduct, CommonName = commonName
                        };
                        var lipidTarget          = lipid.CreateLipidTarget();
                        var spectrumSearchResult = new SpectrumSearchResult(null, null, spectrum, null, null, new Xic(), lcmsRun)
                        {
                            PrecursorTolerance = tolerance
                        };
                        var pearsonCorrScore       = correlationCalculator.GetFitScore(spectrumSearchResult, lipidTarget.Composition);
                        var pearsonCorrMinus1Score = correlationCalculator.GetFitMinus1Score(spectrumSearchResult, lipidTarget.Composition);
                        var cosineScore            = cosineCalculator.GetFitScore(spectrumSearchResult, lipidTarget.Composition);
                        var cosineMinus1Score      = cosineCalculator.GetFitScore(
                            spectrumSearchResult,
                            lipidTarget.Composition);

                        // Add results to results dictionary.
                        datasetResults["Raw File"].Add(rawName);
                        foreach (var header in headerToIndex.Keys)
                        {
                            datasetResults[header].Add(pieces[headerToIndex[header]]);
                        }

                        datasetResults["Pearson Corr Score"].Add(pearsonCorrScore.ToString());
                        datasetResults["Pearson Corr M-1 Score"].Add(pearsonCorrMinus1Score.ToString());
                        datasetResults["Cosine Score"].Add(cosineScore.ToString());
                        datasetResults["Cosine M-1 Score"].Add(cosineMinus1Score.ToString());
                    }
                }
            }

            // Write results
            var outputFilePath = Path.Combine(directoryPath, "training.tsv");

            using (var writer = new StreamWriter(outputFilePath))
            {
                // Write headers
                foreach (var header in headers)
                {
                    writer.Write("{0}\t", header);
                }

                writer.WriteLine();

                // Write data
                foreach (var datasetResults in results)
                {
                    var fileLength = datasetResults["Pearson Corr Score"].Count;
                    for (var i = 0; i < fileLength; i++)
                    {
                        foreach (var header in headers)
                        {
                            var value = datasetResults.ContainsKey(header) ? datasetResults[header][i] : string.Empty;
                            writer.Write("{0}\t", value);
                        }

                        writer.WriteLine();
                    }
                }
            }
        }
Пример #57
0
        public void MassToleranceConstructorDaType()
        {
            var tol = new Tolerance(ToleranceUnit.DA, 10);

            Assert.AreEqual(ToleranceUnit.DA, tol.Unit);
        }
Пример #58
0
        private static ApartmentContourInfo GetApartmentContour(IEnumerable <ObjectId> linearIds)
        {
            var info     = ApartmentContour.CalcContour(Application.DocumentManager.MdiActiveDocument, linearIds);
            var database = Application.DocumentManager.MdiActiveDocument.Database;

            // Test code !
            using (var transaction = database.TransactionManager.StartTransaction())
            {
                var contourSegments = info.Contour;
                var innerSegments   = info.InternalSegments;

                // find out windows on wall, usually there are 4 or 3 parallel line segments for a window like below.
                //   -----------------------
                //   -----------------------
                //   -----------------------
                //   -----------------------
                const double                 maxWallWidth       = 600; // todo
                Tolerance                    tol                = new Tolerance(0.001, 0.001);
                List <LineSegment3d>         sideWindowSegments = new List <LineSegment3d>();
                List <List <LineSegment3d> > windowGroup        = new List <List <LineSegment3d> >();
                for (var i = 0; i < innerSegments.Count; i++)
                {
                    var thisSeg = innerSegments[i];
                    //var startPt = innerSegments[i].StartPoint;
                    //var endPt = innerSegments[i].EndPoint;
                    // do check if the start and end points are on the wall
                    //var cIndex = contourSegments.FindIndex((seg) => {
                    //    if (seg.IsOn(startPt, tol) && seg.IsOn(endPt, tol))
                    //        return true;
                    //    return false;
                    //});

                    // find out if the line segment is on the outer contour wall
                    //var cIndex = contourSegments.FindIndex((seg) =>
                    //{
                    //    if (seg.IsParallelTo(innerSegments[i], tol))
                    //    {
                    //        var dist = seg.GetDistanceTo(thisSeg.MidPoint);
                    //        if (dist < maxWallWidth)
                    //            return true;
                    //    }
                    //    return false;
                    //});
                    //if (cIndex != -1)
                    {
                        // find out all other parallel and equal length segments with this one
                        var                  startPt      = thisSeg.StartPoint;
                        var                  endPt        = thisSeg.EndPoint;
                        double               thisLength   = thisSeg.Length;
                        Vector3d             direction    = thisSeg.Direction.RotateBy(Math.PI / 2, Vector3d.ZAxis);
                        Line3d               startLine    = new Line3d(startPt, direction);
                        Line3d               endLine      = new Line3d(endPt, direction);
                        List <LineSegment3d> parEqualSegs = new List <LineSegment3d>();
                        for (var j = 0; j < innerSegments.Count; j++)
                        {
                            if (i == j || innerSegments[j] == null)
                            {
                                continue;                                     // itself
                            }
                            if (thisSeg.IsParallelTo(innerSegments[j]) &&
                                (startLine.IsOn(innerSegments[j].StartPoint, tol) && endLine.IsOn(innerSegments[j].EndPoint, tol)) ||
                                (startLine.IsOn(innerSegments[j].EndPoint, tol) && endLine.IsOn(innerSegments[j].StartPoint, tol)))
                            {
                                parEqualSegs.Add(innerSegments[j]);
                            }
                        }

                        if (parEqualSegs.Count > 1)
                        {
                            Line3d helper1    = new Line3d(thisSeg.MidPoint, thisSeg.Direction);
                            Line3d helper2    = new Line3d(thisSeg.MidPoint, direction);
                            var    intersects = helper1.IntersectWith(helper2);
                            var    basePt     = intersects[0];

                            //direction = (thisSeg.MidPoint - basePt).GetNormal();
                            // sort them by direction
                            parEqualSegs.Add(thisSeg);
                            parEqualSegs.Sort((seg1, seg2) => {
                                Vector3d vector1 = seg1.MidPoint - basePt;
                                Vector3d vector2 = seg2.MidPoint - basePt;
                                if (Math.Abs(vector1.DotProduct(direction) - vector2.DotProduct(direction)) < 0.001)
                                {
                                    return(0);
                                }
                                else if (vector1.DotProduct(direction) > vector2.DotProduct(direction))
                                {
                                    return(1);
                                }
                                return(-1);
                            });

                            List <LineSegment3d> thisWindow = new List <LineSegment3d>();
                            double distance = parEqualSegs[1].MidPoint.DistanceTo(parEqualSegs[0].MidPoint);
                            if (distance < maxWallWidth / 2)
                            {
                                thisWindow.Add(parEqualSegs[0]);
                                thisWindow.Add(parEqualSegs[1]);
                                for (var k = 2; k < parEqualSegs.Count; k++)
                                {
                                    var dist = parEqualSegs[k].MidPoint.DistanceTo(parEqualSegs[k - 1].MidPoint);
                                    if (thisWindow.Count > 4 || Math.Abs(dist - distance) > 100)
                                    {
                                        // we have find out 4 parallel lines or the distance is not equal to previous one
                                        break;
                                    }
                                    thisWindow.Add(parEqualSegs[k]);
                                }

                                if (thisWindow.Count > 2)
                                {
                                    // this will be treaded as a valid window
                                    windowGroup.Add(thisWindow);
                                }
                            }
                        }
                    }
                }

                innerSegments.RemoveAll((seg) => {
                    var index = windowGroup.FindIndex((window) => {
                        return(window.Contains(seg));
                    });
                    if (index != -1)
                    {
                        return(true);
                    }
                    return(false);
                });
                // end window

                var modelspaceId = SymbolUtilityServices.GetBlockModelSpaceId(database);
                var modelspace   = (BlockTableRecord)transaction.GetObject(modelspaceId, OpenMode.ForWrite);

                var      color1  = Color.FromColorIndex(ColorMethod.ByAci, 1); // 1: red
                ObjectId layerId = LayerUtils.AddNewLayer(database, "temp-poly", "Continuous", color1);

                for (var i = 0; i < windowGroup.Count; i++)
                {
                    var window = windowGroup[i];
                    for (var j = 0; j < window.Count; j++)
                    {
                        var start = window[j].StartPoint;
                        var end   = window[j].EndPoint;
                        var line  = new Line(start, end);
                        line.Color   = color1;
                        line.LayerId = layerId;

                        modelspace.AppendEntity(line);
                        transaction.AddNewlyCreatedDBObject(line, add: true);
                    }
                }

                var color    = Color.FromColorIndex(ColorMethod.ByAci, 3); // Green
                var polyline = new Polyline();
                for (var i = 0; i < contourSegments.Count; i++)
                {
                    var segment = contourSegments[i];
                    var start   = segment.StartPoint;
                    polyline.AddVertexAt(i, new Point2d(start.X, start.Y), 0, 0, 0);
                }
                polyline.Closed  = true;
                polyline.Color   = color;
                polyline.LayerId = layerId;

                modelspace.AppendEntity(polyline);
                transaction.AddNewlyCreatedDBObject(polyline, add: true);

                foreach (var segment in innerSegments)
                {
                    var line = new Line(segment.StartPoint, segment.EndPoint);

                    line.Color   = color;
                    line.LayerId = layerId;
                    modelspace.AppendEntity(line);
                    transaction.AddNewlyCreatedDBObject(line, add: true);
                }

                transaction.Commit();
            }
            return(info);
        }
Пример #59
0
        public void MassToleranceImplicitPlusMinus()
        {
            var tol = new Tolerance("+-10 ppm");

            Assert.AreEqual(tol.Type, ToleranceType.PlusAndMinus);
        }
Пример #60
0
 public static bool IsLineDuplicate(Line ln1, Line ln2, Tolerance tol)
 {
     return
         (ln1.StartPoint.IsEqualTo(ln2.StartPoint, tol) || ln1.StartPoint.IsEqualTo(ln2.EndPoint, tol)) &&
         (ln1.EndPoint.IsEqualTo(ln2.EndPoint) || ln1.EndPoint.IsEqualTo(ln2.StartPoint));
 }