Beispiel #1
0
        private static GraphML_List <MsMsPeak> AssignChargeStatesbkp(GraphML_List <MsMsPeak> peaks, int maxCharge, MassTolerance isotopicMzTolerance)
        {
            GraphML_List <MsMsPeak> new_peaks = new GraphML_List <MsMsPeak>();

            for (int i = 0; i < peaks.Count - 1; i++)
            {
                int        j       = i + 1;
                List <int> charges = new List <int>();
                while (j < peaks.Count)
                {
                    if (peaks[j].MZ > (peaks[i].MZ + Constants.C12_C13_MASS_DIFFERENCE) + isotopicMzTolerance)
                    {
                        break;
                    }

                    for (int c = maxCharge; c >= 1; c--)
                    {
                        if (Math.Abs(Numerics.CalculateMassError(peaks[j].MZ, peaks[i].MZ + Constants.C12_C13_MASS_DIFFERENCE / (double)c, isotopicMzTolerance.Units)) <= isotopicMzTolerance.Value)
                        {
                            new_peaks.Add(new MsMsPeak(peaks[i].MZ, peaks[i].Intensity, c));
                            charges.Add(c);
                        }
                    }

                    j++;
                }
                if (charges.Count == 0)
                {
                    new_peaks.Add(new MsMsPeak(peaks[i].MZ, peaks[i].Intensity, 0));
                }
            }

            return(new_peaks);
        }
 public void AddOnlyOnce(Cluster cluster)
 {
     if (!clusters.Contains(cluster))
     {
         clusters.Add(cluster);
     }
 }
 public clCondition(int nbReplicates)
 {
     replicates = new GraphML_List <clReplicate>();
     for (int i = 0; i < nbReplicates; i++)
     {
         replicates.Add(null);
     }
 }
Beispiel #4
0
        public static Spectra Import(string filenameMSMS, string filenameTracks, DBOptions dbOptions)
        {
            Spectra spectra = new Spectra();
            vsCSV   csv     = new vsCSV(filenameMSMS);

            if (csv.LINES_LIST.Count == 0 || csv.LINES_LIST[0].CompareTo(ProductSpectrum.TITLE) != 0)
            {
                return(null);
            }
            for (int i = 1; i < csv.LINES_LIST.Count; i++)
            {
                string[] splits  = csv.LINES_LIST[i].Split(vsCSV._Generic_Separator);
                double   mz      = double.Parse(splits[3]);
                int      charge  = int.Parse(splits[5]);
                int      nbPeaks = int.Parse(splits[9]);
                GraphML_List <MsMsPeak> peaks = new GraphML_List <MsMsPeak>(nbPeaks);
                i++;
                for (int j = 0; j < nbPeaks; i++, j++)
                {
                    try
                    {
                        string[] splitPeaks = csv.LINES_LIST[i].Split('\t');
                        if (splitPeaks.Length > 2)
                        {
                            peaks.Add(new MsMsPeak(double.Parse(splitPeaks[0]), double.Parse(splitPeaks[1]), int.Parse(splitPeaks[2])));
                        }
                        else
                        {
                            peaks.Add(new MsMsPeak(double.Parse(splitPeaks[0]), double.Parse(splitPeaks[1]), 0));
                        }
                    }
                    catch (Exception)
                    {
                        dbOptions.ConSole.WriteLine("Error parsing line : " + csv.LINES_LIST[i]);
                    }
                }
                spectra.AddMSMS(new ProductSpectrum(int.Parse(splits[0]), double.Parse(splits[1]), splits[2], mz, double.Parse(splits[4]), charge, Proteomics.Utilities.Numerics.MassFromMZ(mz, charge), peaks, double.Parse(splits[8]), double.Parse(splits[10]), double.Parse(splits[11])));
            }
            if (!string.IsNullOrEmpty(filenameTracks))
            {
                spectra.tracks = Tracks.Import(filenameTracks, dbOptions);
            }
            return(spectra);
        }
Beispiel #5
0
        public static GraphML_List <Precursor> GetIsotopes(Track track, DBOptions dbOptions, Tracks listTracks, Sample entry)
        {
            double isotopicMzTolerance = dbOptions.precursorMassTolerance.Value * 0.5;

            if (dbOptions.precursorMassTolerance.Units == MassToleranceUnits.ppm)
            {
                isotopicMzTolerance = (isotopicMzTolerance / 1e6) * track.MZ;
            }

            GraphML_List <Precursor> bestIsotopes = new GraphML_List <Precursor>();

            for (int charge = dbOptions.MaximumPrecursorChargeState; charge >= dbOptions.MinimumPrecursorChargeState; charge--)
            {
                GraphML_List <Precursor> isotopes = new GraphML_List <Precursor>();
                for (int nbIsotope = 1; nbIsotope < 5; nbIsotope++)
                {
                    double bestDeltaMz = isotopicMzTolerance;
                    Track  bestTrack   = null;
                    double massShift   = Numerics.IsotopicMassShift(nbIsotope, charge);
                    double mzIsotope   = track.MZ + massShift;

                    foreach (Track trackToTest in listTracks.GetTracksInMzRange(mzIsotope, isotopicMzTolerance))
                    {
                        if (trackToTest.RT >= track.RT_Min &&
                            trackToTest.RT <= track.RT_Max)
                        {
                            double mzDiff = Math.Abs(mzIsotope - trackToTest.MZ);
                            if (mzDiff < bestDeltaMz)//TODO Is the best isotope the most precise one or the most intense?? Use a scoring function!
                            {
                                bestDeltaMz = mzDiff;
                                bestTrack   = trackToTest;
                            }
                        }
                    }
                    if (bestTrack != null)
                    {
                        isotopes.Add(new Precursor(bestTrack, charge, entry, Constants.C12_C13_MASS_DIFFERENCE * nbIsotope, null));
                    }
                    else
                    {
                        break;
                    }
                }
                if (isotopes.Count > bestIsotopes.Count)//TODO Best way to compare isotope potentials? Number of isotopes? Delta Mass? Intensity ratios?
                {
                    bestIsotopes = isotopes;
                }
            }
            return(bestIsotopes);
        }
        public GraphML_List <Cluster> Search(Precursors precursors, bool runCluster)
        {
            options.ConSole.WriteLine("Grouping precursors based on common features...");
            precursors.Sort(Precursor.CompareProbabilityScore);

            GraphML_List <Cluster> clusters = new GraphML_List <Cluster>();

            bool[] done = new bool[precursors.Count];
            for (int i = 0; i < done.Length; i++)
            {
                done[i] = false;
            }

            //Step 1 : Regroup psms based on mz/rt/intensity/Sequence proximity score (ProteoProfile Code)
            for (int i = 0; i < precursors.Count; i++)
            {
                if (!done[i])
                {
                    Cluster group = new Cluster(samples);
                    group.Add(precursors[i]);
                    if (runCluster)
                    {
                        for (int j = i + 1; j < precursors.Count; j++)
                        {
                            if (!done[j] && precursors[i].sample != precursors[j].sample)
                            {
                                double score = Score(precursors[i], precursors[j]);
                                //TODO Implement ProteoProfile Clustering algorithm, or anything on the litterature, as long as its backed by the scoring function
                                if (score > 0.75)//TODO Should we put a threshold here? Can it be computed dynamically?
                                {
                                    group.Add(precursors[j]);
                                    done[j] = true;
                                }
                            }
                        }
                    }
                    clusters.Add(group);
                }
            }
            options.ConSole.WriteLine("Created " + clusters.Count + " clusters");
            return(clusters);
            //TODO I should not use psms in more than one cluster...
        }
Beispiel #7
0
        public static GraphML_List <Precursor> GetOtherCharges(Precursor precursor, DBOptions dbOptions, Tracks listTracks, Sample entry)
        {
            GraphML_List <Precursor> otherPrecursor = new GraphML_List <Precursor>();

            for (int charge = dbOptions.MaximumPrecursorChargeState; charge >= 1; charge--)
            {
                if (charge != precursor.Charge)
                {
                    double aimedMZ = Numerics.MZFromMass(precursor.Mass, charge);

                    double chargeMzTolerance = dbOptions.precursorMassTolerance.Value * 0.5;
                    if (dbOptions.precursorMassTolerance.Units == MassToleranceUnits.ppm)
                    {
                        chargeMzTolerance = (chargeMzTolerance / 1e6) * aimedMZ;
                    }

                    double bestDeltaMz = chargeMzTolerance;
                    Track  bestTrack   = null;
                    foreach (Track trackToTest in listTracks.GetTracksInMzRange(aimedMZ, chargeMzTolerance))
                    {
                        if (trackToTest.RT >= precursor.Track.RT_Min &&
                            trackToTest.RT <= precursor.Track.RT_Max)
                        {
                            double mzDiff = Math.Abs(aimedMZ - trackToTest.MZ);
                            if (mzDiff < bestDeltaMz)//TODO Is the best isotope the most precise one or the closest in intensity?? Use a scoring function to get both!
                            {
                                bestDeltaMz = mzDiff;
                                bestTrack   = trackToTest;
                            }
                        }
                    }
                    if (bestTrack != null)
                    {
                        otherPrecursor.Add(new Precursor(bestTrack, charge, precursor.sample, 0, GetIsotopes(bestTrack, dbOptions, listTracks, entry)));
                    }
                }
            }
            return(otherPrecursor);
        }
        public void Initialize(DBOptions options, IEnumerable <ProductMatch> productMZs)
        {
            //List<double> theoretical_product_mz = Peptide.CalculateAllProductMz(PRODUCT_TYPES[Query.spectrum.FragmentationMethod], Query.precursor);
            //TotalProducts = theoretical_product_mz.Count;
            MatchingProducts  = 0;
            MatchingIntensity = 0.0;
            double cumulDiff = 0;

            ProductScore = 0;
            GraphML_List <ProductMatch> cumulMatch = new GraphML_List <ProductMatch>();

            MatchingWeightedProducts = 0;
            foreach (ProductMatch match in productMZs)
            {
                MatchingProducts++;
                MatchingWeightedProducts += match.weight;
                MatchingIntensity        += match.obsIntensity;
                cumulDiff += Math.Abs(match.mass_diff) * match.weight;

                cumulMatch.Add(match);
                if (match.obsIntensity > highestFragmentIntensity)
                {
                    highestFragmentIntensity = match.obsIntensity;
                }
                //options.ConSole.WriteLine("fragment intensity higher than most intense fragment ... should not happen!");
            }
            AllProductMatches         = cumulMatch;
            MatchingProductsFraction  = (double)MatchingWeightedProducts / (double)TotalWeightedProducts;
            MatchingIntensityFraction = MatchingIntensity / (double)(highestFragmentIntensity * TotalTheoreticalProducts);
            if (MatchingIntensityFraction > 1)
            {
                MatchingIntensityFraction = 1.0;
            }
            ProductScore   = 1.0 - (cumulDiff / (double)(MatchingProducts * options.productMassTolerance.Value));
            IntensityScore = MatchingIntensityFraction / (double)Query.spectrum.Peaks.Count;
            MorpheusScore  = MatchingProducts + MatchingIntensityFraction;
            FragmentScore  = ComputeFragmentScore();
        }
 public void Add(Precursor precursor)
 {
     precursors.Add(precursor);
 }
Beispiel #10
0
        public static Spectra Load(pwiz.CLI.msdata.MSDataFile msFile, DBOptions options, string filePath, bool loadMS = true, bool filterMS2 = true)
        {
            //Find file name in msFile;
            string  mzMlFilepath = filePath;
            int     num_spectra  = msFile.run.spectrumList.size();
            Spectra spectra      = new Spectra(num_spectra);
            //List<Trail> trails = new List<Trail>();
            MS1Spectrum previousMS1 = null;

            try
            {
                //TODO DONT forget to remove the limiter
                //int maxNbMSMS = 10;
                double LastMs1InjectionTime = 0;
                for (int i = 0; i < num_spectra /* && i < 200*/; i++)//TODO Fix that later!
                {
                    //Spectrum
                    pwiz.CLI.msdata.Spectrum spec = msFile.run.spectrumList.spectrum(i, true);

                    if (spec.precursors.Count > 0 || spec.cvParam(pwiz.CLI.cv.CVID.MS_ms_level).value > 1)//is an MSMS
                    {
                        double retention_time = spec.scanList.scans[0].cvParam(pwiz.CLI.cv.CVID.MS_scan_start_time).timeInSeconds() / 60.0;

                        //List precursors and their intensities
                        double precursor_mz         = 0;//Is there a value for the time a scan took to complete?
                        int    charge               = 2;
                        double precursor_intensity  = 0;
                        string fragmentation_method = "unknown";
                        double isolationWindow      = 1.0;
                        double injectionTime        = spec.scanList.scans[0].cvParam(pwiz.CLI.cv.CVID.MS_ion_injection_time).value;
                        foreach (pwiz.CLI.msdata.Precursor precursor in spec.precursors)
                        {
                            fragmentation_method = precursor.activation.cvParams[0].name;
                            if (precursor.isolationWindow.cvParams.Count > 2 && (double)precursor.isolationWindow.cvParams[1].value == (double)precursor.isolationWindow.cvParams[2].value)
                            {
                                isolationWindow = precursor.isolationWindow.cvParams[1].value;
                            }
                            else if (precursor.isolationWindow.cvParams.Count > 2)
                            {
                                options.ConSole.WriteLine("Weird Isolation Window");
                            }

                            foreach (pwiz.CLI.msdata.SelectedIon ion in precursor.selectedIons)
                            {
                                //Cycle through MS to get real precursor intensities
                                precursor_mz = ion.cvParams[0].value;
                                if (ion.cvParams.Count > 1)
                                {
                                    charge = (int)ion.cvParams[1].value;
                                }
                                //else
                                //    dbOptions.ConSole.WriteLine("No charge computed for precursor ");
                                if (ion.cvParams.Count > 2)
                                {
                                    precursor_intensity = ion.cvParams[2].value;
                                }
                            }
                        }

                        int scan_index  = i;
                        int scan_number = scan_index + 1;

                        pwiz.CLI.msdata.BinaryDataArray mz        = spec.getMZArray();
                        pwiz.CLI.msdata.BinaryDataArray intensity = spec.getIntensityArray();

                        int num_peaks = mz.data.Count;
                        if (num_peaks != intensity.data.Count)
                        {
                            options.ConSole.WriteLine("PreoteWizard reports peaks arrays (mz/intensity) of different sizes : (" + num_peaks + "/" + intensity.data.Count + ")");
                            if (intensity.data.Count < num_peaks)
                            {
                                num_peaks = intensity.data.Count;
                            }
                        }
                        GraphML_List <MsMsPeak> peaks = new GraphML_List <MsMsPeak>(num_peaks);
                        for (int k = 0; k < num_peaks; k++)
                        {
                            if (intensity.data[k] > 0)
                            {
                                MsMsPeak peak = new MsMsPeak(mz.data[k], intensity.data[k], 0);
                                peaks.Add(peak);
                            }
                        }
                        mz.Dispose(); mz = null;
                        intensity.Dispose(); intensity = null;

                        peaks.Sort(MsMsPeak.AscendingMzComparison);

                        if (filterMS2)
                        {
                            //peaks = AssignChargeStates(peaks, options.maximumAssumedPrecursorChargeState, options.precursorMassTolerance);
                            //peaks = Deisotopebkp(peaks, options.maximumAssumedPrecursorChargeState, options.precursorMassTolerance);
                            peaks = AssignChargeStatesAndDeisotope(peaks, options.MaximumPrecursorChargeState, new MassTolerance(options.productMassTolerance.Value * 0.5, options.productMassTolerance.Units));
                            peaks = FilterPeaks(peaks, options.MaximumNumberOfFragmentsPerSpectrum);

                            //TODO Add Contaminant removal
                            //peaks = ContaminantMasses.RemoveContaminantsFromMzSortedList(peaks, options.productMassTolerance);

                            //Can sometime be sorted by intensity after this call
                            //peaks = FilterPeaksV2(peaks);
                            peaks.Sort(MsMsPeak.AscendingMzComparison);
                        }

                        /*//TODO Validate that in most cases, next steps can calculate missing charge
                         * if (charge == 0)
                         * {
                         *  for (int c = options.minimumAssumedPrecursorChargeState; c <= options.maximumAssumedPrecursorChargeState; c++)
                         *  {
                         *      if (options.assignChargeStates)
                         *      {
                         *          peaks = AssignChargeStates(peaks, c, options.productMassTolerance);
                         *          if (options.deisotope)
                         *          {
                         *              peaks = Deisotope(peaks, c, options.productMassTolerance);
                         *          }
                         *      }
                         *
                         *      double precursor_mass = Utilities.MassFromMZ(precursor_mz, c);
                         *
                         *      ProductSpectrum spectrum = new ProductSpectrum(mzMlFilepath, scan_number, retention_time, fragmentation_method, precursor_mz, precursor_intensity, c, precursor_mass, peaks);
                         *      spectra.Add(spectrum);
                         *  }
                         * }
                         * else//*/
                        {/*
                          * if (options.assignChargeStates)
                          * {
                          * peaks = AssignChargeStatesbkp(peaks, charge, options.productMassTolerance);
                          * if (options.deisotope)
                          * {
                          *     peaks = Deisotopebkp(peaks, charge, options.productMassTolerance);
                          * }
                          * }//*/
                         //peaks = AssignChargeStatesAndDeisotope(peaks, options.maximumAssumedPrecursorChargeState, options.productMassTolerance);

                            double precursor_mass = Numerics.MassFromMZ(precursor_mz, charge);

                            ProductSpectrum spectrum = new ProductSpectrum(scan_number, retention_time, fragmentation_method, precursor_mz, precursor_intensity, charge, precursor_mass, peaks, isolationWindow, injectionTime, LastMs1InjectionTime);
                            spectra.AddMSMS(spectrum);
                            //zones.Add(new Zone(precursor_mz - isolationWindow, precursor_mz + isolationWindow, retention_time));
                        }

                        //if (spectra.Count >= maxNbMSMS)
                        //    i = 10000000;
                    }
                    else //Is an MS
                    {
                        LastMs1InjectionTime = spec.scanList.scans[0].cvParam(pwiz.CLI.cv.CVID.MS_ion_injection_time).value;
                        if (loadMS)
                        {
                            double retention_time = spec.scanList.scans[0].cvParam(pwiz.CLI.cv.CVID.MS_scan_start_time).timeInSeconds() / 60.0;

                            pwiz.CLI.msdata.BinaryDataArray mz        = spec.getMZArray();
                            pwiz.CLI.msdata.BinaryDataArray intensity = spec.getIntensityArray();

                            if (previousMS1 != null)
                            {
                                previousMS1.ScanDuration = retention_time - previousMS1.RetentionTimeInMin;
                                spectra.MS1s.Add(previousMS1);
                            }
                            previousMS1 = new MS1Spectrum(i, retention_time, intensity.data, mz.data, 1);
                            //Trail.Follow(mz.data, intensity.data, retention_time, ref trails, options);
                            //Trail.RemoveFinished(ref trails, spectra, 1);
                        }
                    }
                    spec.Dispose(); spec = null;
                    Console.Write("\r{0}%   ", ((100 * i) / num_spectra));
                }
                if (previousMS1 != null)
                {
                    spectra.MS1s.Add(previousMS1);
                }

                /*
                 * //Optimization of Track following parameters
                 * long nbChargedTracks = 0;
                 * for(int missingScans = 1; missingScans < 5; missingScans++)
                 * {
                 *  for(int centroid = 1; centroid < 5; centroid++)
                 *  {
                 *      for(int minPeaks = 1; minPeaks < 7; minPeaks++)
                 *      {
                 *          for(double valleyFactor = 0.1; valleyFactor < 4; valleyFactor += 0.3)
                 *          {
                 *              //weightedMean
                 *              Tracks tracks = ComputeSpectraTracks(spectra, options, mzMlFilepath, missingScans, centroid, minPeaks, valleyFactor, MaxQuant.CentroidPosition.weightedMean);
                 *              tracks.Sort(Tracks.AscendingPrecursorMassComparison);
                 *              long cumulIsotopes = 0;
                 *              foreach (stTrack track in tracks)
                 *                  cumulIsotopes += Queries.GetIsotopes(track, options, tracks, sample).Count;
                 *              if (cumulIsotopes > nbChargedTracks)
                 *              {
                 *                  nbChargedTracks = cumulIsotopes;
                 *                  dbOptions.ConSole.WriteLine(missingScans + "," + centroid + "," + minPeaks + "," + valleyFactor + ",weightedMean");
                 *              }
                 *
                 *              //Gaussian
                 *              tracks = ComputeSpectraTracks(spectra, options, mzMlFilepath, missingScans, centroid, minPeaks, valleyFactor, MaxQuant.CentroidPosition.gaussian);
                 *              tracks.Sort(Tracks.AscendingPrecursorMassComparison);
                 *              cumulIsotopes = 0;
                 *              foreach (stTrack track in tracks)
                 *                  cumulIsotopes += Queries.GetIsotopes(track, options, tracks, sample).Count;
                 *              if (cumulIsotopes > nbChargedTracks)
                 *              {
                 *                  nbChargedTracks = cumulIsotopes;
                 *                  dbOptions.ConSole.WriteLine(missingScans + "," + centroid + "," + minPeaks + "," + valleyFactor + ",Gaussian");
                 *              }
                 *          }
                 *      }
                 *  }
                 * }//*/

                if (spectra.MS1s.Count > 0)
                {
                    spectra.tracks = ComputeSpectraTracks(spectra, options, mzMlFilepath, 3, 1, 3, 1.7, MaxQuant.CentroidPosition.weightedMean);
                }
                else
                {
                    spectra.tracks = new Tracks();
                }
                spectra.tracks.Sort(Tracks.AscendingPrecursorMassComparison);
                Console.Write("\r{0}%   ", 100);

                //ContaminantMasses.DisplayContaminants();
            }
            catch (Exception ex)
            {
                options.ConSole.WriteLine(ex.StackTrace);
                options.ConSole.WriteLine(ex.Message);
            }
            return(spectra);
        }