Ejemplo n.º 1
0
        /// <summary>
        /// Provides deconvoluted elution curves of mixed spectra from the provided raw files using the provided synthetic raw file
        /// Exports in CSV files and stores everything in class objects
        /// </summary>
        /// <param name="spikedRaws"></param>
        /// <param name="mixedRaws"></param>
        /// <param name="fastaFile"></param>
        /// <param name="folderToOutputTo"></param>
        /// <param name="conSol"></param>
        public void Solve(string[] spikedRaws, string[] mixedRaws, string fastaFile, string folderToOutputTo, IConSol conSol)
        {
            dbOptions     = CreateOptions(fastaFile, folderToOutputTo, precTolPpm, prodTolDa, conSol);
            SpikedSamples = new Samples(dbOptions);
            for (int i = 0; i < spikedRaws.Length; i++)
            {
                SpikedSamples.Add(new Sample(i + 1, 1, 1, spikedRaws[i], spikedRaws[i], 0, ""));
            }

            //Precompute Spiked peptide identifications
            SpikedResult = Ace.Start(dbOptions, SpikedSamples, false, false);
            SpikedResult.ExportPSMs(1, dbOptions.OutputFolder + "Identifications" + System.IO.Path.DirectorySeparatorChar + "SpikedSamplesPSMs.csv");

            MixedSamples = new Samples(dbOptions);
            for (int i = 0; i < mixedRaws.Length; i++)
            {
                MixedSamples.Add(new Sample(i + 1, 1, 1, mixedRaws[i], mixedRaws[i], 0, ""));
            }

            //Precompute Mixed peptide identifications
            mixedResult = Ace.Start(dbOptions, MixedSamples, false, false);
            if (mixedResult == null)
            {
                conSol.WriteLine("OOPS! No queries could be extracted from the list of mixed spectrum files...");
            }
            else
            {
                mixedResult.ExportPSMs(1, dbOptions.OutputFolder + "Identifications" + System.IO.Path.DirectorySeparatorChar + "MixedSamplesPSMs.csv");

                conSol.WriteLine("Computing gradient descents...");

                //Compute all usable spiked peptides
                characterizedPeptides = CharacterizedPrecursor.GetSpikedPrecursors(SpikedSamples, SpikedResult, dbOptions, nbMinFragments, nbMaxFragments);
                ExportSpikedSampleResult(characterizedPeptides, dbOptions);

                vsCSVWriter writerCumul        = new vsCSVWriter(OutputFolder + "Results.csv");
                string      titleCombined      = "Mixed Sample,Precursor";
                string      curveStr           = "Polynomial Curve,";
                string      spikedIntensityStr = "Area under the curve,";
                foreach (double precursor in characterizedPeptides.Keys)
                {
                    foreach (CharacterizedPrecursor charPrec in characterizedPeptides[precursor].Values)
                    {
                        titleCombined += "," + charPrec.Peptide.Sequence + " Charge " + charPrec.Charge;

                        if (charPrec.eCurveIntensityCount.Coefficients != null && charPrec.eCurveIntensityCount.Coefficients.Length == 3)
                        {
                            curveStr += "," + charPrec.eCurveIntensityCount.Coefficients[0] + "x^2 + " + charPrec.eCurveIntensityCount.Coefficients[1] + "x" + charPrec.eCurveIntensityCount.Coefficients[2];
                        }
                        else
                        {
                            curveStr += ",NA";
                        }

                        spikedIntensityStr += "," + charPrec.eCurveIntensityCount.Area;
                    }
                }
                writerCumul.AddLine(titleCombined);
                writerCumul.AddLine(curveStr);
                writerCumul.AddLine(spikedIntensityStr);

                mixedPrecursors = new Dictionary <Sample, List <MixedPrecursor> >();

                foreach (Sample mixedSample in MixedSamples)
                {
                    mixedPrecursors.Add(mixedSample, MixedPrecursor.GetMixedPrecursors(mixedSample, mixedResult, dbOptions, characterizedPeptides));
                }

                //Get the list of precursors to characterize
                foreach (Sample mixedSample in MixedSamples)
                {
                    foreach (double keyMz in characterizedPeptides.Keys)
                    {
                        List <Dictionary <CharacterizedPrecursor, MaxFlowElutionCurve> > listOfRatios = new List <Dictionary <CharacterizedPrecursor, MaxFlowElutionCurve> >();
                        foreach (MixedPrecursor mPrec in mixedPrecursors[mixedSample])
                        {
                            if (mPrec.MZ == keyMz)
                            {
                                // Compute Max Flow for this precursor
                                Dictionary <CharacterizedPrecursor, MaxFlowElutionCurve> ratios = GetRatios(characterizedPeptides, mPrec, dbOptions, nbMinFragments, nbMaxFragments);
                                listOfRatios.Add(ratios);

                                ExportMixedSampleResult(ratios, mixedSample, mPrec, keyMz, dbOptions);
                            }
                        }

                        bool   isEmpty   = true;
                        string resultStr = vsCSV.GetFileName(mixedSample.sSDF) + "," + keyMz;
                        foreach (double precursor in characterizedPeptides.Keys)
                        {
                            foreach (CharacterizedPrecursor charPrec in characterizedPeptides[precursor].Values)
                            {
                                double cumulArea = 0.0;
                                foreach (Dictionary <CharacterizedPrecursor, MaxFlowElutionCurve> ratios in listOfRatios)
                                {
                                    if (ratios.ContainsKey(charPrec))
                                    {
                                        cumulArea += ratios[charPrec].eCurvePerMs.Area;
                                    }
                                }
                                resultStr += "," + cumulArea;
                                if (cumulArea > 0)
                                {
                                    isEmpty = false;
                                }
                            }
                        }
                        if (!isEmpty)
                        {
                            writerCumul.AddLine(resultStr);
                        }
                    }
                }
                writerCumul.WriteToFile();

                //List Modifications
                Dictionary <Modification, double> dicOfIntensityPerMod = new Dictionary <Modification, double>();
                foreach (Sample sample in mixedPrecursors.Keys)
                {
                    foreach (MixedPrecursor mP in mixedPrecursors[sample])
                    {
                        foreach (CharacterizedPrecursor cP in mP.PeptideRatios.Keys)
                        {
                            if (cP.Peptide.VariableModifications != null)
                            {
                                foreach (Modification mod in cP.Peptide.VariableModifications.Values)
                                {
                                    if (!dicOfIntensityPerMod.ContainsKey(mod))
                                    {
                                        dicOfIntensityPerMod.Add(mod, 0.0);
                                    }
                                }
                            }
                        }
                    }
                }


                //Compute site occupancy for identical sequences (real positionnal isomers)
                vsCSVWriter    writerSitesOccupancy = new vsCSVWriter(OutputFolder + "Results_SiteOccupancy.csv");
                List <Protein> AllProteins          = Ace.ReadProteomeFromFasta(fastaFile, false, dbOptions);
                foreach (Protein protein in AllProteins)
                {
                    string newTitleProtein = protein.Description.Replace(',', ' ') + "," + protein.Sequence;
                    for (int i = 0; i < protein.Sequence.Length; i++)
                    {
                        newTitleProtein += "," + protein[i].ToString();
                    }
                    writerSitesOccupancy.AddLine(newTitleProtein);

                    foreach (Sample mixedSample in mixedPrecursors.Keys)
                    {
                        string coverage = "Coverage," + mixedSample.Name;
                        for (int i = 0; i < protein.Sequence.Length; i++)
                        {
                            double cumulSite = 0.0;
                            newTitleProtein += "," + protein[i].ToString();
                            foreach (MixedPrecursor mP in mixedPrecursors[mixedSample])
                            {
                                foreach (CharacterizedPrecursor cP in mP.PeptideRatios.Keys)
                                {
                                    if (i + 1 >= cP.Peptide.StartResidueNumber && i + 1 <= cP.Peptide.EndResidueNumber)
                                    {
                                        cumulSite += mP.PeptideRatios[cP].eCurvePerMs.Area;
                                    }
                                }
                            }
                            coverage += "," + cumulSite;
                        }
                        writerSitesOccupancy.AddLine(coverage);
                    }

                    foreach (Modification mod in dicOfIntensityPerMod.Keys)
                    {
                        Dictionary <Sample, string> dicOfLines = new Dictionary <Sample, string>();
                        for (int i = 0; i < protein.Sequence.Length; i++)
                        {
                            foreach (Sample mixedSample in mixedPrecursors.Keys)
                            {
                                double cumulModArea = 0.0;
                                foreach (MixedPrecursor mP in mixedPrecursors[mixedSample])
                                {
                                    foreach (CharacterizedPrecursor cP in mP.PeptideRatios.Keys)
                                    {
                                        if (i + 1 >= cP.Peptide.StartResidueNumber && i + 1 <= cP.Peptide.EndResidueNumber &&
                                            cP.Peptide.VariableModifications != null)
                                        {
                                            foreach (int pos in cP.Peptide.VariableModifications.Keys)
                                            {
                                                if (cP.Peptide.StartResidueNumber + pos - 2 == i + 1 && cP.Peptide.VariableModifications[pos] == mod)
                                                {
                                                    cumulModArea += mP.PeptideRatios[cP].eCurvePerMs.Area;
                                                }
                                            }
                                        }
                                    }
                                }
                                if (!dicOfLines.ContainsKey(mixedSample))
                                {
                                    dicOfLines.Add(mixedSample, mod.Description + "," + mixedSample.Name + "," + cumulModArea);
                                }
                                else
                                {
                                    dicOfLines[mixedSample] += "," + cumulModArea;
                                }
                            }
                        }
                        foreach (string line in dicOfLines.Values)
                        {
                            writerSitesOccupancy.AddLine(line);
                        }
                    }
                }
                writerSitesOccupancy.WriteToFile();
            }
        }