public static bool Run()
        {
            //TODO test GPU instead
            DBOptions dbOptions = MhcSample.CreateOptions("");
            Dictionary <string, int> sequences = new Dictionary <string, int>();
            List <Protein>           proteins  = Propheus.ReadProteomeFromFasta(Path.Combine(Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]), "UnitTest", "proteins.fasta"), false, dbOptions);//ETLPAMCNVYYVNCMAPLTE
            string sequence = proteins[0].BaseSequence;

            double[] proteinMasses = new double[sequence.Length];

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

            for (int i = 0; i < sequence.Length; i++)
            {
                for (int j = i + dbOptions.MinimumPeptideLength - 1; j < sequence.Length; j++)
                {
                    int size = j - i + 1;
                    if (size <= dbOptions.MaximumPeptideLength)
                    {
                        string subStr = sequence.Substring(i, j - i + 1);
                        if (!sequences.ContainsKey(subStr))
                        {
                            sequences.Add(subStr, 1);
                        }
                        else
                        {
                            sequences[subStr]++;
                        }

                        double mass = Constants.WATER_MONOISOTOPIC_MASS;
                        for (int k = 0; k < subStr.Length; k++)
                        {
                            mass += AminoAcidMasses.GetMonoisotopicMass(subStr[k]);
                        }
                        precursors.Add(mass);
                    }
                }
                proteinMasses[i] = AminoAcidMasses.GetMonoisotopicMass(sequence[i]);
            }
            precursors.Sort();

            Queries        queries = new Queries(dbOptions, precursors.ToArray());
            Digestion      ps      = new Digestion(dbOptions);
            List <Protein> lProt   = new List <Protein>();

            lProt.Add(proteins[0]);
            //for each protein, build matrix of mass
            //Trinity_Gpu.ProteinDigest pg = new Trinity_Gpu.ProteinDigest(precursors.ToArray(), sequence.Length);
            //Test twice to test that precursor list stays in gpu memory
            for (int iter = 0; iter < 2; iter++)
            {
                Dictionary <string, int> sequencesTmp = new Dictionary <string, int>(sequences);

                foreach (Tuple <Peptide, int> item in ps.DigestProteomeOnTheFlyNoEnzyme(lProt, queries))
                {
                    sequencesTmp[item.Item1.BaseSequence] -= 1;//TODO add modifications
                }

                /*
                 * foreach (Trinity_Gpu.ProteinPrecursorMatch match in pg.Execute(proteinMasses, 0.00005, 10000000))//TODO compute correct tolerance window
                 * {
                 *  int size = match.proteinEndPos - match.proteinStartPos;
                 *  string str = sequence.Substring(match.proteinStartPos, size);
                 *  if (size >= dbOptions.MinimumPeptideLength)
                 *  {
                 *      sequencesTmp[str] -= 1;//TODO add modifications
                 *  }
                 * }//*/

                foreach (int val in sequencesTmp.Values)
                {
                    if (val != 0)
                    {
                        return(false);//*/
                    }
                }
            }
            //pg.Dispose();
            return(true);
        }
Esempio n. 2
0
        public void Solve(string[] spikedRaws, string[] mixedRaws, string fastaFile, string folderToOutputTo, IConSol conSol)
        {
            dbOptions     = CreateOptions(fastaFile, folderToOutputTo, 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 = Propheus.Start(dbOptions, SpikedSamples, false, false, true, 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 = Propheus.Start(dbOptions, MixedSamples, false, false, true, false);
            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, precision);
            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.eCurve.Coefficients != null && charPrec.eCurve.Coefficients.Length == 3)
                    {
                        curveStr += "," + charPrec.eCurve.Coefficients[0] + "x^2 + " + charPrec.eCurve.Coefficients[1] + "x" + charPrec.eCurve.Coefficients[2];
                    }
                    else
                    {
                        curveStr += ",NA";
                    }

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

            //mixedPrecursors = new Dictionary<Sample, Dictionary<double, MixedPrecursor>>();
            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, ElutionCurve> > listOfRatios = new List <Dictionary <CharacterizedPrecursor, ElutionCurve> >();
                    foreach (MixedPrecursor mPrec in mixedPrecursors[mixedSample])
                    {
                        if (mPrec.MZ == keyMz)
                        {
                            // Compute Max Flow for this precursor
                            Dictionary <CharacterizedPrecursor, ElutionCurve> ratios = GetRatios(characterizedPeptides, mPrec);
                            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, ElutionCurve> ratios in listOfRatios)
                            {
                                if (ratios.ContainsKey(charPrec))
                                {
                                    cumulArea += ratios[charPrec].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          = Propheus.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].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].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();
        }