}//*/

        public IEnumerable <ProductMatch> GetProductMZs(DBOptions options, GraphML_List <MsMsPeak> peaks)//, List<double> theoretical_product_mzs = null)
        {
            // speed optimizations
            //double[] experimental_masses = Query.spectrum.Masses;
            //GraphML_List<MSPeak> peaks = Query.spectrum.Peaks;
            int num_experimental_peaks = peaks.Count;

            TotalTheoreticalProducts = 0;
            TotalWeightedProducts    = 0;
            //New version that should include charged ions

            //foreach (ProductMatch matchTheo in AllFragmentSearch.ComputeAllFragments(Peptide, Query.precursor.Charge, options))
            foreach (ProductMatch matchTheo in options.fragments.ComputeFragmentsFast(Peptide.GetMasses(), Query.precursor.Charge, options))
            //foreach (ProductMatch matchTheo in options.fragments.ComputeFragments(Peptide, Query.precursor.Charge, options))
            {
                TotalTheoreticalProducts++;
                TotalWeightedProducts += matchTheo.weight;//++;
                //TODO test what is most common between charged ions, and uncharged ions
                //for (int charge = 1; charge <= Query.precursor.Charge; charge++)
                //for (int charge = Query.precursor.Charge; charge >= 1; charge--)
                //{
                double massDiff = options.productMassTolerance.Value;
                double bestMz   = -1;
                double bestInt  = 0;

                foreach (int index in Query.spectrum.GetIndexOfMZInRange(matchTheo.theoMz, options.productMassTolerance))
                {
                    if (peaks[index].Charge <= 0 || peaks[index].Charge == matchTheo.charge)
                    {
                        double diff = Numerics.CalculateMassError(peaks[index].MZ, matchTheo.theoMz, options.productMassTolerance.Units);
                        //double diff = matchTheo.theoMz - peaks[index].MZ;// experimental_masses[index];//TODO DALTON ONLY : add product mass tolerance unit test
                        if (Math.Abs(diff) < options.productMassTolerance.Value)
                        {
                            if (Math.Abs(diff) < Math.Abs(massDiff))//TODO Priority to intensity, or precision?
                            {
                                massDiff = diff;
                                bestMz   = peaks[index].MZ;
                            }
                            //if (peaks[index].Intensity > bestInt)
                            bestInt += peaks[index].Intensity;
                        }
                    }
                }
                if (bestMz >= 0)
                {
                    ProductMatch pMatch = new ProductMatch();
                    pMatch.weight              = matchTheo.weight;
                    pMatch.theoMz              = matchTheo.theoMz; // Utilities.MZFromMzSingleCharge(theoMass, charge);
                    pMatch.obsMz               = bestMz;           // experimental_masses[bestIndex];
                    pMatch.mass_diff           = massDiff;
                    pMatch.obsIntensity        = bestInt;          // Intensities[bestIndex];
                    pMatch.charge              = matchTheo.charge; // peaks[bestIndex].Charge;
                    pMatch.Fragment            = matchTheo.Fragment;
                    pMatch.fragmentPos         = matchTheo.fragmentPos;
                    pMatch.normalizedIntensity = pMatch.obsIntensity / (Query.spectrum.InjectionTime * Query.spectrum.PrecursorIntensityPerMilliSecond);
                    yield return(pMatch);
                    //break;
                }
            }
        }
Ejemplo n.º 2
0
 public Precursor()
 {
     this.psms_AllPossibilities = new PeptideSpectrumMatches(); // GraphML_List<PeptideSpectrumMatch>();
     this.psms    = new PeptideSpectrumMatches();               // GraphML_List<PeptideSpectrumMatch>();
     Isotopes     = new GraphML_List <Precursor>();
     OtherCharges = new GraphML_List <Precursor>();
 }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
0
        public Precursor(Track track, int charge, Sample entry, double massShift = 0, GraphML_List <Precursor> isotopes = null)//double mz, double intensity, int charge = -1, double rt = 0, double massShift = 0, List<Precursor> isotopes = null)
        {
            INDEX       = COMPTEUR++;
            this.Track  = track;
            this.Charge = charge;
            this.sample = entry;

            this.psms_AllPossibilities = new PeptideSpectrumMatches(); // GraphML_List<PeptideSpectrumMatch>();
            this.psms = new PeptideSpectrumMatches();                  // GraphML_List<PeptideSpectrumMatch>();
            //  this.Mz         = mz;
            //  this.Intensity  = intensity;
            //  this.Charge     = charge;
            this.Mass = Numerics.MassFromMZ(track.MZ, charge);
            //  this.Rt         = rt;
            MassShift = massShift;
            if (isotopes == null)
            {
                Isotopes = new GraphML_List <Precursor>();
            }
            else
            {
                Isotopes = isotopes;
            }
            OtherCharges = new GraphML_List <Precursor>();
        }
Ejemplo n.º 5
0
 public clCondition(int nbReplicates)
 {
     replicates = new GraphML_List <clReplicate>();
     for (int i = 0; i < nbReplicates; i++)
     {
         replicates.Add(null);
     }
 }
Ejemplo n.º 6
0
 public Cluster(Dictionary <int, List <int> > samples)
 {
     this.conditions = new GraphML_List <clCondition>();
     foreach (int c in samples.Keys)
     {
         this.conditions.Add(null);
     }
     this.samples = samples;
 }
Ejemplo n.º 7
0
        private static GraphML_List <MsMsPeak> FilterPeaks(GraphML_List <MsMsPeak> peaks, int maximumNumberOfPeaks)
        {
            GraphML_List <MsMsPeak> filtered_peaks = new GraphML_List <MsMsPeak>(peaks);

            if (maximumNumberOfPeaks > 0 && filtered_peaks.Count > maximumNumberOfPeaks)
            {
                filtered_peaks.Sort(MsMsPeak.DescendingIntensityComparison);
                filtered_peaks.RemoveRange(maximumNumberOfPeaks, filtered_peaks.Count - maximumNumberOfPeaks);
            }

            return(filtered_peaks);
        }
Ejemplo n.º 8
0
 public Result()
 {
     queries           = new Queries();
     precursors        = new Precursors();
     matchedPrecursors = new Precursors();
     clusters          = new GraphML_List <Cluster>();
     peptides          = new PeptideMatches(new PeptideMatch[0]);
     peptideSequences  = new PeptideMatches(new PeptideMatch[0]);
     proteins          = new ProteinGroupMatches();
     dbOptions         = new DBOptions("");
     samples           = new Samples();
 }
Ejemplo n.º 9
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);
        }
Ejemplo n.º 10
0
 /// <summary>
 /// Parameter less constructor used for save states
 /// </summary>
 public DBOptions(IConSol console = null)
 {
     fixedModifications    = new GraphML_List <Modification>();
     variableModifications = new GraphML_List <Modification>();
     fragments             = new Fragments();
     if (console == null)
     {
         ConSole = new ConSolCommandLine();
     }
     else
     {
         ConSole = console;
     }
 }
Ejemplo n.º 11
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);
        }
Ejemplo n.º 12
0
        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...
        }
Ejemplo n.º 13
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();
        }
Ejemplo n.º 15
0
        private static GraphML_List <MsMsPeak> Deisotopebkp(GraphML_List <MsMsPeak> peaks, int maxCharge, MassTolerance isotopicMzTolerance)
        {
            GraphML_List <MsMsPeak> new_peaks = new GraphML_List <MsMsPeak>(peaks);

            peaks.Sort(MsMsPeak.AscendingMzComparison);

            for (int lowMassIndex = 0; lowMassIndex < new_peaks.Count - 1; lowMassIndex++)
            {
                if (new_peaks[lowMassIndex].Charge > 0)
                {
                    int    toRemove      = -1;
                    double bestMassError = isotopicMzTolerance.Value;
                    double aim           = Numerics.IsotopicMassShift(1, new_peaks[lowMassIndex].Charge) + new_peaks[lowMassIndex].MZ;

                    int potentialIsotopeIndex = lowMassIndex + 1;
                    while (potentialIsotopeIndex < new_peaks.Count && new_peaks[potentialIsotopeIndex].MZ < aim + bestMassError)
                    {
                        if (new_peaks[lowMassIndex].Intensity > new_peaks[potentialIsotopeIndex].Intensity)
                        {
                            double massError = Math.Abs(Numerics.CalculateMassError(new_peaks[potentialIsotopeIndex].MZ, aim, isotopicMzTolerance.Units));
                            if (massError < bestMassError)
                            {
                                bestMassError = massError;
                                toRemove      = potentialIsotopeIndex;
                            }
                        }
                        potentialIsotopeIndex++;
                    }
                    if (toRemove > 0)
                    {
                        new_peaks[lowMassIndex].Intensity += new_peaks[toRemove].Intensity;
                        new_peaks.RemoveAt(toRemove);
                    }
                }
            }
            return(new_peaks);
        }
Ejemplo n.º 16
0
        public DBOptions(string fasta, IConSol console = null)
        {
            if (console == null)
            {
                ConSole = new ConSolCommandLine();
            }
            else
            {
                ConSole = console;
            }
            //Create with default values
            this.DecoyFusion           = true;
            this.FastaDatabaseFilepath = fasta;
            this.MaximumPeptideMass    = 10000;
            ProteaseDictionary proteases = ProteaseDictionary.Instance;

            this.DigestionEnzyme                     = proteases["no enzyme"]; // proteases["trypsin (no proline rule)"];
            this.NoEnzymeSearch                      = true;
            this.ToleratedMissedCleavages            = 100;                    // 3;//determines the length of peptides with no-enzyme option
            this.initiatorMethionineBehavior         = InitiatorMethionineBehavior.Variable;
            this.fixedModifications                  = new GraphML_List <Modification>();
            this.variableModifications               = new GraphML_List <Modification>();
            this.maximumVariableModificationIsoforms = 1024;

            this.MinimumPrecursorChargeState         = 1;
            this.MaximumPrecursorChargeState         = 4;
            this.MaximumNumberOfFragmentsPerSpectrum = 400;
            //TODO Add precision to the precursor by reading MS part of file
            this.precursorMassTolerance = new MassTolerance(0.005, MassToleranceUnits.Da);//2.1
            //TODO Add precision to the product masses by reading corresponding MS part of raw file
            this.productMassTolerance = new MassTolerance(0.005, MassToleranceUnits.Da);

            this.PSMFalseDiscoveryRate = 0.25;             // 0.05;

            this.OutputFolder    = @"C:\_IRIC\DATA\Test2"; //C:\Documents and Settings\ProteoAdmin\Desktop\AEffacer\Morpheus\Output";
            this.MinimumPSMScore = 0.0001;
        }
Ejemplo n.º 17
0
 public clCondition()
 {
     replicates = new GraphML_List <clReplicate>();
 }
Ejemplo n.º 18
0
 public clReplicate()
 {
     precursors = new GraphML_List <Precursor>();
 }
 public PeptideSpectrumMatch()
 {
     AllProductMatches = new GraphML_List <ProductMatch>();
 }
Ejemplo n.º 20
0
 public Cluster()
 {
     conditions = new GraphML_List <clCondition>();
 }
Ejemplo n.º 21
0
        public void GenerateQueries(Sample entry, Spectra spectra, Tracks tracks)//, double mz, double rt, double intensity)
        {
            Dictionary <Track, Precursor> Tracks   = new Dictionary <Track, Precursor>();
            Dictionary <Track, Precursor> Isotopes = new Dictionary <Track, Precursor>();

            //Create one query per Spectrum-Precursor duo, including Isotopes in the process to ease search
            //For further analysis, maintain a list of precursors (excluding isotopes)
            int nbMissedTrack = 0;

            //vsSDF sdf = entry.GetSDF();// Samples.LoadSDF(entry);
            //tracks.PrepareRtSort();
            //sdf.TRACKS_LIST.PrepareRtSort();
            spectra.Sort(ProductSpectrum.AscendingPrecursorMassComparison);

            foreach (ProductSpectrum spectrum in spectra)
            {
                NbSpectrum++;
                double intensityCumul = 0.0;
                bool   foundCharge    = false;
                Track  closestTrack   = null;

                List <Query> newQueries = new List <Query>();

                //TODO No threshold on sdf files, and preferably a C# routine that does what MassSense do
                foreach (Track track in tracks.GetTracksInMzRange(spectrum.PrecursorMZ, spectrum.IsolationWindow * dbOptions.EffectiveIsolationWindowRatio))//TODO Optimize this value
                {
                    Precursor prec = null;

                    if (track.RT_Min <= spectrum.RetentionTimeInMin &&
                        track.RT_Max >= spectrum.RetentionTimeInMin)
                    {
                        if (closestTrack == null || Math.Abs(track.MZ - spectrum.PrecursorMZ) < Math.Abs(closestTrack.MZ - spectrum.PrecursorMZ))
                        {
                            closestTrack = track;
                        }

                        if (Isotopes.ContainsKey(track))
                        {
                            break;
                        }

                        if (Tracks.ContainsKey(track))
                        {
                            prec = Tracks[track];
                        }
                        else
                        {
                            GraphML_List <Precursor> isotopes = GetIsotopes(track, dbOptions, tracks, entry);
                            if (isotopes.Count > 0)
                            {
                                prec = new Precursor(track, isotopes[0].Charge, entry, 0.0, isotopes);
                                Tracks.Add(track, prec);
                                prec.OtherCharges = GetOtherCharges(prec, dbOptions, tracks, entry);

                                foreach (Precursor isotope in prec.Isotopes)
                                {
                                    if (!Isotopes.ContainsKey(isotope.Track))
                                    {
                                        Isotopes.Add(isotope.Track, isotope);
                                    }
                                }
                            }
                        }
                        if (prec != null)
                        {
                            intensityCumul += track.INTENSITY;
                            newQueries.Add(new Query(dbOptions, entry, spectrum, prec, NbSpectrum));

                            if (prec.Charge == spectrum.PrecursorCharge)
                            {
                                foundCharge = true;
                            }
                        }
                    }
                }

                if (!foundCharge)
                {
                    /*if (closestTrack != null && Tracks.ContainsKey(closestTrack) && Math.Abs(Numerics.CalculateMassError(closestTrack.MZ, spectrum.PrecursorMZ, dbOptions.precursorMassTolerance.Units)) < dbOptions.precursorMassTolerance.Value)
                     * {
                     *  if(closestTrack.RT_Min > (float)(spectrum.RetentionTimeInMin - dbOptions.ComputedRetentionTimeDiff))
                     *      closestTrack.RT_Min = (float)(spectrum.RetentionTimeInMin - dbOptions.ComputedRetentionTimeDiff);
                     *  if (closestTrack.RT_Max < (float)(spectrum.RetentionTimeInMin + dbOptions.ComputedRetentionTimeDiff))
                     *      closestTrack.RT_Max = (float)(spectrum.RetentionTimeInMin + dbOptions.ComputedRetentionTimeDiff);
                     *  if (closestTrack.INTENSITY < spectrum.PrecursorIntensity)
                     *      closestTrack.INTENSITY = spectrum.PrecursorIntensity;
                     *
                     *  Precursor prec = Tracks[closestTrack];
                     *  if (prec.Charge == spectrum.PrecursorCharge)
                     *  {
                     *      Add(new Query(dbOptions, entry, spectrum, prec, NbSpectrum));
                     *  }
                     *  else
                     *  {
                     *      Precursor newPrec = new Precursor(closestTrack, spectrum.PrecursorCharge, entry);
                     *      Add(new Query(dbOptions, entry, spectrum, newPrec, NbSpectrum));
                     *  }
                     * }
                     * else//*/
                    {
                        nbMissedTrack++;
                        closestTrack = new Track((float)spectrum.PrecursorMZ, (float)spectrum.RetentionTimeInMin, spectrum.PrecursorIntensity,
                                                 (float)(spectrum.RetentionTimeInMin - dbOptions.ComputedRetentionTimeDiff), (float)(spectrum.RetentionTimeInMin + dbOptions.ComputedRetentionTimeDiff),
                                                 true);

                        Precursor prec = new Precursor(closestTrack, spectrum.PrecursorCharge, entry);
                        Tracks.Add(closestTrack, prec);
                        Add(new Query(dbOptions, entry, spectrum, prec, NbSpectrum));
                    }
                }//*/

                if (newQueries.Count > 0)
                {
                    //Remove precursors if estimated fragment intensities are too low (based on precursor intensity ratios and isolation window placement)
                    foreach (Query q in newQueries)
                    {
                        //if (q.precursor.Track.INTENSITY > intensityCumul * dbOptions.MinimumPrecursorIntensityRatioInIsolationWindow)//Need to be 5% of all intensity
                        //{
                        this.Add(q);
                        //}
                    }
                }
                Console.Write("\r{0}%   ", ((100 * NbSpectrum) / spectra.Count));
            }
            Console.Write("\r{0}%   ", 100);

            //Sort queries to ease search
            this.Sort(AscendingPrecursorMassComparison);
            foreach (Track track in Tracks.Keys)
            {
                if (!Isotopes.ContainsKey(track))
                {
                    Precursors.Add(Tracks[track]);
                }
            }

            //TODO Validate this approach
            //REMOVE QUERIES RELATED TO AN ISOTOPE and Compute the average CoElution
            Dictionary <ProductSpectrum, double> DicOfSpectrumIntensities = new Dictionary <ProductSpectrum, double>();

            for (int i = 0; i < this.Count;)
            {
                Query query = this[i];
                if (!Isotopes.ContainsKey(query.precursor.Track))
                {
                    if (!DicOfSpectrumIntensities.ContainsKey(query.spectrum))
                    {
                        DicOfSpectrumIntensities.Add(query.spectrum, query.precursor.Track.INTENSITY);
                    }
                    else
                    {
                        DicOfSpectrumIntensities[query.spectrum] += query.precursor.Track.INTENSITY;
                    }
                    i++;
                }
                else
                {
                    this.RemoveAt(i);
                }
            }

            //REMOVE Queries with Precursor intensities too low
            for (int i = 0; i < this.Count;)
            {
                Query query = this[i];
                if (query.precursor.Track.INTENSITY < DicOfSpectrumIntensities[query.spectrum] * dbOptions.MinimumPrecursorIntensityRatioInIsolationWindow)
                {
                    this.RemoveAt(i);
                }
                else
                {
                    i++;
                }
            }//*/

            Dictionary <ProductSpectrum, int> DicOfSpectrumTracks = new Dictionary <ProductSpectrum, int>();

            for (int i = 0; i < this.Count;)
            {
                Query query = this[i];
                if (!Isotopes.ContainsKey(query.precursor.Track))
                {
                    if (!DicOfSpectrumTracks.ContainsKey(query.spectrum))
                    {
                        DicOfSpectrumTracks.Add(query.spectrum, 1);
                    }
                    else
                    {
                        DicOfSpectrumTracks[query.spectrum]++;
                    }
                    i++;
                }
                else
                {
                    this.RemoveAt(i);
                }
            }

            double averageNbPrecursorPerSpectrum = 0;
            int    nbSpectrumMatchedToTrack      = 0;

            foreach (ProductSpectrum spectrum in DicOfSpectrumTracks.Keys)
            {
                nbSpectrumMatchedToTrack++;
                averageNbPrecursorPerSpectrum += DicOfSpectrumTracks[spectrum];
            }
            dbOptions.ConSole.WriteLine(entry.sSDF + " :" + Precursors.Count + " precursors [" + Isotopes.Count + " isotopes] spreaded in " + Count + " queries [" + nbMissedTrack + " trackless precursors]");
            dbOptions.ConSole.WriteLine("Average Precursors per Spectrum : " + averageNbPrecursorPerSpectrum / (double)nbSpectrumMatchedToTrack);
        }
Ejemplo n.º 22
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);
        }
Ejemplo n.º 23
0
        public ProductSpectrum(int scanNumber, double retentionTimeInMin, string fragmentationMethod, double precursorMZ, double precursorIntensity, int precursorCharge, double precursorMass, GraphML_List <MsMsPeak> peaks, double isolationWindow, double injectionTime, double ms1InjectionTime)
        {
            this.INDEX = COMPTEUR++;
            //this.Filename = filename;
            this.ScanNumber          = scanNumber;
            this.RetentionTimeInMin  = retentionTimeInMin;
            this.FragmentationMethod = fragmentationMethod;
            this.PrecursorMZ         = precursorMZ;
            this.PrecursorIntensity  = precursorIntensity;
            this.PrecursorCharge     = precursorCharge;
            this.PrecursorMass       = precursorMass;
            this.IsolationWindow     = isolationWindow;
            this.InjectionTime       = injectionTime;
            this.Ms1InjectionTime    = ms1InjectionTime;

            this.TotalIntensity  = 0.0;
            this.MostIntensePeak = 0.0;
            if (peaks != null)
            {
                this.Peaks = peaks;
                for (int p = 0; p < peaks.Count; p++)
                {
                    if (peaks[p].Intensity > this.MostIntensePeak)
                    {
                        this.MostIntensePeak = peaks[p].Intensity;
                    }
                    this.TotalIntensity += peaks[p].Intensity;
                }
            }
            this.PrecursorIntensityPerMilliSecond = precursorIntensity / ms1InjectionTime;
        }
Ejemplo n.º 24
0
        private static GraphML_List <MsMsPeak> AssignChargeStatesAndDeisotope(GraphML_List <MsMsPeak> peaks, int maxCharge, MassTolerance isotopicMzTolerance)
        {
            GraphML_List <MsMsPeak> new_peaks = new GraphML_List <MsMsPeak>(peaks);

            //peaks.Sort(MSPeak.AscendingMzComparison);

            int[] bestIsotopes    = new int[4];
            int[] currentIsotopes = new int[4];
            for (int lowMassIndex = 0; lowMassIndex < new_peaks.Count - 1; lowMassIndex++)
            {
                double bestChargeScore = 0;
                int    bestCharge      = 0;
                bestIsotopes[0] = 0; bestIsotopes[1] = 0; bestIsotopes[2] = 0; bestIsotopes[3] = 0;
                for (int charge = maxCharge; charge > 0; charge--)
                {
                    currentIsotopes[0] = 0; currentIsotopes[1] = 0; currentIsotopes[2] = 0; currentIsotopes[3] = 0;
                    double score = 0;
                    int    potentialIsotopeIndex = lowMassIndex + 1;
                    for (int isotope = 1; isotope <= 4; isotope++)
                    {
                        double bestMassError = isotopicMzTolerance.Value;
                        double aim           = Numerics.IsotopicMassShift(isotope, charge) + new_peaks[lowMassIndex].MZ;

                        while (potentialIsotopeIndex < new_peaks.Count && new_peaks[potentialIsotopeIndex].MZ < aim + bestMassError)
                        {
                            if (new_peaks[lowMassIndex].Intensity > new_peaks[potentialIsotopeIndex].Intensity)
                            {
                                double massError = Math.Abs(Numerics.CalculateMassError(new_peaks[potentialIsotopeIndex].MZ, aim, isotopicMzTolerance.Units));
                                if (massError < bestMassError)
                                {
                                    bestMassError = massError;
                                    currentIsotopes[isotope - 1] = potentialIsotopeIndex;
                                }
                            }
                            potentialIsotopeIndex++;
                        }
                        score += isotopicMzTolerance.Value - bestMassError;
                        if (score == 0)
                        {
                            break;
                        }
                        ;
                    }
                    if (score > bestChargeScore)
                    {
                        bestIsotopes[0] = currentIsotopes[0];
                        bestIsotopes[1] = currentIsotopes[1];
                        bestIsotopes[2] = currentIsotopes[2];
                        bestIsotopes[3] = currentIsotopes[3];
                        bestChargeScore = score;
                        bestCharge      = charge;
                    }
                }

                new_peaks[lowMassIndex].Charge = bestCharge;
                for (int i = 3; i >= 0; i--)
                {
                    if (bestIsotopes[i] > 0)
                    {
                        new_peaks[lowMassIndex].Intensity += new_peaks[bestIsotopes[i]].Intensity;
                        new_peaks.RemoveAt(bestIsotopes[i]);
                    }
                }
            }
            return(new_peaks);
        }
Ejemplo n.º 25
0
 public PeptideMatch()
 {
     clusters = new GraphML_List <Cluster>();
 }
Ejemplo n.º 26
0
 public PeptideMatch(Peptide peptide)
 {
     this.peptide = peptide;
     clusters     = new GraphML_List <Cluster>();
 }
Ejemplo n.º 27
0
 public ProductSpectrum()
 {
     Peaks = new GraphML_List <MsMsPeak>();
 }
Ejemplo n.º 28
0
 public ProteinGroupMatch()
     : base()
 {
     PeptideMatches = new GraphML_List <PeptideMatch>();
     Proteins       = new GraphML_List <Protein>();
 }